Version 0.1.2

This commit is contained in:
Ca2didi 2025-02-08 19:36:07 +08:00
parent 3e8f62f5cf
commit bc3a8fbfa8
27 changed files with 12 additions and 447 deletions

View File

@ -1,18 +1,19 @@
using System; using System;
using System.Reflection;
namespace Ca2d.Toolkit namespace Ca2d.Toolkit
{ {
public static class AotHelper public static class AotHelper
{ {
private static DateTime alwayasFalseSource = DateTime.UtcNow; private static readonly DateTime AlwaysFalseSource = DateTime.UtcNow;
/// <summary> /// <summary>
/// Utility to provide a must be false option for Ahead-of-time compiler. /// Utility to provide a must be false option for Ahead-of-time compiler.
/// </summary> /// </summary>
/// <returns>False</returns> /// <returns>False</returns>
public static bool AlwaysFalseProvider() public static bool False()
{ {
return alwayasFalseSource.Year < 0; return AlwaysFalseSource.Year < 0;
} }
/// <summary> /// <summary>
@ -20,7 +21,12 @@ namespace Ca2d.Toolkit
/// </summary> /// </summary>
public static void Ensure(Action call) public static void Ensure(Action call)
{ {
if (AlwaysFalseProvider()) call?.Invoke(); if (False()) call?.Invoke();
}
public static void Ensure<T>()
{
if (False()) typeof(T).GetTypeInfo();
} }
} }
} }

View File

@ -1,45 +0,0 @@
#if ENABLE_LOGG
using UnityEngine;
namespace Ca2d.Toolkit
{
public enum LoggType
{
/// <summary>
/// <para>LogType used for Errors.</para>
/// </summary>
Error,
/// <summary>
/// <para>LogType used for Asserts. (These could also indicate an error inside Unity itself.)</para>
/// </summary>
Assert,
/// <summary>
/// <para>LogType used for Warnings.</para>
/// </summary>
Warning,
/// <summary>
/// <para>LogType used for regular log messages.</para>
/// </summary>
Log,
/// <summary>
/// <para>LogType used for Exceptions.</para>
/// </summary>
Exception,
/// <summary>
/// <para>LogType used for Developers on testing.</para>
/// </summary>
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();
}
}
#endif

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: bfec301646cc46339921f4c1c9170412
timeCreated: 1715271270

View File

@ -1,284 +0,0 @@
#if ENABLE_LOGG
using System;
using System.Diagnostics;
using System.Text;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Ca2d.Toolkit
{
/// <summary>
/// Alternatives to the default Unity Logger.
/// </summary>
public struct Logg
{
#region BackendConfig
// ReSharper disable once MemberCanBePrivate.Global
public const string kDefaultLoggerLevelSplit = "::";
private static ILoggHandler _backend;
/// <summary>
/// Which backend will be used for logger?
/// </summary>
public static ILoggHandler Backend
{
get => _backend;
set => _backend = value ?? throw new NullReferenceException();
}
/// <summary>
/// Logger level name spliter sign.
/// </summary>
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;
/// <summary>
/// Create a logger with first-level namespace
/// </summary>
/// <param name="ns">Namespace</param>
public Logg(string ns)
{
if (string.IsNullOrWhiteSpace(ns))
{
m_useLabel = false;
m_label = null;
}
else
{
m_useLabel = true;
m_label = ns.Trim();
}
}
/// <summary>
/// Create a copy of source logger.
/// </summary>
/// <param name="source">Source logger.</param>
public Logg(Logg source)
{
if (source.m_useLabel)
{
m_useLabel = true;
m_label = source.m_label;
}
else
{
m_useLabel = false;
m_label = null;
}
}
/// <summary>
/// Create a nested logger with given namespace.
/// </summary>
/// <param name="source">Nest source.</param>
/// <param name="ns">Namespace</param>
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
}
/// <summary>
/// Quick log API for <see cref="Logg"/>.
/// </summary>
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);
}
}
}
#endif

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 59eb97c7769441599ea2569c2f5402b8
timeCreated: 1715264700

View File

@ -1,104 +0,0 @@
#if ENABLE_LOGG
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));
}
}
}
#endif

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: f543cfba5cf04258b3ab59b75849e7a9
timeCreated: 1715271314

View File

@ -1,6 +1,6 @@
{ {
"name": "xyz.ca2didi.unity.toolkit", "name": "xyz.ca2didi.unity.toolkit",
"version": "0.1.1", "version": "0.1.2",
"displayName": "Ca2D Toolkit", "displayName": "Ca2D Toolkit",
"description": "Set of toolkits which can boost your develop experience and speed in Unity.", "description": "Set of toolkits which can boost your develop experience and speed in Unity.",
"unity": "2021.3", "unity": "2021.3",

View File

@ -5,6 +5,7 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Cpackage_005Ctoolkit/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Cpackage_005Ctoolkit/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Cpackage_005Ctoolkit_005Cscripts/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Cpackage_005Ctoolkit_005Cscripts/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Ctoolkit_005Ctoolkit_005Ceditor_005Cscripts/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Ctoolkit_005Ctoolkit_005Ceditor_005Cscripts/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Ctoolkit_005Ctoolkit_005Cruntime/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Ctoolkit_005Ctoolkit_005Cscripts/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Ctoolkit_005Ctoolkit_005Cscripts/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Ctoolkit_005Ctoolkit/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Ctoolkit_005Ctoolkit/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Ctoolkit/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> <s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=assets_005Ctoolkit/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>