Skip to content

Commit

Permalink
Fix lua decompilation
Browse files Browse the repository at this point in the history
  • Loading branch information
evandixon committed Mar 9, 2020
1 parent 608c279 commit 3626afa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Expand Up @@ -341,7 +341,7 @@ Namespace MysteryDungeon.PSMD.Projects
Me.Message = My.Resources.Language.LoadingDecompilingScripts
Me.Progress = 0

Dim scriptSource As String = Path.Combine(Me.GetRawFilesDir, "romfs", "script")
Dim scriptSourceDir As String = Path.Combine(Me.GetRawFilesDir, "romfs", "script")
Dim scriptDestination As String = Path.Combine(Me.GetRootDirectory, "script")
Dim filesToOpen As New ConcurrentBag(Of String)

Expand All @@ -352,15 +352,15 @@ Namespace MysteryDungeon.PSMD.Projects

f.BatchSize = Environment.ProcessorCount * 2

Await f.RunForEach(Directory.GetFiles(scriptSource, "*.lua", SearchOption.AllDirectories),
Sub(item As String)
Dim dest = item.Replace(scriptSource, scriptDestination)
Await f.RunForEach(Directory.GetFiles(scriptSourceDir, "*.lua", SearchOption.AllDirectories),
Sub(scriptSource As String)
Dim dest = scriptSource.Replace(scriptSourceDir, scriptDestination)
If Not Directory.Exists(Path.GetDirectoryName(dest)) Then
Directory.CreateDirectory(Path.GetDirectoryName(dest))
End If

Dim decompiledScript = LuaDecompiler.DecompileScript(File.ReadAllBytes(scriptSource))
File.WriteAllText(scriptDestination, decompiledScript)
File.WriteAllText(dest, decompiledScript)

File.Copy(dest, dest & ".original")
filesToOpen.Add(dest)
Expand Down
17 changes: 14 additions & 3 deletions SkyEditor.RomEditor.CSharp/Utilities/LuaDecompiler.cs
Expand Up @@ -15,10 +15,21 @@ public static string DecompileScript(byte[] compiledScript)
var decompiler = new Decompiler(function);
decompiler.decompile();

var output = new StringOutput();
decompiler.print(output);
try
{
var output = new StringOutput();
decompiler.print(output);

return output.GetOutput();
return output.GetOutput();
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.ToString());
return $"--[=====[\n" +
$"Encountered exception in LuaDecompiler.DecompileScript:" +
$"{ex}\n" +
$"--]=====]";
}
}

private static LFunction LoadScript(byte[] compiledScript)
Expand Down

0 comments on commit 3626afa

Please sign in to comment.