-
Notifications
You must be signed in to change notification settings - Fork 29
/
HexToPseudoCode.cpp
85 lines (67 loc) · 2.31 KB
/
HexToPseudoCode.cpp
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
#include <iostream>
#include <sstream>
#include "UPKUtils.h"
#include "UToken.h"
using namespace std;
string GetFilename(string str)
{
unsigned found = str.find_last_of("/\\");
return str.substr(found + 1);
}
int main(int argN, char* argV[])
{
//cout << "HexToPseudoCode by wghost81 aka Wasteland Ghost" << endl;
if (argN < 3 || argN > 4)
{
cerr << "Usage: HexToPseudoCode UnpackedResourceFile.upk ObjectName [/d]" << endl;
return 1;
}
UPKUtils package(argV[1]);
UPKReadErrors err = package.GetError();
if (err != UPKReadErrors::NoErrors)
{
cerr << "Error reading package:\n" << FormatReadErrors(err);
if (package.IsCompressed())
cerr << "Compression flags:\n" << FormatCompressionFlags(package.GetCompressionFlags());
return 1;
}
string NameToFind = argV[2];
//cout << "Object to find: " << NameToFind << endl;
UObjectReference ObjRef = package.FindObject(NameToFind, false);
if (ObjRef == 0)
{
cerr << "Unable to find object entry by name " << NameToFind << endl;
return 1;
}
if (ObjRef > 0 && argN == 4 && string(argV[3]) == "/d")
{
package.SaveExportData((uint32_t)ObjRef);
}
if (ObjRef > 0)
{
//cout << "Found Export Object:\n" << package.FormatExport(ObjRef, true);
}
else
{
//cout << "Found Import Object:\n" << package.FormatImport(-ObjRef, true);
}
if (ObjRef <= 0 || (package.GetExportEntry(ObjRef).Type != "Function" && package.GetExportEntry(ObjRef).Type != "State"))
{
cerr << "Object is not a Function nor a State, can not convert to pseudo-code!\n";
return 1;
}
//cout << "Attempting deserialization:\n";
vector<char> ObjData = package.GetExportData(ObjRef);
stringstream stream;
stream.write(ObjData.data(), ObjData.size());
size_t ScrPos = package.GetScriptRelOffset(ObjRef);
stream.seekg(ScrPos);
UScriptCode ScrCode;
string PseudoCode = ScrCode.Deserialize(stream, package);
cout << "//This script was generated by HexToPseudoCode decompiler for use with PatchUPK/PatcherGUI tool\n"
<< "UPK_FILE = " << GetFilename(argV[1]) << "\n"
<< "OBJECT = " << NameToFind << " : AUTO\n"
<< "[REPLACEMENT_CODE]\n"
<< PseudoCode;
return 0;
}