From 8336abfa6e8681759554cc18c20168d079d53e4f Mon Sep 17 00:00:00 2001 From: "Dixon, Evan" Date: Mon, 13 Jan 2020 17:07:05 -0600 Subject: [PATCH] Remove Java dependency --- .../PSMD/Projects/PsmdLuaProject.vb | 32 ++++----- .../SkyEditor.ROMEditor.Windows.vbproj | 7 +- .../Windows/Language.Designer.vb | 11 +-- .../Windows/Language.resx | 3 - SkyEditor.ROMEditor.sln | 18 ++++- SkyEditor.ROMEditor/Language.Designer.vb | 11 +-- SkyEditor.ROMEditor/Language.resx | 3 - SkyEditor.ROMEditor/ProcessManagement/Java.vb | 33 --------- .../JavaNotFoundException.vb | 9 --- .../ProcessManagement/UnluacManager.vb | 70 ------------------- .../ProcessManagement/unluac.vb | 28 -------- .../SkyEditor.ROMEditor.vbproj | 4 ++ .../SkyEditor.RomEditor.CSharp.csproj | 12 ++++ .../Utilities/LuaDecompiler.cs | 57 +++++++++++++++ .../SkyEditor.ROMEditor.BigTestsCSharp.csproj | 1 + ...kyEditor.ROMEditor.IntegrationTests.vbproj | 1 + .../SkyEditor.ROMEditor.Tests.vbproj | 1 + 17 files changed, 114 insertions(+), 187 deletions(-) delete mode 100644 SkyEditor.ROMEditor/ProcessManagement/Java.vb delete mode 100644 SkyEditor.ROMEditor/ProcessManagement/JavaNotFoundException.vb delete mode 100644 SkyEditor.ROMEditor/ProcessManagement/UnluacManager.vb delete mode 100644 SkyEditor.ROMEditor/ProcessManagement/unluac.vb create mode 100644 SkyEditor.RomEditor.CSharp/SkyEditor.RomEditor.CSharp.csproj create mode 100644 SkyEditor.RomEditor.CSharp/Utilities/LuaDecompiler.cs diff --git a/SkyEditor.ROMEditor.Windows/MysteryDungeon/PSMD/Projects/PsmdLuaProject.vb b/SkyEditor.ROMEditor.Windows/MysteryDungeon/PSMD/Projects/PsmdLuaProject.vb index 47a17a6..2e5501a 100644 --- a/SkyEditor.ROMEditor.Windows/MysteryDungeon/PSMD/Projects/PsmdLuaProject.vb +++ b/SkyEditor.ROMEditor.Windows/MysteryDungeon/PSMD/Projects/PsmdLuaProject.vb @@ -7,6 +7,7 @@ Imports SkyEditor.Core.Projects Imports SkyEditor.Core.Utilities Imports SkyEditor.ROMEditor.ProcessManagement Imports SkyEditor.ROMEditor.Projects +Imports SkyEditor.ROMEditor.Utilities Imports SkyEditor.Utilities.AsyncFor Namespace MysteryDungeon.PSMD.Projects @@ -351,24 +352,19 @@ Namespace MysteryDungeon.PSMD.Projects f.BatchSize = Environment.ProcessorCount * 2 - Try - Using unluac As New UnluacManager - Await f.RunForEach(Directory.GetFiles(scriptSource, "*.lua", SearchOption.AllDirectories), - Async Function(item As String) As Task - Dim dest = item.Replace(scriptSource, scriptDestination) - If Not Directory.Exists(Path.GetDirectoryName(dest)) Then - Directory.CreateDirectory(Path.GetDirectoryName(dest)) - End If - - Await unluac.DecompileScript(item, dest) - - File.Copy(dest, dest & ".original") - filesToOpen.Add(dest) - End Function) - End Using - Catch ex As JavaNotFoundException - ReportError(New ErrorInfo(Me) With {.Type = ErrorType.Error, .Message = My.Resources.Language.ProcessManagement_JavaNotFoundMessage}) - End Try + Await f.RunForEach(Directory.GetFiles(scriptSource, "*.lua", SearchOption.AllDirectories), + Sub(item As String) + Dim dest = item.Replace(scriptSource, 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.Copy(dest, dest & ".original") + filesToOpen.Add(dest) + End Sub) If AddScriptsToProject Then For Each item In filesToOpen diff --git a/SkyEditor.ROMEditor.Windows/SkyEditor.ROMEditor.Windows.vbproj b/SkyEditor.ROMEditor.Windows/SkyEditor.ROMEditor.Windows.vbproj index 6df3ae7..8613e06 100644 --- a/SkyEditor.ROMEditor.Windows/SkyEditor.ROMEditor.Windows.vbproj +++ b/SkyEditor.ROMEditor.Windows/SkyEditor.ROMEditor.Windows.vbproj @@ -119,7 +119,7 @@ ..\packages\SkyEditor.Core.4.2.10\lib\net462\SkyEditor.Core.dll - + ..\packages\SkyEditor.IO.5.0.8\lib\net462\SkyEditor.IO.dll @@ -254,6 +254,7 @@ My.Resources ResXFileCodeGenerator Language.Designer.vb + Designer VbMyResourcesResXFileCodeGenerator @@ -333,6 +334,10 @@ {e92df34a-5cf0-4f9c-9640-a16b7453270d} EveryFileExplorer + + {48b3e8de-de58-4a1e-9b70-7cd53a739b4d} + SkyEditor.RomEditor.CSharp + {f1b430d7-c00d-4673-8816-97c9df4376bc} SkyEditor.ROMEditor diff --git a/SkyEditor.ROMEditor.Windows/Windows/Language.Designer.vb b/SkyEditor.ROMEditor.Windows/Windows/Language.Designer.vb index cbfcc43..da59031 100644 --- a/SkyEditor.ROMEditor.Windows/Windows/Language.Designer.vb +++ b/SkyEditor.ROMEditor.Windows/Windows/Language.Designer.vb @@ -22,7 +22,7 @@ Namespace My.Resources ''' ''' A strongly-typed resource class, for looking up localized strings, etc. ''' - _ Friend Class Language @@ -508,15 +508,6 @@ Namespace My.Resources End Get End Property - ''' - ''' Looks up a localized string similar to Java could not be run on your system. Please ensure it's installed and accessible in the PATH environemnt variable.. - ''' - Friend Shared ReadOnly Property ProcessManagement_JavaNotFoundMessage() As String - Get - Return ResourceManager.GetString("ProcessManagement_JavaNotFoundMessage", resourceCulture) - End Get - End Property - ''' ''' Looks up a localized string similar to Pokémon Super Mystery Dungeon. ''' diff --git a/SkyEditor.ROMEditor.Windows/Windows/Language.resx b/SkyEditor.ROMEditor.Windows/Windows/Language.resx index ecc6ca0..302754c 100644 --- a/SkyEditor.ROMEditor.Windows/Windows/Language.resx +++ b/SkyEditor.ROMEditor.Windows/Windows/Language.resx @@ -329,7 +329,4 @@ evandixon (Personality test, bgp files) Starter Mod - - Java could not be run on your system. Please ensure it's installed and accessible in the PATH environemnt variable. - \ No newline at end of file diff --git a/SkyEditor.ROMEditor.sln b/SkyEditor.ROMEditor.sln index ad99645..a8b4fda 100644 --- a/SkyEditor.ROMEditor.sln +++ b/SkyEditor.ROMEditor.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27130.2024 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29613.14 MinimumVisualStudioVersion = 10.0.40219.1 Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "SkyEditor.ROMEditor.Windows", "SkyEditor.ROMEditor.Windows\SkyEditor.ROMEditor.Windows.vbproj", "{F05C12E8-EAB7-4B9E-A8D2-D21B80C1A646}" EndProject @@ -36,6 +36,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Misc", "Misc", "{924A8A27-C EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FarcFilenameHasher", "Misc\FarcFilenameHasher\FarcFilenameHasher.csproj", "{78E03B61-5B59-45B6-8388-F9F059199954}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkyEditor.RomEditor.CSharp", "SkyEditor.RomEditor.CSharp\SkyEditor.RomEditor.CSharp.csproj", "{48B3E8DE-DE58-4A1E-9B70-7CD53A739B4D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -138,6 +140,18 @@ Global {78E03B61-5B59-45B6-8388-F9F059199954}.Test|Any CPU.Build.0 = Debug|Any CPU {78E03B61-5B59-45B6-8388-F9F059199954}.Test|x86.ActiveCfg = Debug|Any CPU {78E03B61-5B59-45B6-8388-F9F059199954}.Test|x86.Build.0 = Debug|Any CPU + {48B3E8DE-DE58-4A1E-9B70-7CD53A739B4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {48B3E8DE-DE58-4A1E-9B70-7CD53A739B4D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {48B3E8DE-DE58-4A1E-9B70-7CD53A739B4D}.Debug|x86.ActiveCfg = Debug|Any CPU + {48B3E8DE-DE58-4A1E-9B70-7CD53A739B4D}.Debug|x86.Build.0 = Debug|Any CPU + {48B3E8DE-DE58-4A1E-9B70-7CD53A739B4D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {48B3E8DE-DE58-4A1E-9B70-7CD53A739B4D}.Release|Any CPU.Build.0 = Release|Any CPU + {48B3E8DE-DE58-4A1E-9B70-7CD53A739B4D}.Release|x86.ActiveCfg = Release|Any CPU + {48B3E8DE-DE58-4A1E-9B70-7CD53A739B4D}.Release|x86.Build.0 = Release|Any CPU + {48B3E8DE-DE58-4A1E-9B70-7CD53A739B4D}.Test|Any CPU.ActiveCfg = Debug|Any CPU + {48B3E8DE-DE58-4A1E-9B70-7CD53A739B4D}.Test|Any CPU.Build.0 = Debug|Any CPU + {48B3E8DE-DE58-4A1E-9B70-7CD53A739B4D}.Test|x86.ActiveCfg = Debug|Any CPU + {48B3E8DE-DE58-4A1E-9B70-7CD53A739B4D}.Test|x86.Build.0 = Debug|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SkyEditor.ROMEditor/Language.Designer.vb b/SkyEditor.ROMEditor/Language.Designer.vb index 2d72d8e..8e6ab29 100644 --- a/SkyEditor.ROMEditor/Language.Designer.vb +++ b/SkyEditor.ROMEditor/Language.Designer.vb @@ -22,7 +22,7 @@ Namespace My.Resources ''' ''' A strongly-typed resource class, for looking up localized strings, etc. ''' - _ Public Class Language @@ -121,15 +121,6 @@ Namespace My.Resources End Get End Property - ''' - ''' Looks up a localized string similar to Java could not be run on your system. Please ensure it's installed and accessible in the PATH environemnt variable.. - ''' - Public Shared ReadOnly Property ProcessManagement_JavaNotFoundMessage() As String - Get - Return ResourceManager.GetString("ProcessManagement_JavaNotFoundMessage", resourceCulture) - End Get - End Property - ''' ''' Looks up a localized string similar to The program "{0}" exited with an unsuccessful exit code: {1}.. ''' diff --git a/SkyEditor.ROMEditor/Language.resx b/SkyEditor.ROMEditor/Language.resx index da93ca7..03de3fa 100644 --- a/SkyEditor.ROMEditor/Language.resx +++ b/SkyEditor.ROMEditor/Language.resx @@ -139,9 +139,6 @@ evandixon (Personality test, bgp files) ROM Editor - - Java could not be run on your system. Please ensure it's installed and accessible in the PATH environemnt variable. - The program "{0}" exited with an unsuccessful exit code: {1}. diff --git a/SkyEditor.ROMEditor/ProcessManagement/Java.vb b/SkyEditor.ROMEditor/ProcessManagement/Java.vb deleted file mode 100644 index 18a68ba..0000000 --- a/SkyEditor.ROMEditor/ProcessManagement/Java.vb +++ /dev/null @@ -1,33 +0,0 @@ -Imports System.ComponentModel - -Namespace ProcessManagement - Public Class Java - Inherits ConsoleApp - - Public Sub New(jarFilename As String, arguments As String, Optional captureConsoleOutput As Boolean = True, Optional captureConsoleError As Boolean = True) - MyBase.New("java", - $"-jar ""{jarFilename}""" & - If( 'If arguments are present, add a space before them; otherwise, don't - Not String.IsNullOrEmpty(arguments), - $" ""{arguments}""", - ""), - captureConsoleOutput, captureConsoleError - ) - End Sub - - Protected Overrides Sub Start() - Try - MyBase.Start() - Catch ex As Win32Exception - If ex.NativeErrorCode = 2 Then - '2 means "file not found". Additional reading: https://msdn.microsoft.com/en-us/library/cc231199.aspx - Throw New JavaNotFoundException(ex) - Else - Throw - End If - End Try - End Sub - - End Class -End Namespace - diff --git a/SkyEditor.ROMEditor/ProcessManagement/JavaNotFoundException.vb b/SkyEditor.ROMEditor/ProcessManagement/JavaNotFoundException.vb deleted file mode 100644 index 14c93ef..0000000 --- a/SkyEditor.ROMEditor/ProcessManagement/JavaNotFoundException.vb +++ /dev/null @@ -1,9 +0,0 @@ -Namespace ProcessManagement - Public Class JavaNotFoundException - Inherits Exception - - Public Sub New(innerException As Exception) - MyBase.New(My.Resources.Language.ProcessManagement_JavaNotFoundMessage, innerException) - End Sub - End Class -End Namespace \ No newline at end of file diff --git a/SkyEditor.ROMEditor/ProcessManagement/UnluacManager.vb b/SkyEditor.ROMEditor/ProcessManagement/UnluacManager.vb deleted file mode 100644 index 3ec4a0c..0000000 --- a/SkyEditor.ROMEditor/ProcessManagement/UnluacManager.vb +++ /dev/null @@ -1,70 +0,0 @@ -Imports System.IO - -Namespace ProcessManagement - Public Class UnluacManager - Implements IDisposable - - Private Shared Function GetUnluacPath() As String - Dim tempPath = Path.Combine(Path.GetTempPath, "SkyEditor.ROMEditor-" & Guid.NewGuid.ToString(), "unluac.jar") - If Not Directory.Exists(Path.GetDirectoryName(tempPath)) Then - Directory.CreateDirectory(Path.GetDirectoryName(tempPath)) - End If - Return tempPath - End Function - - Public Sub New() - Filename = GetUnluacPath() - If Not File.Exists(Filename) Then - File.WriteAllBytes(Filename, My.Resources.Resources.unluac) - End If - End Sub - - Public ReadOnly Property Filename As String - - Public Async Function DecompileScript(sourceScriptFilename As String, destinationScriptFilename As String) As Task - Using unluac As New Unluac(Filename, sourceScriptFilename) - Await unluac.SaveAllOutput(destinationScriptFilename) - End Using - End Function - - Public Async Function DecompileScript(sourceScriptFilename As String) As Task(Of String) - Using unluac As New Unluac(Filename, sourceScriptFilename) - Return Await unluac.GetAllOutput() - End Using - End Function - -#Region "IDisposable Support" - Private disposedValue As Boolean ' To detect redundant calls - - ' IDisposable - Protected Overridable Sub Dispose(disposing As Boolean) - If Not disposedValue Then - If disposing Then - ' TODO: dispose managed state (managed objects). - File.Delete(Filename) - End If - - ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below. - ' TODO: set large fields to null. - End If - disposedValue = True - End Sub - - ' TODO: override Finalize() only if Dispose(disposing As Boolean) above has code to free unmanaged resources. - 'Protected Overrides Sub Finalize() - ' ' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above. - ' Dispose(False) - ' MyBase.Finalize() - 'End Sub - - ' This code added by Visual Basic to correctly implement the disposable pattern. - Public Sub Dispose() Implements IDisposable.Dispose - ' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above. - Dispose(True) - ' TODO: uncomment the following line if Finalize() is overridden above. - ' GC.SuppressFinalize(Me) - End Sub -#End Region - End Class -End Namespace - diff --git a/SkyEditor.ROMEditor/ProcessManagement/unluac.vb b/SkyEditor.ROMEditor/ProcessManagement/unluac.vb deleted file mode 100644 index 180dad3..0000000 --- a/SkyEditor.ROMEditor/ProcessManagement/unluac.vb +++ /dev/null @@ -1,28 +0,0 @@ -Imports System.IO -Imports SkyEditor.Core -Imports SkyEditor.Core.Utilities - -Namespace ProcessManagement - ''' - ''' Wrapper for unluac - ''' - Public Class Unluac - Inherits Java - - ''' - ''' Creates a new instance of the lua decompiler. - ''' - ''' Filename of the compiled lua script. - Public Sub New(jarFilename As String, scriptFilename As String) - MyBase.New(jarFilename, scriptFilename) - End Sub - - ''' - ''' Saves the result of the decompilation to the given file path - ''' - Public Async Function SaveAllOutput(destinationFilename As String) As Task - File.WriteAllText(destinationFilename, Await GetAllOutput()) - End Function - End Class -End Namespace - diff --git a/SkyEditor.ROMEditor/SkyEditor.ROMEditor.vbproj b/SkyEditor.ROMEditor/SkyEditor.ROMEditor.vbproj index 0bf9750..d9417cd 100644 --- a/SkyEditor.ROMEditor/SkyEditor.ROMEditor.vbproj +++ b/SkyEditor.ROMEditor/SkyEditor.ROMEditor.vbproj @@ -13,6 +13,10 @@ + + + + True diff --git a/SkyEditor.RomEditor.CSharp/SkyEditor.RomEditor.CSharp.csproj b/SkyEditor.RomEditor.CSharp/SkyEditor.RomEditor.CSharp.csproj new file mode 100644 index 0000000..963e638 --- /dev/null +++ b/SkyEditor.RomEditor.CSharp/SkyEditor.RomEditor.CSharp.csproj @@ -0,0 +1,12 @@ + + + + netcoreapp2.2;net472 + SkyEditor.RomEditor + + + + + + + diff --git a/SkyEditor.RomEditor.CSharp/Utilities/LuaDecompiler.cs b/SkyEditor.RomEditor.CSharp/Utilities/LuaDecompiler.cs new file mode 100644 index 0000000..9867405 --- /dev/null +++ b/SkyEditor.RomEditor.CSharp/Utilities/LuaDecompiler.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Text; +using unluac.decompile; +using unluac.parse; +using unluacNet; + +namespace SkyEditor.RomEditor.Utilities +{ + public class LuaDecompiler + { + public static string DecompileScript(byte[] compiledScript) + { + var function = LoadScript(compiledScript); + var decompiler = new Decompiler(function); + decompiler.decompile(); + + var output = new StringOutput(); + decompiler.print(output); + + return output.GetOutput(); + } + + private static LFunction LoadScript(byte[] compiledScript) + { + var fileData = new ByteBuffer(compiledScript); + var header = new BHeader(fileData); + + return header.function.parse(fileData, header); + } + + private class StringOutput : OutputProvider + { + public StringOutput() + { + output = new StringBuilder(); + } + + private readonly StringBuilder output; + + public void print(string s) + { + output.Append(s); + } + + public void println() + { + output.AppendLine(); + } + + public string GetOutput() + { + return output.ToString(); + } + } + } +} diff --git a/Tests/SkyEditor.ROMEditor.BigTestsCSharp/SkyEditor.ROMEditor.BigTestsCSharp.csproj b/Tests/SkyEditor.ROMEditor.BigTestsCSharp/SkyEditor.ROMEditor.BigTestsCSharp.csproj index 86e934e..9ad6e8c 100644 --- a/Tests/SkyEditor.ROMEditor.BigTestsCSharp/SkyEditor.ROMEditor.BigTestsCSharp.csproj +++ b/Tests/SkyEditor.ROMEditor.BigTestsCSharp/SkyEditor.ROMEditor.BigTestsCSharp.csproj @@ -59,6 +59,7 @@ ..\..\packages\SkyEditor.IO.5.0.8\lib\net462\SkyEditor.IO.dll + True ..\..\packages\SkyEditor.Utilities.AsyncFor.5.0.0\lib\netstandard2.0\SkyEditor.Utilities.AsyncFor.dll diff --git a/Tests/SkyEditor.ROMEditor.IntegrationTests/SkyEditor.ROMEditor.IntegrationTests.vbproj b/Tests/SkyEditor.ROMEditor.IntegrationTests/SkyEditor.ROMEditor.IntegrationTests.vbproj index 7ed5451..5dd81fe 100644 --- a/Tests/SkyEditor.ROMEditor.IntegrationTests/SkyEditor.ROMEditor.IntegrationTests.vbproj +++ b/Tests/SkyEditor.ROMEditor.IntegrationTests/SkyEditor.ROMEditor.IntegrationTests.vbproj @@ -80,6 +80,7 @@ ..\..\packages\SkyEditor.IO.5.0.8\lib\net462\SkyEditor.IO.dll + True ..\..\packages\SkyEditor.Utilities.AsyncFor.5.0.0\lib\netstandard2.0\SkyEditor.Utilities.AsyncFor.dll diff --git a/Tests/SkyEditor.ROMEditor.Tests/SkyEditor.ROMEditor.Tests.vbproj b/Tests/SkyEditor.ROMEditor.Tests/SkyEditor.ROMEditor.Tests.vbproj index ea26691..9afab9e 100644 --- a/Tests/SkyEditor.ROMEditor.Tests/SkyEditor.ROMEditor.Tests.vbproj +++ b/Tests/SkyEditor.ROMEditor.Tests/SkyEditor.ROMEditor.Tests.vbproj @@ -69,6 +69,7 @@ ..\..\packages\SkyEditor.IO.5.0.8\lib\net462\SkyEditor.IO.dll + True ..\..\packages\SkyEditor.Utilities.AsyncFor.5.0.0\lib\netstandard2.0\SkyEditor.Utilities.AsyncFor.dll