Skip to content

Commit e921768

Browse files
committed
Merge branch 'fix' of https://github.com/sors89/Open-Terraria-API into fix
2 parents 0b03dda + e0fa848 commit e921768

18 files changed

+337
-197
lines changed

OTAPI.Client.Launcher/Actions/OTAPI.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ public static void Launch(string[] args)
114114

115115
GC.Collect();
116116

117-
118117
CSharpLoader.OnCompilationContext += CSharpLoader_OnCompilationContext;
119118

120119
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
@@ -232,7 +231,7 @@ static string ResolveFile(string path)
232231
using var stream = root.GetManifestResourceStream(text);
233232
if (stream is null) return null;
234233
byte[] array = new byte[stream.Length];
235-
stream.Read(array, 0, array.Length);
234+
stream.ReadExactly(array);
236235
stream.Seek(0, SeekOrigin.Begin);
237236

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

269268
foreach (var basePath in new[] {
270-
Environment.CurrentDirectory,
271-
AppContext.BaseDirectory,
272-
Path.Combine(Environment.CurrentDirectory, "client")
273-
})
269+
Environment.CurrentDirectory,
270+
AppContext.BaseDirectory,
271+
Path.Combine(Environment.CurrentDirectory, "client")
272+
})
274273
{
275274
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
276275
{

OTAPI.Client.Launcher/MainWindow.axaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</Style>
2424

2525
<Style Selector="TextBox.install-path">
26-
<Setter Property="FontSize" Value="8" />
26+
<Setter Property="FontSize" Value="15" />
2727
<Setter Property="FontWeight" Value="300" />
2828
</Style>
2929
<Style Selector="TextBlock.h1">

OTAPI.Client.Launcher/MainWindow.axaml.cs

+56-5
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ You should have received a copy of the GNU General Public License
2020
using Avalonia.Controls;
2121
using Avalonia.Interactivity;
2222
using Avalonia.Markup.Xaml;
23-
using Newtonsoft.Json.Linq;
23+
using Mono.Cecil;
24+
using Mono.Cecil.Cil;
2425
using OTAPI.Client.Launcher.Targets;
2526
using OTAPI.Common;
2627
using OTAPI.Patcher.Targets;
2728
using System;
28-
using System.ComponentModel;
2929
using System.IO;
30+
using System.Linq;
3031
using System.Runtime.InteropServices;
32+
using System.Threading.Tasks;
3133

3234
namespace OTAPI.Client.Launcher;
3335

@@ -80,6 +82,8 @@ public MainWindow()
8082

8183
if (Program.ConsoleWriter is not null)
8284
Program.ConsoleWriter.LineReceived += OnConsoleLineReceived;
85+
86+
PatchMonoMod();
8387
}
8488

8589
private void OnConsoleLineReceived(string line)
@@ -204,13 +208,17 @@ public void OpenFolder(string folder)
204208
process.Start();
205209
}
206210

211+
System.Threading.CancellationTokenSource CancellationTokenSource { get; set; } = new System.Threading.CancellationTokenSource();
212+
207213
public void OnInstall(object sender, RoutedEventArgs e)
208214
{
209215
if (Context.IsInstalling || Context.InstallPath?.Path is null || Context.LaunchTarget is null) return;
210216
Context.IsInstalling = true;
211217

212-
new System.Threading.Thread(() =>
218+
Task.Run(async () =>
213219
{
220+
CancellationTokenSource.Cancel();
221+
CancellationTokenSource = new();
214222
try
215223
{
216224
var target = new PCClientTarget();
@@ -221,7 +229,7 @@ public void OnInstall(object sender, RoutedEventArgs e)
221229
Context.InstallStatus = "Patching completed, installing to existing installation...";
222230

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

226234
Context.InstallStatus = "Install completed";
227235

@@ -235,6 +243,49 @@ public void OnInstall(object sender, RoutedEventArgs e)
235243
Context.InstallStatus = "Err: " + ex.ToString();
236244
OnConsoleLineReceived(ex.ToString());
237245
}
238-
}).Start();
246+
});
247+
}
248+
249+
/// <summary>
250+
/// 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.
251+
/// Ultimately the problem is OTAPI Client using both at once, unlike the server setup which doesnt.
252+
/// 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)
253+
/// </summary>
254+
void PatchMonoMod()
255+
{
256+
var bin = File.ReadAllBytes("MonoMod.dll");
257+
using MemoryStream ms = new(bin);
258+
var asm = AssemblyDefinition.ReadAssembly(ms);
259+
var modder = asm.MainModule.Types.Single(x => x.FullName == "MonoMod.MonoModder");
260+
var gacPaths = modder.Methods.Single(m => m.Name == "get_GACPaths");
261+
var il = gacPaths.Body.GetILProcessor();
262+
if (il.Body.Instructions.Count != 3)
263+
{
264+
il.Body.Instructions.Clear();
265+
il.Emit(OpCodes.Ldc_I4_0);
266+
il.Emit(OpCodes.Newarr, asm.MainModule.ImportReference(typeof(string)));
267+
il.Emit(OpCodes.Ret);
268+
269+
// clear MonoModder.MatchingConditionals(cap, asmName), with "return false;"
270+
var mc = modder.Methods.Single(m => m.Name == "MatchingConditionals" && m.Parameters.Count == 2 && m.Parameters[1].ParameterType.Name == "AssemblyNameReference");
271+
il = mc.Body.GetILProcessor();
272+
mc.Body.Instructions.Clear();
273+
mc.Body.Variables.Clear();
274+
mc.Body.ExceptionHandlers.Clear();
275+
il.Emit(OpCodes.Ldc_I4_1);
276+
il.Emit(OpCodes.Ret);
277+
278+
var writerParams = modder.Methods.Single(m => m.Name == "get_WriterParameters");
279+
il = writerParams.Body.GetILProcessor();
280+
var get_Current = writerParams.Body.Instructions.Single(x => x.Operand is MethodReference mref && mref.Name == "get_Current");
281+
// replace get_Current with a number, and remove the bitwise checks
282+
il.Remove(get_Current.Next);
283+
il.Remove(get_Current.Next);
284+
il.Replace(get_Current, Instruction.Create(
285+
OpCodes.Ldc_I4, RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 37 : 0
286+
));
287+
288+
asm.Write("MonoMod.dll");
289+
}
239290
}
240291
}

