Skip to content

Commit

Permalink
hashing and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Priscillalala committed Dec 29, 2023
1 parent db3f12c commit f0a65f7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 51 deletions.
8 changes: 1 addition & 7 deletions R2API.ProcType.Patcher/ProcTypePatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
using BepInEx.Logging;
using Mono.Cecil;
using System.Collections.Generic;
using System.Net;

namespace R2API;

public static class ProcTypePatcher
{
private static readonly ManualLogSource logger = Logger.CreateLogSource("ProcTypePatcher");

public static IEnumerable<string> TargetDLLs
{
get
Expand All @@ -20,10 +17,7 @@ public static IEnumerable<string> TargetDLLs

public static void Patch(AssemblyDefinition assembly)
{
logger.LogInfo("Patching!");
TypeDefinition procChainMask = assembly.MainModule.GetType("RoR2", "ProcChainMask");
logger.LogInfo(procChainMask.FullName);
procChainMask.Fields.Add(new FieldDefinition("r2api_moddedMask", FieldAttributes.Public, assembly.MainModule.ImportReference(typeof(bool[]))));
logger.LogInfo("Done!");
procChainMask?.Fields.Add(new FieldDefinition("r2api_moddedMask", FieldAttributes.Public, assembly.MainModule.ImportReference(typeof(bool[]))));
}
}
72 changes: 28 additions & 44 deletions R2API.ProcType/ProcTypeAPI.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using HG;
using Mono.Cecil.Cil;
Expand All @@ -9,13 +10,10 @@
using RoR2;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UIElements;

namespace R2API;

#pragma warning disable CS0436 // Type conflicts with imported type
[AutoVersion]
#pragma warning restore CS0436 // Type conflicts with imported type
public static partial class ProcTypeAPI
{
public const string PluginGUID = R2API.PluginGUID + ".proctype";
Expand Down Expand Up @@ -49,28 +47,6 @@ private set
private static int remainingBitCount;
private static bool[] buffer;
#endregion
private static NetworkSettings _networkSettings;

internal struct NetworkSettings
{
internal int totalByteCount;
internal int filledByteCount;
internal int remainingBitCount;
internal bool[] buffer;

internal void Update()
{

}

internal NetworkSettings(int length)
{
totalByteCount = length + 7 >> 3;
filledByteCount = totalByteCount - 1;
remainingBitCount = length - (filledByteCount << 3);
buffer = new bool[length];
}
}

#region API
public static ModdedProcType ReserveProcType()
Expand Down Expand Up @@ -251,9 +227,7 @@ private static ProcChainMask NetworkExtensions_ReadProcChainMask(On.RoR2.Network

private static int ProcChainMask_GetHashCode(On.RoR2.ProcChainMask.orig_GetHashCode orig, ref ProcChainMask self)
{
bool[] mask = ProcTypeInterop.GetModdedMask(self);

return orig(ref self) * 397 ^ ProcTypeInterop.GetModdedMask(self).GetHashCode();
return orig(ref self) * 397 ^ ModdedMaskHashCode(ProcTypeInterop.GetModdedMask(self));
}

private static bool ProcChainMask_Equals_ProcChainMask(On.RoR2.ProcChainMask.orig_Equals_ProcChainMask orig, ref ProcChainMask self, ProcChainMask other)
Expand All @@ -269,41 +243,32 @@ private static void Init()
buffer = new bool[ModdedProcTypeCount];
}

private static void UpdateNetworkSettings()
{
totalByteCount = ModdedProcTypeCount + 7 >> 3;
filledByteCount = totalByteCount - 1;
remainingBitCount = ModdedProcTypeCount - (filledByteCount << 3);
if (buffer != null)
{
Array.Resize(ref buffer, ModdedProcTypeCount);
}
}

private static void NetworkWriteModdedMask(this NetworkWriter writer, bool[] values)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void NetworkWriteModdedMask(this NetworkWriter writer, bool[] mask)
{
int valuesIndex = 0;
int maskIndex = 0;
for (int i = 0; i < totalByteCount; i++)
{
byte b = 0;
if (values == null || valuesIndex < values.Length)
if (mask == null || maskIndex < mask.Length)
{
int validBitCount = (i < filledByteCount) ? 8 : remainingBitCount;
int bitIndex = 0;
while (bitIndex < validBitCount)
{
if (ArrayUtils.GetSafe(values, valuesIndex))
if (ArrayUtils.GetSafe(mask, maskIndex))
{
b |= (byte)(1 << bitIndex);
}
bitIndex++;
valuesIndex++;
maskIndex++;
}
}
writer.Write(b);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool[] NetworkReadModdedMask(NetworkReader reader)
{
int finalLength = 0;
Expand Down Expand Up @@ -332,6 +297,25 @@ private static bool[] NetworkReadModdedMask(NetworkReader reader)
return result;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static byte ModdedMaskHashCode(bool[] mask)
{
byte b = 0;
if (mask != null)
{
int length = Math.Min(mask.Length, 8);
for (int i = 0; i < length; i++)
{
if (mask[i])
{
b |= (byte)(1 << i);
}
}
}
return b;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool ModdedMaskEquals(bool[] a, bool[] b)
{
if (a == null)
Expand Down

0 comments on commit f0a65f7

Please sign in to comment.