ScummVM API documentation
cc_dynamic_object.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 //=============================================================================
23 //
24 // Managed script object interface.
25 //
26 //=============================================================================
27 
28 #ifndef AGS_ENGINE_AC_DYNOBJ_CC_DYNAMIC_OBJECT_H
29 #define AGS_ENGINE_AC_DYNOBJ_CC_DYNAMIC_OBJECT_H
30 
31 #include "ags/lib/std/utility.h"
32 #include "ags/shared/core/types.h"
33 
34 namespace AGS3 {
35 
36 // Forward declaration
37 namespace AGS {
38 namespace Shared {
39 class Stream;
40 } // namespace Shared
41 } // namespace AGS
42 
43 using namespace AGS; // FIXME later
44 
45 // A pair of managed handle and abstract object pointer
46 typedef std::pair<int32_t, void *> DynObjectRef;
47 
48 
49 // OBJECT-BASED SCRIPTING RUNTIME FUNCTIONS
50 // interface
52  // when a ref count reaches 0, this is called with the address
53  // of the object. Return 1 to remove the object from memory, 0 to
54  // leave it
55  // The "force" flag tells system to detach the object, breaking any links and references
56  // to other managed objects or game resources (instead of disposing these too).
57  // TODO: it might be better to rewrite the managed pool and remove this flag at all,
58  // because it makes the use of this interface prone to mistakes.
59  virtual int Dispose(const char *address, bool force = false) = 0;
60  // return the type name of the object
61  virtual const char *GetType() = 0;
62  // serialize the object into BUFFER (which is BUFSIZE bytes)
63  // return number of bytes used
64  // TODO: pass savegame format version
65  virtual int Serialize(const char *address, char *buffer, int bufsize) = 0;
66 
67  // Legacy support for reading and writing object values by their relative offset.
68  // WARNING: following were never a part of plugin API, therefore these methods
69  // should **never** be called for kScValPluginObject script objects!
70  //
71  // RE: GetFieldPtr()
72  // According to AGS script specification, when the old-string pointer or char array is passed
73  // as an argument, the byte-code does not include any specific command for the member variable
74  // retrieval and instructs to pass an address of the object itself with certain offset.
75  // This results in functions like StrCopy writing directly over object address.
76  // There may be other implementations, but the big question is: how to detect when this is
77  // necessary, because byte-code does not contain any distinct operation for this case.
78  // The worst thing here is that with the current byte-code structure we can never tell whether
79  // offset 0 means getting pointer to whole object or a pointer to its first field.
80  virtual const char *GetFieldPtr(const char *address, intptr_t offset) = 0;
81  virtual void Read(const char *address, intptr_t offset, void *dest, int size) = 0;
82  virtual uint8_t ReadInt8(const char *address, intptr_t offset) = 0;
83  virtual int16_t ReadInt16(const char *address, intptr_t offset) = 0;
84  virtual int32_t ReadInt32(const char *address, intptr_t offset) = 0;
85  virtual float ReadFloat(const char *address, intptr_t offset) = 0;
86  virtual void Write(const char *address, intptr_t offset, void *src, int size) = 0;
87  virtual void WriteInt8(const char *address, intptr_t offset, uint8_t val) = 0;
88  virtual void WriteInt16(const char *address, intptr_t offset, int16_t val) = 0;
89  virtual void WriteInt32(const char *address, intptr_t offset, int32_t val) = 0;
90  virtual void WriteFloat(const char *address, intptr_t offset, float val) = 0;
91 
92 protected:
93  ICCDynamicObject() {}
94  virtual ~ICCDynamicObject() {}
95 };
96 
98  virtual ~ICCObjectReader() {}
99  // TODO: pass savegame format version
100  virtual void Unserialize(int index, const char *objectType, const char *serializedData, int dataSize) = 0;
101 };
102 
104  virtual ~ICCStringClass() {}
105  virtual DynObjectRef CreateString(const char *fromText) = 0;
106 };
107 
108 // set the class that will be used for dynamic strings
109 extern void ccSetStringClassImpl(ICCStringClass *theClass);
110 // register a memory handle for the object and allow script
111 // pointers to point to it
112 extern int32_t ccRegisterManagedObject(const void *object, ICCDynamicObject *, bool plugin_object = false);
113 // register a de-serialized object
114 extern int32_t ccRegisterUnserializedObject(int index, const void *object, ICCDynamicObject *, bool plugin_object = false);
115 // unregister a particular object
116 extern int ccUnRegisterManagedObject(const void *object);
117 // remove all registered objects
118 extern void ccUnregisterAllObjects();
119 // serialize all objects to disk
120 extern void ccSerializeAllObjects(Shared::Stream *out);
121 // un-serialise all objects (will remove all currently registered ones)
122 extern int ccUnserializeAllObjects(Shared::Stream *in, ICCObjectReader *callback);
123 // dispose the object if RefCount==0
124 extern void ccAttemptDisposeObject(int32_t handle);
125 // translate between object handles and memory addresses
126 extern int32_t ccGetObjectHandleFromAddress(const void *address);
127 // TODO: not sure if it makes any sense whatsoever to use "const char*"
128 // in these functions, might as well change to char* or just void*.
129 extern const char *ccGetObjectAddressFromHandle(int32_t handle);
130 
131 extern int ccAddObjectReference(int32_t handle);
132 extern int ccReleaseObjectReference(int32_t handle);
133 
134 } // namespace AGS3
135 
136 #endif
Definition: achievements_tables.h:27
Definition: cc_dynamic_object.h:97
Definition: cc_dynamic_object.h:103
Definition: cc_dynamic_object.h:51
Definition: stream.h:52
Definition: ags.h:40