Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Java installation removal #119

Merged
merged 3 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Resources/Misc/Lysis/IKVM.OpenJDK.Core.dll
Binary file not shown.
Binary file added Resources/Misc/Lysis/IKVM.OpenJDK.Util.dll
Binary file not shown.
Binary file added Resources/Misc/Lysis/IKVM.Runtime.dll
Binary file not shown.
Binary file added Resources/Misc/Lysis/LysisDecompiler.exe
Binary file not shown.
Binary file added Resources/Misc/Lysis/lysis-java.dll
Binary file not shown.
Binary file removed Resources/Misc/Lysis/lysis-java.jar
Binary file not shown.
5 changes: 1 addition & 4 deletions Spcode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@
<Compile Include="Utils\Models\Hotkey.cs" />
<Compile Include="Utils\Models\HotkeyInfo.cs" />
<Compile Include="Utils\HotkeyUtils.cs" />
<Compile Include="Utils\JavaInstallation.cs" />
<Compile Include="Utils\NamesHelper.cs" />
<Compile Include="Utils\ObjectBrowserUtils.cs" />
<Compile Include="Utils\Paths.cs" />
Expand Down Expand Up @@ -593,9 +592,7 @@
<Version>1.1.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\Translations\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PreBuildEvent>cd ..\..\Deploy\
Expand Down
12 changes: 10 additions & 2 deletions UI/MainWindow/MainWindowCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -609,15 +609,23 @@ private void Command_TidyCode(bool All)
/// </summary>
private async void Command_Decompile()
{
ProgressDialogController? msg = null;
try
{
var decomp = new DecompileUtil();
await decomp.DecompilePlugin();
var file = DecompileUtil.GetFile();
msg = await this.ShowProgressAsync(Translate("Decompiling") + "...", file.Name, false, MetroDialogOptions);
msg.SetIndeterminate();
ProcessUITasks();
TryLoadSourceFile(DecompileUtil.GetDecompiledPlugin(file), out _);
}
catch (Exception ex)
{
await this.ShowMessageAsync(Translate("Error"), ex.Message, settings: MetroDialogOptions);
}
finally
{
await msg.CloseAsync();
}
}

/// <summary>
Expand Down
45 changes: 25 additions & 20 deletions UI/MainWindow/MainWindowObjectBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ private async void OBItemDecompile_Click(object sender, RoutedEventArgs e)
var selectedItemFile = ((ObjectBrowser.SelectedItem as TreeViewItem)?.Tag as ObjectBrowserTag)?.Value;
if (selectedItemFile != null)
{
await new DecompileUtil().DecompilePlugin(selectedItemFile);
var fInfo = new FileInfo(selectedItemFile);
var msg = await this.ShowProgressAsync(Translate("Decompiling") + "...", fInfo.Name, false, MetroDialogOptions);
msg.SetIndeterminate();
ProcessUITasks();
TryLoadSourceFile(DecompileUtil.GetDecompiledPlugin(fInfo), out _);
await msg.CloseAsync();
}
}

Expand All @@ -160,26 +165,26 @@ private void OBItemRename_Click(object sender, RoutedEventArgs e)
ObjectBrowser.ContextMenu = null;

// If we didn't receive an empty name...
if (!string.IsNullOrEmpty(renameWindow.NewName))
if (string.IsNullOrEmpty(renameWindow.NewName))
{
var oldFileInfo = new FileInfo(fileTag.Value);
var newFileInfo = new FileInfo(oldFileInfo.DirectoryName + @"\" + renameWindow.NewName);
return;
}
var oldFileInfo = new FileInfo(fileTag.Value);
var newFileInfo = new FileInfo(oldFileInfo.DirectoryName + @"\" + renameWindow.NewName);

// Rename file
File.Move(oldFileInfo.FullName, newFileInfo.FullName);
// Rename file
File.Move(oldFileInfo.FullName, newFileInfo.FullName);

// If the new extension is not supported by SPCode, remove it from object browser
// else, rename and update the item
if (!FileIcons.ContainsKey(newFileInfo.Extension))
{
file.Visibility = Visibility.Collapsed;
return;
}
else
{
fileTag.Value = newFileInfo.FullName;
file.Header = BuildTreeViewItemContent(renameWindow.NewName, FileIcons[newFileInfo.Extension]);
}
// If the new extension is not supported by SPCode, remove it from object browser
// else, rename and update the item
if (!FileIcons.ContainsKey(newFileInfo.Extension))
{
file.Visibility = Visibility.Collapsed;
}
else
{
fileTag.Value = newFileInfo.FullName;
file.Header = BuildTreeViewItemContent(renameWindow.NewName, FileIcons[newFileInfo.Extension]);
}
}
}
Expand Down Expand Up @@ -578,8 +583,8 @@ private void ChangeObjectBrowserToDrives()
}

/// <summary>
/// <para> Helper function to build an expanded item's contents. </para>
/// <para> It outs a TreeViewItem list to be used when using the Reload function to keep directories expanded after refreshing. </para>
/// Helper function to build an expanded item's contents. <br/>
/// It outs a TreeViewItem list to be used when using the Reload function to keep directories expanded after refreshing.
/// </summary>
/// <param name="dir">Directory to fetch contents from.</param>
/// <param name="itemsToExpand">List of items that were expanded before calling this function to reload the Object Browser items.</param>
Expand Down
1 change: 1 addition & 0 deletions Utils/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public static class Constants
public const string ErrorFilterRegex = @"^(?<File>.+?)\((?<Line>[0-9]+(\s*--\s*[0-9]+)?)\)\s*:\s*(?<Type>[a-zA-Z]+\s+([a-zA-Z]+\s+)?[0-9]+)\s*:(?<Details>.+)";
public const string FileSaveFilters = @"Sourcepawn Files (*.sp *.inc)|*.sp;*.inc|All Files (*.*)|*.*";
public const string FileOpenFilters = @"Sourcepawn Files (*.sp *.inc)|*.sp;*.inc|Sourcemod Plugins (*.smx)|*.smx|All Files (*.*)|*.*";
public static string DecompileFileFilters = "Sourcepawn Plugins (*.smx)|*.smx";
#endregion

#region Other
Expand Down
149 changes: 43 additions & 106 deletions Utils/DecompileUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,130 +2,67 @@
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using MahApps.Metro.Controls.Dialogs;
using Microsoft.Win32;
using SPCode.UI;
using static SPCode.Interop.TranslationProvider;
using static SPCode.Utils.JavaInstallation;

namespace SPCode.Utils
{
public class DecompileUtil
public static class DecompileUtil
{
public async Task DecompilePlugin(string filePath = null)
public static FileInfo GetFile()
{
var java = new JavaInstallation();
string fileToDecompile;

// First we check the java version of the user, and act accordingly
ProgressDialogController checkingJavaDialog = null;
if (Program.MainWindow != null)
var ofd = new OpenFileDialog
{
checkingJavaDialog = await Program.MainWindow.ShowProgressAsync(Translate("JavaInstallCheck") + "...",
"", false, Program.MainWindow.MetroDialogOptions);
MainWindow.ProcessUITasks();
}
switch (java.GetJavaStatus())
Filter = Constants.DecompileFileFilters,
Title = Translate("ChDecomp")
};
var result = ofd.ShowDialog();
fileToDecompile = result.Value && !string.IsNullOrWhiteSpace(ofd.FileName) ? ofd.FileName : null;

if (fileToDecompile == null)
{
case JavaResults.Absent:
{
// If java is not installed, offer to download it
await checkingJavaDialog.CloseAsync();
if (await Program.MainWindow.ShowMessageAsync(Translate("JavaNotFoundTitle"),
Translate("JavaNotFoundMessage"),
MessageDialogStyle.AffirmativeAndNegative, Program.MainWindow.MetroDialogOptions) == MessageDialogResult.Affirmative)
{
await java.InstallJava();
}
return;
}
case JavaResults.Outdated:
{
// If java is outdated, offer to upgrade it
await checkingJavaDialog.CloseAsync();
if (await Program.MainWindow.ShowMessageAsync(Translate("JavaOutdatedTitle"),
Translate("JavaOutdatedMessage"),
MessageDialogStyle.AffirmativeAndNegative, Program.MainWindow.MetroDialogOptions) == MessageDialogResult.Affirmative)
{
await java.InstallJava();
}
return;
}
case JavaResults.Correct:
{
// Move on
await checkingJavaDialog.CloseAsync();
break;
}
return null;
}
return new FileInfo(fileToDecompile);
}

string fileToDecompile;
if (filePath == null)
public static string GetDecompiledPlugin(FileInfo file)
{
var decompilerPath = Paths.GetLysisDirectory();
var destFile = file.FullName + ".sp";
var standardOutput = new StringBuilder();
using var process = new Process();
var si = new ProcessStartInfo
{
var ofd = new OpenFileDialog
{
Filter = "Sourcepawn Plugins (*.smx)|*.smx",
Title = Translate("ChDecomp")
};
var result = ofd.ShowDialog();
fileToDecompile = result.Value && !string.IsNullOrWhiteSpace(ofd.FileName) ? ofd.FileName : null;
}
else
WorkingDirectory = decompilerPath,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
FileName = "cmd.exe",
Arguments = $"/c LysisDecompiler.exe \"{file.FullName}\"",
};
process.StartInfo = si;

if (File.Exists(destFile))
{
fileToDecompile = filePath;
File.Delete(destFile);
}

if (!string.IsNullOrWhiteSpace(fileToDecompile))
try
{
var fInfo = new FileInfo(fileToDecompile);
if (fInfo.Exists)
{
ProgressDialogController task = null;
if (Program.MainWindow != null)
{
task = await Program.MainWindow.ShowProgressAsync(Translate("Decompiling") + "...",
fInfo.FullName, false, Program.MainWindow.MetroDialogOptions);
MainWindow.ProcessUITasks();
}

// Prepare Lysis execution
var destFile = fInfo.FullName + ".sp";
var standardOutput = new StringBuilder();
using var process = new Process();
process.StartInfo.WorkingDirectory = Paths.GetLysisDirectory();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = "java";

process.StartInfo.Arguments = $"-jar lysis-java.jar \"{fInfo.FullName}\"";

// Execute Lysis, read and store output
try
{
process.Start();
while (!process.HasExited)
{
standardOutput.Append(process.StandardOutput.ReadToEnd());
}
standardOutput.Append(process.StandardOutput.ReadToEnd());
File.WriteAllText(destFile, standardOutput.ToString(), Encoding.UTF8);
}
catch (Exception ex)
{
await Program.MainWindow.ShowMessageAsync($"{fInfo.Name} {Translate("FailedToDecompile")}",
$"{ex.Message}", MessageDialogStyle.Affirmative,
Program.MainWindow.MetroDialogOptions);
}

// Load the decompiled file to SPCode
Program.MainWindow.TryLoadSourceFile(destFile, out _, true, false, true);
if (task != null)
{
await task.CloseAsync();
}
}
process.Start();
standardOutput.Append(process.StandardOutput.ReadToEnd());
File.WriteAllText(destFile, standardOutput.ToString(), Encoding.UTF8);
}
catch (Exception)
{
throw;
}

return destFile;
}
}
}
Loading