Skip to content

Commit

Permalink
Started adding decompression functionality. Added support for car ass…
Browse files Browse the repository at this point in the history
…ets inside ~/.vdrift, which took a lot of adjustments to get working properly.
  • Loading branch information
joevenzon committed Jul 27, 2011
1 parent 7685fa0 commit 6bc622e
Show file tree
Hide file tree
Showing 28 changed files with 485 additions and 171 deletions.
2 changes: 1 addition & 1 deletion SConstruct
Expand Up @@ -135,7 +135,7 @@ else:
LINKFLAGS = ['-pthread'],
CC = 'gcc', CXX = 'g++',
options = opts)
check_headers = ['asio.hpp', 'boost/bind.hpp', 'GL/gl.h', 'GL/glu.h', 'SDL/SDL.h', 'SDL/SDL_image.h', 'SDL/SDL_rotozoom.h', 'vorbis/vorbisfile.h', 'GL/glew.h', 'curl/curl.h', 'bullet/btBulletCollisionCommon.h']
check_headers = ['asio.hpp', 'boost/bind.hpp', 'GL/gl.h', 'GL/glu.h', 'SDL/SDL.h', 'SDL/SDL_image.h', 'SDL/SDL_rotozoom.h', 'vorbis/vorbisfile.h', 'GL/glew.h', 'curl/curl.h', 'bullet/btBulletCollisionCommon.h', 'archive.h']

if ARGUMENTS.get('verbose') != "1":
env['ARCOMSTR'] = "\tARCH $TARGET"
Expand Down
10 changes: 10 additions & 0 deletions include/archiveutils.h
@@ -0,0 +1,10 @@
#ifndef _ARCHIVE_H
#define _ARCHIVE_H

#include <string>
#include <iostream>

// returns true on success
bool Decompress(const std::string & file, const std::string & output_path, std::ostream & info_output, std::ostream & error_output);

#endif
2 changes: 1 addition & 1 deletion include/gui.h
Expand Up @@ -52,7 +52,7 @@ class GUI
const std::string & languagedir,
const std::string & language,
const std::string & texpath,
const std::string & datapath,
const PATHMANAGER & pathmanager,
const std::string & texsize,
const float screenhwratio,
const std::map <std::string, FONT> & fonts,
Expand Down
3 changes: 2 additions & 1 deletion include/guipage.h
Expand Up @@ -18,6 +18,7 @@ class TEXTUREMANAGER;
class MODELMANAGER;
class CONFIG;
class FONT;
class PATHMANAGER;

