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