oldloader.cpp

Go to the documentation of this file.
00001 /* $Id: oldloader.cpp 21890 2011-01-22 14:52:20Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "../stdafx.h"
00013 #include "../debug.h"
00014 #include "../strings_type.h"
00015 #include "../string_func.h"
00016 #include "../settings_type.h"
00017 #include "../fileio_func.h"
00018 
00019 #include "table/strings.h"
00020 
00021 #include "saveload_internal.h"
00022 #include "oldloader.h"
00023 
00024 #include <exception>
00025 
00026 static const int TTO_HEADER_SIZE = 41;
00027 static const int TTD_HEADER_SIZE = 49;
00028 
00029 uint32 _bump_assert_value;
00030 
00031 static inline OldChunkType GetOldChunkType(OldChunkType type)     {return (OldChunkType)GB(type, 0, 4);}
00032 static inline OldChunkType GetOldChunkVarType(OldChunkType type)  {return (OldChunkType)(GB(type, 8, 8) << 8);}
00033 static inline OldChunkType GetOldChunkFileType(OldChunkType type) {return (OldChunkType)(GB(type, 16, 8) << 16);}
00034 
00035 static inline byte CalcOldVarLen(OldChunkType type)
00036 {
00037   static const byte type_mem_size[] = {0, 1, 1, 2, 2, 4, 4, 8};
00038   byte length = GB(type, 8, 8);
00039   assert(length != 0 && length < lengthof(type_mem_size));
00040   return type_mem_size[length];
00041 }
00042 
00048 static byte ReadByteFromFile(LoadgameState *ls)
00049 {
00050   /* To avoid slow reads, we read BUFFER_SIZE of bytes per time
00051   and just return a byte per time */
00052   if (ls->buffer_cur >= ls->buffer_count) {
00053 
00054     /* Read some new bytes from the file */
00055     int count = (int)fread(ls->buffer, 1, BUFFER_SIZE, ls->file);
00056 
00057     /* We tried to read, but there is nothing in the file anymore.. */
00058     if (count == 0) {
00059       DEBUG(oldloader, 0, "Read past end of file, loading failed");
00060       throw std::exception();
00061     }
00062 
00063     ls->buffer_count = count;
00064     ls->buffer_cur   = 0;
00065   }
00066 
00067   return ls->buffer[ls->buffer_cur++];
00068 }
00069 
00075 byte ReadByte(LoadgameState *ls)
00076 {
00077   /* Old savegames have a nice compression algorithm (RLE)
00078   which means that we have a chunk, which starts with a length
00079   byte. If that byte is negative, we have to repeat the next byte
00080   that many times ( + 1). Else, we need to read that amount of bytes.
00081   Works pretty good if you have many zero's behind eachother */
00082 
00083   if (ls->chunk_size == 0) {
00084     /* Read new chunk */
00085     int8 new_byte = ReadByteFromFile(ls);
00086 
00087     if (new_byte < 0) {
00088       /* Repeat next char for new_byte times */
00089       ls->decoding    = true;
00090       ls->decode_char = ReadByteFromFile(ls);
00091       ls->chunk_size  = -new_byte + 1;
00092     } else {
00093       ls->decoding    = false;
00094       ls->chunk_size  = new_byte + 1;
00095     }
00096   }
00097 
00098   ls->total_read++;
00099   ls->chunk_size--;
00100 
00101   return ls->decoding ? ls->decode_char : ReadByteFromFile(ls);
00102 }
00103 
00109 bool LoadChunk(LoadgameState *ls, void *base, const OldChunks *chunks)
00110 {
00111   byte *base_ptr = (byte*)base;
00112 
00113   for (const OldChunks *chunk = chunks; chunk->type != OC_END; chunk++) {
00114     if (((chunk->type & OC_TTD) && _savegame_type == SGT_TTO) ||
00115         ((chunk->type & OC_TTO) && _savegame_type != SGT_TTO)) {
00116       /* TTD(P)-only chunk, but TTO savegame || TTO-only chunk, but TTD/TTDP savegame */
00117       continue;
00118     }
00119 
00120     byte *ptr = (byte*)chunk->ptr;
00121     if (chunk->type & OC_DEREFERENCE_POINTER) ptr = *(byte**)ptr;
00122 
00123     for (uint i = 0; i < chunk->amount; i++) {
00124       /* Handle simple types */
00125       if (GetOldChunkType(chunk->type) != 0) {
00126         switch (GetOldChunkType(chunk->type)) {
00127           /* Just read the byte and forget about it */
00128           case OC_NULL: ReadByte(ls); break;
00129 
00130           case OC_CHUNK:
00131             /* Call function, with 'i' as parameter to tell which item we
00132              * are going to read */
00133             if (!chunk->proc(ls, i)) return false;
00134             break;
00135 
00136           case OC_ASSERT:
00137             DEBUG(oldloader, 4, "Assert point: 0x%X / 0x%X", ls->total_read, chunk->offset + _bump_assert_value);
00138             if (ls->total_read != chunk->offset + _bump_assert_value) throw std::exception();
00139           default: break;
00140         }
00141       } else {
00142         uint64 res = 0;
00143 
00144         /* Reading from the file: bits 16 to 23 have the FILE type */
00145         switch (GetOldChunkFileType(chunk->type)) {
00146           case OC_FILE_I8:  res = (int8)ReadByte(ls); break;
00147           case OC_FILE_U8:  res = ReadByte(ls); break;
00148           case OC_FILE_I16: res = (int16)ReadUint16(ls); break;
00149           case OC_FILE_U16: res = ReadUint16(ls); break;
00150           case OC_FILE_I32: res = (int32)ReadUint32(ls); break;
00151           case OC_FILE_U32: res = ReadUint32(ls); break;
00152           default: NOT_REACHED();
00153         }
00154 
00155         /* When both pointers are NULL, we are just skipping data */
00156         if (base_ptr == NULL && chunk->ptr == NULL) continue;
00157 
00158         /* Writing to the var: bits 8 to 15 have the VAR type */
00159         if (chunk->ptr == NULL) ptr = base_ptr + chunk->offset;
00160 
00161         /* Write the data */
00162         switch (GetOldChunkVarType(chunk->type)) {
00163           case OC_VAR_I8: *(int8  *)ptr = GB(res, 0, 8); break;
00164           case OC_VAR_U8: *(uint8 *)ptr = GB(res, 0, 8); break;
00165           case OC_VAR_I16:*(int16 *)ptr = GB(res, 0, 16); break;
00166           case OC_VAR_U16:*(uint16*)ptr = GB(res, 0, 16); break;
00167           case OC_VAR_I32:*(int32 *)ptr = res; break;
00168           case OC_VAR_U32:*(uint32*)ptr = res; break;
00169           case OC_VAR_I64:*(int64 *)ptr = res; break;
00170           case OC_VAR_U64:*(uint64*)ptr = res; break;
00171           default: NOT_REACHED();
00172         }
00173 
00174         /* Increase pointer base for arrays when looping */
00175         if (chunk->amount > 1 && chunk->ptr != NULL) ptr += CalcOldVarLen(chunk->type);
00176       }
00177     }
00178   }
00179 
00180   return true;
00181 }
00182 
00188 static void InitLoading(LoadgameState *ls)
00189 {
00190   ls->chunk_size   = 0;
00191   ls->total_read   = 0;
00192 
00193   ls->decoding     = false;
00194   ls->decode_char  = 0;
00195 
00196   ls->buffer_cur   = 0;
00197   ls->buffer_count = 0;
00198   memset(ls->buffer, 0, BUFFER_SIZE);
00199 
00200   _bump_assert_value = 0;
00201 
00202   _settings_game.construction.freeform_edges = false; // disable so we can convert map array (SetTileType is still used)
00203 }
00204 
00212 static bool VerifyOldNameChecksum(char *title, uint len)
00213 {
00214   uint16 sum = 0;
00215   for (uint i = 0; i < len - 2; i++) {
00216     sum += title[i];
00217     sum = ROL(sum, 1);
00218   }
00219 
00220   sum ^= 0xAAAA; // computed checksum
00221 
00222   uint16 sum2 = title[len - 2]; // checksum in file
00223   SB(sum2, 8, 8, title[len - 1]);
00224 
00225   return sum == sum2;
00226 }
00227 
00228 static inline bool CheckOldSavegameType(FILE *f, char *temp, const char *last, uint len)
00229 {
00230   assert(last - temp + 1 >= (int)len);
00231 
00232   if (fread(temp, 1, len, f) != len) {
00233     temp[0] = '\0'; // if reading failed, make the name empty
00234     return false;
00235   }
00236 
00237   bool ret = VerifyOldNameChecksum(temp, len);
00238   temp[len - 2] = '\0'; // name is nul-terminated in savegame, but it's better to be sure
00239   str_validate(temp, last);
00240 
00241   return ret;
00242 }
00243 
00244 static SavegameType DetermineOldSavegameType(FILE *f, char *title, const char *last)
00245 {
00246   assert_compile(TTD_HEADER_SIZE >= TTO_HEADER_SIZE);
00247   char temp[TTD_HEADER_SIZE];
00248 
00249   SavegameType type = SGT_TTO;
00250 
00251   /* Can't fseek to 0 as in tar files that is not correct */
00252   long pos = ftell(f);
00253   if (!CheckOldSavegameType(f, temp, lastof(temp), TTO_HEADER_SIZE)) {
00254     type = SGT_TTD;
00255     fseek(f, pos, SEEK_SET);
00256     if (!CheckOldSavegameType(f, temp, lastof(temp), TTD_HEADER_SIZE)) {
00257       type = SGT_INVALID;
00258     }
00259   }
00260 
00261   if (title != NULL) {
00262     switch (type) {
00263       case SGT_TTO: title = strecpy(title, "(TTO) ", last);    break;
00264       case SGT_TTD: title = strecpy(title, "(TTD) ", last);    break;
00265       default:      title = strecpy(title, "(broken) ", last); break;
00266     }
00267     title = strecpy(title, temp, last);
00268   }
00269 
00270   return type;
00271 }
00272 
00273 typedef bool LoadOldMainProc(LoadgameState *ls);
00274 
00275 bool LoadOldSaveGame(const char *file)
00276 {
00277   LoadgameState ls;
00278 
00279   DEBUG(oldloader, 3, "Trying to load a TTD(Patch) savegame");
00280 
00281   InitLoading(&ls);
00282 
00283   /* Open file */
00284   ls.file = FioFOpenFile(file, "rb");
00285 
00286   if (ls.file == NULL) {
00287     DEBUG(oldloader, 0, "Cannot open file '%s'", file);
00288     return false;
00289   }
00290 
00291   SavegameType type = DetermineOldSavegameType(ls.file, NULL, NULL);
00292 
00293   LoadOldMainProc *proc = NULL;
00294 
00295   switch (type) {
00296     case SGT_TTO: proc = &LoadTTOMain; break;
00297     case SGT_TTD: proc = &LoadTTDMain; break;
00298     default: break;
00299   }
00300 
00301   _savegame_type = type;
00302 
00303   bool game_loaded;
00304   try {
00305     game_loaded = proc != NULL && proc(&ls);
00306   } catch (...) {
00307     game_loaded = false;
00308   }
00309 
00310   if (!game_loaded) {
00311     SetSaveLoadError(STR_GAME_SAVELOAD_ERROR_DATA_INTEGRITY_CHECK_FAILED);
00312     fclose(ls.file);
00313     return false;
00314   }
00315 
00316   _pause_mode = 2;
00317 
00318   return true;
00319 }
00320 
00321 void GetOldSaveGameName(const char *file, char *title, const char *last)
00322 {
00323   FILE *f = FioFOpenFile(file, "rb");
00324 
00325   if (f == NULL) {
00326     *title = '\0';
00327     return;
00328   }
00329 
00330   DetermineOldSavegameType(f, title, last);
00331 
00332   fclose(f);
00333 }

Generated on Fri Mar 18 23:17:37 2011 for OpenTTD by  doxygen 1.6.1