summaryrefslogtreecommitdiff
path: root/sourcemod/scripting/momsurffix/utils.sp
blob: e68e342b374122df75344e82471c096ddd79370d (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#define MALLOC(%1) view_as<%1>(AllocatableBase._malloc(%1.Size(), #%1))
#define MEMORYPOOL_NAME_MAX 128

enum struct MemoryPoolEntry
{
	Address addr;
	char name[MEMORYPOOL_NAME_MAX];
}

methodmap AllocatableBase < AddressBase
{
	public static Address _malloc(int size, const char[] name)
	{
		Address addr = Malloc(size, name);
		
		return addr;
	}
	
	public void Free()
	{
		Free(this.Address);
	}
}

methodmap PseudoStackArray < AddressBase
{
	public Address Get8(int idx, int size = 4)
	{
		ASSERT(idx >= 0);
		ASSERT(size > 0);
		
		return view_as<Address>(LoadFromAddress(this.Address + (idx * size), NumberType_Int8));
	}
	
	public Address Get16(int idx, int size = 4)
	{
		ASSERT(idx >= 0);
		ASSERT(size > 0);
		
		return view_as<Address>(LoadFromAddress(this.Address + (idx * size), NumberType_Int16));
	}
	
	public Address Get32(int idx, int size = 4)
	{
		ASSERT(idx >= 0);
		ASSERT(size > 0);
		
		return this.Address + (idx * size);
	}
}

methodmap Vector < AllocatableBase
{
	public static int Size()
	{
		return 12;
	}
	
	public Vector()
	{
		return MALLOC(Vector);
	}
	
	property float x
	{
		public set(float _x) { StoreToAddress(this.Address, view_as<int>(_x), NumberType_Int32, false); }
		public get() { return view_as<float>(LoadFromAddress(this.Address, NumberType_Int32)); }
	}
	
	property float y
	{
		public set(float _y) { StoreToAddress(this.Address + 4, view_as<int>(_y), NumberType_Int32, false); }
		public get() { return view_as<float>(LoadFromAddress(this.Address + 4, NumberType_Int32)); }
	}
	
	property float z
	{
		public set(float _z) { StoreToAddress(this.Address + 8, view_as<int>(_z), NumberType_Int32, false); }
		public get() { return view_as<float>(LoadFromAddress(this.Address + 8, NumberType_Int32)); }
	}
	
	public void ToArray(float buff[3])
	{
		buff[0] = this.x;
		buff[1] = this.y;
		buff[2] = this.z;
	}
	
	public void FromArray(float buff[3])
	{
		this.x = buff[0];
		this.y = buff[1];
		this.z = buff[2];
	}
	
	public void CopyTo(Vector dst)
	{
		dst.x = this.x;
		dst.y = this.y;
		dst.z = this.z;
	}
	
	public float LengthSqr()
	{
		return this.x*this.x + this.y*this.y + this.z*this.z;
	}
	
	public float Length()
	{
		return SquareRoot(this.LengthSqr());
	}
	
	public float Dot(float vec[3])
	{
		return this.x*vec[0] + this.y*vec[1] + this.z*vec[2];
	}
}

static Address g_pMemAlloc;
static Handle gMalloc, gFree, gCreateInterface;
static ArrayList gMemoryPool;

stock void InitUtils(GameData gd)
{
	gMemoryPool = new ArrayList(sizeof(MemoryPoolEntry));
	
	//CreateInterface
	StartPrepSDKCall(SDKCall_Static);
	
	ASSERT_MSG(PrepSDKCall_SetFromConf(gd, SDKConf_Signature, "CreateInterface"), "Failed to get \"CreateInterface\" signature. Gamedata needs an update.");
	
	PrepSDKCall_AddParameter(SDKType_String, SDKPass_Pointer);
	PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
	
	PrepSDKCall_SetReturnInfo(SDKType_PlainOldData, SDKPass_Plain);
	
	gCreateInterface = EndPrepSDKCall();
	ASSERT(gCreateInterface);
	
	if(gEngineVersion == Engine_CSGO || gOSType == OSWindows)
	{
		//g_pMemAlloc
		g_pMemAlloc = gd.GetAddress("g_pMemAlloc");
		ASSERT_MSG(g_pMemAlloc != Address_Null, "Can't get \"g_pMemAlloc\" address from gamedata. Gamedata needs an update.");
		
		//Malloc
		StartPrepSDKCall(SDKCall_Raw);
		
		PrepSDKCall_SetVirtual(gd.GetOffset("Malloc"));
		
		PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
		PrepSDKCall_SetReturnInfo(SDKType_PlainOldData, SDKPass_Plain);
		
		gMalloc = EndPrepSDKCall();
		ASSERT(gMalloc);
		
		//Free
		StartPrepSDKCall(SDKCall_Raw);
		
		PrepSDKCall_SetVirtual(gd.GetOffset("Free"));
		
		PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
		
		gFree = EndPrepSDKCall();
		ASSERT(gFree);
	}
	else
	{
		//Malloc
		StartPrepSDKCall(SDKCall_Static);
		ASSERT_MSG(PrepSDKCall_SetFromConf(gd, SDKConf_Signature, "malloc"), "Failed to get \"malloc\" signature. Gamedata needs an update.");
		
		PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
		PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
		
		PrepSDKCall_SetReturnInfo(SDKType_PlainOldData, SDKPass_Plain);
		
		gMalloc = EndPrepSDKCall();
		ASSERT(gMalloc);
		
		//Free
		StartPrepSDKCall(SDKCall_Static);
		ASSERT_MSG(PrepSDKCall_SetFromConf(gd, SDKConf_Signature, "free"), "Failed to get \"free\" signature. Gamedata needs an update.");
		
		PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
		PrepSDKCall_AddParameter(SDKType_PlainOldData, SDKPass_Plain);
		
		gFree = EndPrepSDKCall();
		ASSERT(gFree);
	}
}

stock Address CreateInterface(const char[] name)
{
	return SDKCall(gCreateInterface, name, 0);
}

stock Address Malloc(int size, const char[] name)
{
	ASSERT(gMemoryPool);
	ASSERT(size > 0);
	
	MemoryPoolEntry entry;
	strcopy(entry.name, sizeof(MemoryPoolEntry::name), name);
	
	if(gEngineVersion == Engine_CSS && gOSType == OSLinux)
		entry.addr = SDKCall(gMalloc, 0, size);
	else
		entry.addr = SDKCall(gMalloc, g_pMemAlloc, size);
	
	ASSERT_FMT(entry.addr != Address_Null, "Failed to allocate memory (size: %i)!", size);
	gMemoryPool.PushArray(entry);
	
	return entry.addr;
}

stock void Free(Address addr)
{
	ASSERT(addr != Address_Null);
	ASSERT(gMemoryPool);
	int idx = gMemoryPool.FindValue(addr, MemoryPoolEntry::addr);
	
	//Memory wasn't allocated by this plugin, return.
	if(idx == -1)
		return;
	
	gMemoryPool.Erase(idx);
	
	if(gEngineVersion == Engine_CSS && gOSType == OSLinux)
		SDKCall(gFree, 0, addr);
	else
		SDKCall(gFree, g_pMemAlloc, addr);
}

stock void AddToMemoryPool(Address addr, const char[] name)
{
	ASSERT(addr != Address_Null);
	ASSERT(gMemoryPool);
	
	MemoryPoolEntry entry;
	strcopy(entry.name, sizeof(MemoryPoolEntry::name), name);
	entry.addr = addr;
	
	gMemoryPool.PushArray(entry);
}

stock void CleanUpUtils()
{
	if(!gMemoryPool)
		return;
	
	MemoryPoolEntry entry;
	
	for(int i = 0; i < gMemoryPool.Length; i++)
	{
		gMemoryPool.GetArray(i, entry, sizeof(MemoryPoolEntry));
		view_as<AllocatableBase>(entry.addr).Free();
	}
	
	delete gMemoryPool;
}

stock void DumpMemoryUsage()
{
	if(!gMemoryPool || (gMemoryPool && gMemoryPool.Length == 0))
	{
		PrintToServer(SNAME..."Theres's currently no active pool or it's empty!");
		return;
	}
	
	MemoryPoolEntry entry;
	
	PrintToServer(SNAME..."Active memory pool (%i):", gMemoryPool.Length);
	for(int i = 0; i < gMemoryPool.Length; i++)
	{
		gMemoryPool.GetArray(i, entry, sizeof(MemoryPoolEntry));
		PrintToServer(SNAME..."[%i]: 0x%08X \"%s\"", i, entry.addr, entry.name);
	}
}