Implement extension.

This commit is contained in:
2023-03-17 18:03:59 +08:00
parent 609645c108
commit ed2c216cf4
4 changed files with 131 additions and 30 deletions

View File

@@ -1,11 +1,13 @@
using Cryville.Common;
using Cryville.Common.Unity;
using Cryville.Crtr.Extension;
using Cryville.Crtr.Extensions.Umg;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEngine;
using Logger = Cryville.Common.Logger;
@@ -16,6 +18,7 @@ namespace Cryville.Crtr.Browsing {
private DirectoryInfo[] items = new DirectoryInfo[0];
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
@@ -23,45 +26,64 @@ namespace Cryville.Crtr.Browsing {
public LegacyResourceManager(string rootPath) {
_rootPath = rootPath;
}
static LegacyResourceManager() {
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) {
foreach (var type in asm.GetTypes()) {
if (!type.IsSubclassOf(typeof(ExtensionInterface))) continue;
var ext = (ExtensionInterface)Activator.CreateInstance(type);
try {
var cs = ext.GetResourceConverters();
if (cs != null) {
foreach (var c in cs) {
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);
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);
}
}
var fs2 = ext.GetResourceFinders();
if (fs2 != null) {
foreach (var f in fs2) {
var name = f.Name;
var path = f.GetRootPath();
if (name != null && path != null)
localRes.Add(name, path);
}
catch (Exception ex) {
Logger.Log("main", 4, "Extension", "An error occured while trying to load extension DLL {0}: {1}", extension.Name, ex);
}
Logger.Log("main", 1, "Resource", "Loaded extension {0}", ReflectionHelper.GetNamespaceQualifiedName(type));
}
catch (Exception ex) {
Logger.Log("main", 4, "Resource", "Failed to initialize extension {0}: {1}", ReflectionHelper.GetNamespaceQualifiedName(type), 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);
}
}
public int ChangeDirectory(string[] dir) {
CurrentDirectory = dir;
cd = new DirectoryInfo(_rootPath + "/charts/" + string.Join("/", dir));

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b35ffffce02252548a66e18cf98050e2
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,60 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Scripting;
namespace Cryville.Crtr.Extension {
[Preserve]
public static class RefTypes {
[Preserve]
public static void PreserveEnumerable() {
IEnumerable<object> p = Enumerable.Empty<object>();
p.All(i => false);
p.Any();
p.Any(i => false);
p.Cast<object>();
p.Concat(Enumerable.Empty<object>());
p.Contains(null);
p.Count();
p.Count(i => false);
p.DefaultIfEmpty();
p.DefaultIfEmpty(null);
p.Distinct();
p.ElementAt(0);
p.ElementAtOrDefault(0);
p.First();
p.First(i => false);
p.FirstOrDefault();
p.FirstOrDefault(i => false);
p.GetEnumerator();
p.Last();
p.Last(i => false);
p.LastOrDefault();
p.LastOrDefault(i => false);
p.LongCount();
p.LongCount(i => false);
p.OrderBy(i => i).ThenBy(i => i);
p.OrderByDescending(i => i).ThenByDescending(i => i);
p.Reverse();
p.Select(i => i);
p.Select((i, j) => false);
p.SequenceEqual(p);
p.Single();
p.Single(i => false);
p.SingleOrDefault();
p.SingleOrDefault(i => false);
p.Skip(0);
p.SkipLast(0);
p.SkipWhile(i => false);
p.SkipWhile((i, j) => false);
p.Take(0);
p.TakeLast(0);
p.TakeWhile(i => false);
p.TakeWhile((i, j) => false);
p.ToArray();
p.ToHashSet();
p.ToList();
p.Where(i => false);
p.Where((i, j) => false);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 29a6376ce10b77e4099d2613876f9549
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: