ScummVM API documentation
script_user_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// ScriptUserObject is a dynamic (managed) struct manager.
25//
26//=============================================================================
27
28#ifndef AGS_ENGINE_DYNOBJ__SCRIPTUSERSTRUCT_H
29#define AGS_ENGINE_DYNOBJ__SCRIPTUSERSTRUCT_H
30
31#include "ags/engine/ac/dynobj/cc_ags_dynamic_object.h"
32#include "ags/shared/util/stream.h"
33
34namespace AGS3 {
35
37public:
38 static const char *TypeName;
39
40 struct Header {
41 uint32_t Size = 0u;
42 // NOTE: we use signed int for Size at the moment, because the managed
43 // object interface's Serialize() function requires the object to return
44 // negative value of size in case the provided buffer was not large
45 // enough. Since this interface is also a part of Plugin API, we would
46 // need more significant change to program before we could use different
47 // approach.
48 };
49
50 ScriptUserObject() = default;
51 ~ScriptUserObject() = default;
52
53 inline static const Header &GetHeader(const void *address) {
54 return reinterpret_cast<const Header &>(*(static_cast<const uint8_t *>(address) - MemHeaderSz));
55 }
56
57 // Create managed struct object and return a pointer to the beginning of a buffer
58 static DynObjectRef Create(size_t size);
59
60 // return the type name of the object
61 const char *GetType() override;
62 int Dispose(void *address, bool force) override;
63 void Unserialize(int index, AGS::Shared::Stream *in, size_t data_sz) override;
64
65private:
66 // The size of the array's header in memory, prepended to the element data
67 static const size_t MemHeaderSz = sizeof(Header);
68 // The size of the serialized header
69 static const size_t FileHeaderSz = sizeof(uint32_t) * 0; // no header serialized
70
71 // Savegame serialization
72 // Calculate and return required space for serialization, in bytes
73 size_t CalcSerializeSize(const void *address) override;
74 // Write object data into the provided stream
75 void Serialize(const void *address, AGS::Shared::Stream *out) override;
76};
77
78// Helper functions for setting up custom managed structs based on ScriptUserObject.
79namespace ScriptStructHelpers {
80// Creates a managed Point object, represented as a pair of X and Y coordinates.
81ScriptUserObject *CreatePoint(int x, int y);
82} // namespace ScriptStructHelpers
83
84} // namespace AGS3
85
86#endif
Definition: stream.h:52
Definition: cc_ags_dynamic_object.h:88
Definition: cc_script_object.h:50
Definition: script_user_object.h:40
Definition: script_user_object.h:36
Definition: geometry.h:148