00001
00002
00005 #include "stdafx.h"
00006 #include "openttd.h"
00007 #include "saveload/saveload.h"
00008 #include "core/alloc_func.hpp"
00009 #include "variables.h"
00010 #include "string_func.h"
00011 #include "settings_type.h"
00012 #include "gamelog.h"
00013 #include "gamelog_internal.h"
00014 #include "console_func.h"
00015 #include "debug.h"
00016 #include "rev.h"
00017
00018 #include <stdarg.h>
00019
00020 extern const uint16 SAVEGAME_VERSION;
00021
00022 extern SavegameType _savegame_type;
00023
00024 extern uint32 _ttdp_version;
00025 extern uint16 _sl_version;
00026 extern byte _sl_minor_version;
00027
00028
00029 static GamelogActionType _gamelog_action_type = GLAT_NONE;
00030
00031 LoggedAction *_gamelog_action = NULL;
00032 uint _gamelog_actions = 0;
00033 static LoggedAction *_current_action = NULL;
00034
00035
00040 void GamelogStartAction(GamelogActionType at)
00041 {
00042 assert(_gamelog_action_type == GLAT_NONE);
00043 _gamelog_action_type = at;
00044 }
00045
00048 void GamelogStopAction()
00049 {
00050 assert(_gamelog_action_type != GLAT_NONE);
00051
00052 bool print = _current_action != NULL;
00053
00054 _current_action = NULL;
00055 _gamelog_action_type = GLAT_NONE;
00056
00057 if (print) GamelogPrintDebug(5);
00058 }
00059
00062 void GamelogReset()
00063 {
00064 assert(_gamelog_action_type == GLAT_NONE);
00065
00066 for (uint i = 0; i < _gamelog_actions; i++) {
00067 const LoggedAction *la = &_gamelog_action[i];
00068 for (uint j = 0; j < la->changes; j++) {
00069 const LoggedChange *lc = &la->change[j];
00070 if (lc->ct == GLCT_SETTING) free(lc->setting.name);
00071 }
00072 free(la->change);
00073 }
00074
00075 free(_gamelog_action);
00076
00077 _gamelog_action = NULL;
00078 _gamelog_actions = 0;
00079 _current_action = NULL;
00080 }
00081
00082 enum {
00083 GAMELOG_BUF_LEN = 1024
00084 };
00085
00086 static int _dbgofs = 0;
00087
00088 static void AddDebugText(char *buf, const char *s, ...)
00089 {
00090 if (GAMELOG_BUF_LEN <= _dbgofs) return;
00091
00092 va_list va;
00093
00094 va_start(va, s);
00095 _dbgofs += vsnprintf(buf + _dbgofs, GAMELOG_BUF_LEN - _dbgofs, s, va);
00096 va_end(va);
00097 }
00098
00099
00103 static void PrintGrfFilename(char *buf, uint grfid)
00104 {
00105 const GRFConfig *gc = FindGRFConfig(grfid);
00106
00107 if (gc == NULL) return;
00108
00109 AddDebugText(buf, ", filename: %s", gc->filename);
00110 }
00111
00116 static void PrintGrfInfo(char *buf, uint grfid, const uint8 *md5sum)
00117 {
00118 char txt[40];
00119
00120 md5sumToString(txt, lastof(txt), md5sum);
00121
00122 AddDebugText(buf, "GRF ID %08X, checksum %s", BSWAP32(grfid), txt);
00123
00124 PrintGrfFilename(buf, grfid);
00125
00126 return;
00127 }
00128
00129
00131 static const char *la_text[] = {
00132 "new game started",
00133 "game loaded",
00134 "GRF config changed",
00135 "cheat was used",
00136 "settings changed",
00137 "GRF bug triggered",
00138 };
00139
00140 assert_compile(lengthof(la_text) == GLAT_END);
00141
00142
00144 void GamelogPrint(GamelogPrintProc *proc)
00145 {
00146 char buf[GAMELOG_BUF_LEN];
00147
00148 proc("---- gamelog start ----");
00149
00150 const LoggedAction *laend = &_gamelog_action[_gamelog_actions];
00151
00152 for (const LoggedAction *la = _gamelog_action; la != laend; la++) {
00153 assert((uint)la->at < GLAT_END);
00154
00155 snprintf(buf, GAMELOG_BUF_LEN, "Tick %u: %s", (uint)la->tick, la_text[(uint)la->at]);
00156 proc(buf);
00157
00158 const LoggedChange *lcend = &la->change[la->changes];
00159
00160 for (const LoggedChange *lc = la->change; lc != lcend; lc++) {
00161 _dbgofs = 0;
00162 AddDebugText(buf, " ");
00163
00164 switch (lc->ct) {
00165 default: NOT_REACHED();
00166 case GLCT_MODE:
00167 AddDebugText(buf, "New game mode: %u landscape: %u",
00168 (uint)lc->mode.mode, (uint)lc->mode.landscape);
00169 break;
00170
00171 case GLCT_REVISION:
00172 AddDebugText(buf, "Revision text changed to %s, savegame version %u, ",
00173 lc->revision.text, lc->revision.slver);
00174
00175 switch (lc->revision.modified) {
00176 case 0: AddDebugText(buf, "not "); break;
00177 case 1: AddDebugText(buf, "maybe "); break;
00178 default: break;
00179 }
00180
00181 AddDebugText(buf, "modified, _openttd_newgrf_version = 0x%08x", lc->revision.newgrf);
00182 break;
00183
00184 case GLCT_OLDVER:
00185 AddDebugText(buf, "Conversion from ");
00186 switch (lc->oldver.type) {
00187 default: NOT_REACHED();
00188 case SGT_OTTD:
00189 AddDebugText(buf, "OTTD savegame without gamelog: version %u, %u",
00190 GB(lc->oldver.version, 8, 16), GB(lc->oldver.version, 0, 8));
00191 break;
00192
00193 case SGT_TTO:
00194 AddDebugText(buf, "TTO savegame");
00195 break;
00196
00197 case SGT_TTD:
00198 AddDebugText(buf, "TTD savegame");
00199 break;
00200
00201 case SGT_TTDP1:
00202 case SGT_TTDP2:
00203 AddDebugText(buf, "TTDP savegame, %s format",
00204 lc->oldver.type == SGT_TTDP1 ? "old" : "new");
00205 if (lc->oldver.version != 0) {
00206 AddDebugText(buf, ", TTDP version %u.%u.%u.%u",
00207 GB(lc->oldver.version, 24, 8), GB(lc->oldver.version, 20, 4),
00208 GB(lc->oldver.version, 16, 4), GB(lc->oldver.version, 0, 16));
00209 }
00210 break;
00211 }
00212 break;
00213
00214 case GLCT_SETTING:
00215 AddDebugText(buf, "Setting changed: %s : %d -> %d", lc->setting.name, lc->setting.oldval, lc->setting.newval);
00216 break;
00217
00218 case GLCT_GRFADD:
00219 AddDebugText(buf, "Added NewGRF: ");
00220 PrintGrfInfo(buf, lc->grfadd.grfid, lc->grfadd.md5sum);
00221 break;
00222
00223 case GLCT_GRFREM:
00224 AddDebugText(buf, "Removed NewGRF: %08X", BSWAP32(lc->grfrem.grfid));
00225 PrintGrfFilename(buf, lc->grfrem.grfid);
00226 break;
00227
00228 case GLCT_GRFCOMPAT:
00229 AddDebugText(buf, "Compatible NewGRF loaded: ");
00230 PrintGrfInfo(buf, lc->grfcompat.grfid, lc->grfcompat.md5sum);
00231 break;
00232
00233 case GLCT_GRFPARAM:
00234 AddDebugText(buf, "GRF parameter changed: %08X", BSWAP32(lc->grfparam.grfid));
00235 PrintGrfFilename(buf, lc->grfparam.grfid);
00236 break;
00237
00238 case GLCT_GRFMOVE:
00239 AddDebugText(buf, "GRF order changed: %08X moved %d places %s",
00240 BSWAP32(lc->grfmove.grfid), abs(lc->grfmove.offset), lc->grfmove.offset >= 0 ? "down" : "up" );
00241 PrintGrfFilename(buf, lc->grfmove.grfid);
00242 break;
00243
00244 case GLCT_GRFBUG:
00245 switch (lc->grfbug.bug) {
00246 default: NOT_REACHED();
00247 case GBUG_VEH_LENGTH:
00248 AddDebugText(buf, "Rail vehicle changes length outside a depot: GRF ID %08X, internal ID 0x%X", BSWAP32(lc->grfbug.grfid), (uint)lc->grfbug.data);
00249 PrintGrfFilename(buf, lc->grfbug.grfid);
00250 break;
00251 }
00252 }
00253
00254 proc(buf);
00255 }
00256 }
00257
00258 proc("---- gamelog end ----");
00259 }
00260
00261
00262 static void GamelogPrintConsoleProc(const char *s)
00263 {
00264 IConsolePrint(CC_WARNING, s);
00265 }
00266
00267 void GamelogPrintConsole()
00268 {
00269 GamelogPrint(&GamelogPrintConsoleProc);
00270 }
00271
00272 static int _gamelog_print_level = 0;
00273
00274 static void GamelogPrintDebugProc(const char *s)
00275 {
00276 DEBUG(gamelog, _gamelog_print_level, s);
00277 }
00278
00279
00285 void GamelogPrintDebug(int level)
00286 {
00287 _gamelog_print_level = level;
00288 GamelogPrint(&GamelogPrintDebugProc);
00289 }
00290
00291
00297 static LoggedChange *GamelogChange(GamelogChangeType ct)
00298 {
00299 if (_current_action == NULL) {
00300 if (_gamelog_action_type == GLAT_NONE) return NULL;
00301
00302 _gamelog_action = ReallocT(_gamelog_action, _gamelog_actions + 1);
00303 _current_action = &_gamelog_action[_gamelog_actions++];
00304
00305 _current_action->at = _gamelog_action_type;
00306 _current_action->tick = _tick_counter;
00307 _current_action->change = NULL;
00308 _current_action->changes = 0;
00309 }
00310
00311 _current_action->change = ReallocT(_current_action->change, _current_action->changes + 1);
00312
00313 LoggedChange *lc = &_current_action->change[_current_action->changes++];
00314 lc->ct = ct;
00315
00316 return lc;
00317 }
00318
00319
00323 void GamelogRevision()
00324 {
00325 assert(_gamelog_action_type == GLAT_START || _gamelog_action_type == GLAT_LOAD);
00326
00327 LoggedChange *lc = GamelogChange(GLCT_REVISION);
00328 if (lc == NULL) return;
00329
00330 memset(lc->revision.text, 0, sizeof(lc->revision.text));
00331 strecpy(lc->revision.text, _openttd_revision, lastof(lc->revision.text));
00332 lc->revision.slver = SAVEGAME_VERSION;
00333 lc->revision.modified = _openttd_revision_modified;
00334 lc->revision.newgrf = _openttd_newgrf_version;
00335 }
00336
00339 void GamelogMode()
00340 {
00341 assert(_gamelog_action_type == GLAT_START || _gamelog_action_type == GLAT_LOAD || _gamelog_action_type == GLAT_CHEAT);
00342
00343 LoggedChange *lc = GamelogChange(GLCT_MODE);
00344 if (lc == NULL) return;
00345
00346 lc->mode.mode = _game_mode;
00347 lc->mode.landscape = _settings_game.game_creation.landscape;
00348 }
00349
00352 void GamelogOldver()
00353 {
00354 assert(_gamelog_action_type == GLAT_LOAD);
00355
00356 LoggedChange *lc = GamelogChange(GLCT_OLDVER);
00357 if (lc == NULL) return;
00358
00359 lc->oldver.type = _savegame_type;
00360 lc->oldver.version = (_savegame_type == SGT_OTTD ? ((uint32)_sl_version << 8 | _sl_minor_version) : _ttdp_version);
00361 }
00362
00368 void GamelogSetting(const char *name, int32 oldval, int32 newval)
00369 {
00370 assert(_gamelog_action_type == GLAT_SETTING);
00371
00372 LoggedChange *lc = GamelogChange(GLCT_SETTING);
00373 if (lc == NULL) return;
00374
00375 lc->setting.name = strdup(name);
00376 lc->setting.oldval = oldval;
00377 lc->setting.newval = newval;
00378 }
00379
00380
00384 void GamelogTestRevision()
00385 {
00386 const LoggedChange *rev = NULL;
00387
00388 const LoggedAction *laend = &_gamelog_action[_gamelog_actions];
00389 for (const LoggedAction *la = _gamelog_action; la != laend; la++) {
00390 const LoggedChange *lcend = &la->change[la->changes];
00391 for (const LoggedChange *lc = la->change; lc != lcend; lc++) {
00392 if (lc->ct == GLCT_REVISION) rev = lc;
00393 }
00394 }
00395
00396 if (rev == NULL || strcmp(rev->revision.text, _openttd_revision) != 0 ||
00397 rev->revision.modified != _openttd_revision_modified ||
00398 rev->revision.newgrf != _openttd_newgrf_version) {
00399 GamelogRevision();
00400 }
00401 }
00402
00406 void GamelogTestMode()
00407 {
00408 const LoggedChange *mode = NULL;
00409
00410 const LoggedAction *laend = &_gamelog_action[_gamelog_actions];
00411 for (const LoggedAction *la = _gamelog_action; la != laend; la++) {
00412 const LoggedChange *lcend = &la->change[la->changes];
00413 for (const LoggedChange *lc = la->change; lc != lcend; lc++) {
00414 if (lc->ct == GLCT_MODE) mode = lc;
00415 }
00416 }
00417
00418 if (mode == NULL || mode->mode.mode != _game_mode || mode->mode.landscape != _settings_game.game_creation.landscape) GamelogMode();
00419 }
00420
00421
00427 static void GamelogGRFBug(uint32 grfid, byte bug, uint64 data)
00428 {
00429 assert(_gamelog_action_type == GLAT_GRFBUG);
00430
00431 LoggedChange *lc = GamelogChange(GLCT_GRFBUG);
00432 if (lc == NULL) return;
00433
00434 lc->grfbug.data = data;
00435 lc->grfbug.grfid = grfid;
00436 lc->grfbug.bug = bug;
00437 }
00438
00446 bool GamelogGRFBugReverse(uint32 grfid, uint16 internal_id)
00447 {
00448 const LoggedAction *laend = &_gamelog_action[_gamelog_actions];
00449 for (const LoggedAction *la = _gamelog_action; la != laend; la++) {
00450 const LoggedChange *lcend = &la->change[la->changes];
00451 for (const LoggedChange *lc = la->change; lc != lcend; lc++) {
00452 if (lc->ct == GLCT_GRFBUG && lc->grfbug.grfid == grfid &&
00453 lc->grfbug.bug == GBUG_VEH_LENGTH && lc->grfbug.data == internal_id) {
00454 return false;
00455 }
00456 }
00457 }
00458
00459 GamelogStartAction(GLAT_GRFBUG);
00460 GamelogGRFBug(grfid, GBUG_VEH_LENGTH, internal_id);
00461 GamelogStopAction();
00462
00463 return true;
00464 }
00465
00466
00471 static inline bool IsLoggableGrfConfig(const GRFConfig *g)
00472 {
00473 return !HasBit(g->flags, GCF_STATIC) && g->status != GCS_NOT_FOUND;
00474 }
00475
00479 void GamelogGRFRemove(uint32 grfid)
00480 {
00481 assert(_gamelog_action_type == GLAT_LOAD || _gamelog_action_type == GLAT_GRF);
00482
00483 LoggedChange *lc = GamelogChange(GLCT_GRFREM);
00484 if (lc == NULL) return;
00485
00486 lc->grfrem.grfid = grfid;
00487 }
00488
00492 void GamelogGRFAdd(const GRFConfig *newg)
00493 {
00494 assert(_gamelog_action_type == GLAT_LOAD || _gamelog_action_type == GLAT_START || _gamelog_action_type == GLAT_GRF);
00495
00496 if (!IsLoggableGrfConfig(newg)) return;
00497
00498 LoggedChange *lc = GamelogChange(GLCT_GRFADD);
00499 if (lc == NULL) return;
00500
00501 memcpy(&lc->grfadd, newg, sizeof(GRFIdentifier));
00502 }
00503
00508 void GamelogGRFCompatible(const GRFIdentifier *newg)
00509 {
00510 assert(_gamelog_action_type == GLAT_LOAD || _gamelog_action_type == GLAT_GRF);
00511
00512 LoggedChange *lc = GamelogChange(GLCT_GRFCOMPAT);
00513 if (lc == NULL) return;
00514
00515 memcpy(&lc->grfcompat, newg, sizeof(GRFIdentifier));
00516 }
00517
00522 static void GamelogGRFMove(uint32 grfid, int32 offset)
00523 {
00524 assert(_gamelog_action_type == GLAT_GRF);
00525
00526 LoggedChange *lc = GamelogChange(GLCT_GRFMOVE);
00527 if (lc == NULL) return;
00528
00529 lc->grfmove.grfid = grfid;
00530 lc->grfmove.offset = offset;
00531 }
00532
00537 static void GamelogGRFParameters(uint32 grfid)
00538 {
00539 assert(_gamelog_action_type == GLAT_GRF);
00540
00541 LoggedChange *lc = GamelogChange(GLCT_GRFPARAM);
00542 if (lc == NULL) return;
00543
00544 lc->grfparam.grfid = grfid;
00545 }
00546
00551 void GamelogGRFAddList(const GRFConfig *newg)
00552 {
00553 assert(_gamelog_action_type == GLAT_START || _gamelog_action_type == GLAT_LOAD);
00554
00555 for (; newg != NULL; newg = newg->next) {
00556 GamelogGRFAdd(newg);
00557 }
00558 }
00559
00561 struct GRFList {
00562 uint n;
00563 const GRFConfig *grf[VARARRAY_SIZE];
00564 };
00565
00569 static GRFList *GenerateGRFList(const GRFConfig *grfc)
00570 {
00571 uint n = 0;
00572 for (const GRFConfig *g = grfc; g != NULL; g = g->next) {
00573 if (IsLoggableGrfConfig(g)) n++;
00574 }
00575
00576 GRFList *list = (GRFList*)MallocT<byte>(sizeof(GRFList) + n * sizeof(GRFConfig*));
00577
00578 list->n = 0;
00579 for (const GRFConfig *g = grfc; g != NULL; g = g->next) {
00580 if (IsLoggableGrfConfig(g)) list->grf[list->n++] = g;
00581 }
00582
00583 return list;
00584 }
00585
00590 void GamelogGRFUpdate(const GRFConfig *oldc, const GRFConfig *newc)
00591 {
00592 GRFList *ol = GenerateGRFList(oldc);
00593 GRFList *nl = GenerateGRFList(newc);
00594
00595 uint o = 0, n = 0;
00596
00597 while (o < ol->n && n < nl->n) {
00598 const GRFConfig *og = ol->grf[o];
00599 const GRFConfig *ng = nl->grf[n];
00600
00601 if (og->grfid != ng->grfid) {
00602 uint oi, ni;
00603 for (oi = 0; oi < ol->n; oi++) {
00604 if (ol->grf[oi]->grfid == nl->grf[n]->grfid) break;
00605 }
00606 if (oi < o) {
00607
00608 n++;
00609 continue;
00610 }
00611 if (oi == ol->n) {
00612
00613 GamelogGRFAdd(nl->grf[n++]);
00614 continue;
00615 }
00616 for (ni = 0; ni < nl->n; ni++) {
00617 if (nl->grf[ni]->grfid == ol->grf[o]->grfid) break;
00618 }
00619 if (ni < n) {
00620
00621 o++;
00622 continue;
00623 }
00624 if (ni == nl->n) {
00625
00626 GamelogGRFRemove(ol->grf[o++]->grfid);
00627 continue;
00628 }
00629
00630
00631
00632 assert(ni > n && ni < nl->n);
00633 assert(oi > o && oi < ol->n);
00634
00635 ni -= n;
00636 oi -= o;
00637
00638 if (ni >= oi) {
00639
00640 GamelogGRFMove(ol->grf[o++]->grfid, ni);
00641 } else {
00642 GamelogGRFMove(nl->grf[n++]->grfid, -(int)oi);
00643 }
00644 } else {
00645 if (memcmp(og->md5sum, ng->md5sum, sizeof(og->md5sum)) != 0) {
00646
00647 GamelogGRFCompatible(nl->grf[n]);
00648 }
00649
00650 if (og->num_params != ng->num_params || memcmp(og->param, ng->param, og->num_params * sizeof(og->param[0])) != 0) {
00651 GamelogGRFParameters(ol->grf[o]->grfid);
00652 }
00653
00654 o++;
00655 n++;
00656 }
00657 }
00658
00659 while (o < ol->n) GamelogGRFRemove(ol->grf[o++]->grfid);
00660 while (n < nl->n) GamelogGRFAdd (nl->grf[n++]);
00661
00662 free(ol);
00663 free(nl);
00664 }
00665
00671 void GamelogGetOriginalGRFMD5Checksum(uint32 grfid, byte *md5sum)
00672 {
00673 const LoggedAction *la = &_gamelog_action[_gamelog_actions - 1];
00674
00675 assert(_gamelog_actions > 0);
00676
00677 do {
00678 const LoggedChange *lc = &la->change[la->changes - 1];
00679
00680 assert(la->changes > 0);
00681
00682 do {
00683 if (lc->ct == GLCT_GRFADD && lc->grfadd.grfid == grfid) {
00684 memcpy(md5sum, lc->grfadd.md5sum, sizeof(lc->grfadd.md5sum));
00685 return;
00686 }
00687 } while (lc-- != la->change);
00688 } while (la-- != _gamelog_action);
00689
00690 NOT_REACHED();
00691 }