added runtime patcher, added MemoryUtils, fixes

This commit is contained in:
kitbyte
2025-03-24 14:51:58 +02:00
parent 123307aed7
commit d3ff13a528
33 changed files with 1294 additions and 287 deletions
+39 -51
View File
@@ -2,12 +2,15 @@
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Threading;
using AsarSharp;
using WeModPatcher.ReactiveCore;
using WeModPatcher.Core;
using WeModPatcher.Models;
using WeModPatcher.ReactiveUICore;
using WeModPatcher.Utils;
using WeModPatcher.View.Popups;
using Application = System.Windows.Application;
@@ -18,7 +21,7 @@ namespace WeModPatcher.View.MainWindow
public class MainWindowVm : ObservableObject
{
private readonly MainWindow _view;
public ObservableCollection<LogEntry> LogList { get; } = new ObservableCollection<LogEntry>();
public ObservableCollection<LogEntry> LogList { get; set; } = new ObservableCollection<LogEntry>();
private static Updater _updater = new Updater();
private string _weModPath;
@@ -71,50 +74,6 @@ namespace WeModPatcher.View.MainWindow
public RelayCommand RestoreBackupCommand { get; }
public AsyncRelayCommand UpdateCommand { get; }
private static bool CheckWeModPath(string root)
{
try
{
return File.Exists(Path.Combine(root, "WeMod.exe")) &&
File.Exists(Path.Combine(root, "resources", "app.asar"));
}
catch
{
return false;
}
}
public string FindWeModDirectory()
{
string localAppDataPath = Environment.GetEnvironmentVariable("LOCALAPPDATA");
string defaultDir = Path.Combine(localAppDataPath ?? "", "WeMod");
if (!Directory.Exists(defaultDir))
{
return null;
}
var appFolders = Directory.EnumerateDirectories(defaultDir)
.Select(folderPath => new DirectoryInfo(folderPath))
.Where(dirInfo => Regex.IsMatch(dirInfo.Name, @"^app-\w+"))
.Select(dirInfo => new
{
Name = dirInfo.Name,
Path = dirInfo.FullName,
LastModified = dirInfo.LastWriteTime
})
.OrderByDescending(item => item.LastModified)
.ToList();
return (
from folder
in appFolders
where CheckWeModPath(folder.Path)
select folder.Path
).FirstOrDefault();
}
private void OnFolderPathSelection(object obj)
{
using (var dialog = new FolderBrowserDialog())
@@ -127,7 +86,7 @@ namespace WeModPatcher.View.MainWindow
string selectedPath = dialog.SelectedPath;
string fileName = Path.GetFileName(selectedPath);
if (CheckWeModPath(selectedPath))
if (Extensions.CheckWeModPath(selectedPath))
{
WeModPath = selectedPath;
return;
@@ -143,6 +102,7 @@ namespace WeModPatcher.View.MainWindow
private void OnBackupRestoring(object param)
{
var backupPath = Path.Combine(WeModPath, "resources", "app.asar.backup");
if (!File.Exists(backupPath))
{
@@ -155,6 +115,22 @@ namespace WeModPatcher.View.MainWindow
using (File.Open(backupPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
}
// This shit doesn't look at the hash and verify() always returns true
//using X509Certificate2 cert = new X509Certificate2(X509Certificate.CreateFromSignedFile(filePath));
var restoreExeResult = MemoryUtils.PatchFile( Path.Combine( WeModPath, "WeMod.exe"),
Constants.ExePatchSignature, Constants.ExePatchSignature.OriginalBytes);
if (restoreExeResult == -1)
{
Log("Failed to restore the backup. Please close the WeMod and try again.", ELogType.Error);
}
else
{
Log(restoreExeResult == 0 ?
"Signature exe is original, does not require restoration"
: "WeMod.exe restored successfully", ELogType.Success);
}
}
catch
{
@@ -177,12 +153,24 @@ namespace WeModPatcher.View.MainWindow
return;
}
MainWindow.Instance.OpenPopup(new PatchVectorsPopup(async config =>
MainWindow.Instance.OpenPopup(new PatchVectorsPopup( async config =>
{
MainWindow.Instance.ClosePopup();
IsPatchEnabled = false;
await Task.Run(() => new Patcher(WeModPath, Log, config).Patch());
IsPatchEnabled = true;
await Task.Run(() =>
{
try
{
new StaticPatcher(WeModPath, Log, config).Patch();
AlreadyPatched = true;
}
catch (Exception e)
{
Log($"Failed to patch: {e.Message}", ELogType.Error);
IsPatchEnabled = true;
}
});
}), "What are we gonna patch?");
}
@@ -229,7 +217,7 @@ namespace WeModPatcher.View.MainWindow
RestoreBackupCommand = new RelayCommand(OnBackupRestoring);
UpdateCommand = new AsyncRelayCommand(OnUpdate);
WeModPath = FindWeModDirectory();
WeModPath = Extensions.FindWeModDirectory();
if (WeModPath == null)
{
Log("WeMod directory not found.", ELogType.Error);