mirror of https://github.com/OpenTTD/OpenTTD.git
Codechange: rework Gamelog changes from union to classes
This commit is contained in:
parent
389b66bd16
commit
52a7c69974
464
src/gamelog.cpp
464
src/gamelog.cpp
|
@ -32,11 +32,6 @@ extern byte _sl_minor_version; ///< the minor savegame version, DO NOT USE!
|
|||
|
||||
Gamelog _gamelog; ///< Gamelog instance
|
||||
|
||||
LoggedChange::~LoggedChange()
|
||||
{
|
||||
if (this->ct == GLCT_SETTING) free(this->setting.name);
|
||||
}
|
||||
|
||||
Gamelog::Gamelog()
|
||||
{
|
||||
this->data = std::make_unique<GamelogInternalData>();
|
||||
|
@ -50,26 +45,18 @@ Gamelog::~Gamelog()
|
|||
|
||||
/**
|
||||
* Return the revision string for the current client version, for use in gamelog.
|
||||
* The string returned is at most GAMELOG_REVISION_LENGTH bytes long.
|
||||
*/
|
||||
static const char * GetGamelogRevisionString()
|
||||
static std::string GetGamelogRevisionString()
|
||||
{
|
||||
/* Allocate a buffer larger than necessary (git revision hash is 40 bytes) to avoid truncation later */
|
||||
static char gamelog_revision[48] = { 0 };
|
||||
static_assert(lengthof(gamelog_revision) > GAMELOG_REVISION_LENGTH);
|
||||
|
||||
if (IsReleasedVersion()) {
|
||||
return _openttd_revision;
|
||||
} else if (gamelog_revision[0] == 0) {
|
||||
/* Prefix character indication revision status */
|
||||
assert(_openttd_revision_modified < 3);
|
||||
gamelog_revision[0] = "gum"[_openttd_revision_modified]; // g = "git", u = "unknown", m = "modified"
|
||||
/* Append the revision hash */
|
||||
strecat(gamelog_revision, _openttd_revision_hash, lastof(gamelog_revision));
|
||||
/* Truncate string to GAMELOG_REVISION_LENGTH bytes */
|
||||
gamelog_revision[GAMELOG_REVISION_LENGTH - 1] = '\0';
|
||||
}
|
||||
return gamelog_revision;
|
||||
|
||||
/* Prefix character indication revision status */
|
||||
assert(_openttd_revision_modified < 3);
|
||||
return fmt::format("{}{}",
|
||||
"gum"[_openttd_revision_modified], // g = "git", u = "unknown", m = "modified"
|
||||
_openttd_revision_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -115,29 +102,28 @@ void Gamelog::Reset()
|
|||
|
||||
/**
|
||||
* Adds the GRF ID, checksum and filename if found to the output iterator
|
||||
* @param outputIterator The iterator to add the GRF info to.
|
||||
* @param output_iterator The iterator to add the GRF info to.
|
||||
* @param last The end of the buffer
|
||||
* @param grfid GRF ID
|
||||
* @param md5sum array of md5sum to print, if known
|
||||
* @param gc GrfConfig, if known
|
||||
*/
|
||||
template <typename T>
|
||||
static void AddGrfInfo(T &outputIterator, uint grfid, const uint8 *md5sum, const GRFConfig *gc)
|
||||
static void AddGrfInfo(std::back_insert_iterator<std::string> &output_iterator, uint32_t grfid, const uint8_t *md5sum, const GRFConfig *gc)
|
||||
{
|
||||
if (md5sum != nullptr) {
|
||||
fmt::format_to(outputIterator, "GRF ID {:08X}, checksum {}", BSWAP32(grfid), MD5SumToString(md5sum));
|
||||
fmt::format_to(output_iterator, "GRF ID {:08X}, checksum {}", BSWAP32(grfid), MD5SumToString(md5sum));
|
||||
} else {
|
||||
fmt::format_to(outputIterator, "GRF ID {:08X}", BSWAP32(grfid));
|
||||
fmt::format_to(output_iterator, "GRF ID {:08X}", BSWAP32(grfid));
|
||||
}
|
||||
|
||||
if (gc != nullptr) {
|
||||
fmt::format_to(outputIterator, ", filename: {} (md5sum matches)", gc->filename);
|
||||
fmt::format_to(output_iterator, ", filename: {} (md5sum matches)", gc->filename);
|
||||
} else {
|
||||
gc = FindGRFConfig(grfid, FGCM_ANY);
|
||||
if (gc != nullptr) {
|
||||
fmt::format_to(outputIterator, ", filename: {} (matches GRFID only)", gc->filename);
|
||||
fmt::format_to(output_iterator, ", filename: {} (matches GRFID only)", gc->filename);
|
||||
} else {
|
||||
fmt::format_to(outputIterator, ", unknown GRF");
|
||||
fmt::format_to(output_iterator, ", unknown GRF");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -156,22 +142,6 @@ static const char * const la_text[] = {
|
|||
|
||||
static_assert(lengthof(la_text) == GLAT_END);
|
||||
|
||||
/**
|
||||
* Information about the presence of a Grf at a certain point during gamelog history
|
||||
* Note about missing Grfs:
|
||||
* Changes to missing Grfs are not logged including manual removal of the Grf.
|
||||
* So if the gamelog tells a Grf is missing we do not know whether it was readded or completely removed
|
||||
* at some later point.
|
||||
*/
|
||||
struct GRFPresence{
|
||||
const GRFConfig *gc; ///< GRFConfig, if known
|
||||
bool was_missing; ///< Grf was missing during some gameload in the past
|
||||
|
||||
GRFPresence(const GRFConfig *gc) : gc(gc), was_missing(false) {}
|
||||
GRFPresence() = default;
|
||||
};
|
||||
typedef SmallMap<uint32, GRFPresence> GrfIDMapping;
|
||||
|
||||
/**
|
||||
* Prints active gamelog
|
||||
* @param proc the procedure to draw with
|
||||
|
@ -183,145 +153,14 @@ void Gamelog::Print(std::function<void(const std::string &)> proc)
|
|||
proc("---- gamelog start ----");
|
||||
|
||||
for (const LoggedAction &la : this->data->action) {
|
||||
assert((uint)la.at < GLAT_END);
|
||||
assert(la.at < GLAT_END);
|
||||
|
||||
proc(fmt::format("Tick {}: {}", la.tick, la_text[la.at]));
|
||||
|
||||
for (const LoggedChange &lc : la.change) {
|
||||
for (auto &lc : la.change) {
|
||||
std::string message;
|
||||
auto outputIterator = std::back_inserter(message);
|
||||
|
||||
switch (lc.ct) {
|
||||
default: NOT_REACHED();
|
||||
case GLCT_MODE:
|
||||
/* Changing landscape, or going from scenario editor to game or back. */
|
||||
fmt::format_to(outputIterator, "New game mode: {} landscape: {}", lc.mode.mode, lc.mode.landscape);
|
||||
break;
|
||||
|
||||
case GLCT_REVISION:
|
||||
/* The game was loaded in a diffferent version than before. */
|
||||
fmt::format_to(outputIterator, "Revision text changed to {}, savegame version {}, ",
|
||||
lc.revision.text, lc.revision.slver);
|
||||
|
||||
switch (lc.revision.modified) {
|
||||
case 0: message += "not "; break;
|
||||
case 1: message += "maybe "; break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
fmt::format_to(outputIterator, "modified, _openttd_newgrf_version = 0x{:08x}", lc.revision.newgrf);
|
||||
break;
|
||||
|
||||
case GLCT_OLDVER:
|
||||
/* The game was loaded from before 0.7.0-beta1. */
|
||||
message += "Conversion from ";
|
||||
switch (lc.oldver.type) {
|
||||
default: NOT_REACHED();
|
||||
case SGT_OTTD:
|
||||
fmt::format_to(outputIterator, "OTTD savegame without gamelog: version {}, {}",
|
||||
GB(lc.oldver.version, 8, 16), GB(lc.oldver.version, 0, 8));
|
||||
break;
|
||||
|
||||
case SGT_TTO:
|
||||
message += "TTO savegame";
|
||||
break;
|
||||
|
||||
case SGT_TTD:
|
||||
message += "TTD savegame";
|
||||
break;
|
||||
|
||||
case SGT_TTDP1:
|
||||
case SGT_TTDP2:
|
||||
fmt::format_to(outputIterator, "TTDP savegame, {} format",
|
||||
lc.oldver.type == SGT_TTDP1 ? "old" : "new");
|
||||
if (lc.oldver.version != 0) {
|
||||
fmt::format_to(outputIterator, ", TTDP version {}.{}.{}.{}",
|
||||
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_SETTING:
|
||||
/* A setting with the SF_NO_NETWORK flag got changed; these settings usually affect NewGRFs, such as road side or wagon speed limits. */
|
||||
fmt::format_to(outputIterator, "Setting changed: {} : {} -> {}", lc.setting.name, lc.setting.oldval, lc.setting.newval);
|
||||
break;
|
||||
|
||||
case GLCT_GRFADD: {
|
||||
/* A NewGRF got added to the game, either at the start of the game (never an issue), or later on when it could be an issue. */
|
||||
const GRFConfig *gc = FindGRFConfig(lc.grfadd.grfid, FGCM_EXACT, lc.grfadd.md5sum);
|
||||
message += "Added NewGRF: ";
|
||||
AddGrfInfo(outputIterator, lc.grfadd.grfid, lc.grfadd.md5sum, gc);
|
||||
GrfIDMapping::Pair *gm = grf_names.Find(lc.grfrem.grfid);
|
||||
if (gm != grf_names.End() && !gm->second.was_missing) message += ". Gamelog inconsistency: GrfID was already added!";
|
||||
grf_names[lc.grfadd.grfid] = gc;
|
||||
break;
|
||||
}
|
||||
|
||||
case GLCT_GRFREM: {
|
||||
/* A NewGRF got removed from the game, either manually or by it missing when loading the game. */
|
||||
GrfIDMapping::Pair *gm = grf_names.Find(lc.grfrem.grfid);
|
||||
message += la.at == GLAT_LOAD ? "Missing NewGRF: " : "Removed NewGRF: ";
|
||||
AddGrfInfo(outputIterator, lc.grfrem.grfid, nullptr, gm != grf_names.End() ? gm->second.gc : nullptr);
|
||||
if (gm == grf_names.End()) {
|
||||
message += ". Gamelog inconsistency: GrfID was never added!";
|
||||
} else {
|
||||
if (la.at == GLAT_LOAD) {
|
||||
/* Missing grfs on load are not removed from the configuration */
|
||||
gm->second.was_missing = true;
|
||||
} else {
|
||||
grf_names.Erase(gm);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case GLCT_GRFCOMPAT: {
|
||||
/* Another version of the same NewGRF got loaded. */
|
||||
const GRFConfig *gc = FindGRFConfig(lc.grfadd.grfid, FGCM_EXACT, lc.grfadd.md5sum);
|
||||
message += "Compatible NewGRF loaded: ";
|
||||
AddGrfInfo(outputIterator, lc.grfcompat.grfid, lc.grfcompat.md5sum, gc);
|
||||
if (!grf_names.Contains(lc.grfcompat.grfid)) message += ". Gamelog inconsistency: GrfID was never added!";
|
||||
grf_names[lc.grfcompat.grfid] = gc;
|
||||
break;
|
||||
}
|
||||
|
||||
case GLCT_GRFPARAM: {
|
||||
/* A parameter of a NewGRF got changed after the game was started. */
|
||||
GrfIDMapping::Pair *gm = grf_names.Find(lc.grfrem.grfid);
|
||||
message += "GRF parameter changed: ";
|
||||
AddGrfInfo(outputIterator, lc.grfparam.grfid, nullptr, gm != grf_names.End() ? gm->second.gc : nullptr);
|
||||
if (gm == grf_names.End()) message += ". Gamelog inconsistency: GrfID was never added!";
|
||||
break;
|
||||
}
|
||||
|
||||
case GLCT_GRFMOVE: {
|
||||
/* The order of NewGRFs got changed, which might cause some other NewGRFs to behave differently. */
|
||||
GrfIDMapping::Pair *gm = grf_names.Find(lc.grfrem.grfid);
|
||||
fmt::format_to(outputIterator, "GRF order changed: {:08X} moved {} places {}",
|
||||
BSWAP32(lc.grfmove.grfid), abs(lc.grfmove.offset), lc.grfmove.offset >= 0 ? "down" : "up" );
|
||||
AddGrfInfo(outputIterator, lc.grfmove.grfid, nullptr, gm != grf_names.End() ? gm->second.gc : nullptr);
|
||||
if (gm == grf_names.End()) message += ". Gamelog inconsistency: GrfID was never added!";
|
||||
break;
|
||||
}
|
||||
|
||||
case GLCT_GRFBUG: {
|
||||
/* A specific bug in a NewGRF, that could cause wide spread problems, has been noted during the execution of the game. */
|
||||
GrfIDMapping::Pair *gm = grf_names.Find(lc.grfrem.grfid);
|
||||
assert(lc.grfbug.bug == GBUG_VEH_LENGTH);
|
||||
|
||||
fmt::format_to(outputIterator, "Rail vehicle changes length outside a depot: GRF ID {:08X}, internal ID 0x{:X}", BSWAP32(lc.grfbug.grfid), (uint)lc.grfbug.data);
|
||||
AddGrfInfo(outputIterator, lc.grfbug.grfid, nullptr, gm != grf_names.End() ? gm->second.gc : nullptr);
|
||||
if (gm == grf_names.End()) message += ". Gamelog inconsistency: GrfID was never added!";
|
||||
break;
|
||||
}
|
||||
|
||||
case GLCT_EMERGENCY:
|
||||
/* At one point the savegame was made during the handling of a game crash.
|
||||
* The generic code already mentioned the emergency savegame, and there is no extra information to log. */
|
||||
break;
|
||||
}
|
||||
auto output_iterator = std::back_inserter(message);
|
||||
lc->FormatTo(output_iterator, grf_names, la.at);
|
||||
|
||||
proc(message);
|
||||
}
|
||||
|
@ -331,6 +170,140 @@ void Gamelog::Print(std::function<void(const std::string &)> proc)
|
|||
}
|
||||
|
||||
|
||||
/* virtual */ void LoggedChangeMode::FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type)
|
||||
{
|
||||
/* Changing landscape, or going from scenario editor to game or back. */
|
||||
fmt::format_to(output_iterator, "New game mode: {} landscape: {}", this->mode, this->landscape);
|
||||
}
|
||||
|
||||
/* virtual */ void LoggedChangeRevision::FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type)
|
||||
{
|
||||
/* The game was loaded in a diffferent version than before. */
|
||||
fmt::format_to(output_iterator, "Revision text changed to {}, savegame version {}, ",
|
||||
this->text, this->slver);
|
||||
|
||||
switch (this->modified) {
|
||||
case 0: fmt::format_to(output_iterator, "not "); break;
|
||||
case 1: fmt::format_to(output_iterator, "maybe "); break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
fmt::format_to(output_iterator, "modified, _openttd_newgrf_version = 0x{:08x}", this->newgrf);
|
||||
}
|
||||
|
||||
/* virtual */ void LoggedChangeOldVersion::FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type)
|
||||
{
|
||||
/* The game was loaded from before 0.7.0-beta1. */
|
||||
fmt::format_to(output_iterator, "Conversion from ");
|
||||
switch (this->type) {
|
||||
default: NOT_REACHED();
|
||||
case SGT_OTTD:
|
||||
fmt::format_to(output_iterator, "OTTD savegame without gamelog: version {}, {}",
|
||||
GB(this->version, 8, 16), GB(this->version, 0, 8));
|
||||
break;
|
||||
|
||||
case SGT_TTO:
|
||||
fmt::format_to(output_iterator, "TTO savegame");
|
||||
break;
|
||||
|
||||
case SGT_TTD:
|
||||
fmt::format_to(output_iterator, "TTD savegame");
|
||||
break;
|
||||
|
||||
case SGT_TTDP1:
|
||||
case SGT_TTDP2:
|
||||
fmt::format_to(output_iterator, "TTDP savegame, {} format",
|
||||
this->type == SGT_TTDP1 ? "old" : "new");
|
||||
if (this->version != 0) {
|
||||
fmt::format_to(output_iterator, ", TTDP version {}.{}.{}.{}",
|
||||
GB(this->version, 24, 8), GB(this->version, 20, 4),
|
||||
GB(this->version, 16, 4), GB(this->version, 0, 16));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* virtual */ void LoggedChangeSettingChanged::FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type)
|
||||
{
|
||||
/* A setting with the SF_NO_NETWORK flag got changed; these settings usually affect NewGRFs, such as road side or wagon speed limits. */
|
||||
fmt::format_to(output_iterator, "Setting changed: {} : {} -> {}", this->name, this->oldval, this->newval);
|
||||
}
|
||||
|
||||
/* virtual */ void LoggedChangeGRFAdd::FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type)
|
||||
{
|
||||
/* A NewGRF got added to the game, either at the start of the game (never an issue), or later on when it could be an issue. */
|
||||
const GRFConfig *gc = FindGRFConfig(this->grfid, FGCM_EXACT, this->md5sum);
|
||||
fmt::format_to(output_iterator, "Added NewGRF: ");
|
||||
AddGrfInfo(output_iterator, this->grfid, this->md5sum, gc);
|
||||
GrfIDMapping::Pair *gm = grf_names.Find(this->grfid);
|
||||
if (gm != grf_names.End() && !gm->second.was_missing) fmt::format_to(output_iterator, ". Gamelog inconsistency: GrfID was already added!");
|
||||
grf_names[this->grfid] = gc;
|
||||
}
|
||||
|
||||
/* virtual */ void LoggedChangeGRFRemoved::FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type)
|
||||
{
|
||||
/* A NewGRF got removed from the game, either manually or by it missing when loading the game. */
|
||||
GrfIDMapping::Pair *gm = grf_names.Find(this->grfid);
|
||||
fmt::format_to(output_iterator, action_type == GLAT_LOAD ? "Missing NewGRF: " : "Removed NewGRF: ");
|
||||
AddGrfInfo(output_iterator, this->grfid, nullptr, gm != grf_names.End() ? gm->second.gc : nullptr);
|
||||
if (gm == grf_names.End()) {
|
||||
fmt::format_to(output_iterator, ". Gamelog inconsistency: GrfID was never added!");
|
||||
} else {
|
||||
if (action_type == GLAT_LOAD) {
|
||||
/* Missing grfs on load are not removed from the configuration */
|
||||
gm->second.was_missing = true;
|
||||
} else {
|
||||
grf_names.Erase(gm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* virtual */ void LoggedChangeGRFChanged::FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type)
|
||||
{
|
||||
/* Another version of the same NewGRF got loaded. */
|
||||
const GRFConfig *gc = FindGRFConfig(this->grfid, FGCM_EXACT, this->md5sum);
|
||||
fmt::format_to(output_iterator, "Compatible NewGRF loaded: ");
|
||||
AddGrfInfo(output_iterator, this->grfid, this->md5sum, gc);
|
||||
if (!grf_names.Contains(this->grfid)) fmt::format_to(output_iterator, ". Gamelog inconsistency: GrfID was never added!");
|
||||
grf_names[this->grfid] = gc;
|
||||
}
|
||||
|
||||
/* virtual */ void LoggedChangeGRFParameterChanged::FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type)
|
||||
{
|
||||
/* A parameter of a NewGRF got changed after the game was started. */
|
||||
GrfIDMapping::Pair *gm = grf_names.Find(this->grfid);
|
||||
fmt::format_to(output_iterator, "GRF parameter changed: ");
|
||||
AddGrfInfo(output_iterator, this->grfid, nullptr, gm != grf_names.End() ? gm->second.gc : nullptr);
|
||||
if (gm == grf_names.End()) fmt::format_to(output_iterator, ". Gamelog inconsistency: GrfID was never added!");
|
||||
}
|
||||
|
||||
/* virtual */ void LoggedChangeGRFMoved::FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type)
|
||||
{
|
||||
/* The order of NewGRFs got changed, which might cause some other NewGRFs to behave differently. */
|
||||
GrfIDMapping::Pair *gm = grf_names.Find(this->grfid);
|
||||
fmt::format_to(output_iterator, "GRF order changed: {:08X} moved {} places {}",
|
||||
BSWAP32(this->grfid), abs(this->offset), this->offset >= 0 ? "down" : "up" );
|
||||
AddGrfInfo(output_iterator, this->grfid, nullptr, gm != grf_names.End() ? gm->second.gc : nullptr);
|
||||
if (gm == grf_names.End()) fmt::format_to(output_iterator, ". Gamelog inconsistency: GrfID was never added!");
|
||||
}
|
||||
|
||||
/* virtual */ void LoggedChangeGRFBug::FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type)
|
||||
{
|
||||
/* A specific bug in a NewGRF, that could cause wide spread problems, has been noted during the execution of the game. */
|
||||
GrfIDMapping::Pair *gm = grf_names.Find(this->grfid);
|
||||
assert(this->bug == GBUG_VEH_LENGTH);
|
||||
|
||||
fmt::format_to(output_iterator, "Rail vehicle changes length outside a depot: GRF ID {:08X}, internal ID 0x{:X}", BSWAP32(this->grfid), this->data);
|
||||
AddGrfInfo(output_iterator, this->grfid, nullptr, gm != grf_names.End() ? gm->second.gc : nullptr);
|
||||
if (gm == grf_names.End()) fmt::format_to(output_iterator, ". Gamelog inconsistency: GrfID was never added!");
|
||||
}
|
||||
|
||||
/* virtual */ void LoggedChangeEmergencySave::FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type)
|
||||
{
|
||||
/* At one point the savegame was made during the handling of a game crash.
|
||||
* The generic code already mentioned the emergency savegame, and there is no extra information to log. */
|
||||
}
|
||||
|
||||
/** Print the gamelog data to the console. */
|
||||
void Gamelog::PrintConsole()
|
||||
{
|
||||
|
@ -354,25 +327,20 @@ void Gamelog::PrintDebug(int level)
|
|||
|
||||
|
||||
/**
|
||||
* Allocates new LoggedChange and new LoggedAction if needed.
|
||||
* If there is no action active, nullptr is returned.
|
||||
* @param ct type of change
|
||||
* @return new LoggedChange, or nullptr if there is no action active
|
||||
* Allocates a new LoggedAction if needed, and add the change when action is active.
|
||||
* @param change The actual change.
|
||||
*/
|
||||
LoggedChange *Gamelog::Change(GamelogChangeType ct)
|
||||
void Gamelog::Change(std::unique_ptr<LoggedChange> &&change)
|
||||
{
|
||||
if (this->current_action == nullptr) {
|
||||
if (this->action_type == GLAT_NONE) return nullptr;
|
||||
if (this->action_type == GLAT_NONE) return;
|
||||
|
||||
this->current_action = &this->data->action.emplace_back();
|
||||
this->current_action->at = this->action_type;
|
||||
this->current_action->tick = TimerGameTick::counter;
|
||||
}
|
||||
|
||||
LoggedChange *lc = &this->current_action->change.emplace_back();
|
||||
lc->ct = ct;
|
||||
|
||||
return lc;
|
||||
this->current_action->change.push_back(std::move(change));
|
||||
}
|
||||
|
||||
|
||||
|
@ -384,7 +352,7 @@ void Gamelog::Emergency()
|
|||
/* Terminate any active action */
|
||||
if (this->action_type != GLAT_NONE) this->StopAction();
|
||||
this->StartAction(GLAT_EMERGENCY);
|
||||
this->Change(GLCT_EMERGENCY);
|
||||
this->Change(std::make_unique<LoggedChangeEmergencySave>());
|
||||
this->StopAction();
|
||||
}
|
||||
|
||||
|
@ -394,8 +362,8 @@ void Gamelog::Emergency()
|
|||
bool Gamelog::TestEmergency()
|
||||
{
|
||||
for (const LoggedAction &la : this->data->action) {
|
||||
for (const LoggedChange &lc : la.change) {
|
||||
if (lc.ct == GLCT_EMERGENCY) return true;
|
||||
for (const auto &lc : la.change) {
|
||||
if (lc->ct == GLCT_EMERGENCY) return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -409,14 +377,8 @@ void Gamelog::Revision()
|
|||
{
|
||||
assert(this->action_type == GLAT_START || this->action_type == GLAT_LOAD);
|
||||
|
||||
LoggedChange *lc = this->Change(GLCT_REVISION);
|
||||
if (lc == nullptr) return;
|
||||
|
||||
memset(lc->revision.text, 0, sizeof(lc->revision.text));
|
||||
strecpy(lc->revision.text, GetGamelogRevisionString(), lastof(lc->revision.text));
|
||||
lc->revision.slver = SAVEGAME_VERSION;
|
||||
lc->revision.modified = _openttd_revision_modified;
|
||||
lc->revision.newgrf = _openttd_newgrf_version;
|
||||
this->Change(std::make_unique<LoggedChangeRevision>(
|
||||
GetGamelogRevisionString(), SAVEGAME_VERSION, _openttd_revision_modified, _openttd_newgrf_version));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -426,11 +388,7 @@ void Gamelog::Mode()
|
|||
{
|
||||
assert(this->action_type == GLAT_START || this->action_type == GLAT_LOAD || this->action_type == GLAT_CHEAT);
|
||||
|
||||
LoggedChange *lc = this->Change(GLCT_MODE);
|
||||
if (lc == nullptr) return;
|
||||
|
||||
lc->mode.mode = _game_mode;
|
||||
lc->mode.landscape = _settings_game.game_creation.landscape;
|
||||
this->Change(std::make_unique<LoggedChangeMode>(_game_mode, _settings_game.game_creation.landscape));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -440,11 +398,8 @@ void Gamelog::Oldver()
|
|||
{
|
||||
assert(this->action_type == GLAT_LOAD);
|
||||
|
||||
LoggedChange *lc = this->Change(GLCT_OLDVER);
|
||||
if (lc == nullptr) return;
|
||||
|
||||
lc->oldver.type = _savegame_type;
|
||||
lc->oldver.version = (_savegame_type == SGT_OTTD ? ((uint32)_sl_version << 8 | _sl_minor_version) : _ttdp_version);
|
||||
this->Change(std::make_unique<LoggedChangeOldVersion>(_savegame_type,
|
||||
(_savegame_type == SGT_OTTD ? ((uint32_t)_sl_version << 8 | _sl_minor_version) : _ttdp_version)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -457,12 +412,7 @@ void Gamelog::Setting(const std::string &name, int32 oldval, int32 newval)
|
|||
{
|
||||
assert(this->action_type == GLAT_SETTING);
|
||||
|
||||
LoggedChange *lc = this->Change(GLCT_SETTING);
|
||||
if (lc == nullptr) return;
|
||||
|
||||
lc->setting.name = stredup(name.c_str());
|
||||
lc->setting.oldval = oldval;
|
||||
lc->setting.newval = newval;
|
||||
this->Change(std::make_unique<LoggedChangeSettingChanged>(name, oldval, newval));
|
||||
}
|
||||
|
||||
|
||||
|
@ -472,17 +422,17 @@ void Gamelog::Setting(const std::string &name, int32 oldval, int32 newval)
|
|||
*/
|
||||
void Gamelog::TestRevision()
|
||||
{
|
||||
const LoggedChange *rev = nullptr;
|
||||
const LoggedChangeRevision *rev = nullptr;
|
||||
|
||||
for (const LoggedAction &la : this->data->action) {
|
||||
for (const LoggedChange &lc : la.change) {
|
||||
if (lc.ct == GLCT_REVISION) rev = &lc;
|
||||
for (const auto &lc : la.change) {
|
||||
if (lc->ct == GLCT_REVISION) rev = static_cast<const LoggedChangeRevision *>(lc.get());
|
||||
}
|
||||
}
|
||||
|
||||
if (rev == nullptr || strcmp(rev->revision.text, GetGamelogRevisionString()) != 0 ||
|
||||
rev->revision.modified != _openttd_revision_modified ||
|
||||
rev->revision.newgrf != _openttd_newgrf_version) {
|
||||
if (rev == nullptr || rev->text != GetGamelogRevisionString() ||
|
||||
rev->modified != _openttd_revision_modified ||
|
||||
rev->newgrf != _openttd_newgrf_version) {
|
||||
this->Revision();
|
||||
}
|
||||
}
|
||||
|
@ -493,15 +443,15 @@ void Gamelog::TestRevision()
|
|||
*/
|
||||
void Gamelog::TestMode()
|
||||
{
|
||||
const LoggedChange *mode = nullptr;
|
||||
const LoggedChangeMode *mode = nullptr;
|
||||
|
||||
for (const LoggedAction &la : this->data->action) {
|
||||
for (const LoggedChange &lc : la.change) {
|
||||
if (lc.ct == GLCT_MODE) mode = &lc;
|
||||
for (const auto &lc : la.change) {
|
||||
if (lc->ct == GLCT_MODE) mode = static_cast<const LoggedChangeMode *>(lc.get());
|
||||
}
|
||||
}
|
||||
|
||||
if (mode == nullptr || mode->mode.mode != _game_mode || mode->mode.landscape != _settings_game.game_creation.landscape) this->Mode();
|
||||
if (mode == nullptr || mode->mode != _game_mode || mode->landscape != _settings_game.game_creation.landscape) this->Mode();
|
||||
}
|
||||
|
||||
|
||||
|
@ -515,12 +465,7 @@ void Gamelog::GRFBug(uint32 grfid, byte bug, uint64 data)
|
|||
{
|
||||
assert(this->action_type == GLAT_GRFBUG);
|
||||
|
||||
LoggedChange *lc = this->Change(GLCT_GRFBUG);
|
||||
if (lc == nullptr) return;
|
||||
|
||||
lc->grfbug.data = data;
|
||||
lc->grfbug.grfid = grfid;
|
||||
lc->grfbug.bug = bug;
|
||||
this->Change(std::make_unique<LoggedChangeGRFBug>(data, grfid, bug));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -535,10 +480,12 @@ void Gamelog::GRFBug(uint32 grfid, byte bug, uint64 data)
|
|||
bool Gamelog::GRFBugReverse(uint32 grfid, uint16 internal_id)
|
||||
{
|
||||
for (const LoggedAction &la : this->data->action) {
|
||||
for (const LoggedChange &lc : la.change) {
|
||||
if (lc.ct == GLCT_GRFBUG && lc.grfbug.grfid == grfid &&
|
||||
lc.grfbug.bug == GBUG_VEH_LENGTH && lc.grfbug.data == internal_id) {
|
||||
return false;
|
||||
for (const auto &lc : la.change) {
|
||||
if (lc->ct == GLCT_GRFBUG) {
|
||||
LoggedChangeGRFBug *bug = static_cast<LoggedChangeGRFBug *>(lc.get());
|
||||
if (bug->grfid == grfid && bug->bug == GBUG_VEH_LENGTH && bug->data == internal_id) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -569,10 +516,7 @@ void Gamelog::GRFRemove(uint32 grfid)
|
|||
{
|
||||
assert(this->action_type == GLAT_LOAD || this->action_type == GLAT_GRF);
|
||||
|
||||
LoggedChange *lc = this->Change(GLCT_GRFREM);
|
||||
if (lc == nullptr) return;
|
||||
|
||||
lc->grfrem.grfid = grfid;
|
||||
this->Change(std::make_unique<LoggedChangeGRFRemoved>(grfid));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -585,10 +529,7 @@ void Gamelog::GRFAdd(const GRFConfig *newg)
|
|||
|
||||
if (!IsLoggableGrfConfig(newg)) return;
|
||||
|
||||
LoggedChange *lc = this->Change(GLCT_GRFADD);
|
||||
if (lc == nullptr) return;
|
||||
|
||||
lc->grfadd = newg->ident;
|
||||
this->Change(std::make_unique<LoggedChangeGRFAdd>(newg->ident));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -600,10 +541,7 @@ void Gamelog::GRFCompatible(const GRFIdentifier *newg)
|
|||
{
|
||||
assert(this->action_type == GLAT_LOAD || this->action_type == GLAT_GRF);
|
||||
|
||||
LoggedChange *lc = this->Change(GLCT_GRFCOMPAT);
|
||||
if (lc == nullptr) return;
|
||||
|
||||
lc->grfcompat = *newg;
|
||||
this->Change(std::make_unique<LoggedChangeGRFChanged>(*newg));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -615,11 +553,7 @@ void Gamelog::GRFMove(uint32 grfid, int32 offset)
|
|||
{
|
||||
assert(this->action_type == GLAT_GRF);
|
||||
|
||||
LoggedChange *lc = this->Change(GLCT_GRFMOVE);
|
||||
if (lc == nullptr) return;
|
||||
|
||||
lc->grfmove.grfid = grfid;
|
||||
lc->grfmove.offset = offset;
|
||||
this->Change(std::make_unique<LoggedChangeGRFMoved>(grfid, offset));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -631,10 +565,7 @@ void Gamelog::GRFParameters(uint32 grfid)
|
|||
{
|
||||
assert(this->action_type == GLAT_GRF);
|
||||
|
||||
LoggedChange *lc = this->Change(GLCT_GRFPARAM);
|
||||
if (lc == nullptr) return;
|
||||
|
||||
lc->grfparam.grfid = grfid;
|
||||
this->Change(std::make_unique<LoggedChangeGRFParameterChanged>(grfid));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -752,14 +683,16 @@ void Gamelog::GRFUpdate(const GRFConfig *oldc, const GRFConfig *newc)
|
|||
void Gamelog::Info(uint32 *last_ottd_rev, byte *ever_modified, bool *removed_newgrfs)
|
||||
{
|
||||
for (const LoggedAction &la : this->data->action) {
|
||||
for (const LoggedChange &lc : la.change) {
|
||||
switch (lc.ct) {
|
||||
for (const auto &lc : la.change) {
|
||||
switch (lc->ct) {
|
||||
default: break;
|
||||
|
||||
case GLCT_REVISION:
|
||||
*last_ottd_rev = lc.revision.newgrf;
|
||||
*ever_modified = std::max(*ever_modified, lc.revision.modified);
|
||||
case GLCT_REVISION: {
|
||||
const LoggedChangeRevision *rev = static_cast<const LoggedChangeRevision *>(lc.get());
|
||||
*last_ottd_rev = rev->newgrf;
|
||||
*ever_modified = std::max(*ever_modified, rev->modified);
|
||||
break;
|
||||
}
|
||||
|
||||
case GLCT_GRFREM:
|
||||
*removed_newgrfs = true;
|
||||
|
@ -780,8 +713,11 @@ const GRFIdentifier *Gamelog::GetOverriddenIdentifier(const GRFConfig *c)
|
|||
const LoggedAction &la = this->data->action.back();
|
||||
if (la.at != GLAT_LOAD) return &c->ident;
|
||||
|
||||
for (const LoggedChange &lc : la.change) {
|
||||
if (lc.ct == GLCT_GRFCOMPAT && lc.grfcompat.grfid == c->ident.grfid) return &lc.grfcompat;
|
||||
for (const auto &lc : la.change) {
|
||||
if (lc->ct != GLCT_GRFCOMPAT) continue;
|
||||
|
||||
const LoggedChangeGRFChanged *grf = static_cast<const LoggedChangeGRFChanged *>(lc.get());
|
||||
if (grf->grfid == c->ident.grfid) return grf;
|
||||
}
|
||||
|
||||
return &c->ident;
|
||||
|
|
|
@ -52,7 +52,7 @@ private:
|
|||
GamelogActionType action_type;
|
||||
struct LoggedAction *current_action;
|
||||
|
||||
LoggedChange *Change(GamelogChangeType ct);
|
||||
void Change(std::unique_ptr<LoggedChange> &&change);
|
||||
|
||||
public:
|
||||
Gamelog();
|
||||
|
|
|
@ -11,60 +11,139 @@
|
|||
#define GAMELOG_INTERNAL_H
|
||||
|
||||
#include "gamelog.h"
|
||||
#include <iterator>
|
||||
|
||||
static const uint GAMELOG_REVISION_LENGTH = 15;
|
||||
/**
|
||||
* Information about the presence of a Grf at a certain point during gamelog history
|
||||
* Note about missing Grfs:
|
||||
* Changes to missing Grfs are not logged including manual removal of the Grf.
|
||||
* So if the gamelog tells a Grf is missing we do not know whether it was readded or completely removed
|
||||
* at some later point.
|
||||
*/
|
||||
struct GRFPresence{
|
||||
const GRFConfig *gc; ///< GRFConfig, if known
|
||||
bool was_missing; ///< Grf was missing during some gameload in the past
|
||||
|
||||
GRFPresence(const GRFConfig *gc) : gc(gc), was_missing(false) {}
|
||||
GRFPresence() = default;
|
||||
};
|
||||
typedef SmallMap<uint32_t, GRFPresence> GrfIDMapping;
|
||||
|
||||
/** 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[GAMELOG_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 setting
|
||||
int32 oldval; ///< old value
|
||||
int32 newval; ///< new value
|
||||
} setting;
|
||||
struct {
|
||||
uint64 data; ///< additional data
|
||||
uint32 grfid; ///< ID of problematic GRF
|
||||
byte bug; ///< type of bug, @see enum GRFBugs
|
||||
} grfbug;
|
||||
};
|
||||
LoggedChange(GamelogChangeType type = GLCT_NONE) : ct(type) {}
|
||||
virtual ~LoggedChange() {}
|
||||
virtual void FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type) = 0;
|
||||
|
||||
~LoggedChange();
|
||||
GamelogChangeType ct;
|
||||
};
|
||||
|
||||
struct LoggedChangeMode : LoggedChange {
|
||||
LoggedChangeMode() : LoggedChange(GLCT_MODE) {}
|
||||
LoggedChangeMode(byte mode, byte landscape) :
|
||||
LoggedChange(GLCT_MODE), mode(mode), landscape(landscape) {}
|
||||
void FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type) override;
|
||||
|
||||
byte mode; ///< new game mode - Editor x Game
|
||||
byte landscape; ///< landscape (temperate, arctic, ...)
|
||||
};
|
||||
|
||||
struct LoggedChangeRevision : LoggedChange {
|
||||
LoggedChangeRevision() : LoggedChange(GLCT_REVISION) {}
|
||||
LoggedChangeRevision(const std::string &text, uint32_t newgrf, uint16_t slver, byte modified) :
|
||||
LoggedChange(GLCT_REVISION), text(text), newgrf(newgrf), slver(slver), modified(modified) {}
|
||||
void FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type) override;
|
||||
|
||||
std::string text; ///< revision string, _openttd_revision
|
||||
uint32_t newgrf; ///< _openttd_newgrf_version
|
||||
uint16_t slver; ///< _sl_version
|
||||
byte modified; //< _openttd_revision_modified
|
||||
};
|
||||
|
||||
struct LoggedChangeOldVersion : LoggedChange {
|
||||
LoggedChangeOldVersion() : LoggedChange(GLCT_OLDVER) {}
|
||||
LoggedChangeOldVersion(uint32_t type, uint32_t version) :
|
||||
LoggedChange(GLCT_OLDVER), type(type), version(version) {}
|
||||
void FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type) override;
|
||||
|
||||
uint32_t type; ///< type of savegame, @see SavegameType
|
||||
uint32_t version; ///< major and minor version OR ttdp version
|
||||
};
|
||||
|
||||
struct LoggedChangeGRFAdd : LoggedChange, GRFIdentifier {
|
||||
LoggedChangeGRFAdd() : LoggedChange(GLCT_GRFADD) {}
|
||||
LoggedChangeGRFAdd(const GRFIdentifier &ident) :
|
||||
LoggedChange(GLCT_GRFADD), GRFIdentifier(ident) {}
|
||||
void FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type) override;
|
||||
};
|
||||
|
||||
struct LoggedChangeGRFRemoved : LoggedChange {
|
||||
LoggedChangeGRFRemoved() : LoggedChange(GLCT_GRFREM) {}
|
||||
LoggedChangeGRFRemoved(uint32_t grfid) :
|
||||
LoggedChange(GLCT_GRFREM), grfid(grfid) {}
|
||||
void FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type) override;
|
||||
|
||||
uint32_t grfid; ///< ID of removed GRF
|
||||
};
|
||||
|
||||
struct LoggedChangeGRFChanged : LoggedChange, GRFIdentifier {
|
||||
LoggedChangeGRFChanged() : LoggedChange(GLCT_GRFCOMPAT) {}
|
||||
LoggedChangeGRFChanged(const GRFIdentifier &ident) :
|
||||
LoggedChange(GLCT_GRFCOMPAT), GRFIdentifier(ident) {}
|
||||
void FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type) override;
|
||||
};
|
||||
|
||||
struct LoggedChangeGRFParameterChanged : LoggedChange {
|
||||
LoggedChangeGRFParameterChanged() : LoggedChange(GLCT_GRFPARAM) {}
|
||||
LoggedChangeGRFParameterChanged(uint32_t grfid) :
|
||||
LoggedChange(GLCT_GRFPARAM), grfid(grfid) {}
|
||||
void FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type) override;
|
||||
|
||||
uint32_t grfid; ///< ID of GRF with changed parameters
|
||||
};
|
||||
|
||||
struct LoggedChangeGRFMoved : LoggedChange {
|
||||
LoggedChangeGRFMoved() : LoggedChange(GLCT_GRFMOVE) {}
|
||||
LoggedChangeGRFMoved(uint32_t grfid, int32_t offset) :
|
||||
LoggedChange(GLCT_GRFMOVE), grfid(grfid), offset(offset) {}
|
||||
void FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type) override;
|
||||
|
||||
uint32_t grfid; ///< ID of moved GRF
|
||||
int32_t offset; ///< offset, positive = move down
|
||||
};
|
||||
|
||||
struct LoggedChangeSettingChanged : LoggedChange {
|
||||
LoggedChangeSettingChanged() : LoggedChange(GLCT_SETTING) {}
|
||||
LoggedChangeSettingChanged(const std::string &name, int32_t oldval, int32_t newval) :
|
||||
LoggedChange(GLCT_SETTING), name(name), oldval(oldval), newval(newval) {}
|
||||
void FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type) override;
|
||||
|
||||
std::string name; ///< name of the setting
|
||||
int32_t oldval; ///< old value
|
||||
int32_t newval; ///< new value
|
||||
};
|
||||
|
||||
struct LoggedChangeGRFBug : LoggedChange {
|
||||
LoggedChangeGRFBug() : LoggedChange(GLCT_GRFBUG) {}
|
||||
LoggedChangeGRFBug(uint64_t data, uint32_t grfid, byte bug) :
|
||||
LoggedChange(GLCT_GRFBUG), data(data), grfid(grfid), bug(bug) {}
|
||||
void FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type) override;
|
||||
|
||||
uint64_t data; ///< additional data
|
||||
uint32_t grfid; ///< ID of problematic GRF
|
||||
byte bug; ///< type of bug, @see enum GRFBugs
|
||||
};
|
||||
|
||||
struct LoggedChangeEmergencySave : LoggedChange {
|
||||
LoggedChangeEmergencySave() : LoggedChange(GLCT_EMERGENCY) {}
|
||||
void FormatTo(std::back_insert_iterator<std::string> &output_iterator, GrfIDMapping &grf_names, GamelogActionType action_type) override;
|
||||
};
|
||||
|
||||
|
||||
/** Contains information about one logged action that caused at least one logged change */
|
||||
struct LoggedAction {
|
||||
std::vector<LoggedChange> change; ///< First logged change in this action
|
||||
std::vector<std::unique_ptr<LoggedChange>> change; ///< Logged changes in this action
|
||||
GamelogActionType at; ///< Type of action
|
||||
uint64 tick; ///< Tick when it happened
|
||||
uint64_t tick; ///< Tick when it happened
|
||||
};
|
||||
|
||||
struct GamelogInternalData {
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
|
||||
#include "../gamelog_internal.h"
|
||||
#include "../fios.h"
|
||||
#include "../string_func.h"
|
||||
|
||||
#include "../safeguards.h"
|
||||
|
||||
|
@ -21,8 +22,8 @@
|
|||
class SlGamelogMode : public DefaultSaveLoadHandler<SlGamelogMode, LoggedChange> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_VAR(LoggedChange, mode.mode, SLE_UINT8),
|
||||
SLE_VAR(LoggedChange, mode.landscape, SLE_UINT8),
|
||||
SLE_VARNAME(LoggedChangeMode, mode, "mode.mode", SLE_UINT8),
|
||||
SLE_VARNAME(LoggedChangeMode, landscape, "mode.landscape", SLE_UINT8),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_mode_sl_compat;
|
||||
|
||||
|
@ -43,11 +44,15 @@ public:
|
|||
|
||||
class SlGamelogRevision : public DefaultSaveLoadHandler<SlGamelogRevision, LoggedChange> {
|
||||
public:
|
||||
static const size_t GAMELOG_REVISION_LENGTH = 15;
|
||||
static char revision_text[GAMELOG_REVISION_LENGTH];
|
||||
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_ARR(LoggedChange, revision.text, SLE_UINT8, GAMELOG_REVISION_LENGTH),
|
||||
SLE_VAR(LoggedChange, revision.newgrf, SLE_UINT32),
|
||||
SLE_VAR(LoggedChange, revision.slver, SLE_UINT16),
|
||||
SLE_VAR(LoggedChange, revision.modified, SLE_UINT8),
|
||||
SLEG_CONDARR("revision.text", SlGamelogRevision::revision_text, SLE_UINT8, GAMELOG_REVISION_LENGTH, SL_MIN_VERSION, SLV_STRING_GAMELOG),
|
||||
SLE_CONDSSTRNAME(LoggedChangeRevision, text, "revision.text", SLE_STR, SLV_STRING_GAMELOG, SL_MAX_VERSION),
|
||||
SLE_VARNAME(LoggedChangeRevision, newgrf, "revision.newgrf", SLE_UINT32),
|
||||
SLE_VARNAME(LoggedChangeRevision, slver, "revision.slver", SLE_UINT16),
|
||||
SLE_VARNAME(LoggedChangeRevision, modified, "revision.modified", SLE_UINT8),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_revision_sl_compat;
|
||||
|
||||
|
@ -61,16 +66,23 @@ public:
|
|||
{
|
||||
if (lc->ct != GLCT_REVISION) return;
|
||||
SlObject(lc, this->GetLoadDescription());
|
||||
|
||||
if (IsSavegameVersionBefore(SLV_STRING_GAMELOG)) {
|
||||
StrMakeValidInPlace(SlGamelogRevision::revision_text, lastof(SlGamelogRevision::revision_text));
|
||||
static_cast<LoggedChangeRevision *>(lc)->text = SlGamelogRevision::revision_text;
|
||||
}
|
||||
}
|
||||
|
||||
void LoadCheck(LoggedChange *lc) const override { this->Load(lc); }
|
||||
};
|
||||
|
||||
/* static */ char SlGamelogRevision::revision_text[GAMELOG_REVISION_LENGTH];
|
||||
|
||||
class SlGamelogOldver : public DefaultSaveLoadHandler<SlGamelogOldver, LoggedChange> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_VAR(LoggedChange, oldver.type, SLE_UINT32),
|
||||
SLE_VAR(LoggedChange, oldver.version, SLE_UINT32),
|
||||
SLE_VARNAME(LoggedChangeOldVersion, type, "oldver.type", SLE_UINT32),
|
||||
SLE_VARNAME(LoggedChangeOldVersion, version, "oldver.version", SLE_UINT32),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_oldver_sl_compat;
|
||||
|
||||
|
@ -92,9 +104,9 @@ public:
|
|||
class SlGamelogSetting : public DefaultSaveLoadHandler<SlGamelogSetting, LoggedChange> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_STR(LoggedChange, setting.name, SLE_STR, 128),
|
||||
SLE_VAR(LoggedChange, setting.oldval, SLE_INT32),
|
||||
SLE_VAR(LoggedChange, setting.newval, SLE_INT32),
|
||||
SLE_SSTRNAME(LoggedChangeSettingChanged, name, "setting.name", SLE_STR),
|
||||
SLE_VARNAME(LoggedChangeSettingChanged, oldval, "setting.oldval", SLE_INT32),
|
||||
SLE_VARNAME(LoggedChangeSettingChanged, newval, "setting.newval", SLE_INT32),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_setting_sl_compat;
|
||||
|
||||
|
@ -116,8 +128,8 @@ public:
|
|||
class SlGamelogGrfadd : public DefaultSaveLoadHandler<SlGamelogGrfadd, LoggedChange> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_VAR(LoggedChange, grfadd.grfid, SLE_UINT32 ),
|
||||
SLE_ARR(LoggedChange, grfadd.md5sum, SLE_UINT8, 16),
|
||||
SLE_VARNAME(LoggedChangeGRFAdd, grfid, "grfadd.grfid", SLE_UINT32 ),
|
||||
SLE_ARRNAME(LoggedChangeGRFAdd, md5sum, "grfadd.md5sum", SLE_UINT8, 16),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_grfadd_sl_compat;
|
||||
|
||||
|
@ -139,7 +151,7 @@ public:
|
|||
class SlGamelogGrfrem : public DefaultSaveLoadHandler<SlGamelogGrfrem, LoggedChange> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_VAR(LoggedChange, grfrem.grfid, SLE_UINT32),
|
||||
SLE_VARNAME(LoggedChangeGRFRemoved, grfid, "grfrem.grfid", SLE_UINT32),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_grfrem_sl_compat;
|
||||
|
||||
|
@ -161,8 +173,8 @@ public:
|
|||
class SlGamelogGrfcompat : public DefaultSaveLoadHandler<SlGamelogGrfcompat, LoggedChange> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_VAR(LoggedChange, grfcompat.grfid, SLE_UINT32 ),
|
||||
SLE_ARR(LoggedChange, grfcompat.md5sum, SLE_UINT8, 16),
|
||||
SLE_VARNAME(LoggedChangeGRFChanged, grfid, "grfcompat.grfid", SLE_UINT32 ),
|
||||
SLE_ARRNAME(LoggedChangeGRFChanged, md5sum, "grfcompat.md5sum", SLE_UINT8, 16),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_grfcompat_sl_compat;
|
||||
|
||||
|
@ -184,7 +196,7 @@ public:
|
|||
class SlGamelogGrfparam : public DefaultSaveLoadHandler<SlGamelogGrfparam, LoggedChange> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_VAR(LoggedChange, grfparam.grfid, SLE_UINT32),
|
||||
SLE_VARNAME(LoggedChangeGRFParameterChanged, grfid, "grfparam.grfid", SLE_UINT32),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_grfparam_sl_compat;
|
||||
|
||||
|
@ -206,8 +218,8 @@ public:
|
|||
class SlGamelogGrfmove : public DefaultSaveLoadHandler<SlGamelogGrfmove, LoggedChange> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_VAR(LoggedChange, grfmove.grfid, SLE_UINT32),
|
||||
SLE_VAR(LoggedChange, grfmove.offset, SLE_INT32),
|
||||
SLE_VARNAME(LoggedChangeGRFMoved, grfid, "grfmove.grfid", SLE_UINT32),
|
||||
SLE_VARNAME(LoggedChangeGRFMoved, offset, "grfmove.offset", SLE_INT32),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_grfmove_sl_compat;
|
||||
|
||||
|
@ -229,9 +241,9 @@ public:
|
|||
class SlGamelogGrfbug : public DefaultSaveLoadHandler<SlGamelogGrfbug, LoggedChange> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
SLE_VAR(LoggedChange, grfbug.data, SLE_UINT64),
|
||||
SLE_VAR(LoggedChange, grfbug.grfid, SLE_UINT32),
|
||||
SLE_VAR(LoggedChange, grfbug.bug, SLE_UINT8),
|
||||
SLE_VARNAME(LoggedChangeGRFBug, data, "grfbug.data", SLE_UINT64),
|
||||
SLE_VARNAME(LoggedChangeGRFBug, grfid, "grfbug.grfid", SLE_UINT32),
|
||||
SLE_VARNAME(LoggedChangeGRFBug, bug, "grfbug.bug", SLE_UINT8),
|
||||
};
|
||||
inline const static SaveLoadCompatTable compat_description = _gamelog_grfbug_sl_compat;
|
||||
|
||||
|
@ -278,6 +290,27 @@ public:
|
|||
void LoadCheck(LoggedChange *lc) const override { this->Load(lc); }
|
||||
};
|
||||
|
||||
static std::unique_ptr<LoggedChange> MakeLoggedChange(GamelogChangeType type)
|
||||
{
|
||||
switch (type) {
|
||||
case GLCT_MODE: return std::make_unique<LoggedChangeMode>();
|
||||
case GLCT_REVISION: return std::make_unique<LoggedChangeRevision>();
|
||||
case GLCT_OLDVER: return std::make_unique<LoggedChangeOldVersion>();
|
||||
case GLCT_SETTING: return std::make_unique<LoggedChangeSettingChanged>();
|
||||
case GLCT_GRFADD: return std::make_unique<LoggedChangeGRFAdd>();
|
||||
case GLCT_GRFREM: return std::make_unique<LoggedChangeGRFRemoved>();
|
||||
case GLCT_GRFCOMPAT: return std::make_unique<LoggedChangeGRFChanged>();
|
||||
case GLCT_GRFPARAM: return std::make_unique<LoggedChangeGRFParameterChanged>();
|
||||
case GLCT_GRFMOVE: return std::make_unique<LoggedChangeGRFMoved>();
|
||||
case GLCT_GRFBUG: return std::make_unique<LoggedChangeGRFBug>();
|
||||
case GLCT_EMERGENCY: return std::make_unique<LoggedChangeEmergencySave>();
|
||||
case GLCT_END:
|
||||
case GLCT_NONE:
|
||||
default:
|
||||
SlErrorCorrupt("Invalid gamelog action type");
|
||||
}
|
||||
}
|
||||
|
||||
class SlGamelogAction : public DefaultSaveLoadHandler<SlGamelogAction, LoggedAction> {
|
||||
public:
|
||||
inline static const SaveLoad description[] = {
|
||||
|
@ -301,22 +334,25 @@ public:
|
|||
SlSetStructListLength(la->change.size());
|
||||
|
||||
for (auto &lc : la->change) {
|
||||
assert((uint)lc.ct < GLCT_END);
|
||||
SlObject(&lc, this->GetDescription());
|
||||
assert(lc->ct < GLCT_END);
|
||||
SlObject(lc.get(), this->GetDescription());
|
||||
}
|
||||
}
|
||||
|
||||
void LoadChange(LoggedAction *la, GamelogChangeType type) const
|
||||
{
|
||||
std::unique_ptr<LoggedChange> lc = MakeLoggedChange(type);
|
||||
SlObject(lc.get(), this->GetLoadDescription());
|
||||
la->change.push_back(std::move(lc));
|
||||
}
|
||||
|
||||
void Load(LoggedAction *la) const override
|
||||
{
|
||||
if (IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY)) {
|
||||
byte type;
|
||||
while ((type = SlReadByte()) != GLCT_NONE) {
|
||||
if (type >= GLCT_END) SlErrorCorrupt("Invalid gamelog change type");
|
||||
GamelogChangeType ct = (GamelogChangeType)type;
|
||||
|
||||
LoggedChange &lc = la->change.emplace_back();
|
||||
lc.ct = ct;
|
||||
SlObject(&lc, this->GetLoadDescription());
|
||||
LoadChange(la, (GamelogChangeType)type);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
@ -325,9 +361,7 @@ public:
|
|||
la->change.reserve(length);
|
||||
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
LoggedChange &lc = la->change.emplace_back();
|
||||
lc.ct = (GamelogChangeType)SlReadByte();
|
||||
SlObject(&lc, this->GetLoadDescription());
|
||||
LoadChange(la, (GamelogChangeType)SlReadByte());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -354,6 +354,7 @@ enum SaveLoadVersion : uint16 {
|
|||
SLV_EXTEND_ENTITY_MAPPING, ///< 311 PR#10672 Extend entity mapping range.
|
||||
SLV_DISASTER_VEH_STATE, ///< 312 PR#10798 Explicit storage of disaster vehicle state.
|
||||
SLV_SAVEGAME_ID, ///< 313 PR#10719 Add an unique ID to every savegame (used to deduplicate surveys).
|
||||
SLV_STRING_GAMELOG, ///< 314 PR#10801 Use std::string in gamelog.
|
||||
|
||||
SL_MAX_VERSION, ///< Highest possible saveload version
|
||||
};
|
||||
|
@ -771,6 +772,18 @@ struct SaveLoadCompat {
|
|||
*/
|
||||
#define SLE_CONDARR(base, variable, type, length, from, to) SLE_GENERAL(SL_ARR, base, variable, type, length, from, to, 0)
|
||||
|
||||
/**
|
||||
* Storage of a fixed-size array of #SL_VAR elements in some savegame versions.
|
||||
* @param base Name of the class or struct containing the array.
|
||||
* @param variable Name of the variable in the class or struct referenced by \a base.
|
||||
* @param name Field name for table chunks.
|
||||
* @param type Storage of the data in memory and in the savegame.
|
||||
* @param length Number of elements in the array.
|
||||
* @param from First savegame version that has the array.
|
||||
* @param to Last savegame version that has the array.
|
||||
*/
|
||||
#define SLE_CONDARRNAME(base, variable, name, type, length, from, to) SLE_GENERAL_NAME(SL_ARR, name, base, variable, type, length, from, to, 0)
|
||||
|
||||
/**
|
||||
* Storage of a string in some savegame versions.
|
||||
* @param base Name of the class or struct containing the string.
|
||||
|
@ -792,6 +805,17 @@ struct SaveLoadCompat {
|
|||
*/
|
||||
#define SLE_CONDSSTR(base, variable, type, from, to) SLE_GENERAL(SL_STDSTR, base, variable, type, 0, from, to, 0)
|
||||
|
||||
/**
|
||||
* Storage of a \c std::string in some savegame versions.
|
||||
* @param base Name of the class or struct containing the string.
|
||||
* @param variable Name of the variable in the class or struct referenced by \a base.
|
||||
* @param name Field name for table chunks.
|
||||
* @param type Storage of the data in memory and in the savegame.
|
||||
* @param from First savegame version that has the string.
|
||||
* @param to Last savegame version that has the string.
|
||||
*/
|
||||
#define SLE_CONDSSTRNAME(base, variable, name, type, from, to) SLE_GENERAL_NAME(SL_STDSTR, name, base, variable, type, 0, from, to, 0)
|
||||
|
||||
/**
|
||||
* Storage of a list of #SL_REF elements in some savegame versions.
|
||||
* @param base Name of the class or struct containing the list.
|
||||
|
@ -820,6 +844,15 @@ struct SaveLoadCompat {
|
|||
*/
|
||||
#define SLE_VAR(base, variable, type) SLE_CONDVAR(base, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
|
||||
|
||||
/**
|
||||
* Storage of a variable in every version of a savegame.
|
||||
* @param base Name of the class or struct containing the variable.
|
||||
* @param variable Name of the variable in the class or struct referenced by \a base.
|
||||
* @param name Field name for table chunks.
|
||||
* @param type Storage of the data in memory and in the savegame.
|
||||
*/
|
||||
#define SLE_VARNAME(base, variable, name, type) SLE_CONDVARNAME(base, variable, name, type, SL_MIN_VERSION, SL_MAX_VERSION)
|
||||
|
||||
/**
|
||||
* Storage of a reference in every version of a savegame.
|
||||
* @param base Name of the class or struct containing the variable.
|
||||
|
@ -837,6 +870,16 @@ struct SaveLoadCompat {
|
|||
*/
|
||||
#define SLE_ARR(base, variable, type, length) SLE_CONDARR(base, variable, type, length, SL_MIN_VERSION, SL_MAX_VERSION)
|
||||
|
||||
/**
|
||||
* Storage of fixed-size array of #SL_VAR elements in every version of a savegame.
|
||||
* @param base Name of the class or struct containing the array.
|
||||
* @param variable Name of the variable in the class or struct referenced by \a base.
|
||||
* @param name Field name for table chunks.
|
||||
* @param type Storage of the data in memory and in the savegame.
|
||||
* @param length Number of elements in the array.
|
||||
*/
|
||||
#define SLE_ARRNAME(base, variable, name, type, length) SLE_CONDARRNAME(base, variable, name, type, length, SL_MIN_VERSION, SL_MAX_VERSION)
|
||||
|
||||
/**
|
||||
* Storage of a string in every savegame version.
|
||||
* @param base Name of the class or struct containing the string.
|
||||
|
@ -854,6 +897,15 @@ struct SaveLoadCompat {
|
|||
*/
|
||||
#define SLE_SSTR(base, variable, type) SLE_CONDSSTR(base, variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
|
||||
|
||||
/**
|
||||
* Storage of a \c std::string in every savegame version.
|
||||
* @param base Name of the class or struct containing the string.
|
||||
* @param variable Name of the variable in the class or struct referenced by \a base.
|
||||
* @param name Field name for table chunks.
|
||||
* @param type Storage of the data in memory and in the savegame.
|
||||
*/
|
||||
#define SLE_SSTRNAME(base, variable, name, type) SLE_CONDSSTRNAME(base, variable, name, type, SL_MIN_VERSION, SL_MAX_VERSION)
|
||||
|
||||
/**
|
||||
* Storage of a list of #SL_REF elements in every savegame version.
|
||||
* @param base Name of the class or struct containing the list.
|
||||
|
|
Loading…
Reference in New Issue