OTAPI.Client.Launcher/OTAPI.Client.Launcher.csproj

+11-6
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,18 @@
5151
</ItemGroup>
5252
<ItemGroup>
5353
<PackageReference Include="ImGui.NET" Version="1.91.0.1" GeneratePathProperty="true" />
54-
<PackageReference Include="ModFramework.Modules.ClearScript" Version="1.1.9" GeneratePathProperty="true" />
55-
<PackageReference Include="ModFramework.Modules.CSharp" Version="1.1.9" GeneratePathProperty="true" />
56-
<PackageReference Include="ModFramework.Modules.Lua" Version="1.1.9" GeneratePathProperty="true" />
57-
<PackageReference Include="MonoMod.RuntimeDetour.HookGen" Version="22.7.31.1" />
54+
<PackageReference Include="ModFramework.Modules.ClearScript" Version="1.1.11" GeneratePathProperty="true" />
55+
<PackageReference Include="ModFramework.Modules.CSharp" Version="1.1.11" GeneratePathProperty="true" />
56+
<PackageReference Include="ModFramework.Modules.Lua" Version="1.1.11" GeneratePathProperty="true" />
5857
<PackageReference Include="SharpZipLib" Version="1.4.2" />
59-
<PackageReference Include="System.Collections.Specialized" Version="4.3.0" />
60-
<PackageReference Include="System.Drawing.Primitives" Version="4.3.0" />
58+
<PackageReference Include="System.Drawing.Common" Version="9.0.0" />
59+
<PackageReference Include="MonoMod.RuntimeDetour.HookGen" Version="22.7.31.1" />
60+
<PackageReference Include="MonoMod" Version="22.7.31.1">
61+
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
62+
</PackageReference>
63+
<PackageReference Include="MonoMod.RuntimeDetour" Version="25.2.2">
64+
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
65+
</PackageReference>
6166
</ItemGroup>
6267
<Target Name="CleanAll" AfterTargets="Clean">
6368
<RemoveDir Directories="$(OUTDIR)" />

OTAPI.Client.Launcher/Program.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,20 @@ public static void Main(string[] args)
116116
Start(args);
117117
}
118118

119-
public static IEnumerable<string> DiscoverFilesInFolder(string folder, string pattern, SearchOption searchOption = SearchOption.AllDirectories)
119+
public static string[] DiscoverFilesInFolder(string folder, string pattern, SearchOption searchOption = SearchOption.AllDirectories)
120120
{
121121
if (Directory.Exists(folder))
122122
return Directory.GetFiles(folder, pattern, searchOption);
123-
124-
return Enumerable.Empty<string>();
123+
return [];
125124
}
126125

127-
public static IEnumerable<string> DiscoverFoldersInFolder(string folder, string? pattern = null, SearchOption searchOption = SearchOption.AllDirectories)
126+
public static string[] DiscoverFoldersInFolder(string folder, string? pattern = null, SearchOption searchOption = SearchOption.AllDirectories)
128127
{
129128
if (Directory.Exists(folder))
130129
return pattern is not null ?
131130
Directory.GetDirectories(folder, pattern, searchOption)
132131
: Directory.GetDirectories(folder);
133-
134-
return Enumerable.Empty<string>();
132+
return [];
135133
}
136134

137135
public static string[] DiscoverPlugins()

OTAPI.Client.Launcher/Targets/IPlatformTarget.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ You should have received a copy of the GNU General Public License
1818
*/
1919
using OTAPI.Common;
2020
using System;
21+
using System.Threading;
22+
using System.Threading.Tasks;
2123

2224
namespace OTAPI.Client.Launcher.Targets;
2325

2426
public interface IPlatformTarget : IInstallDiscoverer
2527
{
26-
void Install(string installPath);
27-
bool IsValidInstallPath(string installPath);
28+
Task InstallAsync(string installPath, CancellationToken cancellationToken);
29+
//bool IsValidInstallPath(string installPath);
2830
event EventHandler<InstallStatusUpdate> StatusUpdate;
2931
string Status { set; }
3032

0 commit comments

Comments
 (0)