|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using UnityEditor; |
| 4 | +using UnityEngine; |
| 5 | + |
| 6 | +namespace GameLovers.Services.AddressableIds.Editor |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Editor-only project-level settings for the Addressable Ids Generator. |
| 10 | + /// Persisted to <c>ProjectSettings/AddressableIdsEditorSettings.asset</c> via <see cref="ScriptableSingleton{T}"/>. |
| 11 | + /// </summary> |
| 12 | + [FilePath("ProjectSettings/AddressableIdsEditorSettings.asset", FilePathAttribute.Location.ProjectFolder)] |
| 13 | + internal sealed class AddressableIdsEditorSettings : ScriptableSingleton<AddressableIdsEditorSettings> |
| 14 | + { |
| 15 | + [SerializeField] private string _scriptFilename = "AddressableId"; |
| 16 | + [SerializeField] private string _namespace = "Game.Ids"; |
| 17 | + [SerializeField] private string _addressableLabel = "GenerateIds"; |
| 18 | + |
| 19 | + // ---- Last-generation snapshot (persisted) ---- |
| 20 | + [SerializeField] private long _lastGenerationUtcTicks; |
| 21 | + [SerializeField] private int _lastGenerationIdCount; |
| 22 | + [SerializeField] private int _lastGenerationLabelCount; |
| 23 | + [SerializeField] private string _lastGenerationFilenameUsed; |
| 24 | + [SerializeField] private string _lastGenerationLabelFilterUsed; |
| 25 | + [SerializeField] private string[] _lastGenerationAddresses = Array.Empty<string>(); |
| 26 | + [SerializeField] private string[] _lastGenerationLabels = Array.Empty<string>(); |
| 27 | + |
| 28 | + /// <summary>Name of the generated C# file (without extension) and the enum/class it contains.</summary> |
| 29 | + public string ScriptFilename |
| 30 | + { |
| 31 | + get => string.IsNullOrWhiteSpace(_scriptFilename) ? "AddressableId" : _scriptFilename; |
| 32 | + set |
| 33 | + { |
| 34 | + var trimmed = (value ?? "AddressableId").Trim(); |
| 35 | + if (_scriptFilename == trimmed) |
| 36 | + { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + _scriptFilename = trimmed; |
| 41 | + Save(true); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary>C# namespace for the generated file.</summary> |
| 46 | + public string Namespace |
| 47 | + { |
| 48 | + get => string.IsNullOrWhiteSpace(_namespace) ? "Game.Ids" : _namespace; |
| 49 | + set |
| 50 | + { |
| 51 | + var trimmed = (value ?? "Game.Ids").Trim(); |
| 52 | + if (_namespace == trimmed) |
| 53 | + { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + _namespace = trimmed; |
| 58 | + Save(true); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary>Addressables label used to filter which assets get Ids generated. Empty = generate all.</summary> |
| 63 | + public string AddressableLabel |
| 64 | + { |
| 65 | + get => _addressableLabel ?? ""; |
| 66 | + set |
| 67 | + { |
| 68 | + var trimmed = (value ?? "").Trim(); |
| 69 | + if (_addressableLabel == trimmed) |
| 70 | + { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + _addressableLabel = trimmed; |
| 75 | + Save(true); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // ---- Last-generation snapshot accessors ---- |
| 80 | + |
| 81 | + /// <summary>True when a generation snapshot has been recorded by <see cref="RecordGeneration"/>.</summary> |
| 82 | + public bool HasSnapshot => _lastGenerationUtcTicks != 0L; |
| 83 | + |
| 84 | + /// <summary>UTC timestamp of the last successful generation, or <c>default(DateTime)</c> when none.</summary> |
| 85 | + public DateTime LastGenerationUtc => _lastGenerationUtcTicks == 0L |
| 86 | + ? default |
| 87 | + : new DateTime(_lastGenerationUtcTicks, DateTimeKind.Utc); |
| 88 | + |
| 89 | + public int LastGenerationIdCount => _lastGenerationIdCount; |
| 90 | + public int LastGenerationLabelCount => _lastGenerationLabelCount; |
| 91 | + public string LastGenerationFilenameUsed => _lastGenerationFilenameUsed ?? string.Empty; |
| 92 | + public string LastGenerationLabelFilterUsed => _lastGenerationLabelFilterUsed ?? string.Empty; |
| 93 | + |
| 94 | + /// <summary>Sorted list of addressable addresses that were emitted in the last generation. Empty array when no snapshot.</summary> |
| 95 | + public IReadOnlyList<string> LastGenerationAddresses => _lastGenerationAddresses ?? Array.Empty<string>(); |
| 96 | + |
| 97 | + /// <summary>Sorted list of addressable labels that were emitted in the last generation. Empty array when no snapshot.</summary> |
| 98 | + public IReadOnlyList<string> LastGenerationLabels => _lastGenerationLabels ?? Array.Empty<string>(); |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Records the snapshot of the last successful generation: addresses, labels, and the |
| 102 | + /// generator settings (filename, label filter) that were used at that moment. Both lists are |
| 103 | + /// stored sorted so subsequent set-diffs can be done in O(n+m) without re-sorting at read time. |
| 104 | + /// Persists immediately via <c>Save(true)</c>. |
| 105 | + /// </summary> |
| 106 | + internal void RecordGeneration(IReadOnlyList<string> addresses, IReadOnlyList<string> labels) |
| 107 | + { |
| 108 | + _lastGenerationUtcTicks = DateTime.UtcNow.Ticks; |
| 109 | + _lastGenerationIdCount = addresses?.Count ?? 0; |
| 110 | + _lastGenerationLabelCount = labels?.Count ?? 0; |
| 111 | + _lastGenerationFilenameUsed = ScriptFilename; |
| 112 | + _lastGenerationLabelFilterUsed = AddressableLabel; |
| 113 | + |
| 114 | + _lastGenerationAddresses = SortedCopy(addresses); |
| 115 | + _lastGenerationLabels = SortedCopy(labels); |
| 116 | + |
| 117 | + Save(true); |
| 118 | + } |
| 119 | + |
| 120 | + private static string[] SortedCopy(IReadOnlyList<string> source) |
| 121 | + { |
| 122 | + if (source == null || source.Count == 0) |
| 123 | + { |
| 124 | + return Array.Empty<string>(); |
| 125 | + } |
| 126 | + |
| 127 | + var copy = new string[source.Count]; |
| 128 | + |
| 129 | + for (var i = 0; i < source.Count; i++) |
| 130 | + { |
| 131 | + copy[i] = source[i]; |
| 132 | + } |
| 133 | + |
| 134 | + Array.Sort(copy, StringComparer.Ordinal); |
| 135 | + return copy; |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Validates <paramref name="identifier"/> for use as a C# script filename / enum name. |
| 140 | + /// Returns <c>true</c> when valid; populates <paramref name="error"/> on failure. |
| 141 | + /// </summary> |
| 142 | + public static bool IsValidIdentifier(string identifier, out string error) |
| 143 | + { |
| 144 | + error = null; |
| 145 | + |
| 146 | + if (string.IsNullOrWhiteSpace(identifier)) |
| 147 | + { |
| 148 | + error = "Identifier cannot be empty."; |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + var trimmed = identifier.Trim(); |
| 153 | + |
| 154 | + if (char.IsDigit(trimmed[0])) |
| 155 | + { |
| 156 | + error = "Identifier must not start with a digit."; |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + foreach (var c in trimmed) |
| 161 | + { |
| 162 | + if (!char.IsLetterOrDigit(c) && c != '_') |
| 163 | + { |
| 164 | + error = $"Identifier contains invalid character '{c}'. Only letters, digits, and underscores are allowed."; |
| 165 | + return false; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + return true; |
| 170 | + } |
| 171 | + |
| 172 | + /// <summary> |
| 173 | + /// Validates <paramref name="ns"/> as a C# namespace string (dot-separated identifiers). |
| 174 | + /// Returns <c>true</c> when valid; populates <paramref name="error"/> on failure. |
| 175 | + /// </summary> |
| 176 | + public static bool IsValidNamespace(string ns, out string error) |
| 177 | + { |
| 178 | + error = null; |
| 179 | + |
| 180 | + if (string.IsNullOrWhiteSpace(ns)) |
| 181 | + { |
| 182 | + error = "Namespace cannot be empty."; |
| 183 | + return false; |
| 184 | + } |
| 185 | + |
| 186 | + var segments = ns.Trim().Split('.'); |
| 187 | + |
| 188 | + foreach (var segment in segments) |
| 189 | + { |
| 190 | + if (string.IsNullOrEmpty(segment)) |
| 191 | + { |
| 192 | + error = "Namespace must not contain consecutive dots or trailing dots."; |
| 193 | + return false; |
| 194 | + } |
| 195 | + |
| 196 | + if (!IsValidIdentifier(segment, out var segmentError)) |
| 197 | + { |
| 198 | + error = $"Namespace segment \"{segment}\" is invalid: {segmentError}"; |
| 199 | + return false; |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + return true; |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments