Ensure empty scripts aren't created accidentally for generic types

This commit is contained in:
ds5678 2025-03-30 11:25:03 -07:00
parent f3bc83b3cd
commit fd209c4b3f
3 changed files with 38 additions and 15 deletions

View File

@ -1,13 +1,10 @@
using AssetRipper.SourceGenerated.Classes.ClassID_115; using AssetRipper.SourceGenerated.Classes.ClassID_115;
using System.Text.RegularExpressions; using AssetRipper.SourceGenerated.Extensions;
namespace AssetRipper.Export.UnityProjects.Scripts; 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) public static string GetContent(IMonoScript script)
{ {
return GetContent(script.Namespace.String, script.ClassName_R.String); return GetContent(script.Namespace.String, script.ClassName_R.String);
@ -20,16 +17,11 @@ public static partial class EmptyScript
public static string GetContent(string? @namespace, string name) public static string GetContent(string? @namespace, string name)
{ {
Match match = GenericRegex.Match(name); if (MonoScriptExtensions.IsGeneric(name, out string genericName, out int genericCount))
if (match.Success)
{
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}")); string genericParams = string.Join(", ", Enumerable.Range(1, genericCount).Select(i => $"T{i}"));
name = $"{genericName}<{genericParams}>"; name = $"{genericName}<{genericParams}>";
} }
}
if (string.IsNullOrEmpty(@namespace)) if (string.IsNullOrEmpty(@namespace))
{ {
//Indented so that the numerical section can be easily copy-pasted when needed. //Indented so that the numerical section can be easily copy-pasted when needed.

View File

@ -3,6 +3,7 @@ using AssetRipper.Assets.Collections;
using AssetRipper.Import.Structure.Assembly; using AssetRipper.Import.Structure.Assembly;
using AssetRipper.SourceGenerated.Classes.ClassID_1035; using AssetRipper.SourceGenerated.Classes.ClassID_1035;
using AssetRipper.SourceGenerated.Classes.ClassID_115; using AssetRipper.SourceGenerated.Classes.ClassID_115;
using AssetRipper.SourceGenerated.Extensions;
using System.Diagnostics; using System.Diagnostics;
namespace AssetRipper.Export.UnityProjects.Scripts; 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) 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) private protected static void GetExportSubPath(MonoScriptInfo script, out string folderPath, out string fileName)

View File

@ -1,9 +1,13 @@
using AssetRipper.SourceGenerated.Classes.ClassID_115; using AssetRipper.SourceGenerated.Classes.ClassID_115;
using System.Text.RegularExpressions;
namespace AssetRipper.SourceGenerated.Extensions; 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) public static bool IsFullNameEqual(this IMonoScript _this, IMonoScript other)
{ {
return _this.AssemblyName == other.AssemblyName return _this.AssemblyName == other.AssemblyName
@ -15,4 +19,30 @@ public static class MonoScriptExtensions
{ {
return _this.Namespace == @namespace && _this.ClassName_R == name; 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;
}
} }