111 lines
4.2 KiB
C#
111 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Reflection;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using VersionFlow.Runtime;
|
|
|
|
namespace VersionFlow.Editors
|
|
{
|
|
public static class PatchUploaderUtility
|
|
{
|
|
static Dictionary<BuilderConfig, PatchUploader> s_patchInstance = new Dictionary<BuilderConfig, PatchUploader>();
|
|
static Dictionary<string, Type> s_patchloaderTypesMapper;
|
|
static Dictionary<Type, string> s_patcherPrettyNameMapper;
|
|
|
|
static void PreparePatcherTypes()
|
|
{
|
|
if (s_patchloaderTypesMapper != null) return;
|
|
|
|
s_patchloaderTypesMapper = new Dictionary<string, Type>();
|
|
s_patcherPrettyNameMapper = new Dictionary<Type, string>();
|
|
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
|
|
{
|
|
if (assembly.IsDynamic) continue;
|
|
foreach (var type in assembly.GetExportedTypes())
|
|
{
|
|
if (type.IsAbstract) continue;
|
|
if (!typeof(PatchUploader).IsAssignableFrom(type)) continue;
|
|
|
|
s_patchloaderTypesMapper[type.FullName] = type;
|
|
var desAttName = $"{type.Namespace}.{type.Name}";
|
|
if (type.GetCustomAttribute<DescriptionAttribute>() is DescriptionAttribute desAtt)
|
|
{
|
|
desAttName = desAtt.Description;
|
|
}
|
|
s_patcherPrettyNameMapper[type] = desAttName;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static IEnumerable<Type> GetAllPatcherType(this BuilderConfig builder)
|
|
{
|
|
PreparePatcherTypes();
|
|
|
|
return s_patchloaderTypesMapper.Values;
|
|
}
|
|
public static Type GetPatcherType(string className)
|
|
{
|
|
s_patchloaderTypesMapper.TryGetValue(className, out var type);
|
|
return type;
|
|
}
|
|
public static string GetPatcherPrettyName(string className)
|
|
{
|
|
s_patcherPrettyNameMapper.TryGetValue(GetPatcherType(className), out var prettyName);
|
|
return prettyName;
|
|
}
|
|
public static string GetPatcherPrettyName(Type patcherType)
|
|
{
|
|
s_patcherPrettyNameMapper.TryGetValue(patcherType, out var prettyName);
|
|
return prettyName;
|
|
}
|
|
|
|
public static PatchUploader GetPatchLoader(this BuilderConfig builder)
|
|
{
|
|
PreparePatcherTypes();
|
|
|
|
if (!s_patchInstance.TryGetValue(builder, out var loader) || loader.GetType().FullName != builder.uploaderClassName)
|
|
{
|
|
s_patchInstance.Remove(builder);
|
|
s_patchloaderTypesMapper.TryGetValue(builder.uploaderClassName, out Type patchType);
|
|
|
|
if (patchType == null) //无效的patchuploader类名,置为Empty
|
|
{
|
|
builder.uploaderClassName = string.Empty;
|
|
EditorUtility.SetDirty(builder);
|
|
|
|
return null;
|
|
}
|
|
else //创建patchloader实例
|
|
{
|
|
var patchloaderInstance = ScriptableObject.CreateInstance(patchType) as PatchUploader;
|
|
patchloaderInstance.CfgChanged(builder, builder.uploaderCfgJson);
|
|
s_patchInstance[builder] = patchloaderInstance;
|
|
return patchloaderInstance;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return loader;
|
|
}
|
|
}
|
|
|
|
public static BuilderConfig FindBuilderInProject()
|
|
{
|
|
foreach (var guid in AssetDatabase.FindAssets("t:builderconfig", new string[] { "Assets" }))
|
|
{
|
|
var type = AssetDatabase.GetMainAssetTypeFromGUID(new GUID(guid));
|
|
if (type == null) continue;
|
|
if (type != typeof(BuilderConfig)) continue;
|
|
|
|
|
|
var path = AssetDatabase.GUIDToAssetPath(guid);
|
|
var builder = AssetDatabase.LoadAssetAtPath<BuilderConfig>(path);
|
|
return builder;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
} |