Plugin.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // 具体 Harmony 钩子见 Patches/ 目录(另行实现)
  2. using BepInEx;
  3. using BepInEx.Configuration;
  4. using HarmonyLib;
  5. using OneShot.Core;
  6. namespace OneShot
  7. {
  8. /// <summary>
  9. /// 插件入口:初始化配置、日志与会话闸门。
  10. /// 进入过一次战局(含藏身处)后,本次 SPT 会话内禁止再开始战局。
  11. /// Plugin entry point: initializes config, logging and the session gate.
  12. /// Once a raid (or the hideout) has been entered, starting another raid is blocked for this SPT session.
  13. /// </summary>
  14. [BepInPlugin("com.pj568.oneshot", "OneShot", "0.1.0")]
  15. public class OneShotPlugin : BaseUnityPlugin
  16. {
  17. private const string HarmonyId = "com.pj568.oneshot";
  18. /// <summary>
  19. /// 会话级闸门,记录本次运行是否已进入过场景。
  20. /// The session-level gate recording whether a scene has been entered this run.
  21. /// </summary>
  22. public static SessionGate Gate { get; private set; } = new SessionGate();
  23. private Harmony _harmony;
  24. private void Awake()
  25. {
  26. Log.Init(Logger);
  27. PluginConfig.Init(Config);
  28. PluginConfig.ResetTriggered += OnResetTriggered;
  29. ApplyHarmonyPatches();
  30. Log.Info("OneShot 已加载 | loaded");
  31. }
  32. private void OnDestroy()
  33. {
  34. PluginConfig.ResetTriggered -= OnResetTriggered;
  35. if (_harmony == null)
  36. {
  37. return;
  38. }
  39. try
  40. {
  41. _harmony.UnpatchSelf();
  42. }
  43. catch (System.Exception e)
  44. {
  45. Log.Error("卸载 Harmony 补丁失败 | Failed to unpatch Harmony patches: " + e);
  46. }
  47. }
  48. /// <summary>
  49. /// 响应配置中的一次性重置请求,清除“已进过局”标记。
  50. /// Handles the one-shot reset request from config by clearing the "entered" flag.
  51. /// </summary>
  52. private void OnResetTriggered()
  53. {
  54. Gate?.Reset();
  55. }
  56. private void ApplyHarmonyPatches()
  57. {
  58. try
  59. {
  60. _harmony = new Harmony(HarmonyId);
  61. _harmony.PatchAll(typeof(OneShotPlugin).Assembly);
  62. }
  63. catch (System.Exception e)
  64. {
  65. Log.Error("应用 Harmony 补丁失败 | Failed to apply Harmony patches: " + e);
  66. }
  67. }
  68. }
  69. }