2025-11-01 15:18:34 +08:00
|
|
|
|
|
2025-11-03 18:56:20 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Reflection;
|
2025-11-01 23:26:25 +08:00
|
|
|
|
using HarmonyLib;
|
2025-11-03 18:56:20 +08:00
|
|
|
|
using Newtonsoft.Json;
|
2025-11-01 15:18:34 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using Object = UnityEngine.Object; // 确保引入 UnityEngine 命名空间
|
|
|
|
|
|
|
|
|
|
|
|
namespace HideCharacter
|
|
|
|
|
|
{
|
|
|
|
|
|
public class ModBehaviour : Duckov.Modding.ModBehaviour
|
|
|
|
|
|
{
|
2025-11-01 23:26:25 +08:00
|
|
|
|
public static HideCharacterComponent? hideHideCharacterManager=null;
|
2025-11-01 15:18:34 +08:00
|
|
|
|
private const string CHILD_GAMEOBJECT_NAME = "HideCharacterManager";
|
|
|
|
|
|
|
2025-11-03 18:56:20 +08:00
|
|
|
|
public const string MOD_ID = "HideCharacter";
|
|
|
|
|
|
public const string MOD_NAME = "隐藏角色设置";
|
2025-11-01 23:26:25 +08:00
|
|
|
|
private Harmony _harmony;
|
2025-11-03 18:56:20 +08:00
|
|
|
|
|
|
|
|
|
|
public bool loadedConfigApi = false;
|
|
|
|
|
|
public static HideList? hideList = new HideList();
|
2025-11-01 23:26:25 +08:00
|
|
|
|
|
2025-11-03 18:56:20 +08:00
|
|
|
|
private void Awake()
|
|
|
|
|
|
{
|
|
|
|
|
|
loadedConfigApi = Api.ModConfigAPI.Initialize();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-01 15:18:34 +08:00
|
|
|
|
protected override void OnAfterSetup()
|
|
|
|
|
|
{
|
|
|
|
|
|
AddHideComponent();
|
2025-11-01 23:26:25 +08:00
|
|
|
|
if (_harmony == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_harmony=new Harmony(MOD_ID);
|
|
|
|
|
|
_harmony.PatchAll();
|
|
|
|
|
|
}
|
2025-11-03 18:56:20 +08:00
|
|
|
|
var dllDirectory = info.path;
|
|
|
|
|
|
var configFilePath = Path.Combine(dllDirectory, "config.json");
|
|
|
|
|
|
if (File.Exists(configFilePath))
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var jsonString = File.ReadAllText(configFilePath);
|
|
|
|
|
|
hideList = JsonConvert.DeserializeObject<HideList>(jsonString);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (JsonSerializationException ex) // 捕获 Newtonsoft.Json 特有的异常
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError($"JSON 反序列化错误 (Newtonsoft.Json): {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError($"文件读取错误: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError($"加载配置文件时发生未知错误: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogWarning($"配置文件 '{configFilePath}' 不存在。将使用默认设置。");
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var jsonString = JsonConvert.SerializeObject(hideList, Formatting.Indented);
|
|
|
|
|
|
File.WriteAllText(configFilePath, jsonString);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError($"创建配置文件时发生错误: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError($"创建配置文件时发生未知错误: {ex.Message}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if(Api.ModConfigAPI.IsAvailable())
|
|
|
|
|
|
AddConfigSetting();
|
2025-11-01 15:18:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnBeforeDeactivate()
|
|
|
|
|
|
{
|
|
|
|
|
|
RemoveHideComponent();
|
2025-11-01 23:26:25 +08:00
|
|
|
|
if (_harmony != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_harmony.UnpatchAll(MOD_ID);
|
|
|
|
|
|
_harmony = null;
|
|
|
|
|
|
}
|
2025-11-01 15:18:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void AddHideComponent()
|
|
|
|
|
|
{
|
|
|
|
|
|
var childTransform = this.transform.Find(CHILD_GAMEOBJECT_NAME);
|
|
|
|
|
|
if (childTransform) return;
|
2025-11-01 23:26:25 +08:00
|
|
|
|
|
|
|
|
|
|
var hideCharacterManagerGameObject = new GameObject(CHILD_GAMEOBJECT_NAME);
|
|
|
|
|
|
hideCharacterManagerGameObject.transform.SetParent(this.transform);
|
|
|
|
|
|
hideHideCharacterManager = hideCharacterManagerGameObject.AddComponent<HideCharacterComponent>();
|
2025-11-01 15:18:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void RemoveHideComponent()
|
|
|
|
|
|
{
|
2025-11-01 23:26:25 +08:00
|
|
|
|
if (hideHideCharacterManager)
|
|
|
|
|
|
Destroy(hideHideCharacterManager?.gameObject);
|
2025-11-01 15:18:34 +08:00
|
|
|
|
}
|
2025-11-03 18:56:20 +08:00
|
|
|
|
|
|
|
|
|
|
private void AddConfigSetting()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
Api.ModConfigAPI.SafeAddBoolDropdownList(MOD_NAME,"hideArmor","隐藏装备",hideList.hideArmor);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnConfigChange()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-11-01 15:18:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|