-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathWeaponDataSaveLoad.pas
More file actions
491 lines (394 loc) · 12.9 KB
/
WeaponDataSaveLoad.pas
File metadata and controls
491 lines (394 loc) · 12.9 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
unit WeaponDataSaveLoad;
{$IFDEF FPC}{$MODE DELPHI}{$ENDIF}
interface
function Init:boolean;
implementation
uses BaseGameData, HudItemUtils, WeaponAdditionalBuffer, sysutils, xr_Cartridge, Misc, WeaponEvents, ActorUtils, dynamic_caster;
//çàãðóçêà/ñîõðàíåíèå èãðû
const
CWEAPON_SAVE_FLAG_LASER_ENABLED:cardinal = $1;
CWEAPON_SAVE_FLAG_JUST_AFTER_RELOAD:cardinal = $2;
CWEAPON_SAVE_FLAG_EXPLOSED_STATUS:cardinal = $4;
CWEAPON_SAVE_FLAG_TORCH_ENABLED:cardinal = $100;
CWEAPON_SAVE_FLAG_MISFIRE_STATUS:cardinal = $10000;
CWEAPON_SAVEDATA_VERSION:cardinal = 2;
procedure CWeapon__load(wpn:pointer; packet:pIReader); stdcall;
var
buf:WpnBuf;
tmp_bool:boolean;
tmp_byte,tmp_byte2:byte;
tmp_cardinal:cardinal;
tmp_single:single;
tmp_int:integer;
ammos_in_mag, i, cnt, cnt_total:word;
ammotype:byte;
so:pointer;
lens_params:lens_zoom_params;
ver:byte;
begin
if not WpnCanShoot(wpn) then exit;
buf:=GetBuffer(wpn);
if (buf=nil) then begin
buf:=WpnBuf.Create(wpn);
end;
ReadFromReader(packet, @tmp_cardinal, sizeof(tmp_cardinal));
ver:=tmp_cardinal shr 24;
buf.SetLaserEnabledStatus((tmp_cardinal and CWEAPON_SAVE_FLAG_LASER_ENABLED) <> 0);
buf.SwitchTorch((tmp_cardinal and CWEAPON_SAVE_FLAG_TORCH_ENABLED) <> 0);
SetWeaponMisfireStatus(wpn, (tmp_cardinal and CWEAPON_SAVE_FLAG_MISFIRE_STATUS) <> 0);
buf.SetExplosed((tmp_cardinal and CWEAPON_SAVE_FLAG_EXPLOSED_STATUS) <> 0);
buf.SetJustAfterReloadStatus((tmp_cardinal and CWEAPON_SAVE_FLAG_JUST_AFTER_RELOAD) <> 0);
lens_params:=buf.GetLensParams();
ReadFromReader(packet, @lens_params.target_position, sizeof(lens_params.target_position));
lens_params.real_position:=lens_params.target_position;
buf.SetLensParams(lens_params);
ReadFromReader(packet, @tmp_single, sizeof(tmp_single));
buf.SetOffsetDir(tmp_single);
//ReloadNightBrightnessParams è ïîäîáíûå åìó âûçûâàòü çäåñü íåëüçÿ - ó íàñ åùå íå ïðîãðóçèëèñü è íå óñòàíîâèëèñü àïãðåéäû, ïðèöåëà c íóæíûì èíäåêñîì ìîæåò òóïî íå áûòü!
ReadFromReader(packet, @tmp_int, sizeof(tmp_int));
buf.SetNightBrightnessSavedStep(tmp_int);
ReadFromReader(packet, @ammos_in_mag, sizeof(ammos_in_mag));
SetLength(buf.ammos, ammos_in_mag);
cnt_total:=0;
while cnt_total<ammos_in_mag do begin
ReadFromReader(packet, @cnt, sizeof(cnt));
ReadFromReader(packet, @ammotype, sizeof(ammotype));
for i:=cnt_total to cnt_total+cnt-1 do begin
buf.ammos[i]:=ammotype;
end;
cnt_total:=cnt_total+cnt;
end;
if (IsExplosed(wpn)) then begin
OnWeaponExplode_AfterAnim(wpn, 0);
end;
if ver >= 2 then begin
ReadFromReader(packet, @tmp_byte, sizeof(tmp_byte));
buf.SetLastShotAmmoType(tmp_byte);
end;
end;
procedure CWeapon__save(wpn:pointer; packet:pointer); stdcall;
var
buf:WpnBuf;
tmp_bool:boolean;
tmp_byte:byte;
tmp_cardinal:cardinal;
tmp_single:single;
tmp_stepped:stepped_params;
i, cnt, max_in_mag:word;
ammotype:byte;
c:pCCartridge;
lens_params:lens_zoom_params;
begin
if not WpnCanShoot(wpn) then exit;
buf:=GetBuffer(wpn);
if buf = nil then exit;
tmp_cardinal:=CWEAPON_SAVEDATA_VERSION shl 24;
if buf.IsLaserEnabled() then begin
tmp_cardinal:=tmp_cardinal or CWEAPON_SAVE_FLAG_LASER_ENABLED;
end;
if buf.IsTorchEnabled() then begin
tmp_cardinal:=tmp_cardinal or CWEAPON_SAVE_FLAG_TORCH_ENABLED;
end;
if IsWeaponJammed(wpn) then begin
tmp_cardinal:=tmp_cardinal or CWEAPON_SAVE_FLAG_MISFIRE_STATUS;
end;
if buf.IsExplosed() then begin
tmp_cardinal:=tmp_cardinal or CWEAPON_SAVE_FLAG_EXPLOSED_STATUS;
end;
if buf.IsJustAfterReload() then begin
tmp_cardinal:=tmp_cardinal or CWEAPON_SAVE_FLAG_JUST_AFTER_RELOAD;
end;
WriteToPacket (packet, @tmp_cardinal, sizeof(tmp_cardinal));
lens_params:=buf.GetLensParams();
WriteToPacket(packet, @lens_params.target_position, sizeof(lens_params.target_position));
tmp_single:=buf.GetLensOffsetDir();
WriteToPacket(packet, @tmp_single, sizeof(tmp_single));
tmp_stepped:=buf.GetCurBrightness();
WriteToPacket(packet, @tmp_stepped.cur_step, sizeof(tmp_stepped.cur_step));
//ñîõðàíÿåì òèïû ïàòðîíîâ â ìàãàçèíå, ïðî ïîäñòâîë çàáûâàåì
max_in_mag:=GetAmmoInMagCount(wpn);
WriteToPacket (packet, @max_in_mag, sizeof(max_in_mag));
if max_in_mag>0 then begin
max_in_mag:=max_in_mag-1;
c:=GetCartridgeFromMagVector(wpn, 0);
cnt:=1;
ammotype:=GetCartridgeType(c);
for i:=1 to max_in_mag do begin
c:=GetCartridgeFromMagVector(wpn, i);
if ammotype<>GetCartridgeType(c) then begin
WriteToPacket(packet, @cnt, sizeof(cnt));
WriteToPacket(packet, @ammotype, sizeof(ammotype));
ammotype:=GetCartridgeType(c);
cnt:=1;
end else begin
cnt:=cnt+1;
end;
end;
WriteToPacket(packet, @cnt, sizeof(cnt));
WriteToPacket(packet, @ammotype, sizeof(ammotype));
end;
tmp_byte:=buf.GetLastShotAmmoType();
WriteToPacket(packet, @tmp_byte, sizeof(tmp_byte));
end;
procedure CWeapon__load_Patch(); stdcall;
asm
sub esi, $741
push edi
push esi
call CWeapon__load
pop edi
pop esi
pop ebp
pop ebx
ret 4
end;
procedure CWeapon__save_Patch(); stdcall;
asm
sub esi, $741
push edi
push esi
call CWeapon__save
pop edi
pop esi
ret 4
end;
//-----------------------------------------------------------------------------------------------------------
procedure CWeapon_Load_Data_In_NetSpawn_Patch(); stdcall;
asm
//çäåñü èíèöèàëèçàöèÿ îáúåêòà åùå òîëêîì íå ïðîèçâåäåíà - ïèøåì ÷òî õîòèì
//îáíóëèì ìåñòî, êóäà áóäåò çàïèñûâàòüñÿ óêàçàòåëü íà WpnBuf
mov word ptr [ecx+$6a2], 0
mov word ptr [ecx+$6be], 0
// Îðèãèíàëüíîå
fstp dword ptr [esi+$4C0]
ret
end;
procedure CWeapon_NetDestroy_SaveData(wpn:pointer; so:pointer); stdcall;
var
wpn_gl:pointer;
gl_status:byte;
gl_ammocnt:byte;
gl_ammotype:byte;
begin
wpn_gl:=dynamic_cast(wpn, 0, RTTI_CWeapon, RTTI_CWeaponMagazinedWGrenade, false);
if wpn_gl<>nil then begin
//[bug] áàã - ïðè àêòèâíîì ïîäñòâîëüíèêå ÷èñëî ãðåí â íåì îòïðàâëÿåòñÿ â îñíîâíîé ìàãàçèí ïàòðîíîâ
if IsGrenadeMode(wpn_gl) then begin
//log('Switching GL on '+inttohex(cardinal(so), 8));
PerformSwitchGL(wpn_gl);
end;
//[bug] áàã - îòñóòñòâóåò âûñòàâëåíèå a_elapsed_grenades â ñåðâåðíîì îáúåêòå ïîñëå óäàëåíèÿ, èç-çà ÷åãî ãðåíû ïðîãðóæàþòñÿ íåêîððåêòíî. Ïî-õîðîøåìó, íàäî ïðàâèòü íå òàê òîïîðíî, à ìîäèôèöèðîâàíèåì ìåòîäîâ ýêñïîðòà è èìïîðòà íåòïàêåòîâ
gl_ammocnt:=GetAmmoInGLCount(wpn_gl);
if gl_ammocnt>0 then begin
gl_ammotype:=GetCartridgeType(GetGrenadeCartridgeFromGLVector(wpn_gl, gl_ammocnt-1));
end else begin
gl_ammotype:=GetAmmoTypeIndex(wpn_gl, not IsGrenadeMode(wpn_gl))
end;
gl_status:= (gl_ammotype shl 6) + (gl_ammocnt and $3F);
//log('gl_status ='+inttohex(gl_status, 2));
pbyte(cardinal(so)+$1A8)^:=gl_status;
end;
end;
procedure CWeapon_NetDestroy_SaveData_Patch(); stdcall;
asm
pushad
pushfd
//Ïîëó÷èì ñåðâåðíûé îáúåêò
push ecx
movzx eax, word ptr [ecx+$18c]
push eax
call get_server_object_by_id
pop ecx
test eax, eax
je @finish
pushad
push eax
push ecx
call CWeapon_NetDestroy_SaveData
popad
@finish:
popfd
popad
mov eax, xrgame_addr;
add eax, $2C4770
call eax //parent net_Destroy
ret
end;
procedure CWeapon__net_Export_ammocnt_Patch(); stdcall;
asm
push ecx
lea ecx, [esp]
pushad
push ecx
push esi
call GetAmmoInMagCount
pop ecx
mov [ecx], eax
popad
pop ecx
end;
procedure CWeapon__net_Export_ammotype_Patch(); stdcall;
asm
pushad
push esi
call IsGrenadeMode
cmp al, 0
popad
je @not_gl
movzx eax, byte ptr [esi+$7E4]
jmp @finish
@not_gl:
movzx eax, byte ptr [esi+$6C4]
@finish:
end;
procedure PackScopeType(wpn:pointer; data:pbyte); stdcall;
var
scopetype:byte;
begin
scopetype:=0;
if IsScopeAttached(wpn) then begin
scopetype:=GetCurrentScopeIndex(wpn);
end;
//Â 5 ñòàðøèõ áèòàõ áàéòà ñåðâåðíîãî îáúåêòà îðóæèÿ ìû çàïèøåì èíäåêñ ïðèöåëà
scopetype:=scopetype shl 3;
data^ := (data^ and $7) + scopetype
end;
procedure CWeapon__net_Export_scopetype_Patch(); stdcall;
asm
movzx edx,byte ptr [esi+$460] // original, m_flagsAddOnState
push edx // buffer
mov ecx, esp //ptr
pushad
push ecx // ptr to buf
push esi //wpn
call PackScopeType
popad
pop edx // get updated
end;
procedure UnpackScopeType(wpn:pointer; data:pbyte); stdcall;
var
scopetype:byte;
begin
scopetype:=data^ shr 3;
Log('UnpackScopeType, weapon '+inttohex(cardinal(wpn), 8)+', ID='+inttostr(GetID(wpn))+', scope '+inttostr(scopetype));
data^:= data^ and $F;
SetCurrentScopeType(wpn, scopetype);
end;
procedure CWeapon__net_Import_scopetype_Patch(); stdcall;
asm
push eax //save buffer ptr
push eax // original
mov ecx, edi // original
call ebp // original; P.r_u8 (wstate)
pop eax //restore ptr to buffer
pushad
push eax
push esi
call UnpackScopeType
popad
end;
procedure CWeaponMagazinedWGrenade__save_GLAmmo(wpn:pointer; dest:pCardinal); stdcall;
begin
dest^:=GetAmmoInGLCount(wpn);
end;
procedure CWeaponMagazinedWGrenade__save_GLAmmo_Patch(); stdcall;
asm
lea edx, [esp+$14]
pushad
push edx
push esi
call CWeaponMagazinedWGrenade__save_GLAmmo
popad
end;
procedure CWeaponMagazinedWGrenade__load_saveglstatus(wpn:pointer; status:boolean); stdcall;
var
buf:WpnBuf;
begin
buf:=GetBuffer(wpn);
if buf<>nil then begin
buf.loaded_gl_state:=status;
end;
end;
procedure CWeaponMagazinedWGrenade__load_saveglstatus_Patch(); stdcall;
asm
pushad
push ecx
push esi
call CWeaponMagazinedWGrenade__load_saveglstatus
popad
cmp cl, [esi+$7F8] //original
end;
procedure CWeaponMagazinedWGrenade__net_Spawn_applyglstatus(wpn:pointer); stdcall;
var
buf:WpnBuf;
gl_status:cardinal;
begin
buf:=GetBuffer(wpn);
if buf.loaded_gl_state<>IsGLEnabled(wpn) then begin
gl_status:=GetGLStatus(wpn);
if (gl_status=1) or ((gl_status=2) and IsGLAttached(wpn)) then begin
PerformSwitchGL(wpn);
end;
end;
end;
procedure CWeaponMagazinedWGrenade__net_Spawn_applyglstatus_Patch(); stdcall;
asm
pushad
push esi
call CWeaponMagazinedWGrenade__net_Spawn_applyglstatus
popad
pop eax //ret addr
pop edi
pop esi
pop ebp
push eax //ret addr
mov eax, ebx
end;
procedure CWeaponMagazinedWGrenade__net_Spawn_restorewpnptr_Patch(); stdcall;
asm
sub esi, $E8
end;
//-----------------------------------------------------------------------------------------------------------
function Init:boolean;
var jmp_addr:cardinal;
begin
result:=false;
jmp_addr:=xrGame_addr+$2C1200;
if not WriteJump(jmp_addr, cardinal(@CWeapon_Load_Data_In_NetSpawn_Patch),6, true) then exit;
jmp_addr:=xrGame_addr+$2BEFE4;
if not WriteJump(jmp_addr, cardinal(@CWeapon_NetDestroy_SaveData_Patch), 5, true) then exit;
// Âðåçêà â CWeapon::load äëÿ çàãðóçêè äàííûõ ìîäà
jmp_addr:=xrGame_addr+$2BF195;
if not WriteJump(jmp_addr, cardinal(@CWeapon__load_Patch), 7, false) then exit;
jmp_addr:=xrGame_addr+$2BF104;
if not WriteJump(jmp_addr, cardinal(@CWeapon__save_Patch), 5, false) then exit;
//[bug] áàã - â CWeapon::net_Export ñîõðàíÿåòñÿ íå ÷èñëî ïàòðîíîâ â îñíîâíîì ìàãàçèíå, à ÷èñëî â àêòèâíîì
jmp_addr:=xrGame_addr+$2BC03F;
if not WriteJump(jmp_addr, cardinal(@CWeapon__net_Export_ammocnt_Patch), 7, true) then exit;
//àíàëîãè÷íî ñ òèïîì
jmp_addr:=xrGame_addr+$2BC05F;
if not WriteJump(jmp_addr, cardinal(@CWeapon__net_Export_ammotype_Patch), 7, true) then exit;
//[bug] áàã - òèï óñòàíîâëåííîãî ïðèöåëà íèêàê íå ñîîáùàåòñÿ â ñåðâåðíûé îáúåêò
//èç-çà ýòîãî ïðè óõîäå îáúåêòà â îôôëàéí óñòàíîâëåííûé ïðèöåë âñåãäà ñáðàñûâàåòñÿ íà äåôîëòîâûé
//Äëÿ èñïðàâëåíèå - â CWeapon::net_Export ïàêóåì òèï ïðèöåëà â îäèí áàéò ñ òåêóùèì ñòåéòîì, à â CWeapon::net_Import èçâëåêàåì îòòóäà åãî
//Òàêæå âîññòàíàâëèâàåì àääîíû â CWeapon::net_Spawn - îñíîâíîé ñïîñîá ïîëó÷åíèÿ èõ äëÿ ñèíãëà
jmp_addr:=xrGame_addr+$2BC04F;
if not WriteJump(jmp_addr, cardinal(@CWeapon__net_Export_scopetype_Patch), 7, true) then exit;
jmp_addr:=xrGame_addr+$2c1588;
if not WriteJump(jmp_addr, cardinal(@CWeapon__net_Import_scopetype_Patch), 5, true) then exit;
//ïðàâèì ñîõðàíåíèå ÷èñëà ïîäñòâîëüíûõ ãðåí
jmp_addr:=xrGame_addr+$2D2850;
if not WriteJump(jmp_addr, cardinal(@CWeaponMagazinedWGrenade__save_GLAmmo_Patch), 8, true) then exit;
if not nop_code(xrgame_addr+$2D3C3D, 1, CHR($EB)) then exit;
if not nop_code(xrgame_addr+$2D3CB2, 1, CHR($EB)) then exit;
//çàïîìíèì çàãðóæåííîå èç ñåéâà ñîñòîÿíèå ïîäñòâîëà
jmp_addr:=xrGame_addr+$2D3C37;
if not WriteJump(jmp_addr, cardinal(@CWeaponMagazinedWGrenade__load_saveglstatus_Patch), 6, true) then exit;
//è çàãðóçèì åãî â ñàìîì êîíöå net_Spawn'îâ
jmp_addr:=xrGame_addr+$2D35E1;
if not WriteJump(jmp_addr, cardinal(@CWeaponMagazinedWGrenade__net_Spawn_applyglstatus_Patch), 5, true) then exit;
jmp_addr:=xrGame_addr+$2D35B9;
if not WriteJump(jmp_addr, cardinal(@CWeaponMagazinedWGrenade__net_Spawn_restorewpnptr_Patch), 13, true) then exit;
result:=true;
end;
end.