Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #53 from techno-dwarf-works/dev
Browse files Browse the repository at this point in the history
Update v1.2.3
  • Loading branch information
uurha committed Feb 24, 2024
1 parent af3caa3 commit 34e8729
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Runtime/BetterExtensions.Runtime.asmdef
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "BetterExtensions.Runtime",
"rootNamespace": "Better.Extensions",
"rootNamespace": "Better.Extensions.Runtime",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
33 changes: 33 additions & 0 deletions Runtime/Extension/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Extension/ExceptionExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 23 additions & 2 deletions Runtime/Extension/PlayerLoopSystemExtensions.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -254,5 +255,25 @@ public static bool UnsubscribeRecursive<TLoop>(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<Type>();
loopSystem.CollectTypesRecursive(ref loopTypes);
return loopTypes.ToArray();
}

private static void CollectTypesRecursive(this ref PlayerLoopSystem loopSystem, ref List<Type> 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);
}
}
}
}
12 changes: 12 additions & 0 deletions Runtime/Extension/StringBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Extension/StringBuilderExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions Runtime/Utility/DebugUtility.cs
Original file line number Diff line number Diff line change
@@ -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<T>(string message) where T : Exception, new()
{
var exception = new T();
exception.ReplaceExceptionMessageField(message);
Debug.LogException(exception);
}

[DebuggerHidden]
[DebuggerNonUserCode]
public static void LogException<T>(object message) where T : Exception, new()
{
LogException<T>(message.ToString());
}

[DebuggerHidden]
[DebuggerNonUserCode]
public static void LogException<T>(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<T>(object message, Object context) where T : Exception, new()
{
LogException<T>(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);
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Utility/DebugUtility.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 31 additions & 3 deletions Runtime/Utility/PlayerLoopUtility.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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})";
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -205,5 +206,32 @@ public static bool RemoveLoop<TLoop>()
}

#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
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 34e8729

Please sign in to comment.