| | 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.IO; |
| | 7 | | using System.Runtime.CompilerServices; |
| | 8 | | using System.Text; |
| | 9 | | using GDX.Developer; |
| | 10 | | using GDX.Mathematics.Random; |
| | 11 | | using UnityEngine; |
| | 12 | | using UnityEngine.Rendering; |
| | 13 | |
|
| | 14 | | namespace GDX |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// A collection of platform related helper utilities. |
| | 18 | | /// </summary> |
| | 19 | | [VisualScriptingCompatible(8)] |
| | 20 | | public static class Platform |
| | 21 | | { |
| | 22 | | public const float ImageCompareTolerance = 0.99f; |
| | 23 | | public const float FloatTolerance = 0.000001f; |
| | 24 | | public const double DoubleTolerance = 0.000001d; |
| | 25 | | public const string SafeCharacterPool = "abcdefghijklmnopqrstuvwxyz"; |
| | 26 | | public const int CharacterPoolLength = 25; |
| | 27 | | public const int CharacterPoolLengthExclusive = 24; |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// A filename safe version of the timestamp format. |
| | 31 | | /// </summary>s |
| | 32 | | public const string FilenameTimestampFormat = "yyyyMMdd_HHmmss"; |
| | 33 | | public const string TimestampFormat = "yyyy-MM-dd HH:mm:ss"; |
| | 34 | |
|
| | 35 | | static string s_OutputFolder; |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Validate that all directories are created for a given <paramref name="folderPath" />. |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="folderPath">The path to process and validate.</param> |
| | 41 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 42 | | public static void EnsureFolderHierarchyExists(string folderPath) |
| 6 | 43 | | { |
| 6 | 44 | | if (!string.IsNullOrEmpty(folderPath) && !Directory.Exists(folderPath)) |
| 0 | 45 | | { |
| 0 | 46 | | Directory.CreateDirectory(folderPath); |
| 0 | 47 | | } |
| 6 | 48 | | } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Validate that all parent directories are created for a given <paramref name="filePath" />. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="filePath">The path to process and validate.</param> |
| | 54 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 55 | | public static void EnsureFileFolderHierarchyExists(string filePath) |
| 0 | 56 | | { |
| 0 | 57 | | string targetDirectory = Path.GetDirectoryName(filePath); |
| 0 | 58 | | EnsureFolderHierarchyExists(targetDirectory); |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Validate that the file path is writable, making the necessary folder structure and setting permissions. |
| | 63 | | /// </summary> |
| | 64 | | /// <param name="filePath">The absolute path to validate.</param> |
| | 65 | | public static void EnsureFileWritable(string filePath) |
| 0 | 66 | | { |
| 0 | 67 | | string fileName = Path.GetFileName(filePath); |
| 0 | 68 | | if (fileName != null) |
| 0 | 69 | | { |
| 0 | 70 | | string directoryPath = filePath.TrimEnd(fileName.ToCharArray()); |
| 0 | 71 | | if (!Directory.Exists(directoryPath)) |
| 0 | 72 | | { |
| 0 | 73 | | Directory.CreateDirectory(directoryPath); |
| 0 | 74 | | } |
| 0 | 75 | | } |
| | 76 | |
|
| 0 | 77 | | if (File.Exists(filePath)) |
| 0 | 78 | | { |
| 0 | 79 | | File.SetAttributes(filePath, File.GetAttributes(filePath) & ~FileAttributes.ReadOnly); |
| 0 | 80 | | } |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Use our best attempt to remove a file at the designated <paramref name="filePath" />. |
| | 85 | | /// </summary> |
| | 86 | | /// <param name="filePath">The file path to remove forcefully.</param> |
| | 87 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 88 | | public static void ForceDeleteFile(string filePath) |
| 8 | 89 | | { |
| 8 | 90 | | if (File.Exists(filePath)) |
| 2 | 91 | | { |
| 2 | 92 | | File.SetAttributes(filePath, File.GetAttributes(filePath) & ~FileAttributes.ReadOnly); |
| 2 | 93 | | File.Delete(filePath); |
| 2 | 94 | | } |
| 8 | 95 | | } |
| | 96 | |
|
| | 97 | | /// <summary> |
| | 98 | | /// Gets the current platforms hardware generation number? |
| | 99 | | /// </summary> |
| | 100 | | /// <returns>Returns 0 for base hardware, 1 for updates.</returns> |
| | 101 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 102 | | public static int GetHardwareGeneration() |
| 0 | 103 | | { |
| | 104 | | #if UNITY_XBOXONE && !UNITY_EDITOR |
| | 105 | | if (Hardware.version == HardwareVersion.XboxOneX_Devkit || |
| | 106 | | Hardware.version == HardwareVersion.XboxOneX_Retail) |
| | 107 | | { |
| | 108 | | return 1; |
| | 109 | | } |
| | 110 | | return 0; |
| | 111 | | #elif UNITY_PS4 && !UNITY_EDITOR |
| | 112 | | return 1; |
| | 113 | | #else |
| 0 | 114 | | return 0; |
| | 115 | | #endif // UNITY_XBOXONE && !UNITY_EDITOR |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | /// <summary> |
| | 119 | | /// Returns a runtime writable folder. |
| | 120 | | /// </summary> |
| | 121 | | /// <param name="folderName">An optional additional folder under the provided path that will be created if neces |
| | 122 | | /// <returns>The full path to a writable folder at runtime.</returns> |
| | 123 | | /// <remarks> |
| | 124 | | /// Depending on the platform, different routes are taken to finding a writable folder. |
| | 125 | | /// <list type="table"> |
| | 126 | | /// <item> |
| | 127 | | /// <term>Editor</term> |
| | 128 | | /// <description>The project's root folder is used in this case.</description> |
| | 129 | | /// </item> |
| | 130 | | /// <item> |
| | 131 | | /// <term>Standard Player</term> |
| | 132 | | /// <description>Utilizes <see cref="Application.persistentDataPath" /> to find a suitable place.</d |
| | 133 | | /// </item> |
| | 134 | | /// <item> |
| | 135 | | /// <term>DOTS Runtime</term> |
| | 136 | | /// <description>Uses <see cref="Directory.GetCurrentDirectory()" />.</description> |
| | 137 | | /// </item> |
| | 138 | | /// </list> |
| | 139 | | /// The path can be overridden by assigning GDX_OUTPUT_FOLDER in the launching arguments. |
| | 140 | | /// </remarks> |
| | 141 | | public static string GetOutputFolder(string folderName = null) |
| 20 | 142 | | { |
| 20 | 143 | | if (s_OutputFolder != null && string.IsNullOrEmpty(folderName)) |
| 14 | 144 | | { |
| 14 | 145 | | return s_OutputFolder; |
| | 146 | | } |
| | 147 | |
|
| 6 | 148 | | if (s_OutputFolder == null) |
| 2 | 149 | | { |
| 2 | 150 | | if (CommandLineParser.Arguments.ContainsKey("GDX_OUTPUT_FOLDER")) |
| 0 | 151 | | { |
| | 152 | | // Assign and remove quotes |
| 0 | 153 | | s_OutputFolder = CommandLineParser.Arguments["GDX_OUTPUT_FOLDER"] |
| | 154 | | .Replace("\"", ""); |
| 0 | 155 | | } |
| | 156 | | else |
| 2 | 157 | | { |
| 2 | 158 | | s_OutputFolder = |
| | 159 | | #if UNITY_EDITOR |
| | 160 | | Path.Combine(Application.dataPath, ".."); |
| | 161 | | #elif UNITY_DOTSRUNTIME |
| | 162 | | Directory.GetCurrentDirectory(); |
| | 163 | | #else |
| | 164 | | Application.persistentDataPath; |
| | 165 | | #endif // UNITY_EDITOR |
| 2 | 166 | | } |
| | 167 | |
|
| | 168 | | // Cleanup the folder pathing |
| 2 | 169 | | s_OutputFolder = Path.GetFullPath(s_OutputFolder); |
| | 170 | |
|
| | 171 | | // Ensure that it is created |
| 2 | 172 | | EnsureFolderHierarchyExists(s_OutputFolder); |
| 2 | 173 | | } |
| | 174 | |
|
| 6 | 175 | | if (string.IsNullOrEmpty(folderName)) |
| 2 | 176 | | { |
| 2 | 177 | | return s_OutputFolder; |
| | 178 | | } |
| | 179 | |
|
| 4 | 180 | | string fullPath = Path.Combine(s_OutputFolder, folderName); |
| 4 | 181 | | EnsureFolderHierarchyExists(fullPath); |
| 4 | 182 | | return fullPath; |
| 20 | 183 | | } |
| | 184 | |
|
| | 185 | | public static char GetRandomSafeCharacter(IRandomProvider random) |
| 24 | 186 | | { |
| 24 | 187 | | return SafeCharacterPool[random.NextInteger(0, CharacterPoolLengthExclusive)]; |
| 24 | 188 | | } |
| | 189 | |
|
| | 190 | | public static string GetUniqueOutputFilePath(string prefix = "GDX_", string extension = ".log", |
| | 191 | | string folderName = null) |
| 4 | 192 | | { |
| 4 | 193 | | string tempFolder = GetOutputFolder(folderName); |
| 4 | 194 | | StringBuilder tmpFileName = new StringBuilder(260); |
| 4 | 195 | | tmpFileName.Append(prefix); |
| 4 | 196 | | RandomWrapper random = new RandomWrapper( |
| | 197 | | DateTime.Now.Ticks.ToString().GetStableHashCode()); |
| | 198 | |
|
| 4 | 199 | | tmpFileName.Append(GetRandomSafeCharacter(random)); |
| 4 | 200 | | tmpFileName.Append(GetRandomSafeCharacter(random)); |
| 4 | 201 | | tmpFileName.Append(GetRandomSafeCharacter(random)); |
| 4 | 202 | | tmpFileName.Append(GetRandomSafeCharacter(random)); |
| 4 | 203 | | tmpFileName.Append(GetRandomSafeCharacter(random)); |
| | 204 | |
|
| 4 | 205 | | while (true) |
| 4 | 206 | | { |
| 4 | 207 | | tmpFileName.Append(GetRandomSafeCharacter(random)); |
| 4 | 208 | | string filePath = Path.Combine(tempFolder, $"{tmpFileName}{extension}"); |
| 4 | 209 | | if (!File.Exists(filePath)) |
| 4 | 210 | | { |
| 4 | 211 | | return filePath; |
| | 212 | | } |
| | 213 | |
|
| 0 | 214 | | if (tmpFileName.Length <= 260) |
| 0 | 215 | | { |
| 0 | 216 | | continue; |
| | 217 | | } |
| | 218 | |
|
| 0 | 219 | | tmpFileName.Clear(); |
| 0 | 220 | | tmpFileName.Append(prefix); |
| 0 | 221 | | tmpFileName.Append(GetRandomSafeCharacter(random)); |
| 0 | 222 | | tmpFileName.Append(GetRandomSafeCharacter(random)); |
| 0 | 223 | | tmpFileName.Append(GetRandomSafeCharacter(random)); |
| 0 | 224 | | tmpFileName.Append(GetRandomSafeCharacter(random)); |
| 0 | 225 | | tmpFileName.Append(GetRandomSafeCharacter(random)); |
| 0 | 226 | | } |
| 4 | 227 | | } |
| | 228 | |
|
| | 229 | | #if !UNITY_DOTSRUNTIME |
| | 230 | | /// <summary> |
| | 231 | | /// Is the application focused? |
| | 232 | | /// </summary> |
| | 233 | | /// <remarks> |
| | 234 | | /// There are issues on some platforms with getting an accurate reading. |
| | 235 | | /// </remarks> |
| | 236 | | /// <returns>true/false if the application has focus.</returns> |
| | 237 | | /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception> |
| | 238 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 239 | | public static bool IsFocused() |
| 0 | 240 | | { |
| | 241 | | #if UNITY_XBOXONE && !UNITY_EDITOR |
| | 242 | | return !XboxOnePLM.AmConstrained(); |
| | 243 | | #elif UNITY_PS4 && !UNITY_EDITOR |
| | 244 | | return true; |
| | 245 | | #else |
| 0 | 246 | | return Application.isFocused; |
| | 247 | | #endif // UNITY_XBOXONE && !UNITY_EDITOR |
| 0 | 248 | | } |
| | 249 | | #endif // !UNITY_DOTSRUNTIME |
| | 250 | |
|
| | 251 | | /// <summary> |
| | 252 | | /// Is it safe to write to the indicated <paramref name="filePath" />? |
| | 253 | | /// </summary> |
| | 254 | | /// <param name="filePath">The file path to check if it can be written.</param> |
| | 255 | | /// <returns>true/false if the path can be written too.</returns> |
| | 256 | | public static bool IsFileWritable(string filePath) |
| 0 | 257 | | { |
| 0 | 258 | | if (File.Exists(filePath)) |
| 0 | 259 | | { |
| 0 | 260 | | FileAttributes attributes = File.GetAttributes(filePath); |
| 0 | 261 | | if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly || |
| | 262 | | (attributes & FileAttributes.Offline) == FileAttributes.Offline) |
| 0 | 263 | | { |
| 0 | 264 | | return false; |
| | 265 | | } |
| 0 | 266 | | } |
| | 267 | |
|
| 0 | 268 | | return true; |
| 0 | 269 | | } |
| | 270 | |
|
| | 271 | | #if !UNITY_DOTSRUNTIME |
| | 272 | | /// <summary> |
| | 273 | | /// Is the application running in headless mode?. |
| | 274 | | /// </summary> |
| | 275 | | /// <remarks>Useful for detecting running a server.</remarks> |
| | 276 | | /// <returns>true/false if the application is without an initialized graphics device.</returns> |
| | 277 | | /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception> |
| | 278 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 279 | | public static bool IsHeadless() |
| 2 | 280 | | { |
| 2 | 281 | | return SystemInfo.graphicsDeviceType == GraphicsDeviceType.Null; |
| 2 | 282 | | } |
| | 283 | | #endif // !UNITY_DOTSRUNTIME |
| | 284 | | } |
| | 285 | | } |