| | 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 System.Diagnostics.CodeAnalysis; |
| | 7 | | using System.Threading; |
| | 8 | | using GDX.Mathematics.Random; |
| | 9 | | using UnityEngine; |
| | 10 | | using UnityEngine.LowLevel; |
| | 11 | | using UnityEngine.PlayerLoop; |
| | 12 | | using UnityEngine.Scripting; |
| | 13 | | #if UNITY_EDITOR |
| | 14 | | using UnityEditor; |
| | 15 | | #endif // UNITY_EDITOR |
| | 16 | |
|
| | 17 | | namespace GDX |
| | 18 | | { |
| | 19 | | [Preserve] |
| | 20 | | public static class Core |
| | 21 | | { |
| | 22 | | public const string OverrideClass = "CustomConfig"; |
| | 23 | | public const string OverrideMethod = "Init"; |
| | 24 | | public const string PerformanceCategory = "GDX.Performance"; |
| | 25 | | public const string TestCategory = "GDX.Tests"; |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// An empty <see cref="object" /> array useful when things require it. |
| | 29 | | /// </summary> |
| | 30 | | // ReSharper disable once RedundantArrayCreationExpression, HeapView.ObjectAllocation.Evident |
| | 31 | | public static readonly object[] EmptyObjectArray = new object[] { }; |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// The main threads identifier. |
| | 35 | | /// </summary> |
| | 36 | | public static int MainThreadID = -1; |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// A pseudorandom number generated seeded with <see cref="StartTicks" />. |
| | 40 | | /// </summary> |
| | 41 | | /// <remarks>Useful for generic randomness where determinism is not required.</remarks> |
| | 42 | | public static WELL1024a Random; |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// The point in tick based time when the core was initialized. |
| | 46 | | /// </summary> |
| | 47 | | public static readonly long StartTicks; |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Has the <see cref="Core" /> main thread initialization happened? |
| | 51 | | /// </summary> |
| | 52 | | static bool s_InitializedMainThread; |
| | 53 | |
|
| | 54 | | static bool s_InitializedRuntime; |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Static constructor. |
| | 58 | | /// </summary> |
| | 59 | | /// <remarks>Nothing in here can reference the Unity engine and must be thread-safe.</remarks> |
| | 60 | | [ExcludeFromCodeCoverage] |
| | 61 | | static Core() |
| | 62 | | { |
| | 63 | | // Record initialization time. |
| | 64 | | StartTicks = DateTime.Now.Ticks; |
| | 65 | |
|
| | 66 | | // The assemblies will change between editor time and compile time so we are going to unfortunately pay a |
| | 67 | | // cost to iterate over them and try to find our settings class |
| | 68 | | Reflection.InvokeStaticMethod($"GDX.{OverrideClass}", OverrideMethod); |
| | 69 | |
|
| | 70 | | // Initialize a random provider |
| | 71 | | Random = new WELL1024a((uint)StartTicks); |
| | 72 | |
|
| | 73 | | // ReSharper disable UnusedParameter.Local |
| | 74 | | #if UNITY_EDITOR |
| | 75 | | AppDomain.CurrentDomain.DomainUnload += (sender, args) => |
| | 76 | | { |
| | 77 | | Dispose(); |
| | 78 | | }; |
| | 79 | | #else |
| | 80 | | Application.quitting += Dispose; |
| | 81 | | #endif |
| | 82 | | // ReSharper restore UnusedParameter.Local |
| | 83 | | } |
| | 84 | |
|
| | 85 | | static void Dispose() |
| 0 | 86 | | { |
| 0 | 87 | | Random.Dispose(); |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | /// <summary> |
| | 91 | | /// Main-thread initializer. |
| | 92 | | /// </summary> |
| | 93 | | /// <remarks> |
| | 94 | | /// <para> |
| | 95 | | /// It might be important to call this function if you are using GDX related configurations inside of |
| | 96 | | /// another <see cref="UnityEngine.RuntimeInitializeOnLoadMethod" /> decorated static method. |
| | 97 | | /// </para> |
| | 98 | | /// <para> |
| | 99 | | /// An example of this sort of usage is in the <see cref="GDX.Threading.TaskDirectorSystem" />. |
| | 100 | | /// </para> |
| | 101 | | /// </remarks> |
| | 102 | | #if UNITY_EDITOR |
| | 103 | | [InitializeOnLoadMethod] |
| | 104 | | #else |
| | 105 | | [UnityEngine.RuntimeInitializeOnLoadMethod(UnityEngine.RuntimeInitializeLoadType.AfterAssembliesLoaded)] |
| | 106 | | #endif // UNITY_EDITOR |
| | 107 | | public static void InitializeOnMainThread() |
| 9 | 108 | | { |
| 9 | 109 | | if (s_InitializedMainThread) |
| 6 | 110 | | { |
| 6 | 111 | | return; |
| | 112 | | } |
| | 113 | |
|
| 3 | 114 | | MainThreadID = Thread.CurrentThread.ManagedThreadId; |
| | 115 | |
|
| 3 | 116 | | Localization.SetDefaultCulture(); |
| | 117 | |
|
| 3 | 118 | | s_InitializedMainThread = true; |
| 9 | 119 | | } |
| | 120 | |
|
| | 121 | | [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] |
| | 122 | | public static void InitializeAtRuntime() |
| 5 | 123 | | { |
| 5 | 124 | | if (s_InitializedRuntime) |
| 4 | 125 | | { |
| 4 | 126 | | return; |
| | 127 | | } |
| | 128 | |
|
| 1 | 129 | | PlayerLoopSystem systemRoot = PlayerLoop.GetCurrentPlayerLoop(); |
| | 130 | |
|
| | 131 | | // Subscribe our developer console |
| | 132 | | #if UNITY_2022_2_OR_NEWER |
| 1 | 133 | | if (Config.EnvironmentDeveloperConsole) |
| 1 | 134 | | { |
| | 135 | | // Disable the built in developer console |
| 1 | 136 | | Debug.developerConsoleEnabled = false; |
| | 137 | |
|
| 1 | 138 | | systemRoot.AddSubSystemToFirstSubSystemOfType( |
| | 139 | | typeof(Initialization), |
| | 140 | | typeof(Console), DeveloperConsoleTick); |
| 1 | 141 | | } |
| | 142 | | #endif // UNITY_2022_2_OR_NEWER |
| | 143 | |
|
| 1 | 144 | | PlayerLoop.SetPlayerLoop(systemRoot); |
| | 145 | |
|
| 1 | 146 | | s_InitializedRuntime = true; |
| 5 | 147 | | } |
| | 148 | |
|
| | 149 | | static void DeveloperConsoleTick() |
| 4388 | 150 | | { |
| | 151 | | #if UNITY_2022_2_OR_NEWER |
| | 152 | | // We need to feed in the deltaTime, this could be the previous frames if were being honest about it |
| 4388 | 153 | | Developer.Console.Tick(Time.deltaTime); |
| | 154 | | #endif // UNITY_2022_2_OR_NEWER |
| 4388 | 155 | | } |
| | 156 | | } |
| | 157 | | } |