Skip to content

Commit

Permalink
Merge branch 'SamboyCoding:development' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
IIIImmmyyy authored Jul 23, 2024
2 parents a786707 + c720a52 commit ad3c64c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 12 deletions.
14 changes: 12 additions & 2 deletions Cpp2IL.Core/Model/Contexts/ParameterAnalysisContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,25 @@ public override string ToString()
else if(ParameterType.Byref == 1)
result.Append("ref ");

result.Append(ParameterTypeContext.Name).Append(" ");
result.Append(CsFileUtils.GetTypeName(ParameterTypeContext.Name)).Append(' ');

if (string.IsNullOrEmpty(ParameterName))
result.Append("unnamed_param_").Append(ParamIndex);
else
result.Append(ParameterName);

if (ParameterAttributes.HasFlag(ParameterAttributes.HasDefault))
result.Append(" = ").Append(DefaultValue?.ContainedDefaultValue ?? "null");
{
var defaultValue = DefaultValue!.ContainedDefaultValue;
if (defaultValue is string stringDefaultValue)
defaultValue = $"\"{stringDefaultValue}\"";
else if (defaultValue is bool boolDefaultValue)
defaultValue = boolDefaultValue.ToString().ToLowerInvariant();
else if (defaultValue is null)
defaultValue = "null";

result.Append(" = ").Append(defaultValue);
}

return result.ToString();
}
Expand Down
40 changes: 37 additions & 3 deletions Cpp2IL.Core/OutputFormats/DiffableCsOutputFormat.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Cpp2IL.Core.Api;
using Cpp2IL.Core.Extensions;
Expand Down Expand Up @@ -81,9 +82,9 @@ private static Dictionary<string, StringBuilder> BuildOutput(ApplicationAnalysis

private static void AppendType(StringBuilder sb, TypeAnalysisContext type, int indent = 0)
{
if (type.IsCompilerGeneratedBasedOnCustomAttributes)
// if (type.IsCompilerGeneratedBasedOnCustomAttributes)
//Do not output compiler-generated types
return;
// return;

//Custom attributes for type. Includes a trailing newline
AppendCustomAttributes(sb, type, indent);
Expand Down Expand Up @@ -175,8 +176,41 @@ private static void AppendField(StringBuilder sb, FieldAnalysisContext field, in
sb.Append(CsFileUtils.GetTypeName(field.FieldTypeContext.Name));
sb.Append(' ');
sb.Append(field.Name);

if (field.BackingData?.DefaultValue is {} defaultValue)
{
sb.Append(" = ");

if (defaultValue is string stringDefaultValue)
sb.Append('"').Append(stringDefaultValue).Append('"');
else
sb.Append(defaultValue);
}

sb.Append("; //Field offset: 0x");
sb.Append(field.Offset.ToString("X"));

if ((field.Attributes & FieldAttributes.HasFieldRVA) != 0)
{
sb.Append(" || Has Field RVA: 0x");
var (dataIndex, _) = LibCpp2IlMain.TheMetadata!.GetFieldDefaultValue(field.BackingData!.Field.FieldIndex);
var pointer = LibCpp2IlMain.TheMetadata!.GetDefaultValueFromIndex(dataIndex);
sb.Append(pointer.ToString("X8"));

var actualValue = field.BackingData.Field.StaticArrayInitialValue;
if (actualValue is { Length: > 0 })
{
sb.Append(" || Field RVA Decoded (hex blob): [");
sb.Append(actualValue[0].ToString("X2"));
for (var i = 1; i < actualValue.Length; i++)
{
var b = actualValue[i];
sb.Append(' ').Append(b.ToString("X2"));
}

sb.Append(']');
}
}
sb.AppendLine();
}

Expand Down Expand Up @@ -285,7 +319,7 @@ private static void AppendAccessor(StringBuilder sb, MethodAnalysisContext acces
AppendCustomAttributes(sb, accessor, indent);

sb.Append('\t', indent);
sb.Append(CsFileUtils.GetKeyWordsForMethod(accessor, true));
sb.Append(CsFileUtils.GetKeyWordsForMethod(accessor, true, true));
sb.Append(' ');
sb.Append(accessorType);
sb.Append(" { } //Length: ");
Expand Down
21 changes: 15 additions & 6 deletions Cpp2IL.Core/Utils/CsFileUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,26 @@ public static string GetKeyWordsForField(FieldAnalysisContext field)
/// </summary>
/// <param name="method">The method to generate keywords for</param>
/// <param name="skipSlotRelated">Skip slot-related modifiers like abstract, virtual, override</param>
public static string GetKeyWordsForMethod(MethodAnalysisContext method, bool skipSlotRelated = false)
/// <param name="skipKeywordsInvalidForAccessors">Skip the public and static keywords, as those aren't valid for property accessors</param>
public static string GetKeyWordsForMethod(MethodAnalysisContext method, bool skipSlotRelated = false, bool skipKeywordsInvalidForAccessors = false)
{
var sb = new StringBuilder();
var attributes = method.Definition!.Attributes;

if (attributes.HasFlag(MethodAttributes.Public))
sb.Append("public ");
else if (attributes.HasFlag(MethodAttributes.Family))
sb.Append("protected ");
if (!skipKeywordsInvalidForAccessors)
{
if (attributes.HasFlag(MethodAttributes.Public))
sb.Append("public ");
else if (attributes.HasFlag(MethodAttributes.Family))
sb.Append("protected ");
}

if (attributes.HasFlag(MethodAttributes.Assembly))
sb.Append("internal ");
else if (attributes.HasFlag(MethodAttributes.Private))
sb.Append("private ");
if (attributes.HasFlag(MethodAttributes.Static))

if (!skipKeywordsInvalidForAccessors && attributes.HasFlag(MethodAttributes.Static))
sb.Append("static ");

if (method.DeclaringType!.Definition!.Attributes.HasFlag(TypeAttributes.Interface) || skipSlotRelated)
Expand Down Expand Up @@ -277,6 +283,9 @@ public static string GetTypeName(string originalName)
//Generics - remove `1 etc
return originalName.Remove(originalName.IndexOf('`'), 2);

if (originalName[^1] == '&')
originalName = originalName[..^1]; //Remove trailing & for ref params

return originalName switch
{
"Void" => "void",
Expand Down
3 changes: 2 additions & 1 deletion LibCpp2IL/MetadataUsageType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public enum MetadataUsageType: uint
FieldInfo = 4,
StringLiteral = 5,
MethodRef = 6,
FieldRva = 7,
}
}
}

0 comments on commit ad3c64c

Please sign in to comment.