diff options
author | smatz <smatz@openttd.org> | 2008-06-03 18:35:58 +0000 |
---|---|---|
committer | smatz <smatz@openttd.org> | 2008-06-03 18:35:58 +0000 |
commit | c7a9637834a96722be5a524c4c1e8932a45e3461 (patch) | |
tree | 72b7d141fd63dcb880f610a334390bf9c41f97b1 /src | |
parent | d58f0558761079552c4c607707dd131a2c5e5397 (diff) | |
download | openttd-c7a9637834a96722be5a524c4c1e8932a45e3461.tar.xz |
(svn r13375) -Add: logging of actions that could possibly cause desyncs and crashes to simplify debugging. See readme.txt for details
Diffstat (limited to 'src')
-rw-r--r-- | src/cheat_gui.cpp | 6 | ||||
-rw-r--r-- | src/console_cmds.cpp | 7 | ||||
-rw-r--r-- | src/debug.cpp | 28 | ||||
-rw-r--r-- | src/debug.h | 2 | ||||
-rw-r--r-- | src/gamelog.cpp | 801 | ||||
-rw-r--r-- | src/gamelog.h | 46 | ||||
-rw-r--r-- | src/misc.cpp | 8 | ||||
-rw-r--r-- | src/newgrf_config.cpp | 4 | ||||
-rw-r--r-- | src/newgrf_gui.cpp | 4 | ||||
-rw-r--r-- | src/openttd.cpp | 11 | ||||
-rw-r--r-- | src/rev.cpp.in | 10 | ||||
-rw-r--r-- | src/rev.h | 1 | ||||
-rw-r--r-- | src/saveload.cpp | 26 | ||||
-rw-r--r-- | src/saveload.h | 1 | ||||
-rw-r--r-- | src/settings.cpp | 17 | ||||
-rw-r--r-- | src/win32.cpp | 27 |
16 files changed, 976 insertions, 23 deletions
diff --git a/src/cheat_gui.cpp b/src/cheat_gui.cpp index 510ca7ef2..ce1cb0b58 100644 --- a/src/cheat_gui.cpp +++ b/src/cheat_gui.cpp @@ -19,6 +19,7 @@ #include "rail_gui.h" #include "gui.h" #include "player_gui.h" +#include "gamelog.h" #include "table/strings.h" #include "table/sprites.h" @@ -65,7 +66,12 @@ static int32 ClickChangeClimateCheat(int32 p1, int32 p2) if (p1 == -1) p1 = 3; if (p1 == 4) p1 = 0; _settings_game.game_creation.landscape = p1; + + GamelogStartAction(GLAT_CHEAT); + GamelogTestMode(); ReloadNewGRFData(); + GamelogStopAction(); + return _settings_game.game_creation.landscape; } diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index b0bd54b59..fe69e759b 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -29,6 +29,7 @@ #include "player_func.h" #include "player_base.h" #include "settings_type.h" +#include "gamelog.h" #ifdef ENABLE_NETWORK #include "table/strings.h" @@ -1303,6 +1304,11 @@ DEF_CONSOLE_CMD(ConListDumpVariables) return true; } +DEF_CONSOLE_CMD(ConGamelogPrint) +{ + GamelogPrintConsole(); + return true; +} #ifdef _DEBUG /* ****************************************** */ @@ -1366,6 +1372,7 @@ void IConsoleStdLibRegister() IConsoleCmdRegister("clear", ConClearBuffer); IConsoleCmdRegister("patch", ConPatch); IConsoleCmdRegister("list_patches", ConListPatches); + IConsoleCmdRegister("gamelog", ConGamelogPrint); IConsoleAliasRegister("dir", "ls"); IConsoleAliasRegister("del", "rm %+"); diff --git a/src/debug.cpp b/src/debug.cpp index 6584b9e7f..fb1e28a49 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -30,6 +30,7 @@ int _debug_yapf_level; int _debug_freetype_level; int _debug_sl_level; int _debug_station_level; +int _debug_gamelog_level; struct DebugLevel { @@ -54,24 +55,17 @@ struct DebugLevel { DEBUG_LEVEL(freetype), DEBUG_LEVEL(sl), DEBUG_LEVEL(station), + DEBUG_LEVEL(gamelog), }; #undef DEBUG_LEVEL #if !defined(NO_DEBUG_MESSAGES) -void CDECL debug(const char *dbg, ...) +void CDECL debug_print(const char *dbg, const char *buf) { - va_list va; - va_start(va, dbg); - const char *s; - char buf[1024]; - - s = va_arg(va, const char*); - vsnprintf(buf, lengthof(buf), s, va); - va_end(va); #if defined(ENABLE_NETWORK) if (_debug_socket != INVALID_SOCKET) { - char buf2[lengthof(buf) + 32]; + char buf2[1024 + 32]; snprintf(buf2, lengthof(buf2), "dbg: [%s] %s\n", dbg, buf); send(_debug_socket, buf2, (int)strlen(buf2), 0); @@ -89,6 +83,20 @@ void CDECL debug(const char *dbg, ...) IConsoleDebug(dbg, buf); } } + +void CDECL debug(const char *dbg, ...) +{ + va_list va; + va_start(va, dbg); + const char *s; + char buf[1024]; + + s = va_arg(va, const char*); + vsnprintf(buf, lengthof(buf), s, va); + va_end(va); + + debug_print(dbg, buf); +} #endif /* NO_DEBUG_MESSAGES */ void SetDebugString(const char *s) diff --git a/src/debug.h b/src/debug.h index 936d7f683..4048c16c0 100644 --- a/src/debug.h +++ b/src/debug.h @@ -47,8 +47,10 @@ extern int _debug_freetype_level; extern int _debug_sl_level; extern int _debug_station_level; + extern int _debug_gamelog_level; void CDECL debug(const char *dbg, ...); + void CDECL debug_print(const char *dbg, const char *buf); #endif /* NO_DEBUG_MESSAGES */ void SetDebugString(const char *s); diff --git a/src/gamelog.cpp b/src/gamelog.cpp new file mode 100644 index 000000000..e5518dbe8 --- /dev/null +++ b/src/gamelog.cpp @@ -0,0 +1,801 @@ +/* $Id$ */ + +/** @file gamelog.cpp Definition of functions used for logging of important changes in the game */ + +#include "stdafx.h" +#include "openttd.h" +#include "saveload.h" +#include "core/alloc_func.hpp" +#include "core/bitmath_func.hpp" +#include "core/math_func.hpp" +#include "network/core/config.h" +#include "variables.h" +#include "string_func.h" +#include "settings_type.h" +#include "newgrf_config.h" +#include <string.h> +#include <stdarg.h> +#include "gamelog.h" +#include "console_func.h" +#include "debug.h" +#include "rev.h" + +extern const uint16 SAVEGAME_VERSION; ///< current savegame version + +extern SavegameType _savegame_type; ///< type of savegame we are loading + +extern uint32 _ttdp_version; ///< version of TTDP savegame (if applicable) +extern uint16 _sl_version; ///< the major savegame version identifier +extern byte _sl_minor_version; ///< the minor savegame version, DO NOT USE! + +/** Type of logged change */ +enum GamelogChangeType { + GLCT_MODE, ///< Scenario editor x Game, different landscape + GLCT_REVISION, ///< Changed game revision string + GLCT_OLDVER, ///< Loaded from savegame without logged data + GLCT_PATCH, ///< Non-networksafe patch value changed + GLCT_GRFADD, ///< Removed GRF + GLCT_GRFREM, ///< Added GRF + GLCT_GRFCOMPAT, ///< Loading compatible GRF + GLCT_GRFPARAM, ///< GRF parameter changed + GLCT_GRFMOVE, ///< GRF order changed + GLCT_END, ///< So we know how many GLCTs are there + GLCT_NONE = 0xFF, ///< In savegames, end of list +}; + + +/** Contains information about one logged change */ +struct LoggedChange { + GamelogChangeType ct; ///< Type of change logged in this struct + union { + struct { + byte mode; ///< new game mode - Editor x Game + byte landscape; ///< landscape (temperate, arctic, ...) + } mode; + struct { + char text[NETWORK_REVISION_LENGTH]; ///< revision string, _openttd_revision + uint32 newgrf; ///< _openttd_newgrf_version + uint16 slver; ///< _sl_version + byte modified; ///< _openttd_revision_modified + } revision; + struct { + uint32 type; ///< type of savegame, @see SavegameType + uint32 version; ///< major and minor version OR ttdp version + } oldver; + GRFIdentifier grfadd; ///< ID and md5sum of added GRF + struct { + uint32 grfid; ///< ID of removed GRF + } grfrem; + GRFIdentifier grfcompat; ///< ID and new md5sum of changed GRF + struct { + uint32 grfid; ///< ID of GRF with changed parameters + } grfparam; + struct { + uint32 grfid; ///< ID of moved GRF + int32 offset; ///< offset, positive = move down + } grfmove; + struct { + char *name; ///< name of the patch + int32 oldval; ///< old value + int32 newval; ///< new value + } patch; + }; +}; + + +/** Contains information about one logged action that caused at least one logged change */ +struct LoggedAction { + LoggedChange *change; ///< First logged change in this action + uint32 changes; ///< Number of changes in this action + GamelogActionType at; ///< Type of action + uint16 tick; ///< Tick when it happened +}; + +static GamelogActionType _gamelog_action_type = GLAT_NONE; ///< action to record if anything changes + +static LoggedAction *_gamelog_action = NULL; ///< first logged action +static uint _gamelog_actions = 0; ///< number of actions +static LoggedAction *_current_action = NULL; ///< current action we are logging, NULL when there is no action active + + +/** Stores information about new action, but doesn't allocate it + * Action is allocated only when there is at least one change + * @param at type of action + */ +void GamelogStartAction(GamelogActionType at) +{ + assert(_gamelog_action_type == GLAT_NONE); // do not allow starting new action without stopping the previous first + _gamelog_action_type = at; +} + +/** Stops logging of any changes + */ +void GamelogStopAction() +{ + assert(_gamelog_action_type != GLAT_NONE); // nobody should try to stop if there is no action in progress + + _current_action = NULL; + _gamelog_action_type = GLAT_NONE; + + if (_debug_gamelog_level > 4) GamelogPrintDebug(); +} + +/** Resets and frees all memory allocated - used before loading or starting a new game + */ +void GamelogReset() +{ + assert(_gamelog_action_type == GLAT_NONE); + + for (uint i = 0; i < _gamelog_actions; i++) { + const LoggedAction *la = &_gamelog_action[i]; + for (uint j = 0; j < la->changes; j++) { + const LoggedChange *lc = &la->change[j]; + if (lc->ct == GLCT_PATCH) free(lc->patch.name); + } + free(la->change); + } + + free(_gamelog_action); + + _gamelog_action = NULL; + _gamelog_actions = 0; + _current_action = NULL; +} + +enum { + GAMELOG_BUF_LEN = 1024 ///< length of buffer for one line of text +}; + +static int _dbgofs = 0; ///< offset in current output buffer + +static void AddDebugText(char *buf, const char *s, ...) +{ + if (GAMELOG_BUF_LEN <= _dbgofs) return; + + va_list va; + + va_start(va, s); + _dbgofs += vsnprintf(buf + _dbgofs, GAMELOG_BUF_LEN - _dbgofs, s, va); + va_end(va); +} + + +/** Prints GRF filename if found + * @param grfid GRF which filename to print + */ +static void PrintGrfFilename(char *buf, uint grfid) +{ + const GRFConfig *gc = FindGRFConfig(grfid); + + if (gc == NULL) return; + + AddDebugText(buf, ", filename: %s", gc->filename); +} + +/** Prints GRF ID, checksum and filename if found + * @param grfid GRF ID + * @param md5sum array of md5sum to print + */ +static void PrintGrfInfo(char *buf, uint grfid, const uint8 *md5sum) +{ + char txt[40]; + + md5sumToString(txt, lastof(txt), md5sum); + + AddDebugText(buf, "GRF ID %08X, checksum %s", BSWAP32(grfid), txt); + + PrintGrfFilename(buf, grfid); + + return; +} + + +/** Text messages for various logged actions */ +static const char *la_text[] = { + "new game started", + "game loaded", + "GRF config changed", + "cheat was used", + "patch settings changed" +}; + +assert_compile(lengthof(la_text) == GLAT_END); + + +/** Prints active gamelog */ +void GamelogPrint(GamelogPrintProc *proc) +{ + char buf[GAMELOG_BUF_LEN]; + + proc("---- gamelog start ----"); + + const LoggedAction *laend = &_gamelog_action[_gamelog_actions]; + + for (const LoggedAction *la = _gamelog_action; la != laend; la++) { + assert((uint)la->at < GLAT_END); + + snprintf(buf, GAMELOG_BUF_LEN, "Tick %u: %s", (uint)la->tick, la_text[(uint)la->at]); + proc(buf); + + const LoggedChange *lcend = &la->change[la->changes]; + + for (const LoggedChange *lc = la->change; lc != lcend; lc++) { + _dbgofs = 0; + AddDebugText(buf, " "); + + switch (lc->ct) { + default: NOT_REACHED(); + case GLCT_MODE: + AddDebugText(buf, "New game mode: %u landscape: %u", + (uint)lc->mode.mode, (uint)lc->mode.landscape); + break; + + case GLCT_REVISION: + AddDebugText(buf, "Revision text changed to %s, savegame version %u, ", + lc->revision.text, lc->revision.slver); + + switch (lc->revision.modified) { + case 0: AddDebugText(buf, "not "); break; + case 1: AddDebugText(buf, "maybe "); break; + default: break; + } + + AddDebugText(buf, "modified, _openttd_newgrf_version = 0x%08x", lc->revision.newgrf); + break; + + case GLCT_OLDVER: + AddDebugText(buf, "Conversion from "); + switch (lc->oldver.type) { + default: NOT_REACHED(); + case SGT_OTTD: + AddDebugText(buf, "OTTD savegame without gamelog: version %u, %u", + GB(lc->oldver.version, 8, 16), GB(lc->oldver.version, 0, 8)); + break; + + case SGT_TTD: + AddDebugText(buf, "TTD savegame"); + break; + + case SGT_TTDP1: + case SGT_TTDP2: + AddDebugText(buf, "TTDP savegame, %s format", + lc->oldver.type == SGT_TTDP1 ? "old" : "new"); + if (lc->oldver.version != 0) { + AddDebugText(buf, ", TTDP version %u.%u.%u.%u", + GB(lc->oldver.version, 24, 8), GB(lc->oldver.version, 20, 4), + GB(lc->oldver.version, 16, 4), GB(lc->oldver.version, 0, 16)); + } + break; + } + break; + + case GLCT_PATCH: + AddDebugText(buf, "Patch setting changed: %s : %d -> %d", lc->patch.name, lc->patch.oldval, lc->patch.newval); + break; + + case GLCT_GRFADD: + AddDebugText(buf, "Added NewGRF: "); + PrintGrfInfo(buf, lc->grfadd.grfid, lc->grfadd.md5sum); + break; + + case GLCT_GRFREM: + AddDebugText(buf, "Removed NewGRF: %08X", BSWAP32(lc->grfrem.grfid)); + PrintGrfFilename(buf, lc->grfrem.grfid); + break; + + case GLCT_GRFCOMPAT: + AddDebugText(buf, "Compatible NewGRF loaded: "); + PrintGrfInfo(buf, lc->grfcompat.grfid, lc->grfcompat.md5sum); + break; + + case GLCT_GRFPARAM: + AddDebugText(buf, "GRF parameter changed: %08X", BSWAP32(lc->grfparam.grfid)); + PrintGrfFilename(buf, lc->grfparam.grfid); + break; + + case GLCT_GRFMOVE: + AddDebugText(buf, "GRF order changed: %08X moved %d places %s", + BSWAP32(lc->grfmove.grfid), abs(lc->grfmove.offset), lc->grfmove.offset >= 0 ? "down" : "up" ); + PrintGrfFilename(buf, lc->grfmove.grfid); + break; + } + + proc(buf); + } + } + + proc("---- gamelog end ----"); +} + + +static void GamelogPrintConsoleProc(const char *s) +{ + IConsolePrint(CC_WARNING, s); +} + +void GamelogPrintConsole() +{ + GamelogPrint(&GamelogPrintConsoleProc); +} + + +static void GamelogPrintDebugProc(const char *s) +{ + debug_print("gamelog", s); +} + +void GamelogPrintDebug() +{ + GamelogPrint(&GamelogPrintDebugProc); +} + + +/** Allocates new LoggedChange and new LoggedAction if needed. + * If there is no action active, NULL is returned. + * @param ct type of change + * @return new LoggedChange, or NULL if there is no action active + */ +static LoggedChange *GamelogChange(GamelogChangeType ct) +{ + if (_current_action == NULL) { + if (_gamelog_action_type == GLAT_NONE) return NULL; + + _gamelog_action = ReallocT(_gamelog_action, _gamelog_actions + 1); + _current_action = &_gamelog_action[_gamelog_actions++]; + + _current_action->at = _gamelog_action_type; + _current_action->tick = _tick_counter; + _current_action->change = NULL; + _current_action->changes = 0; + } + + _current_action->change = ReallocT(_current_action->change, _current_action->changes + 1); + + LoggedChange *lc = &_current_action->change[_current_action->changes++]; + lc->ct = ct; + + return lc; +} + + +/** Logs a change in game revision + * @param revision new revision string + */ +void GamelogRevision() +{ + assert(_gamelog_action_type == GLAT_START || _gamelog_action_type == GLAT_LOAD); + + LoggedChange *lc = GamelogChange(GLCT_REVISION); + if (lc == NULL) return; + + strncpy(lc->revision.text, _openttd_revision, lengthof(lc->revision.text)); + lc->revision.slver = SAVEGAME_VERSION; + lc->revision.modified = _openttd_revision_modified; + lc->revision.newgrf = _openttd_newgrf_version; +} + +/** Logs a change in game mode (scenario editor or game) + */ +void GamelogMode() +{ + assert(_gamelog_action_type == GLAT_START || _gamelog_action_type == GLAT_LOAD || _gamelog_action_type == GLAT_CHEAT); + + LoggedChange *lc = GamelogChange(GLCT_MODE); + if (lc == NULL) return; + + lc->mode.mode = _game_mode; + lc->mode.landscape = _settings_game.game_creation.landscape; +} + +/** Logs loading from savegame without gamelog + */ +void GamelogOldver() +{ + assert(_gamelog_action_type == GLAT_LOAD); + + LoggedChange *lc = GamelogChange(GLCT_OLDVER); + if (lc == NULL) return; + + lc->oldver.type = _savegame_type; + lc->oldver.version = (_savegame_type == SGT_OTTD ? ((uint32)_sl_version << 8 | _sl_minor_version) : _ttdp_version); +} + +/** Logs change in game patches. Only non-networksafe patches are logged + * @param name patch name + * @param oldval old patch value + * @param newval new patch value + */ +void GamelogPatch(const char *name, int32 oldval, int32 newval) +{ + assert(_gamelog_action_type == GLAT_PATCH); + + LoggedChange *lc = GamelogChange(GLCT_PATCH); + if (lc == NULL) return; + + lc->patch.name = strdup(name); + lc->patch.oldval = oldval; + lc->patch.newval = newval; +} + + +/** Finds out if current revision is different than last revision stored in the savegame. + * Appends GLCT_REVISION when the revision string changed + */ +void GamelogTestRevision() +{ + const LoggedChange *rev = NULL; + + const LoggedAction *laend = &_gamelog_action[_gamelog_actions]; + for (const LoggedAction *la = _gamelog_action; la != laend; la++) { + const LoggedChange *lcend = &la->change[la->changes]; + for (const LoggedChange *lc = la->change; lc != lcend; lc++) { + if (lc->ct == GLCT_REVISION) rev = lc; + } + } + + if (rev == NULL || strcmp(rev->revision.text, _openttd_revision) != 0 || + rev->revision.modified != _openttd_revision_modified || + rev->revision.newgrf != _openttd_newgrf_version) { + GamelogRevision(); + } +} + +/** Finds last stored game mode or landscape. + * Any change is logged + */ +void GamelogTestMode() +{ + const LoggedChange *mode = NULL; + + const LoggedAction *laend = &_gamelog_action[_gamelog_actions]; + for (const LoggedAction *la = _gamelog_action; la != laend; la++) { + const LoggedChange *lcend = &la->change[la->changes]; + for (const LoggedChange *lc = la->change; lc != lcend; lc++) { + if (lc->ct == GLCT_MODE) mode = lc; + } + } + + if (mode == NULL || mode->mode.mode != _game_mode || mode->mode.landscape != _settings_game.game_creation.landscape) GamelogMode(); +} + + +/** Decides if GRF should be logged + * @param g grf to determine + * @return true iff GRF is not static and is loaded + */ +static inline bool IsLoggableGrfConfig(const GRFConfig *g) +{ + return !HasBit(g->flags, GCF_STATIC) && g->status != GCS_NOT_FOUND; +} + +/** Logs removal of a GRF + * @param grfid ID of removed GRF + */ +void GamelogGRFRemove(uint32 grfid) +{ + assert(_gamelog_action_type == GLAT_LOAD || _gamelog_action_type == GLAT_GRF); + + LoggedChange *lc = GamelogChange(GLCT_GRFREM); + if (lc == NULL) return; + + lc->grfrem.grfid = grfid; +} + +/** Logs adding of a GRF + * @param newg added GRF + */ +void GamelogGRFAdd(const GRFConfig *newg) +{ + assert(_gamelog_action_type == GLAT_LOAD || _gamelog_action_type == GLAT_START || _gamelog_action_type == GLAT_GRF); + + if (!IsLoggableGrfConfig(newg)) return; + + LoggedChange *lc = GamelogChange(GLCT_GRFADD); + if (lc == NULL) return; + + memcpy(&lc->grfadd, newg, sizeof(GRFIdentifier)); +} + +/** Logs loading compatible GRF + * (the same ID, but different MD5 hash) + * @param newg new (updated) GRF + */ +void GamelogGRFCompatible(const GRFIdentifier *newg) +{ + assert(_gamelog_action_type == GLAT_LOAD); + + LoggedChange *lc = GamelogChange(GLCT_GRFCOMPAT); + if (lc == NULL) return; + + memcpy(&lc->grfcompat, newg, sizeof(GRFIdentifier)); +} + +/** Logs changing GRF order + * @param grfid GRF that is moved + * @param offset how far it is moved, positive = moved down + */ +static void GamelogGRFMove(uint32 grfid, int32 offset) +{ + assert(_gamelog_action_type == GLAT_GRF); + + LoggedChange *lc = GamelogChange(GLCT_GRFMOVE); + if (lc == NULL) return; + + lc->grfmove.grfid = grfid; + lc->grfmove.offset = offset; +} + +/** Logs change in GRF parameters. + * Details about parameters changed are not stored + * @param grfid ID of GRF to store + */ +static void GamelogGRFParameters(uint32 grfid) +{ + assert(_gamelog_action_type == GLAT_GRF); + + LoggedChange *lc = GamelogChange(GLCT_GRFPARAM); + if (lc == NULL) return; + + lc->grfparam.grfid = grfid; +} + +/** Logs adding of list of GRFs. + * Useful when old savegame is loaded or when new game is started + * @param newg head of GRF linked list + */ +void GamelogGRFAddList(const GRFConfig *newg) +{ + assert(_gamelog_action_type == GLAT_START || _gamelog_action_type == GLAT_LOAD); + + for (; newg != NULL; newg = newg->next) { + GamelogGRFAdd(newg); + } +} + +/** List of GRFs using array of pointers instead of linked list */ +struct GRFList { + uint n; + const GRFConfig *grf[VARARRAY_SIZE]; +}; + +/** Generates GRFList + * @param grfc head of GRF linked list + */ +static GRFList *GenerateGRFList(const GRFConfig *grfc) +{ + uint n = 0; + for (const GRFConfig *g = grfc; g != NULL; g = g->next) { + if (IsLoggableGrfConfig(g)) n++; + } + + GRFList *list = (GRFList*)MallocT<byte>(sizeof(GRFList) + n * sizeof(GRFConfig*)); + + list->n = 0; + for (const GRFConfig *g = grfc; g != NULL; g = g->next) { + if (IsLoggableGrfConfig(g)) list->grf[list->n++] = g; + } + + return list; +} + +/** Compares two NewGRF lists and logs any change + * @param oldc original GRF list + * @param newc new GRF list + */ +void GamelogGRFUpdate(const GRFConfig *oldc, const GRFConfig *newc) +{ + GRFList *ol = GenerateGRFList(oldc); + GRFList *nl = GenerateGRFList(newc); + + uint o = 0, n = 0; + + while (o < ol->n && n < nl->n) { + const GRFConfig *og = ol->grf[o]; + const GRFConfig *ng = nl->grf[n]; + + if (og->grfid != ng->grfid) { + uint oi, ni; + for (oi = 0; oi < ol->n; oi++) { + if (ol->grf[oi]->grfid == nl->grf[n]->grfid) break; + } + if (oi < o) { + /* GRF was moved, this change has been logged already */ + n++; + continue; + } + if (oi == ol->n) { + /* GRF couldn't be found in the OLD list, GRF was ADDED */ + GamelogGRFAdd(nl->grf[n++]); + continue; + } + for (ni = 0; ni < nl->n; ni++) { + if (nl->grf[ni]->grfid == ol->grf[o]->grfid) break; + } + if (ni < n) { + /* GRF was moved, this change has been logged already */ + o++; + continue; + } + if (ni == nl->n) { + /* GRF couldn't be found in the NEW list, GRF was REMOVED */ + GamelogGRFRemove(ol->grf[o++]->grfid); + continue; + } + + /* o < oi < ol->n + * n < ni < nl->n */ + assert(ni > n && ni < nl->n); + assert(oi > o && oi < ol->n); + + ni -= n; // number of GRFs it was moved downwards + oi -= o; // number of GRFs it was moved upwards + + if (ni >= oi) { // prefer the one that is moved further + /* GRF was moved down */ + GamelogGRFMove(ol->grf[o++]->grfid, ni); + } else { + GamelogGRFMove(nl->grf[n++]->grfid, -(int)oi); + } + } else { + if (memcmp(og->md5sum, ng->md5sum, sizeof(og->md5sum)) != 0) { + /* md5sum changed, probably loading 'compatible' GRF */ + GamelogGRFCompatible(nl->grf[n]); + } + + if (og->num_params != ng->num_params || memcmp(og->param, ng->param, og->num_params * sizeof(og->param[0])) != 0) { + GamelogGRFParameters(ol->grf[o]->grfid); + } + + o++; + n++; + } + } + + while (o < ol->n) GamelogGRFRemove(ol->grf[o++]->grfid); // remaining GRFs were removed ... + while (n < nl->n) GamelogGRFAdd (nl->grf[n++]); // ... or added + + free(ol); + free(nl); +} + + +static const SaveLoad _glog_action_desc[] = { + SLE_VAR(LoggedAction, tick, SLE_UINT16), + SLE_END() +}; + +static const SaveLoad _glog_mode_desc[] = { + SLE_VAR(LoggedChange, mode.mode, SLE_UINT8), + SLE_VAR(LoggedChange, mode.landscape, SLE_UINT8), + SLE_END() +}; + +static const SaveLoad _glog_revision_desc[] = { + SLE_ARR(LoggedChange, revision.text, SLE_UINT8, NETWORK_REVISION_LENGTH), + SLE_VAR(LoggedChange, revision.newgrf, SLE_UINT32), + SLE_VAR(LoggedChange, revision.slver, SLE_UINT16), + SLE_VAR(LoggedChange, revision.modified, SLE_UINT8), + SLE_END() +}; + +static const SaveLoad _glog_oldver_desc[] = { + SLE_VAR(LoggedChange, oldver.type, SLE_UINT32), + SLE_VAR(LoggedChange, oldver.version, SLE_UINT32), + SLE_END() +}; + +static const SaveLoad _glog_patch_desc[] = { + SLE_STR(LoggedChange, patch.name, SLE_STR, 128), + SLE_VAR(LoggedChange, patch.oldval, SLE_INT32), + SLE_VAR(LoggedChange, patch.newval, SLE_INT32), + SLE_END() +}; + +static const SaveLoad _glog_grfadd_desc[] = { + SLE_VAR(LoggedChange, grfadd.grfid, SLE_UINT32 ), + SLE_ARR(LoggedChange, grfadd.md5sum, SLE_UINT8, 16), + SLE_END() +}; + +static const SaveLoad _glog_grfrem_desc[] = { + SLE_VAR(LoggedChange, grfrem.grfid, SLE_UINT32), + SLE_END() +}; + +static const SaveLoad _glog_grfcompat_desc[] = { + SLE_VAR(LoggedChange, grfcompat.grfid, SLE_UINT32 ), + SLE_ARR(LoggedChange, grfcompat.md5sum, SLE_UINT8, 16), + SLE_END() +}; + +static const SaveLoad _glog_grfparam_desc[] = { + SLE_VAR(LoggedChange, grfparam.grfid, SLE_UINT32), + SLE_END() +}; + +static const SaveLoad _glog_grfmove_desc[] = { + SLE_VAR(LoggedChange, grfmove.grfid, SLE_UINT32), + SLE_VAR(LoggedChange, grfmove.offset, SLE_INT32), + SLE_END() +}; + +static const SaveLoad *_glog_desc[] = { + _glog_mode_desc, + _glog_revision_desc, + _glog_oldver_desc, + _glog_patch_desc, + _glog_grfadd_desc, + _glog_grfrem_desc, + _glog_grfcompat_desc, + _glog_grfparam_desc, + _glog_grfmove_desc +}; + +assert_compile(lengthof(_glog_desc) == GLCT_END); + +static void Load_GLOG() +{ + assert(_gamelog_action == NULL); + assert(_gamelog_actions == 0); + + GamelogActionType at; + while ((at = (GamelogActionType)SlReadByte()) != GLAT_NONE) { + _gamelog_action = ReallocT(_gamelog_action, _gamelog_actions + 1); + LoggedAction *la = &_gamelog_action[_gamelog_actions++]; + + la->at = at; + + SlObject(la, _glog_action_desc); // has to be saved after 'DATE'! + la->change = NULL; + la->changes = 0; + + GamelogChangeType ct; + while ((ct = (GamelogChangeType)SlReadByte()) != GLCT_NONE) { + la->change = ReallocT(la->change, la->changes + 1); + + LoggedChange *lc = &la->change[la->changes++]; + lc->ct = ct; + + assert((uint)ct < GLCT_END); + + SlObject(lc, _glog_desc[ct]); + } + } +} + +static void Save_GLOG() +{ + const LoggedAction *laend = &_gamelog_action[_gamelog_actions]; + size_t length = 0; + + for (const LoggedAction *la = _gamelog_action; la != laend; la++) { + const LoggedChange *lcend = &la->change[la->changes]; + for (LoggedChange *lc = la->change; lc != lcend; lc++) { + assert((uint)lc->ct < lengthof(_glog_desc)); + length += SlCalcObjLength(lc, _glog_desc[lc->ct]) + 1; + } + length += 4; + } + length++; + + SlSetLength(length); + + for (LoggedAction *la = _gamelog_action; la != laend; la++) { + SlWriteByte(la->at); + SlObject(la, _glog_action_desc); + + const LoggedChange *lcend = &la->change[la->changes]; + for (LoggedChange *lc = la->change; lc != lcend; lc++) { + SlWriteByte(lc->ct); + assert((uint)lc->ct < GLCT_END); + SlObject(lc, _glog_desc[lc->ct]); + } + SlWriteByte(GLCT_NONE); + } + SlWriteByte(GLAT_NONE); +} + + +extern const ChunkHandler _gamelog_chunk_handlers[] = { + { 'GLOG', Save_GLOG, Load_GLOG, CH_RIFF | CH_LAST } +}; diff --git a/src/gamelog.h b/src/gamelog.h new file mode 100644 index 000000000..418ec1ee4 --- /dev/null +++ b/src/gamelog.h @@ -0,0 +1,46 @@ +/* $Id$ */ + +/** @file gamelog.h Functions to be called to log possibly unsafe game events */ + +#ifndef GAMELOG_H +#define GAMELOG_H + +#include "newgrf_config.h" + +enum GamelogActionType { + GLAT_START, ///< Game created + GLAT_LOAD, ///< Game loaded + GLAT_GRF, ///< GRF changed + GLAT_CHEAT, ///< Cheat was used + GLAT_PATCH, ///< Patches setting changed + GLAT_END, ///< So we know how many GLATs are there + GLAT_NONE = 0xFF, ///< No logging active; in savegames, end of list +}; + +void GamelogStartAction(GamelogActionType at); +void GamelogStopAction(); + +void GamelogReset(); + +typedef void GamelogPrintProc(const char *s); +void GamelogPrint(GamelogPrintProc *proc); // needed for WIN32 / WINCE crash.log + +void GamelogPrintDebug(); +void GamelogPrintConsole(); + +void GamelogRevision(); +void GamelogMode(); +void GamelogOldver(); +void GamelogPatch(const char *name, int32 oldval, int32 newval); + +void GamelogGRFUpdate(const GRFConfig *oldg, const GRFConfig *newg); +void GamelogGRFAddList(const GRFConfig *newg); +void GamelogGRFRemove(uint32 grfid); +void GamelogGRFAdd(const GRFConfig *newg); +void GamelogGRFCompatible(const GRFIdentifier *newg); + +void GamelogTestRevision(); +void GamelogTestMode(); +void GamelogTestGRF(); + +#endif /* GAMELOG_H */ diff --git a/src/misc.cpp b/src/misc.cpp index 9c1cb82ed..f89f233c5 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -25,6 +25,7 @@ #include "texteff.hpp" #include "gfx_func.h" #include "core/alloc_type.hpp" +#include "gamelog.h" #include "animated_tile_func.h" #include "tilehighlight_func.h" #include "core/bitmath_func.hpp" @@ -110,6 +111,13 @@ void InitializeGame(uint size_x, uint size_y, bool reset_date) InitializeLandscapeVariables(false); ResetObjectToPlace(); + + GamelogReset(); + GamelogStartAction(GLAT_START); + GamelogRevision(); + GamelogMode(); + GamelogGRFAddList(_grfconfig); + GamelogStopAction(); } diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index 0c8e0dbd2..0a09c82f4 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -12,6 +12,7 @@ #include "newgrf_config.h" #include "core/alloc_func.hpp" #include "string_func.h" +#include "gamelog.h" #include "network/network_type.h" #include "fileio.h" @@ -231,6 +232,7 @@ GRFListCompatibility IsGoodGRFConfigList() /* Non-found has precedence over compatibility load */ if (res != GLC_NOT_FOUND) res = GLC_COMPATIBLE; + GamelogGRFCompatible(f); goto compatible_grf; } @@ -238,6 +240,8 @@ GRFListCompatibility IsGoodGRFConfigList() md5sumToString(buf, lastof(buf), c->md5sum); DEBUG(grf, 0, "NewGRF %08X (%s) not found; checksum %s", BSWAP32(c->grfid), c->filename, buf); + GamelogGRFRemove(c->grfid); + c->status = GCS_NOT_FOUND; res = GLC_NOT_FOUND; } else { diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 70b0ac6b8..4b4765fe2 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -15,6 +15,7 @@ #include "core/alloc_func.hpp" #include "string_func.h" #include "gfx_func.h" +#include "gamelog.h" #include "table/strings.h" #include "table/sprites.h" @@ -564,8 +565,11 @@ static void NewGRFConfirmationCallback(Window *w, bool confirmed) GRFConfig *c; int i = 0; + GamelogStartAction(GLAT_GRF); + GamelogGRFUpdate(_grfconfig, nw->list); // log GRF changes CopyGRFConfigList(nw->orig_list, nw->list, false); ReloadNewGRFData(); + GamelogStopAction(); /* Show new, updated list */ for (c = nw->list; c != NULL && c != nw->sel; c = c->next, i++) {} diff --git a/src/openttd.cpp b/src/openttd.cpp index 5cf5fb6ea..50bebca62 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -54,6 +54,7 @@ #include "strings_func.h" #include "date_func.h" #include "vehicle_func.h" +#include "gamelog.h" #include "cheat_func.h" #include "animated_tile_func.h" #include "functions.h" @@ -304,6 +305,7 @@ static void ShutdownGame() UnInitializeAirports(); /* Uninitialize variables that are allocated dynamically */ + GamelogReset(); _Town_pool.CleanPool(); _Industry_pool.CleanPool(); _Station_pool.CleanPool(); @@ -1257,6 +1259,13 @@ bool AfterLoadGame() TileIndex map_size = MapSize(); Player *p; + if (CheckSavegameVersion(98)) GamelogOldver(); + + GamelogTestRevision(); + GamelogTestMode(); + + if (CheckSavegameVersion(98)) GamelogGRFAddList(_grfconfig); + /* in version 2.1 of the savegame, town owner was unified. */ if (CheckSavegameVersionOldStyle(2, 1)) ConvertTownOwner(); @@ -2420,6 +2429,8 @@ bool AfterLoadGame() } } + if (_debug_gamelog_level > 0) GamelogPrintDebug(); + return InitializeWindowsAndCaches(); } diff --git a/src/rev.cpp.in b/src/rev.cpp.in index 458f92e86..bae5c6085 100644 --- a/src/rev.cpp.in +++ b/src/rev.cpp.in @@ -23,6 +23,16 @@ const char _openttd_revision[] = "@@VERSION@@"; /** + * Let us know if current build was modified. This detection + * works even in the case when revision string is overriden by + * --revision argument. + * Value 0 means no modification, 1 is for unknown state + * (compiling from sources without any version control software) + * and 2 is for modified revision. + */ +const byte _openttd_revision_modified = @@MODIFIED@@; + +/** * The NewGRF revision of OTTD: * bits meaning. * 28-31 major version @@ -6,6 +6,7 @@ #define REV_H extern const char _openttd_revision[]; +extern const byte _openttd_revision_modified; extern const uint32 _openttd_newgrf_version; #endif /* REV_H */ diff --git a/src/saveload.cpp b/src/saveload.cpp index 76feecc38..5cec94cdf 100644 --- a/src/saveload.cpp +++ b/src/saveload.cpp @@ -32,10 +32,11 @@ #include "autoreplace_base.h" #include "statusbar_gui.h" #include <list> +#include "gamelog.h" #include "table/strings.h" -extern const uint16 SAVEGAME_VERSION = 97; +extern const uint16 SAVEGAME_VERSION = 98; SavegameType _savegame_type; ///< type of savegame we are loading @@ -765,7 +766,7 @@ static inline bool SlSkipVariableOnLoad(const SaveLoad *sld) * @param sld The SaveLoad description of the object so we know how to manipulate it * @return size of given objetc */ -static size_t SlCalcObjLength(const void *object, const SaveLoad *sld) +size_t SlCalcObjLength(const void *object, const SaveLoad *sld) { size_t length = 0; @@ -1286,6 +1287,7 @@ static void UninitWriteZlib() *******************************************/ /* these define the chunks */ +extern const ChunkHandler _gamelog_chunk_handlers[]; extern const ChunkHandler _misc_chunk_handlers[]; extern const ChunkHandler _name_chunk_handlers[]; extern const ChunkHandler _cheat_chunk_handlers[] ; @@ -1307,6 +1309,7 @@ extern const ChunkHandler _group_chunk_handlers[]; extern const ChunkHandler _cargopacket_chunk_handlers[]; static const ChunkHandler * const _chunk_handlers[] = { + _gamelog_chunk_handlers, _misc_chunk_handlers, _name_chunk_handlers, _cheat_chunk_handlers, @@ -1643,10 +1646,16 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb) /* Load a TTDLX or TTDPatch game */ if (mode == SL_OLD_LOAD) { InitializeGame(256, 256, true); // set a mapsize of 256x256 for TTDPatch games or it might get confused + GamelogReset(); if (!LoadOldSaveGame(filename)) return SL_REINIT; _sl_version = 0; _sl_minor_version = 0; - if (!AfterLoadGame()) return SL_REINIT; + GamelogStartAction(GLAT_LOAD); + if (!AfterLoadGame()) { + GamelogStopAction(); + return SL_REINIT; + } + GamelogStopAction(); return SL_OK; } @@ -1757,15 +1766,24 @@ SaveOrLoadResult SaveOrLoad(const char *filename, int mode, Subdirectory sb) * confuse old games */ InitializeGame(256, 256, true); + GamelogReset(); + SlLoadChunks(); fmt->uninit_read(); fclose(_sl.fh); + GamelogStartAction(GLAT_LOAD); + _savegame_type = SGT_OTTD; /* After loading fix up savegame for any internal changes that * might've occured since then. If it fails, load back the old game */ - if (!AfterLoadGame()) return SL_REINIT; + if (!AfterLoadGame()) { + GamelogStopAction(); + return SL_REINIT; + } + + GamelogStopAction(); } return SL_OK; diff --git a/src/saveload.h b/src/saveload.h index bed4f7737..dfe07350c 100644 --- a/src/saveload.h +++ b/src/saveload.h @@ -317,6 +317,7 @@ void SlAutolength(AutolengthProc *proc, void *arg); size_t SlGetFieldLength(); void SlSetLength(size_t length); size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld); +size_t SlCalcObjLength(const void *object, const SaveLoad *sld); byte SlReadByte(); void SlWriteByte(byte b); diff --git a/src/settings.cpp b/src/settings.cpp index 88596c9f1..b3cc06127 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -60,6 +60,7 @@ #include "sound/sound_driver.hpp" #include "music/music_driver.hpp" #include "blitter/factory.hpp" +#include "gamelog.h" #include "station_func.h" #include "table/strings.h" @@ -2128,8 +2129,20 @@ CommandCost CmdChangePatchSetting(TileIndex tile, uint32 flags, uint32 p1, uint3 if (flags & DC_EXEC) { GameSettings *s = (_game_mode == GM_MENU) ? &_settings_newgame : &_settings_game; void *var = GetVariableAddress(s, &sd->save); - Write_ValidateSetting(var, sd, (int32)p2); - if (sd->desc.proc != NULL) sd->desc.proc((int32)ReadValue(var, sd->save.conv)); + + int32 oldval = (int32)ReadValue(var, sd->save.conv); + int32 newval = (int32)p2; + + Write_ValidateSetting(var, sd, newval); + newval = (int32)ReadValue(var, sd->save.conv); + + if (sd->desc.proc != NULL) sd->desc.proc(newval); + + if ((sd->desc.flags & SGF_NO_NETWORK) && oldval != newval) { + GamelogStartAction(GLAT_PATCH); + GamelogPatch(sd->desc.name, oldval, newval); + GamelogStopAction(); + } InvalidateWindow(WC_GAME_OPTIONS, 0); } diff --git a/src/win32.cpp b/src/win32.cpp index 21ddfed03..5fa7edc82 100644 --- a/src/win32.cpp +++ b/src/win32.cpp @@ -25,6 +25,7 @@ #include "core/random_func.hpp" #include "core/bitmath_func.hpp" #include "string_func.h" +#include "gamelog.h" #include <ctype.h> #include <tchar.h> #include <errno.h> @@ -454,6 +455,15 @@ static void Handler2() extern bool CloseConsoleLogIfActive(); +static HANDLE _file_crash_log; + +static void GamelogPrintCrashLogProc(const char *s) +{ + DWORD num_written; + WriteFile(_file_crash_log, s, strlen(s), &num_written, NULL); + WriteFile(_file_crash_log, "\r\n", strlen("\r\n"), &num_written, NULL); +} + static LONG WINAPI ExceptionHandler(EXCEPTION_POINTERS *ep) { char *output; @@ -588,17 +598,15 @@ static LONG WINAPI ExceptionHandler(EXCEPTION_POINTERS *ep) os.dwOSVersionInfoSize = sizeof(os); GetVersionEx(&os); output += sprintf(output, "\r\nSystem information:\r\n" - " Windows version %d.%d %d %s\r\n", + " Windows version %d.%d %d %s\r\n\r\n", os.dwMajorVersion, os.dwMinorVersion, os.dwBuildNumber, os.szCSDVersion); } - { - HANDLE file = CreateFile(_T("crash.log"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0); + _file_crash_log = CreateFile(_T("crash.log"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0); + + if (_file_crash_log != INVALID_HANDLE_VALUE) { DWORD num_written; - if (file != INVALID_HANDLE_VALUE) { - WriteFile(file, _crash_msg, output - _crash_msg, &num_written, NULL); - CloseHandle(file); - } + WriteFile(_file_crash_log, _crash_msg, output - _crash_msg, &num_written, NULL); } #if !defined(_DEBUG) @@ -636,6 +644,11 @@ static LONG WINAPI ExceptionHandler(EXCEPTION_POINTERS *ep) } #endif + if (_file_crash_log != INVALID_HANDLE_VALUE) { + GamelogPrint(&GamelogPrintCrashLogProc); + CloseHandle(_file_crash_log); + } + /* Close any possible log files */ CloseConsoleLogIfActive(); |