# gamelog_sl.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "../stdafx.h"
    00013 #include "../gamelog_internal.h"
    00014
    00015 #include "saveload.h"
    00016
    00017 static const SaveLoad _glog_action_desc[] = {
    00018   SLE_VAR(LoggedAction, tick,              SLE_UINT16),
    00019   SLE_END()
    00020 };
    00021
    00022 static const SaveLoad _glog_mode_desc[] = {
    00023   SLE_VAR(LoggedChange, mode.mode,         SLE_UINT8),
    00024   SLE_VAR(LoggedChange, mode.landscape,    SLE_UINT8),
    00025   SLE_END()
    00026 };
    00027
    00028 static const SaveLoad _glog_revision_desc[] = {
    00029   SLE_ARR(LoggedChange, revision.text,     SLE_UINT8,  NETWORK_REVISION_LENGTH),
    00030   SLE_VAR(LoggedChange, revision.newgrf,   SLE_UINT32),
    00031   SLE_VAR(LoggedChange, revision.slver,    SLE_UINT16),
    00032   SLE_VAR(LoggedChange, revision.modified, SLE_UINT8),
    00033   SLE_END()
    00034 };
    00035
    00036 static const SaveLoad _glog_oldver_desc[] = {
    00037   SLE_VAR(LoggedChange, oldver.type,       SLE_UINT32),
    00038   SLE_VAR(LoggedChange, oldver.version,    SLE_UINT32),
    00039   SLE_END()
    00040 };
    00041
    00042 static const SaveLoad _glog_setting_desc[] = {
    00043   SLE_STR(LoggedChange, setting.name,      SLE_STR,    128),
    00044   SLE_VAR(LoggedChange, setting.oldval,    SLE_INT32),
    00045   SLE_VAR(LoggedChange, setting.newval,    SLE_INT32),
    00046   SLE_END()
    00047 };
    00048
    00049 static const SaveLoad _glog_grfadd_desc[] = {
    00050   SLE_VAR(LoggedChange, grfadd.grfid,      SLE_UINT32    ),
    00051   SLE_ARR(LoggedChange, grfadd.md5sum,     SLE_UINT8,  16),
    00052   SLE_END()
    00053 };
    00054
    00055 static const SaveLoad _glog_grfrem_desc[] = {
    00056   SLE_VAR(LoggedChange, grfrem.grfid,      SLE_UINT32),
    00057   SLE_END()
    00058 };
    00059
    00060 static const SaveLoad _glog_grfcompat_desc[] = {
    00061   SLE_VAR(LoggedChange, grfcompat.grfid,   SLE_UINT32    ),
    00062   SLE_ARR(LoggedChange, grfcompat.md5sum,  SLE_UINT8,  16),
    00063   SLE_END()
    00064 };
    00065
    00066 static const SaveLoad _glog_grfparam_desc[] = {
    00067   SLE_VAR(LoggedChange, grfparam.grfid,    SLE_UINT32),
    00068   SLE_END()
    00069 };
    00070
    00071 static const SaveLoad _glog_grfmove_desc[] = {
    00072   SLE_VAR(LoggedChange, grfmove.grfid,     SLE_UINT32),
    00073   SLE_VAR(LoggedChange, grfmove.offset,    SLE_INT32),
    00074   SLE_END()
    00075 };
    00076
    00077 static const SaveLoad _glog_grfbug_desc[] = {
    00078   SLE_VAR(LoggedChange, grfbug.data,       SLE_UINT64),
    00079   SLE_VAR(LoggedChange, grfbug.grfid,      SLE_UINT32),
    00080   SLE_VAR(LoggedChange, grfbug.bug,        SLE_UINT8),
    00081   SLE_END()
    00082 };
    00083
    00084 static const SaveLoad _glog_emergency_desc[] = {
    00085   SLE_END()
    00086 };
    00087
    00088 static const SaveLoad * const _glog_desc[] = {
    00089   _glog_mode_desc,
    00090   _glog_revision_desc,
    00091   _glog_oldver_desc,
    00092   _glog_setting_desc,
    00093   _glog_grfadd_desc,
    00094   _glog_grfrem_desc,
    00095   _glog_grfcompat_desc,
    00096   _glog_grfparam_desc,
    00097   _glog_grfmove_desc,
    00098   _glog_grfbug_desc,
    00099   _glog_emergency_desc,
    00100 };
    00101
    00102 assert_compile(lengthof(_glog_desc) == GLCT_END);
    00103
    00104 static void Load_GLOG()
    00105 {
    00106   assert(_gamelog_action == NULL);
    00107   assert(_gamelog_actions == 0);
    00108
    00109   GamelogActionType at;
    00110   while ((at = (GamelogActionType)SlReadByte()) != GLAT_NONE) {
    00111     _gamelog_action = ReallocT(_gamelog_action, _gamelog_actions + 1);
    00112     LoggedAction *la = &_gamelog_action[_gamelog_actions++];
    00113
    00114     la->at = at;
    00115
    00116     SlObject(la, _glog_action_desc); // has to be saved after 'DATE'!
    00117     la->change = NULL;
    00118     la->changes = 0;
    00119
    00120     GamelogChangeType ct;
    00121     while ((ct = (GamelogChangeType)SlReadByte()) != GLCT_NONE) {
    00122       la->change = ReallocT(la->change, la->changes + 1);
    00123
    00124       LoggedChange *lc = &la->change[la->changes++];
    00125       /* for SLE_STR, pointer has to be valid! so make it NULL */
    00126       memset(lc, 0, sizeof(*lc));
    00127       lc->ct = ct;
    00128
    00129       assert((uint)ct < GLCT_END);
    00130
    00131       SlObject(lc, _glog_desc[ct]);
    00132     }
    00133   }
    00134 }
    00135
    00136 static void Save_GLOG()
    00137 {
    00138   const LoggedAction *laend = &_gamelog_action[_gamelog_actions];
    00139   size_t length = 0;
    00140
    00141   for (const LoggedAction *la = _gamelog_action; la != laend; la++) {
    00142     const LoggedChange *lcend = &la->change[la->changes];
    00143     for (LoggedChange *lc = la->change; lc != lcend; lc++) {
    00144       assert((uint)lc->ct < lengthof(_glog_desc));
    00145       length += SlCalcObjLength(lc, _glog_desc[lc->ct]) + 1;
    00146     }
    00147     length += 4;
    00148   }
    00149   length++;
    00150
    00151   SlSetLength(length);
    00152
    00153   for (LoggedAction *la = _gamelog_action; la != laend; la++) {
    00154     SlWriteByte(la->at);
    00155     SlObject(la, _glog_action_desc);
    00156
    00157     const LoggedChange *lcend = &la->change[la->changes];
    00158     for (LoggedChange *lc = la->change; lc != lcend; lc++) {
    00159       SlWriteByte(lc->ct);
    00160       assert((uint)lc->ct < GLCT_END);
    00161       SlObject(lc, _glog_desc[lc->ct]);
    00162     }
    00163     SlWriteByte(GLCT_NONE);
    00164   }
    00165   SlWriteByte(GLAT_NONE);
    00166 }
    00167
    00168
    00169 extern const ChunkHandler _gamelog_chunk_handlers[] = {
    00170   { 'GLOG', Save_GLOG, Load_GLOG, NULL, NULL, CH_RIFF | CH_LAST }
    00171 };