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/GameSave.cpp | |
| 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/GameSave.cpp')
| -rw-r--r-- | src/client/GameSave.cpp | 99 |
1 files changed, 77 insertions, 22 deletions
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; |
