From e8ff87871a498f574671e731e98bd2fa2db5f26a Mon Sep 17 00:00:00 2001 From: sebas77 Date: Mon, 30 Oct 2017 11:19:59 +0000 Subject: [PATCH] Make the framework more unity agnostic --- Context/Factories/GameObjectFactory.cs | 2 + Context/Factories/MonoBehaviourFactory.cs | 2 + Context/ICompositionRoot.cs | 4 +- Context/UnityContext.cs | 2 + DataStructures/FasterList.cs | 5 +-- DataStructures/ThreadSafeDictionary.cs | 3 +- DataStructures/WeakReference.cs | 35 ++++++++++----- ECS/EntityDescriptor.cs | 8 ++-- Utilities/DesignByContract.cs | 2 +- Utilities/Print.cs | 53 ++++++++++++++++++----- 10 files changed, 83 insertions(+), 33 deletions(-) diff --git a/Context/Factories/GameObjectFactory.cs b/Context/Factories/GameObjectFactory.cs index a8cde2b..ecd3350 100644 --- a/Context/Factories/GameObjectFactory.cs +++ b/Context/Factories/GameObjectFactory.cs @@ -1,3 +1,4 @@ +#if UNITY_5 || UNITY_5_3_OR_NEWER using System.Collections.Generic; using UnityEngine; @@ -61,3 +62,4 @@ namespace Svelto.Context Dictionary _prefabs; } } +#endif \ No newline at end of file diff --git a/Context/Factories/MonoBehaviourFactory.cs b/Context/Factories/MonoBehaviourFactory.cs index 94786dd..7f862ab 100644 --- a/Context/Factories/MonoBehaviourFactory.cs +++ b/Context/Factories/MonoBehaviourFactory.cs @@ -1,3 +1,4 @@ +#if UNITY_5 || UNITY_5_3_OR_NEWER #region using System; @@ -18,3 +19,4 @@ namespace Svelto.Context } } } +#endif \ No newline at end of file diff --git a/Context/ICompositionRoot.cs b/Context/ICompositionRoot.cs index bb2af15..88acda0 100644 --- a/Context/ICompositionRoot.cs +++ b/Context/ICompositionRoot.cs @@ -1,3 +1,4 @@ +#if UNITY_5 || UNITY_5_3_OR_NEWER namespace Svelto.Context { public interface ICompositionRoot @@ -7,5 +8,4 @@ namespace Svelto.Context void OnContextDestroyed(); } } - - +#endif \ No newline at end of file diff --git a/Context/UnityContext.cs b/Context/UnityContext.cs index 70782f4..9591861 100644 --- a/Context/UnityContext.cs +++ b/Context/UnityContext.cs @@ -1,3 +1,4 @@ +#if UNITY_5 || UNITY_5_3_OR_NEWER using System.Collections; using Svelto.Context; using UnityEngine; @@ -42,3 +43,4 @@ public class UnityContext: UnityContext where T:class, ICompositionRoot, new( T _applicationRoot; } +#endif \ No newline at end of file diff --git a/DataStructures/FasterList.cs b/DataStructures/FasterList.cs index 6828af0..d87c13b 100644 --- a/DataStructures/FasterList.cs +++ b/DataStructures/FasterList.cs @@ -2,7 +2,6 @@ using System; using System.Collections; using System.Collections.Generic; using System.Threading; -using UnityEngine; namespace Svelto.DataStructures { @@ -766,14 +765,14 @@ namespace Svelto.DataStructures void AllocateMore() { - var newList = new T[Mathf.Max(_buffer.Length << 1, MIN_SIZE)]; + var newList = new T[Math.Max(_buffer.Length << 1, MIN_SIZE)]; if (_count > 0) _buffer.CopyTo(newList, 0); _buffer = newList; } void AllocateMore(int newSize) { - var oldLength = Mathf.Max(_buffer.Length, MIN_SIZE); + var oldLength = Math.Max(_buffer.Length, MIN_SIZE); while (oldLength < newSize) oldLength <<= 1; diff --git a/DataStructures/ThreadSafeDictionary.cs b/DataStructures/ThreadSafeDictionary.cs index 19e72e4..4a4a71f 100644 --- a/DataStructures/ThreadSafeDictionary.cs +++ b/DataStructures/ThreadSafeDictionary.cs @@ -1,5 +1,4 @@ -using System; using System.Collections.Generic; using System.Threading; @@ -11,7 +10,7 @@ namespace Svelto.DataStructures /// /// /// - [Serializable] + public class ThreadSafeDictionary { public ThreadSafeDictionary(int v) diff --git a/DataStructures/WeakReference.cs b/DataStructures/WeakReference.cs index a3310ec..0f14cca 100644 --- a/DataStructures/WeakReference.cs +++ b/DataStructures/WeakReference.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.Serialization; /// /// Represents a weak reference, which references an object while still allowing /// that object to be reclaimed by garbage collection. @@ -8,7 +7,7 @@ using System.Runtime.Serialization; namespace Svelto.DataStructures { - [Serializable] +#if !NETFX_CORE public class WeakReference : WeakReference where T : class { @@ -50,15 +49,31 @@ namespace Svelto.DataStructures public WeakReference(T target, bool trackResurrection) : base(target, trackResurrection) { } -#if !NETFX_CORE - protected WeakReference(SerializationInfo info, StreamingContext context) - : base(info, context) - { } -#endif } - - public static class WeakReferenceUtility +#else + public class WeakReference : System.WeakReference where T : class { - public static bool IsValid(this WeakReference obj) { return obj != null && obj.IsAlive == true && obj.Target != null; } + public bool IsValid { get { return Target != null && IsAlive == true; } } + + public new T Target + { + get + { + return (T)base.Target; + } + set + { + base.Target = value; + } + } + + public WeakReference(T target) + : base(target) + { } + + public WeakReference(T target, bool trackResurrection) + : base(target, trackResurrection) + { } } +#endif } diff --git a/ECS/EntityDescriptor.cs b/ECS/EntityDescriptor.cs index b82e01c..fc9b030 100644 --- a/ECS/EntityDescriptor.cs +++ b/ECS/EntityDescriptor.cs @@ -2,9 +2,6 @@ using System; using System.Collections.Generic; using System.Reflection; using Svelto.DataStructures; -#if NETFX_CORE -using BindingFlags = System.Reflection.BindingFlags; -#endif namespace Svelto.ECS { @@ -33,8 +30,11 @@ namespace Svelto.ECS { var implementor = implementors[index]; if (implementor == null) + { Utility.Console.LogWarning( - "Null implementor, are you using a wild GetComponents to fetch it? ").FastConcat(ToString())); + "Null implementor, are you using a wild GetComponents to fetch it? " + .FastConcat(ToString())); + } else { if (implementor is IRemoveEntityComponent) diff --git a/Utilities/DesignByContract.cs b/Utilities/DesignByContract.cs index 0ac9bf8..b6a22d7 100644 --- a/Utilities/DesignByContract.cs +++ b/Utilities/DesignByContract.cs @@ -371,7 +371,7 @@ namespace DesignByContract /// any DesignByContract exception and other runtime exceptions. /// /// - public class DesignByContractException : ApplicationException + public class DesignByContractException : Exception { protected DesignByContractException() {} protected DesignByContractException(string message) : base(message) {} diff --git a/Utilities/Print.cs b/Utilities/Print.cs index 095c110..07e21f4 100644 --- a/Utilities/Print.cs +++ b/Utilities/Print.cs @@ -1,7 +1,6 @@ using System; using System.Diagnostics; using System.Text; -using UnityEngine; public static class FastConcatUtility { @@ -101,8 +100,8 @@ namespace Utility { void Log (string txt, string stack = null, LogType type = LogType.Log); } - - public class SlowLogger : ILogger +#if UNITY_5 || UNITY_5_3_OR_NEWER + public class SlowLoggerUnity : ILogger { public void Log(string txt, string stack = null, LogType type = LogType.Log) { @@ -123,12 +122,45 @@ namespace Utility } } } +#endif + public class SimpleLogger : ILogger + { + public void Log(string txt, string stack = null, LogType type = LogType.Log) + { + switch (type) + { + case LogType.Log: + Console.SystemLog(stack != null ? txt.FastConcat(stack) : txt); + break; + case LogType.Exception: + Console.SystemLog("Log of exceptions not supported"); + break; + case LogType.Warning: + Console.SystemLog(stack != null ? txt.FastConcat(stack) : txt); + break; + case LogType.Error: + Console.SystemLog(stack != null ? txt.FastConcat(stack) : txt); + break; + } + } + } + + public enum LogType + { + Log, + Exception, + Warning, + Error + } public static class Console { static StringBuilder _stringBuilder = new StringBuilder(256); - - public static ILogger logger = new SlowLogger(); +#if UNITY_5 || UNITY_5_3_OR_NEWER + public static ILogger logger = new SlowLoggerUnity(); +#else + public static ILogger logger = new SimpleLogger(); +#endif public static volatile bool BatchLog = false; public static void Log(string txt) @@ -167,17 +199,18 @@ namespace Utility logger.Log(toPrint, stack, LogType.Error); } - public static void LogException(Exception e) { +#if UNITY_5 || UNITY_5_3_OR_NEWER LogException(e, null); +#endif } - +#if UNITY_5 || UNITY_5_3_OR_NEWER public static void LogException(Exception e, UnityEngine.Object obj) { UnityEngine.Debug.LogException(e, obj); } - +#endif public static void LogWarning(string txt) { string toPrint; @@ -217,13 +250,11 @@ namespace Utility toPrint = _stringBuilder.ToString(); } -#if !UNITY_EDITOR +#if !UNITY_EDITOR System.Console.WriteLine(toPrint); #else UnityEngine.Debug.Log(toPrint); #endif -#else - UnityEngine.Debug.Log(txt); #endif } }