efd-trading-card-base/src/Patches.cs
Cal Corum 91ee0333db Implement card pack system with gacha mechanics
Add card packs that can be opened via right-click Use menu to receive
random cards based on weighted slot configurations. Packs support
customizable rarity-based or card-specific drop rates.

Key features:
- CardPack/PackSlot data classes with weighted random selection
- PackParser for user-defined pack configurations
- PackUsageBehavior extending game's UsageBehavior system
- Runtime data lookup to handle Unity serialization limitations
- Pack consumption after opening with stack support
- Auto-generated default packs per card set

Technical notes:
- UsageUtilities registration for context menu integration
- All non-serializable fields (Dictionary, List<PackSlot>) looked up
  at runtime from ModBehaviour to survive Unity instantiation
- F9 debug spawn now uses InstantiateSync for proper item copies

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-19 21:44:29 -06:00

81 lines
2.8 KiB
C#

using System;
using HarmonyLib;
using UnityEngine;
namespace TradingCardMod
{
/// <summary>
/// Contains all Harmony patches for the Trading Card Mod.
/// Patches are applied in ModBehaviour.Start() via Harmony.PatchAll().
/// </summary>
public static class Patches
{
/// <summary>
/// Unique Harmony ID for this mod. Used to identify and unpatch if needed.
/// </summary>
public const string HarmonyId = "com.manticorum.tradingcardgame.duckov";
private static Harmony? _harmony;
/// <summary>
/// Apply all Harmony patches defined in this assembly.
/// </summary>
public static void ApplyPatches()
{
try
{
_harmony = new Harmony(HarmonyId);
_harmony.PatchAll();
Debug.Log($"[TradingCardMod] Harmony patches applied successfully");
}
catch (Exception ex)
{
Debug.LogError($"[TradingCardMod] Failed to apply Harmony patches: {ex.Message}");
Debug.LogException(ex);
}
}
/// <summary>
/// Remove all Harmony patches applied by this mod.
/// Called during mod unload to clean up.
/// </summary>
public static void RemovePatches()
{
try
{
_harmony?.UnpatchAll(HarmonyId);
Debug.Log($"[TradingCardMod] Harmony patches removed");
}
catch (Exception ex)
{
Debug.LogError($"[TradingCardMod] Failed to remove Harmony patches: {ex.Message}");
}
}
}
// ==========================================================================
// Safety Patches - Prevent crashes from missing mod items
// ==========================================================================
/// <summary>
/// Patch to prevent crashes when loading saves with mod items that aren't registered yet.
/// Logs a warning for missing mod items instead of letting the game crash.
/// </summary>
[HarmonyPatch(typeof(ItemStatsSystem.ItemAssetsCollection), "GetPrefab", new Type[] { typeof(int) })]
public static class GetPrefab_SafetyPatch
{
[HarmonyPostfix]
public static void Postfix(int typeID, ItemStatsSystem.Item __result)
{
// Check if this TypeID is in our mod's range and wasn't found
if (typeID >= 100000 && __result == null)
{
Debug.LogWarning($"[TradingCardMod] Item TypeID {typeID} not found. Item was likely saved before mod loaded. It will be lost.");
}
}
}
// Search injection patch disabled - causing index errors
// TODO: Find better approach for loot table integration
}