-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from Sewer56/network-overhaul
Added: Useful Networking Related APIs & Functionality to Mod Helper
- Loading branch information
Showing
9 changed files
with
220 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,54 @@ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
using NinjaKiwi.NKMulti; | ||
using UnhollowerBaseLib; | ||
|
||
namespace BTD_Mod_Helper.Api.Coop | ||
{ | ||
/// <summary> | ||
/// Utility functions used for sending messages over the network. | ||
/// </summary> | ||
public class MessageUtils | ||
{ | ||
public static Message CreateMessage<T>(T objectToSend, string code = "") where T : Il2CppSystem.Object | ||
/// <summary> | ||
/// Creates a message to be sent over the network. | ||
/// The message will be serialized as JSON. | ||
/// </summary> | ||
/// <param name="objectToSend">The object to be sent. The object's properties will be serialized.</param> | ||
/// <param name="code">Unique code for your specific message.</param> | ||
public static Message CreateMessageEx<T>(T objectToSend, string code = "") | ||
{ | ||
var json = JsonConvert.SerializeObject(objectToSend); | ||
var serialize = Il2CppSystem.Text.Encoding.Default.GetBytes(json); | ||
var serialize = Encoding.Default.GetBytes(json); | ||
code = string.IsNullOrEmpty(code) ? MelonMain.coopMessageCode : code; | ||
return new Message(code, serialize); | ||
|
||
//throw new System.Exception("This code was broken in BTD6 update 27.0"); | ||
// commented code below broke in update 27.0 | ||
//Il2CppStructArray<byte> serialize = SerialisationUtil.Serialise(objectToSend); | ||
|
||
// this code is commented because code above is broken | ||
/*code = string.IsNullOrEmpty(code) ? MelonMain.coopMessageCode : code; | ||
return new Message(code, serialize);*/ | ||
} | ||
|
||
/// <summary> | ||
/// Reads a message sent from the network. | ||
/// Assumes message is sent as JSON. (via <see cref="CreateMessageEx{T}"/>) | ||
/// </summary> | ||
/// <param name="serializedMessage">Raw bytes received from the network.</param> | ||
public static T ReadMessage<T>(Il2CppStructArray<byte> serializedMessage) | ||
{ | ||
var modMessage = Il2CppSystem.Text.Encoding.Default.GetString(serializedMessage); | ||
var modMessage = Encoding.Default.GetString(serializedMessage); | ||
return JsonConvert.DeserializeObject<T>(modMessage); | ||
|
||
//throw new System.Exception("This code was broken in BTD6 update 27.0"); | ||
// commented code below broke in update 27.0 | ||
//return SerialisationUtil.Deserialise<T>(serializedMessage); | ||
} | ||
|
||
/// <summary> | ||
/// Reads a message sent from the network. | ||
/// Assumes message is sent as JSON. (via <see cref="CreateMessageEx{T}"/>) | ||
/// </summary> | ||
/// <param name="message">Message received from the network.</param> | ||
public static T ReadMessage<T>(Message message) | ||
{ | ||
//throw new System.Exception("This code was broken in BTD6 update 27.0"); | ||
// commented code below broke in update 27.0 | ||
return ReadMessage<T>(message.bytes); | ||
} | ||
|
||
#region Backwards Binary Compatibility | ||
[Obsolete($"For backwards compatibility reasons only, please use {nameof(CreateMessageEx)}")] | ||
public static Message CreateMessage<T>(T objectToSend, string code = "") where T : Il2CppSystem.Object => CreateMessage(objectToSend, code); | ||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
BloonsTD6 Mod Helper/Patches/Player/NKMultiGameInterface_Connect.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using HarmonyLib; | ||
using MelonLoader; | ||
using NinjaKiwi.NKMulti; | ||
using Task = Il2CppSystem.Threading.Tasks.Task; | ||
|
||
namespace BTD_Mod_Helper.Patches.Player | ||
{ | ||
[HarmonyPatch(typeof(NKMultiGameInterface), nameof(NKMultiGameInterface.Connect))] | ||
internal class NKMultiGameInterface_Connect | ||
{ | ||
[HarmonyPostfix] | ||
public static void Postfix(NKMultiGameInterface __instance, ref Task __result) => __result.ContinueWith(new Action<Task>(task => OnConnectTaskFinished(__instance, task))); | ||
|
||
private static void OnConnectTaskFinished(NKMultiGameInterface instance, Task obj) | ||
{ | ||
if (instance.IsConnected) | ||
{ | ||
MelonMain.PerformHook(mod => { mod.OnConnected(instance); }); | ||
instance.add_PeerConnectedEvent(new Action<int>((peerId) => OnPeerConnected(instance, peerId))); | ||
instance.add_PeerDisconnectedEvent(new Action<int>((peerId) => OnPeerDisconnected(instance, peerId))); | ||
} | ||
else | ||
{ | ||
MelonMain.PerformHook(mod => { mod.OnConnectFail(instance); }); | ||
} | ||
} | ||
|
||
private static void OnPeerDisconnected(NKMultiGameInterface nkGi, int peerId) => MelonMain.PerformHook(mod => { mod.OnPeerDisconnected(nkGi, peerId); }); | ||
|
||
private static void OnPeerConnected(NKMultiGameInterface nkGi, int peerId) => MelonMain.PerformHook(mod => { mod.OnPeerConnected(nkGi, peerId); }); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
BloonsTD6 Mod Helper/Patches/Player/NKMultiGameInterface_Disconnect.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using HarmonyLib; | ||
using NinjaKiwi.NKMulti; | ||
|
||
namespace BTD_Mod_Helper.Patches.Player | ||
{ | ||
[HarmonyPatch(typeof(NKMultiGameInterface), nameof(NKMultiGameInterface.Disconnect))] | ||
public class NKMultiGameInterface_Disconnect | ||
{ | ||
[HarmonyPostfix] | ||
public static void Postfix(NKMultiGameInterface __instance) => MelonMain.PerformHook(mod => { mod.OnDisconnected(__instance); }); | ||
} | ||
} |
Oops, something went wrong.