修复编译错误

This commit is contained in:
ALIENJACK\alien 2024-12-26 10:40:59 +08:00
parent 62fbb6e11b
commit 7b29000a70
2 changed files with 373 additions and 374 deletions

View File

@ -1,4 +1,4 @@
#if UNITY_EDITOR #if UNITY_EDITOR
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -6,6 +6,7 @@ using System.Linq;
using UnityEditor; using UnityEditor;
using UnityEditor.SceneManagement; using UnityEditor.SceneManagement;
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement;
public class AxiProjectTools : EditorWindow public class AxiProjectTools : EditorWindow
{ {
@ -28,7 +29,7 @@ public class AxiProjectTools : EditorWindow
} }
} }
[MenuItem("Axibug移植工具/[1]UGUI组件")] [MenuItem("Axibug移植工具/[1]UGUI组件")]
public static void Part1() public static void Part1()
{ {
GoTAxiProjectToolsSence(); GoTAxiProjectToolsSence();
@ -42,17 +43,17 @@ public class AxiProjectTools : EditorWindow
EditorSceneManager.OpenScene(path); EditorSceneManager.OpenScene(path);
// 创建一个列表来存储根节点 // 创建一个列表来存储根节点
List<GameObject> rootNodes = new List<GameObject>(); List<GameObject> rootNodes = new List<GameObject>();
// 遍历场景中的所有对象 // 遍历场景中的所有对象
GameObject[] allObjects = FindObjectsOfType<GameObject>(); GameObject[] allObjects = FindObjectsOfType<GameObject>();
foreach (GameObject obj in allObjects) foreach (GameObject obj in allObjects)
{ {
// 检查对象是否有父对象 // 检查对象是否有父对象
if (obj.transform.parent == null) if (obj.transform.parent == null)
{ {
// 如果没有父对象,则它是一个根节点 // 如果没有父对象,则它是一个根节点
rootNodes.Add(obj); rootNodes.Add(obj);
} }
} }
@ -76,7 +77,7 @@ public class AxiProjectTools : EditorWindow
AssetDatabase.SaveAssets(); AssetDatabase.SaveAssets();
AssetDatabase.Refresh(); AssetDatabase.Refresh();
GoTAxiProjectToolsSence(); GoTAxiProjectToolsSence();
Debug.Log("<Color=#FFF333>处理完毕 [1]采集所有预制体和场景下的UGUI组件</color>"); Debug.Log("<Color=#FFF333>处理完毕 [1]采集所有预制体和场景下的UGUI组件</color>");
} }
static void GetPrefab(string path) static void GetPrefab(string path)
@ -102,11 +103,11 @@ public class AxiProjectTools : EditorWindow
Type monoType = monoCom.GetType(); Type monoType = monoCom.GetType();
if (!monoType.Assembly.FullName.Contains("UnityEngine.UI")) if (!monoType.Assembly.FullName.Contains("UnityEngine.UI"))
continue; continue;
// 获取MonoScript资源 // 获取MonoScript资源
MonoScript monoScript = MonoScript.FromMonoBehaviour(monoCom); MonoScript monoScript = MonoScript.FromMonoBehaviour(monoCom);
if (monoScript != null) if (monoScript != null)
{ {
// 获取MonoScript资源的GUID // 获取MonoScript资源的GUID
string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(monoScript)); string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(monoScript));
Debug.Log($"{nodename} | <color=#FFF333>[{monoType.Name}]</color> <color=#FF0000>{guid}</color><color=#00FF00>({monoType.FullName})</color>"); Debug.Log($"{nodename} | <color=#FFF333>[{monoType.Name}]</color> <color=#FF0000>{guid}</color><color=#00FF00>({monoType.FullName})</color>");
ComType2GUID[monoType.FullName] = ComType2GUID[monoType.FullName] =
@ -119,19 +120,19 @@ public class AxiProjectTools : EditorWindow
} }
else else
{ {
Debug.LogError("!!!! 没得"); Debug.LogError("!!!! 没得");
} }
} }
//遍历 //遍历
foreach (Transform child in trans.transform) foreach (Transform child in trans.transform)
LoopPrefabNode(nodename, child.gameObject, depth + 1); LoopPrefabNode(nodename, child.gameObject, depth + 1);
#else #else
Debug.Log("低版本不要执行本函数"); Debug.Log("低版本不要执行本函数");
#endif #endif
} }
[MenuItem("Axibug移植工具/[2]")] [MenuItem("Axibug移植工具/[2]")]
public static void Part2() public static void Part2()
{ {
if (UnityEngine.Windows.Directory.Exists(outCsDir)) if (UnityEngine.Windows.Directory.Exists(outCsDir))
@ -151,16 +152,16 @@ public class AxiProjectTools : EditorWindow
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.LogError("写入失败" + ex.ToString()); Debug.LogError("写入失败" + ex.ToString());
} }
} }
Debug.Log("写入完毕"); Debug.Log("写入完毕");
AssetDatabase.SaveAssets(); AssetDatabase.SaveAssets();
AssetDatabase.Refresh(); AssetDatabase.Refresh();
Debug.Log("<Color=#FFF333>处理完毕 [2]生成中间脚本代码</color>"); Debug.Log("<Color=#FFF333>处理完毕 [2]生成中间脚本代码</color>");
} }
[MenuItem("Axibug移植工具/[3]")] [MenuItem("Axibug移植工具/[3]")]
public static void Part3() public static void Part3()
{ {
AxiPrefabCache cache = AssetDatabase.LoadAssetAtPath<AxiPrefabCache>(cachecfgPath); AxiPrefabCache cache = AssetDatabase.LoadAssetAtPath<AxiPrefabCache>(cachecfgPath);
@ -170,17 +171,17 @@ public class AxiProjectTools : EditorWindow
MonoScript monoScript = allMonoScripts.FirstOrDefault(w => w.name == data.ToName); MonoScript monoScript = allMonoScripts.FirstOrDefault(w => w.name == data.ToName);
if (monoScript == null) if (monoScript == null)
{ {
Debug.LogError("没找到" + data.ToName); Debug.LogError("没找到" + data.ToName);
continue; continue;
} }
string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(monoScript)); string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(monoScript));
data.ToGUID = guid; data.ToGUID = guid;
data.monoScript = monoScript; data.monoScript = monoScript;
} }
Debug.Log("写入完毕"); Debug.Log("写入完毕");
AssetDatabase.SaveAssets(); AssetDatabase.SaveAssets();
AssetDatabase.Refresh(); AssetDatabase.Refresh();
Debug.Log("<Color=#FFF333>处理完毕 [3]收集生成的脚本</color>"); Debug.Log("<Color=#FFF333>处理完毕 [3]收集生成的脚本</color>");
} }
static List<T> FindAllAssetsOfType<T>() where T : UnityEngine.Object static List<T> FindAllAssetsOfType<T>() where T : UnityEngine.Object
@ -191,7 +192,7 @@ public class AxiProjectTools : EditorWindow
foreach (string guid in allGuids) foreach (string guid in allGuids)
{ {
string path = AssetDatabase.GUIDToAssetPath(guid); string path = AssetDatabase.GUIDToAssetPath(guid);
if (path.EndsWith(".cs") || path.EndsWith(".js") || path.EndsWith(".boo")) // Unity支持多种脚本语言但现代Unity主要使用C# if (path.EndsWith(".cs") || path.EndsWith(".js") || path.EndsWith(".boo")) // Unity支持多种脚本语言但现代Unity主要使用C#
{ {
T asset = AssetDatabase.LoadAssetAtPath<T>(path); T asset = AssetDatabase.LoadAssetAtPath<T>(path);
if (asset != null) if (asset != null)
@ -204,7 +205,7 @@ public class AxiProjectTools : EditorWindow
} }
[MenuItem("Axibug移植工具/[4]")] [MenuItem("Axibug移植工具/[4]")]
public static void Part4() public static void Part4()
{ {
AxiPrefabCache cache = AssetDatabase.LoadAssetAtPath<AxiPrefabCache>(cachecfgPath); AxiPrefabCache cache = AssetDatabase.LoadAssetAtPath<AxiPrefabCache>(cachecfgPath);
@ -218,7 +219,7 @@ public class AxiProjectTools : EditorWindow
ProcessAllPrefabs("*.anim", tempReplaceDict); ProcessAllPrefabs("*.anim", tempReplaceDict);
AssetDatabase.SaveAssets(); AssetDatabase.SaveAssets();
AssetDatabase.Refresh(); AssetDatabase.Refresh();
Debug.Log("<Color=#FFF333>处理完毕 [4]替换所有预制体和场景中的组件</color>"); Debug.Log("<Color=#FFF333>处理完毕 [4]替换所有预制体和场景中的组件</color>");
} }
static void ProcessAllPrefabs(string form, Dictionary<string, string> tempReplaceDict, bool reverse = false) static void ProcessAllPrefabs(string form, Dictionary<string, string> tempReplaceDict, bool reverse = false)
@ -235,15 +236,15 @@ public class AxiProjectTools : EditorWindow
string newValue = reverse ? VARIABLE.Key : VARIABLE.Value; string newValue = reverse ? VARIABLE.Key : VARIABLE.Value;
ReplaceValue(absolutePaths[i], oldValue, newValue); ReplaceValue(absolutePaths[i], oldValue, newValue);
} }
EditorUtility.DisplayProgressBar("处理预制体……", "处理预制体中……", (float)i / absolutePaths.Length); EditorUtility.DisplayProgressBar("处理预制体……", "处理预制体中……", (float)i / absolutePaths.Length);
} }
EditorUtility.ClearProgressBar(); EditorUtility.ClearProgressBar();
} }
/// <summary> /// <summary>
/// 替换值 /// 替换值
/// </summary> /// </summary>
/// <param name="strFilePath">文件路径</param> /// <param name="strFilePath">文件路径</param>
static void ReplaceValue(string strFilePath, string oldLine, string newLine) static void ReplaceValue(string strFilePath, string oldLine, string newLine)
{ {
if (File.Exists(strFilePath)) if (File.Exists(strFilePath))
@ -258,7 +259,7 @@ public class AxiProjectTools : EditorWindow
} }
[MenuItem("Axibug移植工具/[5]UnPack所有嵌套预制体和场景中的预制体")] [MenuItem("Axibug移植工具/[5]UnPack所有嵌套预制体和场景中的预制体")]
public static void UnpackPrefabs() public static void UnpackPrefabs()
{ {
@ -276,7 +277,7 @@ public class AxiProjectTools : EditorWindow
prefabCount++; prefabCount++;
} }
} }
Debug.Log($"{prefabCount}个预制体Unpack"); Debug.Log($"{prefabCount}个预制体Unpack");
string[] sceneGuids = AssetDatabase.FindAssets("t:scene"); string[] sceneGuids = AssetDatabase.FindAssets("t:scene");
foreach (string guid in sceneGuids) foreach (string guid in sceneGuids)
@ -290,21 +291,21 @@ public class AxiProjectTools : EditorWindow
GameObject[] rootObjects = currentScene.GetRootGameObjects(); GameObject[] rootObjects = currentScene.GetRootGameObjects();
foreach (GameObject rootObj in rootObjects) foreach (GameObject rootObj in rootObjects)
{ {
// 遍历场景中的所有对象 // 遍历场景中的所有对象
TraverseHierarchy(rootObj); TraverseHierarchy(rootObj);
} }
// Save the scene // 获取当前打开的场景 // Save the scene // 获取当前打开的场景
currentScene = EditorSceneManager.GetActiveScene(); currentScene = EditorSceneManager.GetActiveScene();
// 保存场景到文件(默认路径和名称) // 保存场景到文件(默认路径和名称)
bool success = EditorSceneManager.SaveScene(currentScene, currentScene.path); bool success = EditorSceneManager.SaveScene(currentScene, currentScene.path);
Debug.Log($"{currentScene.name}场景中 所有物体Unpack"); Debug.Log($"{currentScene.name}场景中 所有物体Unpack");
} }
GoTAxiProjectToolsSence(); GoTAxiProjectToolsSence();
Debug.Log("<Color=#FFF333>处理完毕 [5]UnPack所有预制体</color>"); Debug.Log("<Color=#FFF333>处理完毕 [5]UnPack所有预制体</color>");
#else #else
Debug.Log("低版本不要执行本函数"); Debug.Log("低版本不要执行本函数");
#endif #endif
} }
@ -323,33 +324,33 @@ public class AxiProjectTools : EditorWindow
PrefabUtility.SaveAsPrefabAsset(obj, prefabPath); PrefabUtility.SaveAsPrefabAsset(obj, prefabPath);
GameObject.DestroyImmediate(obj); GameObject.DestroyImmediate(obj);
#else #else
Debug.Log("低版本不要执行本函数"); Debug.Log("低版本不要执行本函数");
#endif #endif
} }
static void TraverseHierarchy(GameObject obj) static void TraverseHierarchy(GameObject obj)
{ {
#if UNITY_2018_4_OR_NEWER #if UNITY_2018_4_OR_NEWER
// 检查该对象是否是预制体的实例 // 检查该对象是否是预制体的实例
if (PrefabUtility.IsPartOfPrefabInstance(obj)) if (PrefabUtility.IsPartOfPrefabInstance(obj))
{ {
// 将预制体实例转换为普通游戏对象 // 将预制体实例转换为普通游戏对象
PrefabUtility.UnpackPrefabInstance(obj, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction); PrefabUtility.UnpackPrefabInstance(obj, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
Debug.Log("Prefab instance converted to game object: " + obj.name); Debug.Log("Prefab instance converted to game object: " + obj.name);
} }
// 递归遍历子对象 // 递归遍历子对象
for (int i = 0; i < obj.transform.childCount; i++) for (int i = 0; i < obj.transform.childCount; i++)
{ {
TraverseHierarchy(obj.transform.GetChild(i).gameObject); TraverseHierarchy(obj.transform.GetChild(i).gameObject);
} }
#else #else
Debug.Log("低版本不要执行本函数"); Debug.Log("低版本不要执行本函数");
#endif #endif
} }
[MenuItem("Axibug移植工具/[6]Sprite")] [MenuItem("Axibug移植工具/[6]Sprite")]
public static void FixMultipleMaterialSprites() public static void FixMultipleMaterialSprites()
{ {
string[] guids = AssetDatabase.FindAssets("t:sprite"); string[] guids = AssetDatabase.FindAssets("t:sprite");
@ -360,7 +361,7 @@ public class AxiProjectTools : EditorWindow
string path = AssetDatabase.GUIDToAssetPath(guid); string path = AssetDatabase.GUIDToAssetPath(guid);
Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path); Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path);
// 检查是否有多个材质 // 检查是否有多个材质
if (IsUsingMultipleMaterials(sprite)) if (IsUsingMultipleMaterials(sprite))
{ {
spritesToFix.Add(sprite); spritesToFix.Add(sprite);
@ -368,7 +369,7 @@ public class AxiProjectTools : EditorWindow
} }
} }
// 修复每个找到的Sprite // 修复每个找到的Sprite
foreach (var sprite in spritesToFix) foreach (var sprite in spritesToFix)
{ {
FixSprite(sprite); FixSprite(sprite);
@ -376,14 +377,14 @@ public class AxiProjectTools : EditorWindow
AssetDatabase.SaveAssets(); AssetDatabase.SaveAssets();
AssetDatabase.Refresh(); AssetDatabase.Refresh();
Debug.Log("<Color=#FFF333>处理完毕 [6]修复Sprite</color>"); Debug.Log("<Color=#FFF333>处理完毕 [6]修复Sprite</color>");
} }
private static bool IsUsingMultipleMaterials(Sprite sprite) private static bool IsUsingMultipleMaterials(Sprite sprite)
{ {
if (sprite == null) return false; if (sprite == null) return false;
// 获取精灵的材质 // 获取精灵的材质
var textureImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(sprite)) as TextureImporter; var textureImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(sprite)) as TextureImporter;
return textureImporter != null && textureImporter.spriteImportMode == SpriteImportMode.Multiple; return textureImporter != null && textureImporter.spriteImportMode == SpriteImportMode.Multiple;
@ -391,24 +392,24 @@ public class AxiProjectTools : EditorWindow
private static void FixSprite(Sprite sprite) private static void FixSprite(Sprite sprite)
{ {
// 获取Sprite的路径 // 获取Sprite的路径
string path = AssetDatabase.GetAssetPath(sprite); string path = AssetDatabase.GetAssetPath(sprite);
var textureImporter = AssetImporter.GetAtPath(path) as TextureImporter; var textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
if (textureImporter != null) if (textureImporter != null)
{ {
// 保存当前切割信息 // 保存当前切割信息
SpriteMetaData[] originalMetaData = textureImporter.spritesheet; SpriteMetaData[] originalMetaData = textureImporter.spritesheet;
// 临时禁用Sprite导入 // 临时禁用Sprite导入
textureImporter.spriteImportMode = SpriteImportMode.None; textureImporter.spriteImportMode = SpriteImportMode.None;
textureImporter.SaveAndReimport(); textureImporter.SaveAndReimport();
// 重新启用Sprite导入并保持原样切割参数 // 重新启用Sprite导入并保持原样切割参数
textureImporter.spriteImportMode = SpriteImportMode.Multiple; textureImporter.spriteImportMode = SpriteImportMode.Multiple;
textureImporter.spritesheet = originalMetaData; // 恢复原来的切割信息 textureImporter.spritesheet = originalMetaData; // 恢复原来的切割信息
// 重新导入以应用更改 // 重新导入以应用更改
textureImporter.SaveAndReimport(); textureImporter.SaveAndReimport();
} }
} }

