# gamelog_sl.cpp

Go to the documentation of this file.

    00001 /* $Id: gamelog_sl.cpp 15904 2009-03-30 23:22:13Z rubidium $ */
    00002
    00005 #include "../stdafx.h"
    00006 #include "../gamelog.h"
    00007 #include "../gamelog_internal.h"
    00008 #include "../core/alloc_func.hpp"
    00009
    00010 #include "saveload.h"
    00011
    00012 static const SaveLoad _glog_action_desc[] = {
    00013   SLE_VAR(LoggedAction, tick,              SLE_UINT16),
    00014   SLE_END()
    00015 };
    00016
    00017 static const SaveLoad _glog_mode_desc[] = {
    00018   SLE_VAR(LoggedChange, mode.mode,         SLE_UINT8),
    00019   SLE_VAR(LoggedChange, mode.landscape,    SLE_UINT8),
    00020   SLE_END()
    00021 };
    00022
    00023 static const SaveLoad _glog_revision_desc[] = {
    00024   SLE_ARR(LoggedChange, revision.text,     SLE_UINT8,  NETWORK_REVISION_LENGTH),
    00025   SLE_VAR(LoggedChange, revision.newgrf,   SLE_UINT32),
    00026   SLE_VAR(LoggedChange, revision.slver,    SLE_UINT16),
    00027   SLE_VAR(LoggedChange, revision.modified, SLE_UINT8),
    00028   SLE_END()
    00029 };
    00030
    00031 static const SaveLoad _glog_oldver_desc[] = {
    00032   SLE_VAR(LoggedChange, oldver.type,       SLE_UINT32),
    00033   SLE_VAR(LoggedChange, oldver.version,    SLE_UINT32),
    00034   SLE_END()
    00035 };
    00036
    00037 static const SaveLoad _glog_setting_desc[] = {
    00038   SLE_STR(LoggedChange, setting.name,      SLE_STR,    128),
    00039   SLE_VAR(LoggedChange, setting.oldval,    SLE_INT32),
    00040   SLE_VAR(LoggedChange, setting.newval,    SLE_INT32),
    00041   SLE_END()
    00042 };
    00043
    00044 static const SaveLoad _glog_grfadd_desc[] = {
    00045   SLE_VAR(LoggedChange, grfadd.grfid,      SLE_UINT32    ),
    00046   SLE_ARR(LoggedChange, grfadd.md5sum,     SLE_UINT8,  16),
    00047   SLE_END()
    00048 };
    00049
    00050 static const SaveLoad _glog_grfrem_desc[] = {
    00051   SLE_VAR(LoggedChange, grfrem.grfid,      SLE_UINT32),
    00052   SLE_END()
    00053 };
    00054
    00055 static const SaveLoad _glog_grfcompat_desc[] = {
    00056   SLE_VAR(LoggedChange, grfcompat.grfid,   SLE_UINT32    ),
    00057   SLE_ARR(LoggedChange, grfcompat.md5sum,  SLE_UINT8,  16),
    00058   SLE_END()
    00059 };
    00060
    00061 static const SaveLoad _glog_grfparam_desc[] = {
    00062   SLE_VAR(LoggedChange, grfparam.grfid,    SLE_UINT32),
    00063   SLE_END()
    00064 };
    00065
    00066 static const SaveLoad _glog_grfmove_desc[] = {
    00067   SLE_VAR(LoggedChange, grfmove.grfid,     SLE_UINT32),
    00068   SLE_VAR(LoggedChange, grfmove.offset,    SLE_INT32),
    00069   SLE_END()
    00070 };
    00071
    00072 static const SaveLoad _glog_grfbug_desc[] = {
    00073   SLE_VAR(LoggedChange, grfbug.data,       SLE_UINT64),
    00074   SLE_VAR(LoggedChange, grfbug.grfid,      SLE_UINT32),
    00075   SLE_VAR(LoggedChange, grfbug.bug,        SLE_UINT8),
    00076   SLE_END()
    00077 };
    00078
    00079 static const SaveLoad _glog_emergency_desc[] = {
    00080   SLE_END()
    00081 };
    00082
    00083 static const SaveLoad *_glog_desc[] = {
    00084   _glog_mode_desc,
    00085   _glog_revision_desc,
    00086   _glog_oldver_desc,
    00087   _glog_setting_desc,
    00088   _glog_grfadd_desc,
    00089   _glog_grfrem_desc,
    00090   _glog_grfcompat_desc,
    00091   _glog_grfparam_desc,
    00092   _glog_grfmove_desc,
    00093   _glog_grfbug_desc,
    00094   _glog_emergency_desc,
    00095 };
    00096
    00097 assert_compile(lengthof(_glog_desc) == GLCT_END);
    00098
    00099 static void Load_GLOG()
    00100 {
    00101   assert(_gamelog_action == NULL);
    00102   assert(_gamelog_actions == 0);
    00103
    00104   GamelogActionType at;
    00105   while ((at = (GamelogActionType)SlReadByte()) != GLAT_NONE) {
    00106     _gamelog_action = ReallocT(_gamelog_action, _gamelog_actions + 1);
    00107     LoggedAction *la = &_gamelog_action[_gamelog_actions++];
    00108
    00109     la->at = at;
    00110
    00111     SlObject(la, _glog_action_desc); // has to be saved after 'DATE'!
    00112     la->change = NULL;
    00113     la->changes = 0;
    00114
    00115     GamelogChangeType ct;
    00116     while ((ct = (GamelogChangeType)SlReadByte()) != GLCT_NONE) {
    00117       la->change = ReallocT(la->change, la->changes + 1);
    00118
    00119       LoggedChange *lc = &la->change[la->changes++];
    00120       /* for SLE_STR, pointer has to be valid! so make it NULL */
    00121       memset(lc, 0, sizeof(*lc));
    00122       lc->ct = ct;
    00123
    00124       assert((uint)ct < GLCT_END);
    00125
    00126       SlObject(lc, _glog_desc[ct]);
    00127     }
    00128   }
    00129 }
    00130
    00131 static void Save_GLOG()
    00132 {
    00133   const LoggedAction *laend = &_gamelog_action[_gamelog_actions];
    00134   size_t length = 0;
    00135
    00136   for (const LoggedAction *la = _gamelog_action; la != laend; la++) {
    00137     const LoggedChange *lcend = &la->change[la->changes];
    00138     for (LoggedChange *lc = la->change; lc != lcend; lc++) {
    00139       assert((uint)lc->ct < lengthof(_glog_desc));
    00140       length += SlCalcObjLength(lc, _glog_desc[lc->ct]) + 1;
    00141     }
    00142     length += 4;
    00143   }
    00144   length++;
    00145
    00146   SlSetLength(length);
    00147
    00148   for (LoggedAction *la = _gamelog_action; la != laend; la++) {
    00149     SlWriteByte(la->at);
    00150     SlObject(la, _glog_action_desc);
    00151
    00152     const LoggedChange *lcend = &la->change[la->changes];
    00153     for (LoggedChange *lc = la->change; lc != lcend; lc++) {
    00154       SlWriteByte(lc->ct);
    00155       assert((uint)lc->ct < GLCT_END);
    00156       SlObject(lc, _glog_desc[lc->ct]);
    00157     }
    00158     SlWriteByte(GLCT_NONE);
    00159   }
    00160   SlWriteByte(GLAT_NONE);
    00161 }
    00162
    00163
    00164 extern const ChunkHandler _gamelog_chunk_handlers[] = {
    00165   { 'GLOG', Save_GLOG, Load_GLOG, CH_RIFF | CH_LAST }
    00166 };