-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathLuaScriptFunction.cs
More file actions
697 lines (630 loc) · 32.2 KB
/
Copy pathLuaScriptFunction.cs
File metadata and controls
697 lines (630 loc) · 32.2 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
using LuaToolkit.Core;
using LuaToolkit.Disassembler;
using LuaToolkit.Models;
using System;
using System.Collections.Generic;
using System.Linq;
namespace LuaToolkit.Decompiler
{
public class LuaScriptFunction
{
public int Depth;
private LuaDecoder Decoder;
private LuaFunction Func;
public bool IsLocal = false;
public bool HasVarargs = false;
private List<int> Args;
private List<string> NameArgs;
public List<LuaScriptLine> Lines;
public List<LuaScriptBlock> Blocks;
public List<int> UsedLocals;
private string _text;
public string Name
{
get
{
if (this.Func == null || this.Func.Name == null || this.Func.Name == "" || this.Func.Name.Contains("@"))
{
return GetName();
}
else
return this.Func.Name;
}
set
{
if (this.Func != null && this.Func.Name != null)
this.Func.Name = value;
}
}
public string Text
{
get { return GetText(); }
}
public LuaScriptFunction(string name, int argsCount, ref LuaFunction func, ref LuaDecoder decoder)
{
this.Name = name;
this.Func = func;
this.Func.ScriptFunction = this; // reference this for lateron
this.Decoder = decoder;
this.Lines = new List<LuaScriptLine>();
this.Blocks = new List<LuaScriptBlock>();
this.UsedLocals = new List<int>();
InitArgs(argsCount);
this.UsedLocals.AddRange(this.Args);
HandleUpvalues(); // get upvalues from parent TODO: Bugfix
}
private void InitArgs(int count)
{
this.Args = new List<int>();
this.NameArgs = new List<string>();
for (int i = 0; i < count; i++)
{
this.Args.Add(i);
this.NameArgs.Add($"var{i}");
}
}
public override string ToString()
{
string args = "(";
for (int i = 0; i < this.NameArgs.Count; i++)
{
args += this.NameArgs[i];
if (i < this.NameArgs.Count - 1 || this.HasVarargs)
args += ", ";
}
args += (this.HasVarargs ? "...)" : ")");
return (this.IsLocal ? "local " : "") + $"function {GetName()}{args}\r\n";
}
private string GetName()
{
if (this.Func.Name == "" || this.Func.Name.Contains("@")) // unknownX
{
// TODO: prefix functions so we can distiguins one parent from another? (like: unknown_0_1)
var parent = GetParentFunction();
if (parent == null)
return "unkErr";
// TODO: get all parents?
int unkCount = -1;
for (int i = 0; i < parent.Functions.IndexOf(this.Func); i++)
{
if (parent.Functions[i].ScriptFunction.IsLocal)
unkCount++;
}
return "unknown" + (unkCount + 1); // should give right index?
}
return this.Func.Name;
}
public LuaFunction GetParentFunction()
{
if (this.Decoder.File.Function == this.Func)
return null; // we root already
return FindParentFunction(this.Decoder.File.Function);
}
private LuaFunction FindParentFunction(LuaFunction function, LuaFunction search = null)
{
// NOTE: recursive, always nice to stackoverflow
// check if any the functions matches us
if (search == null)
search = this.Decoder.File.Function;
var target = search.Functions.FindIndex(x => x.ScriptFunction == this);
if (target != -1)
return search;
// search children
foreach (var f in search.Functions)
{
var res = FindParentFunction(function, f);
if (res != null)
return res;
}
return null;
}
// NOTE: Please do NOT touch this unless you 110% know what you are doing!!!
private void GenerateBlocks(bool overwriteBlocks = false)
{
int index = 0;
if (overwriteBlocks || this.Blocks.Count == 0)
{
this.Blocks.Clear();
while (index < this.Lines.Count)
{
LuaScriptBlock b = new LuaScriptBlock(index, ref this.Decoder, ref this.Func);
while (index < this.Lines.Count)
{
if (b.AddScriptLine(this.Lines[index]))
break;
index++;
}
index++;
this.Blocks.Add(b); // save block
}
// add block jumpsFrom and split
List<KeyValuePair<int, int>> BlockSplitLines = new List<KeyValuePair<int, int>>();// block, line
for (int i = 0; i < this.Blocks.Count; i++)
{
if (this.Blocks[i].JumpsTo == -1)
continue;
var targets = this.Blocks.FindAll(x => x.HasLineNumber(this.Blocks[i].JumpsTo));
if (targets.Count > 0)
BlockSplitLines.Add(new KeyValuePair<int, int>(this.Blocks.FindIndex(x => x.StartAddress == targets[0].StartAddress), this.Blocks[i].JumpsTo));
//foreach (var tb in targets)
// BlockSplitLines.Add(new KeyValuePair<int, int>(this.Blocks.FindIndex(x => x.StartAddress == tb.StartAddress), i));
}
BlockSplitLines = BlockSplitLines.OrderBy(x => x.Value).ToList(); // important to sort by lineNumber or other blocks wont get done otherwise
// cut blocks and make new ones
for (int i = 0; i < BlockSplitLines.Count; i++)
{
if (this.Blocks[BlockSplitLines[i].Key].StartAddress + this.Blocks[BlockSplitLines[i].Key].Lines.Count < BlockSplitLines[i].Value ||
this.Blocks[BlockSplitLines[i].Key].StartAddress > BlockSplitLines[i].Value)
continue; // already circumcised this boi
if (this.Blocks.Find(x => x.StartAddress == BlockSplitLines[i].Value) != null)
continue; // been there, done that
LuaScriptBlock splitBlock = new LuaScriptBlock(BlockSplitLines[i].Value, ref this.Decoder, ref this.Func);
for (int j = BlockSplitLines[i].Value - this.Blocks[BlockSplitLines[i].Key].StartAddress; j < this.Blocks[BlockSplitLines[i].Key].Lines.Count; j++)
splitBlock.Lines.Add(this.Blocks[BlockSplitLines[i].Key].Lines[j]); // copy from old to new
// delete old lines
if (splitBlock.Lines.Count > 0)
this.Blocks[BlockSplitLines[i].Key].Lines.RemoveRange(BlockSplitLines[i].Value - this.Blocks[BlockSplitLines[i].Key].StartAddress, splitBlock.Lines.Count);
this.Blocks.Insert(BlockSplitLines[i].Key + 1, splitBlock); // insert new block after modified one
// update BlockSplitLines indexing
for (int j = i + 1; j < BlockSplitLines.Count; j++)
BlockSplitLines[j] = new KeyValuePair<int, int>(BlockSplitLines[j].Key + 1, BlockSplitLines[j].Value); // offset remaining blocks
}
// fix JumpsTo and JumpsNext ?
this.Blocks.OrderBy(x => x.StartAddress);
for (int i = 0; i < this.Blocks.Count; i++)
{
// last return
if (i == this.Blocks.Count - 1)
{
// Last block shouldnt jump to anywhere
this.Blocks[i].JumpsTo = -1;
this.Blocks[i].JumpsNext = -1;
if (this.Blocks[i].Lines[this.Blocks[i].Lines.Count - 1].Instr.OpCode == LuaOpcode.RETURN)
this.Blocks[i].Lines[this.Blocks[i].Lines.Count - 1].Op1 = "end"; // replace last RETURN with END
continue;
}
// Conditions without JMP
if (this.Blocks[i].Lines.Count > 0)
{
switch (this.Blocks[i].Lines[this.Blocks[i].Lines.Count - 1].Instr.OpCode)
{
// TODO: check which instructions dont pick the next one
case LuaOpcode.TFORLOOP:
case LuaOpcode.FORLOOP: // calculate LOOP jump
this.Blocks[i].JumpsTo = (this.Blocks[i].StartAddress + this.Blocks[i].Lines.Count - 1) + (short)this.Blocks[i].Lines[this.Blocks[i].Lines.Count - 1].Instr.sBx + 1; // TODO: verify math
this.Blocks[i].JumpsNext = this.Blocks[i + 1].StartAddress;
break; // jmp?
//case LuaOpcode.LOADBOOL: // pc++
// this.Blocks[i].JumpsTo = (this.Blocks[i].StartAddress + this.Blocks[i].Lines.Count - 1) + 2; // skips one if C
// this.Blocks[i].JumpsNext = (this.Blocks[i].StartAddress + this.Blocks[i].Lines.Count - 1) + 1; // next block
// break;
case LuaOpcode.JMP: // pc++
// check previous condition
if (this.Blocks[i].Lines.Count > 1 && this.Blocks[i].Lines[this.Blocks[i].Lines.Count - 2].IsCondition()) // check for IF
{
// if/test/testset
this.Blocks[i].JumpsTo = (this.Blocks[i].StartAddress + this.Blocks[i].Lines.Count - 1) + (short)this.Blocks[i].Lines[this.Blocks[i].Lines.Count - 1].Instr.sBx + 1; // TODO: verify math
this.Blocks[i].JumpsNext = this.Blocks[i + 1].StartAddress;
}
else
{
// unknown jump
this.Blocks[i].JumpsTo = (this.Blocks[i].StartAddress + this.Blocks[i].Lines.Count - 1) + (short)this.Blocks[i].Lines[this.Blocks[i].Lines.Count - 1].Instr.sBx + 1; // TODO: verify math
this.Blocks[i].JumpsNext = -1; // this.Blocks[i + 1].StartAddress;
}
break;
default:
this.Blocks[i].JumpsTo = -1; // erase from possible previous block?
this.Blocks[i].JumpsNext = this.Blocks[i + 1].StartAddress;
break;
}
}
}
}
for (int i = 0; i < this.Blocks.Count; i++)
{
// IF: JMP != -1 && ELSE != -1 (&& GetConditionLine != NULL; ELSE; FORLOOP END (dont care))
// ELSE: JMP != -1 && ELSE == -1
// ENDIF: JMP == -1 && ELSE != -1
// END: JMP == -1 && ELSE == -1
if (this.Blocks[i].GetBranchLine() != null &&
(this.Blocks[i].GetBranchLine().Instr.OpCode == LuaOpcode.FORLOOP || this.Blocks[i].GetBranchLine().Instr.OpCode == LuaOpcode.TFORLOOP))
{
#if DEBUG
this.Blocks[i].GetBranchLine().Text = "end -- ENDLOOP\r\n";
#else
this.Blocks[i].GetBranchLine().Text = "end\r\n";
#endif
}
else if (this.Blocks[i].JumpsTo != -1 && this.Blocks[i].JumpsNext != -1 && this.Blocks[i].GetConditionLine() != null) // IF detected
{
try
{
// merge
int lastifIndex = -1;
int bIndex = i + 1; // search end of IF
while (bIndex < this.Blocks.Count)
{
if (this.Blocks[bIndex].JumpsTo == -1 || this.Blocks[bIndex].JumpsNext == -1)
{
lastifIndex = bIndex - 1;
break; // IF found
}
bIndex++;
}
// TODO: find the bodyblock for the last IF and compare against previous IF to find merge chain,
// re-do group IF's that do not match the ifbodyblock end/start-1 and figure out if its and/or
// depending on where the jump is set to. The last one should always be classified as 'and'
// can be used to figure out the END of the ifbodyblock, we check others by keeping in mind
// they can be both and/or, meaning ifbodyblock (and) || ifbodyblock-1 (or)
// iterate from lastifIndex to i and split
int ifIndex = lastifIndex;
if (this.Blocks[i].IfChainIndex != -1)
continue; // skip if already discovered
while (ifIndex >= i)
{
// NOTE: not always the case??
// TODO: INF loop somtimes!!
var ifbodyBlockEnd = this.Blocks.ToList().Single(x => x.StartAddress == this.Blocks[lastifIndex].JumpsTo); // end JMP
var ifbodyBlockStart = this.Blocks[lastifIndex + 1]; // start +1
// NOTE: iterate from end to here to which block it JMPs to
bool found = false;
int cIndex = this.Blocks.IndexOf(ifbodyBlockEnd); // start from endblock
while (cIndex > ifIndex)
{
// scan if's (and ONLT if's)
if (this.Blocks[ifIndex].JumpsTo == this.Blocks[cIndex].StartAddress
&& this.Blocks[ifIndex].GetConditionLine() != null && this.Blocks[ifIndex].GetConditionLine().IsCondition())
{
found = true;
bool jmpsToStart = this.Blocks[ifIndex].JumpsTo == ifbodyBlockStart.StartAddress; // is or?
if (jmpsToStart)
{
if (ifIndex != lastifIndex)
this.Blocks[ifIndex].GetConditionLine().Op3 = "or";
if (this.Blocks[ifIndex].GetConditionLine().Instr.A == 0)
this.Blocks[ifIndex].GetConditionLine().Op2 = this.Blocks[ifIndex].GetConditionLine().Op2.Replace("==", "~=");
}
else
{
if (ifIndex != lastifIndex)
this.Blocks[ifIndex].GetConditionLine().Op3 = "and";
if (this.Blocks[ifIndex].GetConditionLine().Instr.A == 1)
this.Blocks[ifIndex].GetConditionLine().Op2 = this.Blocks[ifIndex].GetConditionLine().Op2.Replace("==", "~=");
}
if (ifIndex != lastifIndex && this.Blocks[ifIndex + 1].GetConditionLine() != null)
this.Blocks[ifIndex + 1].GetConditionLine().Op1 = "";
if (this.Blocks[ifIndex].IfChainIndex == -1)
this.Blocks[ifIndex].IfChainIndex = ifIndex - i; // NOTE: numbers are NOT correct after rebase!
break;
}
cIndex--;
}
ifIndex--;
if (!found)
{
ifIndex++;
if (this.Blocks[ifIndex + 1].IfChainIndex != -1)
{
// cleanup existing end of merged ifchain
int ifIndexFix = ifIndex + 1;
do
{
this.Blocks[ifIndexFix].IfChainIndex = ifIndexFix - ifIndex - 1; // rebase
ifIndexFix++;
}
while (this.Blocks[ifIndexFix].IfChainIndex != -1);
}
lastifIndex = ifIndex; // new IF end found!
// TODO: this below shit is very buggy, temp fix for FORLOOPs?
if(cIndex < ifIndex || this.Blocks[ifIndex].GetConditionLine() == null) // skip LOOPs?
ifIndex--; // subtract or inf loop?? TODO: bugfix, may
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
else if (this.Blocks[i].JumpsTo != -1 && this.Blocks[i].JumpsNext == -1)
{
#if DEBUG
this.Blocks[i].GetBranchLine().Postfix += "else -- ELSE";
#else
this.Blocks[i].GetBranchLine().Postfix += "else";
#endif
}
else if (this.Blocks[i].JumpsTo == -1 && this.Blocks[i].JumpsNext != -1 && this.Blocks[i].GetBranchLine() != null
&& this.Blocks[i].GetBranchLine().Instr.OpCode != LuaOpcode.FORPREP) // also make sure if condifition is set (no forloop)
{
#if DEBUG
this.Blocks[i].Lines[this.Blocks[i].Lines.Count - 1].Postfix += "\r\nend -- ENDIF";
#else
this.Blocks[i].Lines[this.Blocks[i].Lines.Count - 1].Postfix += "\r\nend";
#endif
}
else if (this.Blocks[i].JumpsTo == -1 && this.Blocks[i].JumpsNext == -1)
{
#if DEBUG
this.Blocks[i].GetBranchLine().Postfix += " -- END\r\n"; // already taken care of
#endif
}
}
}
// NOTE: OO?
public string GetConstant(int index, LuaFunction targetFunc = null)
{
if (targetFunc == null)
targetFunc = this.Func; // self
if (index > 255 && targetFunc.Constants[index - 256] != null)
return targetFunc.Constants[index - 256].ToString();
else if (targetFunc.Constants.Count > index)
return targetFunc.Constants[index].ToString();
else
return "unk" + index;
}
private void HandleUpvalues()
{
// NOTE: Upvalues are used for function prototypes and are referenced to in a global scope
// My job is to generate a list of static available upvalues so that the decompiler can
// reference to them, they are commonly used for function calls that are in scope of the root function.
//return;
LuaFunction parent = GetParentFunction();
if (parent == null)
return; // we in root UwU
//return;
// create Upvalues List from parent
int functionIndex = parent.Functions.IndexOf(this.Func);
for (int i = 0; i < parent.Instructions.Count; i++)
{
// TODO: bugfix
var instr = parent.Instructions[i];
if (!(instr.OpCode == LuaOpcode.CLOSURE && instr.Bx == functionIndex))
continue;
string globalName = "";
this.IsLocal = true;
int j = i - 1;
// Find GETGLOBAL - if needed?
//while (j >= 0)
//{
// if (parent.Instructions[j].OpCode == LuaOpcode.GETGLOBAL && parent.Instructions[i].A == parent.Instructions[j].A)
// {
// globalName = parent.Constants[parent.Instructions[j].Bx].ToString();
// globalName = globalName.Substring(1, globalName.Length - 2);
// break; // job's done
// }
// j--;
//}
j = i + 1; // instr after CLOSURE to start with
bool closure = false;
int setTableIndex = -1;
while (j < parent.Instructions.Count)
{
if (parent.Instructions[j].OpCode == LuaOpcode.CLOSURE || parent.Instructions[j].OpCode == LuaOpcode.CLOSE || parent.Instructions[j].OpCode == LuaOpcode.RETURN)
break; // end of closure
//closure = true; // stop MOVEs after closure, keep going for settable/setglobal
if (parent.Instructions[j].OpCode == LuaOpcode.MOVE && !closure)
{
// upvalues!
if (parent.Instructions[j].A == 0) // 0 = _ENV
{
// TODO: handle value correct & erase script line
LuaConstant cons;
string obj = GetConstant(parent.Instructions[j].B, parent).ToString();
//if(parent.ScriptFunction != null)
// obj = parent.ScriptFunction.Lines.FirstOrDefault().WriteIndex(parent.Instructions[j].B);
//if (!obj.Contains("var"))
// cons = new PrototypeConstant($"{parent.Name}_{parent.Instructions[j].B}\0"); // TODO: parent name or actual name?
//else
// cons = new StringConstant(obj); // idk?
if(obj.Contains('\"'))
cons = new StringConstant(obj.Substring(1, obj.Length-2) + "\0");
else
cons = new StringConstant(obj + "\0");
this.Func.Upvalues.Add(cons);
}
}
else if (parent.Instructions[j].OpCode == LuaOpcode.SETTABLE)
{
// check the source and desitnation of the SETTABLE to find out both local and global name
if (setTableIndex == -1 && parent.Instructions[i].A == parent.Instructions[j].C) // SETTABLE x y == CLOSURE y ?
{
// find first part of the table
// TODO: bugfix false locals
this.IsLocal = false;
this.Name = GetConstant(parent.Instructions[j].B, parent).ToString();
this.Name = this.Name.Substring(1, this.Name.Length - 2);
//closure = true;
setTableIndex = j; // src
}
else if (setTableIndex > -1 && parent.Instructions[setTableIndex].A == parent.Instructions[j].C)
{
// find second part of the table, which is the root/global
globalName = GetConstant(parent.Instructions[j].B, parent).ToString();
//globalName = globalName.Substring(1, globalName.Length - 2);
//break;
}
}
else if (parent.Instructions[j].OpCode == LuaOpcode.SETGLOBAL && !closure && parent.Instructions[i].A == parent.Instructions[j].A) // CLOSURE x ? == SETGLOBAL x ?
{
// is global!
this.IsLocal = false;
this.Name = GetConstant(parent.Instructions[j].C, parent).ToString();
this.Name = this.Name.Substring(1, this.Name.Length - 2);
//closure = true;
//break;
}
j++;
}
if (globalName != "")
this.Name = globalName + ":"+ this.Name;
// set line CLOSURE from parent
parent.ScriptFunction.Lines[i].FunctionRef = this.Func;
}
}
private void HandleTailcallReturns()
{
// NOTE: Tailcalls have 2 returns when C functions or 1 when Lua functions
// My job is to remove those RETURNS because they are only used in the Lua VM
for (int i = 0; i < this.Blocks.Count; i++)
{
for (int j = 0; j < this.Blocks[i].Lines.Count; j++)
{
if (this.Blocks[i].Lines[j].Instr.OpCode == LuaOpcode.RETURN)
{
// check if previous 1/2 is a TAILCALL
bool erase = false;
if (j >= 1 && this.Blocks[i].Lines[j - 1].Instr.OpCode == LuaOpcode.TAILCALL)
erase = true;
else if (j >= 2 && this.Blocks[i].Lines[j - 2].Instr.OpCode == LuaOpcode.TAILCALL)
erase = true;
if (erase)
{
#if DEBUG
this.Blocks[i].Lines[j].Op1 = "-- TAILCALL RETURN"; // erase keyword
#else
this.Blocks[i].Lines[j].Op1 = ""; // erase keyword
#endif
this.Blocks[i].Lines[j].Op2 = ""; // erase variables
//this.Blocks[i].Lines[j].Op3 = ""; // erase (dont erase this, contains else)
}
}
}
}
}
private void OutlineConditions()
{
// NOTE: IF statements may have more then 2 instruction (IF, JMP) when they are chained
// My job is to optimize those merged IF blocks so that inline IFs are working fine
// EDIT: nvm I just tweak them and move the instructions that arent IF/JMP up so the merge is clean
foreach (var b in this.Blocks)
if (b.GetConditionLine() != null && b.IfChainIndex > 0)
b.Optimize();
}
private void UpdateClosures()
{
// NOTE: update the names of the functions
for(int i = 0; i < this.Lines.Count; i++)
{
if (this.Lines[i].Instr.OpCode != LuaOpcode.CLOSURE)
continue;
if (this.Func.Functions[this.Lines[i].Instr.Bx].ScriptFunction != null)
this.Lines[i].Op3 = this.Func.Functions[this.Lines[i].Instr.Bx].ScriptFunction.Name;
}
}
public void Complete(bool overwriteBlocks = false)
{
Cleanlines();
GenerateBlocks(overwriteBlocks);
UpdateClosures(); // fixes closure name referncing
HandleTailcallReturns(); // fix returns
OutlineConditions(); // moves IF code above IF chain
}
public string GetText()
{
if (this.Blocks.Count == 0)
this.Complete(); // i guess?
//if (_text != null)
// return _text; // stores end results
string result = this.ToString();
#if DEBUG
result += GenerateDebugCode();
#else
result += GenerateCleanCode();
#endif
return result;
}
private string GenerateDebugCode()
{
string result = "";
int tabLevel = 0;
for (int b = 0; b < this.Blocks.Count; b++)
{
// print block content
for (int i = 0; i < this.Blocks[b].Lines.Count; i++)
{
if (this.Blocks[b].Lines[i].Instr.OpCode == LuaOpcode.CLOSURE)
result += this.Blocks[b].Lines[i].FunctionRef.ScriptFunction.GetText(); // inline func in parent
result += (this.Blocks[b].StartAddress + i).ToString("0000") + $": {new string(' ', tabLevel)}" + this.Blocks[b].Lines[i].Text.Replace("\t", "");
}
result += new string('-', 50) + $" ({this.Blocks[b].JumpsTo}) \r\n";
if (b == this.Blocks.Count - 1)
result += "\r\n"; // keep it clean?
}
return result;
}
private string GenerateCleanCode()
{
string result = "";
for (int b = 0; b < this.Blocks.Count; b++)
{
for (int i = 0; i < this.Blocks[b].Lines.Count; i++)
{
if(this.Blocks[b].Lines[i].Instr.OpCode == LuaOpcode.CLOSURE)
if (this.Blocks[b].Lines[i].FunctionRef != null)
result += this.Blocks[b].Lines[i].FunctionRef.ScriptFunction.BeautifieCode(); // inline func in parent
//result += this.Blocks[b].Lines[i].FunctionRef.ScriptFunction.RealignText().Replace("\r\n",$"\r\n{new string('\t',1)}"); // inline func in parent
result += this.Blocks[b].Lines[i].Text; //.Replace("\t", "");
}
if (b == this.Blocks.Count - 1)
result += "\n\r"; // keep it clean?
}
return result;
}
public void Cleanlines()
{
for (int i = 0; i < this.Lines.Count; i++)
this.Lines[i].ClearLine();
}
public string BeautifieCode()
{
// text based because we did wanky things instead of respecting the list
int tabCount = 1;
string[] lines = Text.Replace("\r", "").Replace("\t", "").Split('\n');
string newText = "";
for (int i = 0; i < lines.Length; i++)
{
bool postAdd = false;
bool postSub = false;
if (lines[i].StartsWith("if") || lines[i].StartsWith("function") || lines[i].StartsWith("local function") || lines[i].StartsWith("for"))
postAdd = true;
else if (lines[i].StartsWith("else"))
{
if (i < lines.Length - 1 && lines[i + 1].StartsWith("if"))
{
// elseif
newText += $"{new string('\t', tabCount)}{lines[i]}{lines[i + 1]}\r\n";
i += 1; // brrrr fuck y'all, i skip next one this way!
continue;
}
else
{
// else
tabCount -= 1;
postAdd = true;
}
}
else if (lines[i].StartsWith("end"))
tabCount -= 1;
if (tabCount < 0)
tabCount = 0;
if (lines[i].StartsWith("if"))
newText += $"{new string('\t', tabCount)}{lines[i]}";
else if (lines[i].EndsWith("or") || lines[i].EndsWith("and") || lines[i].StartsWith(" not"))
newText += $"{lines[i]}";
else if (lines[i] == "")
newText += "";
else
newText += $"{new string('\t', tabCount)}{lines[i]}\r\n";
if (lines[i].EndsWith("then"))
newText += "\r\n";
if (postAdd)
tabCount += 1;
if (postSub)
tabCount -= 1;
}
return newText;
}
}
}