Skip to content

About

CheaterMod example addon for third-party plugin developers

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

CheaterMod Addon API

CheaterMod 提供了一套完整的第三方 Addon 接口,允许其他 BepInEx 插件向 CheaterMod 注册自定义模块和分类。模块的开关状态、GUI 面板位置、详细配置项均会由 CheaterMod 自动持久化。

快速开始

1. 创建 BepInEx 插件项目

<!-- .csproj -->
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
    <OutputType>Library</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="BepInEx" HintPath="..." />
    <Reference Include="CheaterMod" HintPath="path/to/CheaterMod.dll" />
    <Reference Include="UnityEngine" HintPath="..." />
    <Reference Include="UnityEngine.CoreModule" HintPath="..." />
    <Reference Include="UnityEngine.IMGUIModule" HintPath="..." />
  </ItemGroup>
</Project>

2. 声明依赖并注册模块

[BepInPlugin("your.plugin.guid", "Your Plugin Name", "1.0.0")]
[BepInDependency("com.mod.casualties.cheatermod", BepInDependency.DependencyFlags.HardDependency)]
public class YourPlugin : BaseUnityPlugin
{
    private void Awake()
    {
        CheaterAPI.RegisterModule(new YourModule());
    }
}

API 参考

CheaterAPI

静态注册网关,位于 FullBrightMod 命名空间。

方法 说明
static void RegisterModule(ModuleBase module) 向 CheaterMod 注册一个模块。自动恢复保存的配置状态并创建 GUI 面板。
CheaterAPI.RegisterModule(new MyModule());

ModuleBase

抽象基类,位于 FullBrightMod.Core 命名空间。

public abstract class ModuleBase
{
    // ── 元数据 ──
    public virtual string Name => GetType().Name;
    public virtual string Description => "";
    public virtual ModuleCategory Category => ModuleCategory.Misc;

    // ── 运行时状态(由框架自动持久化) ──
    public bool Enabled { get; set; }
    public bool IsExpanded { get; set; }
    public KeyCode BindKey { get; set; }
    public bool IsBinding { get; set; }

    // ── 配置持久化(由框架自动序列化) ──
    public Dictionary<string, string> CustomSettings;
    public string GetSetting(string key, string defaultValue);
    public Color  GetSetting(string key, Color defaultValue);
    public void   SetSetting(string key, string value);
    public void   SetSetting(string key, Color value);

    // ── 生命周期 ──
    public virtual void OnEnable()  { }
    public virtual void OnDisable() { }
    public virtual void OnUpdate()  { }
    public virtual void OnLateUpdate() { }
    public virtual void OnGUI()     { }
    public virtual float GetSettingsHeight() => 0f;
    public virtual void DrawSettings(float x, ref float y, float width, Event e) { }
}

生命周期回调:

方法 调用时机
OnEnable() 模块开关从关→开
OnDisable() 模块开关从开→关
OnUpdate() 每帧调用(仅 Enabled = true 时)
OnGUI() 每帧 OnGUI 阶段(用于渲染 UI/ESP)
GetSettingsHeight() GUI 计算设置面板高度
DrawSettings() 绘制设置面板 UI(滑块、开关、颜色选择器等)

配置持久化系统

CheaterMod 会自动将模块的 CustomSettings 字典序列化到 BepInEx/config/CheaterMod.json 中,并在下次启动时自动恢复到对应模块。模块作者只需使用 GetSetting / SetSetting 读写配置,无需关心 JSON 序列化、生命周期钩子或恢复时机。

public class MyModule : ModuleBase
{
    public override void OnGUI()
    {
        // 读取已保存配置(首次使用时自动回退到默认值)
        Color color = GetSetting("myColor", new Color(1f, 0f, 0f, 1f));
        // ... 使用 color ...
    }

    public override void DrawSettings(float x, ref float y, float width, Event e)
    {
        Color color = GetSetting("myColor", Color.white);

        // CustomGUI 提供颜色选择器、开关、滑块等
        CustomGUI.DrawColorPicker(x, ref y, width, e, "颜色:", ref color);

        // 修改后写回(框架自动在菜单关闭时持久化)
        SetSetting("myColor", color);
    }
}

重要:GetSetting 在模块的任何生命周期阶段都可以安全调用。CheaterMod 在游戏启动时一次性加载所有配置到 CustomSettings,模块无需在 OnEnable 或构造函数中手动初始化。

ModuleCategory

密封类,位于 FullBrightMod.Core,基于 Name 字符串的相等性比较。

预定义实例 显示名称
ModuleCategory.Combat "⚔ 战斗"
ModuleCategory.Player "👤 玩家"
ModuleCategory.Movement "🏃 移动"
ModuleCategory.Render "👁 视觉"
ModuleCategory.World "🌍 世界"
ModuleCategory.Misc "🔧 杂项"
ModuleCategory.Hidden (不显示在面板中)

创建自定义分类:

private static readonly ModuleCategory MyCategory = ModuleCategory.Create("My Category");

自定义分类名称会直接用作面板标题。同名分类的模块会合并到同一个面板中。面板位置由用户拖拽决定,自动持久化。

CustomGUI

位于 FullBrightMod.UI 命名空间,提供常用设置 UI 控件:

方法 说明
DrawColorPicker(x, ref y, w, e, label, ref color) 颜色选择器(预览+调色板按钮)
DrawToggle(x, ref y, w, e, label, ref bool) 开关按钮
DrawSlider(x, ref y, w, e, label, ref float, min, max) 滑动条
DrawSelector(x, ref y, w, e, label, options[], ref index) 选项选择器
DrawTextField(x, ref y, w, label, ref string) 文本输入框

设计建议

  1. BepInDependency 标记 HardDependency — 确保 CheaterMod 先于你的插件加载
  2. 使用 private static readonly 缓存 Category 实例 — 避免重复 Create() 导致面板分裂
  3. 所有配置通过 GetSetting/SetSetting 读写 — 无需手动管理 CustomSettings 字典
  4. 模块尽量轻量 — OnGUI/OnUpdate 中的计算保持最小开销
  5. 分类命名清晰 — 自定义分类名尽量简短,能在一行内显示
  6. 设置提供合理默认值 — GetSetting(key, defaultValue) 的第二个参数即为缺省值

加载顺序(Load Order)

1. BepInEx 加载所有插件
2. CheaterMod.Awake() → 注册内置模块 → 初始化 GUI → 加载配置(恢复所有历史模块状态)
3. 你的插件 .Awake() → CheaterAPI.RegisterModule() → 匹配已加载配置 → 恢复模块状态

无论你的插件何时注册,只要 CheaterMod.json 中存在对应模块的配置条目,状态都会自动恢复。

示例项目

本目录包含完整的示例插件:

  • Plugin.cs — BepInEx 入口,注册模块
  • ExampleModule.cs — 在鼠标位置绘制彩色方形的测试模块,展示自定义分类、颜色选择器、配置持久化

About

CheaterMod example addon for third-party plugin developers

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages