PacketHelper
Packet Helper is a helper mod designed to easily create client and server packets as well as attaching them to your mod's custom methods.
模組介紹 作者原文
Packet Helper
Packet Helper is a helper mod designed to easily create client and server packets as well as attaching them to your mod's custom methods.
An example of implementing this library can be found at
Initializing
Packet Helper uses a class called Session to store data for your packets
Session session = new Session("YOUR.MOD.GUID")
Session uses your mod GUID to create a unique packet offset to ensure that you don't overwrite packets on the game or other mods.
Creating a packet
Creating packets with Packet Helper is insanely easy first we will be creating a client packet.
It is important to remember that you must only add your packet once. Be sure to patch a method that gets called once.
public static Session session = new Session("YOUR.MOD.GUID");
public void MyClientPacketMethod(Packet packet)
{
// do some stuff with the packet
}
// This is my go-to method to create client packets
[HarmonyPatch(typeof(LocalClient), "InitializeClientData")]
[HarmonyPostfix]
static void CreateNewClientPackets()
{
session.CreateNewClientPacket("A Unique Packet Name", MyPacketMethod);
}
The packet name will be used later to identify which packet to send. The unique packet name does not have to correspond to the method name in any way.
Next, we will create a server packet.
public static Session session = new Session("YOUR.MOD.GUID");
public void MyServerPacketMethod(int fromClient, Packet packet)
{
// do some stuff with the packet
}
// This is my go-to method to create server packets
[HarmonyPatch(typeof(Server), "InitializeServerPackets")]
[HarmonyPostfix]
static void CreateNewServerPackets()
{
session.CreateNewServerPacket("A Unique Packet Name", MyPacketMethod);
}
It is important to note that server packet methods differ from client packet methods. They include a fromClient parameter that indicates the client.id that the packet came from.
When creating a packet it's vital to understand that the method you attach to a packet will be handling how that packet is interacted with when received.
Sending packets
Sending a packet is slightly more complicated as it uses the class Data shown below
public class Data
{
public int ints = null;
public string strings = null;
public bool bools = null;
public float floats = null;
public short shorts = null;
public long longs = null;
public Vector3 vector3s = null;
public Quaternion quaternions = null;
}
Data is an important class as it allows us to send any amount of data through the packet.
Here we will be sending a packet to the server.
public static Session session = new Session("YOUR.MOD.GUID");
public void MyServerPacketMethod(int fromClient, Packet packet)
{
itemId = packet.ReadInt();
Debug.Log($"{fromClient}, {itemId}");
}
[HarmonyPatch(typeof(Server), "InitializeServerPackets")]
[HarmonyPostfix]
static void CreateNewServerPackets()
{
session.CreateNewServerPacket("MyServerPacket", MyServerPacketMethod);
}
[HarmonyPatch(typeof (ClientSend), "PickupItem")]
[HarmonyPrefix]
static void ClientSendSharePacket(ClientSend __instance, int itemID) {
Item item = ItemManager.Instance.list[itemID].GetComponent ();
if (item.powerup) {
// Create new data object
Data data = new Data();
// Add the powerup id to the data
data.ints = new int { item.powerup.id };
// Send packet to the server and call subsequent method MyServerPacketMethod()
session.SendPacketToServer("MyServerPacket", data);
}
}
Next is sending a packet to the client.
Remember, when sending a packet to the client, fromClient is not accessible. If you need that value in the method that handles the packet, put it in the data class.
public static Session session = new Session("YOUR.MOD.GUID");
public void MyClientPacketMethod(Packet packet)
{
fromClient = packet.ReadInt();
itemId = packet.ReadInt();
Debug.Log($"{fromClient}, {itemId}");
}
public void MyServerPacketMethod(int fromClient, Packet packet)
{
ite
安裝與使用 安裝:BepInEx 外掛
- 本站下載的壓縮包就是模組本體,通常是 BepInEx 外掛(解壓後得到 .dll 或外掛資料夾)。
- 建議流程:用社群模組管理器(如 r2modman)選中本遊戲後匯入該模組,管理器會自動裝好 BepInEx 前置並處理依賴。
- 手動流程:先在遊戲目錄裝好 BepInEx 載入器,再把解壓出的 .dll 或外掛資料夾放進 BepInEx/plugins/。
- 啟動遊戲,BepInEx 控制台會列印載入日誌;確認這個模組出現在日誌裡即為安裝成功。
帶「前置依賴」的模組需要一併安裝,否則不會載入。功能開關通常在遊戲內或外掛自帶的設定檔裡調整。