using System; using System.Linq; using System.Reflection; using UnityEngine; namespace CharacterPreview { public static class Load_DuckovCustomModel { public static ModelMove? CreateModel() { Action startMethod = null; Action midMethod = null; Action endMethod = null; // 尝试获取 LevelManager_OnLevelBeginInitializing try { Debug.Log("\n[Main Runner] 尝试获取 LevelManager_OnLevelBeginInitializing..."); startMethod = GetPrivateStaticVoidMethod( "DuckovCustomModel.ModEntry.LevelManager_OnLevelBeginInitializing"); Debug.Log("[Main Runner] 成功获取 LevelManager_OnLevelBeginInitializing 委托。"); } catch (Exception ex) { Debug.LogError( $"[Main Runner] 获取 LevelManager_OnLevelBeginInitializing 时发生错误: {ex.GetType().Name} - {ex.Message}"); } // 尝试获取 LevelManager_OnLevelInitialized try { Debug.Log("\n[Main Runner] 尝试获取 LevelManager_OnLevelInitialized..."); midMethod = GetPrivateStaticVoidMethod( "DuckovCustomModel.ModEntry.LevelManager_OnLevelInitialized"); Debug.Log("[Main Runner] 成功获取 LevelManager_OnLevelInitialized 委托。"); } catch (Exception ex) { Debug.LogError( $"[Main Runner] 获取 LevelManager_OnLevelInitialized 时发生错误: {ex.GetType().Name} - {ex.Message}"); } // 尝试获取 LevelManager_OnAfterLevelInitialized try { Debug.Log("\n[Main Runner] 尝试获取 LevelManager_OnAfterLevelInitialized..."); endMethod = GetPrivateStaticVoidMethod( "DuckovCustomModel.ModEntry.LevelManager_OnAfterLevelInitialized"); Debug.Log("[Main Runner] 成功获取 LevelManager_OnAfterLevelInitialized 委托。"); } catch (Exception ex) { Debug.LogError( $"[Main Runner] 获取 LevelManager_OnAfterLevelInitialized 时发生错误: {ex.GetType().Name} - {ex.Message}"); } Debug.Log("\n[Main Runner] --- 调用获取到的方法 ---"); if (startMethod != null) { Debug.Log("[Main Runner] 正在调用 LevelManager_OnLevelBeginInitializing..."); startMethod.Invoke(); Debug.Log("[Main Runner] LevelManager_OnLevelBeginInitializing 调用完成。"); } else Debug.LogWarning("[Main Runner] LevelManager_OnLevelBeginInitializing 委托为 null,跳过调用。"); if (midMethod != null) { Debug.Log("[Main Runner] 正在调用 LevelManager_OnLevelInitialized..."); midMethod.Invoke(); Debug.Log("[Main Runner] LevelManager_OnLevelInitialized 调用完成。"); } else Debug.LogWarning("[Main Runner] LevelManager_OnLevelInitialized 委托为 null,跳过调用。"); if (endMethod != null) { Debug.Log("[Main Runner] 正在调用 LevelManager_OnAfterLevelInitialized..."); endMethod.Invoke(); Debug.Log("[Main Runner] LevelManager_OnAfterLevelInitialized 调用完成。"); } else Debug.LogWarning("[Main Runner] LevelManager_OnAfterLevelInitialized 委托为 null,跳过调用。"); // var target = DuckovCustomModel.Core.Data.ModelTarget.Character; // // Debug.Log("运行替换"); // var currentModelID = DuckovCustomModel.ModEntry.UsingModel?.GetModelID(target) ?? string.Empty; // if (string.IsNullOrEmpty(currentModelID)) // { // // 错误日志:未能获取当前模型ID // Debug.LogError($"[DuckovCustomModel] Failed to get current model ID for target '{target}'. Current model reference might be null or GetModelID returned null/empty. Returning null."); // return null; // } // // 注意:out _ 用于忽略 ModelManager 返回的第一个 out 参数 // if (!DuckovCustomModel.Managers.ModelManager.FindModelByID(currentModelID, out _, out var modelInfo)) // { // // 错误日志:未能找到指定的模型 // Debug.LogError($"[DuckovCustomModel] Model with ID '{currentModelID}' not found by ModelManager for target '{target}'. Returning null."); // return null; // } // if (!modelInfo.CompatibleWithType(target)) // { // // 错误日志:模型与目标不兼容 // Debug.LogError($"[DuckovCustomModel] Model '{currentModelID}' is not compatible with target '{target}'. Returning null."); // return null; // } // Debug.Log($"inf{currentModelID}"); // // 如果前面的检查都通过,则应用模型 // DuckovCustomModel.Managers.ModelListManager.ApplyModelToTarget(target, currentModelID, true); // // var handlers=DuckovCustomModel.Managers.ModelManager.GetAllModelHandlers(target); // Debug.Log($"handlers{handlers.Count}"); // Debug.Log("运行完成"); return null; } public static Action GetPrivateStaticVoidMethod(string fullMethodName) { var lastDotIndex = fullMethodName.LastIndexOf('.'); if (lastDotIndex == -1 || lastDotIndex == fullMethodName.Length - 1) { throw new ArgumentException($"无效的方法名格式: {fullMethodName}。应为 'Namespace.ClassName.MethodName'。"); } var fullTypeName = fullMethodName.Substring(0, lastDotIndex); var methodName = fullMethodName.Substring(lastDotIndex + 1); Type targetType = null; foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { targetType = assembly.GetType(fullTypeName); if (targetType != null) { break; // 找到类型后停止搜索 } } if (targetType == null) { throw new TypeLoadException($"在任何已加载的程序集中都未找到类型: {fullTypeName}。"); } var methodInfo = targetType.GetMethod( methodName, BindingFlags.NonPublic | BindingFlags.Static ); if (methodInfo == null) { throw new MissingMethodException( $"在类型 '{fullTypeName}' 中未找到名为 '{methodName}' 的私有静态方法," + $"或者该方法不满足私有、静态、无参的条件。" ); } if (methodInfo.GetParameters().Length != 0) { throw new MissingMethodException( $"方法 '{fullMethodName}' 存在参数,不符合无参 Action 的要求。" ); } if (methodInfo.ReturnType != typeof(void)) { throw new MissingMethodException( $"方法 '{fullMethodName}' 返回值不是 void,不符合 Action 的要求。" ); } return (Action)methodInfo.CreateDelegate(typeof(Action)); } } }