diff --git a/Assets/Scripts/ServerShared/Extensions.cs b/Assets/Scripts/ServerShared/Extensions.cs index 6ac7b5186..4f4c3fb1d 100644 --- a/Assets/Scripts/ServerShared/Extensions.cs +++ b/Assets/Scripts/ServerShared/Extensions.cs @@ -11,7 +11,6 @@ using MessagePack; using CultMath; using static CultMath.math; using Random = CultMath.Random; -using Unity.Tiny; using float2 = CultMath.float2; public static class Extensions @@ -212,125 +211,4 @@ public static class Extensions var num = sqrt(lengthsq(from) * lengthsq(to)); return num < 1.00000000362749E-15 ? 0.0f : acos(clamp(dot(from, to) / num, -1f, 1f)) * 57.29578f; } -} - -// https://github.com/Burtsev-Alexey/net-object-deep-copy -public static class ObjectExtensions -{ - private static readonly MethodInfo CloneMethod = typeof(Object).GetMethod("MemberwiseClone", BindingFlags.NonPublic | BindingFlags.Instance); - - public static bool IsPrimitive(this Type type) - { - if (type == typeof(String)) return true; - return (type.IsValueType & type.IsPrimitive); - } - - public static Object Copy(this Object originalObject) - { - return InternalCopy(originalObject, new Dictionary(new ReferenceEqualityComparer())); - } - private static Object InternalCopy(Object originalObject, IDictionary visited) - { - if (originalObject == null) return null; - var typeToReflect = originalObject.GetType(); - if (IsPrimitive(typeToReflect)) return originalObject; - if (visited.ContainsKey(originalObject)) return visited[originalObject]; - if (typeof(Delegate).IsAssignableFrom(typeToReflect)) return null; - var cloneObject = CloneMethod.Invoke(originalObject, null); - if (typeToReflect.IsArray) - { - var arrayType = typeToReflect.GetElementType(); - if (IsPrimitive(arrayType) == false) - { - Array clonedArray = (Array)cloneObject; - clonedArray.ForEach((array, indices) => array.SetValue(InternalCopy(clonedArray.GetValue(indices), visited), indices)); - } - - } - visited.Add(originalObject, cloneObject); - CopyFields(originalObject, visited, cloneObject, typeToReflect); - RecursiveCopyBaseTypePrivateFields(originalObject, visited, cloneObject, typeToReflect); - return cloneObject; - } - - private static void RecursiveCopyBaseTypePrivateFields(object originalObject, IDictionary visited, object cloneObject, Type typeToReflect) - { - if (typeToReflect.BaseType != null) - { - RecursiveCopyBaseTypePrivateFields(originalObject, visited, cloneObject, typeToReflect.BaseType); - CopyFields(originalObject, visited, cloneObject, typeToReflect.BaseType, BindingFlags.Instance | BindingFlags.NonPublic, info => info.IsPrivate); - } - } - - private static void CopyFields(object originalObject, IDictionary visited, object cloneObject, Type typeToReflect, BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy, Func filter = null) - { - foreach (FieldInfo fieldInfo in typeToReflect.GetFields(bindingFlags)) - { - if (filter != null && filter(fieldInfo) == false) continue; - if (IsPrimitive(fieldInfo.FieldType)) continue; - var originalFieldValue = fieldInfo.GetValue(originalObject); - var clonedFieldValue = InternalCopy(originalFieldValue, visited); - fieldInfo.SetValue(cloneObject, clonedFieldValue); - } - } - public static T Copy(this T original) - { - return (T)Copy((Object)original); - } -} - -public class ReferenceEqualityComparer : EqualityComparer -{ - public override bool Equals(object x, object y) - { - return ReferenceEquals(x, y); - } - public override int GetHashCode(object obj) - { - if (obj == null) return 0; - return obj.GetHashCode(); - } -} -public static class ArrayExtensions -{ - public static void ForEach(this Array array, Action action) - { - if (array.LongLength == 0) return; - ArrayTraverse walker = new ArrayTraverse(array); - do action(array, walker.Position); - while (walker.Step()); - } -} - -internal class ArrayTraverse -{ - public int[] Position; - private int[] maxLengths; - - public ArrayTraverse(Array array) - { - maxLengths = new int[array.Rank]; - for (int i = 0; i < array.Rank; ++i) - { - maxLengths[i] = array.GetLength(i) - 1; - } - Position = new int[array.Rank]; - } - - public bool Step() - { - for (int i = 0; i < Position.Length; ++i) - { - if (Position[i] < maxLengths[i]) - { - Position[i]++; - for (int j = 0; j < i; j++) - { - Position[j] = 0; - } - return true; - } - } - return false; - } } \ No newline at end of file diff --git a/Assets/Scripts/ServerShared/Rect.cs b/Assets/Scripts/ServerShared/Rect.cs deleted file mode 100644 index 6e660a309..000000000 --- a/Assets/Scripts/ServerShared/Rect.cs +++ /dev/null @@ -1,102 +0,0 @@ -using CultMath; -using static CultMath.math; -using float2 = CultMath.float2; - -namespace Unity.Tiny -{ - /// - /// 2D rectangle - /// - public struct Rect - { - /// - /// Default unit rectangle. - /// - public static Rect Default { get; } = new Rect() - { - position = float2.zero, - size = float2(1,1) - }; - - public Rect(float x, float y, float width, float height) - { - position = float2(x, y); - size = float2(width, height); - } - - public Rect(float2 position, float2 size) - { - this.position = position; - this.size = size; - } - - public bool IsEmpty() - { - return !(size.x > 0.0f && size.y > 0.0f); - } - - /// - /// Returns true if is inside this rectangle. - /// - /// - /// The left and bottom edges are inclusive, while the right and top edges - /// are exclusive. Also, while this class doesn't forbid 0 or negative - /// width / heights, Contains() will be always be false in that case. - /// - public bool Contains(float2 pos) - { - return pos.x >= position.x && pos.y >= position.y && pos.x < position.x + size.x && pos.y < position.y + size.y; - } - - /// - /// Returns a new rectangle translated and scaled by . - /// - public Rect Region(in Rect relative) - { - return new Rect - { - position = float2(position.x + relative.position.x * size.x,position.y + relative.position.y * size.y) , - size = float2(size.x * relative.size.x, size.y * relative.size.y) - }; - } - - public bool IntersectsWith(Rect r2) - { - return r2.position.x + r2.size.x >= position.x && r2.position.x <= position.x + size.x && - r2.position.y + r2.size.y >= position.y && r2.position.y <= position.y + size.y; - } - - public void Clamp(Rect r) - { - float x2 = position.x + size.x; - float y2 = position.y + size.y; - float rx2 = r.position.x + r.size.x; - float ry2 = r.position.y + r.size.y; - if (position.x < r.position.x) position.x = r.position.x; - if (x2 > rx2) x2 = rx2; - if (position.y < r.position.y) position.y = r.position.y; - if (y2 > ry2) y2 = ry2; - size.x = x2 - position.x; - if (size.x < 0.0f) size.x = 0.0f; - size.y = y2 - position.y; - if (size.y < 0.0f) size.y = 0.0f; - } - - public float2 position; - public float2 size; - - /// - /// The position of the center of the rectangle. - /// - public float2 Center - { - get => new float2(position.x + size.x / 2f, position.y + size.y/ 2f); - set - { - position.x = value.x - size.x / 2f; - position.y = value.y - size.y / 2f; - } - } - - } -} diff --git a/Assets/Scripts/ServerShared/Rect.cs.meta b/Assets/Scripts/ServerShared/Rect.cs.meta deleted file mode 100644 index 92e95a713..000000000 --- a/Assets/Scripts/ServerShared/Rect.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9ce4de4d27275c5498970b04f0b61fd5 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: