BepInExプラグインにしたい場合
[注意]COM3D2.5のVersion3.41.0以降はこのやり方は通用しないみたいです。
作り的にはSybarisプラグインとほぼ同じ、参照先、基底クラス、クラスに付けるべき属性を変えるぐらいでBepInExプラグインになる。
ネット上の資料がBepInExの方が多いのでBepInExプラグインの方が作りやすい。パッチはもうHarmonyが使えるの比較にならない。
参照先
UnityInjectorだったものが、BepInExに変わる。
パッチを当てる場合はmono.cecilより0Harmonyを使う方が楽。
基底クラス
UnityInjector.PluginBaseがBepInEx.BaseUnityPluginになる。
どちらもUnityEngine.MonoBehaviourを継承しているので、AwakeとかUpdateで実装するのは同じ。
属性
UnityInjector.Attributes.PluginNameAttribute,UnityInjector.Attributes.PluginVersionAttributeだったものが、BepInEx.BepInPluginに変わる。
名前とバージョン以外にGuidを設定する必要がある。
参考
XmlSerializerを使ったときに出るあのうざいメッセージを表示しなくするプラグイン。
using BepInEx; using HarmonyLib; using System.Collections.Generic; using System.Reflection.Emit; using System.Xml.Serialization; namespace DisableXmlSerializerExceptionMessage { /// <summary> /// プラグイン本体 /// </summary> [BepInPlugin(PluginGuid, PluginName, PluginVersion)] public class Plugin : BaseUnityPlugin { /// <summary> /// プラグイン名 /// </summary> public const string PluginName = "Disable XmlSerializer Exception Message Plug-In"; /// <summary> /// プラグインGuid /// </summary> public const string PluginGuid = "jp.fumble.DisableXmlSerializerExceptionMessage"; /// <summary> /// プラグインカンパニー /// </summary> public const string CompanyName = "Fumble Warez"; /// <summary> /// プラグインコピーライト /// </summary> public const string Copyright = "Copyright © Fumble Warez 2025"; /// <summary> /// プラグインバージョン /// </summary> public const string PluginVersion = "1.0.1.0"; /// <summary> /// HarmonyLib(HarmonyX)のインスタンス /// </summary> /// <remarks> /// Harmonyがあれば「もう何も恐くない」。 /// </remarks> private static Harmony harmony; /// <summary> /// コンストラクタ /// </summary> static Plugin() { // パッチする if (Plugin.harmony == null) Plugin.harmony = new Harmony(PluginGuid); Plugin.harmony.PatchAll(typeof(Patch_XmlSerializer_RunSerializerGeneration)); } /// <summary> /// プラグイン起動 /// </summary> private void Awake() { // 何もしない、パッチしてしまえばアンロードされても「もう何も恐くない」。 } } /// <summary> /// XmlSerializer.RunSerializerGenerationパッチ /// </summary> [HarmonyPatch(typeof(XmlSerializer), "RunSerializerGeneration", typeof(System.Object))] public static class Patch_XmlSerializer_RunSerializerGeneration { /// <summary> /// とらんすぱいら /// </summary> /// <param name="instructions">変更前のIL</param> /// <returns>変更後のIL</returns> [HarmonyTranspiler] static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) { // 見つかっただけ書き換える、なかったら何もしない。 // 想定は1箇所、まかり間違って2回目走ってもすべて書き換え済みなのでやはり「もう何も恐くない」。 // 書き換え元IL // call Void WriteLine(System.Object) // 書き換え先IL // pop foreach (var instruction in instructions) { if (instruction.opcode == OpCodes.Call && instruction.operand != null && instruction.operand.ToString() == "Void WriteLine(System.Object)") yield return new CodeInstruction(OpCodes.Pop); else yield return instruction; } } } }
コメント