File: cc_dynamic_array.cpp

package info (click to toggle)
scummvm 2.7.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 363,784 kB
  • sloc: cpp: 3,622,060; asm: 27,410; python: 10,528; sh: 10,241; xml: 6,752; java: 5,579; perl: 2,570; yacc: 1,635; javascript: 1,016; lex: 539; makefile: 398; ansic: 378; awk: 275; objc: 82; sed: 11; php: 1
file content (152 lines) | stat: -rw-r--r-- 5,082 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "ags/engine/ac/dynobj/cc_dynamic_array.h"
#include  "ags/globals.h"

namespace AGS3 {

// return the type name of the object
const char *CCDynamicArray::GetType() {
	return CC_DYNAMIC_ARRAY_TYPE_NAME;
}

int CCDynamicArray::Dispose(const char *address, bool force) {
	address -= 8;

	// If it's an array of managed objects, release their ref counts;
	// except if this array is forcefully removed from the managed pool,
	// in which case just ignore these.
	if (!force) {
		int *elementCount = (int *)const_cast<char *>(address);
		if (elementCount[0] & ARRAY_MANAGED_TYPE_FLAG) {
			elementCount[0] &= ~ARRAY_MANAGED_TYPE_FLAG;
			for (int i = 0; i < elementCount[0]; i++) {
				if (elementCount[2 + i] != 0) {
					ccReleaseObjectReference(elementCount[2 + i]);
				}
			}
		}
	}

	delete address;
	return 1;
}

// serialize the object into BUFFER (which is BUFSIZE bytes)
// return number of bytes used
int CCDynamicArray::Serialize(const char *address, char *buffer, int bufsize) {
	const int *sizeInBytes = &((const int *)address)[-1];
	int sizeToWrite = *sizeInBytes + 8;
	if (sizeToWrite > bufsize) {
		// buffer not big enough, ask for a bigger one
		return -sizeToWrite;
	}
	memcpy(buffer, address - 8, sizeToWrite);
	return sizeToWrite;
}

void CCDynamicArray::Unserialize(int index, const char *serializedData, int dataSize) {
	char *newArray = new char[dataSize];
	memcpy(newArray, serializedData, dataSize);
	ccRegisterUnserializedObject(index, &newArray[8], this);
}

DynObjectRef CCDynamicArray::Create(int numElements, int elementSize, bool isManagedType) {
	char *newArray = new char[numElements * elementSize + 8]();
	int *sizePtr = (int *)newArray;
	sizePtr[0] = numElements;
	sizePtr[1] = numElements * elementSize;
	if (isManagedType)
		sizePtr[0] |= ARRAY_MANAGED_TYPE_FLAG;
	void *obj_ptr = &newArray[8];
	int32_t handle = ccRegisterManagedObject(obj_ptr, this);
	if (handle == 0) {
		delete[] newArray;
		return DynObjectRef(0, nullptr);
	}
	return DynObjectRef(handle, obj_ptr);
}


const char *CCDynamicArray::GetFieldPtr(const char *address, intptr_t offset) {
	return address + offset;
}

void CCDynamicArray::Read(const char *address, intptr_t offset, void *dest, int size) {
	memcpy(dest, address + offset, size);
}

uint8_t CCDynamicArray::ReadInt8(const char *address, intptr_t offset) {
	return *(const uint8_t *)(address + offset);
}

int16_t CCDynamicArray::ReadInt16(const char *address, intptr_t offset) {
	return *(const int16_t *)(address + offset);
}

int32_t CCDynamicArray::ReadInt32(const char *address, intptr_t offset) {
	return *(const int32_t *)(address + offset);
}

float CCDynamicArray::ReadFloat(const char *address, intptr_t offset) {
	return *(const float *)(address + offset);
}

void CCDynamicArray::Write(const char *address, intptr_t offset, void *src, int size) {
	memcpy((void *)(const_cast<char *>(address) + offset), src, size);
}

void CCDynamicArray::WriteInt8(const char *address, intptr_t offset, uint8_t val) {
	*(uint8_t *)(const_cast<char *>(address) + offset) = val;
}

void CCDynamicArray::WriteInt16(const char *address, intptr_t offset, int16_t val) {
	*(int16_t *)(const_cast<char *>(address) + offset) = val;
}

void CCDynamicArray::WriteInt32(const char *address, intptr_t offset, int32_t val) {
	*(int32_t *)(const_cast<char *>(address) + offset) = val;
}

void CCDynamicArray::WriteFloat(const char *address, intptr_t offset, float val) {
	*(float *)(const_cast<char *>(address) + offset) = val;
}

DynObjectRef DynamicArrayHelpers::CreateStringArray(const std::vector<const char *> items) {
	// NOTE: we need element size of "handle" for array of managed pointers
	DynObjectRef arr = _GP(globalDynamicArray).Create(items.size(), sizeof(int32_t), true);
	if (!arr.second)
		return arr;
	// Create script strings and put handles into array
	int32_t *slots = static_cast<int32_t *>(arr.second);
	for (auto s : items) {
		DynObjectRef str = _G(stringClassImpl)->CreateString(s);
		// We must add reference count, because the string is going to be saved
		// within another object (array), not returned to script directly
		ccAddObjectReference(str.first);
		*(slots++) = str.first;
	}

	return arr;
}

} // namespace AGS3