diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-07-27 19:06:17 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-07-27 19:06:17 (GMT) |
| commit | 5befe5c25f8f188e7588de44ab2c8bead22ae999 (patch) | |
| tree | 643b02af217770c1a3156be03e01442557795760 /src/client | |
| parent | f8ca8af387b8611c18ca7c5357efd19c8bc28941 (diff) | |
| download | powder-5befe5c25f8f188e7588de44ab2c8bead22ae999.zip powder-5befe5c25f8f188e7588de44ab2c8bead22ae999.tar.gz | |
Local file browser + some more interesting things like Progress bar UI component
Diffstat (limited to 'src/client')
| -rw-r--r-- | src/client/Client.cpp | 101 | ||||
| -rw-r--r-- | src/client/Client.h | 5 | ||||
| -rw-r--r-- | src/client/GameSave.cpp | 99 | ||||
| -rw-r--r-- | src/client/GameSave.h | 3 | ||||
| -rw-r--r-- | src/client/SaveFile.cpp | 26 | ||||
| -rw-r--r-- | src/client/SaveFile.h | 5 |
6 files changed, 210 insertions, 29 deletions
diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 2e74c48..a9661ca 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -22,13 +22,19 @@ #include "Misc.h" #include "interface/Point.h" - #include "client/SaveInfo.h" - #include "ClientListener.h" - #include "Update.h" +extern "C" +{ +#if defined(WIN32) && !defined(__GNUC__) +#include <io.h> +#else +#include <dirent.h> +#endif +} + Client::Client(): authUser(0, ""), updateAvailable(false) @@ -117,6 +123,95 @@ Client::Client(): versionCheckRequest = http_async_req_start(NULL, SERVER "/Download/Version.json", NULL, 0, 1); } +std::vector<std::string> Client::DirectorySearch(std::string directory, std::string search, std::string extension) +{ + std::vector<std::string> extensions; + extensions.push_back(extension); + return DirectorySearch(directory, search, extensions); +} + +std::vector<std::string> Client::DirectorySearch(std::string directory, std::string search, std::vector<std::string> extensions) +{ + std::vector<std::string> results; + + //Get full file listing + std::vector<std::string> directoryList; +#if defined(WIN32) && !defined(__GNUC__) + //Windows + struct _finddata_t currentFile; + intptr_t findFileHandle; + std::string fileMatch = directory + "*.*"; + findFileHandle = _findfirst(fileMatch.c_str(), ¤tFile); + if (findFileHandle == -1L) + { + printf("Unable to open directory\n"); + return std::vector<std::string>(); + } + do + { + std::string currentFileName = std::string(currentFile.name); + if(currentFileName.length()>4) + directoryList.push_back(directory+currentFileName); + } + while (_findnext(findFileHandle, ¤tFile) == 0); + _findclose(findFileHandle); +#else + //Linux or MinGW + struct dirent * directoryEntry; + DIR *directoryHandle = opendir(directory.c_str()); + if(!directoryHandle) + { + printf("Unable to open directory\n"); + return std::vector<std::string>(); + } + while(directoryEntry = readdir(directoryHandle)) + { + std::string currentFileName = std::string(directoryEntry->d_name); + if(currentFileName.length()>4) + directoryList.push_back(directory+currentFileName); + } + closedir(directoryHandle); +#endif + + //Filter results + return directoryList; + return results; +} + +std::vector<unsigned char> Client::ReadFile(std::string filename) +{ + try + { + std::ifstream fileStream; + fileStream.open(string(filename).c_str(), ios::binary); + if(fileStream.is_open()) + { + fileStream.seekg(0, ios::end); + size_t fileSize = fileStream.tellg(); + fileStream.seekg(0); + + unsigned char * tempData = new unsigned char[fileSize]; + fileStream.read((char *)tempData, fileSize); + fileStream.close(); + + std::vector<unsigned char> fileData; + fileData.insert(fileData.end(), tempData, tempData+fileSize); + delete[] tempData; + + return fileData; + } + else + { + return std::vector<unsigned char>(); + } + } + catch(std::exception & e) + { + std::cerr << "Readfile: " << e.what() << std::endl; + throw; + } +} + void Client::Tick() { //Check status on version check request diff --git a/src/client/Client.h b/src/client/Client.h index 7b94393..d7e1afb 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -81,6 +81,11 @@ public: Client(); ~Client(); + std::vector<std::string> DirectorySearch(std::string directory, std::string search, std::vector<std::string> extensions); + std::vector<std::string> DirectorySearch(std::string directory, std::string search, std::string extension); + + std::vector<unsigned char> ReadFile(std::string filename); + void AddListener(ClientListener * listener); void RemoveListener(ClientListener * listener); diff --git a/src/client/GameSave.cpp b/src/client/GameSave.cpp index 1243ea0..f11b443 100644 --- a/src/client/GameSave.cpp +++ b/src/client/GameSave.cpp @@ -7,6 +7,7 @@ // #include <iostream> +#include <sstream> #include <bzlib.h> #include "Config.h" #include "bson/BSON.h" @@ -56,6 +57,50 @@ GameSave::GameSave(int width, int height) setSize(width, height); } +GameSave::GameSave(std::vector<char> data) +{ + blockWidth = 0; + blockHeight = 0; + + blockMap = NULL; + blockMapPtr = NULL; + fanVelX = NULL; + fanVelXPtr = NULL; + fanVelY = NULL; + fanVelYPtr = NULL; + particles = NULL; + + try{ + read(&data[0], data.size()); + } catch (ParseException& e) { + std::cout << e.what() << std::endl; + this->~GameSave(); //Free any allocated memory + throw; + } +} + +GameSave::GameSave(std::vector<unsigned char> data) +{ + blockWidth = 0; + blockHeight = 0; + + blockMap = NULL; + blockMapPtr = NULL; + fanVelX = NULL; + fanVelXPtr = NULL; + fanVelY = NULL; + fanVelYPtr = NULL; + particles = NULL; + + try{ + read((char*)(&data[0]), data.size()); + } catch (ParseException& e) { + std::cout << e.what() << std::endl; + this->~GameSave(); //Free any allocated memory + throw; + } +} + GameSave::GameSave(char * data, int dataSize) { blockWidth = 0; @@ -69,31 +114,36 @@ GameSave::GameSave(char * data, int dataSize) fanVelYPtr = NULL; particles = NULL; - try { - if(dataSize > 0) + try{ + read(data, dataSize); + } catch (ParseException& e) { + std::cout << e.what() << std::endl; + this->~GameSave(); //Free any allocated memory + throw; + } +} + +void GameSave::read(char * data, int dataSize) +{ + if(dataSize > 0) + { + if(data[0] == 0x50 || data[0] == 0x66) { - if(data[0] == 0x50 || data[0] == 0x66) - { - readPSv(data, dataSize); - } - else if(data[0] == 'O') - { - readOPS(data, dataSize); - } - else - { - std::cerr << "Got Magic number '" << data[0] << "'" << std::endl; - throw ParseException(ParseException::Corrupt, "Invalid save format"); - } + readPSv(data, dataSize); + } + else if(data[0] == 'O') + { + readOPS(data, dataSize); } else { - throw ParseException(ParseException::Corrupt, "No data"); + std::cerr << "Got Magic number '" << data[0] << "'" << std::endl; + throw ParseException(ParseException::Corrupt, "Invalid save format"); } - } catch (ParseException& e) { - std::cout << e.what() << std::endl; - this->~GameSave(); //Free any allocated memory - throw; + } + else + { + throw ParseException(ParseException::Corrupt, "No data"); } } @@ -895,8 +945,13 @@ void GameSave::readPSv(char * data, int dataLength) setSize(bw, bh); - if (BZ2_bzBuffToBuffDecompress((char *)d, (unsigned *)&i, (char *)(c+12), dataLength-12, 0, 0)) - throw ParseException(ParseException::Corrupt, "Cannot decompress"); + int bzStatus = 0; + if (bzStatus = BZ2_bzBuffToBuffDecompress((char *)d, (unsigned *)&i, (char *)(c+12), dataLength-12, 0, 0)) + { + std::stringstream bzStatusStr; + bzStatusStr << bzStatus; + throw ParseException(ParseException::Corrupt, "Cannot decompress: " + bzStatusStr.str()); + } dataLength = i; std::cout << "Parsing " << dataLength << " bytes of data, version " << ver << std::endl; diff --git a/src/client/GameSave.h b/src/client/GameSave.h index 1e5b514..a1bd1cf 100644 --- a/src/client/GameSave.h +++ b/src/client/GameSave.h @@ -59,6 +59,8 @@ public: GameSave(GameSave & save); GameSave(int width, int height); GameSave(char * data, int dataSize); + GameSave(std::vector<char> data); + GameSave(std::vector<unsigned char> data); ~GameSave(); void setSize(int width, int height); char * Serialise(int & dataSize); @@ -85,6 +87,7 @@ private: float * fanVelYPtr; unsigned char * blockMapPtr; + void read(char * data, int dataSize); void readOPS(char * data, int dataLength); void readPSv(char * data, int dataLength); char * serialiseOPS(int & dataSize); diff --git a/src/client/SaveFile.cpp b/src/client/SaveFile.cpp index 6675aae..7c35466 100644 --- a/src/client/SaveFile.cpp +++ b/src/client/SaveFile.cpp @@ -6,19 +6,35 @@ */ #include "SaveFile.h" +#include "Client.h" + #include "search/Thumbnail.h" SaveFile::SaveFile(SaveFile & save): - gameSave(NULL) + gameSave(NULL), + thumbnail(NULL) { if(save.gameSave) gameSave = new GameSave(*save.gameSave); + if(save.thumbnail) + thumbnail = new Thumbnail(*save.thumbnail); +} + +Thumbnail * SaveFile::GetThumbnail() +{ + return thumbnail; +} + +void SaveFile::SetThumbnail(Thumbnail * thumb) +{ + thumbnail = thumb; } SaveFile::SaveFile(string filename): filename(filename), - gameSave(NULL) - { - //Load file + gameSave(NULL), + thumbnail(NULL) +{ + } GameSave * SaveFile::GetGameSave() @@ -39,5 +55,7 @@ string SaveFile::GetName() SaveFile::~SaveFile() { if(gameSave) delete gameSave; + if(thumbnail) + delete thumbnail; } diff --git a/src/client/SaveFile.h b/src/client/SaveFile.h index 8b4005a..54a9290 100644 --- a/src/client/SaveFile.h +++ b/src/client/SaveFile.h @@ -11,19 +11,24 @@ #include <string> #include "GameSave.h" + using namespace std; +class Thumbnail; class SaveFile { public: SaveFile(SaveFile & save); SaveFile(string filename); + Thumbnail * GetThumbnail(); GameSave * GetGameSave(); + void SetThumbnail(Thumbnail * thumb); void SetGameSave(GameSave * save); string GetName(); virtual ~SaveFile(); private: + Thumbnail * thumbnail; GameSave * gameSave; string filename; }; |
