Skip to content

Commit

Permalink
replace SHA256 hash to FNV1a 64bit hash because it's crashes on Godot…
Browse files Browse the repository at this point in the history
…/Net8 android
  • Loading branch information
RevenantX committed Feb 8, 2024
1 parent 0cbe30a commit 3d80a5a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
20 changes: 11 additions & 9 deletions LiteEntitySystem/EntityTypesMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using LiteEntitySystem.Internal;

namespace LiteEntitySystem
Expand All @@ -24,7 +22,6 @@ public abstract class EntityTypesMap
{
internal ushort MaxId;
internal readonly Dictionary<Type, RegisteredTypeInfo> RegisteredTypes = new();
private readonly SHA256 _sha256 = SHA256.Create();
private bool _isFinished;
private const BindingFlags FieldsFlags = BindingFlags.Instance |
BindingFlags.Public |
Expand All @@ -35,8 +32,11 @@ public abstract class EntityTypesMap
/// Can be used to detect that server/client has difference
/// </summary>
/// <returns>hash</returns>
public byte[] EvaluateEntityClassDataHash()
public ulong EvaluateEntityClassDataHash()
{
//FNV1a 64 bit hash
ulong hash = 14695981039346656037UL; //offset

if (!_isFinished)
{
foreach (var (entType, _) in RegisteredTypes.OrderBy(kv => kv.Value.ClassId))
Expand All @@ -57,20 +57,22 @@ public byte[] EvaluateEntityClassDataHash()
}
}
}
_sha256.TransformFinalBlock(new byte[]{ 255 }, 0, 1);
_isFinished = true;
}

return _sha256.Hash;
return hash;

void TryHashField(FieldInfo fi)
{
var ft = fi.FieldType;
if ((fi.IsStatic && EntityClassData.IsRemoteCallType(ft)) ||
(ft.IsGenericType && !ft.IsArray && ft.GetGenericTypeDefinition() == typeof(SyncVar<>)))
{
byte[] ftName = Encoding.ASCII.GetBytes(ft.Name + (ft.IsGenericType ? ft.GetGenericArguments()[0].Name : string.Empty));
_sha256.TransformBlock(ftName, 0, ftName.Length, null, 0);
string ftName = ft.Name + (ft.IsGenericType ? ft.GetGenericArguments()[0].Name : string.Empty);
for (int i = 0; i < ftName.Length; i++)
{
hash ^= ftName[i];
hash *= 1099511628211UL; //prime
}
}
}
}
Expand Down
14 changes: 4 additions & 10 deletions LiteEntitySystem/ServerEntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,12 @@ public ControllerLogic GetPlayerController(NetPlayer player)
/// <param name="initMethod">Method that will be called after entity construction</param>
/// <typeparam name="T">Entity type</typeparam>
/// <returns>Created entity or null in case of limit</returns>
public T AddController<T>(NetPlayer owner, Action<T> initMethod = null) where T : ControllerLogic
{
var result = Add<T>(ent =>
public T AddController<T>(NetPlayer owner, Action<T> initMethod = null) where T : ControllerLogic =>
Add<T>(ent =>
{
ent.InternalOwnerId = owner.Id;
initMethod?.Invoke(ent);
});
return result;
}

/// <summary>
/// Add new player controller entity and start controlling entityToControl
Expand All @@ -214,16 +211,13 @@ public T AddController<T>(NetPlayer owner, Action<T> initMethod = null) where T
/// <param name="initMethod">Method that will be called after entity construction</param>
/// <typeparam name="T">Entity type</typeparam>
/// <returns>Created entity or null in case of limit</returns>
public T AddController<T>(NetPlayer owner, PawnLogic entityToControl, Action<T> initMethod = null) where T : ControllerLogic
{
var result = Add<T>(ent =>
public T AddController<T>(NetPlayer owner, PawnLogic entityToControl, Action<T> initMethod = null) where T : ControllerLogic =>
Add<T>(ent =>
{
ent.InternalOwnerId = owner.Id;
ent.StartControl(entityToControl);
initMethod?.Invoke(ent);
});
return result;
}

/// <summary>
/// Add new AI controller entity
Expand Down

0 comments on commit 3d80a5a

Please sign in to comment.