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

Commit

Permalink
fix: fixed assembly management.
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchiDog1998 committed Apr 11, 2023
1 parent c7c9d21 commit b1371d0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 45 deletions.
2 changes: 1 addition & 1 deletion RotationSolver.Basic/Actions/BaseAction_Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ private bool TargetSelf(bool mustUse, int aoeCount)
return false;
}

//如果不用自动找目标,那就不打AOE
//not use when aoe.
if (DataCenter.StateType == StateCommandType.Manual)
{
if (!Service.Config.GetValue(SettingsCommand.UseAOEWhenManual) && !mustUse) return false;
Expand Down
66 changes: 41 additions & 25 deletions RotationSolver/RotationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
using FFXIVClientStructs.Interop;
using Lumina.Excel;
using RotationSolver.Updaters;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Loader;
using System.Text;

namespace RotationSolver;

internal record AssemblyInfo(string Name, string Author, string Path);

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



static Dictionary<string, Assembly> _handledAssemblies;
public RotationLoadContext(DirectoryInfo directoryInfo) : base(true)
{
Expand All @@ -26,15 +27,22 @@ public RotationLoadContext(DirectoryInfo directoryInfo) : base(true)

static RotationLoadContext()
{
_handledAssemblies = new Dictionary<string, Assembly>()
var assemblies = new 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,
typeof(RotationSolverPlugin).Assembly,
typeof(Resolver).Assembly,
typeof(DalamudPluginInterface).Assembly,
typeof(DataCenter).Assembly,
typeof(SheetAttribute).Assembly,
typeof(ExcelRow).Assembly,
};

_handledAssemblies = new Dictionary<string, Assembly>();

foreach (var assembly in assemblies)
{
_handledAssemblies.Add(assembly.GetName().Name, assembly);
}
}

protected override Assembly Load(AssemblyName assemblyName)
Expand Down Expand Up @@ -68,22 +76,27 @@ internal Assembly LoadFromFile(string filePath)
var assembly = LoadFromStream(file, pdbFile);
return assembly;
}


}

public static Dictionary<string, string> AssemblyPaths = new Dictionary<string, string>();
public static readonly SortedList<Assembly, AssemblyInfo> _assemblyInfos = new SortedList<Assembly, AssemblyInfo>();

public static string[] AllowedAssembly { get; private set; } = new string[0];
static readonly SortedList<Assembly, string> _authors = new SortedList<Assembly, string>();
public static string[] _allowedAssembly { get; private set; } = new string[0];
public static AssemblyInfo GetInfo(this Assembly assembly)
{
if(_assemblyInfos.TryGetValue(assembly, out var value))
{
return value;
}
return _assemblyInfos[assembly] = new AssemblyInfo(assembly.GetName().Name, "Unknown", assembly.Location);
}
public static async void LoadList()
{
using (var client = new HttpClient())
{
try
{
var bts = await client.GetByteArrayAsync("https://raw.githubusercontent.com/ArchiDog1998/RotationSolver/main/Resources/whitelist.json");
AllowedAssembly = JsonConvert.DeserializeObject<string[]>(Encoding.Default.GetString(bts));
_allowedAssembly = JsonConvert.DeserializeObject<string[]>(Encoding.Default.GetString(bts));
}
catch (Exception ex)
{
Expand All @@ -94,15 +107,19 @@ public static async void LoadList()

public static bool IsAllowed(this ICustomRotation rotation, out string name)
{
name = "Unknown";
if (rotation == null)
{
name = "Unknown";
return false;
}
var assembly = rotation.GetType().Assembly;
name = assembly.GetName().Name;

return AllowedAssembly.Contains(name + " - " + assembly.GetAuthor());
if(_assemblyInfos.TryGetValue(assembly, out var info))
{
name = info.Name;
return _allowedAssembly.Contains(name + " - " + info.Author);
}
return false;
}

public static Vector4 GetColor(this ICustomRotation rotation)
Expand All @@ -112,15 +129,11 @@ public static Vector4 GetColor(this ICustomRotation rotation)
public static bool IsBeta(this ICustomRotation rotation)
=> rotation.GetType().GetCustomAttribute<BetaRotationAttribute>() != null;

public static string GetAuthor(this Assembly assembly)
public static string GetAuthor(string path, string name)
{
if (_authors.TryGetValue(assembly, out var author)) return author;
try
{
var name = assembly.GetName().Name;
return _authors[assembly] =
(AssemblyPaths.TryGetValue(name, out var path)
? FileVersionInfo.GetVersionInfo(path)?.CompanyName : name)
return FileVersionInfo.GetVersionInfo(path)?.CompanyName
?? name ?? "Unknown";
}
catch
Expand All @@ -133,7 +146,10 @@ public static Assembly LoadFrom(string filePath)
{
var loadContext = new RotationLoadContext(new FileInfo(filePath).Directory);
var assembly = loadContext.LoadFromFile(filePath);
AssemblyPaths[assembly.GetName().Name] = filePath;

var name = assembly.GetName().Name;

_assemblyInfos[assembly] = new AssemblyInfo(name, GetAuthor(filePath, name), filePath);
return assembly;
}
}
36 changes: 18 additions & 18 deletions RotationSolver/UI/RotationConfigWindow_RotationDev.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,23 @@ private void DrawAssemblyInfos()
.Select(r => r.GetType().Assembly)
.ToHashSet();

if(ImGui.BeginTable("AssemblyTable", 2))
{
foreach (var assembly in assemblies)
{
ImGui.TableNextRow();
if (ImGui.Button(assembly.GetName().Name + assembly.GetName().Version))
{
if (!RotationHelper.AssemblyPaths.TryGetValue(assembly.GetName().Name, out var path))
path = assembly.Location;

Process.Start("explorer.exe", path);
}

ImGui.TableNextColumn();
ImGui.Text(assembly.GetAuthor());
}
ImGui.EndTable();
}
//if(ImGui.BeginTable("AssemblyTable", 2))
//{
// foreach (var assembly in assemblies)
// {
// ImGui.TableNextRow();
// if (ImGui.Button(assembly.GetName().Name + assembly.GetName().Version))
// {
// if (!RotationHelper.AssemblyInfos.TryGetValue(assembly.GetName().Name, out var path))
// path = assembly.Location;

// Process.Start("explorer.exe", path);
// }

// ImGui.TableNextColumn();
// ImGui.Text(assembly.GetAuthor());
// }
// ImGui.EndTable();
//}
}
}
2 changes: 1 addition & 1 deletion RotationSolver/Updaters/RotationUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ from l in Directory.GetFiles(dir, "*.dll")
select (a, a.GetCustomAttribute<AuthorHashAttribute>()) into author
where author.Item2 != null
group author by author.Item2 into gr
select (gr.Key.Hash, string.Join(", ", gr.Select(i => i.a.GetAuthor() + " - " + i.a.GetName().Name))))
select (gr.Key.Hash, string.Join(", ", gr.Select(i => i.a.GetInfo().Author + " - " + i.a.GetInfo().Name))))
.ToDictionary(i => i.Hash, i => i.Item2));

_customRotations = (
Expand Down

0 comments on commit b1371d0

Please sign in to comment.