Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Reflection;
- using System.Security.Permissions;
- using HarmonyLib;
- using Mono.Cecil;
- using UnityEngine;
- internal static void TestAssembly()
- {
- var resolver = new DefaultAssemblyResolver();
- resolver.AddSearchDirectory(Path.Combine(Application.dataPath, "Managed"));
- foreach (var mod in ModManager.GetLoadedMods())
- {
- resolver.AddSearchDirectory(mod.Path);
- }
- AssemblyDefinition assdef_main = AssemblyDefinition.ReadAssembly(Assembly.GetAssembly(typeof(GameManager)).Location, new ReaderParameters() { AssemblyResolver = resolver });
- var workingAssembly = AssemblyDefinition.CreateAssembly(new AssemblyNameDefinition( "TestAssembly",
- new Version(0, 0, 0, 0)),
- "TestAssembly.dll",
- new ModuleParameters()
- {
- Kind = ModuleKind.Dll,
- AssemblyResolver = resolver,
- Architecture = TargetArchitecture.I386,
- Runtime = TargetRuntime.Net_4_0
- });
- workingAssembly.MainModule.Attributes = assdef_main.MainModule.Attributes;
- workingAssembly.MainModule.Characteristics = assdef_main.MainModule.Characteristics;
- //write security attributes so that calling non-public patch methods from this assembly is allowed
- Mono.Cecil.SecurityAttribute sattr_permission = new Mono.Cecil.SecurityAttribute(workingAssembly.MainModule.ImportReference(typeof(SecurityPermissionAttribute)));
- Mono.Cecil.CustomAttributeNamedArgument caarg_SkipVerification = new Mono.Cecil.CustomAttributeNamedArgument(nameof(SecurityPermissionAttribute.SkipVerification), new CustomAttributeArgument(workingAssembly.MainModule.TypeSystem.Boolean, true));
- sattr_permission.Properties.Add(caarg_SkipVerification);
- SecurityDeclaration sdec = new SecurityDeclaration(Mono.Cecil.SecurityAction.RequestMinimum);
- sdec.SecurityAttributes.Add(sattr_permission);
- workingAssembly.SecurityDeclarations.Add(sdec);
- var mtdref_start_holding = workingAssembly.MainModule.ImportReference(AccessTools.Method(typeof(ItemActionRanged), nameof(ItemActionRanged.StartHolding)));
- Log.Out($"before resolve: {mtdref_start_holding.FullName}");
- var mtddef_start_holding = mtdref_start_holding.Resolve();
- Log.Out($"after resolve: {mtddef_start_holding.FullName}");
- resolver.Dispose();
- workingAssembly.Dispose();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement