Skip to content

Commit

Permalink
-Patched RPC Execution, allowing PML RPCs to run earlier.
Browse files Browse the repository at this point in the history
-Changed Patch locations to send mod info and verify clients earlier.
  • Loading branch information
Nagord committed Sep 4, 2022
1 parent 300311e commit 4114f27
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
47 changes: 41 additions & 6 deletions PulsarModLoader/MPModChecks/MPModCheckManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public bool ClientClickJoinRoom(RoomInfo room)
}
}

public void HostOnClientJoined(PhotonPlayer Player)
public bool HostOnClientJoined(PhotonPlayer Player)
{
MPModDataBlock[] ClientMods = null;
bool foundplayer = false;
Expand Down Expand Up @@ -458,6 +458,7 @@ public void HostOnClientJoined(PhotonPlayer Player)
KickClient(Player);

Logger.Info("Kicked client for failing mod check with the following message:" + message);
return false;
}
else
{
Expand All @@ -472,13 +473,14 @@ public void HostOnClientJoined(PhotonPlayer Player)
string message = $"You have been disconnected for not having the mod loader installed";
ModMessageHelper.Instance.photonView.RPC("RecieveErrorMessage", Player, new object[] { message });
KickClient(Player);
return;
return false;
}
Utilities.Logger.Info("Didn't receive message or proper modlist, but the server doesn't have multiplayer explicit mods. Proceeding onwards");
}
return true;
}

public IEnumerator ServerVerifyClient(PhotonPlayer player)
/*public IEnumerator ServerVerifyClient(PhotonPlayer player)
{
int loops = 0;
WaitForSeconds cachedWaitTime = new WaitForSeconds(0.25f);
Expand All @@ -492,7 +494,7 @@ public IEnumerator ServerVerifyClient(PhotonPlayer player)
yield return cachedWaitTime;
}
HostOnClientJoined(player);
}
}*/

[HarmonyPatch(typeof(PLUIPlayMenu), "ActuallyJoinRoom")] //allow/disallow local client to join server.
class JoinRoomPatch
Expand All @@ -503,16 +505,24 @@ static bool Prefix(RoomInfo room)
}
}

[HarmonyPatch(typeof(PLServer), "ServerOnClientVerified")] //Starts host mod verification coroutine
/*[HarmonyPatch(typeof(PLServer), "ServerOnClientVerified")] //Starts host mod verification coroutine
class ServerOnClientVerifiedPatch
{
static void Postfix(PhotonPlayer client)
{
PLServer.Instance.StartCoroutine(Instance.ServerVerifyClient(client));
}
}*/
[HarmonyPatch(typeof(PLServer), "AttemptGetVerified")]
class AttemptGetVerifiedRecievePatch
{
static bool Prefix(PhotonMessageInfo pmi)
{
return Instance.HostOnClientJoined(pmi.sender);
}
}

[HarmonyPatch(typeof(PLServer), "VerifyClient")] //Client sends mod info as early as possible during connection
/*[HarmonyPatch(typeof(PLServer), "VerifyClient")] //Client sends mod info as early as possible during connection
class ClientJoinPatch
{
static void Postfix(PhotonPlayer player, PhotonMessageInfo pmi)
Expand All @@ -530,6 +540,31 @@ static void Postfix(PhotonPlayer player, PhotonMessageInfo pmi)
player.Verified = true;
}
}
}*/

[HarmonyPatch(typeof(PLServer), "Update")]
class AttemptGetVerifiedSendPatch
{
static void PatchMethod() //Client sends mod info just before requesting verification
{
Logger.Info("Sending 'RecieveConnectionMessage' RPC");
ModMessageHelper.Instance.photonView.RPC("ReceiveConnectionMessage", PhotonTargets.MasterClient, new object[]
{
Instance.SerializeHashfullUserData()
});
}
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
List<CodeInstruction> targetSequence = new List<CodeInstruction>()
{
new CodeInstruction(OpCodes.Ldstr, "Attempting to get verified")
};
List<CodeInstruction> injectedSequence = new List<CodeInstruction>()
{
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(AttemptGetVerifiedSendPatch), "PatchMethod"))
};
return HarmonyHelpers.PatchBySequence(instructions, targetSequence, injectedSequence);
}
}

[HarmonyPatch(typeof(PLServer), "RemovePlayer")]
Expand Down
38 changes: 38 additions & 0 deletions PulsarModLoader/ModMessage/AllowPMLRPCPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using HarmonyLib;
using PulsarModLoader.Patches;
using System.Collections.Generic;
using System.Reflection.Emit;

namespace PulsarModLoader.ModMessages
{
[HarmonyPatch(typeof(NetworkingPeer), "ExecuteRpc")]
class AllowPMLRPCPatch
{
static bool PatchMethod(bool ShouldContinue, string MethodName)
{
if (MethodName == "ReceiveConnectionMessage" || MethodName == "ReceiveMessage")
{
return true;
}
else
{
return ShouldContinue;
}
}
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
List<CodeInstruction> targetSequence = new List<CodeInstruction>()
{
new CodeInstruction(OpCodes.Stloc_S, (byte)10),
new CodeInstruction(OpCodes.Ldloc_S, (byte)10),
};

List<CodeInstruction> injectedSequence = new List<CodeInstruction>()
{
new CodeInstruction(OpCodes.Ldloc_2),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(AllowPMLRPCPatch), "PatchMethod")),
};
return HarmonyHelpers.PatchBySequence(instructions, targetSequence, injectedSequence, patchMode: HarmonyHelpers.PatchMode.AFTER);
}
}
}
1 change: 1 addition & 0 deletions PulsarModLoader/PulsarModLoader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
<Compile Include="CustomGUI\GUIMain.cs" />
<Compile Include="CustomGUI\ModSettingsMenu.cs" />
<Compile Include="CustomGUI\PMLSettings.cs" />
<Compile Include="ModMessage\AllowPMLRPCPatch.cs" />
<Compile Include="ModMessage\ModMessage.cs" />
<Compile Include="ModMessage\ModMessageHelper.cs" />
<Compile Include="MPModChecks\MPModCheckManager.cs" />
Expand Down

0 comments on commit 4114f27

Please sign in to comment.