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

Update v1.2.3 #53

Merged
merged 7 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Assets/BetterExtensions/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());
}
}
}

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

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);
}
}
}
}
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());
}
}
}

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

72 changes: 72 additions & 0 deletions Assets/BetterExtensions/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 Assets/BetterExtensions/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 Assets/BetterExtensions/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 Assets/BetterExtensions/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
Loading