/
smart.simba
833 lines (617 loc) · 19 KB
/
smart.simba
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
(*
Smart
=====
The Smart file stores all the functions and procedures used to initiate SMART
in scripts. To use SMART in a script, simply add the following code before SRL is
included.
.. code-block:: pascal
{$DEFINE SMART}
The source for this file can be found `here <https://github.com/SRL/SRL-6/blob/master/lib/misc/smart.simba>`_.
*)
{$loadlib libsmartremote}
{$f-}
(*
var SMART plugins
~~~~~~~~~~~~~~~~~
A TStringArray of smart plugins
*)
var
smartPlugins: TStringArray = ['OpenGL32.dll', 'd3d9.dll'];
(*
var User Set
~~~~~~~~~~~~
Variables that can be used in scripts.
* smartGetJavaPath - If enabled will attempt to find your java path
* smartShowConole - If enabled will show the java console else wont.
* smartForceNewClient - If enabled will skip attempting to pair to a previously spawned client
* smartEnableDrawing - If enabled will allow drawing on smart.
* smartInitSeq - Key that smart will press while smart is loading.
* smartUserAgent - The userAgent header (i.e, the 'browser' information sent to the RS server)
* smartResetAttempts - The max attempts to respawn SMART during sixHourFix()
*)
var
smartGetJavaPath: boolean := true;
smartShowConsole: boolean := true;
smartForceNewClient: boolean := false;
smartEnableDrawing: boolean := false;
smartInitSeq: string := '';
smartUserAgent: string := '';
smartResetAttempts: integer = 5;
{*
var Internal
~~~~~~~~~~~~
Internal SMART variables used only in smart.simba.
** Target <> PID. PID for identifying smarts. Target for targetting a specific applet and cleaning up.
* __smartCurrentPid: Keeps track of the current smart PID.
* __smartCurrentTarget: Keeps track of the current smart target.
* __smartIsDrawingSetup: Keeps track if smart drawing is setup or not.
* __smartNativeDebugButtonID: The id of the debug button (if not using a native button) set as -1.
* __smartClientWidth, __smartClientHeight - The dimensions of the smart client
* smartImage: TMufasaBitmap that is displayed onto smart.
*}
{$IFNDEF CODEINSIGHT}
__smartCurrentPid: integer := 0;
__smartCurrentTarget: integer := 0;
__smartIsDrawingSetup: boolean := False;
__smartNativeDebugButtonID: integer := 101;
__smartClientWidth: integer := 800;
__smartClientHeight: integer := 600;
{$ENDIF}
smartImage: TMufasaBitmap;
function _LocateJavaPath(var Res: String; x86: Boolean; Dir: char = 'A'; Recursive: Boolean = False): Boolean;
function GetDirectoryName(Directory, Path: String): String;
var
I: Integer;
Directories: TStringArray;
begin
Directories := GetDirectories(Path);
for I := 0 To High(Directories) Do
if ExecRegExpr(Directory, Directories[I]) then
begin
Result := Directories[I];
Exit;
end;
end;
function GetDirectoryNames(Directory, Path: String): TStringArray;
var
I: Integer;
Directories: TStringArray;
begin
Directories := GetDirectories(Path);
for I := 0 To High(Directories) Do
if ExecRegExpr(Directory, Directories[I]) then
Result := Result + Directories[I];
end;
function FindDirectory(Directory, Path: String): Boolean;
begin
Result := Length(GetDirectoryName(Directory, Path)) > 0;
end;
var
Drive: String;
CurrentPath, c: String;
Drives: TStringArray;
i: Integer;
begin
Result := False;
Res := '';
if (Recursive) then
begin
Drive := Dir + ':/';
end else
Drive := Copy(PluginPath, 0, 3);
if (x86) then
CurrentPath := Drive + 'Program Files (x86)\'
else
CurrentPath := Drive + 'Program Files\';
if (FindDirectory('Java|java', CurrentPath + '\')) then
begin
CurrentPath := CurrentPath + 'Java\';
end else
if (Dir <> 'Z') then
Result := _LocateJavaPath(Res, x86, chr(ord(Dir) + 1), True);
if (Result) then
Exit();
Drives := GetDirectoryNames('jre', CurrentPath);
if (Length(Drives) > 0) then
begin
c := CurrentPath;
for i := 0 to High(Drives) do
begin
CurrentPath := c;
if (smartShowConsole) then
CurrentPath := CurrentPath + Drives[i] + '\bin\java.exe'
else
CurrentPath := CurrentPath + Drives[i] + '\bin\javaw.exe';
Result := FileExists(CurrentPath);
if (Result) then
Break;
end;
end else begin
Drives := GetDirectoryNames('jdk', CurrentPath);
c := CurrentPath;
for i := 0 to High(Drives) do
begin
CurrentPath := c;
if (smartShowConsole) then
CurrentPath := CurrentPath + Drives[i] + '\bin\java.exe'
else
CurrentPath := CurrentPath + Drives[i] + '\bin\javaw.exe';
Result := FileExists(CurrentPath);
if (Result) then
Break;
end;
end;
Result := FileExists(CurrentPath);
Res := CurrentPath;
end;
function FindJavaPath(out Path: String): Boolean;
begin
if (not _LocateJavaPath(Path, True)) then
if (not _LocateJavaPath(Path, False)) then
Exit(False);
Result := True;
end;
(*
getJavaPath
~~~~~~~~~~~
.. code-block:: pascal
function getJavaPath(): string;
Will return the Java path needed for the first parameter of SmartSpawnClient.
.. note::
- By: Olly
- Last Updated: 26 August 2013 by Olly
Example:
.. code-block:: pascal
s := getJavaPath();
*)
function getJavaPath(): string;
begin
print('getJavaPath():', TDebug.HEADER);
{$IFDEF WINDOWS}
if (not smartGetJavaPath) then
begin
if (smartShowConsole) then
result := 'java.exe'
else
result := 'javaw.exe';
print('Not searching for your Java path. Make sure the JRE is in your system''s PATH before JDK (if you have JDK).', TDebug.WARNING);
print('getJavaPath()', TDebug.FOOTER);
exit();
end;
print('Attempting to search for your Java path');
if (findJavaPath(result)) then
print('Found your java path @ ' + result)
else begin
print('Failed to find your Java path', TDebug.WARNING);
print('If RS is failing to switch to OpenGL mode, add JRE to your systems path before JDK (if you have JDK)', TDebug.HINT);
end;
{$ELSE}
print('Not searching for your Java path. Make sure the JRE is in your system''s PATH before JDK (if you have JDK).', TDebug.WARNING);
Result := 'java';
if (not (smartShowConsole)) then
Result := Result + 'w';
{$ENDIF}
print('getJavaPath()', TDebug.FOOTER);
end;
(*
smartSetDebug; overload
~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: pascal
procedure smartSetDebug(enable: boolean); overload;
Overloaded function that toggles the debug image on smart without needing the
target parameter.
.. note::
- By: Olly
- Last Updated: 26 August 2013 by Olly
Example:
.. code-block:: pascal
smartSetDebug(true);
*)
procedure smartSetDebug(enable: boolean); overload;
begin
if (__smartNativeDebugButtonID <> -1) then
begin
smartSetNativeButton(__smartCurrentTarget, 0, __smartNativeDebugButtonID, not enable);
smartSetNativeButton(__smartCurrentTarget, 1, __smartNativeDebugButtonID, not enable);
end else
smartSetDebug(__smartCurrentTarget, enable);
end;
(*
smartGetParameters
~~~~~~~~~~~~~~~~~~
.. code-block:: pascal
function smartGetParameters(out params: array of string): boolean;
Attempts to grab the correct parameters for spawning a SMART client. Results if
succesfull or not.
.. note::
- By: Brandon, Mormanman, Benland100
- Last Updated: 26 May 2013 by Olly
Example:
.. code-block:: pascal
smartGetParameters(tsa);
*)
function SmartGetParameters(out params: TStringArray): Boolean;
var
WorldLink: String;
p: Integer;
begin
WorldLink := Between('var worldLink=', ''',', GetPage('http://www.runescape.com/game-applet'));
if (WorldLink = '') then
Exit(False);
p := Pos('com/', WorldLink);
Params := Params + Copy(WorldLink, 2, p + 2);
Params := Params + Copy(WorldLink, p + 4, Length(WorldLink));
Result := True;
end;
(*
smartSetAsTarget
~~~~~~~~~~~~~~~~
.. code-block:: pascal
function smartSetAsTarget(pid: integer): boolean;
Pairs Simba to the smart client via EIOS.
.. note::
- By: Olly
- Last Updated: 28 August 2013 by Olly
Example:
.. code-block:: pascal
*)
function smartSetAsTarget(pid: Int32): boolean;
begin
result := true;
try
setEIOSTarget('libsmartremote', intToStr(pid));
print('smartSetAsTarget(): Succesfully set SMART['+ intToStr(pid) +'] as Simba''s target', TDebug.SUB);
except
print('smartSetAsTarget(): Failed to set SMART['+ intToStr(pid) +'] as Simba''s target', TDebug.SUB);
result := false;
end;
end;
(*
smartCreateClient
~~~~~~~~~~~~~~~~~
.. code-block:: pascal
procedure smartCreateClient(w, h: integer);
Creates a smart client using the dimensions "w" and "h".
.. note::
- By: Olly
- Last Updated: 28 August 2013 by Olly
Example:
.. code-block:: pascal
smartCreateClient(800, 600);
*)
function smartCreateClient(w, h: integer): boolean;
var
plugins: string;
params: TStringArray;
path: string;
begin
print('smartCreateClient():', TDebug.HEADER);
if (not smartGetParameters(params)) then
begin
print('Failed to grab parameters', TDebug.FOOTER);
exit(false);
end;
path := getJavaPath();
plugins := implode(',', smartPlugins);
print('Root: ' + params[0]);
print('Params: ' + params[1]);
print('Using plugins "' + plugins + '"');
__smartCurrentTarget := 0;
__smartCurrentTarget := smartSpawnClient(path, stringReplace(PluginPath, '\', '/', [rfReplaceAll]),
params[0], params[1], w, h, smartInitSeq, smartUserAgent,'', plugins);
if (__smartCurrentTarget <> 0) then
begin
print('Succesfully spawned a client, attempting to target');
__smartCurrentPID := smartGetClientPID(__smartCurrentTarget);
if (smartSetAsTarget(__smartCurrentPID)) then
begin
print('smartCreateClient(): result = true', TDebug.FOOTER);
result := true;
end;
end else
print('Failed to spawn a SMART client', TDebug.FOOTER);
end;
(*
smartGetPIDs
~~~~~~~~~~~~
.. code-block:: pascal
function smartGetPIDs(): TIntegerArray;
Returns all true, non pair PIDs of smart clients.
.. note::
- By: Olly
- Last Updated: August 28, 2013 by Olly
Example:
.. code-block:: pascal
tia := smartGetPIDs();
*)
function smartGetPIDs(): TIntegerArray;
var
i, count: Integer;
begin
count := SmartGetClients(true);
setLength(result, count);
if (count > 0) then
for i := 0 to (count - 1) do
result[i] := SmartGetAvailablePID(i);
end;
(*
smartPairToExistingClient
~~~~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: pascal
function smartPairToExistingClient(): boolean;
Attempts to pair us to a previously spawned smart client, will result true if
succesfull.
.. note::
- By: Olly
- Last Updated: August 28, 2013 by Olly
Example:
.. code-block:: pascal
*)
function smartPairToExistingClient(): boolean;
var
pidArr: TIntegerArray;
t: PtrUInt;
i, p: Int32;
begin
result := false;
print('smartPairToExistingClient():', TDebug.HEADER);
pidArr := smartGetPIDs();
if (length(pidArr) < 1) then
begin
print('Found no free clients to pair to');
print('smartPairToExistingClient(): result = false', TDebug.FOOTER);
exit();
end;
print('Found ' + intToStr(length(pidArr)) + ' free client(s), attempting to pair to one...');
for i := 0 to high(pidArr) do
begin
t := 0;
t := SmartPairClient(pidArr[i]);
if (t <> 0) then
begin
p := pidArr[i];
result := smartSetAsTarget(p);
if (result) then
begin
__smartCurrentPID := p;
__smartCurrentTarget := t;
if (not (smartEnabled(__smartCurrentTarget))) then
smartSetEnabled(__smartCurrentTarget, True);
print('Succesfully paired to a existing client, SMART['+ intToStr(__smartCurrentPID) +']');
print('smartPairToExistingClient(): result = true', TDebug.FOOTER);
addOnTerminate('smartFreeCurrentClient');
exit();
end;
end else begin
print('Failed to pair to SMART['+ intToStr(pidArr[i]) +']');
smartFreeClient(t);
end;
end;
print('smartPairToExistingClient(): result = false', TDebug.FOOTER);
end;
(*
initSmart
~~~~~~~~~
.. code-block:: pascal
procedure initSmart();
Initializes a SMART client. If it cannot pair to an existing client, it will
attempt to spawn a client. It will terminate the script if anything goes wrong.
.. note::
- By: Olly
- Last Updated: 26 August 2013 by Olly
Example:
.. code-block:: pascal
*)
function initSmart(forceNew: boolean): boolean;
begin
print('initSmart():', TDebug.HEADER);
if (not forceNew) then
begin
print('Attempting to pair to a previously spawned client');
if (smartPairToExistingClient()) then
begin
print('Succesfully initialized via pairing to a previously spawned client');
print('initSmart()', TDebug.FOOTER);
exit(true);
end;
end;
print('Attempting to spawn a new client..');
if smartCreateClient(__smartClientWidth, __smartClientHeight) then
begin
result := true;
print('Succesfully initialized via spawning a new client');
end;
smartSetDebug(false);
addOnTerminate('smartFreeCurrentClient');
print('initSmart() result = ' + boolToStr(result), TDebug.FOOTER);
end;
(*
smartFreeCurrentClient
~~~~~~~~~~~~~~~~~~~~~~
.. code-block:: pascal
procedure smartFreeCurrentClient();
Frees our current smart client.
.. note::
- By: Olly
- Last Updated: 28 August 2013 by Olly
Example:
.. code-block:: pascal
*)
procedure smartFreeCurrentClient();
begin
try
smartFreeClient(__smartCurrentTarget);
print('Succesfully freed SMART[' + intToStr(__smartCurrentPid) + ']');
except
print('Error freeing SMART[' + intToStr(__smartCurrentPid) + ']', TDebug.ERROR);
end;
end;
(*
smartSetupDrawing
~~~~~~~~~~~~~~~~~
.. code-block:: pascal
procedure smartSetupDrawing();
Setups SMART so it's ready to be drawn on-to.
.. note::
- By: Olly
- Last Updated: 28 August 2013 by Olly
Example:
.. code-block:: pascal
*)
procedure smartSetupDrawing();
begin
if __smartIsDrawingSetup then
exit();
smartSetDebug(true);
try
smartImage.init(Client.getMBitmaps());
smartImage.setName('SMART Debug Image');
smartImage.setPersistentMemory(SmartDebugArray(__smartCurrentTarget), __smartClientWidth, __smartClientHeight);
smartImage.drawClear(0);
print('smartSetupDrawing(): Succesfully setup SMART drawing', TDebug.SUB);
except
print('smartSetupDrawing(): Unable to setup SMART drawing', TDebug.FATAL);
end;
__smartIsDrawingSetup := true;
addOnTerminate('smartFreeDrawing');
end;
(*
smartFreeDrawing
~~~~~~~~~~~~~~~~
.. code-block:: pascal
procedure smartFreeDrawing();
Frees the TMufasaBitmap used for smart drawing.
.. note::
- By: Olly
- Last Updated: 28 August 2013 by Olly
Example:
.. code-block:: pascal
*)
procedure smartFreeDrawing();
begin
try
smartImage.resetPersistentMemory();
smartImage.free();
except
finally
__smartIsDrawingSetup := false;
end;
end;
(*
smartReloadClient
~~~~~~~~~~~~~~~~~
.. code-block:: pascal
procedure smartReloadClient();
Kills the current client, and spawns a new one. The 'waitTime' parameter is
how long to wait after killing the client before spawning a new one.
.. note::
- By: Olly
- Last Updated: 20 September 2013 by Olly
Example:
.. code-block:: pascal
smartReloadClient(60000);
*)
procedure smartReloadClient(waitTime: integer);
var
img, r, h, m, s: integer;
begin
print('smartReloadClient():', TDebug.HEADER);
img := -999;
ConvertTime(waitTime, h, m, s);
if __smartIsDrawingSetup then
begin
img := copyBitmap(smartImage.getIndex());
smartFreeDrawing();
end;
r := SmartGetRefresh(__smartCurrentTarget);
smartFreeCurrentClient();
smartKillClient(__smartCurrentPID);
print('Killed current client. Waiting ' + intToStr(m) + 'm & ' + intToStr(s) + 's before reloading SMART');
wait(waitTime);
if initSmart(true) then
begin
print('Setting refresh rate back to ' + intToStr(r));
SmartSetRefresh(__smartCurrentTarget, r);
if bitmapExists(img) then
begin
smartSetupDrawing();
print('Re-drawing the smartImage bitmap');
getMufasaBitmap(img).drawTransparent(0, 0, smartImage);
freeBitmap(img);
end;
end;
print('smartReloadClient()', TDebug.FOOTER);
end;
(*
sixHourFix
~~~~~~~~~~
.. code-block:: pascal
procedure sixHourFix();
Kills the current client, spawns a new one, and then logs the currentPlayer back
into the game.
.. note::
- By: Olly
- Last Updated: 1 July 2015 by The Mayor
Example:
.. code-block:: pascal
*)
procedure sixHourFix();
var
i: integer;
begin
print('sixHourFix():', TDebug.HEADER);
for i := 1 to smartResetAttempts do
begin
print('SMART Reload Attempt ' + toStr(i) + '/' + toStr(smartResetAttempts));
if (players.getActive() < 1) then
print('No active players, no use in reloading the client!', TDebug.FATAL);
smartReloadClient(60 * randomRange(3000, 10000));
if waitClientReady() then
begin
print('Client succesfully loaded after reload');
if (players[currentPlayer].login()) then
begin
print('sixHourFix(): Succesfully logged in after reload', TDebug.FOOTER);
exit();
end else
print('Failed to login player after restart attempt ' + toStr(i), TDebug.ERROR);
end else
if i = smartResetAttempts then
print('Client never reloaded after ' + toStr(i) + ' attempts', TDebug.FATAL);
inc(i);
end;
end;
(*
__smartCheckPlugins
~~~~~~~~~~~~~~~~~~~
.. code-block:: pascal
procedure __smartCheckPlugins();
Checks what plugins are present in the plugins folder and warns the user if the
directX of OpenGL ones are missing.
.. note::
- By: Olly
Example:
.. code-block:: pascal
*)
{$IFNDEF CODEINSIGHT}
procedure __smartCheckPlugins();
var
dx, ogl: boolean;
begin
dx := fileExists(pluginPath + 'd3d9.dll');
ogl := fileExists(pluginPath + 'OpenGL32.dll');
if (dx = false) and (ogl = false) then
print('No SMART Plugins found, make sure you update your plugins!', TDebug.FATAL);
if (dx = true) and (ogl = false) then
print('Only found the direct-x plugin, you must be in direct-x mode!');
if (dx = false) and (ogl = true) then
print('Only found the OpenGL plugin, you must be in OpenGL mode');
end;
{$ENDIF}
begin
SRL_Events[EVENT_RS_UPDATE] := sixHourFix;
{$IFDEF WINDOWS}
__smartCheckPlugins();
{$ENDIF}
end;
{$f+}