From 622f9afe070d80d83801dc0d113fcdfad9a8bf0f Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Fri, 17 Nov 2023 12:56:59 -0800 Subject: [PATCH 01/27] Add Transform.FindDeepChild extension --- JotunnLib/Extensions/TransformExtensions.cs | 58 +++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 JotunnLib/Extensions/TransformExtensions.cs diff --git a/JotunnLib/Extensions/TransformExtensions.cs b/JotunnLib/Extensions/TransformExtensions.cs new file mode 100644 index 000000000..0831a4e5a --- /dev/null +++ b/JotunnLib/Extensions/TransformExtensions.cs @@ -0,0 +1,58 @@ +using System.Collections.Generic; +using UnityEngine; + +namespace Jotunn.Extensions +{ + internal static class TransformExtensions + { + /// + /// Extension method to find nested children by name using either + /// a breadth-first or depth-first search. Default is breadth-first. + /// + /// + /// + /// + public static Transform FindDeepChild( + this Transform transform, + string childName, + bool breadthFirst = true + ) + { + if (breadthFirst) + { + var queue = new Queue(); + queue.Enqueue(transform); + while (queue.Count > 0) + { + var child = queue.Dequeue(); + if (child.name == childName) + { + return child; + } + + foreach (Transform t in child) + { + queue.Enqueue(t); + } + } + return null; + } + else + { + foreach (Transform child in transform) + { + if (child.name == childName) + { + return child; + } + var result = child.FindDeepChild(childName); + if (result != null) + { + return result; + } + } + return null; + } + } + } +} From 923b82f652b6d240d3b590622db07953052e4912 Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:02:11 -0800 Subject: [PATCH 02/27] Add string extensions for handling substrings, prefixes, and suffixes --- JotunnLib/Extensions/StringExtensions.cs | 92 ++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 JotunnLib/Extensions/StringExtensions.cs diff --git a/JotunnLib/Extensions/StringExtensions.cs b/JotunnLib/Extensions/StringExtensions.cs new file mode 100644 index 000000000..24adac6a0 --- /dev/null +++ b/JotunnLib/Extensions/StringExtensions.cs @@ -0,0 +1,92 @@ +namespace Jotunn.Extensions +{ + internal static class StringExtensions + { + /// + /// Returns true if the string contains any of the substrings. + /// + /// + /// + /// + internal static bool ContainsAny(this string str, params string[] substrings) + { + foreach (var substring in substrings) + { + if (str.Contains(substring)) + { + return true; + } + } + return false; + } + + /// + /// Returns true if the string ends with any one of the suffixes. + /// + /// + /// + /// + internal static bool EndsWithAny(this string str, params string[] suffixes) + { + foreach (var substring in suffixes) + { + if (str.EndsWith(substring)) + { + return true; + } + } + return false; + } + + /// + /// Returns true if the string starts with any one of the prefixes. + /// + /// + /// + /// + internal static bool StartsWithAny(this string str, params string[] prefixes) + { + foreach (var substring in prefixes) + { + if (str.StartsWith(substring)) + { + return true; + } + } + return false; + } + + /// + /// If the string ends with the suffix then return a copy of the string + /// with the suffix stripped, otherwise return the original string. + /// + /// + /// + /// + internal static string RemoveSuffix(this string s, string suffix) + { + if (s.EndsWith(suffix)) + { + return s.Substring(0, s.Length - suffix.Length); + } + + return s; + } + + /// + /// If the string starts with the prefix then return a copy of the string + /// with the prefix stripped, otherwise return the original string. + /// + /// + /// + /// + internal static string RemovePrefix(this string s, string prefix) + { + if (s.StartsWith(prefix)) + { + return s.Substring(prefix.Length, s.Length - prefix.Length); + } + return s; + } + } +} From 5115e846c40caa58cf981d908035eb5a12c2a0b4 Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:02:57 -0800 Subject: [PATCH 03/27] Add string extension to help with formatting piece names to match Vanilla formatting --- JotunnLib/Extensions/StringExtensions.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/JotunnLib/Extensions/StringExtensions.cs b/JotunnLib/Extensions/StringExtensions.cs index 24adac6a0..01a52f016 100644 --- a/JotunnLib/Extensions/StringExtensions.cs +++ b/JotunnLib/Extensions/StringExtensions.cs @@ -88,5 +88,19 @@ internal static string RemovePrefix(this string s, string prefix) } return s; } + + /// + /// Returns a copy of the string with the first character capitalized + /// + /// + /// + internal static string CapitalizeFirstLetter(this string s) + { + if (s.Length == 0) + return s; + else if (s.Length == 1) + return $"{char.ToUpper(s[0])}"; + else + return char.ToUpper(s[0]) + s.Substring(1); } } From feacd8593580fa1887527c73ac844917daea72df Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:03:40 -0800 Subject: [PATCH 04/27] Add extension to convert a null string to an empty string to prevent type error if needed --- JotunnLib/Extensions/StringExtensions.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/JotunnLib/Extensions/StringExtensions.cs b/JotunnLib/Extensions/StringExtensions.cs index 01a52f016..7af2fe868 100644 --- a/JotunnLib/Extensions/StringExtensions.cs +++ b/JotunnLib/Extensions/StringExtensions.cs @@ -102,5 +102,20 @@ internal static string CapitalizeFirstLetter(this string s) return $"{char.ToUpper(s[0])}"; else return char.ToUpper(s[0]) + s.Substring(1); + } + + /// + /// Returns an Empty string if value is null + /// + /// + /// + internal static string EmptyIfNull(this object value) + { + if (value == null) + { + return string.Empty; + } + return value.ToString(); + } } } From a881562859ef49dda896d0bad21ea19832fce74f Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:05:13 -0800 Subject: [PATCH 05/27] Fix access modifiers --- JotunnLib/Extensions/TransformExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JotunnLib/Extensions/TransformExtensions.cs b/JotunnLib/Extensions/TransformExtensions.cs index 0831a4e5a..1f8d88df4 100644 --- a/JotunnLib/Extensions/TransformExtensions.cs +++ b/JotunnLib/Extensions/TransformExtensions.cs @@ -3,7 +3,7 @@ namespace Jotunn.Extensions { - internal static class TransformExtensions + public static class TransformExtensions { /// /// Extension method to find nested children by name using either From dfb2342fc13298603d5bf347e54d8a8b586a53df Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:05:49 -0800 Subject: [PATCH 06/27] Fix access modifiers to be public --- JotunnLib/Extensions/StringExtensions.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/JotunnLib/Extensions/StringExtensions.cs b/JotunnLib/Extensions/StringExtensions.cs index 7af2fe868..b388c2cf2 100644 --- a/JotunnLib/Extensions/StringExtensions.cs +++ b/JotunnLib/Extensions/StringExtensions.cs @@ -1,6 +1,6 @@ namespace Jotunn.Extensions { - internal static class StringExtensions + public static class StringExtensions { /// /// Returns true if the string contains any of the substrings. @@ -8,7 +8,7 @@ internal static class StringExtensions /// /// /// - internal static bool ContainsAny(this string str, params string[] substrings) + public static bool ContainsAny(this string str, params string[] substrings) { foreach (var substring in substrings) { @@ -26,7 +26,7 @@ internal static bool ContainsAny(this string str, params string[] substrings) /// /// /// - internal static bool EndsWithAny(this string str, params string[] suffixes) + public static bool EndsWithAny(this string str, params string[] suffixes) { foreach (var substring in suffixes) { @@ -44,7 +44,7 @@ internal static bool EndsWithAny(this string str, params string[] suffixes) /// /// /// - internal static bool StartsWithAny(this string str, params string[] prefixes) + public static bool StartsWithAny(this string str, params string[] prefixes) { foreach (var substring in prefixes) { @@ -63,7 +63,7 @@ internal static bool StartsWithAny(this string str, params string[] prefixes) /// /// /// - internal static string RemoveSuffix(this string s, string suffix) + public static string RemoveSuffix(this string s, string suffix) { if (s.EndsWith(suffix)) { @@ -80,7 +80,7 @@ internal static string RemoveSuffix(this string s, string suffix) /// /// /// - internal static string RemovePrefix(this string s, string prefix) + public static string RemovePrefix(this string s, string prefix) { if (s.StartsWith(prefix)) { @@ -94,7 +94,7 @@ internal static string RemovePrefix(this string s, string prefix) /// /// /// - internal static string CapitalizeFirstLetter(this string s) + public static string CapitalizeFirstLetter(this string s) { if (s.Length == 0) return s; @@ -109,7 +109,7 @@ internal static string CapitalizeFirstLetter(this string s) /// /// /// - internal static string EmptyIfNull(this object value) + public static string EmptyIfNull(this object value) { if (value == null) { From 24eca2a8299fac5f8800ea158e12e25dafafa909 Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:06:35 -0800 Subject: [PATCH 07/27] GameObject extension to check if gameObject has a component, used to improve readability of code or force null result to be a bool instead --- JotunnLib/Extensions/GameObjectExtension.cs | 28 ++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index ec3729976..274b59c62 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -1,6 +1,6 @@ -using System.Reflection; - using TMPro; - using UnityEngine; +using System.Reflection; +using TMPro; +using UnityEngine; using UnityEngine.UI; namespace Jotunn @@ -104,6 +104,28 @@ public static Component AddComponentCopy(this GameObject gameObject, T duplic return target; } + + /// + /// Extension method to check if GameObject has a component. + /// + /// + /// + /// + public static bool HasComponent(this GameObject gameObject) where T : UnityEngine.Component + { + return gameObject.GetComponent() != null; + } + + /// + /// Extension method to check if GameObject has a component. + /// + /// + /// + /// + public static bool HasComponent(this GameObject gameObject, string componentName) + { + return gameObject.GetComponent(componentName) != null; + } } /// From 5ed769597464daa5d1a590bd92cf0d1fa640b3e6 Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:09:03 -0800 Subject: [PATCH 08/27] Extensions method (with overloads) to check if gameObject contains an instance of any one of the given components --- JotunnLib/Extensions/GameObjectExtension.cs | 37 +++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index 274b59c62..2f0d6723a 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -1,4 +1,5 @@ using System.Reflection; +using System; using TMPro; using UnityEngine; using UnityEngine.UI; @@ -126,6 +127,42 @@ public static bool HasComponent(this GameObject gameObject, string componentName { return gameObject.GetComponent(componentName) != null; } + + /// + /// Check if GameObject has any of the specified components. + /// + /// + /// + /// + public static bool HasAnyComponent(this GameObject gameObject, params Type[] components) + { + foreach (var compo in components) + { + if (gameObject.GetComponent(compo) != null) + { + return true; + } + } + return false; + } + + /// + /// Check if GameObject has any of the specified components. + /// + /// + /// + /// + public static bool HasAnyComponent(this GameObject gameObject, params string[] componentNames) + { + foreach (var name in componentNames) + { + if (gameObject.GetComponent(name) != null) + { + return true; + } + } + return false; + } } /// From 0b979ec0781ba1c08895927411b2f3805b19bc93 Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:10:23 -0800 Subject: [PATCH 09/27] Extension method (with overloads) to check if a gameObject has all of the given components. --- JotunnLib/Extensions/GameObjectExtension.cs | 36 +++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index 2f0d6723a..1668f80ab 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -163,6 +163,42 @@ public static bool HasAnyComponent(this GameObject gameObject, params string[] c } return false; } + + /// + /// Check if GameObject has all of the specified components. + /// + /// + /// + /// + public static bool HasAllComponents(this GameObject gameObject, params string[] componentNames) + { + foreach (var name in componentNames) + { + if (gameObject.GetComponent(name) == null) + { + return false; + } + } + return true; + } + + /// + /// Check if GameObject has all of the specified components. + /// + /// + /// + /// + public static bool HasAllComponents(this GameObject gameObject, params Type[] components) + { + foreach (var compo in components) + { + if (gameObject.GetComponent(compo) == null) + { + return false; + } + } + return true; + } } /// From 5c28bf360b675b263168c82882603c716e27a083 Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:11:36 -0800 Subject: [PATCH 10/27] Extension method to check if gameObject has any of the given components in any of its children. --- JotunnLib/Extensions/GameObjectExtension.cs | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index 1668f80ab..63a599a86 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -199,6 +199,29 @@ public static bool HasAllComponents(this GameObject gameObject, params Type[] co } return true; } + + /// + /// Check if GameObject or any of it's children + /// have any of the specified components. + /// + /// + /// + /// + public static bool HasAnyComponentInChildren( + this GameObject gameObject, + bool includeInactive = false, + params Type[] components + ) + { + foreach (var compo in components) + { + if (gameObject.GetComponentInChildren(compo, includeInactive) != null) + { + return true; + } + } + return false; + } } /// From d2d7f74030afcd195741353875dccd4702bd18df Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:13:51 -0800 Subject: [PATCH 11/27] Extension method to check if gameObject or any of it's children has a component. Intended to improve readability or force null result to a bool if needed --- JotunnLib/Extensions/GameObjectExtension.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index 63a599a86..ebb05f66c 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -222,6 +222,18 @@ params Type[] components } return false; } + + /// + /// Check if GameObject or any of it's children + /// have the specific component. + /// + /// + /// + /// + public static bool HasComponentInChildren(this GameObject gameObject, bool includeInactive = false) where T : Component + { + return gameObject.GetComponentInChildren(includeInactive) != null; + } } /// From 5b507f68f9695f62a7c59ccfb8e93abddf70b503 Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:15:46 -0800 Subject: [PATCH 12/27] Extension method to search for a specific component based on the name of the child object it is attached to. --- JotunnLib/Extensions/GameObjectExtension.cs | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index ebb05f66c..e73da8d47 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -234,6 +234,32 @@ public static bool HasComponentInChildren(this GameObject gameObject, bool in { return gameObject.GetComponentInChildren(includeInactive) != null; } + + /// + /// Extension method to get the first component in the GameObject + /// or it's children that has the specified name. + /// + /// + /// + /// + /// + /// + public static T GetComponentInChildrenByName( + this GameObject gameObject, + string name, + bool includeInactive = false + ) where T : Component + { + foreach (var compo in gameObject.GetComponentsInChildren(includeInactive)) + { + if (compo.name == name) + { + return compo; + } + } + Logger.LogWarning($"No {nameof(T)} with name {name} found for GameObject: {gameObject.name}"); + return null; + } } /// From 310f1f591c9819ddbc09c7b6566df0f22c43002e Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:19:08 -0800 Subject: [PATCH 13/27] Extension to expose the Transform extension FindDeepChild as a method for GameObject --- JotunnLib/Extensions/GameObjectExtension.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index e73da8d47..cd40bdc1e 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -3,6 +3,7 @@ using TMPro; using UnityEngine; using UnityEngine.UI; +using Jotunn.Extensions; namespace Jotunn { @@ -260,6 +261,19 @@ public static T GetComponentInChildrenByName( Logger.LogWarning($"No {nameof(T)} with name {name} found for GameObject: {gameObject.name}"); return null; } + + /// + /// Extension method to find nested children by name using either + /// a breadth-first or depth-first search. Default is breadth-first. + /// + /// + /// + /// + /// + public static Transform FindDeepChild(this GameObject gameObject, string childName, bool breadthFirst = true) + { + return gameObject?.transform.FindDeepChild(childName, breadthFirst); + } } /// From 43a011e0275fa0085f8218995a999b5ef2ed5da7 Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:21:56 -0800 Subject: [PATCH 14/27] Improved parameter descriptions --- JotunnLib/Extensions/GameObjectExtension.cs | 12 ++++++------ JotunnLib/Extensions/TransformExtensions.cs | 3 ++- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index cd40bdc1e..0fd878cbf 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -113,7 +113,7 @@ public static Component AddComponentCopy(this GameObject gameObject, T duplic /// /// /// - public static bool HasComponent(this GameObject gameObject) where T : UnityEngine.Component + public static bool HasComponent(this GameObject gameObject) where T : Component { return gameObject.GetComponent() != null; } @@ -151,7 +151,7 @@ public static bool HasAnyComponent(this GameObject gameObject, params Type[] com /// Check if GameObject has any of the specified components. /// /// - /// + /// /// public static bool HasAnyComponent(this GameObject gameObject, params string[] componentNames) { @@ -229,7 +229,7 @@ params Type[] components /// have the specific component. /// /// - /// + /// Whether to include inactive child objects in the search or not. /// public static bool HasComponentInChildren(this GameObject gameObject, bool includeInactive = false) where T : Component { @@ -243,7 +243,7 @@ public static bool HasComponentInChildren(this GameObject gameObject, bool in /// /// /// - /// + /// Whether to include inactive child objects in the search or not. /// public static T GetComponentInChildrenByName( this GameObject gameObject, @@ -267,8 +267,8 @@ public static T GetComponentInChildrenByName( /// a breadth-first or depth-first search. Default is breadth-first. /// /// - /// - /// + /// Name of the child object to search for. + /// Whether to preform a breadth first or depth first search. Default is breadth first. /// public static Transform FindDeepChild(this GameObject gameObject, string childName, bool breadthFirst = true) { diff --git a/JotunnLib/Extensions/TransformExtensions.cs b/JotunnLib/Extensions/TransformExtensions.cs index 1f8d88df4..22f964946 100644 --- a/JotunnLib/Extensions/TransformExtensions.cs +++ b/JotunnLib/Extensions/TransformExtensions.cs @@ -10,7 +10,8 @@ public static class TransformExtensions /// a breadth-first or depth-first search. Default is breadth-first. /// /// - /// + /// Name of the child object to search for. + /// Whether to preform a breadth first or depth first search. Default is breadth first. /// public static Transform FindDeepChild( this Transform transform, From 1d08d21e14789763161ff84974cf1d6d62d42cbf Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:34:11 -0800 Subject: [PATCH 15/27] Fix build failure due to ambiguous reference to Object introduced by new extension methods using System --- JotunnLib/Extensions/GameObjectExtension.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index 0fd878cbf..8b110dda5 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -28,7 +28,7 @@ public static GameObject OrNull(this GameObject @this) /// Any type that inherits MonoBehaviour /// this /// Returns null when MonoBehaviours.op_equality returns false. - public static T OrNull(this T @this) where T : Object + public static T OrNull(this T @this) where T : UnityEngine.Object { return (T)(@this ? @this : null); } From 3ff63a55a8d89d7908da2b470ff893e38803b72c Mon Sep 17 00:00:00 2001 From: Searica <143636061+searica@users.noreply.github.com> Date: Sat, 18 Nov 2023 07:39:26 -0800 Subject: [PATCH 16/27] Update JotunnLib/Extensions/GameObjectExtension.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Maximilian Schmöcker <39767545+MSchmoecker@users.noreply.github.com> --- JotunnLib/Extensions/GameObjectExtension.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index 8b110dda5..1df952457 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -139,7 +139,7 @@ public static bool HasAnyComponent(this GameObject gameObject, params Type[] com { foreach (var compo in components) { - if (gameObject.GetComponent(compo) != null) + if (gameObject.GetComponent(compo)) { return true; } From 7ae8bbcc0f195ef3d347128c66b173bdc139540e Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Sat, 18 Nov 2023 07:48:28 -0800 Subject: [PATCH 17/27] Wrap Vanilla Valheim method instead of using custom version --- JotunnLib/Extensions/GameObjectExtension.cs | 11 ++++-- JotunnLib/Extensions/TransformExtensions.cs | 44 +++------------------ 2 files changed, 13 insertions(+), 42 deletions(-) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index 8b110dda5..91f9014f6 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -268,11 +268,14 @@ public static T GetComponentInChildrenByName( /// /// /// Name of the child object to search for. - /// Whether to preform a breadth first or depth first search. Default is breadth first. - /// - public static Transform FindDeepChild(this GameObject gameObject, string childName, bool breadthFirst = true) + /// Whether to preform a breadth first or depth first search. Default is breadth first. + public static Transform FindDeepChild( + this GameObject gameObject, + string childName, + global::Utils.IterativeSearchType searchType = global::Utils.IterativeSearchType.BreadthFirst + ) { - return gameObject?.transform.FindDeepChild(childName, breadthFirst); + return gameObject.transform.FindDeepChild(childName, searchType); } } diff --git a/JotunnLib/Extensions/TransformExtensions.cs b/JotunnLib/Extensions/TransformExtensions.cs index 22f964946..89fc5219a 100644 --- a/JotunnLib/Extensions/TransformExtensions.cs +++ b/JotunnLib/Extensions/TransformExtensions.cs @@ -1,8 +1,10 @@ -using System.Collections.Generic; using UnityEngine; namespace Jotunn.Extensions { + /// + /// Convenience methods for Transforms + /// public static class TransformExtensions { /// @@ -11,49 +13,15 @@ public static class TransformExtensions /// /// /// Name of the child object to search for. - /// Whether to preform a breadth first or depth first search. Default is breadth first. + /// Whether to preform a breadth first or depth first search. Default is breadth first. /// public static Transform FindDeepChild( this Transform transform, string childName, - bool breadthFirst = true + global::Utils.IterativeSearchType searchType = global::Utils.IterativeSearchType.BreadthFirst ) { - if (breadthFirst) - { - var queue = new Queue(); - queue.Enqueue(transform); - while (queue.Count > 0) - { - var child = queue.Dequeue(); - if (child.name == childName) - { - return child; - } - - foreach (Transform t in child) - { - queue.Enqueue(t); - } - } - return null; - } - else - { - foreach (Transform child in transform) - { - if (child.name == childName) - { - return child; - } - var result = child.FindDeepChild(childName); - if (result != null) - { - return result; - } - } - return null; - } + return global::Utils.FindChild(transform, childName, searchType); } } } From 9518875b1659cf010c9996bcde3fea1aa54353a1 Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Sat, 18 Nov 2023 07:56:05 -0800 Subject: [PATCH 18/27] Remove redundant methods --- JotunnLib/Extensions/GameObjectExtension.cs | 23 --------------------- 1 file changed, 23 deletions(-) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index 91f9014f6..0db9852c0 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -107,29 +107,6 @@ public static Component AddComponentCopy(this GameObject gameObject, T duplic return target; } - /// - /// Extension method to check if GameObject has a component. - /// - /// - /// - /// - public static bool HasComponent(this GameObject gameObject) where T : Component - { - return gameObject.GetComponent() != null; - } - - /// - /// Extension method to check if GameObject has a component. - /// - /// - /// - /// - public static bool HasComponent(this GameObject gameObject, string componentName) - { - return gameObject.GetComponent(componentName) != null; - } - - /// /// Check if GameObject has any of the specified components. /// /// From 2eff6f8fca4fecb44368eae4681fe17cc8aca80e Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Sat, 18 Nov 2023 07:57:21 -0800 Subject: [PATCH 19/27] Remove null checks --- JotunnLib/Extensions/GameObjectExtension.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index 0db9852c0..1c1ba7283 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -116,7 +116,7 @@ public static bool HasAnyComponent(this GameObject gameObject, params Type[] com { foreach (var compo in components) { - if (gameObject.GetComponent(compo) != null) + if (gameObject.GetComponent(compo)) { return true; } @@ -134,7 +134,7 @@ public static bool HasAnyComponent(this GameObject gameObject, params string[] c { foreach (var name in componentNames) { - if (gameObject.GetComponent(name) != null) + if (gameObject.GetComponent(name)) { return true; } From f799a32f006f71a30f2fde9d8167e5002cc158d3 Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Sat, 18 Nov 2023 07:58:08 -0800 Subject: [PATCH 20/27] Fix missing param in doc --- JotunnLib/Extensions/GameObjectExtension.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index 1c1ba7283..0d67463b1 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -183,6 +183,7 @@ public static bool HasAllComponents(this GameObject gameObject, params Type[] co /// have any of the specified components. /// /// + /// /// /// public static bool HasAnyComponentInChildren( From 3c9b4343be412d2366f5537b40b4ba357c2a6f57 Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Sat, 18 Nov 2023 08:03:57 -0800 Subject: [PATCH 21/27] Better accomplished by using Utils.FindChild() + GetComponent() so remove redundant method --- JotunnLib/Extensions/GameObjectExtension.cs | 26 --------------------- 1 file changed, 26 deletions(-) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index 0d67463b1..ae1158a6e 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -214,32 +214,6 @@ public static bool HasComponentInChildren(this GameObject gameObject, bool in return gameObject.GetComponentInChildren(includeInactive) != null; } - /// - /// Extension method to get the first component in the GameObject - /// or it's children that has the specified name. - /// - /// - /// - /// - /// Whether to include inactive child objects in the search or not. - /// - public static T GetComponentInChildrenByName( - this GameObject gameObject, - string name, - bool includeInactive = false - ) where T : Component - { - foreach (var compo in gameObject.GetComponentsInChildren(includeInactive)) - { - if (compo.name == name) - { - return compo; - } - } - Logger.LogWarning($"No {nameof(T)} with name {name} found for GameObject: {gameObject.name}"); - return null; - } - /// /// Extension method to find nested children by name using either /// a breadth-first or depth-first search. Default is breadth-first. From 20643f9042f1fe388b7c43dc856c927b04537fe5 Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Sat, 18 Nov 2023 08:04:33 -0800 Subject: [PATCH 22/27] Remove unneeded null check --- JotunnLib/Extensions/GameObjectExtension.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index ae1158a6e..47865b8a8 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -194,7 +194,7 @@ params Type[] components { foreach (var compo in components) { - if (gameObject.GetComponentInChildren(compo, includeInactive) != null) + if (gameObject.GetComponentInChildren(compo, includeInactive)) { return true; } From 6d1bee6b14f624ae065e31b7874299f6d336bc69 Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Sat, 18 Nov 2023 08:07:53 -0800 Subject: [PATCH 23/27] This method is redundant and can be written as string ?? string.Empty or string ?? "" --- JotunnLib/Extensions/StringExtensions.cs | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/JotunnLib/Extensions/StringExtensions.cs b/JotunnLib/Extensions/StringExtensions.cs index b388c2cf2..44ae1d0d9 100644 --- a/JotunnLib/Extensions/StringExtensions.cs +++ b/JotunnLib/Extensions/StringExtensions.cs @@ -103,19 +103,5 @@ public static string CapitalizeFirstLetter(this string s) else return char.ToUpper(s[0]) + s.Substring(1); } - - /// - /// Returns an Empty string if value is null - /// - /// - /// - public static string EmptyIfNull(this object value) - { - if (value == null) - { - return string.Empty; - } - return value.ToString(); - } } } From 030b8c85876bd97f8a6ee6a5b5dddd18176340ac Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Sat, 18 Nov 2023 08:18:45 -0800 Subject: [PATCH 24/27] Rewrite to avoid null checks, not sure if I could just use if (!gameObject.GetComponent(name)) instead though? --- JotunnLib/Extensions/GameObjectExtension.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index 47865b8a8..159935ad5 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -150,14 +150,19 @@ public static bool HasAnyComponent(this GameObject gameObject, params string[] c /// public static bool HasAllComponents(this GameObject gameObject, params string[] componentNames) { + int compCount = 0; foreach (var name in componentNames) { - if (gameObject.GetComponent(name) == null) + if (gameObject.GetComponent(name)) + { + compCount++; + } + else { return false; } } - return true; + return compCount == componentNames.Length; } /// @@ -168,14 +173,19 @@ public static bool HasAllComponents(this GameObject gameObject, params string[] /// public static bool HasAllComponents(this GameObject gameObject, params Type[] components) { + int compCount = 0; foreach (var compo in components) { - if (gameObject.GetComponent(compo) == null) + if (gameObject.GetComponent(compo)) + { + compCount++; + } + else { return false; } } - return true; + return compCount == components.Length; } /// From bebcbbc51284a028cd3fa3adf059d0dcc6a650a9 Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Sat, 18 Nov 2023 08:20:53 -0800 Subject: [PATCH 25/27] Remove redundant method --- JotunnLib/Extensions/GameObjectExtension.cs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index 159935ad5..3940143e7 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -212,18 +212,6 @@ params Type[] components return false; } - /// - /// Check if GameObject or any of it's children - /// have the specific component. - /// - /// - /// Whether to include inactive child objects in the search or not. - /// - public static bool HasComponentInChildren(this GameObject gameObject, bool includeInactive = false) where T : Component - { - return gameObject.GetComponentInChildren(includeInactive) != null; - } - /// /// Extension method to find nested children by name using either /// a breadth-first or depth-first search. Default is breadth-first. From b2220bff8b80b2121547646b35e0f129dbc9be3d Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Sat, 18 Nov 2023 08:27:11 -0800 Subject: [PATCH 26/27] Fix doc format --- JotunnLib/Extensions/GameObjectExtension.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index 3940143e7..9a875d70e 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -107,6 +107,7 @@ public static Component AddComponentCopy(this GameObject gameObject, T duplic return target; } + /// /// Check if GameObject has any of the specified components. /// /// From f7441aa73b502244c4c5ae6e9eb5a4b08322e530 Mon Sep 17 00:00:00 2001 From: searica <143636061+searica@users.noreply.github.com> Date: Sat, 18 Nov 2023 08:54:11 -0800 Subject: [PATCH 27/27] Take advantage of Monobehavior implicit cast to bool and use bool check instead of counter --- JotunnLib/Extensions/GameObjectExtension.cs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/JotunnLib/Extensions/GameObjectExtension.cs b/JotunnLib/Extensions/GameObjectExtension.cs index 9a875d70e..f769abd52 100644 --- a/JotunnLib/Extensions/GameObjectExtension.cs +++ b/JotunnLib/Extensions/GameObjectExtension.cs @@ -151,19 +151,14 @@ public static bool HasAnyComponent(this GameObject gameObject, params string[] c /// public static bool HasAllComponents(this GameObject gameObject, params string[] componentNames) { - int compCount = 0; foreach (var name in componentNames) { - if (gameObject.GetComponent(name)) - { - compCount++; - } - else + if (!gameObject.GetComponent(name)) { return false; } } - return compCount == componentNames.Length; + return true; } /// @@ -174,19 +169,14 @@ public static bool HasAllComponents(this GameObject gameObject, params string[] /// public static bool HasAllComponents(this GameObject gameObject, params Type[] components) { - int compCount = 0; foreach (var compo in components) { - if (gameObject.GetComponent(compo)) - { - compCount++; - } - else + if (!gameObject.GetComponent(compo)) { return false; } } - return compCount == components.Length; + return true; } ///