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