Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
fix: make LoadContext Private
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchiDog1998 committed Apr 11, 2023
1 parent 35fdb0e commit c7c9d21
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 80 deletions.
7 changes: 4 additions & 3 deletions RotationSolver.Basic/Actions/BaseAction_Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,8 @@ private bool TargetHostileManual(BattleChara b, bool mustUse, int aoeCount, out
return true;
}

if (Service.Config.UseAOEAction && Service.Config.UseAOEWhenManual || mustUse)
if (Service.Config.GetValue(SettingsCommand.UseAOEAction)
&& Service.Config.GetValue(SettingsCommand.UseAOEWhenManual) || mustUse)
{
if (GetMostObjects(TargetFilterFuncEot(DataCenter.HostileTargets, mustUse), aoeCount).Contains(b))
{
Expand All @@ -324,7 +325,7 @@ private bool TargetSelf(bool mustUse, int aoeCount)
//如果不用自动找目标,那就不打AOE
if (DataCenter.StateType == StateCommandType.Manual)
{
if (!Service.Config.UseAOEWhenManual && !mustUse) return false;
if (!Service.Config.GetValue(SettingsCommand.UseAOEWhenManual) && !mustUse) return false;
}

var tars = TargetFilter.GetObjectInRadius(TargetFilterFuncEot(DataCenter.HostileTargets, mustUse), _action.EffectRange);
Expand Down Expand Up @@ -490,7 +491,7 @@ private static bool NoAOE
{
get
{
if (!Service.Config.UseAOEAction) return true;
if (!Service.Config.GetValue(SettingsCommand.UseAOEAction)) return true;

return Service.Config.ChooseAttackMark
&& !Service.Config.CanAttackMarkAOE
Expand Down
76 changes: 75 additions & 1 deletion RotationSolver/RotationHelper.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,79 @@
using Dalamud.Interface.Colors;
using Dalamud.Logging;
using Dalamud.Plugin;
using FFXIVClientStructs.Interop;
using Lumina.Excel;
using RotationSolver.Updaters;
using System.Diagnostics;
using System.Runtime.Loader;
using System.Text;

namespace RotationSolver;

internal static class RotationHelper
{
private class RotationLoadContext : AssemblyLoadContext
{
DirectoryInfo _directory;



static Dictionary<string, Assembly> _handledAssemblies;
public RotationLoadContext(DirectoryInfo directoryInfo) : base(true)
{
_directory = directoryInfo;
}

static RotationLoadContext()
{
_handledAssemblies = new Dictionary<string, Assembly>()
{
["RotationSolver"] = typeof(RotationSolverPlugin).Assembly,
["FFXIVClientStructs"] = typeof(Resolver).Assembly,
["Dalamud"] = typeof(DalamudPluginInterface).Assembly,
["RotationSolver.Basic"] = typeof(DataCenter).Assembly,
["Lumina"] = typeof(SheetAttribute).Assembly,
["Lumina.Excel"] = typeof(ExcelRow).Assembly,
};
}

protected override Assembly Load(AssemblyName assemblyName)
{
if (assemblyName.Name != null && _handledAssemblies.ContainsKey(assemblyName.Name))
{
return _handledAssemblies[assemblyName.Name];
}

var file = Path.Join(_directory.FullName, $"{assemblyName.Name}.dll");
if (File.Exists(file))
{
try
{
return LoadFromFile(file);
}
catch
{
//
}
}
return base.Load(assemblyName);
}

internal Assembly LoadFromFile(string filePath)
{
using var file = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
var pdbPath = Path.ChangeExtension(filePath, ".pdb");
if (!File.Exists(pdbPath)) return LoadFromStream(file);
using var pdbFile = File.Open(pdbPath, FileMode.Open, FileAccess.Read, FileShare.Read);
var assembly = LoadFromStream(file, pdbFile);
return assembly;
}


}

public static Dictionary<string, string> AssemblyPaths = new Dictionary<string, string>();

public static string[] AllowedAssembly { get; private set; } = new string[0];
static readonly SortedList<Assembly, string> _authors = new SortedList<Assembly, string>();
public static async void LoadList()
Expand Down Expand Up @@ -53,7 +119,7 @@ public static string GetAuthor(this Assembly assembly)
{
var name = assembly.GetName().Name;
return _authors[assembly] =
(RotationLoadContext.AssemblyPaths.TryGetValue(name, out var path)
(AssemblyPaths.TryGetValue(name, out var path)
? FileVersionInfo.GetVersionInfo(path)?.CompanyName : name)
?? name ?? "Unknown";
}
Expand All @@ -62,4 +128,12 @@ public static string GetAuthor(this Assembly assembly)
return "Unknown";
}
}

public static Assembly LoadFrom(string filePath)
{
var loadContext = new RotationLoadContext(new FileInfo(filePath).Directory);
var assembly = loadContext.LoadFromFile(filePath);
AssemblyPaths[assembly.GetName().Name] = filePath;
return assembly;
}
}
73 changes: 0 additions & 73 deletions RotationSolver/RotationLoadContext.cs

This file was deleted.

2 changes: 1 addition & 1 deletion RotationSolver/UI/RotationConfigWindow_Param.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ private void DrawParamTarget()
DrawCheckBox(LocalizationManager.RightLang.ConfigWindow_Param_ChooseAttackMark,
ref Service.Config.ChooseAttackMark, Service.Default.ChooseAttackMark);

if (Service.Config.ChooseAttackMark && Service.Config.UseAOEAction)
if (Service.Config.ChooseAttackMark && Service.Config.GetValue(SettingsCommand.UseAOEAction))
{
ImGui.Indent();

Expand Down
2 changes: 1 addition & 1 deletion RotationSolver/UI/RotationConfigWindow_RotationDev.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private void DrawAssemblyInfos()
ImGui.TableNextRow();
if (ImGui.Button(assembly.GetName().Name + assembly.GetName().Version))
{
if (!RotationLoadContext.AssemblyPaths.TryGetValue(assembly.GetName().Name, out var path))
if (!RotationHelper.AssemblyPaths.TryGetValue(assembly.GetName().Name, out var path))
path = assembly.Location;

Process.Start("explorer.exe", path);
Expand Down
4 changes: 4 additions & 0 deletions RotationSolver/Updaters/PreviewUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ internal static unsafe void PulseActionBar(uint actionID)
{
LoopAllSlotBar((bar, hot, index) =>
{
//Pulse Check
#if DEBUG
return true;
#endif
return IsActionSlotRight(bar, hot, actionID);
});
}
Expand Down
2 changes: 1 addition & 1 deletion RotationSolver/Updaters/RotationUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private static void LoadRotationsFromLocal(string relayFolder)
var assemblies = from dir in directories
where Directory.Exists(dir)
from l in Directory.GetFiles(dir, "*.dll")
select RotationLoadContext.LoadFrom(l);
select RotationHelper.LoadFrom(l);

PluginLog.Log("Try to load rotations from these assemblies.\n" + string.Join('\n', assemblies.Select(a => "- " + a.FullName)));

Expand Down

0 comments on commit c7c9d21

Please sign in to comment.