00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "pent_include.h"
00020
00021 #include "SavegameWriter.h"
00022 #include "ODataSource.h"
00023
00024
00025 #include "zip.h"
00026
00027
00028
00029 static voidpf ods_open(voidpf opaque, const char* filename, int mode);
00030 static uLong ods_read(voidpf opaque, voidpf stream, void* buf, uLong size);
00031 static uLong ods_write(voidpf opaque, voidpf stream,
00032 const void* buf, uLong size);
00033 static long ods_tell(voidpf opaque, voidpf stream);
00034 static long ods_seek(voidpf opaque, voidpf stream, uLong offset, int origin);
00035 static int ods_close(voidpf opaque, voidpf stream);
00036 static int ods_error(voidpf opaque, voidpf stream);
00037
00038 PentZip::zlib_filefunc_def ODS_filefunc_templ = {
00039 ods_open, ods_read, ods_write, ods_tell, ods_seek, ods_close, ods_error, 0
00040 };
00041
00042
00043
00044 SavegameWriter::SavegameWriter(ODataSource* ds_)
00045 {
00046 ds = ds_;
00047
00048 PentZip::zlib_filefunc_def filefuncs = ODS_filefunc_templ;
00049 filefuncs.opaque = static_cast<void*>(ds);
00050
00051 PentZip::zipFile zfile = PentZip::zipOpen2("", 0, 0, &filefuncs);
00052 zipfile = static_cast<void*>(zfile);
00053 }
00054
00055 SavegameWriter::~SavegameWriter()
00056 {
00057 if (ds)
00058 delete ds;
00059 ds = 0;
00060 }
00061
00062 bool SavegameWriter::finish()
00063 {
00064 PentZip::zipFile zfile = static_cast<PentZip::zipFile>(zipfile);
00065 zipfile = 0;
00066 if (PentZip::zipClose(zfile, comment.c_str()) != ZIP_OK) return false;
00067
00068 return true;
00069 }
00070
00071
00072 bool SavegameWriter::writeFile(const char* name,
00073 const uint8* data, uint32 size)
00074 {
00075 PentZip::zipFile zfile = static_cast<PentZip::zipFile>(zipfile);
00076 perr << name << ": " << size << std::endl;
00077
00078
00079
00080
00081 VALGRIND_CHECK_READABLE(data, size);
00082
00083 if (PentZip::zipOpenNewFileInZip(zfile, name, 0, 0, 0, 0, 0, 0,
00084 Z_DEFLATED, Z_BEST_COMPRESSION) != ZIP_OK)
00085 return false;
00086
00087 if (PentZip::zipWriteInFileInZip(zfile, data, size) != ZIP_OK)
00088 return false;
00089
00090 if (PentZip::zipCloseFileInZip(zfile) != ZIP_OK)
00091 return false;
00092
00093 return true;
00094 }
00095
00096 bool SavegameWriter::writeFile(const char* name, OAutoBufferDataSource* ods)
00097 {
00098 return writeFile(name, ods->getBuf(), ods->getSize());
00099 }
00100
00101 bool SavegameWriter::writeVersion(uint32 version)
00102 {
00103 uint8 buf[4];
00104 buf[0] = version & 0xFF;
00105 buf[1] = (version >> 8) & 0xFF;
00106 buf[2] = (version >> 16) & 0xFF;
00107 buf[3] = (version >> 24) & 0xFF;
00108 return writeFile("VERSION", buf, 4);
00109 }
00110
00111 bool SavegameWriter::writeDescription(const std::string& desc)
00112 {
00113 comment = desc;
00114 return true;
00115 }
00116
00117
00118
00119
00120 static voidpf ods_open(voidpf opaque, const char* filename, int mode)
00121 {
00122
00123
00124
00125
00126
00127 return opaque;
00128 }
00129
00130 static uLong ods_read(voidpf opaque, voidpf stream, void* buf, uLong size)
00131 {
00132 return 0;
00133 }
00134
00135 static uLong ods_write(voidpf opaque, voidpf stream,
00136 const void* buf, uLong size)
00137 {
00138 ODataSource* ods = static_cast<ODataSource*>(stream);
00139 ods->write(buf, size);
00140 return size;
00141 }
00142
00143 static long ods_tell(voidpf opaque, voidpf stream)
00144 {
00145 ODataSource* ods = static_cast<ODataSource*>(stream);
00146 return ods->getPos();
00147 }
00148
00149 static long ods_seek(voidpf opaque, voidpf stream, uLong offset, int origin)
00150 {
00151 ODataSource* ods = static_cast<ODataSource*>(stream);
00152 switch (origin) {
00153 case ZLIB_FILEFUNC_SEEK_CUR:
00154 ods->skip(offset);
00155 break;
00156 case ZLIB_FILEFUNC_SEEK_END:
00157 ods->seek(ods->getSize()+offset);
00158 break;
00159 case ZLIB_FILEFUNC_SEEK_SET:
00160 ods->seek(offset);
00161 break;
00162 default:
00163 return -1;
00164 }
00165 return 0;
00166 }
00167
00168 static int ods_close(voidpf opaque, voidpf stream)
00169 {
00170 return 0;
00171 }
00172
00173 static int ods_error(voidpf opaque, voidpf stream)
00174 {
00175 return 0;
00176 }
00177