From 34e87295d559c2bb061440fce4617e70a6592be3 Mon Sep 17 00:00:00 2001 From: uurha Date: Sat, 24 Feb 2024 09:09:31 +0000 Subject: [PATCH] Merge pull request #53 from techno-dwarf-works/dev Update v1.2.3 --- Runtime/BetterExtensions.Runtime.asmdef | 2 +- Runtime/Extension/ExceptionExtensions.cs | 33 +++++++++ Runtime/Extension/ExceptionExtensions.cs.meta | 11 +++ .../Extension/PlayerLoopSystemExtensions.cs | 25 ++++++- Runtime/Extension/StringBuilderExtensions.cs | 12 ++++ .../Extension/StringBuilderExtensions.cs.meta | 3 + Runtime/Utility/DebugUtility.cs | 72 +++++++++++++++++++ Runtime/Utility/DebugUtility.cs.meta | 11 +++ Runtime/Utility/PlayerLoopUtility.cs | 34 ++++++++- package.json | 2 +- 10 files changed, 198 insertions(+), 7 deletions(-) create mode 100644 Runtime/Extension/ExceptionExtensions.cs create mode 100644 Runtime/Extension/ExceptionExtensions.cs.meta create mode 100644 Runtime/Extension/StringBuilderExtensions.cs create mode 100644 Runtime/Extension/StringBuilderExtensions.cs.meta create mode 100644 Runtime/Utility/DebugUtility.cs create mode 100644 Runtime/Utility/DebugUtility.cs.meta diff --git a/Runtime/BetterExtensions.Runtime.asmdef b/Runtime/BetterExtensions.Runtime.asmdef index 7eee38f..97700cd 100644 --- a/Runtime/BetterExtensions.Runtime.asmdef +++ b/Runtime/BetterExtensions.Runtime.asmdef @@ -1,6 +1,6 @@ { "name": "BetterExtensions.Runtime", - "rootNamespace": "Better.Extensions", + "rootNamespace": "Better.Extensions.Runtime", "references": [], "includePlatforms": [], "excludePlatforms": [], diff --git a/Runtime/Extension/ExceptionExtensions.cs b/Runtime/Extension/ExceptionExtensions.cs new file mode 100644 index 0000000..4fbd731 --- /dev/null +++ b/Runtime/Extension/ExceptionExtensions.cs @@ -0,0 +1,33 @@ +using System; +using System.Reflection; + +namespace Better.Extensions.Runtime +{ + public static class ExceptionExtensions + { + private const string ExceptionMessageFieldName = "_message"; + private static readonly FieldInfo _messageField; + + static ExceptionExtensions() + { + _messageField = GetExceptionMessageField(); + } + + private static FieldInfo GetExceptionMessageField() + { + var type = typeof(Exception); + const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic; + return type.GetField(ExceptionMessageFieldName, flags); + } + + public static void ReplaceExceptionMessageField(this Exception exception, string message) + { + _messageField.SetValue(exception, message); + } + + public static void ReplaceExceptionMessageField(this Exception exception, object message) + { + exception.ReplaceExceptionMessageField(message.ToString()); + } + } +} \ No newline at end of file diff --git a/Runtime/Extension/ExceptionExtensions.cs.meta b/Runtime/Extension/ExceptionExtensions.cs.meta new file mode 100644 index 0000000..997bed1 --- /dev/null +++ b/Runtime/Extension/ExceptionExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 592a891474fdaa0468456ea07c004590 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Extension/PlayerLoopSystemExtensions.cs b/Runtime/Extension/PlayerLoopSystemExtensions.cs index be779fc..a45d364 100644 --- a/Runtime/Extension/PlayerLoopSystemExtensions.cs +++ b/Runtime/Extension/PlayerLoopSystemExtensions.cs @@ -1,8 +1,9 @@ using System; +using System.Collections.Generic; using System.Linq; using UnityEngine.LowLevel; -namespace Better.Extensions +namespace Better.Extensions.Runtime { // TODO: Add summary for possible result self-ref public static class PlayerLoopSystemExtensions @@ -141,7 +142,7 @@ public static bool RemoveSubSystem(this ref PlayerLoopSystem loopSystem, Type su var subSystems = loopSystem.subSystemList.ToList(); for (var i = subSystems.Count - 1; i >= 0; i--) { - if (subSystems[i].type != subSystemType) + if (subSystems[i].type == subSystemType) { subSystems.RemoveAt(i); anyRemoved = true; @@ -254,5 +255,25 @@ public static bool UnsubscribeRecursive(this ref PlayerLoopSystem loopSys var loopType = typeof(TLoop); return loopSystem.UnsubscribeRecursive(loopType, updateFunction); } + + public static Type[] GetTypes(this ref PlayerLoopSystem loopSystem) + { + var loopTypes = new List(); + loopSystem.CollectTypesRecursive(ref loopTypes); + return loopTypes.ToArray(); + } + + private static void CollectTypesRecursive(this ref PlayerLoopSystem loopSystem, ref List loopTypes) + { + loopTypes.Add(loopSystem.type); + + var subSystems = loopSystem.subSystemList; + if (subSystems == null) return; + + for (int i = 0; i < subSystems.Length; i++) + { + subSystems[i].CollectTypesRecursive(ref loopTypes); + } + } } } \ No newline at end of file diff --git a/Runtime/Extension/StringBuilderExtensions.cs b/Runtime/Extension/StringBuilderExtensions.cs new file mode 100644 index 0000000..2b510df --- /dev/null +++ b/Runtime/Extension/StringBuilderExtensions.cs @@ -0,0 +1,12 @@ +using System.Text; + +namespace Better.Extensions.Runtime +{ + public static class StringBuilderExtensions + { + public static StringBuilder AppendLine(this StringBuilder builder, int value) + { + return builder.AppendLine(value.ToString()); + } + } +} \ No newline at end of file diff --git a/Runtime/Extension/StringBuilderExtensions.cs.meta b/Runtime/Extension/StringBuilderExtensions.cs.meta new file mode 100644 index 0000000..3d2b8d2 --- /dev/null +++ b/Runtime/Extension/StringBuilderExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: cb4ce9f19d2b44f7bdedb44ab47b967e +timeCreated: 1708763289 \ No newline at end of file diff --git a/Runtime/Utility/DebugUtility.cs b/Runtime/Utility/DebugUtility.cs new file mode 100644 index 0000000..6747abf --- /dev/null +++ b/Runtime/Utility/DebugUtility.cs @@ -0,0 +1,72 @@ +using System; +using System.Diagnostics; +using Debug = UnityEngine.Debug; +using Object = UnityEngine.Object; + +namespace Better.Extensions.Runtime +{ + public static class DebugUtility + { + [DebuggerHidden] + [DebuggerNonUserCode] + public static void LogException(string message) where T : Exception, new() + { + var exception = new T(); + exception.ReplaceExceptionMessageField(message); + Debug.LogException(exception); + } + + [DebuggerHidden] + [DebuggerNonUserCode] + public static void LogException(object message) where T : Exception, new() + { + LogException(message.ToString()); + } + + [DebuggerHidden] + [DebuggerNonUserCode] + public static void LogException(string message, Object context) where T : Exception, new() + { + var exception = new T(); + exception.ReplaceExceptionMessageField(message); + Debug.LogException(exception, context); + } + + [DebuggerHidden] + [DebuggerNonUserCode] + public static void LogException(object message, Object context) where T : Exception, new() + { + LogException(message.ToString(), context); + } + + [DebuggerHidden] + [DebuggerNonUserCode] + public static void LogException(string message) + { + var exception = new Exception(message); + Debug.LogException(exception); + } + + [DebuggerHidden] + [DebuggerNonUserCode] + public static void LogException(object message) + { + LogException(message.ToString()); + } + + [DebuggerHidden] + [DebuggerNonUserCode] + public static void LogException(string message, Object context) + { + var exception = new Exception(message); + Debug.LogException(exception, context); + } + + [DebuggerHidden] + [DebuggerNonUserCode] + public static void LogException(object message, Object context) + { + LogException(message.ToString(), context); + } + } +} \ No newline at end of file diff --git a/Runtime/Utility/DebugUtility.cs.meta b/Runtime/Utility/DebugUtility.cs.meta new file mode 100644 index 0000000..29045c4 --- /dev/null +++ b/Runtime/Utility/DebugUtility.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0d18cc3116167cf4e8e68a67400d90b5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Utility/PlayerLoopUtility.cs b/Runtime/Utility/PlayerLoopUtility.cs index 9094231..475ddef 100644 --- a/Runtime/Utility/PlayerLoopUtility.cs +++ b/Runtime/Utility/PlayerLoopUtility.cs @@ -1,9 +1,10 @@ using System; using System.Linq; +using System.Text; using UnityEngine; using UnityEngine.LowLevel; -namespace Better.Extensions +namespace Better.Extensions.Runtime { public static class PlayerLoopUtility { @@ -71,7 +72,7 @@ public static void Unsubscribe(PlayerLoopSystem.UpdateFunction updateFunction) public static void AddSubLoop(Type sourceLoopType, Type destinationLoopType, PlayerLoopSystem.UpdateFunction updateFunction) { var currentSystem = PlayerLoop.GetCurrentPlayerLoop(); - ref var sourceSystem = ref currentSystem.GetSubSystem(sourceLoopType); + ref var sourceSystem = ref currentSystem.GetSubSystemRecursive(sourceLoopType); if (sourceSystem.type != sourceLoopType) { var message = $"[{nameof(PlayerLoopUtility)}] {nameof(AddSubLoop)}: not found {nameof(sourceLoopType)}({sourceLoopType.Name})"; @@ -146,7 +147,7 @@ private static void InsertLoopWithOffset(Type sourceLoopType, Type destinationLo ref var parentSystem = ref currentSystem.GetParentSystemRecursiveOf(sourceLoopType); var sourceSubIndex = parentSystem.FindSubSystemIndex(sourceLoopType); - if (sourceSubIndex < 0) + if (sourceSubIndex == -1) { var message = $"[{nameof(PlayerLoopUtility)}] {nameof(InsertLoopWithOffset)}: not found {nameof(sourceLoopType)}({sourceLoopType.Name})"; Debug.LogWarning(message); @@ -205,5 +206,32 @@ public static bool RemoveLoop() } #endregion + + #region Logging + + public static void LogCurrentPlayerLoopTypes(string message, LogType logType = LogType.Log) + { + var currentLoop = PlayerLoop.GetCurrentPlayerLoop(); + var loopTypes = currentLoop.GetTypes(); + + var stringBuilder = new StringBuilder(); + stringBuilder.AppendLine(message); + stringBuilder.Append("Loops count: "); + stringBuilder.AppendLine(loopTypes.Length); + foreach (var loopType in loopTypes) + { + if (loopType == null) + { + stringBuilder.AppendLine("Empty(null)"); + continue; + } + + stringBuilder.AppendLine(loopType.Name); + } + + Debug.unityLogger.Log(logType, stringBuilder); + } + + #endregion } } \ No newline at end of file diff --git a/package.json b/package.json index f5aba92..c793362 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "com.uurha.betterextensions", "displayName": "Better Extensions", "description": "Unity extensions, serialize extension, async extension, string extension and UI extensions", - "version": "1.2.2", + "version": "1.2.3", "unity": "2020.1", "author": { "name": "Better Plugins",