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