1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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; either version 2
* of the License, or (at your option) any later version.
* This program 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. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/*
* This file is based on WME Lite.
* http://dead-code.org/redir.php?target=wmelite
* Copyright (c) 2011 Jan Nedoma
*/
#include "engines/wintermute/Base/BGame.h"
#include "engines/wintermute/PlatformSDL.h"
#include "engines/wintermute/wintermute.h"
#include "SysInstance.h"
#include "SysClassRegistry.h"
#include "common/stream.h"
namespace WinterMute {
//////////////////////////////////////////////////////////////////////////
CSysClassRegistry::CSysClassRegistry() {
}
//////////////////////////////////////////////////////////////////////////
CSysClassRegistry::~CSysClassRegistry() {
unregisterClasses();
}
//////////////////////////////////////////////////////////////////////////
CSysClassRegistry *CSysClassRegistry::GetInstance() {
return g_wintermute->getClassRegistry();
}
void CSysClassRegistry::unregisterClasses() {
// CSysClass calls UnregisterClass upon destruction.
while (_classes.size() > 0) {
delete _classes.begin()->_value;
}
}
//////////////////////////////////////////////////////////////////////////
bool CSysClassRegistry::RegisterClass(CSysClass *classObj) {
classObj->SetID(_count++);
//_classes.insert(classObj);
_classes[classObj] = classObj;
_nameMap[classObj->GetName()] = classObj;
_idMap[classObj->GetID()] = classObj;
return true;
}
//////////////////////////////////////////////////////////////////////////
bool CSysClassRegistry::UnregisterClass(CSysClass *classObj) {
Classes::iterator it = _classes.find(classObj);
if (it == _classes.end()) return false;
if (classObj->GetNumInstances() != 0) {
char str[MAX_PATH];
sprintf(str, "Memory leak@class %-20s: %d instance(s) left\n", classObj->GetName().c_str(), classObj->GetNumInstances());
CBPlatform::OutputDebugString(str);
}
_classes.erase(it);
NameMap::iterator mapIt = _nameMap.find(classObj->GetName());
if (mapIt != _nameMap.end()) _nameMap.erase(mapIt);
IdMap::iterator idIt = _idMap.find(classObj->GetID());
if (idIt != _idMap.end()) _idMap.erase(idIt);
return true;
}
//////////////////////////////////////////////////////////////////////////
bool CSysClassRegistry::RegisterInstance(const char *className, void *instance) {
if (_disabled) return true;
NameMap::iterator mapIt = _nameMap.find(className);
if (mapIt == _nameMap.end()) return false;
CSysInstance *inst = (*mapIt)._value->AddInstance(instance, _count++);
return (inst != NULL);
}
//////////////////////////////////////////////////////////////////////////
void CSysClassRegistry::AddInstanceToTable(CSysInstance *instance, void *pointer) {
_instanceMap[pointer] = instance;
if (instance->GetSavedID() >= 0)
_savedInstanceMap[instance->GetSavedID()] = instance;
}
//////////////////////////////////////////////////////////////////////////
int CSysClassRegistry::GetNextID() {
return _count++;
}
//////////////////////////////////////////////////////////////////////////
bool CSysClassRegistry::UnregisterInstance(const char *className, void *instance) {
NameMap::iterator mapIt = _nameMap.find(className);
if (mapIt == _nameMap.end()) return false;
(*mapIt)._value->RemoveInstance(instance);
InstanceMap::iterator instIt = _instanceMap.find(instance);
if (instIt != _instanceMap.end()) {
_instanceMap.erase(instIt);
return true;
} else return false;
}
//////////////////////////////////////////////////////////////////////////
bool CSysClassRegistry::GetPointerID(void *pointer, int *classID, int *instanceID) {
if (pointer == NULL) return true;
InstanceMap::iterator it = _instanceMap.find(pointer);
if (it == _instanceMap.end()) return false;
CSysInstance *inst = (*it)._value;
*instanceID = inst->GetID();
*classID = inst->GetClass()->GetID();
return true;
}
//////////////////////////////////////////////////////////////////////////
void *CSysClassRegistry::IDToPointer(int classID, int instanceID) {
SavedInstanceMap::iterator it = _savedInstanceMap.find(instanceID);
if (it == _savedInstanceMap.end()) return NULL;
else return (*it)._value->GetInstance();
}
bool checkHeader(const char *tag, CBPersistMgr *pm) {
char *test = pm->getString();
Common::String verify = test;
delete[] test;
bool retVal = (verify == tag);
if (!retVal) {
error("Expected %s in Save-file not found", tag);
}
return retVal;
}
//////////////////////////////////////////////////////////////////////////
HRESULT CSysClassRegistry::SaveTable(CBGame *Game, CBPersistMgr *persistMgr, bool quickSave) {
persistMgr->putString("<CLASS_REGISTRY_TABLE>");
persistMgr->putDWORD(_classes.size());
int counter = 0;
Classes::iterator it;
for (it = _classes.begin(); it != _classes.end(); ++it) {
counter++;
if (!quickSave) {
Game->_indicatorProgress = (int)(50.0f / (float)((float)_classes.size() / (float)counter));
Game->DisplayContent(false);
Game->_renderer->Flip();
}
(it->_value)->SaveTable(Game, persistMgr);
}
persistMgr->putString("</CLASS_REGISTRY_TABLE>");
return S_OK;
}
//////////////////////////////////////////////////////////////////////////
HRESULT CSysClassRegistry::LoadTable(CBGame *Game, CBPersistMgr *persistMgr) {
checkHeader("<CLASS_REGISTRY_TABLE>", persistMgr);
// reset SavedID of current instances
Classes::iterator it;
for (it = _classes.begin(); it != _classes.end(); ++it) {
(it->_value)->ResetSavedIDs();
}
for (it = _classes.begin(); it != _classes.end(); ++it) {
if ((it->_value)->IsPersistent()) continue;
(it->_value)->RemoveAllInstances();
}
_instanceMap.clear();
uint32 numClasses = persistMgr->getDWORD();
for (uint32 i = 0; i < numClasses; i++) {
Game->_indicatorProgress = (int)(50.0f / (float)((float)numClasses / (float)i));
Game->DisplayContentSimple();
Game->_renderer->Flip();
char *className = persistMgr->getString();
NameMap::iterator mapIt = _nameMap.find(className);
if (mapIt != _nameMap.end())(*mapIt)._value->LoadTable(Game, persistMgr);
}
checkHeader("</CLASS_REGISTRY_TABLE>", persistMgr);
return S_OK;
}
//////////////////////////////////////////////////////////////////////////
HRESULT CSysClassRegistry::SaveInstances(CBGame *Game, CBPersistMgr *persistMgr, bool quickSave) {
Classes::iterator it;
// count total instances
int numInstances = 0;
for (it = _classes.begin(); it != _classes.end(); ++it) {
numInstances += (it->_value)->GetNumInstances();
}
persistMgr->putDWORD(numInstances);
int counter = 0;
for (it = _classes.begin(); it != _classes.end(); ++it) {
counter++;
if (!quickSave) {
if (counter % 20 == 0) {
Game->_indicatorProgress = 50 + 50.0f / (float)((float)_classes.size() / (float)counter);
Game->DisplayContent(false);
Game->_renderer->Flip();
}
}
Game->MiniUpdate();
(it->_value)->SaveInstances(Game, persistMgr);
}
return S_OK;
}
//////////////////////////////////////////////////////////////////////////
HRESULT CSysClassRegistry::LoadInstances(CBGame *Game, CBPersistMgr *persistMgr) {
// get total instances
int numInstances = persistMgr->getDWORD();
for (int i = 0; i < numInstances; i++) {
if (i % 20 == 0) {
Game->_indicatorProgress = 50 + 50.0f / (float)((float)numInstances / (float)i);
Game->DisplayContentSimple();
Game->_renderer->Flip();
}
checkHeader("<INSTANCE_HEAD>", persistMgr);
int classID = persistMgr->getDWORD();
int instanceID = persistMgr->getDWORD();
void *instance = IDToPointer(classID, instanceID);
checkHeader("</INSTANCE_HEAD>", persistMgr);
Classes::iterator it;
for (it = _classes.begin(); it != _classes.end(); ++it) {
if ((it->_value)->GetSavedID() == classID) {
(it->_value)->LoadInstance(instance, persistMgr);
break;
}
}
checkHeader("</INSTANCE>", persistMgr);
}
_savedInstanceMap.clear();
return S_OK;
}
//////////////////////////////////////////////////////////////////////////
HRESULT CSysClassRegistry::EnumInstances(SYS_INSTANCE_CALLBACK lpCallback, const char *className, void *lpData) {
NameMap::iterator mapIt = _nameMap.find(className);
if (mapIt == _nameMap.end()) return E_FAIL;
(*mapIt)._value->InstanceCallback(lpCallback, lpData);
return S_OK;
}
//////////////////////////////////////////////////////////////////////////
void CSysClassRegistry::DumpClasses(Common::WriteStream *stream) {
Classes::iterator it;
for (it = _classes.begin(); it != _classes.end(); ++it)
(it->_value)->Dump(stream);
}
} // end of namespace WinterMute
|