View File

@ -7,19 +7,19 @@ using static AxibugEmuOnline.Client.FilterEffect;
namespace AxibugEmuOnline.Client namespace AxibugEmuOnline.Client
{ {
public class FilterManager public class FilterManager
{ {
private List<Filter> m_filters; private List<Filter> m_filters;
private Dictionary<EnumPlatform, Filter> m_filterPlatforms = new Dictionary<EnumPlatform, Filter>(); private Dictionary<EnumPlatform, Filter> m_filterPlatforms = new Dictionary<EnumPlatform, Filter>();
private AlphaWraper m_previewFilterWraper; private AlphaWraper m_previewFilterWraper;
FilterRomSetting m_filterRomSetting; FilterRomSetting m_filterRomSetting;
/// <summary> /// <summary>
/// 滤镜列表 /// 滤镜列表
/// </summary> /// </summary>
public IReadOnlyList<Filter> Filters => m_filters; public IReadOnlyList<Filter> Filters => m_filters;
public FilterManager(CanvasGroup filterPreview, CanvasGroup mainBg) public FilterManager(CanvasGroup filterPreview, CanvasGroup mainBg)
{ {
#if UNITY_PSP2 #if UNITY_PSP2
m_filters = new List<Filter>(); m_filters = new List<Filter>();
m_filterRomSetting = new FilterRomSetting(); m_filterRomSetting = new FilterRomSetting();
@ -27,376 +27,374 @@ namespace AxibugEmuOnline.Client
return; return;
#endif #endif
m_filters = new List<Filter> m_filters = new List<Filter>
{ {
new Filter(new FixingPixelArtGrille()), new Filter(new FixingPixelArtGrille()),
new Filter(new LCDPostEffect()), new Filter(new LCDPostEffect()),
new Filter(new MattiasCRT()), new Filter(new MattiasCRT()),
}; };
var json = PlayerPrefs.GetString(nameof(FilterRomSetting)); var json = PlayerPrefs.GetString(nameof(FilterRomSetting));
m_filterRomSetting = JsonUtility.FromJson<FilterRomSetting>(json) ?? new FilterRomSetting(); m_filterRomSetting = JsonUtility.FromJson<FilterRomSetting>(json) ?? new FilterRomSetting();
m_previewFilterWraper = new AlphaWraper(mainBg, filterPreview, false); m_previewFilterWraper = new AlphaWraper(mainBg, filterPreview, false);
ShutDownFilterPreview(); ShutDownFilterPreview();
ShutDownFilter(); ShutDownFilter();
} }
private RenderTexture result = null; private RenderTexture result = null;
public Texture ExecuteFilterRender(Texture src) public Texture ExecuteFilterRender(Texture src)
{ {
if (result == null) if (result == null)
{ {
//result = RenderTexture.GetTemporary(Screen.width, Screen.height); result = RenderTexture.GetTemporary(Screen.width, Screen.height);
result = Initer.instance.renderTest; }
} else if (result.width != Screen.width || result.height != Screen.height)
else if (result.width != Screen.width || result.height != Screen.height) {
{ RenderTexture.ReleaseTemporary(result);
//RenderTexture.ReleaseTemporary(result); result = RenderTexture.GetTemporary(Screen.width, Screen.height);
//result = RenderTexture.GetTemporary(Screen.width, Screen.height); }
result = Initer.instance.renderTest;
}
bool anyFilterEnable = false; bool anyFilterEnable = false;
foreach (var filter in Filters) foreach (var filter in Filters)
{ {
if (!filter.m_setting.Enable.GetValue()) continue; if (!filter.m_setting.Enable.GetValue()) continue;
filter.m_setting.Render(src, result); filter.m_setting.Render(src, result);
anyFilterEnable = true; anyFilterEnable = true;
} }
if (anyFilterEnable) if (anyFilterEnable)
return result; return result;
else else
return src; return src;
} }
/// <summary> 关闭滤镜预览 </summary> /// <summary> 关闭滤镜预览 </summary>
public void ShutDownFilterPreview() public void ShutDownFilterPreview()
{ {
m_previewFilterWraper.On = false; m_previewFilterWraper.On = false;
} }
/// <summary> 开启滤镜预览 </summary> /// <summary> 开启滤镜预览 </summary>
public void EnableFilterPreview() public void EnableFilterPreview()
{ {
m_previewFilterWraper.On = true; m_previewFilterWraper.On = true;
} }
/// <summary> /// <summary>
/// 打开滤镜 /// 打开滤镜
/// </summary> /// </summary>
/// <param name="filter"></param> /// <param name="filter"></param>
public void EnableFilter(Filter filter) public void EnableFilter(Filter filter)
{ {
foreach (var selfFiler in Filters) foreach (var selfFiler in Filters)
{ {
if (selfFiler != filter) selfFiler.m_setting.Enable.Override(false); if (selfFiler != filter) selfFiler.m_setting.Enable.Override(false);
else selfFiler.m_setting.Enable.Override(true); else selfFiler.m_setting.Enable.Override(true);
} }
} }
/// <summary> /// <summary>
/// 关闭滤镜效果 /// 关闭滤镜效果
/// </summary> /// </summary>
public void ShutDownFilter() public void ShutDownFilter()
{ {
//关闭所有后处理效果 //关闭所有后处理效果
foreach (var filter in Filters) foreach (var filter in Filters)
filter.m_setting.Enable.Override(false); filter.m_setting.Enable.Override(false);
} }
/// <summary> /// <summary>
/// 为指定rom设置滤镜以及滤镜的预设 /// 为指定rom设置滤镜以及滤镜的预设
/// </summary> /// </summary>
/// <param name="rom">rom对象</param> /// <param name="rom">rom对象</param>
/// <param name="filter">滤镜</param> /// <param name="filter">滤镜</param>
/// <param name="preset">滤镜预设</param> /// <param name="preset">滤镜预设</param>
public void SetupFilter(RomFile rom, Filter filter, FilterPreset preset) public void SetupFilter(RomFile rom, Filter filter, FilterPreset preset)
{ {
m_filterRomSetting.Setup(rom, filter, preset); m_filterRomSetting.Setup(rom, filter, preset);
string json = m_filterRomSetting.ToJson(); string json = m_filterRomSetting.ToJson();
PlayerPrefs.SetString(nameof(FilterRomSetting), json); PlayerPrefs.SetString(nameof(FilterRomSetting), json);
} }
/// <summary> /// <summary>
/// 获得指定rom配置的滤镜设置 /// 获得指定rom配置的滤镜设置
/// </summary> /// </summary>
/// <param name="rom">rom对象</param> /// <param name="rom">rom对象</param>
/// <returns>此元组任意内任意成员都有可能为空</returns> /// <returns>此元组任意内任意成员都有可能为空</returns>
public GetFilterSetting_result GetFilterSetting(RomFile rom) public GetFilterSetting_result GetFilterSetting(RomFile rom)
{ {
var value = m_filterRomSetting.Get(rom); var value = m_filterRomSetting.Get(rom);
Filter filter = null; Filter filter = null;
FilterPreset preset = null; FilterPreset preset = null;
//filter = Filters.FirstOrDefault(f => f.Name == value.filterName); //filter = Filters.FirstOrDefault(f => f.Name == value.filterName);
//if (filter != null) //if (filter != null)
//{ //{
// string presetName = value.presetName; // string presetName = value.presetName;
// preset = filter.Presets.FirstOrDefault(p => p.Name == presetName); // preset = filter.Presets.FirstOrDefault(p => p.Name == presetName);
//} //}
filter = Filters.FirstOrDefault(f => f.Name == value.Item1); filter = Filters.FirstOrDefault(f => f.Name == value.Item1);
if (filter != null) if (filter != null)
{ {
string presetName = value.Item2; string presetName = value.Item2;
preset = filter.Presets.FirstOrDefault(p => p.Name == presetName); preset = filter.Presets.FirstOrDefault(p => p.Name == presetName);
} }
return new GetFilterSetting_result() return new GetFilterSetting_result()
{ {
filter = filter, filter = filter,
preset = preset preset = preset
}; };
} }
public struct GetFilterSetting_result public struct GetFilterSetting_result
{ {
public Filter filter; public Filter filter;
public FilterPreset preset; public FilterPreset preset;
} }
public class Filter public class Filter
{ {
public string Name => m_setting.Name; public string Name => m_setting.Name;
public IReadOnlyCollection<EditableParamerter> Paramerters => m_setting.EditableParam; public IReadOnlyCollection<EditableParamerter> Paramerters => m_setting.EditableParam;
/// <summary> 滤镜预设 </summary> /// <summary> 滤镜预设 </summary>
public List<FilterPreset> Presets = new List<FilterPreset>(); public List<FilterPreset> Presets = new List<FilterPreset>();
/// <summary> 滤镜默认预设 </summary> /// <summary> 滤镜默认预设 </summary>
public FilterPreset DefaultPreset = new FilterPreset("DEFAULT"); public FilterPreset DefaultPreset = new FilterPreset("DEFAULT");
internal FilterEffect m_setting; internal FilterEffect m_setting;
public Filter(FilterEffect setting) public Filter(FilterEffect setting)
{ {
m_setting = setting; m_setting = setting;
loadPresets(); loadPresets();
} }
private void loadPresets() private void loadPresets()
{ {
var json = PlayerPrefs.GetString($"Filter_{Name}_PresetList", string.Empty); var json = PlayerPrefs.GetString($"Filter_{Name}_PresetList", string.Empty);
var loadedPresets = JsonUtility.FromJson<FilterPresetList>(json); var loadedPresets = JsonUtility.FromJson<FilterPresetList>(json);
if (loadedPresets == null) return; if (loadedPresets == null) return;
else Presets = loadedPresets.presets; else Presets = loadedPresets.presets;
} }
public void SavePresets() public void SavePresets()
{ {
var json = JsonUtility.ToJson(new FilterPresetList(Presets)); var json = JsonUtility.ToJson(new FilterPresetList(Presets));
PlayerPrefs.SetString($"Filter_{Name}_PresetList", json); PlayerPrefs.SetString($"Filter_{Name}_PresetList", json);
} }
public MsgBool CreatePreset(string presetName, out FilterPreset newPreset) public MsgBool CreatePreset(string presetName, out FilterPreset newPreset)
{ {
newPreset = null; newPreset = null;
if (string.IsNullOrWhiteSpace(presetName)) return "名称不能为空"; if (string.IsNullOrWhiteSpace(presetName)) return "名称不能为空";
if (Presets.Count(p => p.Name == presetName) != 0) return "名称重复"; if (Presets.Count(p => p.Name == presetName) != 0) return "名称重复";
newPreset = new FilterPreset(presetName); newPreset = new FilterPreset(presetName);
Presets.Add(newPreset); Presets.Add(newPreset);
SavePresets(); SavePresets();
return true; return true;
} }
public void RemovePreset(FilterPreset preset) public void RemovePreset(FilterPreset preset)
{ {
if (!Presets.Remove(preset)) return; if (!Presets.Remove(preset)) return;
SavePresets(); SavePresets();
EventInvoker.RaiseFilterPresetRemoved(this, preset); EventInvoker.RaiseFilterPresetRemoved(this, preset);
} }
public void ResetPreset() public void ResetPreset()
{ {
foreach (var param in Paramerters) foreach (var param in Paramerters)
{ {
param.ResetToDefault(); param.ResetToDefault();
} }
} }
public void ApplyPreset(FilterPreset preset) public void ApplyPreset(FilterPreset preset)
{ {
foreach (var param in Paramerters) foreach (var param in Paramerters)
{ {
var value = preset.GetParamValue(param.Name, param.ValueType); var value = preset.GetParamValue(param.Name, param.ValueType);
if (value == null) if (value == null)
param.ResetToDefault(); param.ResetToDefault();
else else
param.Apply(value); param.Apply(value);
} }
} }
} }
[Serializable] [Serializable]
private class FilterPresetList private class FilterPresetList
{ {
public List<FilterPreset> presets; public List<FilterPreset> presets;
public FilterPresetList(List<FilterPreset> presets) public FilterPresetList(List<FilterPreset> presets)
{ {
this.presets = presets; this.presets = presets;
foreach (var preset in presets) foreach (var preset in presets)
{ {
preset.ReadyForJson(); preset.ReadyForJson();
} }
} }
} }
[Serializable] [Serializable]
public class FilterPreset public class FilterPreset
{ {
[SerializeField] [SerializeField]
public string Name; public string Name;
[SerializeField] [SerializeField]
private List<string> m_paramName = new List<string>(); private List<string> m_paramName = new List<string>();
[SerializeField] [SerializeField]
private List<string> m_valueJson = new List<string>(); private List<string> m_valueJson = new List<string>();
private bool m_cacheReady = false; private bool m_cacheReady = false;
private Dictionary<string, string> m_paramName2ValueJson; private Dictionary<string, string> m_paramName2ValueJson;
public FilterPreset(string presetName) public FilterPreset(string presetName)
{ {
Name = presetName; Name = presetName;
} }
public void ReadyForJson() public void ReadyForJson()
{ {
prepareCache(); prepareCache();
m_paramName = m_paramName2ValueJson.Keys.ToList(); m_paramName = m_paramName2ValueJson.Keys.ToList();
m_valueJson = m_paramName2ValueJson.Values.ToList(); m_valueJson = m_paramName2ValueJson.Values.ToList();
} }
public string GetParamValueJson(string paramName) public string GetParamValueJson(string paramName)
{ {
prepareCache(); prepareCache();
string value; string value;
m_paramName2ValueJson.TryGetValue(paramName, out value); m_paramName2ValueJson.TryGetValue(paramName, out value);
return value; return value;
} }
public object GetParamValue(string paramName, Type valueType) public object GetParamValue(string paramName, Type valueType)
{ {
var rawStr = GetParamValueJson(paramName); var rawStr = GetParamValueJson(paramName);
if (rawStr == null) return null; if (rawStr == null) return null;
if (valueType == typeof(float)) if (valueType == typeof(float))
{ {
float floatVal; float floatVal;
float.TryParse(rawStr, out floatVal); float.TryParse(rawStr, out floatVal);
return floatVal; return floatVal;
} }
else if (valueType.IsEnum) else if (valueType.IsEnum)
{ {
var names = Enum.GetNames(valueType); var names = Enum.GetNames(valueType);
var values = Enum.GetValues(valueType); var values = Enum.GetValues(valueType);
for (int i = 0; i < names.Length; i++) for (int i = 0; i < names.Length; i++)
{ {
if (names[i].Equals(rawStr)) if (names[i].Equals(rawStr))
{ {
return values.GetValue(i); return values.GetValue(i);
} }
} }
return null; return null;
} }
else else
{ {
App.log.Error($"尚未支持的滤镜参数类型{valueType}"); App.log.Error($"尚未支持的滤镜参数类型{valueType}");
return null; return null;
} }
} }
public void SetParamValue(string paramName, Type valueType, object value) public void SetParamValue(string paramName, Type valueType, object value)
{ {
prepareCache(); prepareCache();
m_paramName2ValueJson[paramName] = value.ToString(); m_paramName2ValueJson[paramName] = value.ToString();
} }
private void prepareCache() private void prepareCache()
{ {
if (m_cacheReady) return; if (m_cacheReady) return;
m_paramName2ValueJson = new Dictionary<string, string>(); m_paramName2ValueJson = new Dictionary<string, string>();
for (int i = 0; i < m_paramName.Count; i++) for (int i = 0; i < m_paramName.Count; i++)
{ {
m_paramName2ValueJson[m_paramName[i]] = m_valueJson[i]; m_paramName2ValueJson[m_paramName[i]] = m_valueJson[i];
} }
m_cacheReady = true; m_cacheReady = true;
} }
} }
[Serializable] [Serializable]
public class FilterRomSetting public class FilterRomSetting
{ {
[SerializeField] [SerializeField]
private List<int> m_romID; private List<int> m_romID;
[SerializeField] [SerializeField]
private List<Item> m_items; private List<Item> m_items;
bool m_cacheReady = false; bool m_cacheReady = false;
Dictionary<int, Item> m_cache; Dictionary<int, Item> m_cache;
public void Setup(RomFile rom, Filter filter, FilterPreset preset) public void Setup(RomFile rom, Filter filter, FilterPreset preset)
{ {
prepareCache(); prepareCache();
if (filter == null) if (filter == null)
m_cache.Remove(rom.ID); m_cache.Remove(rom.ID);
else else
m_cache[rom.ID] = new Item { FilterName = filter.Name, PresetName = preset != null ? preset.Name : null }; m_cache[rom.ID] = new Item { FilterName = filter.Name, PresetName = preset != null ? preset.Name : null };
} }
public string ToJson() public string ToJson()
{ {
prepareCache(); prepareCache();
m_romID = m_cache.Keys.ToList(); m_romID = m_cache.Keys.ToList();
m_items = m_cache.Values.ToList(); m_items = m_cache.Values.ToList();
return JsonUtility.ToJson(this); return JsonUtility.ToJson(this);
} }
public ValueTuple<string, string> Get(RomFile rom) public ValueTuple<string, string> Get(RomFile rom)
{ {
prepareCache(); prepareCache();
Item item; Item item;
m_cache.TryGetValue(rom.ID, out item); m_cache.TryGetValue(rom.ID, out item);
return new ValueTuple<string, string>(item.FilterName, item.PresetName); return new ValueTuple<string, string>(item.FilterName, item.PresetName);
} }
private void prepareCache() private void prepareCache()
{ {
if (m_cacheReady) return; if (m_cacheReady) return;
if (m_items == null) m_items = new List<Item>(); if (m_items == null) m_items = new List<Item>();
if (m_romID == null) m_romID = new List<int>(); if (m_romID == null) m_romID = new List<int>();
m_cache = new Dictionary<int, Item>(); m_cache = new Dictionary<int, Item>();
for (int i = 0; i < m_romID.Count && i < m_items.Count; i++) for (int i = 0; i < m_romID.Count && i < m_items.Count; i++)
{ {
m_cache[m_romID[i]] = m_items[i]; m_cache[m_romID[i]] = m_items[i];
} }
m_cacheReady = true; m_cacheReady = true;
} }
[Serializable] [Serializable]
struct Item struct Item
{ {
public string FilterName; public string FilterName;
public string PresetName; public string PresetName;
} }
} }
} }
} }