Files
se-launcher/PluginLoader/LoaderTools.cs
zznty fbf02ad716
All checks were successful
Build / Build Launcher (push) Successful in 2m17s
move client mods updates to actual steam api
2024-05-31 17:56:24 +07:00

211 lines
7.0 KiB
C#

using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
using Windows.UI.Popups;
using NLog;
using PluginLoader.SEPM;
using Sandbox;
using Sandbox.Game.World;
using Sandbox.Graphics.GUI;
using VRage.FileSystem;
using VRage.Input;
using VRage.Plugins;
namespace PluginLoader;
public static class LoaderTools
{
public static string PluginsDir => Path.GetFullPath(Path.Combine(MyFileSystem.ExePath, "Plugins"));
public static DialogResult ShowMessageBox(string message, string title, MessageBoxButtons buttons, MessageBoxIcon icon)
{
var dialog = new MessageDialog(message, title);
switch (buttons)
{
case MessageBoxButtons.OK:
dialog.Commands.Add(new UICommand("Ok"));
break;
case MessageBoxButtons.OKCancel:
dialog.Commands.Add(new UICommand("Ok"));
dialog.Commands.Add(new UICommand("Cancel"));
break;
case MessageBoxButtons.AbortRetryIgnore:
break;
case MessageBoxButtons.YesNoCancel:
dialog.Commands.Add(new UICommand("Yes"));
dialog.Commands.Add(new UICommand("No"));
dialog.Commands.Add(new UICommand("Cancel"));
break;
case MessageBoxButtons.YesNo:
dialog.Commands.Add(new UICommand("Yes"));
dialog.Commands.Add(new UICommand("No"));
break;
case MessageBoxButtons.RetryCancel:
dialog.Commands.Add(new UICommand("Retry"));
dialog.Commands.Add(new UICommand("Cancel"));
break;
case MessageBoxButtons.CancelTryContinue:
break;
default:
throw new ArgumentOutOfRangeException(nameof(buttons), buttons, null);
}
var hwnd = Process.GetCurrentProcess().MainWindowHandle;
if (hwnd != IntPtr.Zero)
WinRT.Interop.InitializeWithWindow.Initialize(dialog, hwnd);
else
{
Console.WriteLine(message);
return DialogResult.Cancel;
}
var result = dialog.ShowAsync().AsTask().Result;
return buttons switch
{
MessageBoxButtons.OK => DialogResult.OK,
MessageBoxButtons.OKCancel => result.Label == "Ok" ? DialogResult.OK : DialogResult.Cancel,
MessageBoxButtons.AbortRetryIgnore => DialogResult.Ignore,
MessageBoxButtons.YesNoCancel => result.Label switch
{
"Yes" => DialogResult.Yes,
"No" => DialogResult.No,
_ => DialogResult.Cancel
},
MessageBoxButtons.YesNo => result.Label switch
{
"Yes" => DialogResult.Yes,
_ => DialogResult.No
},
MessageBoxButtons.RetryCancel => result.Label switch
{
"Retry" => DialogResult.Retry,
_ => DialogResult.Cancel
},
MessageBoxButtons.CancelTryContinue => DialogResult.Cancel,
_ => throw new ArgumentOutOfRangeException(nameof(buttons), buttons, null)
};
}
public static void UnloadAndRestart()
{
MySessionLoader.Unload();
MySandboxGame.Config.ControllerDefaultOnStart = MyInput.Static.IsJoystickLastUsed;
MySandboxGame.Config.Save();
MyScreenManager.CloseAllScreensNowExcept(null);
MyPlugins.Unload();
LogManager.Flush();
Restart();
}
public static void Restart()
{
Process.Start("explorer.exe", "steam://rungameid/244850");
Process.GetCurrentProcess().Kill();
}
public static void ExecuteMain(SEPMPlugin plugin)
{
var name = plugin.GetType().ToString();
plugin.Main(new(name), new());
}
public static string GetHash1(string file)
{
using (var sha = new SHA1Managed())
{
return GetHash(file, sha);
}
}
public static string GetHash256(string file)
{
using (var sha = new SHA256CryptoServiceProvider())
{
return GetHash(file, sha);
}
}
public static string GetHash(string file, HashAlgorithm hash)
{
using (var fileStream = new FileStream(file, FileMode.Open))
{
using (var bufferedStream = new BufferedStream(fileStream))
{
var data = hash.ComputeHash(bufferedStream);
var sb = new StringBuilder(2 * data.Length);
foreach (var b in data)
sb.AppendFormat("{0:x2}", b);
return sb.ToString();
}
}
}
public static void OpenFileDialog(string title, string directory, string filter, Action<string> onOk)
{
var t = new Thread(() => OpenFileDialogThread(title, directory, filter, onOk));
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
private static void OpenFileDialogThread(string title, string directory, string filter, Action<string> onOk)
{
try
{
// Get the file path via prompt
using (var openFileDialog = new OpenFileDialog())
{
if (Directory.Exists(directory))
openFileDialog.InitialDirectory = directory;
openFileDialog.Title = title;
openFileDialog.Filter = filter;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
// Move back to the main thread so that we can interact with keen code again
MySandboxGame.Static.Invoke(
() => onOk(openFileDialog.FileName),
"PluginLoader");
}
}
catch (Exception e)
{
LogFile.Log.Error(e, "Error while opening file dialog");
}
}
public static void OpenFolderDialog(string title, string directory, Action<string> onOk)
{
var t = new Thread(() => OpenFolderDialogThread(title, directory, onOk));
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
private static void OpenFolderDialogThread(string title, string directory, Action<string> onOk)
{
try
{
// Get the file path via prompt
using (var openFileDialog = new FolderBrowserDialog())
{
if (Directory.Exists(directory))
openFileDialog.SelectedPath = directory;
openFileDialog.Description = title;
if (openFileDialog.ShowDialog() == DialogResult.OK)
// Move back to the main thread so that we can interact with keen code again
MySandboxGame.Static.Invoke(
() => onOk(openFileDialog.SelectedPath),
"PluginLoader");
}
}
catch (Exception e)
{
LogFile.Log.Error(e, "Error while opening file dialog");
}
}
}