Detects assembly references and determines loading order automatically.

This commit is contained in:
2023-03-18 13:46:13 +08:00
parent ed2c216cf4
commit 0e4445e52b
7 changed files with 192 additions and 66 deletions

View File

@@ -7,7 +7,6 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEngine;
using Logger = Cryville.Common.Logger;
@@ -19,68 +18,12 @@ namespace Cryville.Crtr.Browsing {
public string[] CurrentDirectory { get; private set; }
static bool _init;
static readonly Dictionary<string, List<ResourceConverter>> converters
= new Dictionary<string, List<ResourceConverter>>();
static readonly Dictionary<string, string> localRes
= new Dictionary<string, string>();
public LegacyResourceManager(string rootPath) {
_rootPath = rootPath;
if (!_init) {
_init = true;
LoadExtension(typeof(Extensions.Umg.Extension));
var extensionDir = new DirectoryInfo(Path.Combine(rootPath, "extensions"));
if (extensionDir.Exists) {
foreach (var extension in extensionDir.EnumerateFiles("*.dll")) {
try {
using (Stream stream = extension.OpenRead()) {
var buf = new byte[stream.Length];
stream.Read(buf, 0, buf.Length);
var asm = Assembly.Load(buf);
foreach (var type in asm.GetTypes()) {
if (typeof(ExtensionInterface).IsAssignableFrom(type)) {
LoadExtension(type);
}
}
Logger.Log("main", 1, "Extension", "Loaded extension DLL {0}", extension.Name);
}
}
catch (Exception ex) {
Logger.Log("main", 4, "Extension", "An error occured while trying to load extension DLL {0}: {1}", extension.Name, ex);
}
}
}
}
}
static void LoadExtension(Type type) {
try {
var extension = (ExtensionInterface)Activator.CreateInstance(type);
var l1 = extension.GetResourceConverters();
if (l1 != null) {
foreach (var c in l1) {
var fs = c.GetSupportedFormats();
if (fs == null) continue;
foreach (var f in fs) {
if (f == null) continue;
if (!converters.ContainsKey(f))
converters.Add(f, new List<ResourceConverter> { c });
else converters[f].Add(c);
}
}
}
var l2 = extension.GetResourceFinders();
if (l2 != null) {
foreach (var f in l2) {
var name = f.Name;
var path = f.GetRootPath();
if (name != null && path != null) localRes.Add(name, path);
}
}
Logger.Log("main", 1, "Extension", "Loaded extension {0}", type);
}
catch (Exception ex) {
Logger.Log("main", 4, "Extension", "Failed to load extension {0}: {1}", type, ex);
ExtensionManager.Init(rootPath);
}
}
@@ -172,8 +115,9 @@ namespace Cryville.Crtr.Browsing {
public bool ImportItemFrom(string path) {
var file = new FileInfo(path);
if (!converters.ContainsKey(file.Extension)) return false;
foreach (var converter in converters[file.Extension]) {
IEnumerable<ResourceConverter> converters;
if (!ExtensionManager.TryGetConverters(file.Extension, out converters)) return false;
foreach (var converter in converters) {
IEnumerable<Resource> resources = null;
try {
resources = converter.ConvertFrom(file);
@@ -249,11 +193,11 @@ namespace Cryville.Crtr.Browsing {
}
public string[] GetSupportedFormats() {
return converters.Keys.ToArray();
return ExtensionManager.GetSupportedFormats().ToArray();
}
public Dictionary<string, string> GetPresetPaths() {
return localRes;
public IReadOnlyDictionary<string, string> GetPresetPaths() {
return ExtensionManager.GetLocalResourcePaths();
}
static Chart ConvertChartData(ChartData i) {