init a bigInjectbag project
This commit is contained in:
		
							
								
								
									
										65
									
								
								test/InjectionBagSlotPatch.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								test/InjectionBagSlotPatch.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,65 @@
 | 
			
		||||
using System;
 | 
			
		||||
using HarmonyLib;
 | 
			
		||||
using ItemStatsSystem;
 | 
			
		||||
using ItemStatsSystem.Items;
 | 
			
		||||
using UnityEngine;
 | 
			
		||||
using System.Reflection;
 | 
			
		||||
 | 
			
		||||
namespace test
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// 注射器收纳包槽位修改补丁
 | 
			
		||||
    /// 将注射器收纳包(ID 882)的槽位数量从6个修改为12个
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    [HarmonyPatch(typeof(Item))]
 | 
			
		||||
    [HarmonyPatch("Initialize")]
 | 
			
		||||
    public class InjectionBagSlotPatch
 | 
			
		||||
    {
 | 
			
		||||
        private const int InjectionBagTypeID = 882;
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Initialize方法的后缀补丁
 | 
			
		||||
        /// 在物品初始化后检查是否是注射器收纳包
 | 
			
		||||
        /// 槽位默认6个,变为12个
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <param name="__instance">Item实例</param>
 | 
			
		||||
        [HarmonyPostfix]
 | 
			
		||||
        static void Postfix(Item __instance)
 | 
			
		||||
        {
 | 
			
		||||
            Debug.Log($"[注射器收纳包修改] 被调用");
 | 
			
		||||
 | 
			
		||||
            // 检查是否是注射器收纳包
 | 
			
		||||
            if (__instance == null || __instance.TypeID != InjectionBagTypeID || __instance.Slots == null)
 | 
			
		||||
                return;
 | 
			
		||||
 | 
			
		||||
            // 获取槽位集合
 | 
			
		||||
            SlotCollection slots = __instance.Slots;
 | 
			
		||||
            
 | 
			
		||||
            if (slots.Count == 12)
 | 
			
		||||
                return;
 | 
			
		||||
 | 
			
		||||
            // 获取原始槽位数量
 | 
			
		||||
            int originalSlotCount = slots.Count;
 | 
			
		||||
 | 
			
		||||
            // 添加6个新的槽位
 | 
			
		||||
            for (int i = 0; i < 6; i++)
 | 
			
		||||
            {
 | 
			
		||||
                // 创建新的Slot对象
 | 
			
		||||
                Slot newSlot = (Slot)typeof(Slot).GetConstructor(new Type[0])?.Invoke(null);
 | 
			
		||||
 | 
			
		||||
                // 设置Slot的Key属性
 | 
			
		||||
                typeof(Slot).GetField("Key", BindingFlags.Public | BindingFlags.Instance)?.SetValue(newSlot, $"Slot_{originalSlotCount + i}");
 | 
			
		||||
 | 
			
		||||
                // 添加到槽位列表
 | 
			
		||||
                slots.list.Add(newSlot);
 | 
			
		||||
 | 
			
		||||
                // 初始化Slot
 | 
			
		||||
                typeof(Slot).GetMethod("Initialize", BindingFlags.Public | BindingFlags.Instance)?.Invoke(newSlot, new object[] { slots });
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // 重建字典缓存
 | 
			
		||||
            typeof(SlotCollection).GetMethod("BuildDictionary", BindingFlags.NonPublic | BindingFlags.Instance)?.Invoke(slots, null);
 | 
			
		||||
 | 
			
		||||
            Debug.Log($"[注射器收纳包修改] 已将物品ID {__instance.TypeID} 的槽位数量从{originalSlotCount}修改为{slots.Count}");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,20 +1,28 @@
 | 
			
		||||
 | 
			
		||||
using HarmonyLib;
 | 
			
		||||
using UnityEngine;
 | 
			
		||||
 | 
			
		||||
namespace test
 | 
			
		||||
{
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// test模组的主要行为类
 | 
			
		||||
    /// 继承自Duckov.Modding.ModBehaviour,用于在游戏UI中显示物品价值
 | 
			
		||||
    /// 基础模板
 | 
			
		||||
    /// 继承自Duckov.Modding.ModBehaviour
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public class ModBehaviour : Duckov.Modding.ModBehaviour
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        private Harmony harmony;
 | 
			
		||||
        
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 模组初始化时调用,类似于构造函数
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        void Awake()
 | 
			
		||||
        {
 | 
			
		||||
 | 
			
		||||
            // 初始化Harmony实例
 | 
			
		||||
            harmony = new Harmony("com.test.all.patches");
 | 
			
		||||
            
 | 
			
		||||
            // 应用所有Harmony补丁
 | 
			
		||||
            harmony.PatchAll();
 | 
			
		||||
            
 | 
			
		||||
            Debug.Log("Test模组已加载,所有补丁已应用");
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        /// <summary>
 | 
			
		||||
@@ -22,12 +30,18 @@ namespace test
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        void OnDestroy()
 | 
			
		||||
        {
 | 
			
		||||
 | 
			
		||||
            // 卸载所有Harmony补丁
 | 
			
		||||
            harmony?.UnpatchAll();
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        void OnEnable()
 | 
			
		||||
        {
 | 
			
		||||
 | 
			
		||||
            // 模组启用时重新应用补丁
 | 
			
		||||
            if (harmony == null)
 | 
			
		||||
            {
 | 
			
		||||
                harmony = new Harmony("com.test.all.patches");
 | 
			
		||||
                harmony.PatchAll();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        /// <summary>
 | 
			
		||||
@@ -36,7 +50,9 @@ namespace test
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        void OnDisable()
 | 
			
		||||
        {
 | 
			
		||||
 | 
			
		||||
            // 模组禁用时卸载补丁
 | 
			
		||||
            harmony?.UnpatchAll();
 | 
			
		||||
            harmony = null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user