整理代码

This commit is contained in:
Perfare 2018-01-11 06:56:28 +08:00
parent ea58e4408b
commit 218f8e2168
3 changed files with 43 additions and 64 deletions

View file

@ -22,7 +22,7 @@ namespace Il2CppDumper
{ {
this.version = version; this.version = version;
this.maxmetadataUsages = maxmetadataUsages; this.maxmetadataUsages = maxmetadataUsages;
readas32bit = true; readAs32Bit = true;
if (version < 21) if (version < 21)
Search = Searchv20; Search = Searchv20;
else else

View file

@ -18,7 +18,7 @@ namespace Il2CppDumper
{ {
this.version = version; this.version = version;
this.maxmetadataUsages = maxmetadataUsages; this.maxmetadataUsages = maxmetadataUsages;
readas32bit = true; readAs32Bit = true;
if (version < 21) if (version < 21)
Search = Searchv16; Search = Searchv16;
else else

View file

@ -8,20 +8,43 @@ namespace Il2CppDumper
{ {
public class MyBinaryReader : BinaryReader public class MyBinaryReader : BinaryReader
{ {
public int version;
protected bool readAs32Bit;
private MethodInfo readClass;
public MyBinaryReader(Stream stream) : base(stream) public MyBinaryReader(Stream stream) : base(stream)
{ {
readClass = GetType().GetMethod("ReadClass", Type.EmptyTypes); readClass = GetType().GetMethod("ReadClass", Type.EmptyTypes);
} }
public int version; private object ReadPrimitive(Type type)
protected bool readas32bit;
private Dictionary<string, string> _64bitTo32bit = new Dictionary<string, string>()
{ {
{"Int64", "Int32"}, var typename = type.Name;
{"UInt64", "UInt32"} switch (typename)
}; {
case "Int32":
return ReadInt32();
case "UInt32":
return ReadUInt32();
case "Int16":
return ReadInt16();
case "UInt16":
return ReadUInt16();
case "Byte":
return ReadByte();
case "Int64" when readAs32Bit:
return ReadInt32();
case "Int64":
return ReadInt64();
case "UInt64" when readAs32Bit:
return ReadUInt32();
case "UInt64":
return ReadUInt64();
default:
return null;
}
}
public dynamic Position public dynamic Position
{ {
@ -29,8 +52,6 @@ namespace Il2CppDumper
set => BaseStream.Position = (long)value; set => BaseStream.Position = (long)value;
} }
private MethodInfo readClass;
public T ReadClass<T>(dynamic addr) where T : new() public T ReadClass<T>(dynamic addr) where T : new()
{ {
Position = addr; Position = addr;
@ -42,28 +63,7 @@ namespace Il2CppDumper
var type = typeof(T); var type = typeof(T);
if (type.IsPrimitive) if (type.IsPrimitive)
{ {
var typename = type.Name; return (T)ReadPrimitive(type);
if (readas32bit && _64bitTo32bit.ContainsKey(typename))
typename = _64bitTo32bit[typename];
switch (typename)
{
case "Int32":
return (T)(object)ReadInt32();
case "UInt32":
return (T)(object)ReadUInt32();
case "Int16":
return (T)(object)ReadInt16();
case "UInt16":
return (T)(object)ReadUInt16();
case "Byte":
return (T)(object)ReadByte();
case "Int64":
return (T)(object)ReadInt64();
case "UInt64":
return (T)(object)ReadUInt64();
default:
return default(T);
}
} }
else else
{ {
@ -76,37 +76,16 @@ namespace Il2CppDumper
if (version < attr.Min || version > attr.Max) if (version < attr.Min || version > attr.Max)
continue; continue;
} }
var typename = i.FieldType.Name; if (i.FieldType.IsPrimitive)
if (readas32bit && _64bitTo32bit.ContainsKey(typename))
typename = _64bitTo32bit[typename];
switch (typename)
{ {
case "Int32": i.SetValue(t, ReadPrimitive(i.FieldType));
i.SetValue(t, ReadInt32()); }
break; else
case "UInt32": {
i.SetValue(t, ReadUInt32()); var gm = readClass.MakeGenericMethod(i.FieldType);
break; var o = gm.Invoke(this, null);
case "Int16": i.SetValue(t, o);
i.SetValue(t, ReadInt16()); break;
break;
case "UInt16":
i.SetValue(t, ReadUInt16());
break;
case "Byte":
i.SetValue(t, ReadByte());
break;
case "Int64":
i.SetValue(t, ReadInt64());
break;
case "UInt64":
i.SetValue(t, ReadUInt64());
break;
default:
var gm = readClass.MakeGenericMethod(i.FieldType);
var o = gm.Invoke(this, null);
i.SetValue(t, o);
break;
} }
} }
return t; return t;