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

Commit

Permalink
fix: make libs can be a url.
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchiDog1998 committed Apr 8, 2023
1 parent e0e5931 commit 4496afb
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
5 changes: 2 additions & 3 deletions RotationSolver.Basic/Configuration/PluginConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,9 @@ public class PluginConfiguration : IPluginConfiguration

public bool InDebug = false;
public bool AutoUpdateLibs = true;
public string[] OtherLibs = new string[0];
public (string, string)[] OtherLibsUrl = new(string, string)[]
public string[] OtherLibs = new string[]
{
("ArchiDog1998", "RotationSolver"),
"https://github.com/ArchiDog1998/RotationSolver/releases/latest/download/latest.zip",
};

public List<TargetingType> TargetingTypes { get; set; } = new List<TargetingType>();
Expand Down
15 changes: 2 additions & 13 deletions RotationSolver/RotationSolverPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Game.Text.SeStringHandling.Payloads;
using Dalamud.Interface.Windowing;
using Dalamud.Logging;
using Dalamud.Plugin;
using FFXIVClientStructs.Havok;
using RotationSolver.Basic.Configuration;
Expand Down Expand Up @@ -70,24 +71,12 @@ public unsafe RotationSolverPlugin(DalamudPluginInterface pluginInterface)
ChangeUITranslation();

RotationUpdater.GetAllCustomRotations();
RotationHelper.LoadList();

LinkPayload = pluginInterface.AddChatLinkHandler(0, (id, str) =>
{
if(id == 0) OpenConfigWindow();
});

using (var client = new HttpClient())
{
try
{
var bts = client.GetByteArrayAsync("https://raw.githubusercontent.com/ArchiDog1998/RotationSolver/main/Recources/whiteList.json");
RotationHelper.AllowedAssembly = JsonConvert.DeserializeObject<string[]>(Encoding.Default.GetString(bts.Result));

client.GetByteArrayAsync("https://raw.githubusercontent.com/ArchiDog1998/RotationSolver/main/Recources/defaultList.json");
RotationHelper.DefaultAssembly = JsonConvert.DeserializeObject<string[]>(Encoding.Default.GetString(bts.Result));
}
catch { }
}
}


Expand Down
12 changes: 12 additions & 0 deletions RotationSolver/UI/RotationConfigWindow_Major.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Dalamud.Interface.Windowing;
using ImGuiNET;
using RotationSolver.Localization;
using RotationSolver.Updaters;
using System.Collections;
using System.Text;

Expand Down Expand Up @@ -63,6 +64,17 @@ public override unsafe void Draw()

if (Service.Config.InDebug && ImGui.BeginTabItem("Debug"))
{
ImGui.Text(RotationUpdater.message);

foreach (var item in RotationHelper.DefaultAssembly)
{
ImGui.Text(item);
}
foreach (var item in RotationHelper.AllowedAssembly)
{
ImGui.Text(item);
}

DrawDebugTab();
ImGui.EndTabItem();
}
Expand Down
50 changes: 43 additions & 7 deletions RotationSolver/Updaters/RotationUpdater.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Dalamud.Logging;
using RotationSolver.Localization;
using System;
using System.Text;

namespace RotationSolver.Updaters;

Expand All @@ -12,21 +14,55 @@ public record CustomRotationGroup(ClassJobID jobId, ClassJobID[] classJobIds, IC
internal static SortedList<string, string> AuthorHashes { get; private set; } = new SortedList<string, string>();
static CustomRotationGroup[] _customRotations { get; set; } = new CustomRotationGroup[0];

static readonly string[] _locs = new string[] { "RotationSolver.dll", "RotationSolver.Basic.dll" };
public static string message = string.Empty;


public static void GetAllCustomRotations()
public static async void GetAllCustomRotations()
{
var directories = Service.Config.OtherLibs
.Select(s => s.Trim()).Append(Path.GetDirectoryName(Assembly.GetAssembly(typeof(ICustomRotation)).Location));
var relayFolder = Service.Interface.ConfigDirectory.FullName;
if (!Directory.Exists(relayFolder)) Directory.CreateDirectory(relayFolder);

using (var client = new HttpClient())
{
foreach (var url in Service.Config.OtherLibs)
{
var valid = Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out var uriResult)
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
if (!valid) continue;

try
{
var fileName = url.Split('/').LastOrDefault();
if (string.IsNullOrEmpty(fileName)) continue;
if (Path.GetExtension(fileName) != ".dll") continue;
var filePath = Path.Combine(relayFolder, fileName);

//Download
using (HttpResponseMessage response = await client.GetAsync(url))
{
await response.Content.CopyToAsync(new FileStream(filePath, File.Exists(filePath)
? FileMode.Open : FileMode.CreateNew));
}

PluginLog.Log($"Successfully download the {filePath}");
}
catch (Exception ex)
{
PluginLog.LogError(ex, $"failed to download from {url}");
}
}
}

var directories = Service.Config.OtherLibs
.Where(Directory.Exists)
.Append(Path.GetDirectoryName(Assembly.GetAssembly(typeof(ICustomRotation)).Location))
.Append(relayFolder);

var assemblies = from dir in directories
where Directory.Exists(dir)
from l in Directory.GetFiles(dir, "*.dll")
where !_locs.Any(l.Contains)
select RotationLoadContext.LoadFrom(l);

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

AuthorHashes = new SortedList<string, string>(
(from a in assemblies
Expand Down

0 comments on commit 4496afb

Please sign in to comment.