Skip to content

Commit

Permalink
Getting ready for 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
user committed Oct 16, 2022
1 parent d0f230c commit 89f70da
Show file tree
Hide file tree
Showing 18 changed files with 116 additions and 138 deletions.
2 changes: 1 addition & 1 deletion GTA V Script Decompiler/Ast/AstToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class AstToken
public virtual ref TypeContainer GetTypeContainer()
{
if (CanGetGlobalIndex())
return ref Program.globalTypeMgr.GetGlobalType(GetGlobalIndex());
return ref Program.GlobalTypeMgr.GetGlobalType(GetGlobalIndex());

dummyContainer = new();
return ref dummyContainer;
Expand Down
2 changes: 1 addition & 1 deletion GTA V Script Decompiler/Ast/NativeCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal class NativeCall : FunctionCallBase
public NativeCall(Function func, List<AstToken> arguments, string name, ulong hash, int returnCount) : base(func, arguments)
{
Name = name;
Entry = Program.nativeDB.GetEntry(hash);
Entry = Program.NativeDB.GetEntry(hash);
ReturnCount = returnCount;

var i = 0;
Expand Down
2 changes: 1 addition & 1 deletion GTA V Script Decompiler/Ast/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public override string ToString()
{
var hash = Utils.Joaat(function.ScriptFile.StringTable[(int)(Index as ConstantInt).GetValue()]);

if (hash != 0x3acbce85 /*STRING*/ && Program.textDB.Strings.TryGetValue(hash, out var text))
if (hash != 0x3acbce85 /*STRING*/ && Program.TextDB.Strings.TryGetValue(hash, out var text))
{
return Properties.Settings.Default.LocalizedTextType == 1
? $"_(\"{text.Replace("\"", "\\\"")}\")"
Expand Down
50 changes: 50 additions & 0 deletions GTA V Script Decompiler/Crossmap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;

namespace Decompiler
{
internal class Crossmap
{
public Dictionary<ulong, ulong> TranslationTable = new();

public ulong TranslateHash(ulong hash)
{
TranslationTable.TryGetValue(hash, out hash);
return hash;
}

public Crossmap()
: base()
{
StreamReader sr;
sr = new StreamReader(new MemoryStream(Properties.Resources.CrossmapFile));

while (!sr.EndOfStream)
{
var line = sr.ReadLine();
if (line.Length > 1)
{
var val = line.Remove(line.IndexOfAny(new char[] { ':', '=', ',' }));
var nat = line[(val.Length + 1)..];

if (val.StartsWith("0x"))
val = val[2..];

if (nat.StartsWith("0x"))
nat = nat[2..];

if (ulong.TryParse(val.Trim(), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var newer))
{
if (ulong.TryParse(nat.Trim(), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var older))
{
TranslationTable.Add(newer, older);
}
}
}
}

sr.Close();
}
}
}
1 change: 1 addition & 0 deletions GTA V Script Decompiler/GTA V Script Decompiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<PropertyGroup>
<ApplicationIcon>GTA V Icon.ico</ApplicationIcon>
<Nullable>annotations</Nullable>
<AnalysisLevel>preview-all</AnalysisLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<OutputPath>bin\x64\Debug\</OutputPath>
Expand Down
13 changes: 8 additions & 5 deletions GTA V Script Decompiler/GlobalTypeMgr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ public GlobalTypeMgr()

public ref TypeContainer GetGlobalType(int idx)
{
if (GlobalTypes.TryGetValue(idx, out var type))
return ref type.GetContainer();

GlobalTypes[idx] = new();
return ref GlobalTypes[idx].GetContainer();
lock (GlobalTypes)
{
if (GlobalTypes.TryGetValue(idx, out var type))
return ref type.GetContainer();

GlobalTypes[idx] = new();
return ref GlobalTypes[idx].GetContainer();
}
}

public void HintGlobalType(int idx, ref TypeContainer container)
Expand Down
2 changes: 1 addition & 1 deletion GTA V Script Decompiler/Hashes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void Export_Entities()

public string GetHash(uint value)
{
if (Program.shouldReverseHashes && value > 200)
if (Program.ShouldReverseHashes && value > 200)
{
var intvalue = (int)value;
if (hashes.TryGetValue(intvalue, out var val))
Expand Down
4 changes: 2 additions & 2 deletions GTA V Script Decompiler/Instruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ public float GetImmFloatPush

public bool IsJumpInstruction
{
get { return (int)OriginalOpcode is>84 and <93; }
get { return (int)OriginalOpcode is >84 and <93; }
}

public bool IsConditionJump
{
get { return (int)Opcode is>85 and <93; }
get { return (int)Opcode is >85 and <93; }
}

public bool IsWhileJump
Expand Down
28 changes: 10 additions & 18 deletions GTA V Script Decompiler/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ private async Task Decompile(string directory, ProgressBar progressBar)
{
string scriptToDecode;

lock (Program.ThreadLock)
lock (CompileList)
{
scriptToDecode = CompileList.Dequeue();
}
Expand Down Expand Up @@ -336,7 +336,7 @@ private void reverseHashesToolStripMenuItem_Click(object sender, EventArgs e)
{
reverseHashesToolStripMenuItem.Checked = !reverseHashesToolStripMenuItem.Checked;
Properties.Settings.Default.ReverseHashes = reverseHashesToolStripMenuItem.Checked;
Program.shouldReverseHashes = reverseHashesToolStripMenuItem.Checked;
Program.ShouldReverseHashes = reverseHashesToolStripMenuItem.Checked;
Properties.Settings.Default.Save();
}

Expand All @@ -351,7 +351,7 @@ private void shiftVariablesToolStripMenuItem_Click(object sender, EventArgs e)
{
shiftVariablesToolStripMenuItem.Checked = !shiftVariablesToolStripMenuItem.Checked;
Properties.Settings.Default.ShiftVariables = shiftVariablesToolStripMenuItem.Checked;
Program.shouldShiftVariables = shiftVariablesToolStripMenuItem.Checked;
Program.ShouldShiftVariables = shiftVariablesToolStripMenuItem.Checked;
Properties.Settings.Default.Save();
}

Expand All @@ -370,19 +370,10 @@ private void showFuncPointerToolStripMenuItem_Click(object sender, EventArgs e)
Properties.Settings.Default.Save();
}

private void RebuildNativeFiles()
{
if (Program.x64nativefile != null)
{
Program.x64nativefile = new x64NativeFile();
}
}

private void includeNativeNamespaceToolStripMenuItem_Click(object sender, EventArgs e)
{
includeNativeNamespaceToolStripMenuItem.Checked = !includeNativeNamespaceToolStripMenuItem.Checked;
Properties.Settings.Default.ShowNativeNamespace = includeNativeNamespaceToolStripMenuItem.Checked;
RebuildNativeFiles();
Properties.Settings.Default.Save();
}

Expand Down Expand Up @@ -418,7 +409,6 @@ private void uppercaseNativesToolStripMenuItem_Click(object sender, EventArgs e)
{
uppercaseNativesToolStripMenuItem.Checked = !uppercaseNativesToolStripMenuItem.Checked;
Properties.Settings.Default.UppercaseNatives = uppercaseNativesToolStripMenuItem.Checked;
RebuildNativeFiles();
Properties.Settings.Default.Save();
}

Expand All @@ -444,7 +434,7 @@ private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
if (e.Column == fpnColumnSorter.SortColumn)
{
// Reverse the current sort direction for this column.
fpnColumnSorter.Order =fpnColumnSorter.Order == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending;
fpnColumnSorter.Order = fpnColumnSorter.Order == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending;
}
else
{
Expand All @@ -461,15 +451,15 @@ private void toolStripButton1_Click(object sender, EventArgs e)
{
opening = !opening;

panel1.Size =!opening ? new Size(0, panel1.Size.Height) : new Size(240, panel1.Size.Height);
panel1.Size = !opening ? new Size(0, panel1.Size.Height) : new Size(240, panel1.Size.Height);

columnHeader1.Width = 80;
columnHeader2.Width = 76;
}

#endregion

private void resetGlobalTypesToolStripMenuItem_Click(object sender, EventArgs e) => Program.globalTypeMgr.Reset();
private void resetGlobalTypesToolStripMenuItem_Click(object sender, EventArgs e) => Program.GlobalTypeMgr.Reset();

private void entitiesToolStripMenuItem_Click(object sender, EventArgs e) => ScriptFile.HashBank.Export_Entities();

Expand Down Expand Up @@ -506,9 +496,11 @@ private void fctb1_MouseClick(object sender, MouseEventArgs e)

if (e.Button == MouseButtons.Right)
{
var pos = fctb1.PointToPosition(fctb1.PointToClient(Cursor.Position));

if (fctb1.SelectionLength == 0)
{
fctb1.SelectionStart = fctb1.PointToPosition(fctb1.PointToClient(Cursor.Position));
fctb1.SelectionStart = pos;
}

cmsText.Show();
Expand Down Expand Up @@ -552,7 +544,7 @@ private void cmsText_Opening(object sender, CancelEventArgs e)
e.Cancel = true;
}

private bool islegalchar(char c) => char.IsLetterOrDigit(c) ||c == '_';
private bool islegalchar(char c) => char.IsLetterOrDigit(c) || c == '_';

private string GetWordAtCursor()
{
Expand Down
58 changes: 0 additions & 58 deletions GTA V Script Decompiler/NativeFiles.cs

This file was deleted.

9 changes: 5 additions & 4 deletions GTA V Script Decompiler/NativeTables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Decompiler
{
public class X64NativeTable
public class NativeTable
{
public List<string> NativeNames;
public List<ulong> NativeHashes;

public X64NativeTable(Stream scriptFile, int position, int length, int codeSize)
public NativeTable(Stream scriptFile, int position, int length, int codeSize)
{
IO.Reader reader = new(scriptFile);
var count = 0;
Expand All @@ -24,9 +24,9 @@ public X64NativeTable(Stream scriptFile, int position, int length, int codeSize)
// or the earliest game version that native was introduced in.
// Just some of the steps Rockstar take to make reverse engineering harder

nat = Program.x64nativefile.TranslateHash(Rotl(reader.ReadUInt64(), codeSize + count));
nat = Program.Crossmap.TranslateHash(Rotl(reader.ReadUInt64(), codeSize + count));
NativeHashes.Add(nat);
var entry = Program.nativeDB.GetEntry(nat);
var entry = Program.NativeDB.GetEntry(nat);

if (entry != null)
{
Expand Down Expand Up @@ -78,6 +78,7 @@ private static ulong Rotl(ulong hash, int rotate)
rotate %= 64;
return (hash << rotate) | (hash >> (64 - rotate));
}

public ulong GetNativeHashFromIndex(int index)
{
return index < 0
Expand Down
35 changes: 16 additions & 19 deletions GTA V Script Decompiler/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,33 @@ namespace Decompiler
{
internal static class Program
{
public static x64NativeFile x64nativefile;
public static object ThreadLock;
public static NativeDB nativeDB;
public static FunctionDB functionDB;
public static TextDB textDB;
public static GlobalTypeMgr globalTypeMgr;
public static Crossmap Crossmap;
public static NativeDB NativeDB;
public static FunctionDB FunctionDB;
public static TextDB TextDB;
public static GlobalTypeMgr GlobalTypeMgr;

public static bool shouldShiftVariables = Properties.Settings.Default.ShiftVariables;
public static bool shouldReverseHashes = Properties.Settings.Default.ReverseHashes;
public static bool ShouldShiftVariables = Properties.Settings.Default.ShiftVariables;
public static bool ShouldReverseHashes = Properties.Settings.Default.ReverseHashes;

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main(string[] args)
{
ThreadLock = new object();

x64nativefile = new x64NativeFile();
Crossmap = new Crossmap();

ScriptFile.HashBank = new Hashes();

nativeDB = new NativeDB();
nativeDB.LoadData();
NativeDB = new NativeDB();
NativeDB.LoadData();

functionDB = new FunctionDB();
FunctionDB = new FunctionDB();

textDB = new TextDB();
TextDB = new TextDB();

globalTypeMgr = new GlobalTypeMgr();
GlobalTypeMgr = new GlobalTypeMgr();

if (args.Length == 0)
{
Expand All @@ -58,8 +55,7 @@ private static void Main(string[] args)
try
{
fileopen = new ScriptFile(File.OpenRead(args[0]));
var task = fileopen.Decompile();
task.Wait();
fileopen.Decompile().Wait();
}
catch (Exception ex)
{
Expand All @@ -69,7 +65,8 @@ private static void Main(string[] args)

Console.WriteLine("Decompiled in " + (DateTime.Now - Start).ToString());
fileopen.Save(File.OpenWrite(args[0] + ".c"), true);
Console.WriteLine("Extracing native table...");

Console.WriteLine("Extracting native table...");
StreamWriter fw = new(File.OpenWrite(args[0] + " native table.txt"));
foreach (var nat in fileopen.X64NativeTable.NativeHashes)
{
Expand Down
Loading

0 comments on commit 89f70da

Please sign in to comment.