class GUIPAGE
{
Expand All @@ -29,7 +30,7 @@ class GUIPAGE
bool Load(
const std::string & path,
const std::string & texpath,
const std::string & datapath,
const PATHMANAGER & pathmanager,
const std::string & texsize,
const float screenhwratio,
const CONFIG & controlsconfig,
Expand Down
38 changes: 18 additions & 20 deletions include/manager.h
Expand Up @@ -4,6 +4,7 @@
#include <map>
#include <string>
#include <iostream>
#include <vector>

#ifdef _MSC_VER
#include <memory>
Expand All @@ -29,26 +30,11 @@ class MANAGER
Clear();
}

void SetBasePath(const std::string & path)
void AddPath(const std::string & path, bool shared = false)
{
basepath = path;
basepaths.push_back(PATH(path, shared));
}

void SetSharedPath(const std::string & path)
{
sharedpath = path;
}

const std::string & GetBasePath() const
{
return basepath;
}

const std::string & GetSharedPath() const
{
return sharedpath;
}


bool Get(const std::string & path, const std::string & name, const_iterator & it)
{
const_iterator i = objects.find(path + "/" + name);
Expand Down Expand Up @@ -129,8 +115,20 @@ class MANAGER

protected:
container objects;
std::string basepath;
std::string sharedpath;
struct PATH
{
std::string path;
bool shared;
std::string GetAssetPath(const std::string & localpath, const std::string & name) const
{
if (shared)
return name;
else
return localpath + "/" + name;
}
PATH(const std::string & path_, bool shared_) : path(path_), shared(shared_) {}
};
std::vector <PATH> basepaths;
std::ostream & error;
};

Expand Down
33 changes: 20 additions & 13 deletions include/pathmanager.h
Expand Up @@ -8,28 +8,30 @@ class PATHMANAGER
{
public:
std::string GetDataPath() const {return data_directory;}
std::string GetSharedCarPath() const {return data_directory+"/carparts";}
std::string GetSharedTrackPath() const {return data_directory+"/trackparts";}
std::string GetWriteableDataPath() const {return settings_path;}
std::string GetSharedCarPath() const {return GetDataPath()+"/carparts";}
std::string GetSharedTrackPath() const {return GetDataPath()+"/trackparts";}
std::string GetStartupFile() const {return settings_path+"/startingup.txt";}
std::string GetTrackRecordsPath() const {return settings_path+"/records"+profile_suffix;}
std::string GetSettingsFile() const {return settings_path+"/VDrift.config"+profile_suffix;}
std::string GetLogFile() const {return settings_path+"/log.txt";}
std::string GetTrackPath() const {return data_directory+"/"+GetTrackDir();}
std::string GetCarPath() const {return data_directory+"/"+GetCarDir();}
std::string GetGUIMenuPath(const std::string & skinname) const {return data_directory+"/skins/"+skinname+"/menus";}
std::string GetSkinPath() const {return data_directory+"/skins/";}
std::string GetOptionsFile() const {return data_directory + "/settings/options.config";}
std::string GetVideoModeFile() const {return data_directory + "/lists/videomodes";}
std::string GetTrackPath() const {return GetDataPath()+"/"+GetTrackDir();}
std::string GetCarPath(const std::string & carname) const;
std::string GetCarPaintPath(const std::string & carname) const {return GetCarPath(carname)+"/skins";}
std::string GetGUIMenuPath(const std::string & skinname) const {return GetDataPath()+"/skins/"+skinname+"/menus";}
std::string GetSkinPath() const {return GetDataPath()+"/skins/";}
std::string GetOptionsFile() const {return GetDataPath() + "/settings/options.config";}
std::string GetVideoModeFile() const {return GetDataPath() + "/lists/videomodes";}
std::string GetCarControlsFile() const {return settings_path+"/controls.config"+profile_suffix;}
std::string GetDefaultCarControlsFile() const {return data_directory+"/settings/controls.config";}
std::string GetDefaultCarControlsFile() const {return GetDataPath()+"/settings/controls.config";}
std::string GetReplayPath() const {return settings_path+"/replays";}
std::string GetScreenshotPath() const {return settings_path+"/screenshots";}
std::string GetStaticReflectionMap() const {return data_directory+"/textures/weather/cubereflection-nosun.png";}
std::string GetStaticAmbientMap() const {return data_directory+"/textures/weather/cubelighting.png";}
std::string GetShaderPath() const {return data_directory + "/shaders";}
std::string GetStaticReflectionMap() const {return GetDataPath()+"/textures/weather/cubereflection-nosun.png";}
std::string GetStaticAmbientMap() const {return GetDataPath()+"/textures/weather/cubelighting.png";}
std::string GetShaderPath() const {return GetDataPath() + "/shaders";}
std::string GetUpdateManagerFile() const {return settings_path+"/updates.config"+profile_suffix;}
std::string GetUpdateManagerFileBackup() const {return settings_path+"/updates.config.backup"+profile_suffix;}
std::string GetUpdateManagerFileBase() const {return data_directory + "/settings/updates.config";}
std::string GetUpdateManagerFileBase() const {return GetDataPath() + "/settings/updates.config";}

std::string GetTrackDir() const {return "tracks";}
std::string GetCarDir() const {return "cars";}
Expand All @@ -41,6 +43,9 @@ class PATHMANAGER
std::string GetHUDTextureDir() const {return "textures/hud";}
std::string GetEffectsTextureDir() const {return "textures/effects";}
std::string GetTireSmokeTextureDir() const {return "textures/smoke";}

std::string GetReadOnlyCarPath() const {return GetDataPath()+"/"+GetCarDir();}
std::string GetWriteableCarPath() const {return GetWriteableDataPath()+"/"+GetCarDir();}

std::string GetTemporaryFolder() const {return temporary_folder;}

Expand All @@ -53,6 +58,8 @@ class PATHMANAGER
void SetProfile(const std::string & value);

void Init(std::ostream & info_output, std::ostream & error_output);

static void MakeDir(const std::string & dir);

private:
std::string home_directory;
Expand Down
1 change: 1 addition & 0 deletions include/track.h
Expand Up @@ -42,6 +42,7 @@ class TRACK
const std::string & trackdir,
const std::string & effects_texturepath,
const std::string & texsize,
const std::string & sharedobjectpath,
const int anisotropy,
const bool reverse,
const bool dynamicobjects,
Expand Down
2 changes: 2 additions & 0 deletions include/trackloader.h
Expand Up @@ -50,6 +50,7 @@ class TRACK::LOADER
const std::string & trackdir,
const std::string & texturedir,
const std::string & texsize,
const std::string & sharedobjectpath,
const int anisotropy,
const bool reverse,
const bool dynamic_shadows,
Expand Down Expand Up @@ -78,6 +79,7 @@ class TRACK::LOADER
const std::string & trackdir;
const std::string & texturedir;
const std::string & texsize;
const std::string & sharedobjectpath;
const int anisotropy;
const bool reverse;
const bool dynamic_objects;
Expand Down
9 changes: 7 additions & 2 deletions include/widget_spinningcar.h
Expand Up @@ -8,6 +8,7 @@

class TEXTUREMANAGER;
class MODELMANAGER;
class PATHMANAGER;

class WIDGET_SPINNINGCAR : public WIDGET
{
Expand All @@ -31,15 +32,17 @@ class WIDGET_SPINNINGCAR : public WIDGET
TEXTUREMANAGER & textures,
MODELMANAGER & models,
const std::string & texturesize,
const std::string & datapath,
const PATHMANAGER & pathmanager,
const float x,
const float y,
const MATHVECTOR <float, 3> & newcarpos,
std::ostream & error_output,
int order = 0);

private:
std::string data;
std::string datarw;
std::string dataro;
std::string dataparts;
std::string tsize;
MATHVECTOR <float, 2> center;
MATHVECTOR <float, 3> carpos;
Expand All @@ -64,6 +67,8 @@ class WIDGET_SPINNINGCAR : public WIDGET
void Unload(SCENENODE & parent);

void Load(SCENENODE & parent);

bool Valid() const {return carnode.valid();}
};

#endif // _WIDGET_SPINNINGCAR_H
3 changes: 2 additions & 1 deletion src/SConscript
Expand Up @@ -17,6 +17,7 @@ def addbulletpath( val ):
src = Split("""aabb.cpp
aabb_space_partitioning.cpp
ai.cpp
archiveutils.cpp
autoupdate.cpp
bezier.cpp
camera_chase.cpp
Expand Down Expand Up @@ -146,7 +147,7 @@ SConsignFile()
#--------------------------#
appdir = ""
vdrift_install = None
common_libs = ['SDL_image', 'SDL_gfx', 'vorbisfile', 'curl']
common_libs = ['SDL_image', 'SDL_gfx', 'vorbisfile', 'curl', 'archive']

if (sys.platform == 'freebsd6') or (sys.platform == 'freebsd7') or (sys.platform == 'freebsd8') or (sys.platform == 'freebsd9'):
common_libs.append('libLinearMath')
Expand Down
83 changes: 83 additions & 0 deletions src/archiveutils.cpp
@@ -0,0 +1,83 @@
#include "archiveutils.h"

#include "pathmanager.h"

#include <archive.h>
#include <archive_entry.h>

#include <fstream>

bool Decompress(const std::string & file, const std::string & output_path, std::ostream & info_output, std::ostream & error_output)
{
const bool verbose = true;

// ensure the output path exists
PATHMANAGER::MakeDir(output_path);

struct archive * a = archive_read_new();
archive_read_support_compression_all(a);
archive_read_support_format_all(a);
int r = archive_read_open_filename(a, file.c_str(), 10240);
if (r != ARCHIVE_OK)
{
error_output << "Unable to open " << file << std::endl;
return false;
}

const int buffsize = 10240;
char buff[buffsize];
struct archive_entry *entry;
while (archive_read_next_header(a, &entry) == ARCHIVE_OK)
{
std::string filename = archive_entry_pathname(entry);
std::string fullpath = output_path + "/" + filename;

bool isdir = (archive_entry_filetype(entry) == AE_IFDIR);

if (verbose)
{
if (isdir)
info_output << "Creating " << filename << std::endl;
else
info_output << "Decompressing " << filename << std::endl;
}

if (isdir)
PATHMANAGER::MakeDir(fullpath);
else
{
std::fstream f(fullpath.c_str(), std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
if (!f)
{
error_output << "Unable to open file for write (permissions issue?): " << fullpath << std::endl;
return false;
}

int size = -1;
while (size != 0)
{
size = archive_read_data(a, buff, buffsize);
if (size < 0)
{
error_output << "Encountered corrupt file: " << filename << std::endl;
return false;
}
else if (size != 0)
{
f.write(buff, size);
}
}
}

archive_read_data_skip(a);
}
r = archive_read_finish(a);
if (r != ARCHIVE_OK)
{
error_output << "Unable to finish read of " << file << std::endl;
return false;
}

info_output << "Successfully decompressed " << file << std::endl;
return true;
}
7 changes: 7 additions & 0 deletions src/autoupdate.cpp
Expand Up @@ -34,6 +34,13 @@ bool AUTOUPDATE::Write(const std::string & path) const
}
}

// now write formats
for (pair_type::const_iterator p = formats.begin(); p != formats.end(); p++)
{
conf.SetParam("formats", p->first, p->second);
}


// write to disk
return conf.Write(true, path);
}
Expand Down

0 comments on commit 6bc622e

Please sign in to comment.