Skip to content

Commit

Permalink
Zip library updated from Ionic.Zip.Reduced v1.9.1.8 to DotNetZip v1.1…
Browse files Browse the repository at this point in the history
…6.0 (#4109)

* Update Ionic.Zip.Reduced to DotNetZip 1.16.0

* Delete ZipExtractFix, new dll has correct logic, no need for iledit.

* Remove old Ionic.Zip.Reduced from libraries

* Improved assembly resolve hook which allows assembly renames

* Fix backwards logic, tested

---------

Co-authored-by: Chicken-Bones <mehvids@gmail.com>
  • Loading branch information
JavidPack and Chicken-Bones committed Mar 15, 2024
1 parent 6022af7 commit 3c48733
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 88 deletions.
6 changes: 3 additions & 3 deletions patches/tModLoader/ReLogic/ReLogic.csproj.patch
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
--- src/TerrariaNetCore/ReLogic/ReLogic.csproj
+++ src/tModLoader/ReLogic/ReLogic.csproj
@@ -6,9 +_,10 @@
@@ -6,14 +_,20 @@
<Company>Re-Logic</Company>
<Copyright>Copyright © Re-Logic 2017</Copyright>
<RootNamespace>ReLogic</RootNamespace>
+ <GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>
- <Reference Include="../Terraria/Libraries/Common/Ionic.Zip.CF.dll" />
+ <Reference Include="../Terraria/Libraries/Common/Ionic.Zip.Reduced.dll" />
<ProjectReference Include="../../../FNA/FNA.Core.csproj" />
<Reference Include="../Terraria/Libraries/Common/Newtonsoft.Json.dll" />
<Reference Include="../Terraria/Libraries/Common/SteelSeriesEngineWrapper.dll" />
@@ -16,4 +_,9 @@
+ <PackageReference Include="DotNetZip" Version="1.16.0" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Microsoft/**" />
</ItemGroup>
Expand Down
Binary file not shown.
55 changes: 29 additions & 26 deletions patches/tModLoader/Terraria/ModLoader/Core/AssemblyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using Terraria.Localization;
using Microsoft.Xna.Framework;
using System.Text.RegularExpressions;
using Ionic.Zip;
using MonoMod.RuntimeDetour;

namespace Terraria.ModLoader.Core;

Expand Down Expand Up @@ -101,6 +103,9 @@ private Assembly LoadAssembly(byte[] code, byte[] pdb = null)

protected override Assembly Load(AssemblyName assemblyName)
{
if (AssemblyRedirects.GetAssembly(assemblyName.Name) is Assembly redirected)
return redirected;

if (assemblies.TryGetValue(assemblyName.Name, out var asm))
return asm;

Expand Down Expand Up @@ -140,6 +145,30 @@ internal void ClearAssemblyBytes()
}
}

private static class AssemblyRedirects
{
private static Dictionary<string, Assembly> _redirects = new() {
["tModLoader"] = Assembly.GetExecutingAssembly(), // Unsure if still needed, but lets us ignore versioning when mods resolve
["FNA"] = typeof(Vector2).Assembly, // Unsure if still needed, but lets us ignore versioning when mods resolve
["Ionic.Zip.Reduced"] = typeof(ZipFile).Assembly, // Assembly name changed to DotNetZip
};

private static Hook _hook = new Hook(
typeof(AssemblyLoadContext).GetMethod("ValidateAssemblyNameWithSimpleName", BindingFlags.Static | BindingFlags.NonPublic),
hook_ValidateAssemblyNameWithSimpleName);

private delegate Assembly orig_ValidateAssemblyNameWithSimpleName(Assembly assembly, string? requestedSimpleName);
private static Assembly hook_ValidateAssemblyNameWithSimpleName(orig_ValidateAssemblyNameWithSimpleName orig, Assembly assembly, string? requestedSimpleName)
{
if (_redirects.TryGetValue(requestedSimpleName, out var redirect) && assembly == redirect)
return assembly;

return orig(assembly, requestedSimpleName);
}

public static Assembly GetAssembly(string name) => _redirects.TryGetValue(name, out var asm) ? asm : null;
}

[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Unload() {
foreach (var alc in loadedModContexts.Values) {
Expand Down Expand Up @@ -168,30 +197,6 @@ internal static IEnumerable<string> OldLoadContexts()

//private static CecilAssemblyResolver cecilAssemblyResolver = new CecilAssemblyResolver();

private static bool assemblyResolverAdded;
private static void AddAssemblyResolver()
{
if (assemblyResolverAdded)
return;
assemblyResolverAdded = true;

AppDomain.CurrentDomain.AssemblyResolve += TmlCustomResolver;
}

private static Assembly TmlCustomResolver(object sender, ResolveEventArgs args)
{
//Legacy: With FNA and .Net5 changes, had aimed to eliminate the variants of tmodloader (tmodloaderdebug, tmodloaderserver) and Terraria as assembly names.
// However, due to uncertainty in that elimination, in particular for Terraria, have opted to retain the original check. - Solxan
var name = new AssemblyName(args.Name).Name;
if (name.Contains("tModLoader") || name == "Terraria")
return Assembly.GetExecutingAssembly();

if (name == "FNA")
return typeof(Vector2).Assembly;

return null;
}

private static Mod Instantiate(ModLoadContext mod)
{
try {
Expand Down Expand Up @@ -236,8 +241,6 @@ private static void VerifyMod(string modName, Assembly assembly, out Type modTyp

internal static List<Mod> InstantiateMods(List<LocalMod> modsToLoad, CancellationToken token)
{
AddAssemblyResolver();

var modList = modsToLoad.Select(m => new ModLoadContext(m)).ToList();
foreach (var mod in modList)
loadedModContexts.Add(mod.Name, mod);
Expand Down
15 changes: 0 additions & 15 deletions patches/tModLoader/Terraria/ModLoader/Core/AssemblyResolving.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,6 @@ public static void Init()
return;
init = true;

// allow mods which reference embedded assemblies to reference a different version and be safely upgraded
AssemblyResolveEarly((_, args) => {
var name = new AssemblyName(args.Name);
if (Array.Find(typeof(Program).Assembly.GetManifestResourceNames(), s => s.EndsWith(name.Name + ".dll")) == null)
return null;
var existing = AppDomain.CurrentDomain.GetAssemblies().SingleOrDefault(a => a.GetName().Name == name.Name);
if (existing != null) {
Logging.tML.Warn($"Upgraded Reference {name.Name} -> Version={name.Version} -> {existing.GetName().Version}");
return existing;
}
return null;
});

// log all assembly resolutions
AssemblyResolveEarly((_, args) => {
Logging.tML.DebugFormat("Assembly Resolve: {0} -> {1}", args.RequestingAssembly, args.Name);
Expand Down
35 changes: 0 additions & 35 deletions patches/tModLoader/Terraria/ModLoader/Engine/ZipExtractFix.cs

This file was deleted.

1 change: 0 additions & 1 deletion patches/tModLoader/Terraria/ModLoader/ModLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ internal static void EngineInit()
FileAssociationSupport.UpdateFileAssociation();
FolderShortcutSupport.UpdateFolderShortcuts();
MonoModHooks.Initialize();
ZipExtractFix.Init();
FNAFixes.Init();
LoaderManager.AutoLoad();
}
Expand Down
17 changes: 9 additions & 8 deletions patches/tModLoader/Terraria/Terraria.csproj.patch
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--- src/TerrariaNetCore/Terraria/Terraria.csproj
+++ src/tModLoader/Terraria/Terraria.csproj
@@ -11,23 +_,34 @@
@@ -11,45 +_,65 @@
<Company>Re-Logic</Company>
<Copyright>Copyright © 2022 Re-Logic</Copyright>
<RootNamespace>Terraria</RootNamespace>
Expand Down Expand Up @@ -41,23 +41,24 @@
<!-- References -->
<ItemGroup>
<ProjectReference Include="../../../FNA/FNA.Core.csproj" />
@@ -36,7 +_,9 @@
<Link>Libraries/ReLogic/ReLogic.dll</Link>
<LogicalName>Terraria.Libraries.ReLogic.ReLogic.dll</LogicalName>
</EmbeddedResource>
<ProjectReference Include="../ReLogic/ReLogic.csproj" />
- <EmbeddedResource Include="../ReLogic/bin/$(Configuration)/$(TargetFramework)/ReLogic.dll">
- <Link>Libraries/ReLogic/ReLogic.dll</Link>
- <LogicalName>Terraria.Libraries.ReLogic.ReLogic.dll</LogicalName>
- </EmbeddedResource>
+ <ProjectReference Include="../../../tModPorter/tModPorter/tModPorter.csproj" />

+ <Reference Include="Ionic.Zip.Reduced" />
<Reference Include="CsvHelper" />
<Reference Include="Ionic.Zip.CF" />
- <Reference Include="Ionic.Zip.CF" />
<Reference Include="MP3Sharp" />
@@ -44,12 +_,24 @@
<Reference Include="Newtonsoft.Json" />
<Reference Include="NVorbis" />
<Reference Include="RailSDK.Net" />
<Reference Include="SteelSeriesEngineWrapper" />
+ <Reference Include="log4net" />
+ <Reference Include="TerrariaHooks" />

+ <PackageReference Include="DotNetZip" Version="1.16.0" />
<PackageReference Include="Steamworks.NET" Version="20.1.0" />
+ <PackageReference Include="Basic.Reference.Assemblies.Net60" Version="1.2.4" />
+ <PackageReference Include="MonoMod.RuntimeDetour" Version="25.1.0" />
Expand Down

0 comments on commit 3c48733

Please sign in to comment.