Skip to content

Commit

Permalink
Client updates for net9
Browse files Browse the repository at this point in the history
  • Loading branch information
SignatureBeef committed Dec 31, 2024
1 parent 7ba00f8 commit 46fe58f
Show file tree
Hide file tree
Showing 18 changed files with 337 additions and 197 deletions.
11 changes: 5 additions & 6 deletions OTAPI.Client.Launcher/Actions/OTAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ public static void Launch(string[] args)

GC.Collect();


CSharpLoader.OnCompilationContext += CSharpLoader_OnCompilationContext;

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
Expand Down Expand Up @@ -232,7 +231,7 @@ static string ResolveFile(string path)
using var stream = root.GetManifestResourceStream(text);
if (stream is null) return null;
byte[] array = new byte[stream.Length];
stream.Read(array, 0, array.Length);
stream.ReadExactly(array);
stream.Seek(0, SeekOrigin.Begin);

// if (!File.Exists(resourceName))
Expand Down Expand Up @@ -267,10 +266,10 @@ static IntPtr ResolveNativeDep(string libraryName, Assembly assembly, DllImportS
IEnumerable<string> matches = Enumerable.Empty<string>();

foreach (var basePath in new[] {
Environment.CurrentDirectory,
AppContext.BaseDirectory,
Path.Combine(Environment.CurrentDirectory, "client")
})
Environment.CurrentDirectory,
AppContext.BaseDirectory,
Path.Combine(Environment.CurrentDirectory, "client")
})
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Expand Down
2 changes: 1 addition & 1 deletion OTAPI.Client.Launcher/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</Style>

<Style Selector="TextBox.install-path">
<Setter Property="FontSize" Value="8" />
<Setter Property="FontSize" Value="15" />
<Setter Property="FontWeight" Value="300" />
</Style>
<Style Selector="TextBlock.h1">
Expand Down
61 changes: 56 additions & 5 deletions OTAPI.Client.Launcher/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ You should have received a copy of the GNU General Public License
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Newtonsoft.Json.Linq;
using Mono.Cecil;
using Mono.Cecil.Cil;
using OTAPI.Client.Launcher.Targets;
using OTAPI.Common;
using OTAPI.Patcher.Targets;
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;

namespace OTAPI.Client.Launcher;

Expand Down Expand Up @@ -80,6 +82,8 @@ public MainWindow()

if (Program.ConsoleWriter is not null)
Program.ConsoleWriter.LineReceived += OnConsoleLineReceived;

PatchMonoMod();
}

private void OnConsoleLineReceived(string line)
Expand Down Expand Up @@ -204,13 +208,17 @@ public void OpenFolder(string folder)
process.Start();
}

System.Threading.CancellationTokenSource CancellationTokenSource { get; set; } = new System.Threading.CancellationTokenSource();

public void OnInstall(object sender, RoutedEventArgs e)
{
if (Context.IsInstalling || Context.InstallPath?.Path is null || Context.LaunchTarget is null) return;
Context.IsInstalling = true;

new System.Threading.Thread(() =>
Task.Run(async () =>
{
CancellationTokenSource.Cancel();
CancellationTokenSource = new();
try
{
var target = new PCClientTarget();
Expand All @@ -221,7 +229,7 @@ public void OnInstall(object sender, RoutedEventArgs e)
Context.InstallStatus = "Patching completed, installing to existing installation...";

Context.InstallPath.Target.StatusUpdate += (sender, e) => Context.InstallStatus = e.Text;
Context.InstallPath.Target.Install(Context.InstallPath.Path);
await Context.InstallPath.Target.InstallAsync(Context.InstallPath.Path, CancellationTokenSource.Token);

Context.InstallStatus = "Install completed";

Expand All @@ -235,6 +243,49 @@ public void OnInstall(object sender, RoutedEventArgs e)
Context.InstallStatus = "Err: " + ex.ToString();
OnConsoleLineReceived(ex.ToString());
}
}).Start();
});
}

