This commit is contained in:
Perfare 2019-12-04 06:11:54 +08:00
parent 1b2fe8eb09
commit c7726f6c8e

View file

@ -159,68 +159,26 @@ namespace Il2CppDumper
writer.Write($"{GetTypeName(fieldType)} {metadata.GetStringFromIndex(fieldDef.nameIndex)}"); writer.Write($"{GetTypeName(fieldType)} {metadata.GetStringFromIndex(fieldDef.nameIndex)}");
if (fieldDefaultValue != null && fieldDefaultValue.dataIndex != -1) if (fieldDefaultValue != null && fieldDefaultValue.dataIndex != -1)
{ {
var pointer = metadata.GetDefaultValueFromIndex(fieldDefaultValue.dataIndex); if (TryGetDefaultValue(fieldDefaultValue.typeIndex, fieldDefaultValue.dataIndex, out var value))
var fieldDefaultValueType = il2Cpp.types[fieldDefaultValue.typeIndex];
metadata.Position = pointer;
object val = null;
switch (fieldDefaultValueType.type)
{ {
case Il2CppTypeEnum.IL2CPP_TYPE_BOOLEAN: writer.Write($" = ");
val = metadata.ReadBoolean(); if (value is string str)
break; {
case Il2CppTypeEnum.IL2CPP_TYPE_U1: writer.Write($"\"{str.ToEscapedString()}\"");
val = metadata.ReadByte(); }
break; else if (value is char c)
case Il2CppTypeEnum.IL2CPP_TYPE_I1: {
val = metadata.ReadSByte(); var v = (int)c;
break; writer.Write($"'\\x{v:x}'");
case Il2CppTypeEnum.IL2CPP_TYPE_CHAR: }
val = BitConverter.ToChar(metadata.ReadBytes(2), 0); else if (value != null)
break; {
case Il2CppTypeEnum.IL2CPP_TYPE_U2: writer.Write($"{value}");
val = metadata.ReadUInt16(); }
break;
case Il2CppTypeEnum.IL2CPP_TYPE_I2:
val = metadata.ReadInt16();
break;
case Il2CppTypeEnum.IL2CPP_TYPE_U4:
val = metadata.ReadUInt32();
break;
case Il2CppTypeEnum.IL2CPP_TYPE_I4:
val = metadata.ReadInt32();
break;
case Il2CppTypeEnum.IL2CPP_TYPE_U8:
val = metadata.ReadUInt64();
break;
case Il2CppTypeEnum.IL2CPP_TYPE_I8:
val = metadata.ReadInt64();
break;
case Il2CppTypeEnum.IL2CPP_TYPE_R4:
val = metadata.ReadSingle();
break;
case Il2CppTypeEnum.IL2CPP_TYPE_R8:
val = metadata.ReadDouble();
break;
case Il2CppTypeEnum.IL2CPP_TYPE_STRING:
var len = metadata.ReadInt32();
val = Encoding.UTF8.GetString(metadata.ReadBytes(len));
break;
default:
writer.Write($" /*Metadata offset 0x{pointer:X}*/");
break;
} }
if (val is string str) else
{ {
writer.Write($" = \"{str.ToEscapedString()}\""); writer.Write($" /*Metadata offset 0x{value:X}*/");
}
else if (val is char c)
{
var v = (int)c;
writer.Write($" = '\\x{v:x}'");
}
else if (val != null)
{
writer.Write($" = {val}");
} }
} }
if (config.DumpFieldOffset) if (config.DumpFieldOffset)
@ -324,19 +282,26 @@ namespace Il2CppDumper
var parameterDefault = metadata.GetParameterDefaultValueFromIndex(methodDef.parameterStart + j); var parameterDefault = metadata.GetParameterDefaultValueFromIndex(methodDef.parameterStart + j);
if (parameterDefault != null && parameterDefault.dataIndex != -1) if (parameterDefault != null && parameterDefault.dataIndex != -1)
{ {
var value = GetDefaultValue(parameterDefault.typeIndex, parameterDefault.dataIndex); if (TryGetDefaultValue(parameterDefault.typeIndex, parameterDefault.dataIndex, out var value))
if (value is string str)
{ {
parameterStr += $" = \"{str.ToEscapedString()}\""; parameterStr += " = ";
if (value is string str)
{
parameterStr += $"\"{str.ToEscapedString()}\"";
}
else if (value is char c)
{
var v = (int)c;
parameterStr += $"'\\x{v:x}'";
}
else if (value != null)
{
parameterStr += $"{value}";
}
} }
else if (value is char c) else
{ {
var v = (int)c; parameterStr += $" /*Metadata offset 0x{value:X}*/";
parameterStr += $" = '\\x{v:x}'";
}
else if (value != null)
{
parameterStr += $" = {value}";
} }
} }
parameterStrs.Add(parameterStr); parameterStrs.Add(parameterStr);
@ -521,42 +486,57 @@ namespace Il2CppDumper
return str; return str;
} }
private object GetDefaultValue(int typeIndex, int dataIndex) private bool TryGetDefaultValue(int typeIndex, int dataIndex, out object value)
{ {
var pointer = metadata.GetDefaultValueFromIndex(dataIndex); var pointer = metadata.GetDefaultValueFromIndex(dataIndex);
var defaultValueType = il2Cpp.types[typeIndex]; var defaultValueType = il2Cpp.types[typeIndex];
metadata.Position = pointer; metadata.Position = pointer;
value = null;
switch (defaultValueType.type) switch (defaultValueType.type)
{ {
case Il2CppTypeEnum.IL2CPP_TYPE_BOOLEAN: case Il2CppTypeEnum.IL2CPP_TYPE_BOOLEAN:
return metadata.ReadBoolean(); value = metadata.ReadBoolean();
return true;
case Il2CppTypeEnum.IL2CPP_TYPE_U1: case Il2CppTypeEnum.IL2CPP_TYPE_U1:
return metadata.ReadByte(); value = metadata.ReadByte();
return true;
case Il2CppTypeEnum.IL2CPP_TYPE_I1: case Il2CppTypeEnum.IL2CPP_TYPE_I1:
return metadata.ReadSByte(); value = metadata.ReadSByte();
return true;
case Il2CppTypeEnum.IL2CPP_TYPE_CHAR: case Il2CppTypeEnum.IL2CPP_TYPE_CHAR:
return BitConverter.ToChar(metadata.ReadBytes(2), 0); value = BitConverter.ToChar(metadata.ReadBytes(2), 0);
return true;
case Il2CppTypeEnum.IL2CPP_TYPE_U2: case Il2CppTypeEnum.IL2CPP_TYPE_U2:
return metadata.ReadUInt16(); value = metadata.ReadUInt16();
return true;
case Il2CppTypeEnum.IL2CPP_TYPE_I2: case Il2CppTypeEnum.IL2CPP_TYPE_I2:
return metadata.ReadInt16(); value = metadata.ReadInt16();
return true;
case Il2CppTypeEnum.IL2CPP_TYPE_U4: case Il2CppTypeEnum.IL2CPP_TYPE_U4:
return metadata.ReadUInt32(); value = metadata.ReadUInt32();
return true;
case Il2CppTypeEnum.IL2CPP_TYPE_I4: case Il2CppTypeEnum.IL2CPP_TYPE_I4:
return metadata.ReadInt32(); value = metadata.ReadInt32();
return true;
case Il2CppTypeEnum.IL2CPP_TYPE_U8: case Il2CppTypeEnum.IL2CPP_TYPE_U8:
return metadata.ReadUInt64(); value = metadata.ReadUInt64();
return true;
case Il2CppTypeEnum.IL2CPP_TYPE_I8: case Il2CppTypeEnum.IL2CPP_TYPE_I8:
return metadata.ReadInt64(); value = metadata.ReadInt64();
return true;
case Il2CppTypeEnum.IL2CPP_TYPE_R4: case Il2CppTypeEnum.IL2CPP_TYPE_R4:
return metadata.ReadSingle(); value = metadata.ReadSingle();
return true;
case Il2CppTypeEnum.IL2CPP_TYPE_R8: case Il2CppTypeEnum.IL2CPP_TYPE_R8:
return metadata.ReadDouble(); value = metadata.ReadDouble();
return true;
case Il2CppTypeEnum.IL2CPP_TYPE_STRING: case Il2CppTypeEnum.IL2CPP_TYPE_STRING:
var len = metadata.ReadInt32(); var len = metadata.ReadInt32();
return Encoding.UTF8.GetString(metadata.ReadBytes(len)); value = Encoding.UTF8.GetString(metadata.ReadBytes(len));
return true;
default: default:
throw new ArgumentOutOfRangeException(); value = pointer;
return false;
} }
} }
} }