gamelog_sl.cpp

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

Generated on Sat Nov 20 20:59:08 2010 for OpenTTD by  doxygen 1.6.1