-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathScriptFile.cs
More file actions
1593 lines (1324 loc) · 54.6 KB
/
ScriptFile.cs
File metadata and controls
1593 lines (1324 loc) · 54.6 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
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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace gta5refactor
{
public class ScriptFile
{
public string Name { get; set; }
public string FilePath { get; set; }
public long FileSize { get; set; }
public string[] FileLines { get; set; }
public List<ScriptFunction> Functions { get; set; }
public Dictionary<string, ScriptFunction> FunctionMap { get; set; }
public string LastError { get; set; }
public ScriptFile(string fpath)
{
FileInfo fi = new FileInfo(fpath);
Name = fi.Name;
FilePath = fpath;
FileSize = fi.Length;
FileLines = null;
Functions = null;
FunctionMap = null;
}
public void Load()
{
FileLines = File.ReadAllLines(FilePath);
Functions = GetScriptFunctions(FileLines);
FunctionMap = GetFunctionMap(Functions);
foreach (ScriptFunction func in Functions)
{
//make sure lists are all created before trying to populate them...!
func.References = new List<ScriptFunctionReference>();
}
foreach (ScriptFunction func in Functions)
{
func.LoadDependenciesAndReferences();
}
}
public void Unload()
{
FileLines = null;
Functions = null;
FunctionMap = null;
}
public void Save()
{
//save the file...
string ofpath = FilePath;// + "2";
StringBuilder osb = new StringBuilder();
for (int i = 0; i < FileLines.Length; i++)
{
osb.AppendLine(FileLines[i]);
}
File.WriteAllText(ofpath, osb.ToString());
}
public void AppendToGlobalsFile(ScriptFunction func, string newname, List<ScriptFunctionMatch> fullmatches, string gfilepath)
{
//v1 used by main refactorer tool and v1 autorefactor (slow for multi use!)
//convert the given function to global and append to the globals file.
if (func == null) return;
if (func.File != this) return;
StringBuilder fsb = new StringBuilder();
string funcdecl = string.Format("{0} {1}({2})", func.ReturnType, newname, func.Params);
fsb.AppendLine();
if (FileLines[func.StartLine].Contains('{'))
{
funcdecl += " {"; //listener script files have function decl's with the trailing brace.
}
string extrastr = string.Empty;
if (fullmatches.Count > 1)
{
extrastr = string.Format(" +{0} others", fullmatches.Count - 1);
}
funcdecl += string.Format(" /* From {0}::{1}{2} */", Name, func.Name, extrastr);
string recfuncd = func.Name + "("; //to allow matching recursive functions
string newfuncd = newname + "("; //replace matches with the current function name with the new - for recursives
fsb.AppendLine(funcdecl);
for (int i = func.StartLine + 1; i <= func.EndLine; i++)
{
fsb.AppendLine(FileLines[i].Replace(recfuncd, newfuncd));
}
EnsureGlobalsFile(gfilepath);
File.AppendAllText(gfilepath, fsb.ToString());
}
public void AppendToGlobalsFile(ScriptFunction func, string newname, List<ScriptFunction> fullmatches, string gfilepath, string funcbody)
{
//v2 used by v2 autorefactor (for multi use)
//convert the given functions to global and appends them to the globals file.
if (func == null) return;
StringBuilder fsb = new StringBuilder();
string funcdecl = string.Format("{0} {1}({2})", func.ReturnType, newname, func.Params);
fsb.AppendLine();
//find the first brace and the first non whitepace char, to determine if there's an open brace at the start of the first line
int braceind = funcbody.IndexOf('{');
int firstcharind = 0;
for (int i = 0; i < funcbody.Length; i++)
{
if (!char.IsWhiteSpace(funcbody[i]))
{
firstcharind = i;
break;
}
}
if ((braceind < 0) || (braceind > firstcharind))
{
funcdecl += " {"; //listener script files have function decl's with the trailing brace.
}
string extrastr = string.Empty;
if (fullmatches.Count > 1)
{
extrastr = string.Format(" +{0} others", fullmatches.Count - 1);
}
funcdecl += string.Format(" /* From {0}::{1}{2} */", Name, func.Name, extrastr);
string recfuncd = func.Name + "("; //to allow matching recursive functions
string newfuncd = newname + "("; //replace matches with the current function name with the new - for recursives
fsb.AppendLine(funcdecl);
fsb.Append(funcbody);
EnsureGlobalsFile(gfilepath);
File.AppendAllText(gfilepath, fsb.ToString());
}
public int RenameGlobalFunction(string name, string newname)
{
int hits = 0;
foreach (ScriptFunction func in Functions)
{
if (func.Name == name)
{
//probably found the global declaration for this function.. rename it!
func.RenameDeclaration(newname);
hits++;
}
foreach (ScriptFunctionDependency dep in func.AllDependencies)
{
//update any references in dependencies in this function
if (dep.Name == name)
{
dep.Rename(newname);
hits++;
}
}
}
if (hits > 0)
{
Save();
}
return hits;
}
public bool RefactorFunction(string name, string newname)
{
//this will strip the function with the given name from this file,
//and replace all references with the new name.
//saves the file at the end.
if (FileLines == null) Load();
ScriptFunction func;
if (!FunctionMap.TryGetValue(name, out func))
{
LastError = string.Format("Function {0} not found in {1}.", name, Name);
return false; //apparently the function doesn't exist in here?
}
//go through the references list and replace the function name with the new one.
foreach (ScriptFunctionReference reff in func.References)
{
reff.Rename(name, newname);
//since the ref line may have changed length, need to update any other references on this line...
//only really care about references to the same function. so don't worry about others here.
//(everything will only be used for this process.)
int lendiff = newname.Length - name.Length;
foreach (ScriptFunctionReference ref2 in func.References)
{
if ((ref2.Line == reff.Line) && (ref2.Char > reff.Char))
{
ref2.Char += lendiff; //offset the char to account for it.
}
}
}
string ofpath = FilePath;
StringBuilder osb = new StringBuilder();
for (int i = 0; i < FileLines.Length; i++)
{
if (i == func.StartLine)
{
i = func.EndLine;
}
else
{
osb.AppendLine(FileLines[i]);
}
}
File.WriteAllText(ofpath, osb.ToString());
return true;
}
public bool RefactorFunctions(List<ScriptFunction> oldfuncs)
{
LastError = string.Empty;
List<ScriptFunction> refacfuncs = new List<ScriptFunction>();
foreach (ScriptFunction oldfunc in oldfuncs)
{
ScriptFunction func;
if (!FunctionMap.TryGetValue(oldfunc.Name, out func))
{
LastError += string.Format("Function {0} not found in {1}. ", oldfunc.Name, Name);
continue; //this shouldn't really happen..
}
refacfuncs.Add(func);
int lendiff = oldfunc.NewName.Length - func.Name.Length;
foreach (ScriptFunctionReference reff in func.References)
{
reff.Rename(func.Name, oldfunc.NewName);
//could have multiple refs on the same line. need to offset ones that are to the right..
//references to this function have been updated. But other function references might now
//have incorrect Char values if they were on the same line...
foreach (ScriptFunction tfunc in Functions)
{
foreach (ScriptFunctionReference ref3 in tfunc.References)
{
if ((ref3.Line == reff.Line) && (ref3.Char > reff.Char))
{
ref3.Char += lendiff; //offset the char to account for it.
}
}
}
}
}
string ofpath = FilePath;
StringBuilder osb = new StringBuilder();
Dictionary<int, ScriptFunction> startlinedict = new Dictionary<int, ScriptFunction>();
foreach (ScriptFunction func in refacfuncs)
{
if (!startlinedict.ContainsKey(func.StartLine))
{
startlinedict.Add(func.StartLine, func);
}
}
for (int i = 0; i < FileLines.Length; i++)
{
ScriptFunction stfunc;
if (startlinedict.TryGetValue(i, out stfunc))
{
i = stfunc.EndLine;
}
else
{
osb.AppendLine(FileLines[i]);
}
}
//int curline = 0;
//foreach (ScriptFunction func in refacfuncs)
//{
// for (int i = curline; i < func.StartLine; i++)
// {
// osb.AppendLine(FileLines[i]);
// }
// curline = func.EndLine + 1;
//}
File.WriteAllText(ofpath, osb.ToString());
return true;
}
public static void EnsureGlobalsFile(string path)
{
if (!File.Exists(path))
{
StringBuilder sbmsg = new StringBuilder();
sbmsg.AppendLine("// Globals file from GTA V Refactor by dexyfex");
File.AppendAllText(path, sbmsg.ToString());//it's a great message, the greatest of messages
}
}
public int SyntaxCheck()
{
//search for errors that happen in drp4lyf scripts. (missing close braces)
bool loaded = (FileLines != null);
FileLines = File.ReadAllLines(FilePath);
int errors = 0;
int bracedepth = 0;
bool instr = false;
for (int l = 0; l < FileLines.Length; l++)
{
string line = FileLines[l];
char lc = (char)0;
for (int i = 0; i < line.Length; i++)
{
char c = line[i];
if ((c == '"') && (lc != '\\')) //maybe there's an escaped string somewhere?
{
instr = !instr;
}
if ((c == '/') && (lc == '/') && (!instr)) //begin single line comment, go to next line.
{
lc = c;
continue;
}
switch (c)
{
case '{': bracedepth++; break;
case '}': bracedepth--; break;
}
lc = c;
}
}
errors = Math.Abs(bracedepth); //let's just check how badly all the braces line up.
if (!loaded) FileLines = null;
return errors;
}
public int FixSyntax()
{
//try to fix errors that happen in drp4lyf scripts. (missing close braces)
bool loaded = (FileLines != null);
FileLines = File.ReadAllLines(FilePath);
int bracedepth = 0;
bool instr = false;
int additions = 0;
List<int> errlines = new List<int>();
List<int> errdepths = new List<int>();
for (int l = 0; l < FileLines.Length; l++)
{
string line = FileLines[l];
char lc = (char)0;
for (int i = 0; i < line.Length; i++)
{
char c = line[i];
if ((c == '"') && (lc != '\\')) //maybe there's an escaped string somewhere?
{
instr = !instr;
}
if ((c == '/') && (lc == '/') && (!instr)) //begin single line comment, go to next line.
{
lc = c;
continue;
}
switch (c)
{
case '{': bracedepth++; break;
case '}': bracedepth--; break;
}
lc = c;
}
//see if this line is a function declaration...
if (instr) continue; //in a string... shouldn't happen really
if (line.Length == 0) continue; //empty line.. no function
char l0 = line[0];
if (l0 == '#') continue; //ignore #region etc.
if (l0 == ' ') continue; //ignore lines starting with a space... (most function body - listener)
if (l0 == '\t') continue; //ignore lines starting with a tab... (most function body - drp4lyf)
if ((l0 == '/') && (line.Length > 1) && (line[1] == '/')) continue; //line is a comment.
if ((l0 == '{') || (l0 == '}')) continue; //it's an open/close brace line, probably no declaration here.
if (line.IndexOf('(') == -1) continue; //no open bracket found, can't be a function declaration.
//if we got here, it's probably a function declaration.
//need to insert closing braces here to get the depth back to zero, if it isn't zero already..
//but we're iterating the lines here, so save the position to insert the braces in 2nd pass.
if (bracedepth != 0)
{
errlines.Add(l);
errdepths.Add(bracedepth);
bracedepth = 0;
for (int i = 0; i < line.Length; i++) //there might still be braces on this line...
{
char c = line[i];
switch (c)
{
case '{': bracedepth++; break; //shouldn't really happen in drp4lyf scripts
case '}': bracedepth--; break; //this really should be considered an error case. oh well.
}
}
}
}
//quick test to guess if the script is using tab or space indenting...
bool usetab = false;
for (int l = 0; l < 20; l++)
{
string line = FileLines[l];
if (line.Length == 0) continue;
if (line[0] == '\t')
{
usetab = true;
break;
}
}
//now insert the missing closing braces..
int cline = 0;
StringBuilder sb = new StringBuilder();
for (int el = 0; el < errlines.Count; el++)
{
int errl = errlines[el];
int errdepth = errdepths[el];
//copy the lines up until this point.
for (int i = cline; i < errl; i++)
{
sb.AppendLine(FileLines[i]);
}
//add the closing brace lines.
for (int i = 0; i < errdepth; i++)
{
int inset = errdepth - i - 1;
for (int j = 0; j < inset; j++)
{
if (usetab) sb.Append('\t');
else sb.Append(" "); //use 3 spaces if not a tab..
}
sb.Append('}'); //close that brace!
sb.AppendLine(" // This line was added by GTA V Refactor."); //add a helpful message
if (inset == 0)
{
sb.AppendLine(); //try to keep the gap between this and the next function.
}
}
cline = errl; //keep track of where we left off!
}
for (int l = cline; l < FileLines.Length; l++)
{
sb.AppendLine(FileLines[l]); //copy any remaining lines.
}
File.WriteAllText(FilePath, sb.ToString()); //save the new string to the file.
if (!loaded) FileLines = null;
return additions;
}
public List<ScriptHash> FindHashes(bool findunsigned, bool findsigned, bool findhex, int minlength)
{
List<ScriptHash> res = new List<ScriptHash>();
bool loaded = (FileLines != null);
FileLines = File.ReadAllLines(FilePath);
StringBuilder sb = new StringBuilder(); //for building found hashes.
for (int l = 0; l < FileLines.Length; l++)
{
string line = FileLines[l].ToLower();
bool innum = false;
bool hexstart = false;
sb.Clear(); //a new line is a new string.
char lc = (char)0;
for (int i = 0; i < line.Length; i++)
{
char c = line[i];
bool match = false;
switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
match = true;
break;
case '-':
match = findsigned && !innum;
break;
case 'x':
match = findhex && (lc == '0');
hexstart = match;
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
match = findhex && hexstart;
break;
}
if (match)
{
sb.Append(c);
innum = true;
}
else
{
if (innum)
{
//anything we consider a potential match just ended. record it...
if (sb.Length >= minlength)
{
bool issigned = (sb[0] == '-');
bool ishex = findhex && hexstart;
bool isunsigned = !(issigned || ishex);
if ((isunsigned && findunsigned) || (issigned && findsigned) || (ishex && findhex))
{
ScriptHash foundhash = new ScriptHash();
foundhash.File = this;
foundhash.HashStr = sb.ToString();
foundhash.Line = l;
foundhash.Char = i - sb.Length;
res.Add(foundhash);
}
}
}
innum = false;
hexstart = false;
sb.Clear();
}
lc = c;
}
}
if (!loaded) FileLines = null;
return res;
}
public List<ScriptChange> ReplaceHashes(List<ScriptHash> hashes, Dictionary<string, JenkIndMatch> matches, Dictionary<string, List<JenkIndMatch>> collisions, string format, bool replace, bool insert)
{
//hashes are the results from FindHashes!
//matches are what was found in the JenkIndex.
//collisions are matches that have more than one possible value.
bool loaded = (FileLines != null);
FileLines = File.ReadAllLines(FilePath);
List<ScriptChange> changes = new List<ScriptChange>();
foreach (ScriptHash hash in hashes)
{
if (hash.File != this)
{
continue; //ignore hashes that aren't for this file.
}
int l = hash.Line;
string line = FileLines[l];
string linel = line.ToLower();
JenkIndMatch match;
if (matches.TryGetValue(hash.HashStr, out match)) //make sure this hash is one we found ea match for!
{
int ind = linel.IndexOf(hash.HashStr, hash.Char);
if (ind != -1) //make sure this hash is present in this line...
{
string newstr = string.Format(format, match.Value);
int diffoffset;
if (!char.IsWhiteSpace(line[0]))
{
line = line + " //" + newstr; //seems to be a function call line... append onto the end!
diffoffset = newstr.Length + 2;
}
else if (replace)
{
line = line.Replace(hash.HashStr, newstr);
diffoffset = newstr.Length - hash.HashStr.Length;
}
else
{
if (!insert) //insert after... do same thing as insert before, but shifted to the right
{
ind = ind + hash.HashStr.Length;
}
string s1 = (ind > 0) ? line.Substring(0, ind) : string.Empty;
string s2 = (ind < line.Length) ? line.Substring(ind) : string.Empty;
line = s1 + newstr + s2;
diffoffset = newstr.Length;
}
ScriptChange change = new ScriptChange();
change.File = this;
change.LineNum = l;
change.LineBefore = FileLines[l];
change.LineAfter = line;
changes.Add(change);
FileLines[l] = line;
foreach (ScriptHash otherhash in hashes)
{
if ((otherhash.Line == hash.Line) && (otherhash.Char > hash.Char))
{
otherhash.Char += diffoffset;
}
}
}
else
{
//this shouldn't happen?
}
}
}
if (changes.Count > 0)
{
Save();
}
if (!loaded) FileLines = null;
return changes;
}
public List<ScriptChange> ReplaceHash(ScriptHash hash, string val, string format, bool replace, bool insert)
{
//helper. not currently used...
List<ScriptHash> hashes = new List<ScriptHash>();
hashes.Add(hash);
Dictionary<string, JenkIndMatch> matches = new Dictionary<string, JenkIndMatch>();
JenkIndMatch match = new JenkIndMatch(hash.HashStr, val);
matches.Add(hash.HashStr, match);
Dictionary<string, List<JenkIndMatch>> collisions = new Dictionary<string, List<JenkIndMatch>>();
return ReplaceHashes(hashes, matches, collisions, format, replace, insert);
}
public List<ScriptCoord> FindCoordinates()
{
List<ScriptCoord> result = new List<ScriptCoord>();
bool loaded = (FileLines != null);
FileLines = File.ReadAllLines(FilePath);
StringBuilder sb = new StringBuilder(); //for building found hashes.
List<double> vals = new List<double>();
for (int l = 0; l < FileLines.Length; l++)
{
string line = FileLines[l].ToLower();
bool innum = false;
int commacount = 0;
sb.Clear(); //a new line is a new string.
for (int i = 0; i < line.Length; i++)
{
char lastc = (i > 0) ? line[i - 1] : (char)0;
char c = line[i];
bool match = false;
if (char.IsDigit(c) || (c == '-'))
{
match = true;
}
else if (innum && ((c == '.') || (c == ',') || (c == ' ')))
{
match = true;
if (c == ',') commacount++;
}
if (match)
{
if (innum)
{
sb.Append(c);
}
else if (!(char.IsLetterOrDigit(lastc) || (c == '_')))
{
sb.Append(c);
innum = true;
}
}
else
{
if (innum && (commacount > 1))
{
//anything we consider a potential match just ended. record it...
string vecstr = sb.ToString();
List<ScriptCoord> linecoords = GetCoordsFromVecStr(vecstr, l, vals);
result.AddRange(linecoords);
}
sb.Clear();
innum = false;
commacount = 0;
}
}
}
if (!loaded) FileLines = null;
return result;
}
private List<ScriptCoord> GetCoordsFromVecStr(string vecstr, int linenum, List<double> vals)
{
//vals is passed in here as an optimisation. it is a temporary.
List<ScriptCoord> result = new List<ScriptCoord>();
string[] components = vecstr.Split(',');
vals.Clear();
for (int v = 0; v < components.Length; v++)
{
string comp = components[v].Trim();
double val;
double.TryParse(comp, out val);
//bool isdec = (((val - Math.Floor(val)) > 0.0) || ((comp.Contains('.') && (comp.Length > 3))));
components[v] = comp;
vals.Add(val);
}
if (vals.Count == 3) //simple case.
{
//just add it to the result, filtering will be done later
ScriptCoord coord = new ScriptCoord(this, linenum, vals);
if (coord.Score > 0.0)
{
result.Add(coord);
}
}
else
{
List<ScriptCoord> candidates = new List<ScriptCoord>();
ScriptCoord lastcand = null;
int lasti = -10;
for (int i = 0; i < vals.Count - 1; i++)
{
ScriptCoord candidate = new ScriptCoord(this, linenum, vals, i);
if (candidate.Score > 0.0)
{
if ((lastcand == null) || ((i - lasti) > 2))
{
candidates.Add(candidate);
lastcand = candidate;
lasti = i;
}
else if (lastcand.Score < candidate.Score)
{
candidates[candidates.Count - 1] = candidate;
lastcand = candidate;
lasti = i;
}
}
}
if (candidates.Count > 0)
{
lastcand = null;
foreach (ScriptCoord cand in candidates)
{
if ((lastcand != null) && (lastcand.W == cand.X))
{
lastcand.W = 0.0; //sequential 3D candidates - fixing incorrect W val for the first one
}
lastcand = cand;
}
result.AddRange(candidates);
}
}
return result;
}
public ScriptFunction TryGetFunction(int line, bool keeploaded)
{
ScriptFunction rf = null;
bool loaded = (FileLines != null);
if (!loaded)
{
Load();
}
foreach (ScriptFunction func in Functions)
{
if ((func.StartLine <= line) && (func.EndLine >= line))
{
rf = func;
break;
}
}
if (!loaded && !keeploaded)
{
Unload();
}
return rf;
}
private List<ScriptFunction> GetScriptFunctions(string[] lines)
{
List<ScriptFunction> res = new List<ScriptFunction>();
int bracedepthlast = 0;
int bracedepth = 0;
string functype = "";
string funcname = "";
string funcparams = "";
int funcstartline = 0;
bool instr = false;
for (int l = 0; l < lines.Length; l++)
{
string line = lines[l];
if (line.Length == 0) continue;
char l0 = line[0];
if (l0 == '#') continue; //ignore #region etc.
if (l0 == ' ') continue; //ignore lines starting with a space... (most function body - listener)
if (l0 == '\t') continue; //ignore lines starting with a tab... (most function body - drp4lyf)
if ((l0 == '/') && (line.Length > 1) && (line[1] == '/')) continue; //line is a comment.
bool hasspace = false;
int spaceidx = 0;
int linestartbracedepth = bracedepth;
char lc = (char)0;
for (int i = 0; i < line.Length; i++)
{
char c = line[i];
if ((c == '"') && (lc != '\\')) //maybe there's an escaped string somewhere?
{
instr = !instr;
}
if ((c == '/') && (lc == '/') && (!instr)) //begin single line comment, go to next line.
{
lc = c;
continue;
}
switch (c)
{
case '{': bracedepth++; break;
case '}': bracedepth--; break;
case ' ': if (!hasspace) spaceidx = i; hasspace = true; break;
}
lc = c;
}
if ((linestartbracedepth == 0) && (hasspace))
{
int paramstart = line.IndexOf('(', spaceidx);
int paramend = line.LastIndexOf(')');
functype = line.Substring(0, spaceidx);
funcname = line.Substring(spaceidx + 1, paramstart - spaceidx - 1);
funcparams = line.Substring(paramstart + 1, paramend - (paramstart + 1));
funcstartline = l;
}
if ((bracedepthlast == 1) && (bracedepth == 0))
{
//function just ended.. record it
int funclength = l - funcstartline;
ScriptFunction fun = new ScriptFunction();
fun.File = this;
fun.StartLine = funcstartline;
fun.EndLine = l;
fun.Length = funclength;
fun.Name = funcname;
fun.ReturnType = functype;
fun.Params = funcparams;
res.Add(fun);
}
bracedepthlast = bracedepth;
}
return res;
}
private Dictionary<string, ScriptFunction> GetFunctionMap(List<ScriptFunction> funcs)
{
Dictionary<string, ScriptFunction> res = new Dictionary<string, ScriptFunction>();
foreach (ScriptFunction func in funcs)
{
if (!res.ContainsKey(func.Name))
{
res.Add(func.Name, func);
}
}
return res;
}
}
public class ScriptFunction
{
public ScriptFile File { get; set; }
public int StartLine { get; set; }
public int EndLine { get; set; }
public int Length { get; set; }
public string Name { get; set; }
public string ReturnType { get; set; }
public string Params { get; set; }
public string NewName { get; set; }
public List<ScriptFunctionDependency> AllDependencies { get; set; }
public List<ScriptFunctionDependency> LocalDependencies { get; set; }
public List<ScriptFunctionReference> References { get; set; }
public void LoadDependenciesAndReferences()
{
if (File.FileLines == null) return;// File.Load();
AllDependencies = new List<ScriptFunctionDependency>();
LocalDependencies = new List<ScriptFunctionDependency>();