From 03158625274970183b074e7d8f95ed071c5b2383 Mon Sep 17 00:00:00 2001 From: cardidi Date: Fri, 10 May 2024 00:55:42 +0800 Subject: [PATCH] Add some basic features of this toolkit. feat: Logg was almost done. fix: Some impossible call was fixed in UnityObjectWarp.cs feat: Boxing was force to unbox by explicit cast operator. --- Assets/Main/Scripts/AsDisposable.cs | 26 + Assets/Main/Scripts/Boxing.cs | 22 +- Assets/Main/Scripts/ILoggHandler.cs | 43 ++ Assets/Main/Scripts/ILoggHandler.cs.meta | 3 + Assets/Main/Scripts/Log.cs | 497 ------------------ Assets/Main/Scripts/Log.cs.meta | 3 - Assets/Main/Scripts/Logg.cs | 282 ++++++++++ Assets/Main/Scripts/Logg.cs.meta | 3 + Assets/Main/Scripts/LoggHandler.cs | 102 ++++ Assets/Main/Scripts/LoggHandler.cs.meta | 3 + .../Main/Scripts/LoseRequirementException.cs | 3 + Assets/Main/Scripts/Option.cs | 6 + Assets/Main/Scripts/UnityObjectWarp.cs | 18 +- 13 files changed, 491 insertions(+), 520 deletions(-) create mode 100644 Assets/Main/Scripts/ILoggHandler.cs create mode 100644 Assets/Main/Scripts/ILoggHandler.cs.meta delete mode 100644 Assets/Main/Scripts/Log.cs delete mode 100644 Assets/Main/Scripts/Log.cs.meta create mode 100644 Assets/Main/Scripts/Logg.cs create mode 100644 Assets/Main/Scripts/Logg.cs.meta create mode 100644 Assets/Main/Scripts/LoggHandler.cs create mode 100644 Assets/Main/Scripts/LoggHandler.cs.meta diff --git a/Assets/Main/Scripts/AsDisposable.cs b/Assets/Main/Scripts/AsDisposable.cs index 863989f..7387ab9 100644 --- a/Assets/Main/Scripts/AsDisposable.cs +++ b/Assets/Main/Scripts/AsDisposable.cs @@ -7,28 +7,43 @@ namespace Ca2d.Toolkit public static class AsDisposableExtension { + /// + /// Convert an to a IDisposable callback. + /// public static AsDisposable AsDisposable(Action callback) { return new AsDisposable(callback); } + /// + /// Convert an to a IDisposable callback. + /// public static AsAsyncDisposable AsAsyncDisposable(Func callback) { return new AsAsyncDisposable(callback); } + /// + /// Convert an to a IDisposable callback in chain style. + /// public static AsDisposable AsDisposable(this T target, Action callback) { return new AsDisposable(callback, target); } + /// + /// Convert an to a IDisposable callback in chain style. + /// public static AsAsyncDisposable AsAsyncDisposable(this T target, Func callback) { return new AsAsyncDisposable(callback, target); } } + /// + /// An operation container for . + /// public readonly struct AsDisposable : IDisposable { private readonly Action m_callback; @@ -49,6 +64,10 @@ namespace Ca2d.Toolkit } } + /// + /// An operation container for . + /// + /// Object select source. public readonly struct AsDisposable : IDisposable { private readonly Action m_callback; @@ -73,6 +92,9 @@ namespace Ca2d.Toolkit } } + /// + /// An operation container for . + /// public readonly struct AsAsyncDisposable : IAsyncDisposable { private readonly Func m_callback; @@ -94,6 +116,10 @@ namespace Ca2d.Toolkit } } + /// + /// An operation container for . + /// + /// Object select source. public readonly struct AsAsyncDisposable : IAsyncDisposable { private readonly Func m_callback; diff --git a/Assets/Main/Scripts/Boxing.cs b/Assets/Main/Scripts/Boxing.cs index 051b4bb..62fa07a 100644 --- a/Assets/Main/Scripts/Boxing.cs +++ b/Assets/Main/Scripts/Boxing.cs @@ -1,6 +1,5 @@ using System; using UnityEngine; -using UnityEngine.UIElements; namespace Ca2d.Toolkit { @@ -18,7 +17,7 @@ namespace Ca2d.Toolkit /// public Boxing(Boxing source) { - m_value = source.Unbox; + m_value = source.m_value; } /// @@ -30,24 +29,15 @@ namespace Ca2d.Toolkit } /// - /// Create a empty boxing. + /// Create an empty boxing. /// public Boxing() {} /// - /// Get a copy of boxing value. + /// Access to the boxing value directly in reference mode. /// - public T Unbox - { - get => m_value; - set => m_value = value; - } - - /// - /// Access to the boxing value directly. - /// - public ref T Direct => ref m_value; + public ref T Ref => ref m_value; /// /// Clean this boxing container to the default value of boxing target. @@ -62,9 +52,9 @@ namespace Ca2d.Toolkit return m_value.ToString(); } - public static implicit operator T(Boxing wrapper) + public static explicit operator T(Boxing wrapper) { - return wrapper.Unbox; + return wrapper.m_value; } public static implicit operator Boxing(T value) diff --git a/Assets/Main/Scripts/ILoggHandler.cs b/Assets/Main/Scripts/ILoggHandler.cs new file mode 100644 index 0000000..27043ea --- /dev/null +++ b/Assets/Main/Scripts/ILoggHandler.cs @@ -0,0 +1,43 @@ +using UnityEngine; + +namespace Ca2d.Toolkit +{ + public enum LoggType + { + /// + /// LogType used for Errors. + /// + Error, + /// + /// LogType used for Asserts. (These could also indicate an error inside Unity itself.) + /// + Assert, + /// + /// LogType used for Warnings. + /// + Warning, + /// + /// LogType used for regular log messages. + /// + Log, + /// + /// LogType used for Exceptions. + /// + Exception, + /// + /// LogType used for Developers on testing. + /// + Debug + } + + public interface ILoggHandler : ILogHandler + { + public ILogHandler InnerLogHandler { get; } + + public void LoggFormat(LoggType logType, Object context, string format, params object[] args); + + public void SetLabel(string label); + + public void ClearLabel(); + } +} \ No newline at end of file diff --git a/Assets/Main/Scripts/ILoggHandler.cs.meta b/Assets/Main/Scripts/ILoggHandler.cs.meta new file mode 100644 index 0000000..a957eab --- /dev/null +++ b/Assets/Main/Scripts/ILoggHandler.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: bfec301646cc46339921f4c1c9170412 +timeCreated: 1715271270 \ No newline at end of file diff --git a/Assets/Main/Scripts/Log.cs b/Assets/Main/Scripts/Log.cs deleted file mode 100644 index 09d5db0..0000000 --- a/Assets/Main/Scripts/Log.cs +++ /dev/null @@ -1,497 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using UnityEngine; - -namespace Ca2d.Toolkit -{ - public readonly struct Log : IDisposable - { - private const string kNoLabelAndContextName = "Anonymous"; - - private const string kFallbackLabelName = "Fallback"; - - private static HashSet _loopDetector = new(); - - private static StringBuilder _sb = new(); - - #region ManagedRegionOperations - - private static Core _fallbackCore = new Core - { - ValidateNumber = 0, - ParentIndex = 0, - ParentValidateNumber = 0, - Context = null, - Label = kFallbackLabelName - }; - - private static List _core = new() - { - // This is the first element of AdvDebugCore and also as fallback options. - _fallbackCore - }; - - #region Allocation - - private static List m_allocMap = new() - { - // The first element must be allocated. - new Vector2Int(0, 1) - }; - - private static bool RangeOverlap(Vector2Int a, Vector2Int b) - { - var offset = a.x - b.x; - switch (offset) - { - case < 0: - { - var o = -offset; - var lenA = a.y - a.x; - return o < lenA; - } - case > 0: - { - var o = offset; - var lenB = b.y - b.x; - return o < lenB; - } - default: - return true; - } - } - - - private static bool IsIdUsed(int id) - { - return m_allocMap.Exists(range => id >= range.x && id < range.y); - } - - private static int GetUnusedId() - { - var alloc = m_allocMap[0].y; - if (alloc == int.MaxValue) throw new IndexOutOfRangeException("No free index can be exposed!"); - return alloc; - } - - public static bool AllocId(int id) - { - if (id <= 0 || id == int.MaxValue) return false; - var range = new Vector2Int(id, id + 1); - - for (var i = 1; i < m_allocMap.Count; i++) - { - var r = m_allocMap[i]; - var insert = i + 1; - if (m_allocMap.Count != insert && RangeOverlap(r, range)) continue; - - // Get the reaction on merge. - var mergePrev = false; - var mergeNext = false; - - var before = m_allocMap[insert - 1]; - if (before.y == range.x) mergePrev = true; - - var after = m_allocMap.Count > insert ? m_allocMap[insert] : default; - if (after.y != 0 && after.x == range.y) mergeNext = true; - - if (mergePrev & mergeNext) - { - m_allocMap[insert - 1] = new Vector2Int(before.x, after.y); - m_allocMap.RemoveAt(insert); - } - else if (mergePrev) - { - m_allocMap[insert - 1] = new Vector2Int(before.x, range.y); - } - else if (mergeNext) - { - m_allocMap[insert] = new Vector2Int(range.x, after.y); - } - else - { - m_allocMap.Insert(insert, range); - } - - return true; - } - - return false; - } - - public static bool FreeId(int id) - { - if (id <= 0 || id == int.MaxValue) return false; - var range = new Vector2Int(id, id + 1); - var idx = m_allocMap.FindIndex(r => RangeOverlap(r, range)); - if (idx < 0) return false; - - // Get reaction on split - var insertLeft = true; - var insertRight = true; - - var splitTarget = m_allocMap[id]; - if (splitTarget.x == range.x) insertLeft = false; - if (splitTarget.y == range.y) insertRight = false; - - if (insertLeft || insertRight) - { - if (insertLeft) - { - m_allocMap[idx] = new Vector2Int(splitTarget.x, range.x); - } - - if (insertRight) - { - m_allocMap.Insert(idx + 1, new Vector2Int(range.y, splitTarget.y)); - } - } - else - { - m_allocMap.RemoveAt(idx); - } - - return true; - } - - - #endregion - - private struct Capture - { - public readonly ulong CaptureValidateNumber; - - public readonly int ResourceIndex; - - public object Context - { - get - { - if (IsValid()) return _core[ResourceIndex].Context; - return _fallbackCore.Context; - } - } - - public string Label - { - get - { - if (IsValid()) return _core[ResourceIndex].Label; - return _fallbackCore.Label; - } - } - - public bool TryResolveParent(out Capture capture, bool includeFallback = false) - { - if (IsValid()) - { - capture = new Capture(_core[ResourceIndex].ParentIndex); - if (capture.IsFallback()) return includeFallback; - return capture.CaptureValidateNumber == _core[ResourceIndex].ParentValidateNumber; - } - - capture = default; - return includeFallback; - } - - public Capture(int idx) - { - if (idx >= _core.Count || idx <= 0) - { - CaptureValidateNumber = default; - ResourceIndex = default; - return; - } - - ResourceIndex = idx; - CaptureValidateNumber = _core[idx].ValidateNumber; - } - - public bool IsFallback() - { - return ResourceIndex == 0; - } - - public bool IsValid() - { - var resIdx = ResourceIndex; - if (resIdx == 0) return true; - if (resIdx >= _core.Count || resIdx < 0) return false; - return _core[resIdx].ParentValidateNumber == CaptureValidateNumber && IsIdUsed(resIdx); - } - } - - private struct Core - { - public ulong ValidateNumber; - - public ulong ParentValidateNumber; - - public int ParentIndex; - - public object Context; - - public string Label; - } - - private static bool RequestCore(out Capture capture, string label, object context, int parentId) - { - var id = GetUnusedId(); - if (AllocId(id)) - { - // Make sure there has valid space to allocate. - while (_core.Count - 1 < id) _core.Add(default); - - var c = _core[id]; - - // Set context of current Debug. - c.Context = context; - c.Label = label; - - // Check is given parent id is valid and fill result. - c.ParentIndex = parentId != 0 && IsIdUsed(parentId) ? parentId : 0; - c.ParentValidateNumber = c.ParentIndex == 0 ? 0 : _core[c.ParentIndex].ValidateNumber; - - _core[id] = c; - capture = new Capture(id); - return true; - } - - capture = default; - return false; - } - - private static bool ReturnCore(Capture capture) - { - if (capture.IsFallback() || !capture.IsValid()) return false; - if (!FreeId(capture.ResourceIndex)) return false; - - var core = _core[capture.ResourceIndex]; - - core.ValidateNumber += 1; - core.ParentValidateNumber = default; - core.ParentIndex = default; - core.Context = null; - core.Label = kFallbackLabelName; - - _core[capture.ResourceIndex] = core; - return true; - } - - #endregion - - #region ExposeValue - - public bool IsFallback => m_capture.IsFallback(); - - public string Label => m_capture.Label; - - public object Context => m_capture.Context; - - public Log? Parent - { - get - { - if (m_capture.TryResolveParent(out var cap, false)) - return new Log(cap); - - return null; - } - } - - #endregion - - #region Lifecircle - - private readonly Capture m_capture; - - private Log(Capture capture) - { - m_capture = capture; - } - - public Log(Log parent = default) - { - if (!RequestCore(out var capture, kNoLabelAndContextName, null, parent.m_capture.ResourceIndex)) - { - m_capture = default; - return; - } - - m_capture = capture; - } - - public Log(string label, Log parent = default) - { - if (string.IsNullOrWhiteSpace(label)) label = kNoLabelAndContextName; - if (!RequestCore(out var capture, label, null, parent.m_capture.ResourceIndex)) - { - m_capture = default; - return; - } - - m_capture = capture; - } - - public Log(object context, Log parent = default) - { - var label = context == null ? kNoLabelAndContextName : context.GetType().Name; - if (!RequestCore(out var capture, label, null, parent.m_capture.ResourceIndex)) - { - m_capture = default; - return; - } - - m_capture = capture; - } - - public Log(string label, object context, Log parent = default) - { - if (string.IsNullOrWhiteSpace(label)) label = context == null ? kNoLabelAndContextName : context.GetType().Name; - if (!RequestCore(out var capture, label, null, parent.m_capture.ResourceIndex)) - { - m_capture = default; - return; - } - - m_capture = capture; - } - - public void Dispose() - { - if (!FreeId(m_capture.ResourceIndex) || !ReturnCore(m_capture)) throw new InvalidOperationException( - "You can not trying to dispose an outdated or default AdvLogger!"); - } - - #endregion - - public void Debug(string text) - { - } - - public void Info(string text) - {} - - public void Warning(string text) - {} - - public void Error(string text) - {} - - public void Error(Exception err) - {} - } - - public static class QLog - { - - #region (string) - - public static void Debug(string text) - { - default(Log).Debug(text); - } - - public static void Info(string text) - { - default(Log).Info(text); - } - - public static void Warning(string text) - { - default(Log).Warning(text); - } - - public static void Error(string text) - { - default(Log).Error(text); - } - - #endregion - - #region (string, string) - - public static void Debug(string text, string label) - { - using var logger = new Log(label); - logger.Debug(text); - } - - public static void Info(string text, string label) - { - using var logger = new Log(label); - logger.Info(text); - } - - public static void Warning(string text, string label) - { - using var logger = new Log(label); - logger.Warning(text); - } - - public static void Error(string text, string label) - { - using var logger = new Log(label); - logger.Error(text); - } - - #endregion - - #region (string, object) - - public static void Debug(string text, object context) - { - using var logger = new Log(context); - logger.Debug(text); - } - - public static void Info(string text, object context) - { - using var logger = new Log(context); - logger.Info(text); - } - - public static void Warning(string text, object context) - { - using var logger = new Log(context); - logger.Warning(text); - } - - public static void Error(string text, object context) - { - using var logger = new Log(context); - logger.Error(text); - } - - #endregion - - #region (string, string, object) - - public static void Debug(string text, string label, object context) - { - using var logger = new Log(label, context); - logger.Info(text); - } - - public static void Info(string text, string label, object context) - { - using var logger = new Log(label, context); - logger.Info(text); - } - - public static void Warning(string text, string label, object context) - { - using var logger = new Log(label, context); - logger.Warning(text); - } - - public static void Error(string text, string label, object context) - { - using var logger = new Log(label, context); - logger.Error(text); - } - - #endregion - } -} \ No newline at end of file diff --git a/Assets/Main/Scripts/Log.cs.meta b/Assets/Main/Scripts/Log.cs.meta deleted file mode 100644 index 4c8bda9..0000000 --- a/Assets/Main/Scripts/Log.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 3b5d1b48628646eebc9e64c30bd93ae8 -timeCreated: 1713286134 \ No newline at end of file diff --git a/Assets/Main/Scripts/Logg.cs b/Assets/Main/Scripts/Logg.cs new file mode 100644 index 0000000..ea40fb4 --- /dev/null +++ b/Assets/Main/Scripts/Logg.cs @@ -0,0 +1,282 @@ +using System; +using System.Diagnostics; +using System.Text; +using UnityEditor; +using UnityEngine; +using Object = UnityEngine.Object; + +namespace Ca2d.Toolkit +{ + /// + /// Alternatives to the default Unity Logger. + /// + public struct Logg + { + #region BackendConfig + + // ReSharper disable once MemberCanBePrivate.Global + public const string kDefaultLoggerLevelSplit = "::"; + + private static ILoggHandler _backend; + + /// + /// Which backend will be used for logger? + /// + public static ILoggHandler Backend + { + get => _backend; + set => _backend = value ?? throw new NullReferenceException(); + } + + /// + /// Logger level name spliter sign. + /// + public static string LoggerLevelSplit { get; set; } = kDefaultLoggerLevelSplit; + + #endregion + + #region Constructor + + private static readonly StringBuilder kStringBuilder = new(); + + private readonly bool m_useLabel; + + private readonly string m_label; + + /// + /// Create a logger with first-level namespace + /// + /// Namespace + public Logg(string ns) + { + if (string.IsNullOrWhiteSpace(ns)) + { + m_useLabel = false; + m_label = null; + } + else + { + m_useLabel = true; + m_label = ns.Trim(); + } + } + + /// + /// Create a copy of source logger. + /// + /// Source logger. + public Logg(Logg source) + { + if (source.m_useLabel) + { + m_useLabel = true; + m_label = source.m_label; + } + else + { + m_useLabel = false; + m_label = null; + } + } + + /// + /// Create a nested logger with given namespace. + /// + /// Nest source. + /// Namespace + public Logg(Logg source, string ns) + { + if (string.IsNullOrWhiteSpace(ns)) + { + if (source.m_useLabel) + { + m_useLabel = true; + m_label = source.m_label; + } + else + { + m_useLabel = false; + m_label = null; + } + } + else + { + m_useLabel = true; + if (source.m_useLabel) + { + kStringBuilder.Clear(); + m_label = kStringBuilder.AppendJoin(LoggerLevelSplit, source.m_label, ns.Trim()).ToString(); + } + else + { + m_label = ns.Trim(); + } + } + + } + + #endregion + + #region EnvOverrider + + private static bool _init = false; + +#if UNITY_EDITOR + [InitializeOnLoadMethod] + private static void LoggInitEditor() + { + LoggInit(); + } +#else + [RuntimeInitializeOnLoadMethod] + private static void RuntimeLoggInit() + { + LoggInit(); + } +#endif + + private static void LoggInit() + { + if (!_init) + { + var prevLogHandler = UnityEngine.Debug.unityLogger.logHandler; + + if (prevLogHandler is ILoggHandler) + { + prevLogHandler.LogFormat( + LogType.Warning, + null, + "Logg seems to be inited before initialize?"); + } + else + { + var l = new LoggHandler(prevLogHandler); + _backend = l; + UnityEngine.Debug.unityLogger.logHandler = l; + prevLogHandler.LogFormat(LogType.Log, null, "##### Debug.unityLogger was taken over by Logg #####"); + } + + _init = true; + } + } + + #endregion + + #region LogMethods + + [HideInCallstack] + [DebuggerHidden] + [DebuggerStepThrough] + public void Format(LoggType type, Object context, string format, params object[] args) + { + if (_backend != null) + { + if (m_useLabel) _backend.SetLabel(m_label); + _backend.LoggFormat(type, context, format, args); + _backend.ClearLabel(); + } + else + { + // Use fallback logger if Logg is not ready. + UnityEngine.Debug.LogFormat((LogType) type, LogOption.NoStacktrace, context, format, args); + } + } + + [HideInCallstack] + [DebuggerHidden] + [DebuggerStepThrough] + public void Debug(string content, Object context = null) + { + Format(LoggType.Debug, context, content); + } + + [HideInCallstack] + [DebuggerHidden] + [DebuggerStepThrough] + public void Info(string content, Object context = null) + { + Format(LoggType.Log, context, content); + } + + [HideInCallstack] + [DebuggerHidden] + [DebuggerStepThrough] + public void Warning(string content, Object context = null) + { + Format(LoggType.Warning, context, content); + } + + [HideInCallstack] + [DebuggerHidden] + [DebuggerStepThrough] + public void Error(string content, Object context = null) + { + Format(LoggType.Error, context, content); + } + + [HideInCallstack] + [DebuggerHidden] + [DebuggerStepThrough] + public void Error(Exception err, Object context = null) + { + Format(LoggType.Exception, context, err.ToString()); + } + + #endregion + + } + + /// + /// Quick log API for . + /// + public static class DebugLogg + { + [HideInCallstack] + [DebuggerHidden] + [DebuggerStepThrough] + public static void Format(LoggType type, Object context, string format, params object[] args) + { + default(Logg).Format(type, context, format, args); + } + + [HideInCallstack] + [DebuggerHidden] + [DebuggerStepThrough] + public static void Debug(string content, Object context = null) + { + default(Logg).Debug(content, context); + } + + [HideInCallstack] + [DebuggerHidden] + [DebuggerStepThrough] + public static void Info(string content, Object context = null) + { + default(Logg).Info(content, context); + } + + [HideInCallstack] + [DebuggerHidden] + [DebuggerStepThrough] + public static void Warning(string content, Object context = null) + { + default(Logg).Warning(content, context); + } + + [HideInCallstack] + [DebuggerHidden] + [DebuggerStepThrough] + public static void Error(string content, Object context = null) + { + default(Logg).Error(content, context); + } + + [HideInCallstack] + [DebuggerHidden] + [DebuggerStepThrough] + public static void Error(Exception err, Object context = null) + { + default(Logg).Error(err, context); + } + } +} \ No newline at end of file diff --git a/Assets/Main/Scripts/Logg.cs.meta b/Assets/Main/Scripts/Logg.cs.meta new file mode 100644 index 0000000..7bb2ef8 --- /dev/null +++ b/Assets/Main/Scripts/Logg.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 59eb97c7769441599ea2569c2f5402b8 +timeCreated: 1715264700 \ No newline at end of file diff --git a/Assets/Main/Scripts/LoggHandler.cs b/Assets/Main/Scripts/LoggHandler.cs new file mode 100644 index 0000000..add3dbd --- /dev/null +++ b/Assets/Main/Scripts/LoggHandler.cs @@ -0,0 +1,102 @@ +using System; +using System.Diagnostics; +using System.Text; +using UnityEngine; +using Object = UnityEngine.Object; + +namespace Ca2d.Toolkit +{ + public class LoggHandler : ILoggHandler + { + private readonly ILogHandler m_innerHandle; + + private readonly StringBuilder m_sb = new(); + + private bool m_useLabel = false; + + private string m_labelText = default; + + #region PassThrough + + [HideInCallstack] + [DebuggerHidden] + [DebuggerStepThrough] + public void LogFormat(LogType logType, Object context, string format, params object[] args) + { + LoggFormat((LoggType) logType, context, format, args); + } + + [HideInCallstack] + [DebuggerHidden] + [DebuggerStepThrough] + public void LogException(Exception exception, Object context) + { + LoggFormat(LoggType.Exception, context, exception.ToString()); + } + + #endregion + + private void GenerateLabel(StringBuilder sb, LoggType type) + { + var logTypeText = type switch + { + LoggType.Assert => "ASSERT", + LoggType.Exception => "EXCEPTION", + LoggType.Error => "Error", + LoggType.Warning => "Warning", + LoggType.Log => "Info", + _ => "Debug" + }; + + if (m_useLabel) + { + sb.AppendFormat("[{0:yyyy-M-d HH:mm:ss}] {1} ({2}) : ", DateTime.Now, logTypeText, m_labelText); + } + else + { + sb.AppendFormat("[{0:yyyy-M-d HH:mm:ss}] {1} : ", DateTime.Now, logTypeText); + } + } + + [HideInCallstack] + [DebuggerHidden] + [DebuggerStepThrough] + public void LoggFormat(LoggType logType, Object context, string format, params object[] args) + { + m_sb.Clear(); + + // Write label to the front + GenerateLabel(m_sb, logType); + + // Write format text after front + m_sb.AppendFormat(format, args); + + // Redirect Debug to Log. + LogType finalType; + if (logType == LoggType.Debug) finalType = LogType.Log; + else finalType = (LogType) logType; + + // Use inner handle to write. + m_innerHandle.LogFormat(finalType, context, m_sb.ToString()); + } + + public ILogHandler InnerLogHandler => m_innerHandle; + + public void SetLabel(string label) + { + m_labelText = label; + m_useLabel = label != null; + } + + public void ClearLabel() + { + m_labelText = null; + m_useLabel = false; + } + + public LoggHandler(ILogHandler innerHandle) + { + m_innerHandle = innerHandle ?? throw new ArgumentNullException(nameof(innerHandle)); + } + } +} \ No newline at end of file diff --git a/Assets/Main/Scripts/LoggHandler.cs.meta b/Assets/Main/Scripts/LoggHandler.cs.meta new file mode 100644 index 0000000..191f5fc --- /dev/null +++ b/Assets/Main/Scripts/LoggHandler.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f543cfba5cf04258b3ab59b75849e7a9 +timeCreated: 1715271314 \ No newline at end of file diff --git a/Assets/Main/Scripts/LoseRequirementException.cs b/Assets/Main/Scripts/LoseRequirementException.cs index 62bc0a1..b6583cb 100644 --- a/Assets/Main/Scripts/LoseRequirementException.cs +++ b/Assets/Main/Scripts/LoseRequirementException.cs @@ -2,6 +2,9 @@ using System; namespace Ca2d.Toolkit { + /// + /// When Unity Requirements did not fit wants, throw this. + /// public class LoseRequirementException : Exception { public Type RequiredType { get; } diff --git a/Assets/Main/Scripts/Option.cs b/Assets/Main/Scripts/Option.cs index 3a9b3f8..c997502 100644 --- a/Assets/Main/Scripts/Option.cs +++ b/Assets/Main/Scripts/Option.cs @@ -11,8 +11,14 @@ namespace Ca2d.Toolkit [Serializable] public struct Option { + /// + /// Is option set to fill. + /// public bool Enabled; + /// + /// Value inside of option + /// public T Value; public T ValueOrDefault() diff --git a/Assets/Main/Scripts/UnityObjectWarp.cs b/Assets/Main/Scripts/UnityObjectWarp.cs index 038992c..d19c76c 100644 --- a/Assets/Main/Scripts/UnityObjectWarp.cs +++ b/Assets/Main/Scripts/UnityObjectWarp.cs @@ -15,16 +15,26 @@ namespace Ca2d.Toolkit /// /// Is this warp reference to a valid target? /// - public bool Valid => m_referencedObject is T; + public bool Is => m_referencedObject is T; /// /// Trying to get the actual object of this reference. /// - public T Object => m_referencedObject as T; + public T As => m_referencedObject as T; - public static implicit operator T(UnityObjectWarp wrapper) + /// + /// Is this warp reference to a valid target? If this is valid, give the casting result. + /// + public bool IsAs(out T val) { - return wrapper.Object; + if (m_referencedObject is T referenced) + { + val = referenced; + return true; + } + + val = null; + return false; } } } \ No newline at end of file