smatz@10824: /* $Id$ */ smatz@10824: smatz@10824: /** @file gamelog.cpp Definition of functions used for logging of important changes in the game */ smatz@10824: smatz@10824: #include "stdafx.h" smatz@10824: #include "openttd.h" smatz@10824: #include "saveload.h" smatz@10824: #include "core/alloc_func.hpp" smatz@10824: #include "core/bitmath_func.hpp" smatz@10824: #include "core/math_func.hpp" smatz@10824: #include "network/core/config.h" smatz@10824: #include "variables.h" smatz@10824: #include "string_func.h" smatz@10824: #include "settings_type.h" smatz@10824: #include "newgrf_config.h" smatz@10824: #include smatz@10824: #include smatz@10824: #include "gamelog.h" smatz@10824: #include "console_func.h" smatz@10824: #include "debug.h" smatz@10824: #include "rev.h" smatz@10824: smatz@10824: extern const uint16 SAVEGAME_VERSION; ///< current savegame version smatz@10824: smatz@10824: extern SavegameType _savegame_type; ///< type of savegame we are loading smatz@10824: smatz@10824: extern uint32 _ttdp_version; ///< version of TTDP savegame (if applicable) smatz@10824: extern uint16 _sl_version; ///< the major savegame version identifier smatz@10824: extern byte _sl_minor_version; ///< the minor savegame version, DO NOT USE! smatz@10824: smatz@10824: /** Type of logged change */ smatz@10824: enum GamelogChangeType { smatz@10824: GLCT_MODE, ///< Scenario editor x Game, different landscape smatz@10824: GLCT_REVISION, ///< Changed game revision string smatz@10824: GLCT_OLDVER, ///< Loaded from savegame without logged data smatz@10824: GLCT_PATCH, ///< Non-networksafe patch value changed smatz@10824: GLCT_GRFADD, ///< Removed GRF smatz@10824: GLCT_GRFREM, ///< Added GRF smatz@10824: GLCT_GRFCOMPAT, ///< Loading compatible GRF smatz@10824: GLCT_GRFPARAM, ///< GRF parameter changed smatz@10824: GLCT_GRFMOVE, ///< GRF order changed smatz@10824: GLCT_END, ///< So we know how many GLCTs are there smatz@10824: GLCT_NONE = 0xFF, ///< In savegames, end of list smatz@10824: }; smatz@10824: smatz@10824: smatz@10824: /** Contains information about one logged change */ smatz@10824: struct LoggedChange { smatz@10824: GamelogChangeType ct; ///< Type of change logged in this struct smatz@10824: union { smatz@10824: struct { smatz@10824: byte mode; ///< new game mode - Editor x Game smatz@10824: byte landscape; ///< landscape (temperate, arctic, ...) smatz@10824: } mode; smatz@10824: struct { smatz@10824: char text[NETWORK_REVISION_LENGTH]; ///< revision string, _openttd_revision smatz@10824: uint32 newgrf; ///< _openttd_newgrf_version smatz@10824: uint16 slver; ///< _sl_version smatz@10824: byte modified; ///< _openttd_revision_modified smatz@10824: } revision; smatz@10824: struct { smatz@10824: uint32 type; ///< type of savegame, @see SavegameType smatz@10824: uint32 version; ///< major and minor version OR ttdp version smatz@10824: } oldver; smatz@10824: GRFIdentifier grfadd; ///< ID and md5sum of added GRF smatz@10824: struct { smatz@10824: uint32 grfid; ///< ID of removed GRF smatz@10824: } grfrem; smatz@10824: GRFIdentifier grfcompat; ///< ID and new md5sum of changed GRF smatz@10824: struct { smatz@10824: uint32 grfid; ///< ID of GRF with changed parameters smatz@10824: } grfparam; smatz@10824: struct { smatz@10824: uint32 grfid; ///< ID of moved GRF smatz@10824: int32 offset; ///< offset, positive = move down smatz@10824: } grfmove; smatz@10824: struct { smatz@10824: char *name; ///< name of the patch smatz@10824: int32 oldval; ///< old value smatz@10824: int32 newval; ///< new value smatz@10824: } patch; smatz@10824: }; smatz@10824: }; smatz@10824: smatz@10824: smatz@10824: /** Contains information about one logged action that caused at least one logged change */ smatz@10824: struct LoggedAction { smatz@10824: LoggedChange *change; ///< First logged change in this action smatz@10824: uint32 changes; ///< Number of changes in this action smatz@10824: GamelogActionType at; ///< Type of action smatz@10824: uint16 tick; ///< Tick when it happened smatz@10824: }; smatz@10824: smatz@10824: static GamelogActionType _gamelog_action_type = GLAT_NONE; ///< action to record if anything changes smatz@10824: smatz@10824: static LoggedAction *_gamelog_action = NULL; ///< first logged action smatz@10824: static uint _gamelog_actions = 0; ///< number of actions smatz@10824: static LoggedAction *_current_action = NULL; ///< current action we are logging, NULL when there is no action active smatz@10824: smatz@10824: smatz@10824: /** Stores information about new action, but doesn't allocate it smatz@10824: * Action is allocated only when there is at least one change smatz@10824: * @param at type of action smatz@10824: */ smatz@10824: void GamelogStartAction(GamelogActionType at) smatz@10824: { smatz@10824: assert(_gamelog_action_type == GLAT_NONE); // do not allow starting new action without stopping the previous first smatz@10824: _gamelog_action_type = at; smatz@10824: } smatz@10824: smatz@10824: /** Stops logging of any changes smatz@10824: */ smatz@10824: void GamelogStopAction() smatz@10824: { smatz@10824: assert(_gamelog_action_type != GLAT_NONE); // nobody should try to stop if there is no action in progress smatz@10824: smatz@11169: bool print = _current_action != NULL; smatz@11169: smatz@10824: _current_action = NULL; smatz@10824: _gamelog_action_type = GLAT_NONE; smatz@10824: smatz@11169: if (print) GamelogPrintDebug(5); smatz@10824: } smatz@10824: smatz@10824: /** Resets and frees all memory allocated - used before loading or starting a new game smatz@10824: */ smatz@10824: void GamelogReset() smatz@10824: { smatz@10824: assert(_gamelog_action_type == GLAT_NONE); smatz@10824: smatz@10824: for (uint i = 0; i < _gamelog_actions; i++) { smatz@10824: const LoggedAction *la = &_gamelog_action[i]; smatz@10824: for (uint j = 0; j < la->changes; j++) { smatz@10824: const LoggedChange *lc = &la->change[j]; smatz@10824: if (lc->ct == GLCT_PATCH) free(lc->patch.name); smatz@10824: } smatz@10824: free(la->change); smatz@10824: } smatz@10824: smatz@10824: free(_gamelog_action); smatz@10824: smatz@10824: _gamelog_action = NULL; smatz@10824: _gamelog_actions = 0; smatz@10824: _current_action = NULL; smatz@10824: } smatz@10824: smatz@10824: enum { smatz@10824: GAMELOG_BUF_LEN = 1024 ///< length of buffer for one line of text smatz@10824: }; smatz@10824: smatz@10824: static int _dbgofs = 0; ///< offset in current output buffer smatz@10824: smatz@10824: static void AddDebugText(char *buf, const char *s, ...) smatz@10824: { smatz@10824: if (GAMELOG_BUF_LEN <= _dbgofs) return; smatz@10824: smatz@10824: va_list va; smatz@10824: smatz@10824: va_start(va, s); smatz@10824: _dbgofs += vsnprintf(buf + _dbgofs, GAMELOG_BUF_LEN - _dbgofs, s, va); smatz@10824: va_end(va); smatz@10824: } smatz@10824: smatz@10824: smatz@10824: /** Prints GRF filename if found smatz@10824: * @param grfid GRF which filename to print smatz@10824: */ smatz@10824: static void PrintGrfFilename(char *buf, uint grfid) smatz@10824: { smatz@10824: const GRFConfig *gc = FindGRFConfig(grfid); smatz@10824: smatz@10824: if (gc == NULL) return; smatz@10824: smatz@10824: AddDebugText(buf, ", filename: %s", gc->filename); smatz@10824: } smatz@10824: smatz@10824: /** Prints GRF ID, checksum and filename if found smatz@10824: * @param grfid GRF ID smatz@10824: * @param md5sum array of md5sum to print smatz@10824: */ smatz@10824: static void PrintGrfInfo(char *buf, uint grfid, const uint8 *md5sum) smatz@10824: { smatz@10824: char txt[40]; smatz@10824: smatz@10824: md5sumToString(txt, lastof(txt), md5sum); smatz@10824: smatz@10824: AddDebugText(buf, "GRF ID %08X, checksum %s", BSWAP32(grfid), txt); smatz@10824: smatz@10824: PrintGrfFilename(buf, grfid); smatz@10824: smatz@10824: return; smatz@10824: } smatz@10824: smatz@10824: smatz@10824: /** Text messages for various logged actions */ smatz@10824: static const char *la_text[] = { smatz@10824: "new game started", smatz@10824: "game loaded", smatz@10824: "GRF config changed", smatz@10824: "cheat was used", smatz@10824: "patch settings changed" smatz@10824: }; smatz@10824: smatz@10824: assert_compile(lengthof(la_text) == GLAT_END); smatz@10824: smatz@10824: smatz@10824: /** Prints active gamelog */ smatz@10824: void GamelogPrint(GamelogPrintProc *proc) smatz@10824: { smatz@10824: char buf[GAMELOG_BUF_LEN]; smatz@10824: smatz@10824: proc("---- gamelog start ----"); smatz@10824: smatz@10824: const LoggedAction *laend = &_gamelog_action[_gamelog_actions]; smatz@10824: smatz@10824: for (const LoggedAction *la = _gamelog_action; la != laend; la++) { smatz@10824: assert((uint)la->at < GLAT_END); smatz@10824: smatz@10824: snprintf(buf, GAMELOG_BUF_LEN, "Tick %u: %s", (uint)la->tick, la_text[(uint)la->at]); smatz@10824: proc(buf); smatz@10824: smatz@10824: const LoggedChange *lcend = &la->change[la->changes]; smatz@10824: smatz@10824: for (const LoggedChange *lc = la->change; lc != lcend; lc++) { smatz@10824: _dbgofs = 0; smatz@10824: AddDebugText(buf, " "); smatz@10824: smatz@10824: switch (lc->ct) { smatz@10824: default: NOT_REACHED(); smatz@10824: case GLCT_MODE: smatz@10824: AddDebugText(buf, "New game mode: %u landscape: %u", smatz@10824: (uint)lc->mode.mode, (uint)lc->mode.landscape); smatz@10824: break; smatz@10824: smatz@10824: case GLCT_REVISION: smatz@10824: AddDebugText(buf, "Revision text changed to %s, savegame version %u, ", smatz@10824: lc->revision.text, lc->revision.slver); smatz@10824: smatz@10824: switch (lc->revision.modified) { smatz@10824: case 0: AddDebugText(buf, "not "); break; smatz@10824: case 1: AddDebugText(buf, "maybe "); break; smatz@10824: default: break; smatz@10824: } smatz@10824: smatz@10824: AddDebugText(buf, "modified, _openttd_newgrf_version = 0x%08x", lc->revision.newgrf); smatz@10824: break; smatz@10824: smatz@10824: case GLCT_OLDVER: smatz@10824: AddDebugText(buf, "Conversion from "); smatz@10824: switch (lc->oldver.type) { smatz@10824: default: NOT_REACHED(); smatz@10824: case SGT_OTTD: smatz@10824: AddDebugText(buf, "OTTD savegame without gamelog: version %u, %u", smatz@10824: GB(lc->oldver.version, 8, 16), GB(lc->oldver.version, 0, 8)); smatz@10824: break; smatz@10824: smatz@10824: case SGT_TTD: smatz@10824: AddDebugText(buf, "TTD savegame"); smatz@10824: break; smatz@10824: smatz@10824: case SGT_TTDP1: smatz@10824: case SGT_TTDP2: smatz@10824: AddDebugText(buf, "TTDP savegame, %s format", smatz@10824: lc->oldver.type == SGT_TTDP1 ? "old" : "new"); smatz@10824: if (lc->oldver.version != 0) { smatz@10824: AddDebugText(buf, ", TTDP version %u.%u.%u.%u", smatz@10824: GB(lc->oldver.version, 24, 8), GB(lc->oldver.version, 20, 4), smatz@10824: GB(lc->oldver.version, 16, 4), GB(lc->oldver.version, 0, 16)); smatz@10824: } smatz@10824: break; smatz@10824: } smatz@10824: break; smatz@10824: smatz@10824: case GLCT_PATCH: smatz@10824: AddDebugText(buf, "Patch setting changed: %s : %d -> %d", lc->patch.name, lc->patch.oldval, lc->patch.newval); smatz@10824: break; smatz@10824: smatz@10824: case GLCT_GRFADD: smatz@10824: AddDebugText(buf, "Added NewGRF: "); smatz@10824: PrintGrfInfo(buf, lc->grfadd.grfid, lc->grfadd.md5sum); smatz@10824: break; smatz@10824: smatz@10824: case GLCT_GRFREM: smatz@10824: AddDebugText(buf, "Removed NewGRF: %08X", BSWAP32(lc->grfrem.grfid)); smatz@10824: PrintGrfFilename(buf, lc->grfrem.grfid); smatz@10824: break; smatz@10824: smatz@10824: case GLCT_GRFCOMPAT: smatz@10824: AddDebugText(buf, "Compatible NewGRF loaded: "); smatz@10824: PrintGrfInfo(buf, lc->grfcompat.grfid, lc->grfcompat.md5sum); smatz@10824: break; smatz@10824: smatz@10824: case GLCT_GRFPARAM: smatz@10824: AddDebugText(buf, "GRF parameter changed: %08X", BSWAP32(lc->grfparam.grfid)); smatz@10824: PrintGrfFilename(buf, lc->grfparam.grfid); smatz@10824: break; smatz@10824: smatz@10824: case GLCT_GRFMOVE: smatz@10824: AddDebugText(buf, "GRF order changed: %08X moved %d places %s", smatz@10824: BSWAP32(lc->grfmove.grfid), abs(lc->grfmove.offset), lc->grfmove.offset >= 0 ? "down" : "up" ); smatz@10824: PrintGrfFilename(buf, lc->grfmove.grfid); smatz@10824: break; smatz@10824: } smatz@10824: smatz@10824: proc(buf); smatz@10824: } smatz@10824: } smatz@10824: smatz@10824: proc("---- gamelog end ----"); smatz@10824: } smatz@10824: smatz@10824: smatz@10824: static void GamelogPrintConsoleProc(const char *s) smatz@10824: { smatz@10824: IConsolePrint(CC_WARNING, s); smatz@10824: } smatz@10824: smatz@10824: void GamelogPrintConsole() smatz@10824: { smatz@10824: GamelogPrint(&GamelogPrintConsoleProc); smatz@10824: } smatz@10824: smatz@11169: static int _gamelog_print_level = 0; ///< gamelog debug level we need to print stuff smatz@10824: smatz@10824: static void GamelogPrintDebugProc(const char *s) smatz@10824: { smatz@11169: DEBUG(gamelog, _gamelog_print_level, s); smatz@10824: } smatz@10824: smatz@11169: smatz@11169: /** Prints gamelog to debug output. Code is executed even when smatz@11169: * there will be no output. It is called very seldom, so it smatz@11169: * doesn't matter that much. At least it gives more uniform code... smatz@11169: * @param level debug level we need to print stuff smatz@11169: */ smatz@11169: void GamelogPrintDebug(int level) smatz@10824: { smatz@11169: _gamelog_print_level = level; smatz@10824: GamelogPrint(&GamelogPrintDebugProc); smatz@10824: } smatz@10824: smatz@10824: smatz@10824: /** Allocates new LoggedChange and new LoggedAction if needed. smatz@10824: * If there is no action active, NULL is returned. smatz@10824: * @param ct type of change smatz@10824: * @return new LoggedChange, or NULL if there is no action active smatz@10824: */ smatz@10824: static LoggedChange *GamelogChange(GamelogChangeType ct) smatz@10824: { smatz@10824: if (_current_action == NULL) { smatz@10824: if (_gamelog_action_type == GLAT_NONE) return NULL; smatz@10824: smatz@10824: _gamelog_action = ReallocT(_gamelog_action, _gamelog_actions + 1); smatz@10824: _current_action = &_gamelog_action[_gamelog_actions++]; smatz@10824: smatz@10824: _current_action->at = _gamelog_action_type; smatz@10824: _current_action->tick = _tick_counter; smatz@10824: _current_action->change = NULL; smatz@10824: _current_action->changes = 0; smatz@10824: } smatz@10824: smatz@10824: _current_action->change = ReallocT(_current_action->change, _current_action->changes + 1); smatz@10824: smatz@10824: LoggedChange *lc = &_current_action->change[_current_action->changes++]; smatz@10824: lc->ct = ct; smatz@10824: smatz@10824: return lc; smatz@10824: } smatz@10824: smatz@10824: smatz@10824: /** Logs a change in game revision smatz@10824: * @param revision new revision string smatz@10824: */ smatz@10824: void GamelogRevision() smatz@10824: { smatz@10824: assert(_gamelog_action_type == GLAT_START || _gamelog_action_type == GLAT_LOAD); smatz@10824: smatz@10824: LoggedChange *lc = GamelogChange(GLCT_REVISION); smatz@10824: if (lc == NULL) return; smatz@10824: smatz@10824: strncpy(lc->revision.text, _openttd_revision, lengthof(lc->revision.text)); smatz@10824: lc->revision.slver = SAVEGAME_VERSION; smatz@10824: lc->revision.modified = _openttd_revision_modified; smatz@10824: lc->revision.newgrf = _openttd_newgrf_version; smatz@10824: } smatz@10824: smatz@10824: /** Logs a change in game mode (scenario editor or game) smatz@10824: */ smatz@10824: void GamelogMode() smatz@10824: { smatz@10824: assert(_gamelog_action_type == GLAT_START || _gamelog_action_type == GLAT_LOAD || _gamelog_action_type == GLAT_CHEAT); smatz@10824: smatz@10824: LoggedChange *lc = GamelogChange(GLCT_MODE); smatz@10824: if (lc == NULL) return; smatz@10824: smatz@10824: lc->mode.mode = _game_mode; smatz@10824: lc->mode.landscape = _settings_game.game_creation.landscape; smatz@10824: } smatz@10824: smatz@10824: /** Logs loading from savegame without gamelog smatz@10824: */ smatz@10824: void GamelogOldver() smatz@10824: { smatz@10824: assert(_gamelog_action_type == GLAT_LOAD); smatz@10824: smatz@10824: LoggedChange *lc = GamelogChange(GLCT_OLDVER); smatz@10824: if (lc == NULL) return; smatz@10824: smatz@10824: lc->oldver.type = _savegame_type; smatz@10824: lc->oldver.version = (_savegame_type == SGT_OTTD ? ((uint32)_sl_version << 8 | _sl_minor_version) : _ttdp_version); smatz@10824: } smatz@10824: smatz@10824: /** Logs change in game patches. Only non-networksafe patches are logged smatz@10824: * @param name patch name smatz@10824: * @param oldval old patch value smatz@10824: * @param newval new patch value smatz@10824: */ smatz@10824: void GamelogPatch(const char *name, int32 oldval, int32 newval) smatz@10824: { smatz@10824: assert(_gamelog_action_type == GLAT_PATCH); smatz@10824: smatz@10824: LoggedChange *lc = GamelogChange(GLCT_PATCH); smatz@10824: if (lc == NULL) return; smatz@10824: smatz@10824: lc->patch.name = strdup(name); smatz@10824: lc->patch.oldval = oldval; smatz@10824: lc->patch.newval = newval; smatz@10824: } smatz@10824: smatz@10824: smatz@10824: /** Finds out if current revision is different than last revision stored in the savegame. smatz@10824: * Appends GLCT_REVISION when the revision string changed smatz@10824: */ smatz@10824: void GamelogTestRevision() smatz@10824: { smatz@10824: const LoggedChange *rev = NULL; smatz@10824: smatz@10824: const LoggedAction *laend = &_gamelog_action[_gamelog_actions]; smatz@10824: for (const LoggedAction *la = _gamelog_action; la != laend; la++) { smatz@10824: const LoggedChange *lcend = &la->change[la->changes]; smatz@10824: for (const LoggedChange *lc = la->change; lc != lcend; lc++) { smatz@10824: if (lc->ct == GLCT_REVISION) rev = lc; smatz@10824: } smatz@10824: } smatz@10824: smatz@10824: if (rev == NULL || strcmp(rev->revision.text, _openttd_revision) != 0 || smatz@10824: rev->revision.modified != _openttd_revision_modified || smatz@10824: rev->revision.newgrf != _openttd_newgrf_version) { smatz@10824: GamelogRevision(); smatz@10824: } smatz@10824: } smatz@10824: smatz@10824: /** Finds last stored game mode or landscape. smatz@10824: * Any change is logged smatz@10824: */ smatz@10824: void GamelogTestMode() smatz@10824: { smatz@10824: const LoggedChange *mode = NULL; smatz@10824: smatz@10824: const LoggedAction *laend = &_gamelog_action[_gamelog_actions]; smatz@10824: for (const LoggedAction *la = _gamelog_action; la != laend; la++) { smatz@10824: const LoggedChange *lcend = &la->change[la->changes]; smatz@10824: for (const LoggedChange *lc = la->change; lc != lcend; lc++) { smatz@10824: if (lc->ct == GLCT_MODE) mode = lc; smatz@10824: } smatz@10824: } smatz@10824: smatz@10824: if (mode == NULL || mode->mode.mode != _game_mode || mode->mode.landscape != _settings_game.game_creation.landscape) GamelogMode(); smatz@10824: } smatz@10824: smatz@10824: smatz@10824: /** Decides if GRF should be logged smatz@10824: * @param g grf to determine smatz@10824: * @return true iff GRF is not static and is loaded smatz@10824: */ smatz@10824: static inline bool IsLoggableGrfConfig(const GRFConfig *g) smatz@10824: { smatz@10824: return !HasBit(g->flags, GCF_STATIC) && g->status != GCS_NOT_FOUND; smatz@10824: } smatz@10824: smatz@10824: /** Logs removal of a GRF smatz@10824: * @param grfid ID of removed GRF smatz@10824: */ smatz@10824: void GamelogGRFRemove(uint32 grfid) smatz@10824: { smatz@10824: assert(_gamelog_action_type == GLAT_LOAD || _gamelog_action_type == GLAT_GRF); smatz@10824: smatz@10824: LoggedChange *lc = GamelogChange(GLCT_GRFREM); smatz@10824: if (lc == NULL) return; smatz@10824: smatz@10824: lc->grfrem.grfid = grfid; smatz@10824: } smatz@10824: smatz@10824: /** Logs adding of a GRF smatz@10824: * @param newg added GRF smatz@10824: */ smatz@10824: void GamelogGRFAdd(const GRFConfig *newg) smatz@10824: { smatz@10824: assert(_gamelog_action_type == GLAT_LOAD || _gamelog_action_type == GLAT_START || _gamelog_action_type == GLAT_GRF); smatz@10824: smatz@10824: if (!IsLoggableGrfConfig(newg)) return; smatz@10824: smatz@10824: LoggedChange *lc = GamelogChange(GLCT_GRFADD); smatz@10824: if (lc == NULL) return; smatz@10824: smatz@10824: memcpy(&lc->grfadd, newg, sizeof(GRFIdentifier)); smatz@10824: } smatz@10824: smatz@10824: /** Logs loading compatible GRF smatz@10824: * (the same ID, but different MD5 hash) smatz@10824: * @param newg new (updated) GRF smatz@10824: */ smatz@10824: void GamelogGRFCompatible(const GRFIdentifier *newg) smatz@10824: { glx@10845: assert(_gamelog_action_type == GLAT_LOAD || _gamelog_action_type == GLAT_GRF); smatz@10824: smatz@10824: LoggedChange *lc = GamelogChange(GLCT_GRFCOMPAT); smatz@10824: if (lc == NULL) return; smatz@10824: smatz@10824: memcpy(&lc->grfcompat, newg, sizeof(GRFIdentifier)); smatz@10824: } smatz@10824: smatz@10824: /** Logs changing GRF order smatz@10824: * @param grfid GRF that is moved smatz@10824: * @param offset how far it is moved, positive = moved down smatz@10824: */ smatz@10824: static void GamelogGRFMove(uint32 grfid, int32 offset) smatz@10824: { smatz@10824: assert(_gamelog_action_type == GLAT_GRF); smatz@10824: smatz@10824: LoggedChange *lc = GamelogChange(GLCT_GRFMOVE); smatz@10824: if (lc == NULL) return; smatz@10824: smatz@10824: lc->grfmove.grfid = grfid; smatz@10824: lc->grfmove.offset = offset; smatz@10824: } smatz@10824: smatz@10824: /** Logs change in GRF parameters. smatz@10824: * Details about parameters changed are not stored smatz@10824: * @param grfid ID of GRF to store smatz@10824: */ smatz@10824: static void GamelogGRFParameters(uint32 grfid) smatz@10824: { smatz@10824: assert(_gamelog_action_type == GLAT_GRF); smatz@10824: smatz@10824: LoggedChange *lc = GamelogChange(GLCT_GRFPARAM); smatz@10824: if (lc == NULL) return; smatz@10824: smatz@10824: lc->grfparam.grfid = grfid; smatz@10824: } smatz@10824: smatz@10824: /** Logs adding of list of GRFs. smatz@10824: * Useful when old savegame is loaded or when new game is started smatz@10824: * @param newg head of GRF linked list smatz@10824: */ smatz@10824: void GamelogGRFAddList(const GRFConfig *newg) smatz@10824: { smatz@10824: assert(_gamelog_action_type == GLAT_START || _gamelog_action_type == GLAT_LOAD); smatz@10824: smatz@10824: for (; newg != NULL; newg = newg->next) { smatz@10824: GamelogGRFAdd(newg); smatz@10824: } smatz@10824: } smatz@10824: smatz@10824: /** List of GRFs using array of pointers instead of linked list */ smatz@10824: struct GRFList { smatz@10824: uint n; smatz@10824: const GRFConfig *grf[VARARRAY_SIZE]; smatz@10824: }; smatz@10824: smatz@10824: /** Generates GRFList smatz@10824: * @param grfc head of GRF linked list smatz@10824: */ smatz@10824: static GRFList *GenerateGRFList(const GRFConfig *grfc) smatz@10824: { smatz@10824: uint n = 0; smatz@10824: for (const GRFConfig *g = grfc; g != NULL; g = g->next) { smatz@10824: if (IsLoggableGrfConfig(g)) n++; smatz@10824: } smatz@10824: smatz@10824: GRFList *list = (GRFList*)MallocT(sizeof(GRFList) + n * sizeof(GRFConfig*)); smatz@10824: smatz@10824: list->n = 0; smatz@10824: for (const GRFConfig *g = grfc; g != NULL; g = g->next) { smatz@10824: if (IsLoggableGrfConfig(g)) list->grf[list->n++] = g; smatz@10824: } smatz@10824: smatz@10824: return list; smatz@10824: } smatz@10824: smatz@10824: /** Compares two NewGRF lists and logs any change smatz@10824: * @param oldc original GRF list smatz@10824: * @param newc new GRF list smatz@10824: */ smatz@10824: void GamelogGRFUpdate(const GRFConfig *oldc, const GRFConfig *newc) smatz@10824: { smatz@10824: GRFList *ol = GenerateGRFList(oldc); smatz@10824: GRFList *nl = GenerateGRFList(newc); smatz@10824: smatz@10824: uint o = 0, n = 0; smatz@10824: smatz@10824: while (o < ol->n && n < nl->n) { smatz@10824: const GRFConfig *og = ol->grf[o]; smatz@10824: const GRFConfig *ng = nl->grf[n]; smatz@10824: smatz@10824: if (og->grfid != ng->grfid) { smatz@10824: uint oi, ni; smatz@10824: for (oi = 0; oi < ol->n; oi++) { smatz@10824: if (ol->grf[oi]->grfid == nl->grf[n]->grfid) break; smatz@10824: } smatz@10824: if (oi < o) { smatz@10824: /* GRF was moved, this change has been logged already */ smatz@10824: n++; smatz@10824: continue; smatz@10824: } smatz@10824: if (oi == ol->n) { smatz@10824: /* GRF couldn't be found in the OLD list, GRF was ADDED */ smatz@10824: GamelogGRFAdd(nl->grf[n++]); smatz@10824: continue; smatz@10824: } smatz@10824: for (ni = 0; ni < nl->n; ni++) { smatz@10824: if (nl->grf[ni]->grfid == ol->grf[o]->grfid) break; smatz@10824: } smatz@10824: if (ni < n) { smatz@10824: /* GRF was moved, this change has been logged already */ smatz@10824: o++; smatz@10824: continue; smatz@10824: } smatz@10824: if (ni == nl->n) { smatz@10824: /* GRF couldn't be found in the NEW list, GRF was REMOVED */ smatz@10824: GamelogGRFRemove(ol->grf[o++]->grfid); smatz@10824: continue; smatz@10824: } smatz@10824: smatz@10824: /* o < oi < ol->n smatz@10824: * n < ni < nl->n */ smatz@10824: assert(ni > n && ni < nl->n); smatz@10824: assert(oi > o && oi < ol->n); smatz@10824: smatz@10824: ni -= n; // number of GRFs it was moved downwards smatz@10824: oi -= o; // number of GRFs it was moved upwards smatz@10824: smatz@10824: if (ni >= oi) { // prefer the one that is moved further smatz@10824: /* GRF was moved down */ smatz@10824: GamelogGRFMove(ol->grf[o++]->grfid, ni); smatz@10824: } else { smatz@10824: GamelogGRFMove(nl->grf[n++]->grfid, -(int)oi); smatz@10824: } smatz@10824: } else { smatz@10824: if (memcmp(og->md5sum, ng->md5sum, sizeof(og->md5sum)) != 0) { smatz@10824: /* md5sum changed, probably loading 'compatible' GRF */ smatz@10824: GamelogGRFCompatible(nl->grf[n]); smatz@10824: } smatz@10824: smatz@10824: if (og->num_params != ng->num_params || memcmp(og->param, ng->param, og->num_params * sizeof(og->param[0])) != 0) { smatz@10824: GamelogGRFParameters(ol->grf[o]->grfid); smatz@10824: } smatz@10824: smatz@10824: o++; smatz@10824: n++; smatz@10824: } smatz@10824: } smatz@10824: smatz@10824: while (o < ol->n) GamelogGRFRemove(ol->grf[o++]->grfid); // remaining GRFs were removed ... smatz@10824: while (n < nl->n) GamelogGRFAdd (nl->grf[n++]); // ... or added smatz@10824: smatz@10824: free(ol); smatz@10824: free(nl); smatz@10824: } smatz@10824: smatz@10824: smatz@10824: static const SaveLoad _glog_action_desc[] = { smatz@10824: SLE_VAR(LoggedAction, tick, SLE_UINT16), smatz@10824: SLE_END() smatz@10824: }; smatz@10824: smatz@10824: static const SaveLoad _glog_mode_desc[] = { smatz@10824: SLE_VAR(LoggedChange, mode.mode, SLE_UINT8), smatz@10824: SLE_VAR(LoggedChange, mode.landscape, SLE_UINT8), smatz@10824: SLE_END() smatz@10824: }; smatz@10824: smatz@10824: static const SaveLoad _glog_revision_desc[] = { smatz@10824: SLE_ARR(LoggedChange, revision.text, SLE_UINT8, NETWORK_REVISION_LENGTH), smatz@10824: SLE_VAR(LoggedChange, revision.newgrf, SLE_UINT32), smatz@10824: SLE_VAR(LoggedChange, revision.slver, SLE_UINT16), smatz@10824: SLE_VAR(LoggedChange, revision.modified, SLE_UINT8), smatz@10824: SLE_END() smatz@10824: }; smatz@10824: smatz@10824: static const SaveLoad _glog_oldver_desc[] = { smatz@10824: SLE_VAR(LoggedChange, oldver.type, SLE_UINT32), smatz@10824: SLE_VAR(LoggedChange, oldver.version, SLE_UINT32), smatz@10824: SLE_END() smatz@10824: }; smatz@10824: smatz@10824: static const SaveLoad _glog_patch_desc[] = { smatz@10824: SLE_STR(LoggedChange, patch.name, SLE_STR, 128), smatz@10824: SLE_VAR(LoggedChange, patch.oldval, SLE_INT32), smatz@10824: SLE_VAR(LoggedChange, patch.newval, SLE_INT32), smatz@10824: SLE_END() smatz@10824: }; smatz@10824: smatz@10824: static const SaveLoad _glog_grfadd_desc[] = { smatz@10824: SLE_VAR(LoggedChange, grfadd.grfid, SLE_UINT32 ), smatz@10824: SLE_ARR(LoggedChange, grfadd.md5sum, SLE_UINT8, 16), smatz@10824: SLE_END() smatz@10824: }; smatz@10824: smatz@10824: static const SaveLoad _glog_grfrem_desc[] = { smatz@10824: SLE_VAR(LoggedChange, grfrem.grfid, SLE_UINT32), smatz@10824: SLE_END() smatz@10824: }; smatz@10824: smatz@10824: static const SaveLoad _glog_grfcompat_desc[] = { smatz@10824: SLE_VAR(LoggedChange, grfcompat.grfid, SLE_UINT32 ), smatz@10824: SLE_ARR(LoggedChange, grfcompat.md5sum, SLE_UINT8, 16), smatz@10824: SLE_END() smatz@10824: }; smatz@10824: smatz@10824: static const SaveLoad _glog_grfparam_desc[] = { smatz@10824: SLE_VAR(LoggedChange, grfparam.grfid, SLE_UINT32), smatz@10824: SLE_END() smatz@10824: }; smatz@10824: smatz@10824: static const SaveLoad _glog_grfmove_desc[] = { smatz@10824: SLE_VAR(LoggedChange, grfmove.grfid, SLE_UINT32), smatz@10824: SLE_VAR(LoggedChange, grfmove.offset, SLE_INT32), smatz@10824: SLE_END() smatz@10824: }; smatz@10824: smatz@10824: static const SaveLoad *_glog_desc[] = { smatz@10824: _glog_mode_desc, smatz@10824: _glog_revision_desc, smatz@10824: _glog_oldver_desc, smatz@10824: _glog_patch_desc, smatz@10824: _glog_grfadd_desc, smatz@10824: _glog_grfrem_desc, smatz@10824: _glog_grfcompat_desc, smatz@10824: _glog_grfparam_desc, smatz@10824: _glog_grfmove_desc smatz@10824: }; smatz@10824: smatz@10824: assert_compile(lengthof(_glog_desc) == GLCT_END); smatz@10824: smatz@10824: static void Load_GLOG() smatz@10824: { smatz@10824: assert(_gamelog_action == NULL); smatz@10824: assert(_gamelog_actions == 0); smatz@10824: smatz@10824: GamelogActionType at; smatz@10824: while ((at = (GamelogActionType)SlReadByte()) != GLAT_NONE) { smatz@10824: _gamelog_action = ReallocT(_gamelog_action, _gamelog_actions + 1); smatz@10824: LoggedAction *la = &_gamelog_action[_gamelog_actions++]; smatz@10824: smatz@10824: la->at = at; smatz@10824: smatz@10824: SlObject(la, _glog_action_desc); // has to be saved after 'DATE'! smatz@10824: la->change = NULL; smatz@10824: la->changes = 0; smatz@10824: smatz@10824: GamelogChangeType ct; smatz@10824: while ((ct = (GamelogChangeType)SlReadByte()) != GLCT_NONE) { smatz@10824: la->change = ReallocT(la->change, la->changes + 1); smatz@10824: smatz@10824: LoggedChange *lc = &la->change[la->changes++]; smatz@10966: /* for SLE_STR, pointer has to be valid! so make it NULL */ smatz@10966: memset(lc, 0, sizeof(*lc)); smatz@10824: lc->ct = ct; smatz@10824: smatz@10824: assert((uint)ct < GLCT_END); smatz@10824: smatz@10824: SlObject(lc, _glog_desc[ct]); smatz@10824: } smatz@10824: } smatz@10824: } smatz@10824: smatz@10824: static void Save_GLOG() smatz@10824: { smatz@10824: const LoggedAction *laend = &_gamelog_action[_gamelog_actions]; smatz@10824: size_t length = 0; smatz@10824: smatz@10824: for (const LoggedAction *la = _gamelog_action; la != laend; la++) { smatz@10824: const LoggedChange *lcend = &la->change[la->changes]; smatz@10824: for (LoggedChange *lc = la->change; lc != lcend; lc++) { smatz@10824: assert((uint)lc->ct < lengthof(_glog_desc)); smatz@10824: length += SlCalcObjLength(lc, _glog_desc[lc->ct]) + 1; smatz@10824: } smatz@10824: length += 4; smatz@10824: } smatz@10824: length++; smatz@10824: smatz@10824: SlSetLength(length); smatz@10824: smatz@10824: for (LoggedAction *la = _gamelog_action; la != laend; la++) { smatz@10824: SlWriteByte(la->at); smatz@10824: SlObject(la, _glog_action_desc); smatz@10824: smatz@10824: const LoggedChange *lcend = &la->change[la->changes]; smatz@10824: for (LoggedChange *lc = la->change; lc != lcend; lc++) { smatz@10824: SlWriteByte(lc->ct); smatz@10824: assert((uint)lc->ct < GLCT_END); smatz@10824: SlObject(lc, _glog_desc[lc->ct]); smatz@10824: } smatz@10824: SlWriteByte(GLCT_NONE); smatz@10824: } smatz@10824: SlWriteByte(GLAT_NONE); smatz@10824: } smatz@10824: smatz@10824: smatz@10824: extern const ChunkHandler _gamelog_chunk_handlers[] = { smatz@10824: { 'GLOG', Save_GLOG, Load_GLOG, CH_RIFF | CH_LAST } smatz@10824: };