ScummVM API documentation
stateprovider.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 STARK_SERVICES_STATE_PROVIDER_H
23 #define STARK_SERVICES_STATE_PROVIDER_H
24 
25 #include "common/hashmap.h"
26 #include "common/serializer.h"
27 #include "common/hash-str.h"
28 #include "common/stream.h"
29 #include "common/substream.h"
30 
31 #include "math/mathfwd.h"
32 
33 #include "engines/stark/resourcereference.h"
34 
35 namespace Stark {
36 
37 namespace Resources {
38 class Object;
39 class Level;
40 class Location;
41 }
42 
44 public:
45  explicit StateReadStream(Common::SeekableReadStream *parentStream, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::YES);
46  virtual ~StateReadStream();
47 
48  Common::String readString();
49 };
50 
52 public:
54 
55  void syncAsFloat(float &value);
56  void syncAsVector3d(Math::Vector3d &value);
57  void syncAsResourceReference(ResourceReference &reference);
58  void syncAsString32(Common::String &string);
59 
60  template<typename T>
61  void syncAsResourceReference(T **object, Version minVersion = 0, Version maxVersion = kLastVersion);
62 
63  template<typename T>
64  void syncArraySize(Common::Array<T> &array, Version minVersion = 0, Version maxVersion = kLastVersion);
65 };
66 
67 template<typename T>
68 void ResourceSerializer::syncAsResourceReference(T **object, Version minVersion, Version maxVersion) {
69  assert(object);
70 
71  if (_version < minVersion || _version > maxVersion)
72  return; // Ignore anything which is not supposed to be present in this save game version
73 
74  if (isLoading()) {
75  ResourceReference reference;
76  reference.loadFromStream(_loadStream);
77  *object = reference.resolve<T>();
78  } else {
79  ResourceReference reference;
80  reference.buildFromResource(*object);
81  reference.saveToStream(_saveStream);
82  }
83 }
84 
85 template<typename T>
86 void ResourceSerializer::syncArraySize(Common::Array<T> &array, Version minVersion, Version maxVersion) {
87  if (_version < minVersion || _version > maxVersion)
88  return; // Ignore anything which is not supposed to be present in this save game version
89 
90  uint32 size = array.size();
91  syncAsUint32LE(size);
92 
93  if (isLoading()) {
94  array.resize(size);
95  }
96 }
97 
104 public:
105  ~StateProvider();
106 
107  void restoreLevelState(Resources::Level *level);
108  void restoreCurrentLevelState(Resources::Level *level);
109  void restoreLocationState(Resources::Level *level, Resources::Location *location);
110  void restoreCurrentLocationState(Resources::Level *level, Resources::Location *location);
111  void restoreGlobalState(Resources::Level *level);
112 
113  void saveLevelState(Resources::Level *level);
114  void saveCurrentLevelState(Resources::Level *level);
115  void saveLocationState(Resources::Level *level, Resources::Location *location);
116  void saveCurrentLocationState(Resources::Level *level, Resources::Location *location);
117  void saveGlobalState(Resources::Level *level);
118 
120  void readStateFromStream(StateReadStream *stream, uint saveVersion);
121 
123  void writeStateToStream(Common::WriteStream *stream);
124 
126  void clear();
127 
128  static const uint kMinSaveVersion = 6;
129  static const uint kSaveVersion = 13;
130 
131 private:
132  class ResourceTreeState {
133  public:
134  ResourceTreeState(uint32 size, byte *data, uint32 version);
135  ~ResourceTreeState();
136 
137  uint32 getVersion() const { return _version; }
138  uint32 getSize() const { return _size; }
139  byte *getData() const { return _data; }
140 
141  private:
142  uint32 _version;
143  uint32 _size;
144  byte *_data;
145  };
146 
148 
149  void restoreResourceTreeState(const Common::String &storeKey, Resources::Object *root, bool current);
150  void saveResourceTreeState(const Common::String &storeKey, Resources::Object *root, bool current);
151 
152  void readResourceTree(Resources::Object *resource, Common::SeekableReadStream *stream, bool current, uint32 version);
153  void writeResourceTree(Resources::Object *resource, Common::WriteStream *stream, bool current);
154 
155  ResourceTreeStateMap _stateStore;
156 };
157 
158 } // End of namespace Stark
159 
160 #endif // STARK_SERVICES_STATE_PROVIDER_H
Definition: location.h:51
Definition: str.h:59
Definition: stream.h:77
Definition: array.h:52
T * resolve() const
Definition: resourcereference.h:84
Definition: stream.h:745
Definition: serializer.h:79
void loadFromStream(Common::ReadStream *stream)
Definition: resourcereference.h:39
Definition: console.h:27
Definition: object.h:143
Definition: substream.h:78
Definition: stateprovider.h:103
size_type size() const
Definition: array.h:315
void buildFromResource(Resources::Object *resource)
Definition: level.h:39
void saveToStream(Common::WriteStream *stream)
void resize(size_type newSize)
Definition: array.h:411
Definition: stateprovider.h:43
Definition: stateprovider.h:51