-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuscmdrafttypes.pas
283 lines (241 loc) · 7.81 KB
/
uscmdrafttypes.pas
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
280
281
282
283
unit uscmdrafttypes;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, umapinfo;
type
TMenuSection = LongWord;
TRequestedMenuSections = array[0..7] of TMenuSection;
PRequestedMenuSections = ^TRequestedMenuSections;
TAllocRamProc = function(Size: DWord): Pointer; stdcall;
TDeAllocRamProc = procedure(Ram: Pointer); stdcall;
TReAllocRamProc = function(Ram: Pointer; Size: DWord): Pointer; stdcall;
const
MIN_FORCE = 1;
MAX_FORCE = 4;
MIN_UNIT_TYPE = 0;
MAX_UNIT_TYPE = 227;
MIN_WAV = 0;
MAX_WAV = 511;
MIN_SWITCH = 1;
MAX_SWITCH = 256;
type
TLocationNode = packed record
x0,y0,x1,y1: DWord;
NameIndex,Elevation: Word;
Used: byte;
end;
TLocationNodes = packed array[LocationMinIndex..LocationMaxIndex] of TLocationNode;
PLocationNodes = ^TLocationNodes;
TUnitNames = array[MIN_UNIT_TYPE..MAX_UNIT_TYPE] of PChar;
PStandardUnitNames = ^TUnitNames;
TForceNames = array[MIN_FORCE..MAX_FORCE] of LongInt;
PForceNames = ^TForceNames;
TCustomUnitNames = array[MIN_UNIT_TYPE..MAX_UNIT_TYPE] of LongInt;
PCustomUnitNames = ^TCustomUnitNames;
TWavFilenames = array[MIN_WAV..MAX_WAV] of LongInt;
PWavFilenames = ^TWavFilenames;
TSwitchRenamingData = array[MIN_SWITCH..MAX_SWITCH] of LongInt;
PSwitchRenamingData = ^TSwitchRenamingData;
PSCStringList_VirtualCalls = ^TSCStringList_VirtualCalls;
TSCStringList_VirtualCalls = record
filler1: array[1..3] of DWord; FindString_RawIndexProc: pointer;
GetStringProc, UnknownProc1, AddSCMD2StringProc, DereferenceProc : pointer;
DerefAndAddStringProc, SetSCMD2Text, UnknownProc2, GetTotalStringNum : Pointer;
UnknownProc3, UnknownProc4, UnknownProc5, BackupStringsProc: Pointer;
RestoreBackupProc, ClearBackupProc: Pointer;
end;
{ TSCStringList }
PSCStringList = ^TSCStringList;
TSCStringList = object
VirtualCalls: PSCStringList_VirtualCalls;
function GetString(AIndex: LongInt): string;
function IndexOf(AText: string): integer;
function GetCapacity: integer;
function AllocateString(AText: string; ASection: LongWord; AAlwaysCreate: boolean = false): integer;
procedure ReleaseString(AIndex: integer; ASection: LongWord);
end;
PEngineData = ^TEngineData;
{ TEngineData }
TEngineData = object
Size: Word;
StatTxt, MapStrings: PSCStringList;
MapInternalStrings: Pointer;
MapLocations: PLocationNodes;
WavFilenames: PWavFilenames;
ActionLog: Pointer;
ActionLogLevel: Word;
DataInterface: pointer;
CurSelLocation: PLongInt;
CustomUnitNames: PCustomUnitNames;
ForceNames: PForceNames;
StandardUnitNames: PStandardUnitNames;
function GetLocationName(AIndex: integer): string;
function GetLocationIndex(AName: string): integer;
function LocationExists(AIndex: integer): boolean;
function GetForceName(AForce: integer): string;
function GetStandardUnitName(AIndex: integer): string;
function GetCustomUnitName(AIndex: integer): string;
function GetWavFilename(AIndex: integer): string;
function GetWavIndex(AFilename: string): integer;
function TrigAllocateString(AText: string): integer;
procedure TrigReleaseString(AIndex: integer);
end;
type
PChunkData = ^TChunkData;
TChunkData = record
Size: DWord;
Data: Pointer;
end;
function SectionCodeToLongWord(ACode: string): LongWord;
implementation
function SectionCodeToLongWord(ACode: string): LongWord;
var
chars: array[1..4] of char absolute result;
begin
if length(ACode)<>4 then raise exception.Create('Code is supposed to be 4 chars long');
chars[4] := ACode[1];
chars[3] := ACode[2];
chars[2] := ACode[3];
chars[1] := ACode[4];
end;
{ TEngineData }
function TEngineData.GetLocationName(AIndex: integer): string;
begin
if (AIndex < LocationMinIndex) or (AIndex > LocationMaxIndex) then
raise exception.Create('Index out of bounds');
with MapLocations^[AIndex] do
result := MapStrings^.GetString(NameIndex)
end;
function TEngineData.GetLocationIndex(AName: string): integer;
var
i, idxStr: Integer;
begin
idxStr := MapStrings^.IndexOf(AName);
if idxStr > 0 then
begin
for i := LocationMinIndex to LocationMaxIndex do
if MapLocations^[i].NameIndex = idxStr then exit(i);
end;
exit(-1);
end;
function TEngineData.LocationExists(AIndex: integer): boolean;
begin
if (AIndex < LocationMinIndex) or (AIndex > LocationMaxIndex) then
raise exception.Create('Index out of bounds');
result := MapLocations^[AIndex].NameIndex<>0;
end;
function TEngineData.GetForceName(AForce: integer): string;
begin
result := MapStrings^.GetString(ForceNames^[AForce]);
end;
function TEngineData.GetStandardUnitName(AIndex: integer): string;
begin
if (AIndex < MIN_UNIT_TYPE) or (AIndex > MAX_UNIT_TYPE) then
raise exception.Create('Index out of bounds');
result := StandardUnitNames^[AIndex];
end;
function TEngineData.GetCustomUnitName(AIndex: integer): string;
begin
if (AIndex < MIN_UNIT_TYPE) or (AIndex > MAX_UNIT_TYPE) then
raise exception.Create('Index out of bounds');
result := MapStrings^.GetString(CustomUnitNames^[AIndex]);
end;
function TEngineData.GetWavFilename(AIndex: integer): string;
begin
if (AIndex < MIN_WAV) or (AIndex > MAX_WAV) then
raise exception.Create('Index out of bounds');
result := MapStrings^.GetString(WavFilenames^[AIndex]);
end;
function TEngineData.GetWavIndex(AFilename: string): integer;
var
i: Integer;
begin
//cannot check via string index because it may not be unique
for i := MIN_WAV to MAX_WAV do
if GetWavFilename(i) = AFilename then exit(i);
exit(-1);
end;
function TEngineData.TrigAllocateString(AText: string): integer;
begin
result := MapStrings^.AllocateString(AText, SectionCodeToLongWord('TRIG'));
end;
procedure TEngineData.TrigReleaseString(AIndex: integer);
begin
MapStrings^.ReleaseString(AIndex, SectionCodeToLongWord('TRIG'));
end;
{$asmmode intel}
{ TSCStringList }
function TSCStringList.GetString(AIndex: LongInt): string;
var p: PChar;
begin
if AIndex = 0 then exit('');
asm
mov eax, AIndex
mov ecx, self
push eax
mov eax, [ecx + TSCStringList.VirtualCalls]
mov eax, [eax + TSCStringList_VirtualCalls.GetStringProc]
call eax
mov p, eax
end;
result := p;
end;
function TSCStringList.IndexOf(AText: string): integer;
begin
if AText = '' then exit(0);
asm
mov eax, AText
mov ecx, self
push eax
mov eax, [ecx + TSCStringList.VirtualCalls]
mov eax, [eax + TSCStringList_VirtualCalls.FindString_RawIndexProc]
call eax
mov result, eax
end;
if result >= 0 then inc(result);
end;
function TSCStringList.GetCapacity: integer;
begin
asm
mov ecx, self
mov eax, [ecx + TSCStringList.VirtualCalls]
mov eax, [eax + TSCStringList_VirtualCalls.GetTotalStringNum]
call eax
mov result, eax
end;
end;
function TSCStringList.AllocateString(AText: string; ASection: LongWord;
AAlwaysCreate: boolean): integer;
begin
if AText = '' then exit(0);
asm
xor eax, eax
mov al, AAlwaysCreate
push eax
mov eax, ASection
push eax
mov eax, AText
push eax
mov ecx, self
mov eax, [ecx + TSCStringList.VirtualCalls]
mov eax, [eax + TSCStringList_VirtualCalls.AddSCMD2StringProc]
call eax
mov result, eax
end;
end;
procedure TSCStringList.ReleaseString(AIndex: integer; ASection: LongWord);
begin
if (AIndex <= 0) or (AIndex > 32767) then exit;
asm
mov eax, ASection
push eax
mov eax, AIndex
push eax
mov ecx, self
mov eax, [ecx + TSCStringList.VirtualCalls]
mov eax, [eax + TSCStringList_VirtualCalls.DereferenceProc]
call eax
end;
end;
end.