ScummVM API documentation
seg_manager.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef SCI_ENGINE_SEG_MANAGER_H
23 #define SCI_ENGINE_SEG_MANAGER_H
24 
25 #include "common/scummsys.h"
26 #include "common/serializer.h"
27 #include "sci/engine/script.h"
28 #include "sci/engine/vm.h"
29 #include "sci/engine/vm_types.h"
30 #include "sci/engine/segment.h"
31 #ifdef ENABLE_SCI32
32 #include "sci/graphics/celobj32.h" // kLowResX, kLowResY
33 #endif
34 
35 namespace Sci {
36 
44 };
45 
46 class Script;
47 
49  friend class Console;
50 public:
54  SegManager(ResourceManager *resMan, ScriptPatcher *scriptPatcher);
55 
59  ~SegManager() override;
60 
61  void resetSegMan();
62 
63  void saveLoadWithSerializer(Common::Serializer &ser) override;
64 
65  // 1. Scripts
66 
73  Script *allocateScript(int script_nr, SegmentId &seg_id);
74 
75  // The script must then be initialized; see section (1b.), below.
76 
81  void deallocateScript(int script_nr);
82 
87 
93  SegmentId getScriptSegment(int script_nr) const;
94 
103  SegmentId getScriptSegment(int script_nr, ScriptLoadType load, bool applyScriptPatches = true);
104 
115  int instantiateScript(int script_nr, bool applyScriptPatches = true);
116 
124  void uninstantiateScript(int script_nr);
125 
126 private:
127  void uninstantiateScriptSci0(int script_nr);
128 
129 public:
130  // TODO: document this
131  reg_t getClassAddress(int classnr, ScriptLoadType lock, uint16 callerSegment, bool applyScriptPatches = true);
132 
140  Script *getScript(SegmentId seg);
141 
142 
150  Script *getScriptIfLoaded(SegmentId seg) const;
151 
152  // 2. Clones
153 
159  Clone *allocateClone(reg_t *addr);
160 
164  void reconstructClones();
165 
166  // 4. Stack
167 
173  DataStack *allocateStack(int size);
174 
175 
176  // 5. System Strings
177 
181  void initSysStrings();
182 
183 
184  // 6, 7. Lists and Nodes
185 
191  List *allocateList(reg_t *addr);
192 
198  Node *allocateNode(reg_t *addr);
199 
206  reg_t newNode(reg_t value, reg_t key);
207 
213  List *lookupList(reg_t addr);
214 
220  Node *lookupNode(reg_t addr, bool stopOnDiscarded = true);
221 
222 
223  // 8. Hunk Memory
224 
232  reg_t allocateHunkEntry(const char *hunk_type, int size);
233 
238  void freeHunkEntry(reg_t addr);
239 
244  byte *getHunkPointer(reg_t addr);
245 
246  // 9. Dynamic Memory
247 
256  byte *allocDynmem(int size, const char *description, reg_t *addr);
257 
262  bool freeDynmem(reg_t addr);
263 
264 
265  // Generic Operations on Segments and Addresses
266 
272  SegmentRef dereference(reg_t pointer);
273 
281  byte *derefBulkPtr(reg_t pointer, int entries);
282 
291  reg_t *derefRegPtr(reg_t pointer, int entries);
292 
300  char *derefString(reg_t pointer, int entries = 0);
301 
309  Common::String getString(reg_t pointer);
310 
311 
317  void strcpy_(reg_t dest, reg_t src);
318 
324  void strcpy_(reg_t dest, const char *src);
325 
332  void strncpy(reg_t dest, reg_t src, size_t n);
333 
340  void strncpy(reg_t dest, const char *src, size_t n);
341 
347  void memcpy(reg_t dest, reg_t src, size_t n);
348 
354  void memcpy(reg_t dest, const byte* src, size_t n);
355 
361  void memcpy(byte *dest, reg_t src, size_t n);
362 
367  size_t strlen(reg_t str);
368 
374  SegmentId findSegmentByType(int type) const;
375 
376  // TODO: document this
377  SegmentObj *getSegmentObj(SegmentId seg) const;
378 
379  // TODO: document this
380  SegmentType getSegmentType(SegmentId seg) const;
381 
382  // TODO: document this
383  SegmentObj *getSegment(SegmentId seg, SegmentType type) const;
384 
390  Object *getObject(reg_t pos) const;
391 
397  bool isObject(reg_t obj) const { return getObject(obj) != NULL; }
398 
399  // TODO: document this
400  bool isHeapObject(reg_t pos) const;
401 
410  const char *getObjectName(reg_t pos);
411 
416 
428  reg_t findObjectByName(const Common::String &name, int index = -1);
429 
434 
435  uint32 classTableSize() const { return _classTable.size(); }
436  Class getClass(int index) const { return _classTable[index]; }
437  void setClassOffset(int index, reg_t offset) { _classTable[index].reg = offset; }
438  void resizeClassTable(uint32 size) { _classTable.resize(size); }
439 
440  reg_t getSaveDirPtr() const { return _saveDirPtr; }
441  reg_t getParserPtr() const { return _parserPtr; }
442 
443 #ifdef ENABLE_SCI32
444  bool isValidAddr(reg_t reg, SegmentType expected) const {
445  SegmentObj *mobj = getSegmentObj(reg.getSegment());
446  return (mobj &&
447  mobj->getType() == expected &&
448  mobj->isValidOffset(reg.getOffset()));
449  }
450 
451  SciArray *allocateArray(SciArrayType type, uint16 size, reg_t *addr);
452  SciArray *lookupArray(reg_t addr);
453  void freeArray(reg_t addr);
454  bool isArray(reg_t addr) const;
455 
456  SciBitmap *allocateBitmap(reg_t *addr, const int16 width, const int16 height, const uint8 skipColor = kDefaultSkipColor, const int16 originX = 0, const int16 originY = 0, const int16 xResolution = kLowResX, const int16 yResolution = kLowResY, const uint32 paletteSize = 0, const bool remap = false, const bool gc = true);
457  SciBitmap *lookupBitmap(reg_t addr);
458  void freeBitmap(reg_t addr);
459 #endif
460 
461  const Common::Array<SegmentObj *> &getSegments() const { return _heap; }
462 
463 private:
465  Common::Array<Class> _classTable;
467  Common::HashMap<int, SegmentId> _scriptSegMap;
468 
469  ResourceManager *_resMan;
470  ScriptPatcher *_scriptPatcher;
471 
472  SegmentId _clonesSegId;
473  SegmentId _listsSegId;
474  SegmentId _nodesSegId;
475  SegmentId _hunksSegId;
476 
477  // Statically allocated memory for system strings
478  reg_t _saveDirPtr;
479  reg_t _parserPtr;
480 
481 #ifdef ENABLE_SCI32
482  SegmentId _arraysSegId;
483  SegmentId _bitmapSegId;
484 #endif
485 
486 public:
487  SegmentId allocSegment(SegmentObj *mobj);
488 
489 private:
490  void deallocate(SegmentId seg);
491  void createClassTable();
492 
493  SegmentId findFreeSegment() const;
494 
505  SegmentId getActualSegment(SegmentId seg) const;
506 };
507 
508 } // End of namespace Sci
509 
510 #endif // SCI_ENGINE_SEG_MANAGER_H
Script * getScriptIfLoaded(SegmentId seg) const
~SegManager() override
Definition: state.h:100
Definition: str.h:59
List * lookupList(reg_t addr)
byte * allocDynmem(int size, const char *description, reg_t *addr)
Definition: seg_manager.h:42
Clone * allocateClone(reg_t *addr)
void initSysStrings()
List * allocateList(reg_t *addr)
Definition: segment.h:37
void reconstructStack(EngineState *s)
reg_t findObjectByName(const Common::String &name, int index=-1)
SegManager(ResourceManager *resMan, ScriptPatcher *scriptPatcher)
Definition: serializer.h:79
byte * getHunkPointer(reg_t addr)
const char * getObjectName(reg_t pos)
Definition: segment.h:202
void strncpy(reg_t dest, reg_t src, size_t n)
reg_t allocateHunkEntry(const char *hunk_type, int size)
int instantiateScript(int script_nr, bool applyScriptPatches=true)
Definition: resource.h:327
Common::Array< reg_t > findObjectsBySuperClass(const Common::String &superClassName)
Common::String getString(reg_t pointer)
Definition: segment.h:82
void uninstantiateScript(int script_nr)
void strcpy_(reg_t dest, reg_t src)
reg_t * derefRegPtr(reg_t pointer, int entries)
Definition: script.h:71
Definition: object.h:69
SegmentId findSegmentByType(int type) const
Common::Array< reg_t > findObjectsByName(const Common::String &name)
Script * allocateScript(int script_nr, SegmentId &seg_id)
ScriptLoadType
Definition: seg_manager.h:40
byte * derefBulkPtr(reg_t pointer, int entries)
bool isObject(reg_t obj) const
Definition: seg_manager.h:397
void deallocateScript(int script_nr)
Definition: console.h:28
Definition: serializer.h:308
bool freeDynmem(reg_t addr)
Definition: seg_manager.h:48
Definition: script_patches.h:91
reg_t newNode(reg_t value, reg_t key)
void reconstructClones()
size_t strlen(reg_t str)
SegmentId getScriptSegment(int script_nr) const
void memcpy(reg_t dest, reg_t src, size_t n)
SegmentRef dereference(reg_t pointer)
Node * lookupNode(reg_t addr, bool stopOnDiscarded=true)
char * derefString(reg_t pointer, int entries=0)
DataStack * allocateStack(int size)
virtual bool isValidOffset(uint32 offset) const =0
Definition: seg_manager.h:43
Object * getObject(reg_t pos) const
Definition: seg_manager.h:41
Definition: segment.h:195
Node * allocateNode(reg_t *addr)
Definition: vm_types.h:39
Script * getScript(SegmentId seg)
Definition: segment.h:165
Definition: console.h:36
void freeHunkEntry(reg_t addr)
Definition: vm.h:59