| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Text; |
| | | 4 | | using System.Threading; |
| | | 5 | | using Microsoft.CodeAnalysis; |
| | | 6 | | using Microsoft.CodeAnalysis.Text; |
| | | 7 | | using SourceProductionContext = Microsoft.CodeAnalysis.SourceProductionContext; |
| | | 8 | | using SymbolDisplayFormat = Microsoft.CodeAnalysis.SymbolDisplayFormat; |
| | | 9 | | |
| | | 10 | | namespace Hertzole.ScriptableValues.Generator; |
| | | 11 | | |
| | | 12 | | partial class ScriptableCallbackGenerator |
| | | 13 | | { |
| | | 14 | | private static void GenerateCode(SourceProductionContext context, (HierarchyInfo Key, EquatableArray<CallbackData> E |
| | | 15 | | { |
| | 25 | 16 | | if (!ScriptableValueHelper.DoesTypeSupportCallbacks((ITypeSymbol) item.Key.Symbol, out _)) |
| | | 17 | | { |
| | 1 | 18 | | return; |
| | | 19 | | } |
| | | 20 | | |
| | 24 | 21 | | CodeWriter writer = new CodeWriter(); |
| | 24 | 22 | | CancellationToken cancellationToken = context.CancellationToken; |
| | | 23 | | |
| | | 24 | | try |
| | | 25 | | { |
| | 24 | 26 | | writer.AppendFileHeader(); |
| | 24 | 27 | | writer.AppendNamespace(item.Key.Namespace); |
| | | 28 | | |
| | 24 | 29 | | int nestLevel = 0; |
| | 24 | 30 | | INamedTypeSymbol? containingType = item.Key.Symbol.ContainingType; |
| | 24 | 31 | | while (containingType != null && !containingType.IsNamespace) |
| | | 32 | | { |
| | 0 | 33 | | writer.Append("partial "); |
| | 0 | 34 | | writer.Append(containingType.IsValueType ? "struct" : "class"); |
| | 0 | 35 | | writer.AppendLine(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)); |
| | 0 | 36 | | writer.AppendLine("{"); |
| | 0 | 37 | | writer.Indent++; |
| | | 38 | | |
| | 0 | 39 | | nestLevel++; |
| | 0 | 40 | | containingType = containingType.ContainingType; |
| | | 41 | | } |
| | | 42 | | |
| | 24 | 43 | | writer.Append("partial "); |
| | | 44 | | |
| | 24 | 45 | | writer.Append(item.Key.IsStruct ? "struct " : "class "); |
| | | 46 | | |
| | 24 | 47 | | writer.AppendLine(item.Key.TypeName); |
| | 24 | 48 | | writer.AppendLine("{"); |
| | 24 | 49 | | writer.Indent++; |
| | | 50 | | |
| | 24 | 51 | | WriteElements(writer, in item.Key, in item.Elements, in cancellationToken); |
| | | 52 | | |
| | 24 | 53 | | writer.Indent--; |
| | 24 | 54 | | writer.Append("}"); |
| | | 55 | | |
| | 48 | 56 | | for (int i = 0; i < nestLevel; i++) |
| | | 57 | | { |
| | 0 | 58 | | writer.AppendLine(); |
| | 0 | 59 | | writer.Indent--; |
| | 0 | 60 | | writer.Append("}"); |
| | | 61 | | } |
| | 24 | 62 | | } |
| | 0 | 63 | | catch (Exception e) |
| | | 64 | | { |
| | 0 | 65 | | writer.Clear(); |
| | 0 | 66 | | writer.AppendLine("// Error generating code:"); |
| | 0 | 67 | | writer.AppendLine("// " + e.Message); |
| | 0 | 68 | | writer.AppendLine("// " + e.StackTrace); |
| | 0 | 69 | | } |
| | | 70 | | |
| | 24 | 71 | | context.AddSource($"{item.Key.FilenameHint}.g.cs", SourceText.From(writer.ToString(), Encoding.UTF8)); |
| | 24 | 72 | | } |
| | | 73 | | |
| | | 74 | | private static void WriteElements(CodeWriter writer, |
| | | 75 | | in HierarchyInfo hierarchy, |
| | | 76 | | in EquatableArray<CallbackData> elements, |
| | | 77 | | in CancellationToken cancellationToken) |
| | | 78 | | { |
| | 24 | 79 | | WriteSubscribedBitMask(writer, in hierarchy, in elements, in cancellationToken); |
| | 24 | 80 | | writer.AppendLine(); |
| | | 81 | | |
| | 24 | 82 | | WriteSubscribeAndUnsubscribeMethods(in writer, in hierarchy, in elements, in cancellationToken); |
| | 24 | 83 | | writer.AppendLine(); |
| | | 84 | | |
| | 24 | 85 | | WriteCallbacksMethods(in writer, in elements, in cancellationToken); |
| | 24 | 86 | | } |
| | | 87 | | |
| | | 88 | | private static void WriteSubscribedBitMask(CodeWriter writer, |
| | | 89 | | in HierarchyInfo hierarchy, |
| | | 90 | | in EquatableArray<CallbackData> data, |
| | | 91 | | in CancellationToken cancellationToken) |
| | | 92 | | { |
| | 24 | 93 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 94 | | |
| | 24 | 95 | | writer.AppendLine(DocumentationHelper.SUBSCRIBED_MASK_SUMMARY); |
| | 24 | 96 | | using (writer.WithIndent(0)) |
| | | 97 | | { |
| | 24 | 98 | | writer.AppendLine("#if UNITY_EDITOR"); |
| | 24 | 99 | | } |
| | | 100 | | |
| | 24 | 101 | | writer.AppendLine("[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableS |
| | 24 | 102 | | WriteGeneratedCodeAttribute(writer); |
| | 24 | 103 | | using (writer.WithIndent(0)) |
| | | 104 | | { |
| | 24 | 105 | | writer.AppendLine("#endif // UNITY_EDITOR"); |
| | 24 | 106 | | } |
| | | 107 | | |
| | 24 | 108 | | WriteSubscribedEnumMask(in writer, in data, in hierarchy, in cancellationToken); |
| | | 109 | | |
| | 24 | 110 | | writer.AppendLine(); |
| | 24 | 111 | | writer.AppendLine("/// <summary>The current mask of all subscribed callbacks.</summary>"); |
| | 24 | 112 | | using (writer.WithIndent(0)) |
| | | 113 | | { |
| | 24 | 114 | | writer.AppendLine("#if UNITY_EDITOR"); |
| | 24 | 115 | | } |
| | | 116 | | |
| | 24 | 117 | | writer.AppendLine("[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableS |
| | 24 | 118 | | WriteGeneratedCodeAttribute(writer); |
| | 24 | 119 | | using (writer.WithIndent(0)) |
| | | 120 | | { |
| | 24 | 121 | | writer.AppendLine("#endif // UNITY_EDITOR"); |
| | 24 | 122 | | } |
| | | 123 | | |
| | 24 | 124 | | writer.Append("private "); |
| | 24 | 125 | | AppendSubscribeMaskTypeName(in writer, in hierarchy); |
| | 24 | 126 | | writer.Append(' '); |
| | 24 | 127 | | AppendSubscribeMaskFieldName(in writer, in hierarchy); |
| | | 128 | | |
| | 24 | 129 | | if (!hierarchy.IsStruct) |
| | | 130 | | { |
| | 12 | 131 | | writer.Append(" = "); |
| | 12 | 132 | | AppendSubscribeMaskTypeName(in writer, in hierarchy); |
| | 12 | 133 | | writer.Append(".None"); |
| | | 134 | | } |
| | | 135 | | |
| | 24 | 136 | | writer.AppendLine(";"); |
| | 24 | 137 | | } |
| | | 138 | | |
| | | 139 | | internal static void WriteSubscribedEnumMask(in CodeWriter writer, |
| | | 140 | | in EquatableArray<CallbackData> data, |
| | | 141 | | in HierarchyInfo hierarchy, |
| | | 142 | | in CancellationToken cancellationToken) |
| | | 143 | | { |
| | 35 | 144 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 145 | | |
| | 35 | 146 | | writer.Append("private enum "); |
| | 35 | 147 | | AppendSubscribeMaskTypeName(in writer, in hierarchy); |
| | 35 | 148 | | writer.Append(" : "); |
| | 35 | 149 | | if (data.Length <= 8) |
| | | 150 | | { |
| | 26 | 151 | | writer.AppendLine("byte"); |
| | | 152 | | } |
| | 9 | 153 | | else if (data.Length <= 16) |
| | | 154 | | { |
| | 3 | 155 | | writer.AppendLine("ushort"); |
| | | 156 | | } |
| | 6 | 157 | | else if (data.Length <= 32) |
| | | 158 | | { |
| | 3 | 159 | | writer.AppendLine("uint"); |
| | | 160 | | } |
| | | 161 | | else |
| | | 162 | | { |
| | 3 | 163 | | writer.AppendLine("ulong"); |
| | | 164 | | } |
| | | 165 | | |
| | 35 | 166 | | writer.AppendLine("{"); |
| | 35 | 167 | | writer.Indent++; |
| | | 168 | | |
| | 35 | 169 | | writer.Append("None = 0"); |
| | | 170 | | |
| | 708 | 171 | | for (int i = 0; i < data.Length; i++) |
| | | 172 | | { |
| | 319 | 173 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 174 | | |
| | 319 | 175 | | writer.AppendLine(","); |
| | 319 | 176 | | writer.Append(data[i].MaskName); |
| | 319 | 177 | | writer.Append(" = 1 << "); |
| | 319 | 178 | | writer.Append(i); |
| | | 179 | | } |
| | | 180 | | |
| | 35 | 181 | | writer.AppendLine(); |
| | | 182 | | |
| | 35 | 183 | | writer.Indent--; |
| | 35 | 184 | | writer.AppendLine("}"); |
| | 35 | 185 | | } |
| | | 186 | | |
| | | 187 | | private static void WriteCachedFields(CodeWriter writer, |
| | | 188 | | in SourceProductionContext context, |
| | | 189 | | in HierarchyInfo hierarchy, |
| | | 190 | | in EquatableArray<CallbackData> data) |
| | | 191 | | { |
| | 0 | 192 | | for (int i = 0; i < data.Length; i++) |
| | | 193 | | { |
| | 0 | 194 | | context.CancellationToken.ThrowIfCancellationRequested(); |
| | | 195 | | |
| | 0 | 196 | | WriteField(writer, in hierarchy, in data[i]); |
| | | 197 | | } |
| | | 198 | | |
| | 0 | 199 | | return; |
| | | 200 | | |
| | | 201 | | static void WriteField(CodeWriter writer, in HierarchyInfo hierarchy, in CallbackData data) |
| | | 202 | | { |
| | 0 | 203 | | writer.Append("/// <summary>Cached callback for when <see cref=\""); |
| | 0 | 204 | | writer.Append(data.Name); |
| | 0 | 205 | | writer.Append("\" /> is "); |
| | 0 | 206 | | if (data.CallbackType == CallbackType.Event) |
| | | 207 | | { |
| | 0 | 208 | | writer.Append("invoked"); |
| | | 209 | | } |
| | 0 | 210 | | else if (data.CallbackType == CallbackType.Value && (data.Flags & CallbackFlags.PreInvoke) != 0) |
| | | 211 | | { |
| | 0 | 212 | | writer.Append("changing"); |
| | | 213 | | } |
| | | 214 | | else |
| | | 215 | | { |
| | 0 | 216 | | writer.Append("changed"); |
| | | 217 | | } |
| | | 218 | | |
| | 0 | 219 | | writer.AppendLine(".</summary>"); |
| | | 220 | | |
| | 0 | 221 | | switch (data.CallbackType) |
| | | 222 | | { |
| | | 223 | | case CallbackType.Value: |
| | 0 | 224 | | WriteValueField(in writer, in hierarchy, in data); |
| | 0 | 225 | | break; |
| | | 226 | | case CallbackType.Event: |
| | 0 | 227 | | WriteEventField(in writer, in hierarchy, in data); |
| | 0 | 228 | | break; |
| | | 229 | | case CallbackType.Pool: |
| | 0 | 230 | | WritePoolField(in writer, in hierarchy, in data); |
| | 0 | 231 | | break; |
| | | 232 | | case CallbackType.Collection: |
| | 0 | 233 | | WriteCollectionField(in writer, in hierarchy, in data); |
| | | 234 | | break; |
| | | 235 | | } |
| | 0 | 236 | | } |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | private static void WriteSubscribeAndUnsubscribeMethods(in CodeWriter writer, |
| | | 240 | | in HierarchyInfo hierarchy, |
| | | 241 | | in EquatableArray<CallbackData> elements, |
| | | 242 | | in CancellationToken cancellationToken) |
| | | 243 | | { |
| | 24 | 244 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 245 | | |
| | 24 | 246 | | writer.AppendLine("/// <summary>Subscribes to all scriptable callbacks.</summary>"); |
| | | 247 | | |
| | 24 | 248 | | WriteGeneratedCodeAttribute(in writer, true); |
| | 24 | 249 | | writer.AppendExcludeFromCodeCoverageAttribute(); |
| | | 250 | | |
| | | 251 | | // If it should be inherited, it should be protected override. |
| | | 252 | | // If it should not be inherited, and is sealed, it should be private. |
| | | 253 | | // If it should not be inherited, and is not sealed, it should be protected virtual. |
| | 24 | 254 | | if (hierarchy.ShouldInherit) |
| | | 255 | | { |
| | 0 | 256 | | writer.Append("protected override "); |
| | | 257 | | } |
| | | 258 | | else |
| | | 259 | | { |
| | 24 | 260 | | writer.Append(hierarchy.IsSealed ? "private " : "protected virtual "); |
| | | 261 | | } |
| | | 262 | | |
| | 24 | 263 | | writer.AppendLine("void SubscribeToAllScriptableCallbacks()"); |
| | 24 | 264 | | writer.AppendLine("{"); |
| | 24 | 265 | | writer.Indent++; |
| | | 266 | | |
| | 24 | 267 | | if (hierarchy.ShouldInherit) |
| | | 268 | | { |
| | 0 | 269 | | writer.AppendLine("base.SubscribeToAllScriptableCallbacks();"); |
| | | 270 | | } |
| | | 271 | | |
| | 96 | 272 | | for (int i = 0; i < elements.Length; i++) |
| | | 273 | | { |
| | 24 | 274 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 24 | 275 | | WriteIfCheck(in writer, in elements[i], in hierarchy, true); |
| | | 276 | | } |
| | | 277 | | |
| | 24 | 278 | | writer.Indent--; |
| | 24 | 279 | | writer.AppendLine("}"); |
| | 24 | 280 | | writer.AppendLine(); |
| | | 281 | | |
| | 24 | 282 | | writer.AppendLine("/// <summary>Unsubscribes from all scriptable callbacks.</summary>"); |
| | | 283 | | |
| | 24 | 284 | | WriteGeneratedCodeAttribute(in writer, true); |
| | 24 | 285 | | writer.AppendExcludeFromCodeCoverageAttribute(); |
| | | 286 | | |
| | | 287 | | // If it should be inherited, it should be protected override. |
| | | 288 | | // If it should not be inherited, and is sealed, it should be private. |
| | | 289 | | // If it should not be inherited, and is not sealed, it should be protected virtual. |
| | 24 | 290 | | if (hierarchy.ShouldInherit) |
| | | 291 | | { |
| | 0 | 292 | | writer.Append("protected override "); |
| | | 293 | | } |
| | | 294 | | else |
| | | 295 | | { |
| | 24 | 296 | | writer.Append(hierarchy.IsSealed ? "private " : "protected virtual "); |
| | | 297 | | } |
| | | 298 | | |
| | 24 | 299 | | writer.AppendLine("void UnsubscribeFromAllScriptableCallbacks()"); |
| | 24 | 300 | | writer.AppendLine("{"); |
| | 24 | 301 | | writer.Indent++; |
| | | 302 | | |
| | 24 | 303 | | if (hierarchy.ShouldInherit) |
| | | 304 | | { |
| | 0 | 305 | | writer.AppendLine("base.UnsubscribeFromAllScriptableCallbacks();"); |
| | | 306 | | } |
| | | 307 | | |
| | 96 | 308 | | for (int i = 0; i < elements.Length; i++) |
| | | 309 | | { |
| | 24 | 310 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 24 | 311 | | WriteIfCheck(in writer, in elements[i], in hierarchy, false); |
| | | 312 | | } |
| | | 313 | | |
| | 24 | 314 | | writer.Indent--; |
| | 24 | 315 | | writer.AppendLine("}"); |
| | 24 | 316 | | return; |
| | | 317 | | |
| | | 318 | | static void WriteIfCheck(in CodeWriter writer, |
| | | 319 | | in CallbackData data, |
| | | 320 | | in HierarchyInfo hierarchy, |
| | | 321 | | bool subscribe) |
| | | 322 | | { |
| | 48 | 323 | | writer.Append("if (("); |
| | 48 | 324 | | AppendSubscribeMaskFieldName(in writer, in hierarchy); |
| | 48 | 325 | | writer.Append(" & "); |
| | 48 | 326 | | AppendSubscribeMaskTypeName(in writer, in hierarchy); |
| | 48 | 327 | | writer.Append('.'); |
| | 48 | 328 | | writer.Append(data.MaskName); |
| | 48 | 329 | | writer.Append(") "); |
| | 48 | 330 | | writer.Append(subscribe ? "== 0" : "!= 0"); |
| | 48 | 331 | | writer.AppendLine(")"); |
| | 48 | 332 | | writer.AppendLine("{"); |
| | 48 | 333 | | writer.Indent++; |
| | | 334 | | |
| | 48 | 335 | | writer.Append(data.Name); |
| | 48 | 336 | | writer.Append('.'); |
| | 48 | 337 | | writer.Append(data.TargetEvent); |
| | 48 | 338 | | writer.Append(subscribe ? " += " : " -= "); |
| | 48 | 339 | | writer.Append(data.CallbackName); |
| | 48 | 340 | | writer.AppendLine(";"); |
| | | 341 | | |
| | 48 | 342 | | AppendSubscribeMaskFieldName(in writer, in hierarchy); |
| | | 343 | | |
| | 48 | 344 | | writer.Append(subscribe ? " |= " : " &= ~"); |
| | 48 | 345 | | AppendSubscribeMaskTypeName(in writer, in hierarchy); |
| | 48 | 346 | | writer.Append('.'); |
| | | 347 | | |
| | 48 | 348 | | writer.Append(data.MaskName); |
| | 48 | 349 | | writer.AppendLine(";"); |
| | | 350 | | |
| | 48 | 351 | | writer.Indent--; |
| | 48 | 352 | | writer.AppendLine("}"); |
| | 48 | 353 | | } |
| | | 354 | | } |
| | | 355 | | |
| | | 356 | | private static void WriteCallbacksMethods(in CodeWriter writer, in EquatableArray<CallbackData> elements, in Cancell |
| | | 357 | | { |
| | 24 | 358 | | ArrayBuilder<(string name, string type)> parametersBuilder = new ArrayBuilder<(string name, string type)>(2); |
| | 24 | 359 | | ArrayBuilder<(string name, string description)> descriptionsBuilder = new ArrayBuilder<(string name, string desc |
| | 24 | 360 | | HashSet<string> usedCallbacks = new HashSet<string>(StringComparer.Ordinal); |
| | | 361 | | |
| | | 362 | | try |
| | | 363 | | { |
| | 96 | 364 | | for (int i = 0; i < elements.Length; i++) |
| | | 365 | | { |
| | 24 | 366 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 367 | | |
| | 24 | 368 | | if (usedCallbacks.Contains(elements[i].CallbackName)) |
| | | 369 | | { |
| | | 370 | | continue; |
| | | 371 | | } |
| | | 372 | | |
| | 24 | 373 | | parametersBuilder.Clear(); |
| | 24 | 374 | | descriptionsBuilder.Clear(); |
| | | 375 | | |
| | 24 | 376 | | if (i > 0) |
| | | 377 | | { |
| | 0 | 378 | | writer.AppendLine(); |
| | | 379 | | } |
| | | 380 | | |
| | 24 | 381 | | elements[i].AppendParameterTypes(in parametersBuilder); |
| | 24 | 382 | | elements[i].AppendParameterDescriptions(in descriptionsBuilder); |
| | | 383 | | |
| | 24 | 384 | | WriteCallbackMethod(in writer, in elements[i], elements[i].CallbackName.AsSpan(), parametersBuilder.AsSp |
| | 24 | 385 | | usedCallbacks.Add(elements[i].CallbackName); |
| | | 386 | | } |
| | 24 | 387 | | } |
| | | 388 | | finally |
| | | 389 | | { |
| | 24 | 390 | | parametersBuilder.Dispose(); |
| | 24 | 391 | | descriptionsBuilder.Dispose(); |
| | 24 | 392 | | } |
| | | 393 | | |
| | 24 | 394 | | return; |
| | | 395 | | |
| | | 396 | | static void WriteCallbackMethod(in CodeWriter writer, |
| | | 397 | | in CallbackData data, |
| | | 398 | | in ReadOnlySpan<char> name, |
| | | 399 | | in ReadOnlySpan<(string name, string type)> parameters, |
| | | 400 | | in ReadOnlySpan<(string argName, string description)> parameterDescriptions) |
| | | 401 | | { |
| | 24 | 402 | | writer.AppendLine(DocumentationHelper.GetMethodCallbackDescription(data.Name.AsSpan(), data.CallbackType, da |
| | | 403 | | |
| | 144 | 404 | | for (int i = 0; i < parameterDescriptions.Length; i++) |
| | | 405 | | { |
| | 48 | 406 | | writer.Append("/// <param name=\""); |
| | 48 | 407 | | writer.Append(parameterDescriptions[i].argName); |
| | 48 | 408 | | writer.Append("\">"); |
| | 48 | 409 | | writer.Append(parameterDescriptions[i].description); |
| | 48 | 410 | | writer.AppendLine("</param>"); |
| | | 411 | | } |
| | | 412 | | |
| | 24 | 413 | | writer.Append("private partial void "); |
| | 24 | 414 | | writer.Append(name); |
| | 24 | 415 | | writer.Append("("); |
| | | 416 | | |
| | 144 | 417 | | for (int i = 0; i < parameters.Length; i++) |
| | | 418 | | { |
| | 48 | 419 | | if (i > 0) |
| | | 420 | | { |
| | 24 | 421 | | writer.Append(", "); |
| | | 422 | | } |
| | | 423 | | |
| | 48 | 424 | | writer.Append(parameters[i].type); |
| | 48 | 425 | | writer.Append(' '); |
| | 48 | 426 | | writer.Append(parameters[i].name); |
| | | 427 | | } |
| | | 428 | | |
| | 24 | 429 | | writer.AppendLine(");"); |
| | 24 | 430 | | } |
| | | 431 | | } |
| | | 432 | | |
| | | 433 | | internal static void AppendSubscribeMaskFieldName(in CodeWriter builder, in HierarchyInfo hierarchy) |
| | | 434 | | { |
| | 120 | 435 | | builder.Append("subscribedCallbacks"); |
| | | 436 | | |
| | 120 | 437 | | if (hierarchy.ShouldInherit) |
| | | 438 | | { |
| | 0 | 439 | | builder.Append(hierarchy.ParentCount); |
| | | 440 | | } |
| | 120 | 441 | | } |
| | | 442 | | |
| | | 443 | | internal static void AppendSubscribeMaskTypeName(in CodeWriter builder, in HierarchyInfo hierarchy) |
| | | 444 | | { |
| | 167 | 445 | | builder.Append("SubscribedCallbacksMask"); |
| | | 446 | | |
| | 167 | 447 | | if (hierarchy.ShouldInherit) |
| | | 448 | | { |
| | 0 | 449 | | builder.Append(hierarchy.ParentCount); |
| | | 450 | | } |
| | 167 | 451 | | } |
| | | 452 | | } |