/// <summary>
/// Current MonoMod is outdated, and the new reorg is not ready yet, however we need v25 RD for NET9, yet Patcher v22 is the latest, and is not compatible with v25.
/// Ultimately the problem is OTAPI Client using both at once, unlike the server setup which doesnt.
/// For now, the intention is to replace the entire both with "return new string[0];" to prevent the GAC IL from being used (which it isn't anyway)
/// </summary>
void PatchMonoMod()
{
var bin = File.ReadAllBytes("MonoMod.dll");
using MemoryStream ms = new(bin);
var asm = AssemblyDefinition.ReadAssembly(ms);
var modder = asm.MainModule.Types.Single(x => x.FullName == "MonoMod.MonoModder");
var gacPaths = modder.Methods.Single(m => m.Name == "get_GACPaths");
var il = gacPaths.Body.GetILProcessor();
if (il.Body.Instructions.Count != 3)
{
il.Body.Instructions.Clear();
il.Emit(OpCodes.Ldc_I4_0);
il.Emit(OpCodes.Newarr, asm.MainModule.ImportReference(typeof(string)));
il.Emit(OpCodes.Ret);

// clear MonoModder.MatchingConditionals(cap, asmName), with "return false;"
var mc = modder.Methods.Single(m => m.Name == "MatchingConditionals" && m.Parameters.Count == 2 && m.Parameters[1].ParameterType.Name == "AssemblyNameReference");
il = mc.Body.GetILProcessor();
mc.Body.Instructions.Clear();
mc.Body.Variables.Clear();
mc.Body.ExceptionHandlers.Clear();
il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Ret);

var writerParams = modder.Methods.Single(m => m.Name == "get_WriterParameters");
il = writerParams.Body.GetILProcessor();
var get_Current = writerParams.Body.Instructions.Single(x => x.Operand is MethodReference mref && mref.Name == "get_Current");
// replace get_Current with a number, and remove the bitwise checks
il.Remove(get_Current.Next);
il.Remove(get_Current.Next);
il.Replace(get_Current, Instruction.Create(
OpCodes.Ldc_I4, RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 37 : 0
));

asm.Write("MonoMod.dll");
}
}
}
17 changes: 11 additions & 6 deletions OTAPI.Client.Launcher/OTAPI.Client.Launcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,18 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="ImGui.NET" Version="1.91.0.1" GeneratePathProperty="true" />
<PackageReference Include="ModFramework.Modules.ClearScript" Version="1.1.9" GeneratePathProperty="true" />
<PackageReference Include="ModFramework.Modules.CSharp" Version="1.1.9" GeneratePathProperty="true" />
<PackageReference Include="ModFramework.Modules.Lua" Version="1.1.9" GeneratePathProperty="true" />
<PackageReference Include="MonoMod.RuntimeDetour.HookGen" Version="22.7.31.1" />
<PackageReference Include="ModFramework.Modules.ClearScript" Version="1.1.11" GeneratePathProperty="true" />
<PackageReference Include="ModFramework.Modules.CSharp" Version="1.1.11" GeneratePathProperty="true" />
<PackageReference Include="ModFramework.Modules.Lua" Version="1.1.11" GeneratePathProperty="true" />
<PackageReference Include="SharpZipLib" Version="1.4.2" />
<PackageReference Include="System.Collections.Specialized" Version="4.3.0" />
<PackageReference Include="System.Drawing.Primitives" Version="4.3.0" />
<PackageReference Include="System.Drawing.Common" Version="9.0.0" />
<PackageReference Include="MonoMod.RuntimeDetour.HookGen" Version="22.7.31.1" />
<PackageReference Include="MonoMod" Version="22.7.31.1">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</PackageReference>
<PackageReference Include="MonoMod.RuntimeDetour" Version="25.2.2">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</PackageReference>
</ItemGroup>
<Target Name="CleanAll" AfterTargets="Clean">
<RemoveDir Directories="$(OUTDIR)" />
Expand Down
10 changes: 4 additions & 6 deletions OTAPI.Client.Launcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,20 @@ public static void Main(string[] args)
Start(args);
}

public static IEnumerable<string> DiscoverFilesInFolder(string folder, string pattern, SearchOption searchOption = SearchOption.AllDirectories)
public static string[] DiscoverFilesInFolder(string folder, string pattern, SearchOption searchOption = SearchOption.AllDirectories)
{
if (Directory.Exists(folder))
return Directory.GetFiles(folder, pattern, searchOption);

return Enumerable.Empty<string>();
return [];
}

public static IEnumerable<string> DiscoverFoldersInFolder(string folder, string? pattern = null, SearchOption searchOption = SearchOption.AllDirectories)
public static string[] DiscoverFoldersInFolder(string folder, string? pattern = null, SearchOption searchOption = SearchOption.AllDirectories)
{
if (Directory.Exists(folder))
return pattern is not null ?
Directory.GetDirectories(folder, pattern, searchOption)
: Directory.GetDirectories(folder);

return Enumerable.Empty<string>();
return [];
}

public static string[] DiscoverPlugins()
Expand Down
6 changes: 4 additions & 2 deletions OTAPI.Client.Launcher/Targets/IPlatformTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ You should have received a copy of the GNU General Public License
*/
using OTAPI.Common;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace OTAPI.Client.Launcher.Targets;

public interface IPlatformTarget : IInstallDiscoverer
{
void Install(string installPath);
bool IsValidInstallPath(string installPath);
Task InstallAsync(string installPath, CancellationToken cancellationToken);
//bool IsValidInstallPath(string installPath);
event EventHandler<InstallStatusUpdate> StatusUpdate;
string Status { set; }

Expand Down
Loading

0 comments on commit 46fe58f

Please sign in to comment.