even more rpc optimizations

This commit is contained in:
Gurge44 committed 2025-04-21 11:40:15 +02:00
1 parent f724e08f25
commit f79919845c
10 files changed
+98 -66

No files matched your search

@@ -4,6 +4,7 @@
<option name="locations">
<list>
<option value="$PROJECT_DIR$/bin/Release/net8.0-windows/win-x64/publish" />
<option value="$PROJECT_DIR$/publish" />
</list>
</option>
</component>
+28 -23
View File
@@ -267,15 +267,16 @@ public static class CaptureTheFlag
{
var sender = CustomRpcSender.Create("CTF - ResetSkins", SendOption.Reliable);
var hasValue = false;
foreach ((byte key, NetworkedPlayerInfo.PlayerOutfit outfit) in DefaultOutfits)
{
PlayerControl pc = key.GetPlayer();
if (pc != null && outfit != null)
{
Utils.RpcChangeSkin(pc, outfit, sender);
hasValue = true;
if (sender.stream.Length > 800)
{
sender.SendMessage();
@@ -284,7 +285,7 @@ public static class CaptureTheFlag
}
}
}
sender.SendMessage(dispose: !hasValue);
}
}
@@ -406,7 +407,7 @@ public static class CaptureTheFlag
private static void Restart()
{
Logger.Info("Restarting Capture The Flag game", "CTF");
var sender = CustomRpcSender.Create("CTF - Restart", SendOption.Reliable);
var hasValue = false;
@@ -418,7 +419,7 @@ public static class CaptureTheFlag
hasValue |= data.Players.ToValidPlayers().Aggregate(hasValue, (current, pc) => current || sender.TP(pc, flagBase));
}
sender.SendMessage(!hasValue);
}
@@ -427,7 +428,7 @@ public static class CaptureTheFlag
if (!ValidTag || TemporarilyOutPlayers.ContainsKey(killer.PlayerId) || !PlayerTeams.TryGetValue(target.PlayerId, out CTFTeam targetTeam) || !PlayerTeams.TryGetValue(killer.PlayerId, out CTFTeam killerTeam) || killerTeam == targetTeam || TeamData.Values.Any(x => x.FlagCarrier == killer.PlayerId)) return;
var sender = CustomRpcSender.Create("CTF - OnCheckMurder", SendOption.Reliable);
new[] { killer, target }.Do(x => sender.SetKillCooldown(x, TagCooldown.GetFloat()));
if (TeamData.FindFirst(x => x.Value.FlagCarrier == target.PlayerId, out KeyValuePair<CTFTeam, CTFTeamData> kvp))
@@ -460,7 +461,7 @@ public static class CaptureTheFlag
if (PlayerData.TryGetValue(killer.PlayerId, out CTFPlayerData data)) data.TagCount++;
sender.NotifyRolesSpecific(killer, killer);
sender.SendMessage();
}
@@ -594,7 +595,7 @@ public static class CaptureTheFlag
Main.AllPlayerSpeed[id] = Main.RealOptionsData.GetFloat(FloatOptionNames.PlayerSpeedMod) - SpeedReductionForFlagCarrier.GetFloat();
PlayerGameOptionsSender.SetDirty(id);
var sender = CustomRpcSender.Create("CTF - PickUpFlag", SendOption.Reliable);
var hasValue = false;
@@ -609,7 +610,7 @@ public static class CaptureTheFlag
if (arrow) TargetArrow.Add(x.PlayerId, id);
sender.Notify(x, Utils.ColorString(Color.yellow, Translator.GetString("CTF_FlagTaken")));
hasValue = true;
if (sender.stream.Length > 800)
{
sender.SendMessage();
@@ -630,7 +631,7 @@ public static class CaptureTheFlag
if (arrow) TargetArrow.Add(x.PlayerId, id);
sender.Notify(x, Translator.GetString("CTF_EnemyFlagTaken"));
hasValue = true;
if (sender.stream.Length > 800)
{
sender.SendMessage();
@@ -639,17 +640,25 @@ public static class CaptureTheFlag
}
});
}
sender.SendMessage(dispose: !hasValue);
}
public void DropFlag()
{
Logger.Info($"{FlagCarrier.ColoredPlayerName().RemoveHtmlTags()} dropped the {team} flag", "CTF");
if (ArrowToEnemyFlagCarrier.GetBool() || ArrowToOwnFlagCarrier.GetBool()) TeamData.Values.SelectMany(x => x.Players).Do(x => TargetArrow.Remove(x, FlagCarrier));
FlagCarrier = byte.MaxValue;
Utils.NotifyRoles();
if (FlagCarrier != byte.MaxValue)
{
if (ArrowToEnemyFlagCarrier.GetBool() || ArrowToOwnFlagCarrier.GetBool())
TeamData.Values.SelectMany(x => x.Players).Do(x => TargetArrow.Remove(x, FlagCarrier));
Main.AllPlayerSpeed[FlagCarrier] = Main.RealOptionsData.GetFloat(FloatOptionNames.PlayerSpeedMod);
PlayerGameOptionsSender.SetDirty(FlagCarrier);
FlagCarrier = byte.MaxValue;
Utils.NotifyRoles();
}
}
public bool IsNearFlag(Vector2 pos)
@@ -700,7 +709,7 @@ public static class CaptureTheFlag
if (!PlayerTeams.TryGetValue(__instance.PlayerId, out CTFTeam team)) return;
bool blue = team == CTFTeam.Blue;
int colorId = blue ? 1 : 5;
var sender = CustomRpcSender.Create("CTF - FixedUpdate", SendOption.Reliable);
var hasValue = false;
@@ -726,16 +735,12 @@ public static class CaptureTheFlag
}
else if (GameEndCriteria.GetValue() != 2)
{
sender.NotifyRolesSpecific(__instance, __instance);
if (Vector2.Distance(pos, blackRoomPS) > 2f) sender.TP(__instance, blackRoomPS);
hasValue = true;
hasValue |= sender.NotifyRolesSpecific(__instance, __instance);
if (Vector2.Distance(pos, blackRoomPS) > 2f) hasValue |= sender.TP(__instance, blackRoomPS);
}
}
else if (Vector2.Distance(pos, blackRoomPS) <= 2f)
{
hasValue |= sender.TP(__instance, team.GetFlagBase().Position);
}
else if (Vector2.Distance(pos, blackRoomPS) <= 2f) { hasValue |= sender.TP(__instance, team.GetFlagBase().Position); }
sender.SendMessage(dispose: !hasValue);
}
}
+7 -8
View File
@@ -309,7 +309,7 @@ public static class KingOfTheZones
writer.SendMessage(dispose: !hasData);
yield return new WaitForSeconds(showTutorial ? 11f : 2f);
yield return new WaitForSeconds(showTutorial ? 8f : 2f);
NameNotifyManager.Reset();
if (!GameStates.InGame || !Main.IntroDestroyed) goto End;
@@ -335,14 +335,14 @@ public static class KingOfTheZones
};
aapc.NotifyPlayers($"<#ffffff>{gameEnd}</color>", 100f);
yield return new WaitForSeconds(endByPoints && endByTime ? 8f : 5f);
yield return new WaitForSeconds(4f);
NameNotifyManager.Reset();
if (!GameStates.InGame || !Main.IntroDestroyed) goto End;
string tags = string.Format(GetString("KOTZ.Notify.Tutorial.Tagging"), tagCooldown, respawnTime);
aapc.NotifyPlayers($"<#ffffff>{tags}</color>", 100f);
yield return new WaitForSeconds(7f);
yield return new WaitForSeconds(4f);
NameNotifyManager.Reset();
if (!GameStates.InGame || !Main.IntroDestroyed) goto End;
@@ -351,7 +351,7 @@ public static class KingOfTheZones
string zoneMovement = string.Format(GetString(AllZonesMoveAtOnce.GetBool() ? "KOTZ.Notify.Tutorial.ZonesMoving.AllAtOnce" : "KOTZ.Notify.Tutorial.ZonesMoving.Separately"), ZoneMoveTime.GetInt());
aapc.NotifyPlayers($"<#ffffff>{zoneMovement}</color>", 100f);
yield return new WaitForSeconds(5f);
yield return new WaitForSeconds(4f);
NameNotifyManager.Reset();
if (!GameStates.InGame || !Main.IntroDestroyed) goto End;
@@ -362,7 +362,7 @@ public static class KingOfTheZones
string downTimeInfo = string.Format(GetString("KOTZ.Notify.Tutorial.ZonesMoving.Downtime"), downTime);
aapc.NotifyPlayers($"<#ffffff>{downTimeInfo}</color>", 100f);
yield return new WaitForSeconds(7f);
yield return new WaitForSeconds(4f);
NameNotifyManager.Reset();
if (!GameStates.InGame || !Main.IntroDestroyed) goto End;
}
@@ -378,7 +378,7 @@ public static class KingOfTheZones
{
LastShortInfo = info;
aapc.NotifyPlayers($"<#ffffff>{info}</color>", 100f);
yield return new WaitForSeconds(6f);
yield return new WaitForSeconds(4f);
NameNotifyManager.Reset();
if (!GameStates.InGame || !Main.IntroDestroyed) goto End;
}
@@ -424,9 +424,8 @@ public static class KingOfTheZones
{
for (var i = 3; i > 0; i--)
{
int time = i;
NameNotifyManager.Reset();
aapc.NotifyPlayers($"<#ffffff>{string.Format(GetString("RR_ReadyQM"), time)}</color>", 100f);
aapc.NotifyPlayers($"<#ffffff>{string.Format(GetString("RR_ReadyQM"), i)}</color>", 100f);
yield return new WaitForSeconds(1f);
}
+9 -4
View File
@@ -26,9 +26,10 @@ public class CustomRpcSender
}
private readonly bool isUnsafe;
public readonly string name;
private readonly string name;
private readonly OnSendDelegateType onSendDelegate;
private readonly SendOption sendOption;
public readonly MessageWriter stream;
// 0~: targetClientId (GameDataTo)
@@ -46,6 +47,7 @@ public class CustomRpcSender
this.name = name;
this.isUnsafe = isUnsafe;
this.sendOption = sendOption;
currentRpcTarget = -2;
onSendDelegate = () => { };
@@ -116,7 +118,8 @@ public class CustomRpcSender
throw new InvalidOperationException(errorMsg);
}
Logger.Info($"\"{name}\" is finished (Length: {stream.Length}, dispose: {dispose})", "CustomRpcSender");
if (stream.Length > 1500 && sendOption == SendOption.Reliable && !dispose) Logger.Warn($"Large reliable packet \"{name}\" is sending ({stream.Length} bytes)", "CustomRpcSender");
else Logger.Info($"\"{name}\" is finished (Length: {stream.Length}, dispose: {dispose}, sendOption: {sendOption})", "CustomRpcSender");
if (!dispose)
{
@@ -625,7 +628,7 @@ public static class CustomRpcSenderExtensions
CheckInvalidMovementPatch.ExemptedPlayers.Add(pc.PlayerId);
return true;
}
public static bool TPToRandomVent(this CustomRpcSender sender, PlayerControl pc, bool log = true)
{
Vent vent = ShipStatus.Instance.AllVents.RandomElement();
@@ -633,8 +636,10 @@ public static class CustomRpcSenderExtensions
return sender.TP(pc, new(vent.transform.position.x, vent.transform.position.y + 0.3636f), log);
}
public static void NotifyRolesSpecific(this CustomRpcSender sender, PlayerControl seer, PlayerControl target)
public static bool NotifyRolesSpecific(this CustomRpcSender sender, PlayerControl seer, PlayerControl target)
{
if (seer == null || seer.Data.Disconnected || (seer.IsModdedClient() && (seer.IsHost() || CustomGameMode.Standard.IsActiveOrIntegrated())) || (!SetUpRoleTextPatch.IsInIntro && GameStates.IsLobby)) return false;
Utils.WriteSetNameRpcsToSender(ref sender, false, false, false, false, false, false, seer, [seer], [target]);
return true;
}
}
+32 -12
View File
@@ -9,6 +9,7 @@ namespace EHR;
public static class NameNotifyManager
{
public static Dictionary<byte, Dictionary<string, long>> Notifies = [];
private static long LastUpdate;
public static void Reset()
{
@@ -34,7 +35,7 @@ public static class NameNotifyManager
if (log) Logger.Info($"New name notify for {pc.GetNameWithRole().RemoveHtmlTags()}: {text} ({time}s)", "Name Notify");
}
public static void OnFixedUpdate(PlayerControl player)
public static void OnFixedUpdate()
{
if (!GameStates.IsInTask)
{
@@ -42,21 +43,40 @@ public static class NameNotifyManager
return;
}
var removed = false;
long now = Utils.TimeStamp;
if (now == LastUpdate) return;
LastUpdate = now;
if (Notifies.TryGetValue(player.PlayerId, out Dictionary<string, long> notifies))
List<byte> toNotify = [];
foreach ((byte id, Dictionary<string, long> notifies) in Notifies)
{
foreach (KeyValuePair<string, long> notify in notifies.ToArray())
{
if (notify.Value <= Utils.TimeStamp)
{
notifies.Remove(notify.Key);
removed = true;
}
}
List<string> toRemove = [];
notifies.DoIf(x => x.Value <= now, x => toRemove.Add(x.Key));
toRemove.ForEach(x => notifies.Remove(x));
if (toRemove.Count > 0) toNotify.Add(id);
}
if (removed) Utils.NotifyRoles(SpecifySeer: player, SpecifyTarget: player);
if (!AmongUsClient.Instance.AmHost) return;
var sender = CustomRpcSender.Create("NameNotifyManager.OnFixedUpdate", SendOption.Reliable);
var hasValue = false;
toNotify.ToValidPlayers().ForEach(x =>
{
hasValue |= sender.NotifyRolesSpecific(x, x);
if (sender.stream.Length > 800)
{
sender.SendMessage();
sender = CustomRpcSender.Create("NameNotifyManager.OnFixedUpdate", SendOption.Reliable);
hasValue = false;
}
});
sender.SendMessage(dispose: !hasValue);
}
public static bool GetNameNotify(PlayerControl player, out string name)
+2
View File
@@ -2444,6 +2444,8 @@ public static class Utils
if (!forMeeting) selfName += "\r\n";
}
selfName = selfName.Trim().Replace("color=", "").Replace("<#ffffff><#ffffff>", "<#ffffff>");
sender.RpcSetName(seer, selfName, seer);
bool onlySelfNameUpdateRequired = Options.CurrentGameMode switch
+2 -3
View File
@@ -177,9 +177,8 @@ internal static class ExileControllerWrapUpPatch
if ((showRemainingKillers || ejectionNotify) && CustomGameMode.Standard.IsActiveOrIntegrated())
{
string text = showRemainingKillers ? Utils.GetRemainingKillers(true) : string.Empty;
text = $"<#ffffff>{text}</color>";
var r = IRandom.Instance;
var sender = CustomRpcSender.Create("ExileControllerWrapUpPatch.WrapUpFinalizer - 2", SendOption.Reliable);
var sender = CustomRpcSender.Create("ExileControllerWrapUpPatch.WrapUpFinalizer - 2", ejectionNotify ? SendOption.None : SendOption.Reliable);
var hasValue = false;
foreach (PlayerControl pc in Main.AllAlivePlayerControls)
@@ -191,7 +190,7 @@ internal static class ExileControllerWrapUpPatch
if (sender.stream.Length > 800)
{
sender.SendMessage();
sender = CustomRpcSender.Create("ExileControllerWrapUpPatch.WrapUpFinalizer - 2", SendOption.Reliable);
sender = CustomRpcSender.Create("ExileControllerWrapUpPatch.WrapUpFinalizer - 2", ejectionNotify ? SendOption.None : SendOption.Reliable);
hasValue = false;
}
}
+3 -3
View File
@@ -894,7 +894,7 @@ internal static class IntroCutsceneDestroyPatch
lp.RpcResetAbilityCooldown();
}, 1f, log: false);
if (Options.UsePets.GetBool())
if (Options.UsePets.GetBool() && Options.CurrentGameMode is CustomGameMode.Standard or CustomGameMode.HideAndSeek or CustomGameMode.CaptureTheFlag or CustomGameMode.AllInOne)
{
Main.ProcessShapeshifts = false;
@@ -1026,10 +1026,10 @@ internal static class IntroCutsceneDestroyPatch
if (CustomGameMode.Quiz.IsActiveOrIntegrated())
Main.Instance.StartCoroutine(Quiz.OnGameStart());
if (CustomGameMode.MoveAndStop.IsActiveOrIntegrated())
MoveAndStop.OnGameStart();
if (CustomGameMode.CaptureTheFlag.IsActiveOrIntegrated())
CaptureTheFlag.OnGameStart();
+5 -4
View File
@@ -1364,13 +1364,11 @@ internal static class FixedUpdatePatch
Zoom.OnFixedUpdate();
TextBoxTMPSetTextPatch.Update();
if (lowLoad && Options.CurrentGameMode is CustomGameMode.Standard or CustomGameMode.FFA or CustomGameMode.CaptureTheFlag or CustomGameMode.NaturalDisasters && GameStartTimeStamp + 50 == TimeStamp)
NotifyRoles();
if (!lowLoad) NameNotifyManager.OnFixedUpdate();
}
if (!lowLoad)
{
NameNotifyManager.OnFixedUpdate(player);
TargetArrow.OnFixedUpdate(player);
LocateArrow.OnFixedUpdate(player);
@@ -1378,6 +1376,9 @@ internal static class FixedUpdatePatch
{
Camouflage.OnFixedUpdate(player);
AFKDetector.OnFixedUpdate(player);
if (Options.CurrentGameMode is CustomGameMode.Standard or CustomGameMode.FFA or CustomGameMode.CaptureTheFlag or CustomGameMode.NaturalDisasters && GameStartTimeStamp + 50 == TimeStamp)
NotifyRoles();
}
if (RPCHandlerPatch.ReportDeadBodyRPCs.Remove(playerId)) Logger.Info($"Cleared ReportDeadBodyRPC Count for {player.GetRealName().RemoveHtmlTags()}", "FixedUpdatePatch");
@@ -1387,7 +1388,7 @@ internal static class FixedUpdatePatch
{
if (GameStates.IsLobby && ((ModUpdater.HasUpdate && ModUpdater.ForceUpdate) || ModUpdater.IsBroken || !Main.AllowPublicRoom) && AmongUsClient.Instance.IsGamePublic) AmongUsClient.Instance.ChangeGamePublic(false);
// Kick low level people
// Kick low-level people
if (!lowLoad && GameSettingMenuPatch.LastPresetChange + 5 < TimeStamp && GameStates.IsLobby && !player.AmOwner && Options.KickLowLevelPlayer.GetInt() != 0 && (
(player.Data.PlayerLevel != 0 && player.Data.PlayerLevel < Options.KickLowLevelPlayer.GetInt()) ||
player.Data.FriendCode == string.Empty
+9 -9
View File
@@ -4878,15 +4878,15 @@
"KOTZ.Notify.AssignedToTeam.Tan": "<#ffffff>You are in the </color>Tan Team",
"KOTZ.Notify.AssignedToTeam.Rose": "<#ffffff>You are in the </color>Rose Team",
"KOTZ.Notify.AssignedToTeam.Orange": "<#ffffff>You are in the </color>Orange Team",
"KOTZ.Notify.ShortIntro": "Welcome to another game of <#ff0000>King Of The Zones</color>!\\n<size=90%>We're playing in <#ffff44>{0}</color> teams and with <#ffff44>{1}</color> zones.\\nTag cooldown is <#ffff44>{2}</color> seconds, respawn time is <#ffff44>{3}</color> seconds.</size>\\nGood luck and have fun!",
"KOTZ.Notify.Tutorial.Basics": "The current game mode is <#ff0000>King Of The Zones</color>.\\n<size=90%>Players are split into <#ffff44>{0}</color> teams.\\nSome rooms on the map represent a zone. There are <#ffff44>{1}</color> zones.\\nIf a team has more players in a room than\\nall other teams, they control the zone.\\nEvery second, each team gets 1 point for each zone controlled.</size>",
"KOTZ.Notify.Tutorial.GameEnd.PointsOnly": "<size=90%>When a team reaches <#ffff44>{0}</color> points, the game ends and that team wins.\\n<size=70%>If 2 or more teams reach the goal at the same time,\\nthe game waits for the tie to be broken.</size></size>",
"KOTZ.Notify.Tutorial.GameEnd.TimeOnly": "<size=90%>After <#ffff44>{0} minutes and {1} seconds</color>, the team with the most points wins.\\n<size=70%>If there's a tie, the game waits for the tie to be broken.</size></size>",
"KOTZ.Notify.Tutorial.GameEnd.PointsOrTime": "<size=90%>The game can end in 2 ways:\\n1. a team reaches <#ffff44>{0}</color> points\\n2. <#ffff44>{1} minutes and {2} seconds</color> pass by.\\nAfter ensuring there isn't a tie, the team with the most points wins.</size>",
"KOTZ.Notify.Tutorial.Tagging": "<size=90%>Players can tag others using the kill button with a <#ffff44>{0}</color> second cooldown.\\nTagged players are teleported outside of the map\\nfor <#ffff44>{1}</color> seconds, then teleport back.</size>",
"KOTZ.Notify.Tutorial.ZonesMoving.AllAtOnce": "<size=90%>Every <#ffff44>{0}</color> seconds, all zones will move to a different room.\\nThis will be indicated by a timer next to the name of each room.</size>",
"KOTZ.Notify.Tutorial.ZonesMoving.Separately": "<size=90%>Every <#ffff44>{0}</color> seconds, a zone will move to a different room.\\nThis will be indicated by a timer next to the name of each room.</size>",
"KOTZ.Notify.Tutorial.ZonesMoving.Downtime": "<size=90%>After a zone has moved, it stays neutral for <#ffff44>{0}</color> seconds,\\nmeaning no teams can have majority in it.\\nIf the timer next to the name of a zone is gray,\\nit means the zone has downtime.</size>",
"KOTZ.Notify.ShortIntro": "Welcome to another game of <#ff0000>King Of The Zones</color>!\\n<size=90%><#ffff44>{0}</color> teams, <#ffff44>{1}</color> zones.\\nTag cooldown: <#ffff44>{2}</color>s, Respawn time: <#ffff44>{3}</color>s.</size>\\nGood luck and have fun!",
"KOTZ.Notify.Tutorial.Basics": "We're playing <#ff0000>King Of The Zones</color>.\\n<size=90%>Players are split into <#ffff44>{0}</color> teams.\\n<#ffff44>{1}</color> rooms on the map represent a zone.\\nTeams must have majority (by player count) in a zone to control it, which is awarded by 1 point for each zone every second.</size>",
"KOTZ.Notify.Tutorial.GameEnd.PointsOnly": "<size=90%>When a team reaches <#ffff44>{0}</color> points, the game ends and they win.\\n<size=70%>Upon a tie, the game ends when the tie breaks.</size></size>",
"KOTZ.Notify.Tutorial.GameEnd.TimeOnly": "<size=90%>After <#ffff44>{0} minutes and {1} seconds</color>, the team with the most points wins.\\n<size=70%>Upon a tie, the game ends when the tie breaks.</size></size>",
"KOTZ.Notify.Tutorial.GameEnd.PointsOrTime": "<size=90%>The game ends if a team reaches <#ffff44>{0}</color> points or after <#ffff44>{1}m {2}s</color>.\\nWhen there's no tie, the team with the most points wins.</size>",
"KOTZ.Notify.Tutorial.Tagging": "<size=90%>Players can tag others using the kill button (cooldown: <#ffff44>{0}</color>s),\\nwhich teleports them outside the map for <#ffff44>{1}</color>s.</size>",
"KOTZ.Notify.Tutorial.ZonesMoving.AllAtOnce": "<size=90%>Every <#ffff44>{0}</color>s, all zones move to a different room.\\n(Indicated by timers next to the room names.)</size>",
"KOTZ.Notify.Tutorial.ZonesMoving.Separately": "<size=90%>Every <#ffff44>{0}</color>s, a zone moves to a different room.\\n(Indicated by timers next to the room names.)</size>",
"KOTZ.Notify.Tutorial.ZonesMoving.Downtime": "<size=90%>After a zone has moved, it stays neutral for <#ffff44>{0}</color>s,\\nmeaning no teams can have majority in it.\\n(Indicated by a gray timer.)</size>",
"KOTZ.SuffixHelp.Zones.Single": "<size=80%>Zone: </size>",
"KOTZ.SuffixHelp.Zones.Plural": "<size=80%>Zones: </size>",
"KOTZ.SuffixHelp.Points": "<size=80%>Points: </size>",