mirror of
https://github.com/soarqin/DSP_Mods.git
synced 2026-08-06 02:00:23 +08:00
refactor(UXAssist): split Util into focused helpers with forwarding facade
This commit is contained in:
+21
-54
@@ -1,78 +1,45 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UXAssist.Common.Utils;
|
||||||
|
|
||||||
namespace UXAssist.Common;
|
namespace UXAssist.Common;
|
||||||
|
|
||||||
public static class Util
|
public static class Util
|
||||||
{
|
{
|
||||||
/// <summary>
|
[Obsolete("Use ReflectionUtil.GetTypesFiltered")]
|
||||||
/// Returns all types from the assembly matching the predicate, tolerating partially loadable assemblies.
|
|
||||||
/// </summary>
|
|
||||||
public static Type[] GetTypesFiltered(Assembly assembly, Func<Type, bool> predicate)
|
public static Type[] GetTypesFiltered(Assembly assembly, Func<Type, bool> predicate)
|
||||||
{
|
=> ReflectionUtil.GetTypesFiltered(assembly, predicate);
|
||||||
try
|
|
||||||
{
|
|
||||||
return [.. assembly.GetTypes().Where(predicate)];
|
|
||||||
}
|
|
||||||
catch (ReflectionTypeLoadException ex)
|
|
||||||
{
|
|
||||||
return [.. ex.Types.Where(t => t != null).Where(predicate)];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Type[] GetTypesInNamespace(Assembly assembly, string nameSpace) => GetTypesFiltered(assembly, t => string.Equals(t.Namespace, nameSpace, StringComparison.Ordinal));
|
[Obsolete("Use ReflectionUtil.GetTypesInNamespace")]
|
||||||
|
public static Type[] GetTypesInNamespace(Assembly assembly, string nameSpace)
|
||||||
|
=> ReflectionUtil.GetTypesInNamespace(assembly, nameSpace);
|
||||||
|
|
||||||
|
[Obsolete("Use ReflectionUtil.GetTypesInNamespacePrefix")]
|
||||||
public static Type[] GetTypesInNamespacePrefix(Assembly assembly, string prefix)
|
public static Type[] GetTypesInNamespacePrefix(Assembly assembly, string prefix)
|
||||||
{
|
=> ReflectionUtil.GetTypesInNamespacePrefix(assembly, prefix);
|
||||||
return GetTypesFiltered(assembly, t =>
|
|
||||||
t.Namespace != null &&
|
|
||||||
(t.Namespace == prefix || t.Namespace.StartsWith(prefix + ".", StringComparison.Ordinal)));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
[Obsolete("Use ResourceUtil.LoadEmbeddedResource")]
|
||||||
public static byte[] LoadEmbeddedResource(string path, Assembly assembly = null)
|
public static byte[] LoadEmbeddedResource(string path, Assembly assembly = null)
|
||||||
{
|
=> ResourceUtil.LoadEmbeddedResource(path, assembly);
|
||||||
if (assembly == null)
|
|
||||||
{
|
|
||||||
assembly = Assembly.GetCallingAssembly();
|
|
||||||
}
|
|
||||||
var info = assembly.GetName();
|
|
||||||
var name = info.Name;
|
|
||||||
using var stream = assembly.GetManifestResourceStream($"{name}.{path.Replace('/', '.')}")!;
|
|
||||||
var buffer = new byte[stream.Length];
|
|
||||||
_ = stream.Read(buffer, 0, buffer.Length);
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
[Obsolete("Use ResourceUtil.LoadTexture")]
|
||||||
public static Texture2D LoadTexture(string path)
|
public static Texture2D LoadTexture(string path)
|
||||||
{
|
=> ResourceUtil.LoadTexture(path);
|
||||||
var fileData = File.ReadAllBytes(path);
|
|
||||||
var tex = new Texture2D(2, 2);
|
|
||||||
tex.LoadImage(fileData);
|
|
||||||
return tex;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
[Obsolete("Use ResourceUtil.LoadSprite")]
|
||||||
public static Sprite LoadSprite(string path)
|
public static Sprite LoadSprite(string path)
|
||||||
{
|
=> ResourceUtil.LoadSprite(path);
|
||||||
var tex = LoadTexture(path);
|
|
||||||
return Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
[Obsolete("Use ResourceUtil.LoadEmbeddedTexture")]
|
||||||
public static Texture2D LoadEmbeddedTexture(string path, Assembly assembly = null)
|
public static Texture2D LoadEmbeddedTexture(string path, Assembly assembly = null)
|
||||||
{
|
=> ResourceUtil.LoadEmbeddedTexture(path, assembly);
|
||||||
var fileData = LoadEmbeddedResource(path, assembly);
|
|
||||||
var tex = new Texture2D(2, 2);
|
|
||||||
tex.LoadImage(fileData);
|
|
||||||
return tex;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
[Obsolete("Use ResourceUtil.LoadEmbeddedSprite")]
|
||||||
public static Sprite LoadEmbeddedSprite(string path, Assembly assembly = null)
|
public static Sprite LoadEmbeddedSprite(string path, Assembly assembly = null)
|
||||||
{
|
=> ResourceUtil.LoadEmbeddedSprite(path, assembly);
|
||||||
var tex = LoadEmbeddedTexture(path, assembly);
|
|
||||||
return Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string PluginFolder(Assembly assembly = null) => Path.GetDirectoryName((assembly == null ? Assembly.GetCallingAssembly() : assembly).Location);
|
[Obsolete("Use PathUtil.PluginFolder")]
|
||||||
|
public static string PluginFolder(Assembly assembly = null)
|
||||||
|
=> PathUtil.PluginFolder(assembly);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace UXAssist.Common.Utils;
|
||||||
|
|
||||||
|
public static class PathUtil
|
||||||
|
{
|
||||||
|
public static string PluginFolder(Assembly assembly = null) => Path.GetDirectoryName((assembly == null ? Assembly.GetCallingAssembly() : assembly).Location);
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace UXAssist.Common.Utils;
|
||||||
|
|
||||||
|
public static class ReflectionUtil
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Returns all types from the assembly matching the predicate, tolerating partially loadable assemblies.
|
||||||
|
/// </summary>
|
||||||
|
public static Type[] GetTypesFiltered(Assembly assembly, Func<Type, bool> predicate)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return [.. assembly.GetTypes().Where(predicate)];
|
||||||
|
}
|
||||||
|
catch (ReflectionTypeLoadException ex)
|
||||||
|
{
|
||||||
|
return [.. ex.Types.Where(t => t != null).Where(predicate)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Type[] GetTypesInNamespace(Assembly assembly, string nameSpace) => GetTypesFiltered(assembly, t => string.Equals(t.Namespace, nameSpace, StringComparison.Ordinal));
|
||||||
|
|
||||||
|
public static Type[] GetTypesInNamespacePrefix(Assembly assembly, string prefix)
|
||||||
|
{
|
||||||
|
return GetTypesFiltered(assembly, t =>
|
||||||
|
t.Namespace != null &&
|
||||||
|
(t.Namespace == prefix || t.Namespace.StartsWith(prefix + ".", StringComparison.Ordinal)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Reflection;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace UXAssist.Common.Utils;
|
||||||
|
|
||||||
|
public static class ResourceUtil
|
||||||
|
{
|
||||||
|
public static byte[] LoadEmbeddedResource(string path, Assembly assembly = null)
|
||||||
|
{
|
||||||
|
if (assembly == null)
|
||||||
|
{
|
||||||
|
assembly = Assembly.GetCallingAssembly();
|
||||||
|
}
|
||||||
|
var info = assembly.GetName();
|
||||||
|
var name = info.Name;
|
||||||
|
using var stream = assembly.GetManifestResourceStream($"{name}.{path.Replace('/', '.')}")!;
|
||||||
|
var buffer = new byte[stream.Length];
|
||||||
|
_ = stream.Read(buffer, 0, buffer.Length);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Texture2D LoadTexture(string path)
|
||||||
|
{
|
||||||
|
var fileData = File.ReadAllBytes(path);
|
||||||
|
var tex = new Texture2D(2, 2);
|
||||||
|
tex.LoadImage(fileData);
|
||||||
|
return tex;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Sprite LoadSprite(string path)
|
||||||
|
{
|
||||||
|
var tex = LoadTexture(path);
|
||||||
|
return Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Texture2D LoadEmbeddedTexture(string path, Assembly assembly = null)
|
||||||
|
{
|
||||||
|
var fileData = LoadEmbeddedResource(path, assembly);
|
||||||
|
var tex = new Texture2D(2, 2);
|
||||||
|
tex.LoadImage(fileData);
|
||||||
|
return tex;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Sprite LoadEmbeddedSprite(string path, Assembly assembly = null)
|
||||||
|
{
|
||||||
|
var tex = LoadEmbeddedTexture(path, assembly);
|
||||||
|
return Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user