Skip to content

Commit 913b673

Browse files
ds5678SamboyCoding
authored andcommitted
Fix diffable-cs throwing ArgumentOutOfRangeException
1 parent d396ef3 commit 913b673

3 files changed

Lines changed: 68 additions & 35 deletions

File tree

Cpp2IL.Core/Model/Contexts/ParameterAnalysisContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public override string ToString()
102102
else if (ParameterType is ByRefTypeAnalysisContext)
103103
result.Append("ref ");
104104

105-
result.Append(CsFileUtils.GetTypeName(ParameterType.Name)).Append(' ');
105+
result.Append(CsFileUtils.GetTypeName(ParameterType)).Append(' ');
106106

107107
if (string.IsNullOrEmpty(ParameterName))
108108
result.Append("unnamed_param_").Append(ParameterIndex);

Cpp2IL.Core/OutputFormats/DiffableCsOutputFormat.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private static void AppendType(StringBuilder sb, TypeAnalysisContext type, int i
9595

9696
sb.Append(CsFileUtils.GetKeyWordsForType(type));
9797
sb.Append(' ');
98-
sb.Append(CsFileUtils.GetTypeName(type.Name));
98+
sb.Append(CsFileUtils.GetTypeName(type));
9999
CsFileUtils.AppendInheritanceInfo(type, sb);
100100
sb.AppendLine();
101101
sb.Append('\t', indent);
@@ -173,7 +173,7 @@ private static void AppendField(StringBuilder sb, FieldAnalysisContext field, in
173173
sb.Append('\t', indent);
174174
sb.Append(CsFileUtils.GetKeyWordsForField(field));
175175
sb.Append(' ');
176-
sb.Append(CsFileUtils.GetTypeName(field.FieldType.Name));
176+
sb.Append(CsFileUtils.GetTypeName(field.FieldType));
177177
sb.Append(' ');
178178
sb.Append(field.Name);
179179

@@ -226,7 +226,7 @@ private static void AppendEvent(StringBuilder sb, EventAnalysisContext evt, int
226226
sb.Append('\t', indent);
227227
sb.Append(CsFileUtils.GetKeyWordsForEvent(evt));
228228
sb.Append(' ');
229-
sb.Append(CsFileUtils.GetTypeName(evt.EventType.Name));
229+
sb.Append(CsFileUtils.GetTypeName(evt.EventType));
230230
sb.Append(' ');
231231
sb.Append(evt.Name).AppendLine();
232232
sb.Append('\t', indent);
@@ -257,7 +257,7 @@ private static void AppendProperty(StringBuilder sb, PropertyAnalysisContext pro
257257
sb.Append('\t', indent);
258258
sb.Append(CsFileUtils.GetKeyWordsForProperty(prop));
259259
sb.Append(' ');
260-
sb.Append(CsFileUtils.GetTypeName(prop.PropertyType.Name));
260+
sb.Append(CsFileUtils.GetTypeName(prop.PropertyType));
261261
sb.Append(' ');
262262
sb.Append(prop.Name);
263263
sb.AppendLine();
@@ -292,14 +292,14 @@ private static void AppendMethod(StringBuilder sb, MethodAnalysisContext method,
292292
sb.Append(' ');
293293
if (method.Name is not ".ctor" and not ".cctor")
294294
{
295-
sb.Append(CsFileUtils.GetTypeName(method.ReturnType.Name));
295+
sb.Append(CsFileUtils.GetTypeName(method.ReturnType));
296296
sb.Append(' ');
297297
sb.Append(method.Name);
298298
}
299299
else
300300
{
301301
//Constructor
302-
sb.Append(method.DeclaringType!.Name);
302+
sb.Append(method.DeclaringType!);
303303
}
304304

305305
sb.Append('(');

Cpp2IL.Core/Utils/CsFileUtils.cs

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -276,36 +276,69 @@ public static string GetCustomAttributeStrings(HasCustomAttributes context, int
276276
/// Returns the name of the given type, as it would appear in a C# source file.
277277
/// This mainly involves stripping the backtick section from generic type names, and replacing certain system types with their primitive name.
278278
/// </summary>
279-
/// <param name="originalName">The original name of the type</param>
280-
public static string GetTypeName(string originalName)
279+
/// <param name="type"></param>
280+
public static string GetTypeName(TypeAnalysisContext type)
281281
{
282-
if (originalName.Contains("`"))
283-
//Generics - remove `1 etc
284-
return originalName.Remove(originalName.IndexOf('`'), 2);
282+
if (type is WrappedTypeAnalysisContext wrapped)
283+
{
284+
var elementTypeName = GetTypeName(wrapped.ElementType);
285+
switch (wrapped)
286+
{
287+
case ArrayTypeAnalysisContext arrayType:
288+
{
289+
return arrayType.Rank switch
290+
{
291+
1 => elementTypeName + "[]",
292+
2 => elementTypeName + "[,]",
293+
3 => elementTypeName + "[,,]",
294+
_ => elementTypeName + "[" + new string(',', arrayType.Rank - 1) + "]"
295+
};
296+
}
297+
case SzArrayTypeAnalysisContext:
298+
return elementTypeName + "[]";
299+
case PointerTypeAnalysisContext:
300+
return elementTypeName + "*";
301+
case ByRefTypeAnalysisContext:
302+
return elementTypeName; //Remove trailing & for ref params
303+
default:
304+
return elementTypeName;
305+
}
306+
}
285307

286-
if (originalName[^1] == '&')
287-
originalName = originalName[..^1]; //Remove trailing & for ref params
308+
if (type is GenericInstanceTypeAnalysisContext genericInstanceType)
309+
{
310+
var genericTypeName = GetTypeName(genericInstanceType.GenericType);
311+
var backTickIndex = genericTypeName.IndexOf('`');
312+
return backTickIndex > 0 ? genericTypeName[..backTickIndex] : genericTypeName;
313+
}
288314

289-
return originalName switch
315+
if (type.Namespace is "System")
316+
{
317+
return type.Name switch
318+
{
319+
"Void" => "void",
320+
"Boolean" => "bool",
321+
"Byte" => "byte",
322+
"SByte" => "sbyte",
323+
"Char" => "char",
324+
"Decimal" => "decimal",
325+
"Single" => "float",
326+
"Double" => "double",
327+
"Int32" => "int",
328+
"UInt32" => "uint",
329+
"Int64" => "long",
330+
"UInt64" => "ulong",
331+
"Int16" => "short",
332+
"UInt16" => "ushort",
333+
"String" => "string",
334+
"Object" => "object",
335+
_ => type.Name,
336+
};
337+
}
338+
else
290339
{
291-
"Void" => "void",
292-
"Boolean" => "bool",
293-
"Byte" => "byte",
294-
"SByte" => "sbyte",
295-
"Char" => "char",
296-
"Decimal" => "decimal",
297-
"Single" => "float",
298-
"Double" => "double",
299-
"Int32" => "int",
300-
"UInt32" => "uint",
301-
"Int64" => "long",
302-
"UInt64" => "ulong",
303-
"Int16" => "short",
304-
"UInt16" => "ushort",
305-
"String" => "string",
306-
"Object" => "object",
307-
_ => originalName
308-
};
340+
return type.Name;
341+
}
309342
}
310343

311344
/// <summary>
@@ -319,7 +352,7 @@ public static void AppendInheritanceInfo(TypeAnalysisContext type, StringBuilder
319352
var baseType = type.BaseType;
320353
var needsBaseClass = baseType is { FullName: not "System.Object" and not "System.ValueType" and not "System.Enum" };
321354
if (needsBaseClass)
322-
sb.Append(" : ").Append(GetTypeName(baseType!.Name));
355+
sb.Append(" : ").Append(GetTypeName(baseType!));
323356

324357
//Interfaces
325358
if (type.InterfaceContexts.Count <= 0)
@@ -336,7 +369,7 @@ public static void AppendInheritanceInfo(TypeAnalysisContext type, StringBuilder
336369

337370
addComma = true;
338371

339-
sb.Append(GetTypeName(iface.Name));
372+
sb.Append(GetTypeName(iface));
340373
}
341374
}
342375
}

0 commit comments

Comments
 (0)