1 //=============================================================================
2 //
3 // Adventure Game Studio (AGS)
4 //
5 // Copyright (C) 1999-2011 Chris Jones and 2011-20xx others
6 // The full list of copyright holders can be found in the Copyright.txt
7 // file, which is part of this source code distribution.
8 //
9 // The AGS source code is provided under the Artistic License 2.0.
10 // A copy of this license can be found in the file License.txt and at
11 // http://www.opensource.org/licenses/artistic-license-2.0.php
12 //
13 //=============================================================================
14
15 #include <stdio.h>
16 #include <string.h>
17 #include "cc_dynamicarray.h"
18
19 // return the type name of the object
GetType()20 const char *CCDynamicArray::GetType() {
21 return CC_DYNAMIC_ARRAY_TYPE_NAME;
22 }
23
Dispose(const char * address,bool force)24 int CCDynamicArray::Dispose(const char *address, bool force) {
25 address -= 8;
26
27 // If it's an array of managed objects, release their ref counts;
28 // except if this array is forcefully removed from the managed pool,
29 // in which case just ignore these.
30 if (!force)
31 {
32 int *elementCount = (int*)address;
33 if (elementCount[0] & ARRAY_MANAGED_TYPE_FLAG)
34 {
35 elementCount[0] &= ~ARRAY_MANAGED_TYPE_FLAG;
36 for (int i = 0; i < elementCount[0]; i++)
37 {
38 if (elementCount[2 + i] != 0)
39 {
40 ccReleaseObjectReference(elementCount[2 + i]);
41 }
42 }
43 }
44 }
45
46 delete address;
47 return 1;
48 }
49
50 // serialize the object into BUFFER (which is BUFSIZE bytes)
51 // return number of bytes used
Serialize(const char * address,char * buffer,int bufsize)52 int CCDynamicArray::Serialize(const char *address, char *buffer, int bufsize) {
53 int *sizeInBytes = &((int*)address)[-1];
54 int sizeToWrite = *sizeInBytes + 8;
55 if (sizeToWrite > bufsize)
56 {
57 // buffer not big enough, ask for a bigger one
58 return -sizeToWrite;
59 }
60 memcpy(buffer, address - 8, sizeToWrite);
61 return sizeToWrite;
62 }
63
Unserialize(int index,const char * serializedData,int dataSize)64 void CCDynamicArray::Unserialize(int index, const char *serializedData, int dataSize) {
65 char *newArray = new char[dataSize];
66 memcpy(newArray, serializedData, dataSize);
67 ccRegisterUnserializedObject(index, &newArray[8], this);
68 }
69
Create(int numElements,int elementSize,bool isManagedType)70 int32_t CCDynamicArray::Create(int numElements, int elementSize, bool isManagedType)
71 {
72 char *newArray = new char[numElements * elementSize + 8];
73 memset(newArray, 0, numElements * elementSize + 8);
74 int *sizePtr = (int*)newArray;
75 sizePtr[0] = numElements;
76 sizePtr[1] = numElements * elementSize;
77 if (isManagedType)
78 sizePtr[0] |= ARRAY_MANAGED_TYPE_FLAG;
79 return ccRegisterManagedObject(&newArray[8], this);
80 }
81
82
GetFieldPtr(const char * address,intptr_t offset)83 const char* CCDynamicArray::GetFieldPtr(const char *address, intptr_t offset)
84 {
85 return address + offset;
86 }
87
Read(const char * address,intptr_t offset,void * dest,int size)88 void CCDynamicArray::Read(const char *address, intptr_t offset, void *dest, int size)
89 {
90 memcpy(dest, address + offset, size);
91 }
92
ReadInt8(const char * address,intptr_t offset)93 uint8_t CCDynamicArray::ReadInt8(const char *address, intptr_t offset)
94 {
95 return *(uint8_t*)(address + offset);
96 }
97
ReadInt16(const char * address,intptr_t offset)98 int16_t CCDynamicArray::ReadInt16(const char *address, intptr_t offset)
99 {
100 return *(int16_t*)(address + offset);
101 }
102
ReadInt32(const char * address,intptr_t offset)103 int32_t CCDynamicArray::ReadInt32(const char *address, intptr_t offset)
104 {
105 return *(int32_t*)(address + offset);
106 }
107
ReadFloat(const char * address,intptr_t offset)108 float CCDynamicArray::ReadFloat(const char *address, intptr_t offset)
109 {
110 return *(float*)(address + offset);
111 }
112
Write(const char * address,intptr_t offset,void * src,int size)113 void CCDynamicArray::Write(const char *address, intptr_t offset, void *src, int size)
114 {
115 memcpy((void*)(address + offset), src, size);
116 }
117
WriteInt8(const char * address,intptr_t offset,uint8_t val)118 void CCDynamicArray::WriteInt8(const char *address, intptr_t offset, uint8_t val)
119 {
120 *(uint8_t*)(address + offset) = val;
121 }
122
WriteInt16(const char * address,intptr_t offset,int16_t val)123 void CCDynamicArray::WriteInt16(const char *address, intptr_t offset, int16_t val)
124 {
125 *(int16_t*)(address + offset) = val;
126 }
127
WriteInt32(const char * address,intptr_t offset,int32_t val)128 void CCDynamicArray::WriteInt32(const char *address, intptr_t offset, int32_t val)
129 {
130 *(int32_t*)(address + offset) = val;
131 }
132
WriteFloat(const char * address,intptr_t offset,float val)133 void CCDynamicArray::WriteFloat(const char *address, intptr_t offset, float val)
134 {
135 *(float*)(address + offset) = val;
136 }
137
138 CCDynamicArray globalDynamicArray;
139