From db327604c58451eeca47a1f34e0f3f3a25285e1c Mon Sep 17 00:00:00 2001
From: XtraCube <72575280+XtraCube@users.noreply.github.com>
Date: Thu, 13 Mar 2025 12:19:08 -0400
Subject: [PATCH] Remove PopulateResultsRpc and simplify.
---
MiraAPI/Networking/MiraRpc.cs | 5 --
MiraAPI/Patches/Voting/MeetingHudPatches.cs | 24 ++++++--
MiraAPI/Voting/PopulateResultsRpc.cs | 62 ---------------------
3 files changed, 18 insertions(+), 73 deletions(-)
delete mode 100644 MiraAPI/Voting/PopulateResultsRpc.cs
diff --git a/MiraAPI/Networking/MiraRpc.cs b/MiraAPI/Networking/MiraRpc.cs
index 6d51e2a..9ee1088 100644
--- a/MiraAPI/Networking/MiraRpc.cs
+++ b/MiraAPI/Networking/MiraRpc.cs
@@ -40,11 +40,6 @@ public enum MiraRpc : uint
///
CustomGameOver,
- ///
- /// Custom RPC to handle populating vote results.
- ///
- PopulateResults,
-
///
/// Custom RPC to undo a player's vote.
///
diff --git a/MiraAPI/Patches/Voting/MeetingHudPatches.cs b/MiraAPI/Patches/Voting/MeetingHudPatches.cs
index d38c075..b375914 100644
--- a/MiraAPI/Patches/Voting/MeetingHudPatches.cs
+++ b/MiraAPI/Patches/Voting/MeetingHudPatches.cs
@@ -1,11 +1,11 @@
using System.Linq;
using HarmonyLib;
+using Il2CppInterop.Runtime.InteropTypes.Arrays;
using MiraAPI.Events;
using MiraAPI.Events.Vanilla.Meeting;
using MiraAPI.Events.Vanilla.Meeting.Voting;
using MiraAPI.Utilities;
using MiraAPI.Voting;
-using Reactor.Networking.Rpc;
namespace MiraAPI.Patches.Voting;
@@ -201,21 +201,33 @@ internal static class MeetingHudPatches
exiled = @event.ExiledPlayer;
}
- __instance.RpcVotingComplete(new MeetingHud.VoterState[__instance.playerStates.Length], exiled, isTie);
+ var voterStates = new Il2CppStructArray([
+ .. votes.Select(
+ v=> new MeetingHud.VoterState
+ {
+ VoterId = v.Voter,
+ VotedForId = v.Suspect,
+ })
+ ]);
+
+ __instance.RpcVotingComplete(voterStates, exiled, isTie);
return false;
}
[HarmonyPrefix]
[HarmonyPatch(nameof(MeetingHud.PopulateResults))]
- public static bool PopulateResultsPatch(MeetingHud __instance)
+ public static bool PopulateResultsPatch(MeetingHud __instance, ref Il2CppStructArray states)
{
- if (!AmongUsClient.Instance.AmHost)
+ var votes = states.Select(x=> new CustomVote(x.VoterId, x.VotedForId)).ToList();
+ var @event = new PopulateResultsEvent(votes);
+ MiraEventManager.InvokeEvent(@event);
+
+ if (@event.IsCancelled)
{
return false;
}
- var votes = VotingUtils.CalculateVotes();
- Rpc.Instance.Send([.. votes]);
+ VotingUtils.HandlePopulateResults(votes);
return false;
}
diff --git a/MiraAPI/Voting/PopulateResultsRpc.cs b/MiraAPI/Voting/PopulateResultsRpc.cs
deleted file mode 100644
index 24d21ff..0000000
--- a/MiraAPI/Voting/PopulateResultsRpc.cs
+++ /dev/null
@@ -1,62 +0,0 @@
-using Hazel;
-using MiraAPI.Networking;
-using Reactor.Networking.Attributes;
-using Reactor.Networking.Rpc;
-
-namespace MiraAPI.Voting;
-
-///
-/// Handles the networking for populating results of a meeting. Custom implementation to work with Mira voting API.
-///
-[RegisterCustomRpc((uint)MiraRpc.PopulateResults)]
-public class PopulateResultsRpc(MiraApiPlugin plugin, uint id) : PlayerCustomRpc(plugin, id)
-{
- ///
- public override RpcLocalHandling LocalHandling => RpcLocalHandling.Before;
-
- ///
- public override void Write(MessageWriter writer, CustomVote[]? data)
- {
- if (data == null)
- {
- writer.WritePacked(0);
- return;
- }
-
- writer.WritePacked(data.Length);
- foreach (var t in data)
- {
- writer.Write(t.Voter);
- writer.Write(t.Suspect);
- }
- }
-
- ///
- public override CustomVote[] Read(MessageReader reader)
- {
- var votes = new CustomVote[reader.ReadPackedInt32()];
-
- for (var i = 0; i < votes.Length; i++)
- {
- votes[i] = new CustomVote(reader.ReadByte(), reader.ReadByte());
- }
-
- return votes;
- }
-
- ///
- public override void Handle(PlayerControl innerNetObject, CustomVote[]? data)
- {
- if (AmongUsClient.Instance.HostId != innerNetObject.OwnerId)
- {
- return;
- }
-
- if (data == null)
- {
- return;
- }
-
- VotingUtils.HandlePopulateResults([.. data]);
- }
-}