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

Commit

Permalink
fix: RotationHelper formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
RiotNOR committed May 16, 2023
1 parent 9f57e24 commit b73ab67
Showing 1 changed file with 125 additions and 125 deletions.
250 changes: 125 additions & 125 deletions RotationSolver/Helpers/RotationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,171 +3,171 @@
using RotationSolver.Data;
using System.Diagnostics;

namespace RotationSolver.Helpers;

internal record AssemblyInfo(string Name, string Author, string FilePath, string SupportLink, string HelpLink, string ChangeLog, string DonateLink);

internal static class RotationHelper
namespace RotationSolver.Helpers
{
private static readonly Dictionary<Assembly, AssemblyInfo> _assemblyInfos = new();
internal static class RotationHelper
{
private static readonly Dictionary<Assembly, AssemblyInfo> _assemblyInfos = new();

public static List<LoadedAssembly> LoadedCustomRotations { get; } = new List<LoadedAssembly>();
public static List<LoadedAssembly> LoadedCustomRotations { get; } = new List<LoadedAssembly>();

public static string[] AllowedAssembly { get; private set; } = Array.Empty<string>();
public static string[] AllowedAssembly { get; private set; } = Array.Empty<string>();

public static async Task LoadListAsync()
{
try
public static async Task LoadListAsync()
{
using var client = new HttpClient();
var response = await client.GetAsync("https://raw.githubusercontent.com/ArchiDog1998/RotationSolver/main/Resources/whitelist.json");
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
AllowedAssembly = JsonConvert.DeserializeObject<string[]>(content);
}
catch (Exception ex)
{
PluginLog.Log(ex, "Failed to load white List.");
try
{
using var client = new HttpClient();
var response = await client.GetAsync("https://raw.githubusercontent.com/ArchiDog1998/RotationSolver/main/Resources/whitelist.json");
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
AllowedAssembly = JsonConvert.DeserializeObject<string[]>(content);
}
catch (Exception ex)
{
PluginLog.Log(ex, "Failed to load white List.");
}
}
}

public static AssemblyInfo GetInfo(this Assembly assembly)
{
if (_assemblyInfos.TryGetValue(assembly, out var info))
public static AssemblyInfo GetInfo(this Assembly assembly)
{
return info;
}

var name = assembly.GetName().Name;
var location = assembly.Location;
var version = assembly.GetName().Version?.ToString();
var description = assembly.GetCustomAttribute<AssemblyDescriptionAttribute>()?.Description;
var company = assembly.GetCustomAttribute<AssemblyCompanyAttribute>()?.Company;
var product = assembly.GetCustomAttribute<AssemblyProductAttribute>()?.Product;
var trademark = assembly.GetCustomAttribute<AssemblyTrademarkAttribute>()?.Trademark;
if (_assemblyInfos.TryGetValue(assembly, out var info))
{
return info;
}

var assemblyInfo = new AssemblyInfo(name, version, location, description, company, product, trademark);
var name = assembly.GetName().Name;
var location = assembly.Location;
var version = assembly.GetName().Version?.ToString();
var description = assembly.GetCustomAttribute<AssemblyDescriptionAttribute>()?.Description;
var company = assembly.GetCustomAttribute<AssemblyCompanyAttribute>()?.Company;
var product = assembly.GetCustomAttribute<AssemblyProductAttribute>()?.Product;
var trademark = assembly.GetCustomAttribute<AssemblyTrademarkAttribute>()?.Trademark;

_assemblyInfos[assembly] = assemblyInfo;
var assemblyInfo = new AssemblyInfo(name, version, location, description, company, product, trademark);

return assemblyInfo;
}
_assemblyInfos[assembly] = assemblyInfo;

public static bool IsAllowed(this ICustomRotation rotation, out string name)
{
name = "Unknown";
if (rotation == null) return false;
return assemblyInfo;
}

var assembly = GetTypeAssembly(rotation);
if (assembly == null) return false;
public static bool IsAllowed(this ICustomRotation rotation, out string name)
{
name = "Unknown";
if (rotation == null) return false;

name = assembly.GetName().Name;
return assembly.IsAllowed();
}
var assembly = GetTypeAssembly(rotation);
if (assembly == null) return false;

public static bool IsAllowed(this Assembly assembly)
{
if (_assemblyInfos.TryGetValue(assembly, out var info))
{
var assemblyName = $"{info.Name} - {info.Author}";
return AllowedAssembly.Contains(assemblyName);
name = assembly.GetName().Name;
return assembly.IsAllowed();
}
return false;
}

public static Assembly GetTypeAssembly(this ICustomRotation rotation)
{
try
{
return rotation.GetType().Assembly;
}
catch (Exception ex)
public static bool IsAllowed(this Assembly assembly)
{
PluginLog.LogError($"Failed to get assembly for rotation {rotation.GetType().Name}: {ex}");
return null;
if (_assemblyInfos.TryGetValue(assembly, out var info))
{
var assemblyName = $"{info.Name} - {info.Author}";
return AllowedAssembly.Contains(assemblyName);
}
return false;
}
}

public static Vector4 GetColor(this ICustomRotation rotation)
{
if (!rotation.IsValid)
public static Assembly GetTypeAssembly(this ICustomRotation rotation)
{
return ImGuiColors.DPSRed;
try
{
return rotation.GetType().Assembly;
}
catch (Exception ex)
{
PluginLog.LogError($"Failed to get assembly for rotation {rotation.GetType().Name}: {ex}");
return null;
}
}

if (!rotation.IsAllowed(out _))
public static Vector4 GetColor(this ICustomRotation rotation)
{
return ImGuiColors.DalamudViolet;
}
if (!rotation.IsValid)
{
PluginLog.Log("This is not a valid rotation: " + rotation.RotationName);
return ImGuiColors.DPSRed;
}

if (rotation.IsBeta())
{
return ImGuiColors.DalamudOrange;
}
if (!rotation.IsAllowed(out _))
{
return ImGuiColors.DalamudViolet;
}

return ImGuiColors.DalamudWhite;
}
if (rotation.IsBeta())
{
return ImGuiColors.DalamudOrange;
}

public static bool IsBeta(this ICustomRotation rotation)
{
var betaAttribute = rotation.GetType().GetCustomAttribute<BetaRotationAttribute>();
return betaAttribute != null;
}
return ImGuiColors.DalamudWhite;
}

public static Assembly LoadCustomRotationAssembly(string filePath)
{
var directoryInfo = new FileInfo(filePath).Directory;
var loadContext = new RotationLoadContext(directoryInfo);
var assembly = loadContext.LoadFromFile(filePath);

var assemblyName = assembly.GetName().Name;
var author = GetAuthor(filePath, assemblyName);

var attr = assembly.GetCustomAttribute<AssemblyLinkAttribute>();
var assemblyInfo = new AssemblyInfo(
assemblyName,
author,
filePath,
attr?.SupportLink,
attr?.HelpLink,
attr?.ChangeLog,
attr?.Donate);

var existingAssembly = GetAssemblyFromPath(filePath);
if (existingAssembly != null)
public static bool IsBeta(this ICustomRotation rotation)
{
_assemblyInfos.Remove(existingAssembly);
var betaAttribute = rotation.GetType().GetCustomAttribute<BetaRotationAttribute>();
return betaAttribute != null;
}

_assemblyInfos[assembly] = assemblyInfo;
public static Assembly LoadCustomRotationAssembly(string filePath)
{
var directoryInfo = new FileInfo(filePath).Directory;
var loadContext = new RotationLoadContext(directoryInfo);
var assembly = loadContext.LoadFromFile(filePath);

var assemblyName = assembly.GetName().Name;
var author = GetAuthor(filePath, assemblyName);

var attr = assembly.GetCustomAttribute<AssemblyLinkAttribute>();
var assemblyInfo = new AssemblyInfo(
assemblyName,
author,
filePath,
attr?.SupportLink,
attr?.HelpLink,
attr?.ChangeLog,
attr?.Donate);

var existingAssembly = GetAssemblyFromPath(filePath);
if (existingAssembly != null)
{
_assemblyInfos.Remove(existingAssembly);
}

var loadedAssembly = new LoadedAssembly(
filePath,
File.GetLastWriteTimeUtc(filePath).ToString());
_assemblyInfos[assembly] = assemblyInfo;

LoadedCustomRotations.RemoveAll(item => item.FilePath == loadedAssembly.FilePath);
LoadedCustomRotations.Add(loadedAssembly);
var loadedAssembly = new LoadedAssembly(
filePath,
File.GetLastWriteTimeUtc(filePath).ToString());

return assembly;
}
LoadedCustomRotations.RemoveAll(item => item.FilePath == loadedAssembly.FilePath);
LoadedCustomRotations.Add(loadedAssembly);

private static Assembly GetAssemblyFromPath(string filePath)
{
foreach (var asm in _assemblyInfos)
return assembly;
}

private static Assembly GetAssemblyFromPath(string filePath)
{
if (asm.Value.FilePath == filePath)
foreach (var asm in _assemblyInfos)
{
return asm.Key;
if (asm.Value.FilePath == filePath)
{
return asm.Key;
}
}
return null;
}
return null;
}

private static string GetAuthor(string filePath, string assemblyName)
{
var fileVersionInfo = FileVersionInfo.GetVersionInfo(filePath);
return string.IsNullOrWhiteSpace(fileVersionInfo.CompanyName) ? assemblyName : fileVersionInfo.CompanyName;
}
private static string GetAuthor(string filePath, string assemblyName)
{
var fileVersionInfo = FileVersionInfo.GetVersionInfo(filePath);
return string.IsNullOrWhiteSpace(fileVersionInfo.CompanyName) ? assemblyName : fileVersionInfo.CompanyName;
}


}
}

0 comments on commit b73ab67

Please sign in to comment.