From fd209c4b3f758a0e4f39f27b81c7288d78f60e62 Mon Sep 17 00:00:00 2001 From: ds5678 <49847914+ds5678@users.noreply.github.com> Date: Sun, 30 Mar 2025 11:25:03 -0700 Subject: [PATCH] Ensure empty scripts aren't created accidentally for generic types --- .../Scripts/EmptyScript.cs | 18 +++-------- .../Scripts/ScriptExportCollectionBase.cs | 3 +- .../MonoScriptExtensions.cs | 32 ++++++++++++++++++- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/Source/AssetRipper.Export.UnityProjects/Scripts/EmptyScript.cs b/Source/AssetRipper.Export.UnityProjects/Scripts/EmptyScript.cs index c68f5c0d1..d1b0a05c6 100644 --- a/Source/AssetRipper.Export.UnityProjects/Scripts/EmptyScript.cs +++ b/Source/AssetRipper.Export.UnityProjects/Scripts/EmptyScript.cs @@ -1,13 +1,10 @@ using AssetRipper.SourceGenerated.Classes.ClassID_115; -using System.Text.RegularExpressions; +using AssetRipper.SourceGenerated.Extensions; namespace AssetRipper.Export.UnityProjects.Scripts; -public static partial class EmptyScript +public static class EmptyScript { - [GeneratedRegex(@"^(\w+)`([1-9][0-9]*)$")] - private static partial Regex GenericRegex { get; } - public static string GetContent(IMonoScript script) { return GetContent(script.Namespace.String, script.ClassName_R.String); @@ -20,15 +17,10 @@ public static partial class EmptyScript public static string GetContent(string? @namespace, string name) { - Match match = GenericRegex.Match(name); - if (match.Success) + if (MonoScriptExtensions.IsGeneric(name, out string genericName, out int genericCount)) { - string genericName = match.Groups[1].Value; - if (int.TryParse(match.Groups[2].Value, out int genericCount)) - { - string genericParams = string.Join(", ", Enumerable.Range(1, genericCount).Select(i => $"T{i}")); - name = $"{genericName}<{genericParams}>"; - } + string genericParams = string.Join(", ", Enumerable.Range(1, genericCount).Select(i => $"T{i}")); + name = $"{genericName}<{genericParams}>"; } if (string.IsNullOrEmpty(@namespace)) { diff --git a/Source/AssetRipper.Export.UnityProjects/Scripts/ScriptExportCollectionBase.cs b/Source/AssetRipper.Export.UnityProjects/Scripts/ScriptExportCollectionBase.cs index 269d066ab..3a4a88455 100644 --- a/Source/AssetRipper.Export.UnityProjects/Scripts/ScriptExportCollectionBase.cs +++ b/Source/AssetRipper.Export.UnityProjects/Scripts/ScriptExportCollectionBase.cs @@ -3,6 +3,7 @@ using AssetRipper.Assets.Collections; using AssetRipper.Import.Structure.Assembly; using AssetRipper.SourceGenerated.Classes.ClassID_1035; using AssetRipper.SourceGenerated.Classes.ClassID_115; +using AssetRipper.SourceGenerated.Extensions; using System.Diagnostics; namespace AssetRipper.Export.UnityProjects.Scripts; @@ -81,7 +82,7 @@ public abstract class ScriptExportCollectionBase : ExportCollection protected static void GetExportSubPath(IMonoScript script, out string folderPath, out string fileName) { - GetExportSubPath(script.GetAssemblyNameFixed(), script.Namespace.String, script.ClassName_R.String, out folderPath, out fileName); + GetExportSubPath(script.GetAssemblyNameFixed(), script.Namespace.String, script.GetNonGenericClassName(), out folderPath, out fileName); } private protected static void GetExportSubPath(MonoScriptInfo script, out string folderPath, out string fileName) diff --git a/Source/AssetRipper.SourceGenerated.Extensions/MonoScriptExtensions.cs b/Source/AssetRipper.SourceGenerated.Extensions/MonoScriptExtensions.cs index 0300bcb49..b83dcfc6e 100644 --- a/Source/AssetRipper.SourceGenerated.Extensions/MonoScriptExtensions.cs +++ b/Source/AssetRipper.SourceGenerated.Extensions/MonoScriptExtensions.cs @@ -1,9 +1,13 @@ using AssetRipper.SourceGenerated.Classes.ClassID_115; +using System.Text.RegularExpressions; namespace AssetRipper.SourceGenerated.Extensions; -public static class MonoScriptExtensions +public static partial class MonoScriptExtensions { + [GeneratedRegex(@"^(\w+)`([1-9][0-9]*)$")] + private static partial Regex GenericRegex { get; } + public static bool IsFullNameEqual(this IMonoScript _this, IMonoScript other) { return _this.AssemblyName == other.AssemblyName @@ -15,4 +19,30 @@ public static class MonoScriptExtensions { return _this.Namespace == @namespace && _this.ClassName_R == name; } + + public static bool IsGeneric(this IMonoScript script, out string genericName, out int genericCount) + { + return IsGeneric(script.ClassName_R.String, out genericName, out genericCount); + } + + public static bool IsGeneric(string className, out string genericName, out int genericCount) + { + Match match = GenericRegex.Match(className); + if (match.Success) + { + genericName = match.Groups[1].Value; + if (int.TryParse(match.Groups[2].Value, out genericCount)) + { + return true; + } + } + genericName = className; + genericCount = 0; + return false; + } + + public static string GetNonGenericClassName(this IMonoScript script) + { + return IsGeneric(script, out string genericName, out _) ? genericName : script.ClassName_R.String; + } }