Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow using currently loaded assemblies in scripts #111

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 38 additions & 23 deletions Source/Triggernometry/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,27 +326,7 @@ public Interpreter()
var asms = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly asm in asms)
{
try
{
_so = _so.AddReferences(asm);
}
catch (Exception)
{
try
{
MethodInfo grb = asm.GetType().GetMethod("GetRawBytes", BindingFlags.Instance | BindingFlags.NonPublic);
byte[] asmb = (byte[])grb.Invoke(asm, null);
if (asmb == null || asmb.Length == 0)
{
continue;
}
PortableExecutableReference pe = MetadataReference.CreateFromImage(asmb);
_so = _so.AddReferences(pe);
}
catch (Exception)
{
}
}
_so = _so.AddMetadataReferenceFromAssembly(asm);
}
_so = _so.AddImports("System");
Evaluate("int whee;", null, new Context() { plug = RealPlugin.plug });
Expand Down Expand Up @@ -404,8 +384,14 @@ public void Evaluate(string command, string assy, Context ctx)
ScriptOptions _myso = _so.WithAllowUnsafe(GetUnsafeUsage(ctx));
if (assy != null)
{
string[] assys = assy.Split(',');
_myso = _myso.AddReferences(assys.Select(x => x.Trim()));
var assys = assy.Split(',').Select(x => x.Trim());
var currentAssemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var asmName in assys)
{
var assembly = currentAssemblies.FirstOrDefault(a => a.GetName().Name.Equals(asmName, StringComparison.OrdinalIgnoreCase));
// try to first load from current assemblies (including the assemblies loaded into memory by ACT which were not detected during InitPlugin)
_myso = (assembly != null) ? _myso.AddMetadataReferenceFromAssembly(assembly) : _myso.AddReferences(asmName);
}
}
string[] badApis = GetBadApis(ctx);
if (badApis != null && badApis.Length > 0)
Expand Down Expand Up @@ -472,4 +458,33 @@ public void Evaluate(string command, string assy, Context ctx)

}

public static class ScriptOptionsExtensions
{
public static ScriptOptions AddMetadataReferenceFromAssembly(this ScriptOptions options, Assembly asm)
{
try
{
options = options.AddReferences(asm);
}
catch (Exception)
{
try
{
MethodInfo grb = asm.GetType().GetMethod("GetRawBytes", BindingFlags.Instance | BindingFlags.NonPublic);
byte[] asmb = (byte[])grb.Invoke(asm, null);
if (asmb == null || asmb.Length == 0)
{
return options;
}
PortableExecutableReference pe = MetadataReference.CreateFromImage(asmb);
options = options.AddReferences(pe);
}
catch (Exception)
{
}
}
return options;
}
}

}