using HarmonyLib;
using Hazel;
namespace BetterAmongUs.Examples;
///
/// Example patch that demonstrates how to send BetterAmongUs modded support flags to BAU users.
/// This patch hooks into the player info serialization process to transmit custom flags that control various BAU features.
///
[HarmonyPatch]
internal static class SendBAUSupportFlagsExamplePatch
{
///
/// Postfix patch that runs after NetworkedPlayerInfo.Serialize.
/// When the host is serializing the player data, this method writes
/// BAU support flags into the message stream to communicate mod capabilities to the BAU client.
///
/// The NetworkedPlayerInfo instance being serialized.
/// The MessageWriter used for serialization.
///
/// This example sends the "command.force.bau.prefix" flag which forces all BAU commands
/// to use the "bau:" prefix. The flag is hashed using GetFlagHash for efficient transmission.
///
/// Important: This functionality only works on protocol version +25 (modded protocol)!
///
///
[HarmonyPatch(typeof(NetworkedPlayerInfo), nameof(NetworkedPlayerInfo.Serialize))]
[HarmonyPriority(Priority.Last)]
[HarmonyPostfix]
internal static void Serialize_Postfix(NetworkedPlayerInfo __instance, MessageWriter writer)
{
if (!AmongUsClient.Instance.AmHost || __instance.OwnerId != -2)
return;
writer.Write("bau:flags"); // Mark message as BAU flags
writer.WritePacked(1); // Amount of flags we are sending
writer.Write(GetFlagHash("command.force.bau.prefix")); // Send flag hash, must use GetFlagHash with the exact logic in this example!
}
///
/// Generates a deterministic integer hash from a string for use as a flag identifier.
///
/// The input string to hash
///
/// A 32-bit integer hash value. Returns 0 for null or empty strings.
///
internal static int GetFlagHash(string input)
{
if (string.IsNullOrEmpty(input)) return 0;
int hash = 17;
hash = hash * 31 + input.Length;
for (int i = 0; i < input.Length; i++)
{
char c = input[i];
hash = hash * 31 + c;
hash = hash * 31 + i;
}
hash = hash * 31 + input.Length;
return hash;
}
}