mirror of
https://github.com/goatcorp/Dalamud.git
synced 2026-01-01 13:23:40 +01:00
feat: add plugin installer
This commit is contained in:
parent
6d57da2fec
commit
fd95379aa3
8 changed files with 377 additions and 87 deletions
16
Dalamud/Plugin/Features/IHasConfigUi.cs
Normal file
16
Dalamud/Plugin/Features/IHasConfigUi.cs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
using System;
|
||||
using Dalamud.Interface;
|
||||
|
||||
namespace Dalamud.Plugin.Features
|
||||
{
|
||||
/// <summary>
|
||||
/// This interface represents a Dalamud plugin that has a configuration UI which can be triggered.
|
||||
/// </summary>
|
||||
public interface IHasConfigUi : IHasUi
|
||||
{
|
||||
/// <summary>
|
||||
/// An event handler that is fired when the plugin should show its configuration interface.
|
||||
/// </summary>
|
||||
EventHandler OpenConfigUi { get; }
|
||||
}
|
||||
}
|
||||
21
Dalamud/Plugin/Features/IHasUi.cs
Normal file
21
Dalamud/Plugin/Features/IHasUi.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Dalamud.Interface;
|
||||
|
||||
namespace Dalamud.Plugin.Features
|
||||
{
|
||||
/// <summary>
|
||||
/// This interface represents a Dalamud plugin that has user interface which can be drawn.
|
||||
/// </summary>
|
||||
public interface IHasUi : IDalamudPlugin
|
||||
{
|
||||
/// <summary>
|
||||
/// A function that gets called when Dalamud is ready to draw your UI.
|
||||
/// </summary>
|
||||
/// <param name="uiBuilder">An <see cref="UiBuilder"/> object you can use to e.g. load images.</param>
|
||||
void Draw(UiBuilder uiBuilder);
|
||||
}
|
||||
}
|
||||
17
Dalamud/Plugin/PluginDefinition.cs
Normal file
17
Dalamud/Plugin/PluginDefinition.cs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Dalamud.Plugin
|
||||
{
|
||||
public class PluginDefinition
|
||||
{
|
||||
public string Author { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string InternalName { get; set; }
|
||||
public string AssemblyVersion { get; set; }
|
||||
public string Description { get; set; }
|
||||
}
|
||||
}
|
||||
207
Dalamud/Plugin/PluginInstallerWindow.cs
Normal file
207
Dalamud/Plugin/PluginInstallerWindow.cs
Normal file
|
|
@ -0,0 +1,207 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Dalamud.Plugin.Features;
|
||||
using ImGuiNET;
|
||||
using Newtonsoft.Json;
|
||||
using Serilog;
|
||||
|
||||
namespace Dalamud.Plugin
|
||||
{
|
||||
class PluginInstallerWindow {
|
||||
private const string PluginRepoBaseUrl = "https://goaaats.github.io/DalamudPlugins/";
|
||||
|
||||
private PluginManager manager;
|
||||
private string pluginDirectory;
|
||||
private ReadOnlyCollection<PluginDefinition> pluginMaster;
|
||||
private bool errorModalDrawing = true;
|
||||
|
||||
private enum PluginInstallStatus {
|
||||
None,
|
||||
InProgress,
|
||||
Success,
|
||||
Fail
|
||||
}
|
||||
|
||||
private PluginInstallStatus installStatus = PluginInstallStatus.None;
|
||||
|
||||
private bool masterDownloadFailed = false;
|
||||
|
||||
public PluginInstallerWindow(PluginManager manager, string pluginDirectory) {
|
||||
this.manager = manager;
|
||||
this.pluginDirectory = pluginDirectory;
|
||||
Task.Run(CachePluginMaster).ContinueWith(t => { this.masterDownloadFailed = t.IsFaulted; });
|
||||
}
|
||||
|
||||
private void CachePluginMaster() {
|
||||
try {
|
||||
using var client = new WebClient();
|
||||
|
||||
var data = client.DownloadString(PluginRepoBaseUrl + "pluginmaster.json");
|
||||
|
||||
this.pluginMaster = JsonConvert.DeserializeObject<ReadOnlyCollection<PluginDefinition>>(data);
|
||||
} catch {
|
||||
this.masterDownloadFailed = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void InstallPlugin(PluginDefinition definition) {
|
||||
try {
|
||||
var path = Path.GetTempFileName();
|
||||
|
||||
Log.Information("Downloading plugin to {0}", path);
|
||||
|
||||
using var client = new WebClient();
|
||||
|
||||
client.DownloadFile(PluginRepoBaseUrl + $"/plugins/{definition.InternalName}/latest.zip", path);
|
||||
var outputDir = Path.Combine(this.pluginDirectory, definition.InternalName);
|
||||
|
||||
if (File.Exists(Path.Combine(outputDir, ".disabled"))) {
|
||||
Log.Information("Plugin was disabled, re-enabling.");
|
||||
File.Delete(Path.Combine(outputDir, ".disabled"));
|
||||
}
|
||||
|
||||
Log.Information("Extracting to {0}", outputDir);
|
||||
|
||||
Directory.CreateDirectory(outputDir);
|
||||
|
||||
ZipFile.ExtractToDirectory(path, outputDir);
|
||||
|
||||
this.installStatus = PluginInstallStatus.Success;
|
||||
this.manager.LoadPluginFromAssembly(new FileInfo(Path.Combine(outputDir, $"{definition.InternalName}.dll")));
|
||||
} catch (Exception e) {
|
||||
Log.Error(e, "Plugin download failed hard.");
|
||||
this.installStatus = PluginInstallStatus.Fail;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Draw() {
|
||||
var windowOpen = true;
|
||||
|
||||
ImGui.SetNextWindowSize(new Vector2(750, 520));
|
||||
|
||||
ImGui.Begin("Plugin Installer", ref windowOpen,
|
||||
ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoResize | ImGuiWindowFlags.NoScrollbar);
|
||||
|
||||
ImGui.Text("This window allows you install and remove in-game plugins.");
|
||||
ImGui.Text("They are made by third-party developers.");
|
||||
ImGui.Separator();
|
||||
|
||||
ImGui.BeginChild("scrolling", new Vector2(0, 400), true, ImGuiWindowFlags.HorizontalScrollbar);
|
||||
|
||||
ImGui.PushStyleVar(ImGuiStyleVar.ItemSpacing, new Vector2(1, 3));
|
||||
|
||||
if (this.pluginMaster == null) {
|
||||
ImGui.Text("Loading plugins...");
|
||||
} else if (this.masterDownloadFailed) {
|
||||
ImGui.Text("Download failed.");
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var pluginDefinition in this.pluginMaster)
|
||||
{
|
||||
if (ImGui.CollapsingHeader(pluginDefinition.Name))
|
||||
{
|
||||
ImGui.Indent();
|
||||
|
||||
ImGui.Text(pluginDefinition.Name);
|
||||
ImGui.SameLine();
|
||||
ImGui.TextColored(new Vector4(0.5f, 0.5f, 0.5f, 1.0f), $" by {pluginDefinition.Author}");
|
||||
|
||||
ImGui.Text(pluginDefinition.Description);
|
||||
|
||||
var isInstalled = this.manager.Plugins.Where(x=> x.Definition != null).Any(
|
||||
x => x.Definition.InternalName == pluginDefinition.InternalName);
|
||||
|
||||
if (!isInstalled) {
|
||||
if (this.installStatus == PluginInstallStatus.InProgress) {
|
||||
ImGui.Button($"Install in progress...");
|
||||
} else {
|
||||
if (ImGui.Button($"Install v{pluginDefinition.AssemblyVersion}"))
|
||||
{
|
||||
this.installStatus = PluginInstallStatus.InProgress;
|
||||
|
||||
Task.Run(() => InstallPlugin(pluginDefinition)).ContinueWith(t => { this.installStatus = t.IsFaulted ? PluginInstallStatus.Fail : this.installStatus; });
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
var installedPlugin = this.manager.Plugins.Where(x => x.Definition != null).First(
|
||||
x => x.Definition.InternalName == pluginDefinition.InternalName);
|
||||
|
||||
if (ImGui.Button("Disable"))
|
||||
{
|
||||
this.manager.DisablePlugin(installedPlugin.Definition);
|
||||
}
|
||||
|
||||
if (installedPlugin.Plugin is IHasConfigUi v2Plugin && v2Plugin.OpenConfigUi != null) {
|
||||
ImGui.SameLine();
|
||||
|
||||
if (ImGui.Button("Open Configuration"))
|
||||
{
|
||||
v2Plugin.OpenConfigUi?.Invoke(null, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ImGui.Unindent();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ImGui.PopStyleVar();
|
||||
|
||||
ImGui.EndChild();
|
||||
|
||||
ImGui.Separator();
|
||||
|
||||
if (ImGui.Button("Remove All"))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ImGui.SameLine();
|
||||
|
||||
if (ImGui.Button("Open Plugin folder"))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ImGui.SameLine();
|
||||
|
||||
if (ImGui.Button("Close"))
|
||||
{
|
||||
windowOpen = false;
|
||||
}
|
||||
|
||||
if (ImGui.Button("test modal")) {
|
||||
this.installStatus = PluginInstallStatus.Fail;
|
||||
}
|
||||
|
||||
ImGui.Spacing();
|
||||
|
||||
if (this.installStatus == PluginInstallStatus.Fail || this.masterDownloadFailed) {
|
||||
if (ImGui.BeginPopupModal("Installer failed", ref this.errorModalDrawing, ImGuiWindowFlags.AlwaysAutoResize))
|
||||
{
|
||||
ImGui.TextWrapped("The installer ran into an issue. Please restart the game and report this error on our discord.");
|
||||
|
||||
if (ImGui.Button("OK", new Vector2(120, 40))) { ImGui.CloseCurrentPopup(); }
|
||||
|
||||
ImGui.EndPopup();
|
||||
}
|
||||
}
|
||||
|
||||
ImGui.End();
|
||||
|
||||
return windowOpen;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,7 @@ using System.Linq;
|
|||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Serilog;
|
||||
|
||||
namespace Dalamud.Plugin
|
||||
|
|
@ -12,14 +13,14 @@ namespace Dalamud.Plugin
|
|||
public class PluginManager {
|
||||
private readonly Dalamud dalamud;
|
||||
private readonly string pluginDirectory;
|
||||
private readonly string defaultPluginDirectory;
|
||||
|
||||
public List<IDalamudPlugin> Plugins;
|
||||
private readonly Type interfaceType = typeof(IDalamudPlugin);
|
||||
|
||||
public PluginManager(Dalamud dalamud, string pluginDirectory, string defaultPluginDirectory) {
|
||||
public readonly List<(IDalamudPlugin Plugin, PluginDefinition Definition)> Plugins = new List<(IDalamudPlugin plugin, PluginDefinition def)>();
|
||||
|
||||
public PluginManager(Dalamud dalamud, string pluginDirectory) {
|
||||
this.dalamud = dalamud;
|
||||
this.pluginDirectory = pluginDirectory;
|
||||
this.defaultPluginDirectory = defaultPluginDirectory;
|
||||
}
|
||||
|
||||
public void UnloadPlugins() {
|
||||
|
|
@ -27,60 +28,83 @@ namespace Dalamud.Plugin
|
|||
return;
|
||||
|
||||
for (var i = 0; i < this.Plugins.Count; i++) {
|
||||
this.Plugins[i].Dispose();
|
||||
this.Plugins[i] = null;
|
||||
this.Plugins[i].Plugin.Dispose();
|
||||
}
|
||||
|
||||
this.Plugins.Clear();
|
||||
}
|
||||
|
||||
public void LoadPlugins() {
|
||||
LoadPluginsAt(new DirectoryInfo(this.defaultPluginDirectory));
|
||||
LoadPluginsAt(new DirectoryInfo(this.pluginDirectory));
|
||||
}
|
||||
|
||||
public void DisablePlugin(PluginDefinition definition) {
|
||||
var thisPlugin = this.Plugins.Where(x => x.Definition != null)
|
||||
.First(x => x.Definition.InternalName == definition.InternalName);
|
||||
|
||||
var outputDir = Path.Combine(this.pluginDirectory, definition.InternalName);
|
||||
File.Create(Path.Combine(outputDir, ".disabled"));
|
||||
|
||||
thisPlugin.Plugin.Dispose();
|
||||
|
||||
this.Plugins.Remove(thisPlugin);
|
||||
}
|
||||
|
||||
public void LoadPluginFromAssembly(FileInfo dllFile) {
|
||||
Log.Information("Loading assembly at {0}", dllFile);
|
||||
var assemblyName = AssemblyName.GetAssemblyName(dllFile.FullName);
|
||||
var pluginAssembly = Assembly.Load(assemblyName);
|
||||
|
||||
if (pluginAssembly != null)
|
||||
{
|
||||
Log.Information("Loading types for {0}", pluginAssembly.FullName);
|
||||
var types = pluginAssembly.GetTypes();
|
||||
foreach (var type in types)
|
||||
{
|
||||
if (type.IsInterface || type.IsAbstract)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (type.GetInterface(interfaceType.FullName) != null)
|
||||
{
|
||||
var plugin = (IDalamudPlugin)Activator.CreateInstance(type);
|
||||
|
||||
var dalamudInterface = new DalamudPluginInterface(this.dalamud, type.Assembly.GetName().Name);
|
||||
|
||||
var defJsonFile = new FileInfo(Path.Combine(dllFile.Directory.FullName, $"{Path.GetFileNameWithoutExtension(dllFile.Name)}.json"));
|
||||
|
||||
PluginDefinition pluginDef = null;
|
||||
if (defJsonFile.Exists)
|
||||
{
|
||||
Log.Information("Loading definition for plugin DLL {0}", dllFile.FullName);
|
||||
|
||||
pluginDef =
|
||||
JsonConvert.DeserializeObject<PluginDefinition>(
|
||||
File.ReadAllText(defJsonFile.FullName));
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Information("Plugin DLL {0} has no definition.", dllFile.FullName);
|
||||
}
|
||||
|
||||
plugin.Initialize(dalamudInterface);
|
||||
Log.Information("Loaded plugin: {0}", plugin.Name);
|
||||
this.Plugins.Add((plugin, pluginDef));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadPluginsAt(DirectoryInfo folder) {
|
||||
if (folder.Exists)
|
||||
{
|
||||
Log.Debug("Loading plugins at {0}", folder);
|
||||
Log.Information("Loading plugins at {0}", folder);
|
||||
|
||||
var pluginDlls = folder.GetFiles("*.dll", SearchOption.AllDirectories);
|
||||
|
||||
var assemblies = new List<Assembly>(pluginDlls.Length);
|
||||
foreach (var dllFile in pluginDlls)
|
||||
{
|
||||
Log.Debug("Loading assembly at {0}", dllFile);
|
||||
var assemblyName = AssemblyName.GetAssemblyName(dllFile.FullName);
|
||||
var pluginAssembly = Assembly.Load(assemblyName);
|
||||
assemblies.Add(pluginAssembly);
|
||||
}
|
||||
|
||||
var interfaceType = typeof(IDalamudPlugin);
|
||||
var foundImplementations = new List<Type>();
|
||||
foreach (var assembly in assemblies) {
|
||||
if (assembly != null) {
|
||||
Log.Debug("Loading types for {0}", assembly.FullName);
|
||||
var types = assembly.GetTypes();
|
||||
foreach (var type in types) {
|
||||
if (type.IsInterface || type.IsAbstract) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (type.GetInterface(interfaceType.FullName) != null) {
|
||||
foundImplementations.Add(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.Plugins = new List<IDalamudPlugin>(foundImplementations.Count);
|
||||
foreach (var pluginType in foundImplementations)
|
||||
{
|
||||
var plugin = (IDalamudPlugin)Activator.CreateInstance(pluginType);
|
||||
|
||||
var dalamudInterface = new DalamudPluginInterface(this.dalamud, pluginType.Assembly.GetName().Name);
|
||||
|
||||
plugin.Initialize(dalamudInterface);
|
||||
Log.Information("Loaded plugin: {0}", plugin.Name);
|
||||
this.Plugins.Add(plugin);
|
||||
foreach (var dllFile in pluginDlls) {
|
||||
LoadPluginFromAssembly(dllFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue