# oldloader.cpp

Go to the documentation of this file.

    00001 /* $Id: oldloader.cpp 17693 2009-10-04 17:16:41Z 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 "../openttd.h"
    00014 #include "../debug.h"
    00015 #include "../strings_type.h"
    00016 #include "../string_func.h"
    00017 #include "../settings_type.h"
    00018 #include "../fileio_func.h"
    00019
    00020 #include "table/strings.h"
    00021
    00022 #include "saveload_internal.h"
    00023 #include "oldloader.h"
    00024
    00025 enum {
    00026   TTO_HEADER_SIZE = 41,
    00027   TTD_HEADER_SIZE = 49,
    00028 };
    00029
    00030 uint32 _bump_assert_value;
    00031
    00032 static inline OldChunkType GetOldChunkType(OldChunkType type)     {return (OldChunkType)GB(type, 0, 4);}
    00033 static inline OldChunkType GetOldChunkVarType(OldChunkType type)  {return (OldChunkType)(GB(type, 8, 8) << 8);}
    00034 static inline OldChunkType GetOldChunkFileType(OldChunkType type) {return (OldChunkType)(GB(type, 16, 8) << 16);}
    00035
    00036 static inline byte CalcOldVarLen(OldChunkType type)
    00037 {
    00038   static const byte type_mem_size[] = {0, 1, 1, 2, 2, 4, 4, 8};
    00039   byte length = GB(type, 8, 8);
    00040   assert(length != 0 && length < lengthof(type_mem_size));
    00041   return type_mem_size[length];
    00042 }
    00043
    00049 static byte ReadByteFromFile(LoadgameState *ls)
    00050 {
    00051   /* To avoid slow reads, we read BUFFER_SIZE of bytes per time
    00052   and just return a byte per time */
    00053   if (ls->buffer_cur >= ls->buffer_count) {
    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       ls->failed = true;
    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       if (ls->failed) return false;
    00125
    00126       /* Handle simple types */
    00127       if (GetOldChunkType(chunk->type) != 0) {
    00128         switch (GetOldChunkType(chunk->type)) {
    00129           /* Just read the byte and forget about it */
    00130           case OC_NULL: ReadByte(ls); break;
    00131
    00132           case OC_CHUNK:
    00133             /* Call function, with 'i' as parameter to tell which item we
    00134              * are going to read */
    00135             if (!chunk->proc(ls, i)) return false;
    00136             break;
    00137
    00138           case OC_ASSERT:
    00139             DEBUG(oldloader, 4, "Assert point: 0x%X / 0x%X", ls->total_read, chunk->offset + _bump_assert_value);
    00140             if (ls->total_read != chunk->offset + _bump_assert_value) ls->failed = true;
    00141           default: break;
    00142         }
    00143       } else {
    00144         uint64 res = 0;
    00145
    00146         /* Reading from the file: bits 16 to 23 have the FILE type */
    00147         switch (GetOldChunkFileType(chunk->type)) {
    00148           case OC_FILE_I8:  res = (int8)ReadByte(ls); break;
    00149           case OC_FILE_U8:  res = ReadByte(ls); break;
    00150           case OC_FILE_I16: res = (int16)ReadUint16(ls); break;
    00151           case OC_FILE_U16: res = ReadUint16(ls); break;
    00152           case OC_FILE_I32: res = (int32)ReadUint32(ls); break;
    00153           case OC_FILE_U32: res = ReadUint32(ls); break;
    00154           default: NOT_REACHED();
    00155         }
    00156
    00157         /* When both pointers are NULL, we are just skipping data */
    00158         if (base_ptr == NULL && chunk->ptr == NULL) continue;
    00159
    00160         /* Writing to the var: bits 8 to 15 have the VAR type */
    00161         if (chunk->ptr == NULL) ptr = base_ptr + chunk->offset;
    00162
    00163         /* Write the data */
    00164         switch (GetOldChunkVarType(chunk->type)) {
    00165           case OC_VAR_I8: *(int8  *)ptr = GB(res, 0, 8); break;
    00166           case OC_VAR_U8: *(uint8 *)ptr = GB(res, 0, 8); break;
    00167           case OC_VAR_I16:*(int16 *)ptr = GB(res, 0, 16); break;
    00168           case OC_VAR_U16:*(uint16*)ptr = GB(res, 0, 16); break;
    00169           case OC_VAR_I32:*(int32 *)ptr = res; break;
    00170           case OC_VAR_U32:*(uint32*)ptr = res; break;
    00171           case OC_VAR_I64:*(int64 *)ptr = res; break;
    00172           case OC_VAR_U64:*(uint64*)ptr = res; break;
    00173           default: NOT_REACHED();
    00174         }
    00175
    00176         /* Increase pointer base for arrays when looping */
    00177         if (chunk->amount > 1 && chunk->ptr != NULL) ptr += CalcOldVarLen(chunk->type);
    00178       }
    00179     }
    00180   }
    00181
    00182   return true;
    00183 }
    00184
    00190 static void InitLoading(LoadgameState *ls)
    00191 {
    00192   ls->chunk_size   = 0;
    00193   ls->total_read   = 0;
    00194   ls->failed       = false;
    00195
    00196   ls->decoding     = false;
    00197   ls->decode_char  = 0;
    00198
    00199   ls->buffer_cur   = 0;
    00200   ls->buffer_count = 0;
    00201   memset(ls->buffer, 0, BUFFER_SIZE);
    00202
    00203   _bump_assert_value = 0;
    00204
    00205   _settings_game.construction.freeform_edges = false; // disable so we can convert map array (SetTileType is still used)
    00206 }
    00207
    00215 static bool VerifyOldNameChecksum(char *title, uint len)
    00216 {
    00217   uint16 sum = 0;
    00218   for (uint i = 0; i < len - 2; i++) {
    00219     sum += title[i];
    00220     sum = ROL(sum, 1);
    00221   }
    00222
    00223   sum ^= 0xAAAA; // computed checksum
    00224
    00225   uint16 sum2 = title[len - 2]; // checksum in file
    00226   SB(sum2, 8, 8, title[len - 1]);
    00227
    00228   return sum == sum2;
    00229 }
    00230
    00231 static inline bool CheckOldSavegameType(FILE *f, char *temp, const char *last, uint len)
    00232 {
    00233   assert(last - temp + 1 >= (int)len);
    00234
    00235   if (fread(temp, 1, len, f) != len) {
    00236     temp[0] = '\0'; // if reading failed, make the name empty
    00237     return false;
    00238   }
    00239
    00240   bool ret = VerifyOldNameChecksum(temp, len);
    00241   temp[len - 2] = '\0'; // name is nul-terminated in savegame, but it's better to be sure
    00242   str_validate(temp, last);
    00243
    00244   return ret;
    00245 }
    00246
    00247 static SavegameType DetermineOldSavegameType(FILE *f, char *title, const char *last)
    00248 {
    00249   assert_compile(TTD_HEADER_SIZE >= TTO_HEADER_SIZE);
    00250   char temp[TTD_HEADER_SIZE];
    00251
    00252   SavegameType type = SGT_TTO;
    00253
    00254   /* Can't fseek to 0 as in tar files that is not correct */
    00255   long pos = ftell(f);
    00256   if (!CheckOldSavegameType(f, temp, lastof(temp), TTO_HEADER_SIZE)) {
    00257     type = SGT_TTD;
    00258     fseek(f, pos, SEEK_SET);
    00259     if (!CheckOldSavegameType(f, temp, lastof(temp), TTD_HEADER_SIZE)) {
    00260       type = SGT_INVALID;
    00261     }
    00262   }
    00263
    00264   if (title != NULL) {
    00265     switch (type) {
    00266       case SGT_TTO: title = strecpy(title, "(TTO) ", last);    break;
    00267       case SGT_TTD: title = strecpy(title, "(TTD) ", last);    break;
    00268       default:      title = strecpy(title, "(broken) ", last); break;
    00269     }
    00270     title = strecpy(title, temp, last);
    00271   }
    00272
    00273   return type;
    00274 }
    00275
    00276 typedef bool LoadOldMainProc(LoadgameState *ls);
    00277
    00278 bool LoadOldSaveGame(const char *file)
    00279 {
    00280   LoadgameState ls;
    00281
    00282   DEBUG(oldloader, 3, "Trying to load a TTD(Patch) savegame");
    00283
    00284   InitLoading(&ls);
    00285
    00286   /* Open file */
    00287   ls.file = FioFOpenFile(file, "rb");
    00288
    00289   if (ls.file == NULL) {
    00290     DEBUG(oldloader, 0, "Cannot open file '%s'", file);
    00291     return false;
    00292   }
    00293
    00294   SavegameType type = DetermineOldSavegameType(ls.file, NULL, NULL);
    00295
    00296   LoadOldMainProc *proc = NULL;
    00297
    00298   switch (type) {
    00299     case SGT_TTO: proc = &LoadTTOMain; break;
    00300     case SGT_TTD: proc = &LoadTTDMain; break;
    00301     default: break;
    00302   }
    00303
    00304   _savegame_type = type;
    00305
    00306   if (proc == NULL || !proc(&ls)) {
    00307     SetSaveLoadError(STR_GAME_SAVELOAD_ERROR_DATA_INTEGRITY_CHECK_FAILED);
    00308     fclose(ls.file);
    00309     return false;
    00310   }
    00311
    00312   _pause_mode = 2;
    00313
    00314   return true;
    00315 }
    00316
    00317 void GetOldSaveGameName(const char *file, char *title, const char *last)
    00318 {
    00319   FILE *f = FioFOpenFile(file, "rb");
    00320
    00321   if (f == NULL) {
    00322     *title = '\0';
    00323     return;
    00324   }
    00325
    00326   DetermineOldSavegameType(f, title, last);
    00327
    00328   fclose(f);
    00329 }