-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.pas
633 lines (572 loc) · 16.5 KB
/
Main.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
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
unit Main;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Config;
type
TPatchedJmp = record
Addr: DWORD;
JmpTo: DWORD;
OrigMemory: array of Byte;
end;
TPatchedMem = record
Addr: DWORD;
OrigMemory: array of Byte;
NewMemory: array of Byte;
end;
TAPMThread = class(TThread)
private
FShutdown: Boolean;
public
destructor Destroy; override;
procedure Execute; override;
procedure Init;
procedure HandleHotkeys(var Msg: TMsg);
end;
procedure Patch;
procedure Unpatch;
const
APMInterval: Real = 0.95; // time after which actions are worth 1/e (in minutes)
var
Patched, AlertMinPassed: Boolean;
AlertMinPTime: Cardinal;
APMCounter: array[0..11] of Real;
APMStr: array[0..11] of String;
APMStrLens: array[0..11] of Integer;
LastSoundAlert, LastAPMUpdate: Cardinal;
GameStartTime: Cardinal;
ConfigVars: TConfRecord;
PatchedArr: array of TPatchedJmp;
PatchedMemArr: array of TPatchedMem;
implementation
uses Math, Token, uBWAddresses, uSpeedChanges, MMSystem, BWUtil;
procedure AddPatch(dAddr,dJmpTo: DWORD; NumNops: Integer = 0);
var
I: Integer;
begin
SetLength(PatchedArr,Length(PatchedArr)+1);
with PatchedArr[Length(PatchedArr)-1] do
begin
Addr := dAddr;
JmpTo := dJmpTo;
SetLength(OrigMemory,5+NumNops);
//WriteMem(Cardinal(@OrigMemory),dAddr,Length(OrigMemory));
//CopyMemory(@OrigMemory, Pointer(dAddr), Length(OrigMemory));
//ShowMessage(IntToStr(Length(OrigMemory)));
for I := 0 to Length(OrigMemory) - 1 do
begin
OrigMemory[I] := Byte((Pointer(dAddr+Cardinal(I)))^)
//ShowMessage(IntToHex(OrigMemory[I],2));
end;
end;
end;
procedure AddPatchedMem(dAddr: DWORD; NewMem: array of Byte);
var
I: Integer;
begin
SetLength(PatchedMemArr,Length(PatchedMemArr)+1);
with PatchedMemArr[Length(PatchedMemArr)-1] do
begin
Addr := dAddr;
SetLength(NewMemory, Length(NewMem));
for I := 0 to Length(NewMem) - 1 do
NewMemory[I] := NewMem[I];
SetLength(OrigMemory,Length(NewMemory));
for I := 0 to Length(OrigMemory) - 1 do
begin
OrigMemory[I] := Byte((Pointer(dAddr+Cardinal(I)))^);
end;
end;
end;
procedure TAPMThread.HandleHotkeys(var Msg: TMsg);
begin
// wParam = hotkey ID
if Msg.wParam = 0 then
begin
// VK_F8
if Patched then
Unpatch
else
Patch;
end;
end;
function CalcATime: Cardinal;
var
CurTick: DWORD;
begin
PeekMem(Offsets.GameTime,@CurTick,SizeOf(CurTick));
Result := CurTick*42;
end;
procedure AlertAPM; stdcall;
begin
if (GetTickCount - LastSoundAlert > 2000)
and (not IsPaused) and (not IsObsMode)//1.16
and (AlertMinPassed and (CalcATime - AlertMinPTime > 20000)) then
begin
if ConfigVars.PlaySound then
PlaySound(PChar(ConfigVars.SoundFile), 0, SND_ASYNC or SND_NODEFAULT or SND_NOSTOP);
LastSoundAlert := GetTickCount;
end;
end;
procedure CalcAPM; stdcall;
var
CurTick, TimeDiff: Cardinal;
GameDurationFactor: Real;
APM: Integer;
I: Integer;
begin
if CalcATime > LastAPMUpdate then
begin
// do exponential decay, thanks MasterOfChaos :)
CurTick := CalcATime; //GetTickCount;
TimeDiff := CurTick - LastAPMUpdate;
for I := 0 to 11 do
APMCounter[I] := APMCounter[I]*exp(-Timediff/(APMInterval*60000));
LastAPMUpdate := CurTick;
end;
CurTick := CalcATime;
GameDurationFactor := 1-exp(-(CurTick - GameStartTime)/(APMInterval*60000));
if GameDurationFactor < 0.01 then GameDurationFactor := 0.01;//Prevent div by 0
for I := 0 to Length(APMCounter) - 1 do
begin
if GetPlayerName(I) <> '' then
begin
if (I <> GetPNum) or (IsObsMode) then
APMStr[I] := GetColoredPlayerName(I) + ': '
else if (I = GetPNum) and (not IsObsMode) then
APMStr[I] := #4'APM: ';
APM := Trunc(APMCounter[I]/(APMInterval*GameDurationFactor));
if (not AlertMinPassed) and
((APM > ConfigVars.MinAPM * 1.1) or (CurTick >= 90000)) then
begin
AlertMinPassed := True;
AlertMinPTime := CurTick;
end;
if APM >= ConfigVars.MinAPM then
APMStr[I] := APMStr[I] + #7 + IntToStr(APM)
else
begin
APMStr[I] := APMStr[I] + #6 + IntToStr(APM);
if (I = GetPNum) and (not IsObsMode) then
AlertAPM;
end;
end
else
APMStr[I] := '';
APMStrLens[I] := BWGetTextWidth(PChar(APMStr[I]));
end;
end;
function GetElapsedTimeStr: string;
var
Milliseconds: Cardinal;
Seconds, Minutes, Hours: Integer;
begin
// time format: [hh:]mm:ss
Milliseconds := CalcATime;
Seconds := Integer(Milliseconds div 1000);
Minutes := Seconds div 60;
Seconds := Seconds - (Minutes*60);
Hours := Minutes div 60;
Minutes := Minutes - (Hours*60);
if Hours > 0 then // only display hours if their greater than 0
Result := Format('%.2d:%.2d:%.2d',[Hours,Minutes,Seconds])
else
Result := Format('%.2d:%.2d',[Minutes,Seconds]);
end;
function GetLocalTimeStr: string;
var
CurTime: TDateTime;
begin
CurTime := Time;
Result := FormatDateTime('hh:nn ampm',CurTime);
end;
function GetOnScreenTextPos(PosX, TextWidth: Integer): Integer;
begin
if PosX + TextWidth < 640 then
Result := PosX
else
begin
Result := Floor((639 - TextWidth) / 10) * 10; // prevent jittering
end;
end;
procedure DrawIt;
var
I,LineNo: Integer;
MaxAPMLen, OnScreenPos: Integer;
GameClock, LocalClock: String;
PreTextFormat: Cardinal;
begin
PreTextFormat := GetCurTextFormat;
BWFormatTextR(Offsets.bwtf_Normal);
CalcAPM;
if ConfigVars.EnableLiveAPM then
begin
if (ConfigVars.DispAllAPMs) and (IsObsMode) then
begin
MaxAPMLen := 0;
for I := 0 to Length(APMStrLens) - 1 do
begin
if MaxAPMLen < APMStrLens[I] then
MaxAPMLen := APMStrLens[I];
end;
OnScreenPos := GetOnScreenTextPos(ConfigVars.PosX,MaxAPMLen);
LineNo := 0;
for I := 0 to Length(APMStr) - 1 do
begin
if APMStr[I] <> '' then
begin
BWDrawText(OnScreenPos,ConfigVars.PosY+(LineNo*11),PChar(APMStr[I]));
LineNo := LineNo + 1;
end;
end;
end
else if GetPNum <> -1 then
begin
OnScreenPos := GetOnScreenTextPos(ConfigVars.PosX,APMStrLens[GetPNum]);
BWDrawText(OnScreenPos, ConfigVars.PosY,PChar(APMStr[GetPNum]));
end;
end;
BWFormatTextR(Offsets.bwtf_Large);
if ConfigVars.EnableGameClock then
begin
GameClock := #4+GetElapsedTimeStr+#1;
OnScreenPos := GetOnScreenTextPos(ConfigVars.GameClockX,
BWGetTextWidth(PChar(GameClock)));
BWDrawText(OnScreenPos,ConfigVars.GameClockY,PChar(GameClock));
end;
if ConfigVars.EnableLocalClock then
begin
LocalClock := #4+GetLocalTimeStr+#1;
OnScreenPos := GetOnScreenTextPos(ConfigVars.LocalClockX,
BWGetTextWidth(PChar(LocalClock)));
BWDrawText(OnScreenPos,ConfigVars.LocalClockY,PChar(LocalClock));
end;
BWFormatText(Offsets.bwtf_Reset);
BWRestoreTextFormat(PreTextFormat);
end;
procedure MyDrawFxn; stdcall;
{Code in 1.16:
004BD504 |. E8 47F9FCFF CALL StarCraf.0048CE50 ; overwrite
004BD509 |. E8 D22FFBFF CALL StarCraf.004704E0 ; jump back
}
begin
asm
call dword ptr [Offsets.HOOK_DrawRepFunc] // overwritten command
pushad
end;
DrawIt;
asm
popad
jmp [Offsets.HOOK_DrawJmpBack]
end;
end;
function HandleCommand(CmdStr: PChar): Boolean; stdcall;
//TODO: Fix this crazy mess of a function please
var
Cmd, Args: PChar;
CmdString, ArgsString: string;
begin
Result := False;
if Pos('/',CmdStr) = 1 then
begin
// it at least looks like a command, now we can process it and see if we can handle it
CmdString := GetToken(CmdStr, ' ', 1);
CmdString := GetToken(CmdString,'/',2); // Strip off the slash
ArgsString := '';
if NumToken(CmdStr, ' ') >= 1 then
ArgsString := Copy(CmdStr, Length(CmdString) + 2, Length(CmdStr) - Length(CmdString) + 2);
CmdString := UpperCase(CmdString);
Cmd := AllocMem(Length(CmdString) + 1);
StrPCopy(Cmd, CmdString);
Args := AllocMem(Length(ArgsString) + 1);
StrPCopy(Args,ArgsString);
// check for specific commands now
Result := True;
try
if Cmd = 'ABOUTAPM' then
BWTextOut(#7'APMAlert v1.0'#4' by '#6'tec27')
else if (Cmd = 'MINAPM') or (Cmd = 'SETAPM') then
begin
ConfigVars.MinAPM := StrToInt(Trim(Args));
SaveAPMConf(ConfigVars);
BWTextOut(PChar('Got new minimum APM: '#7+FloatToStr(ConfigVars.MinAPM)));
end
else if (Cmd = 'TOGGLESOUND') or (Cmd = 'TGLSND') then
begin
ConfigVars.PlaySound := not ConfigVars.PlaySound;
SaveAPMConf(ConfigVars);
if ConfigVars.PlaySound then
BWTextOut('[Sound Alerts] Now '#7+'On')
else
BWTextOut('[Sound Alerts] Now '#6+'Off');
end
else if Cmd = 'SHOWAPM' then
BWTextOut(PChar('Minimum APM: '#7+FloatToStr(ConfigVars.MinAPM)))
else if Cmd = 'MOVEAPM' then
begin
ConfigVars.PosX := Integer(Pointer(Offsets.CursorX)^);
ConfigVars.PosY := Integer(Pointer(Offsets.CursorY)^);
SaveAPMConf(ConfigVars);
BWTextOut(PChar('Got new box position: '#7 + IntToStr(ConfigVars.PosX) + ',' + IntToStr(ConfigVars.PosY)));
end
else if (Cmd = 'TOGGLEAPM') or (Cmd = 'TGLAPM') then
begin
ConfigVars.EnableLiveAPM := not ConfigVars.EnableLiveAPM;
SaveAPMConf(ConfigVars);
if ConfigVars.EnableLiveAPM then
BWTextOut('[LiveAPM] Now '#7+'On')
else
BWTextOut('[LiveAPM] Now '#6+'Off');
end
else if Cmd = 'MOVELOCAL' then
begin
ConfigVars.LocalClockX := Integer(Pointer(Offsets.CursorX)^);
ConfigVars.LocalClockY := Integer(Pointer(Offsets.CursorY)^);
SaveAPMConf(ConfigVars);
BWTextOut(PChar('Got new box position: '#7 +
IntToStr(ConfigVars.LocalClockX) + ','
+ IntToStr(ConfigVars.LocalClockY)));
end
else if Cmd = 'MOVEGAME' then
begin
ConfigVars.GameClockX := Integer(Pointer(Offsets.CursorX)^);
ConfigVars.GameClockY := Integer(Pointer(Offsets.CursorY)^);
SaveAPMConf(ConfigVars);
BWTextOut(PChar('Got new box position: '#7 +
IntToStr(ConfigVars.GameClockX) + ','
+ IntToStr(ConfigVars.GameClockY)));
end
else if (Cmd = 'TOGGLELOCAL') or (Cmd = 'TGLLOCAL') then
begin
ConfigVars.EnableLocalClock := not ConfigVars.EnableLocalClock;
SaveAPMConf(ConfigVars);
if ConfigVars.EnableLocalClock then
BWTextOut('[Local Clock] Now '#7+'On')
else
BWTextOut('[Local Clock] Now '#6+'Off');
end
else if (Cmd = 'TOGGLEGAME') or (Cmd = 'TGLGAME') then
begin
ConfigVars.EnableGameClock := not ConfigVars.EnableGameClock;
SaveAPMConf(ConfigVars);
if ConfigVars.EnableGameClock then
BWTextOut('[Game Clock] Now '#7+'On')
else
BWTextOut('[Game Clock] Now '#6+'Off');
end
else if (Cmd = 'TOGGLEDISPALL') or (Cmd = 'TGLDISPALL') then
begin
ConfigVars.DispAllAPMs := not ConfigVars.DispAllAPMs;
SaveAPMConf(ConfigVars);
if ConfigVars.DispAllAPMs then
BWTextOut('[Display All APMs] Now '#7+'On')
else
BWTextOut('[Display All APMs] Now '#6+'Off');
end
else
Result := False; // If nothing was hit, then we never handled it
except
on E: Exception do
begin
BWTextOut(PChar('Error in command: ' + E.ClassName + ': ' + E.Message));
Result := True; // even after an error, it was still 'handled'
end;
end;
FreeMem(Cmd);
FreeMem(Args);
end;
end;
procedure CommandProc; stdcall;
// 1.16 Code:
{
004F31BE |. 52 PUSH EDX ; /Arg1
004F31BF |. 8D5D 0C LEA EBX,DWORD PTR SS:[EBP+C] ; |
004F31C2 |. 894D 0C MOV DWORD PTR SS:[EBP+C],ECX ; |
004F31C5 |. E8 06F1FBFF CALL StarCraf.004B22D0 ; \StarCraf.004B22D0
}
var
EnteredStr: PChar;
Processed: Boolean;
begin
asm
pushad
mov EnteredStr, edx
end;
// ok, do what you want with the string, just don't modify it if you want SC to have access to the original string
Processed := HandleCommand(EnteredStr);
// after we're done:
if not Processed then
begin
asm
popad
pop ecx
pop ebp // above two fix for delphi stuff
call dword ptr [Offsets. HOOK_TextCommandsRepFunc] // the call that makes SC process its messages
jmp [Offsets.HOOK_TextCommandsJmpBack]
end;
exit; // Should never reach here
end;
// else
asm
popad
pop ecx
pop ebp // above two fix for delphi stuff
jmp [Offsets.HOOK_TextCommandsJmpBack]
end;
end;
procedure GotAction(ActionCode: Byte; PlayerNum: Integer);
begin
{BWTextOut(PChar('Code: 0x' + IntToHex(ActionCode,2) + ' - Player: 0x'
+ IntToHex(PlayerNum,8)),9);}
if (ActionCode <> $37) and (PlayerNum < 12) then
begin
// anything that triggers this is not a keepalive action
APMCounter[PlayerNum] := APMCounter[PlayerNum] + 1;
//BWTextOut('Action.',PNum);
{BWTextOut(PChar('Code: 0x' + IntToHex(ActionCode,2) + ' - Player: '
+ IntToStr(PlayerNum)),9);}
end;
end;
// This hook will be called each time an action is executed in SC
procedure SendActionHook; stdcall;
var
ActionCode: Byte;
ACPtr: Pointer;
PNum: DWORD;
begin
// 1.16 code:
{
00486C78 |. 52 |PUSH EDX ; /Arg1 => 00000008
00486C79 |. 8BDE |MOV EBX,ESI ; |
00486C7B |. E8 D0700400 |CALL StarCraf.004CDD50 ; \StarCraf.004CDD50
}
asm
pushad
mov EDX,[Offsets.SendActionPNum]
mov EDX,[EDX]
mov PNum,EDX
mov ACPtr,ESI
end;
ActionCode := Byte(ACPtr^);
GotAction(ActionCode,PNum);
asm
popad
add ESP,8
pop EBP
call [Offsets.HOOK_SendActionRepFunc] // what we replaced
jmp [Offsets.HOOK_SendActionJmpBack]
end;
end;
procedure GameInit; stdcall;
var
I: Integer;
begin
GameStartTime := CalcATime;
LastAPMUpdate := GameStartTime;
LastSoundAlert := 0; // Sound alerts are still based off of actual timing, not game timing
AlertMinPassed := False;
AlertMinPTime := 0;
for I := 0 to 11 do
begin
APMCounter[I] := 0;
APMStr[I] := 'Starting...';
end;
end;
procedure TAPMThread.Execute;
begin
while not Terminated do
begin
if (IsInGame) and (not Patched) then
Patch
else if (not IsInGame) and (Patched) then
Unpatch;
Sleep(75);
end;
end;
procedure TAPMThread.Init;
begin
LoadAPMConf(ConfigVars);
SetLength(PatchedArr,0);
SetLength(PatchedMemArr,0);
Patched := False;
LastSoundAlert := 0;
LastAPMUpdate := 0;
AlertMinPassed := False;
AlertMinPTime := 0;
// All our possible patches
AddPatch(Offsets.HOOK_Draw,Cardinal(@MyDrawFxn));
AddPatch(Offsets.HOOK_TextCommands,Cardinal(@CommandProc));
AddPatch(Offsets.HOOK_SendAction,Cardinal(@SendActionHook));
// Text Refreshing patches, thanks Perma
AddPatchedMem(Offsets.PTCH_TextRefresh1,[$90,$90]);
AddPatchedMem(Offsets.PTCH_TextRefresh2,[$EB,$04]);
Resume;
end;
destructor TAPMThread.Destroy;
begin
FShutdown := True;
try
if not Terminated then
Self.Terminate;
finally
inherited;
end;
end;
procedure Patch;
const
Nop: Byte = $90;
var
I,J: Integer;
begin
for I := 0 to Length(PatchedArr) - 1 do
begin
with PatchedArr[I] do
begin
JmpPatch(Addr,JmpTo);
if Length(OrigMemory) > 5 then
begin
for J := 5 to Length(OrigMemory) - 1 do
WriteMem(Addr+Cardinal(J),Cardinal(@Nop),1);
end;
end;
end;
for I := 0 to Length(PatchedMemArr) - 1 do
begin
with PatchedMemArr[I] do
begin
for J := 0 to Length(NewMemory) - 1 do
begin
WriteMem(Addr+Cardinal(J),Cardinal(@NewMemory[J]),1);
end;
end;
end;
Patched := True;
if IsInGame then
GameInit; // NOT GameStart, lol (although restarting games midway through was sorta fun :P )
end;
procedure Unpatch;
var
I, J: Integer;
begin
for I := 0 to Length(PatchedArr) - 1 do
begin
with PatchedArr[I] do
begin
for J := 0 to Length(OrigMemory) - 1 do
begin
WriteMem(Addr+Cardinal(J),Cardinal(@OrigMemory[J]),1);
end;
end;
end;
for I := 0 to Length(PatchedMemArr) - 1 do
begin
with PatchedMemArr[I] do
begin
for J := 0 to Length(OrigMemory) - 1 do
begin
WriteMem(Addr+Cardinal(J),Cardinal(@OrigMemory[J]),1);
end;
end;
end;
Patched := False;
end;
end.