mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-20 11:39:45 +00:00
untested role sync changes
This commit is contained in:
1 parent
f560c8ca02
commit
eafc0bea44
4 files changed
+63
-52
No files matched your search
@@ -49,17 +49,15 @@ namespace MiraAPI.GameOptions
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void SyncAllOptions(int targetId)
|
||||
public static void SyncAllOptions(int targetId=-1)
|
||||
{
|
||||
List<NetData> data = [];
|
||||
int count = 0;
|
||||
Logger<MiraApiPlugin>.Error("num options: " + ModdedOptions.Count);
|
||||
foreach (var option in ModdedOptions.Values)
|
||||
{
|
||||
var netData = option.GetNetData();
|
||||
data.Add(netData);
|
||||
count += netData.GetLength();
|
||||
Logger<MiraApiPlugin>.Error($"syncing data for {netData.Id}");
|
||||
|
||||
if (count > 1000)
|
||||
{
|
||||
@@ -74,15 +72,13 @@ namespace MiraAPI.GameOptions
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void HandleSyncOptions(NetData[] data)
|
||||
{
|
||||
foreach (var netData in data)
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error($"syncing data for {netData.Id}");
|
||||
|
||||
if (ModdedOptions.TryGetValue(netData.Id, out var option))
|
||||
{
|
||||
Logger<MiraApiPlugin>.Error($"syncing data for {option.Title}");
|
||||
option.HandleNetData(netData.Data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,51 +8,42 @@ using Reactor.Utilities;
|
||||
namespace MiraAPI.Networking;
|
||||
|
||||
[RegisterCustomRpc((uint)MiraRpc.SyncRoleOptions)]
|
||||
public class SyncRoleOptionsRpc(MiraApiPlugin plugin, uint id) : PlayerCustomRpc<MiraApiPlugin, SyncRoleOptionsRpc.Data>(plugin, id)
|
||||
public class SyncRoleOptionsRpc(MiraApiPlugin plugin, uint id) : PlayerCustomRpc<MiraApiPlugin, NetData[]>(plugin, id)
|
||||
{
|
||||
|
||||
public struct Data(ushort roleId, int number, int chance)
|
||||
{
|
||||
public readonly ushort RoleId = roleId;
|
||||
public readonly int Number = number;
|
||||
public readonly int Chance = chance;
|
||||
}
|
||||
|
||||
public override RpcLocalHandling LocalHandling => RpcLocalHandling.None;
|
||||
|
||||
public override void Write(MessageWriter writer, Data data)
|
||||
public override void Write(MessageWriter writer, NetData[] data)
|
||||
{
|
||||
writer.Write(data.RoleId);
|
||||
writer.WritePacked(data.Number);
|
||||
writer.WritePacked(data.Chance);
|
||||
writer.WritePacked((uint)data.Length);
|
||||
foreach (var netData in data)
|
||||
{
|
||||
writer.WritePacked(netData.Id);
|
||||
writer.WriteBytesAndSize(netData.Data);
|
||||
}
|
||||
}
|
||||
|
||||
public override Data Read(MessageReader reader)
|
||||
public override NetData[] Read(MessageReader reader)
|
||||
{
|
||||
return new Data(reader.ReadUInt16(), reader.ReadPackedInt32(), reader.ReadPackedInt32());
|
||||
var length = reader.ReadPackedUInt32();
|
||||
var data = new NetData[length];
|
||||
for (var i = 0; i < length; i++)
|
||||
{
|
||||
var id = reader.ReadPackedUInt32();
|
||||
var bytes = reader.ReadBytesAndSize();
|
||||
data[i] = new NetData(id, bytes);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public override void Handle(PlayerControl playerControl, Data data)
|
||||
public override void Handle(PlayerControl playerControl, NetData[] data)
|
||||
{
|
||||
if (AmongUsClient.Instance.HostId != playerControl.OwnerId)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!CustomRoleManager.CustomRoles.TryGetValue(data.RoleId, out var roleBehaviour) ||
|
||||
!CustomRoleManager.GetCustomRoleBehaviour(roleBehaviour.Role, out var role)) return;
|
||||
|
||||
PluginSingleton<MiraApiPlugin>.Instance.Config.TryGetEntry<int>(role.NumConfigDefinition, out var numEntry);
|
||||
PluginSingleton<MiraApiPlugin>.Instance.Config.TryGetEntry<int>(role.ChanceConfigDefinition, out var chanceEntry);
|
||||
|
||||
try
|
||||
{
|
||||
numEntry.Value = data.Number;
|
||||
chanceEntry.Value = data.Chance;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger<MiraApiPlugin>.Warning(e.ToString());
|
||||
}
|
||||
CustomRoleManager.HandleSyncRoleOptions(data);
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,6 @@ public static class AmongUsClientSyncPatch
|
||||
}
|
||||
|
||||
ModdedOptionsManager.SyncAllOptions(clientData.Id);
|
||||
CustomRoleManager.SyncRoleSettings(clientData.Id);
|
||||
CustomRoleManager.SyncAllRoleSettings(clientData.Id);
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ using Reactor.Utilities.Extensions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using HarmonyLib;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
@@ -135,9 +136,11 @@ public static class CustomRoleManager
|
||||
|
||||
panel.SetTaskText(role.SetTabText().ToString());
|
||||
}
|
||||
|
||||
public static void SyncRoleSettings()
|
||||
|
||||
public static void SyncAllRoleSettings(int targetId=-1)
|
||||
{
|
||||
List<NetData> data = [];
|
||||
int count = 0;
|
||||
foreach (var role in CustomRoles.Values)
|
||||
{
|
||||
ICustomRole customRole = role as ICustomRole;
|
||||
@@ -145,28 +148,49 @@ public static class CustomRoleManager
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
PluginSingleton<MiraApiPlugin>.Instance.Config.TryGetEntry<int>(customRole.NumConfigDefinition, out var numEntry);
|
||||
PluginSingleton<MiraApiPlugin>.Instance.Config.TryGetEntry<int>(customRole.ChanceConfigDefinition, out var chanceEntry);
|
||||
|
||||
Rpc<SyncRoleOptionsRpc>.Instance.Send(new SyncRoleOptionsRpc.Data((ushort)role.Role, numEntry.Value, chanceEntry.Value));
|
||||
|
||||
var netData = new NetData((uint)role.Role, BitConverter.GetBytes(numEntry.Value).AddRangeToArray(BitConverter.GetBytes(chanceEntry.Value)));
|
||||
|
||||
data.Add(netData);
|
||||
count += netData.GetLength();
|
||||
|
||||
if (count > 1000)
|
||||
{
|
||||
Rpc<SyncRoleOptionsRpc>.Instance.SendTo(PlayerControl.LocalPlayer, targetId, data.ToArray());
|
||||
data.Clear();
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
if (data.Count > 0)
|
||||
{
|
||||
Rpc<SyncRoleOptionsRpc>.Instance.SendTo(PlayerControl.LocalPlayer, targetId, data.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
public static void SyncRoleSettings(int targetId)
|
||||
public static void HandleSyncRoleOptions(NetData[] data)
|
||||
{
|
||||
foreach (var role in CustomRoles.Values)
|
||||
foreach (var netData in data)
|
||||
{
|
||||
ICustomRole customRole = role as ICustomRole;
|
||||
if (customRole is null or { HideSettings: true })
|
||||
if (CustomRoles.TryGetValue((ushort)netData.Id, out var role))
|
||||
{
|
||||
continue;
|
||||
var customRole = role as ICustomRole;
|
||||
if (customRole is null or { HideSettings: true })
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var num = BitConverter.ToInt32(netData.Data, 0);
|
||||
var chance = BitConverter.ToInt32(netData.Data, 4);
|
||||
|
||||
PluginSingleton<MiraApiPlugin>.Instance.Config.TryGetEntry<int>(customRole.NumConfigDefinition, out var numEntry);
|
||||
PluginSingleton<MiraApiPlugin>.Instance.Config.TryGetEntry<int>(customRole.ChanceConfigDefinition, out var chanceEntry);
|
||||
|
||||
numEntry.Value = num;
|
||||
chanceEntry.Value = chance;
|
||||
}
|
||||
|
||||
PluginSingleton<MiraApiPlugin>.Instance.Config.TryGetEntry<int>(customRole.NumConfigDefinition, out var numEntry);
|
||||
PluginSingleton<MiraApiPlugin>.Instance.Config.TryGetEntry<int>(customRole.ChanceConfigDefinition, out var chanceEntry);
|
||||
|
||||
Rpc<SyncRoleOptionsRpc>.Instance.SendTo(targetId, new SyncRoleOptionsRpc.Data((ushort)role.Role, numEntry.Value, chanceEntry.Value));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user