| | 1 | | // Copyright (c) 2020-2023 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using GDX.Developer; |
| | 7 | | using UnityEngine.LowLevel; |
| | 8 | |
|
| | 9 | | namespace GDX |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// <see cref="UnityEngine.LowLevel.PlayerLoopSystem" /> Based Extension Methods |
| | 13 | | /// </summary> |
| | 14 | | public static class PlayerLoopSystemExtensions |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Adds a child (sub) system to the provided <paramref name="parentSystem"/>. |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="parentSystem">The parent system which a child (sub) system should be added too.</param> |
| | 20 | | /// <param name="subSystem">The child (sub) system that is to be added to the parent.</param> |
| | 21 | | public static void AddSubSystem(this ref PlayerLoopSystem parentSystem, ref PlayerLoopSystem subSystem) |
| 5 | 22 | | { |
| 5 | 23 | | if (parentSystem.subSystemList != null) |
| 0 | 24 | | { |
| 0 | 25 | | ref PlayerLoopSystem[] previousSubSystems = ref parentSystem.subSystemList; |
| 0 | 26 | | int subSystemCount = previousSubSystems.Length; |
| 0 | 27 | | parentSystem.subSystemList = new PlayerLoopSystem[subSystemCount + 1]; |
| 0 | 28 | | for (int i = 0; i < subSystemCount; i++) |
| 0 | 29 | | { |
| 0 | 30 | | parentSystem.subSystemList[i] = previousSubSystems[i]; |
| 0 | 31 | | } |
| 0 | 32 | | parentSystem.subSystemList[subSystemCount + 1] = subSystem; |
| 0 | 33 | | } |
| | 34 | | else |
| 5 | 35 | | { |
| 5 | 36 | | parentSystem.subSystemList = new PlayerLoopSystem[1]; |
| 5 | 37 | | parentSystem.subSystemList[0] = subSystem; |
| 5 | 38 | | } |
| 5 | 39 | | } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Adds a child (sub) system to the first found instance of a <paramref name="parentSystemType"/> system in |
| | 43 | | /// <paramref name="rootSystem"/>. |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="rootSystem">The root system which the <paramref name="parentSystemType"/> will be searched recu |
| | 46 | | /// <param name="parentSystemType">The system <see cref="Type"/> that will be searched for as the parent.</param |
| | 47 | | /// <param name="subSystemType">The type assigned when creating the <see cref="PlayerLoopSystem"/> to be added.< |
| | 48 | | /// <param name="subSystemUpdateFunction">The method to invoke when the system is updated.</param> |
| | 49 | | /// <returns>true/false if the <paramref name="parentSystemType"/> was found, and therefore the add could occur. |
| | 50 | | /// <example> |
| | 51 | | /// <code> |
| | 52 | | /// PlayerLoopSystem systemRoot = PlayerLoop.GetCurrentPlayerLoop(); |
| | 53 | | /// systemRoot.AddSubSystemToFirstSubSystemOfType( |
| | 54 | | /// typeof(Update.ScriptRunDelayedTasks), |
| | 55 | | /// typeof(TaskDirectorSystem), PlayerLoopTick); |
| | 56 | | /// PlayerLoop.SetPlayerLoop(systemRoot); |
| | 57 | | /// </code> |
| | 58 | | /// </example> |
| | 59 | | public static bool AddSubSystemToFirstSubSystemOfType(this ref PlayerLoopSystem rootSystem, |
| | 60 | | Type parentSystemType, Type subSystemType, PlayerLoopSystem.UpdateFunction subSystemUpdateFunction) |
| 4 | 61 | | { |
| 4 | 62 | | if (subSystemUpdateFunction == null || subSystemType == null) |
| 0 | 63 | | { |
| 0 | 64 | | return false; |
| | 65 | | } |
| | 66 | |
|
| 4 | 67 | | PlayerLoopSystem newSubSystem = new PlayerLoopSystem() |
| | 68 | | { |
| | 69 | | updateDelegate = subSystemUpdateFunction, type = subSystemType |
| | 70 | | }; |
| | 71 | |
|
| 4 | 72 | | ref PlayerLoopSystem foundParent = ref rootSystem.TryGetFirstSubSystemOfType(parentSystemType, out bool foun |
| 4 | 73 | | if (!foundTargetSystem) |
| 0 | 74 | | { |
| 0 | 75 | | return false; |
| | 76 | | } |
| | 77 | |
|
| 4 | 78 | | AddSubSystem(ref foundParent, ref newSubSystem); |
| 4 | 79 | | return true; |
| 4 | 80 | | } |
| | 81 | |
|
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Adds a child (sub) system to the first found instance of a <paramref name="parentSystemType"/> system in |
| | 85 | | /// <paramref name="rootSystem"/>. |
| | 86 | | /// </summary> |
| | 87 | | /// <param name="rootSystem">The root system which the <paramref name="parentSystemType"/> will be searched recu |
| | 88 | | /// <param name="parentSystemType">The system <see cref="Type"/> that will be searched for as the parent.</param |
| | 89 | | /// <param name="subSystem">The child (sub) system that is to be added to the parent.</param> |
| | 90 | | /// <returns>true/false if the <paramref name="parentSystemType"/> was found, and therefore the add could occur. |
| | 91 | | public static bool AddSubSystemToFirstSubSystemOfType(this ref PlayerLoopSystem rootSystem, Type parentSystemTyp |
| 1 | 92 | | { |
| 1 | 93 | | ref PlayerLoopSystem foundParent = ref rootSystem.TryGetFirstSubSystemOfType(parentSystemType, out bool foun |
| 1 | 94 | | if (!foundTargetSystem) |
| 0 | 95 | | { |
| 0 | 96 | | return false; |
| | 97 | | } |
| | 98 | |
|
| 1 | 99 | | AddSubSystem(ref foundParent, ref subSystem); |
| 1 | 100 | | return true; |
| 1 | 101 | | } |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Populates a <see cref="TextGenerator"/> with a tree-like structure that represents the |
| | 105 | | /// <see cref="PlayerLoopSystem"/> found under the <paramref name="rootSystem"/>. |
| | 106 | | /// </summary> |
| | 107 | | /// <param name="rootSystem">The root system which the tree should be crafted based off of.</param> |
| | 108 | | /// <param name="generator">Optionally, provide a generator to be populated.</param> |
| | 109 | | /// <returns>A <see cref="TextGenerator"/> populated with the system output.</returns> |
| | 110 | | public static TextGenerator GenerateSystemTree(this ref PlayerLoopSystem rootSystem, TextGenerator generator = n |
| 663 | 111 | | { |
| | 112 | | // We abuse this for recursion |
| 663 | 113 | | generator ??= new TextGenerator(); |
| | 114 | |
|
| 663 | 115 | | if (rootSystem.type != null) |
| 658 | 116 | | { |
| 658 | 117 | | generator.AppendLine(rootSystem.type.Name); |
| 658 | 118 | | } |
| | 119 | | else |
| 5 | 120 | | { |
| 5 | 121 | | generator.AppendLine(generator.GetIndentLevel() == 0 ? "_RootSystem_" : "_NullSystem_"); |
| 5 | 122 | | } |
| | 123 | |
|
| 663 | 124 | | if (rootSystem.subSystemList == null || rootSystem.subSystemList.Length <= 0) |
| 615 | 125 | | { |
| 615 | 126 | | return generator; |
| | 127 | | } |
| | 128 | |
|
| 48 | 129 | | int count = rootSystem.subSystemList.Length; |
| 48 | 130 | | if (count > 0) |
| 48 | 131 | | { |
| 48 | 132 | | generator.PushIndent(); |
| 48 | 133 | | } |
| 1412 | 134 | | for (int i = 0; i < count; i++) |
| 658 | 135 | | { |
| 658 | 136 | | GenerateSystemTree(ref rootSystem.subSystemList[i], generator); |
| 658 | 137 | | } |
| 48 | 138 | | if (count > 0) |
| 48 | 139 | | { |
| 48 | 140 | | generator.PopIndent(); |
| 48 | 141 | | } |
| | 142 | |
|
| 48 | 143 | | return generator; |
| 663 | 144 | | } |
| | 145 | |
|
| | 146 | | /// <summary> |
| | 147 | | /// Removes all child (sub) systems of the specified <paramref name="subSystemType"/> from the provided |
| | 148 | | /// <paramref name="parentSystem"/>. |
| | 149 | | /// </summary> |
| | 150 | | /// <remarks> |
| | 151 | | /// This is NOT recursive, and will not effect the child (sub) systems of the child (sub) systems of the |
| | 152 | | /// <paramref name="parentSystem"/> |
| | 153 | | /// </remarks> |
| | 154 | | /// <param name="parentSystem">The parent system which the child (sub) systems should be removed from.</param> |
| | 155 | | /// <param name="subSystemType">The system <see cref="Type"/> that will be removed.</param> |
| | 156 | | /// <returns>true/false, if a remove was done.</returns> |
| | 157 | | public static bool RemoveSubSystemsOfType(this ref PlayerLoopSystem parentSystem, Type subSystemType) |
| 5 | 158 | | { |
| 5 | 159 | | if (parentSystem.subSystemList == null) |
| 0 | 160 | | { |
| 0 | 161 | | return false; |
| | 162 | | } |
| | 163 | |
|
| 5 | 164 | | ref PlayerLoopSystem[] previousSubSystems = ref parentSystem.subSystemList; |
| 5 | 165 | | int subSystemCount = previousSubSystems.Length; |
| | 166 | |
|
| | 167 | | // We need to actually make sure there is a sub system of type to remove, we will use the search as a place |
| | 168 | | // to also figure out if there is multiple options. |
| 5 | 169 | | int foundCount = 0; |
| 20 | 170 | | for (int i = 0; i < subSystemCount; i++) |
| 5 | 171 | | { |
| 5 | 172 | | if (previousSubSystems[i].type == subSystemType) |
| 5 | 173 | | { |
| 5 | 174 | | foundCount++; |
| 5 | 175 | | } |
| 5 | 176 | | } |
| | 177 | |
|
| 5 | 178 | | if (foundCount == 0) |
| 0 | 179 | | { |
| 0 | 180 | | return false; |
| | 181 | | } |
| | 182 | |
|
| 5 | 183 | | int newIndex = 0; |
| 5 | 184 | | int newCount = subSystemCount - foundCount; |
| 5 | 185 | | parentSystem.subSystemList = new PlayerLoopSystem[newCount]; |
| 10 | 186 | | for (int i = 0; i < newCount; i++) |
| 0 | 187 | | { |
| 0 | 188 | | if (previousSubSystems[i].type != subSystemType) |
| 0 | 189 | | { |
| 0 | 190 | | parentSystem.subSystemList[newIndex] = previousSubSystems[i]; |
| 0 | 191 | | newIndex++; |
| 0 | 192 | | } |
| 0 | 193 | | } |
| | 194 | |
|
| 5 | 195 | | return true; |
| | 196 | |
|
| 5 | 197 | | } |
| | 198 | |
|
| | 199 | | /// <summary> |
| | 200 | | /// Removes all the child (sub) systems of to the first found instance of a <paramref name="parentSystemType |
| | 201 | | /// </summary> |
| | 202 | | /// <param name="rootSystem">The root system which the <paramref name="parentSystemType"/> will be searched recu |
| | 203 | | /// <param name="parentSystemType">The system <see cref="Type"/> that will be searched for as the parent.</param |
| | 204 | | /// <param name="subSystemType">The child (sub) system <see cref="Type"/> that will be removed.</param> |
| | 205 | | /// <returns>true/false, if a remove occured.</returns> |
| | 206 | | public static bool RemoveSubSystemsOfTypeFromFirstSubSystemOfType(this ref PlayerLoopSystem rootSystem, |
| | 207 | | Type parentSystemType, Type subSystemType) |
| 5 | 208 | | { |
| 5 | 209 | | ref PlayerLoopSystem foundParent = ref rootSystem.TryGetFirstSubSystemOfType(parentSystemType, out bool foun |
| 5 | 210 | | if (!foundTargetSystem) |
| 0 | 211 | | { |
| 0 | 212 | | return false; |
| | 213 | | } |
| | 214 | |
|
| 5 | 215 | | return RemoveSubSystemsOfType(ref foundParent, subSystemType); |
| 5 | 216 | | } |
| | 217 | |
|
| | 218 | | /// <summary> |
| | 219 | | /// Replaces the first child (sub) system of the given <paramref name="rootSystem"/> of |
| | 220 | | /// <paramref name="subSystemType"/> with the provided <paramref name="updatedSystem"/>. |
| | 221 | | /// </summary> |
| | 222 | | /// <param name="rootSystem">The root system which the <paramref name="subSystemType"/> will be searched recursi |
| | 223 | | /// <param name="subSystemType">The child (sub) system <see cref="Type"/> that will be replaced.</param> |
| | 224 | | /// <param name="updatedSystem">The system to replace the found <paramref name="subSystemType"/> with.</param> |
| | 225 | | /// <returns>true/false if the replace occured.</returns> |
| | 226 | | public static bool ReplaceFirstSubSystemOfType(this ref PlayerLoopSystem rootSystem, Type subSystemType, |
| | 227 | | ref PlayerLoopSystem updatedSystem) |
| 0 | 228 | | { |
| 0 | 229 | | ref PlayerLoopSystem foundParent = ref rootSystem.TryGetFirstSystemWithSubSystemOfType(subSystemType, out bo |
| 0 | 230 | | if (!foundTargetSystem) |
| 0 | 231 | | { |
| 0 | 232 | | return false; |
| | 233 | | } |
| 0 | 234 | | foundParent.subSystemList[foundIndex] = updatedSystem; |
| 0 | 235 | | return true; |
| 0 | 236 | | } |
| | 237 | |
|
| | 238 | | /// <summary> |
| | 239 | | /// Replaces all child (sub) systems of the specified <paramref name="subSystemType"/> from the provided |
| | 240 | | /// <paramref name="parentSystem"/>. |
| | 241 | | /// </summary> |
| | 242 | | /// <remarks> |
| | 243 | | /// This is NOT recursive, and will not effect the child (sub) systems of the child (sub) systems of the |
| | 244 | | /// <paramref name="parentSystem"/> |
| | 245 | | /// </remarks> |
| | 246 | | /// <param name="parentSystem">The parent system which the child (sub) systems should be replaced.</param> |
| | 247 | | /// <param name="subSystemType">The system <see cref="Type"/> that will be replaced.</param> |
| | 248 | | /// <param name="updatedSystem">The system to replace the found <paramref name="subSystemType"/> with.</param> |
| | 249 | | /// <returns>true/false if any replacement occured.</returns> |
| | 250 | | public static bool ReplaceSubSystemsOfType(this ref PlayerLoopSystem parentSystem, Type subSystemType, |
| | 251 | | ref PlayerLoopSystem updatedSystem) |
| 0 | 252 | | { |
| 0 | 253 | | if (parentSystem.subSystemList == null) |
| 0 | 254 | | { |
| 0 | 255 | | return false; |
| | 256 | | } |
| | 257 | |
|
| 0 | 258 | | int count = parentSystem.subSystemList.Length; |
| 0 | 259 | | bool replaced = false; |
| 0 | 260 | | for (int i = 0; i < count; i++) |
| 0 | 261 | | { |
| 0 | 262 | | if (parentSystem.subSystemList[i].type != subSystemType) |
| 0 | 263 | | { |
| 0 | 264 | | continue; |
| | 265 | | } |
| | 266 | |
|
| 0 | 267 | | parentSystem.subSystemList[i] = updatedSystem; |
| 0 | 268 | | replaced = true; |
| 0 | 269 | | } |
| 0 | 270 | | return replaced; |
| 0 | 271 | | } |
| | 272 | |
|
| | 273 | | /// <summary> |
| | 274 | | /// Searches the provided <paramref name="rootSystem"/> child (sub) systems for the first instance of a |
| | 275 | | /// <paramref name="subSystemType"/> system. |
| | 276 | | /// </summary> |
| | 277 | | /// <param name="rootSystem"> |
| | 278 | | /// The root system which the <paramref name="subSystemType"/> will be searched recursively for. |
| | 279 | | /// </param> |
| | 280 | | /// <param name="subSystemType">The child (sub) system <see cref="Type"/> that will be searched for recursively. |
| | 281 | | /// <param name="foundSubSystem">Was an appropriate system found?</param> |
| | 282 | | /// <returns> |
| | 283 | | /// The found system, or the root system. Check <paramref name="foundSubSystem"/> to determine if the system |
| | 284 | | /// was actually found. This pattern is used to preserve the reference. |
| | 285 | | /// </returns> |
| | 286 | | public static ref PlayerLoopSystem TryGetFirstSubSystemOfType(this ref PlayerLoopSystem rootSystem, Type subSyst |
| 70 | 287 | | { |
| 70 | 288 | | if (rootSystem.subSystemList != null) |
| 70 | 289 | | { |
| 70 | 290 | | int subCount = rootSystem.subSystemList.Length; |
| 1504 | 291 | | for (int i = 0; i < subCount; i++) |
| 702 | 292 | | { |
| | 293 | | // Wishful thinking |
| 702 | 294 | | if (rootSystem.subSystemList[i].type == subSystemType) |
| 10 | 295 | | { |
| 10 | 296 | | foundSubSystem = true; |
| 10 | 297 | | return ref rootSystem.subSystemList[i]; |
| | 298 | | } |
| | 299 | |
|
| | 300 | | // Evaluate children |
| 692 | 301 | | if (rootSystem.subSystemList[i].subSystemList != null && |
| | 302 | | rootSystem.subSystemList[i].subSystemList.Length > 0) |
| 60 | 303 | | { |
| 60 | 304 | | ref PlayerLoopSystem child = ref rootSystem.subSystemList[i] |
| | 305 | | .TryGetFirstSubSystemOfType(subSystemType, out foundSubSystem); |
| 60 | 306 | | if (foundSubSystem) |
| 10 | 307 | | { |
| 10 | 308 | | return ref child; |
| | 309 | | } |
| 50 | 310 | | } |
| 682 | 311 | | } |
| 50 | 312 | | } |
| | 313 | |
|
| 50 | 314 | | foundSubSystem = false; |
| 50 | 315 | | return ref rootSystem; |
| 70 | 316 | | } |
| | 317 | |
|
| | 318 | | /// <summary> |
| | 319 | | /// Searches the provided <paramref name="rootSystem"/> child (sub) systems for the first instance of a |
| | 320 | | /// <paramref name="subSystemType"/> and returns the parent system, with <paramref name="foundSystemIndex"/> |
| | 321 | | /// of the found child (sub) system. |
| | 322 | | /// </summary> |
| | 323 | | /// <param name="rootSystem"> |
| | 324 | | /// The root system which the <paramref name="subSystemType"/> will be searched recursively for. |
| | 325 | | /// </param> |
| | 326 | | /// <param name="subSystemType">The child (sub) system <see cref="Type"/> that will be searched for recursively. |
| | 327 | | /// <param name="foundSubSystem">Was an appropriate child (sub) system found?</param> |
| | 328 | | /// <param name="foundSystemIndex">The index of the found sub (child) system.</param> |
| | 329 | | /// <returns> |
| | 330 | | /// The found parent system, or the root system. Check <paramref name="foundSubSystem"/> to determine if the |
| | 331 | | /// child (sub) system was actually found. This pattern is used to preserve the reference. |
| | 332 | | /// </returns> |
| | 333 | | public static ref PlayerLoopSystem TryGetFirstSystemWithSubSystemOfType(this ref PlayerLoopSystem rootSystem, |
| | 334 | | Type subSystemType, out bool foundSubSystem, out int foundSystemIndex) |
| 0 | 335 | | { |
| 0 | 336 | | if (rootSystem.subSystemList != null) |
| 0 | 337 | | { |
| 0 | 338 | | int subCount = rootSystem.subSystemList.Length; |
| 0 | 339 | | for (int i = 0; i < subCount; i++) |
| 0 | 340 | | { |
| | 341 | | // Wishful thinking |
| 0 | 342 | | if (rootSystem.subSystemList[i].type == subSystemType) |
| 0 | 343 | | { |
| 0 | 344 | | foundSubSystem = true; |
| 0 | 345 | | foundSystemIndex = i; |
| 0 | 346 | | return ref rootSystem; |
| | 347 | | } |
| | 348 | |
|
| | 349 | | // Evaluate children |
| 0 | 350 | | if (rootSystem.subSystemList[i].subSystemList != null && |
| | 351 | | rootSystem.subSystemList[i].subSystemList.Length > 0) |
| 0 | 352 | | { |
| 0 | 353 | | ref PlayerLoopSystem child = ref rootSystem.subSystemList[i] |
| | 354 | | .TryGetFirstSystemWithSubSystemOfType(subSystemType, out foundSubSystem, |
| | 355 | | out foundSystemIndex); |
| 0 | 356 | | if (foundSubSystem) |
| 0 | 357 | | { |
| 0 | 358 | | return ref child; |
| | 359 | | } |
| 0 | 360 | | } |
| 0 | 361 | | } |
| 0 | 362 | | } |
| | 363 | |
|
| 0 | 364 | | foundSubSystem = false; |
| 0 | 365 | | foundSystemIndex = -1; |
| 0 | 366 | | return ref rootSystem; |
| 0 | 367 | | } |
| | 368 | | } |
| | 369 | | } |