// 27 Slicer // Copyright 2021 Deftly Games // https://slicer.deftly.games/ using System; using UnityEngine; namespace Slicer.Core { /// /// Contains useful methods for working with UnityEngine.Hash128. /// public static class HashUtility { /// /// Calculates the hash of an UnityEngine.Object. /// /// /// UnityEngine.Object.GetInstanceID() is used to calculate the hash. /// /// The object to calculate the hash of. /// The calculated hash. public static Hash128 CalculateHash(UnityEngine.Object o) { var i = o.GetInstanceID(); var h = CalculateHash(i); return h; } /// /// Calculates the hash of a struct. /// /// the type of object to hash, must be of type struct. /// The struct to hash. /// The calculated hash. public static Hash128 CalculateHash(T s) where T : struct { Hash128 h = new Hash128(); HashUtilities.ComputeHash128(ref s, ref h); return h; } /// /// Calculates the hash of a bool. /// /// The bool to hash. /// How many bits to shift the bool by after converting it to a UInt32 /// The calculated hash. public static Hash128 CalculateHash(bool b, int shift) { var i = Convert.ToUInt32(b); i = i << shift; var h = CalculateHash(i); return h; } /// /// Calculates the hash of a Vector3. /// /// The Vector3 to hash. /// The calculated hash. public static Hash128 CalculateHash(Vector3 v) { Hash128 h = new Hash128(); HashUtilities.QuantisedVectorHash(ref v, ref h); return h; } /// /// Calculates the hash of a Matrix4x4. /// /// The Matrix4x4 to hash. /// The calculated hash. public static Hash128 CalculateHash(Matrix4x4 m) { Hash128 h = new Hash128(); HashUtilities.QuantisedMatrixHash(ref m, ref h); return h; } /// /// Calculates the hash of a Bounds. /// /// The Bounds to hash. /// The calculated hash. public static Hash128 CalculateHash(Bounds b) { var c = CalculateHash(b.center); var e = CalculateHash(b.extents); AppendHash(c, ref e); return e; } /// /// Appends two hashes. /// /// Hash to append with. /// The hash that will be updated. public static void AppendHash(Hash128 inHash, ref Hash128 outHash) { HashUtilities.AppendHash(ref inHash, ref outHash); } /// /// Appends three hashes. /// /// Hash 1 to append with. /// Hash 2 to append with. /// The hash that will be updated. public static void AppendHash(Hash128 inHash1, Hash128 inHash2, ref Hash128 outHash) { HashUtilities.AppendHash(ref inHash1, ref inHash2); HashUtilities.AppendHash(ref inHash2, ref outHash); } /// /// Appends four hashes. /// /// Hash 1 to append with. /// Hash 2 to append with. /// Hash 3 to append with. /// The hash that will be updated. public static void AppendHash(Hash128 inHash1, Hash128 inHash2, Hash128 inHash3, ref Hash128 outHash) { HashUtilities.AppendHash(ref inHash1, ref inHash2); HashUtilities.AppendHash(ref inHash2, ref inHash3); HashUtilities.AppendHash(ref inHash3, ref outHash); } /// /// Appends five hashes. /// /// Hash 1 to append with. /// Hash 2 to append with. /// Hash 3 to append with. /// Hash 4 to append with. /// The hash that will be updated. public static void AppendHash(Hash128 inHash1, Hash128 inHash2, Hash128 inHash3, Hash128 inHash4, ref Hash128 outHash) { HashUtilities.AppendHash(ref inHash1, ref inHash2); HashUtilities.AppendHash(ref inHash2, ref inHash3); HashUtilities.AppendHash(ref inHash3, ref inHash4); HashUtilities.AppendHash(ref inHash4, ref outHash); } /// /// Appends six hashes. /// /// Hash 1 to append with. /// Hash 2 to append with. /// Hash 3 to append with. /// Hash 4 to append with. /// Hash 5 to append with. /// The hash that will be updated. public static void AppendHash(Hash128 inHash1, Hash128 inHash2, Hash128 inHash3, Hash128 inHash4, Hash128 inHash5, ref Hash128 outHash) { HashUtilities.AppendHash(ref inHash1, ref inHash2); HashUtilities.AppendHash(ref inHash2, ref inHash3); HashUtilities.AppendHash(ref inHash3, ref inHash4); HashUtilities.AppendHash(ref inHash4, ref inHash5); HashUtilities.AppendHash(ref inHash5, ref outHash); } /// /// Appends seven hashes. /// /// Hash 1 to append with. /// Hash 2 to append with. /// Hash 3 to append with. /// Hash 4 to append with. /// Hash 5 to append with. /// Hash 6 to append with. /// The hash that will be updated. public static void AppendHash(Hash128 inHash1, Hash128 inHash2, Hash128 inHash3, Hash128 inHash4, Hash128 inHash5, Hash128 inHash6, ref Hash128 outHash) { HashUtilities.AppendHash(ref inHash1, ref inHash2); HashUtilities.AppendHash(ref inHash2, ref inHash3); HashUtilities.AppendHash(ref inHash3, ref inHash4); HashUtilities.AppendHash(ref inHash4, ref inHash5); HashUtilities.AppendHash(ref inHash5, ref inHash6); HashUtilities.AppendHash(ref inHash6, ref outHash); } } }