-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
125 lines (100 loc) · 4.75 KB
/
Program.cs
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
using luadec.IR;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Function = luadec.IR.Function;
namespace luadec
{
class Program
{
public static int SsaWrongcount = 0;
public static int BlockAlreadyCodegenned = 0;
public static int BlockNotUsed = 0;
static void Main(string[] args)
{
Console.WriteLine("MDELuaDecompiler based on CoD Havok Decompiler made from katalash's DSLuaDecompiler");
var files = new List<string>();
foreach (var arg in args)
{
var attr = File.GetAttributes(arg);
// determine if we're a directory first
// if so only includes file that are of ".lua" or ".luac" extension
if (attr.HasFlag(FileAttributes.Directory))
{
files.AddRange(Directory.GetFiles(arg, "*.lua*", SearchOption.AllDirectories).ToList());
}
else if (Path.GetExtension(arg).Contains(".lua"))
{
files.Add(arg);
}
else
{
Console.WriteLine($"Invalid argument passed {arg} | {File.GetAttributes(arg)}!");
}
}
// make sure to remove duplicates
files = files.Distinct().ToList();
// also remove any already dumped files
files.RemoveAll(elem => elem.EndsWith(".dec.lua"));
// if we ever want to pursue directory structure
//if (!Directory.Exists("output"))
//{
// Directory.CreateDirectory("output");
//}
Console.WriteLine($"Total of {files.Count} to process.");
var count = 0;
var errors = 0; // TODO: ??
foreach (var filePath in files)
{
Console.WriteLine($"Decompiling file {filePath}");
var output = MDELuaDecompiler.LuaFileTypes.LuaFile.LoadLuaFile(filePath, new MemoryStream(File.ReadAllBytes(filePath)));
// TODO: ??
Function.DebugIDCounter = 0;
Function.IndentLevel = 0;
LuaDisassembler.SymbolTable = new SymbolTable();
var main = new IR.Function();
output.GenerateIR(main, output.MainFunction);
//var outputPath = Path.GetFileNameWithoutExtension(filePath) + ".dec.lua";
//var outputPath = Path.GetDirectoryName(filePath) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(filePath) + ".dec.lua";
var outputPath = "Out" + Path.DirectorySeparatorChar + Path.GetDirectoryName(filePath) + Path.DirectorySeparatorChar;
var outputName = Path.GetFileName(filePath);
if (!Directory.Exists(Path.GetDirectoryName(outputPath)));
{
Directory.CreateDirectory(outputPath);
}
File.WriteAllText(outputPath + outputName, main.ToString());
count++;
//try
//{
// var output = MDELuaDecompiler.LuaFileTypes.LuaFile.LoadLuaFile(filePath, new MemoryStream(File.ReadAllBytes(filePath)));
// // TODO: ??
// Function.DebugIDCounter = 0;
// Function.IndentLevel = 0;
// LuaDisassembler.SymbolTable = new SymbolTable();
// var main = new IR.Function();
// output.GenerateIR(main, output.MainFunction);
// var outputPath = Path.GetDirectoryName(filePath) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(filePath) + ".dec.lua";
// File.WriteAllText(outputPath, main.ToString());
// count++;
//}
//catch (Exception e)
//{
// errors++;
// var tempColor = Console.ForegroundColor;
// Console.ForegroundColor = ConsoleColor.Red;
// Console.WriteLine($"ERROR: Decompilation Failure -- {e.Message}, no file generated.");
// Console.ForegroundColor = tempColor;
//}
}
var prevColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Blue;
/*Console.WriteLine($"ssaWrongcount: {SsaWrongcount}");
Console.WriteLine($"blockAlreadyCodegenned: {BlockAlreadyCodegenned}");
Console.WriteLine($"BlockNotUsed: {BlockNotUsed}");*/
Console.WriteLine($"Decompilation complete! Processed {count} files with {errors} errors.");
Console.ForegroundColor = prevColor;
}
}
}