ScummVM API documentation
cc_script_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// IScriptObject: script managed object interface.
25// Provides interaction with a object which allocation and lifetime is
26// managed by the engine and/or the managed pool rather than the script VM.
27// These may be both static objects existing throughout the game, and
28// dynamic objects allocated by the script command.
29//
30//=============================================================================
31
32#ifndef AGS_ENGINE_AC_DYNOBJ_CC_SCRIPT_OBJECT_H
33#define AGS_ENGINE_AC_DYNOBJ_CC_SCRIPT_OBJECT_H
34
35#include "common/std/utility.h"
36#include "ags/shared/core/types.h"
37
38namespace AGS3 {
39
40// Forward declaration
41namespace AGS {
42namespace Shared {
43class Stream;
44} // namespace Shared
45} // namespace AGS
46
47struct IScriptObject;
48
49// A convenience struct for grouping handle and dynamic object
51 const int Handle = 0;
52 void *const Obj = nullptr;
53 IScriptObject *const Mgr = nullptr;
54
55 DynObjectRef() = default;
56 DynObjectRef(int handle, void *obj, IScriptObject *mgr)
57 : Handle(handle), Obj(obj), Mgr(mgr) {}
58};
59
61 // WARNING: The first section of this interface is also a part of the AGS plugin API!
62
63 // when a ref count reaches 0, this is called with the address
64 // of the object. Return 1 to remove the object from memory, 0 to
65 // leave it
66 // The "force" flag tells system to detach the object, breaking any links and references
67 // to other managed objects or game resources (instead of disposing these too).
68 // TODO: it might be better to rewrite the managed pool and remove this flag at all,
69 // because it makes the use of this interface prone to mistakes.
70 virtual int Dispose(void *address, bool force = false) = 0;
71 // return the type name of the object
72 virtual const char *GetType() = 0;
73 // serialize the object into BUFFER (which is BUFSIZE bytes)
74 // return number of bytes used
75 // TODO: pass savegame format version
76 virtual int Serialize(void *address, uint8_t *buffer, int bufsize) = 0;
77
78 // WARNING: following section is not a part of plugin API, therefore these methods
79 // should **never** be called for kScValPluginObject script objects!
80
81 // Legacy support for reading and writing object values by their relative offset.
82 // These methods allow to "remap" script struct field access, by taking the
83 // legacy offset, and using it rather as a field ID than an address, for example.
84 // Consequently these also let trigger side-effects, such as updating an object
85 // after a field value is written to.
86 // RE: GetFieldPtr() -
87 // According to AGS script specification, when the old-string pointer or char array is passed
88 // as an argument, the byte-code does not include any specific command for the member variable
89 // retrieval and instructs to pass an address of the object itself with certain offset.
90 // This results in functions like StrCopy writing directly over object address.
91 // There may be other implementations, but the big question is: how to detect when this is
92 // necessary, because byte-code does not contain any distinct operation for this case.
93 // The worst thing here is that with the current byte-code structure we can never tell whether
94 // offset 0 means getting pointer to whole object or a pointer to its first field.
95 virtual void *GetFieldPtr(void *address, intptr_t offset) = 0;
96 virtual void Read(void *address, intptr_t offset, uint8_t *dest, size_t size) = 0;
97 virtual uint8_t ReadInt8(void *address, intptr_t offset) = 0;
98 virtual int16_t ReadInt16(void *address, intptr_t offset) = 0;
99 virtual int32_t ReadInt32(void *address, intptr_t offset) = 0;
100 virtual float ReadFloat(void *address, intptr_t offset) = 0;
101 virtual void Write(void *address, intptr_t offset, const uint8_t *src, size_t size) = 0;
102 virtual void WriteInt8(void *address, intptr_t offset, uint8_t val) = 0;
103 virtual void WriteInt16(void *address, intptr_t offset, int16_t val) = 0;
104 virtual void WriteInt32(void *address, intptr_t offset, int32_t val) = 0;
105 virtual void WriteFloat(void *address, intptr_t offset, float val) = 0;
106
107protected:
108 IScriptObject() {}
109 virtual ~IScriptObject() {}
110};
111
112// The interface of a script objects deserializer that handles multiple types.
114 virtual ~ICCObjectCollectionReader() {}
115 // TODO: pass savegame format version
116 virtual void Unserialize(int32_t handle, const char *objectType, const char *serializedData, int dataSize) = 0;
117};
118
119// The interface of a script objects deserializer that handles a single type.
120// WARNING: a part of the plugin API.
122 virtual ~ICCObjectReader() {}
123 virtual void Unserialize(int32_t handle, const char *serializedData, int dataSize) = 0;
124};
125
126} // namespace AGS3
127
128#endif
Definition: cc_script_object.h:50
Definition: cc_script_object.h:113
Definition: cc_script_object.h:121
Definition: cc_script_object.h:60