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