de4dot-cex/de4dot.code/resources/ResourceDataCreator.cs

236 lines
7.3 KiB
C#
Raw Normal View History

2012-02-11 23:43:53 +08:00
/*
2013-01-02 00:03:16 +08:00
Copyright (C) 2011-2013 de4dot@gmail.com
2012-02-11 23:43:53 +08:00
This file is part of de4dot.
de4dot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
de4dot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using dnlib.DotNet;
2012-02-11 23:43:53 +08:00
namespace de4dot.code.resources {
class ResourceDataCreator {
readonly ModuleDef module;
readonly ModuleDefMD moduleMD;
readonly Dictionary<string, UserResourceType> dict = new Dictionary<string, UserResourceType>(StringComparer.Ordinal);
readonly Dictionary<string, string> asmNameToAsmFullName = new Dictionary<string, string>(StringComparer.Ordinal);
2012-02-11 23:43:53 +08:00
public ResourceDataCreator(ModuleDef module) {
2012-02-11 23:43:53 +08:00
this.module = module;
this.moduleMD = module as ModuleDefMD;
2012-02-11 23:43:53 +08:00
}
public int Count {
get { return dict.Count; }
}
2013-01-19 20:03:57 +08:00
public BuiltInResourceData CreateNull() {
2012-02-11 23:43:53 +08:00
return new BuiltInResourceData(ResourceTypeCode.Null, null);
}
2013-01-19 20:03:57 +08:00
public BuiltInResourceData Create(string value) {
2012-02-11 23:43:53 +08:00
return new BuiltInResourceData(ResourceTypeCode.String, value);
}
2013-01-19 20:03:57 +08:00
public BuiltInResourceData Create(bool value) {
2012-02-11 23:43:53 +08:00
return new BuiltInResourceData(ResourceTypeCode.Boolean, value);
}
2013-01-19 20:03:57 +08:00
public BuiltInResourceData Create(char value) {
2012-02-11 23:43:53 +08:00
return new BuiltInResourceData(ResourceTypeCode.Char, value);
}
2013-01-19 20:03:57 +08:00
public BuiltInResourceData Create(byte value) {
2012-02-11 23:43:53 +08:00
return new BuiltInResourceData(ResourceTypeCode.Byte, value);
}
2013-01-19 20:03:57 +08:00
public BuiltInResourceData Create(sbyte value) {
2012-02-11 23:43:53 +08:00
return new BuiltInResourceData(ResourceTypeCode.SByte, value);
}
2013-01-19 20:03:57 +08:00
public BuiltInResourceData Create(short value) {
2012-02-11 23:43:53 +08:00
return new BuiltInResourceData(ResourceTypeCode.Int16, value);
}
2013-01-19 20:03:57 +08:00
public BuiltInResourceData Create(ushort value) {
2012-02-11 23:43:53 +08:00
return new BuiltInResourceData(ResourceTypeCode.UInt16, value);
}
2013-01-19 20:03:57 +08:00
public BuiltInResourceData Create(int value) {
2012-02-11 23:43:53 +08:00
return new BuiltInResourceData(ResourceTypeCode.Int32, value);
}
2013-01-19 20:03:57 +08:00
public BuiltInResourceData Create(uint value) {
2012-02-11 23:43:53 +08:00
return new BuiltInResourceData(ResourceTypeCode.UInt32, value);
}
2013-01-19 20:03:57 +08:00
public BuiltInResourceData Create(long value) {
2012-02-11 23:43:53 +08:00
return new BuiltInResourceData(ResourceTypeCode.Int64, value);
}
2013-01-19 20:03:57 +08:00
public BuiltInResourceData Create(ulong value) {
2012-02-11 23:43:53 +08:00
return new BuiltInResourceData(ResourceTypeCode.UInt64, value);
}
2013-01-19 20:03:57 +08:00
public BuiltInResourceData Create(float value) {
2012-02-11 23:43:53 +08:00
return new BuiltInResourceData(ResourceTypeCode.Single, value);
}
2013-01-19 20:03:57 +08:00
public BuiltInResourceData Create(double value) {
2012-02-11 23:43:53 +08:00
return new BuiltInResourceData(ResourceTypeCode.Double, value);
}
2013-01-19 20:03:57 +08:00
public BuiltInResourceData Create(decimal value) {
2012-02-11 23:43:53 +08:00
return new BuiltInResourceData(ResourceTypeCode.Decimal, value);
}
2013-01-19 20:03:57 +08:00
public BuiltInResourceData Create(DateTime value) {
2012-02-11 23:43:53 +08:00
return new BuiltInResourceData(ResourceTypeCode.DateTime, value);
}
2013-01-19 20:03:57 +08:00
public BuiltInResourceData Create(TimeSpan value) {
2012-02-11 23:43:53 +08:00
return new BuiltInResourceData(ResourceTypeCode.TimeSpan, value);
}
2013-01-19 20:03:57 +08:00
public BuiltInResourceData Create(byte[] value) {
2012-02-11 23:43:53 +08:00
return new BuiltInResourceData(ResourceTypeCode.ByteArray, value);
}
2013-11-18 03:24:55 +08:00
public BuiltInResourceData CreateStream(byte[] value) {
return new BuiltInResourceData(ResourceTypeCode.Stream, value);
}
2013-01-19 20:03:57 +08:00
public CharArrayResourceData Create(char[] value) {
2013-11-18 03:43:22 +08:00
return new CharArrayResourceData(CreateUserResourceType(CharArrayResourceData.ReflectionTypeName), value);
2012-02-11 23:43:53 +08:00
}
2013-01-19 20:03:57 +08:00
public IconResourceData CreateIcon(byte[] value) {
2013-11-18 03:43:22 +08:00
return new IconResourceData(CreateUserResourceType(IconResourceData.ReflectionTypeName), value);
2012-02-11 23:43:53 +08:00
}
2013-01-19 20:03:57 +08:00
public ImageResourceData CreateImage(byte[] value) {
2013-11-18 03:43:22 +08:00
return new ImageResourceData(CreateUserResourceType(ImageResourceData.ReflectionTypeName), value);
2012-02-11 23:43:53 +08:00
}
2013-01-19 20:03:57 +08:00
public BinaryResourceData CreateSerialized(byte[] value) {
2012-02-11 23:43:53 +08:00
string assemblyName, typeName;
2013-01-19 20:03:57 +08:00
if (!GetSerializedTypeAndAssemblyName(value, out assemblyName, out typeName))
2012-02-11 23:43:53 +08:00
throw new ApplicationException("Could not get serialized type name");
string fullName = string.Format("{0},{1}", typeName, assemblyName);
2013-01-19 20:03:57 +08:00
return new BinaryResourceData(CreateUserResourceType(fullName), value);
2012-02-11 23:43:53 +08:00
}
class MyBinder : SerializationBinder {
public class OkException : Exception {
2012-02-12 04:30:54 +08:00
public string AssemblyName { get; set; }
public string TypeName { get; set; }
2012-02-11 23:43:53 +08:00
}
public override Type BindToType(string assemblyName, string typeName) {
2012-02-12 04:30:54 +08:00
throw new OkException {
AssemblyName = assemblyName,
TypeName = typeName,
};
2012-02-11 23:43:53 +08:00
}
}
2013-01-19 20:03:57 +08:00
bool GetSerializedTypeAndAssemblyName(byte[] value, out string assemblyName, out string typeName) {
2012-02-11 23:43:53 +08:00
try {
var formatter = new BinaryFormatter();
2012-02-12 04:30:54 +08:00
formatter.Binder = new MyBinder();
2012-02-11 23:43:53 +08:00
formatter.Deserialize(new MemoryStream(value));
}
2012-02-12 04:30:54 +08:00
catch (MyBinder.OkException ex) {
assemblyName = ex.AssemblyName;
typeName = ex.TypeName;
2012-02-11 23:43:53 +08:00
return true;
}
catch {
}
assemblyName = null;
typeName = null;
return false;
}
2013-01-19 20:03:57 +08:00
public UserResourceType CreateUserResourceType(string fullName) {
2012-02-11 23:43:53 +08:00
UserResourceType type;
if (dict.TryGetValue(fullName, out type))
return type;
2013-01-19 20:03:57 +08:00
var newFullName = GetRealTypeFullName(fullName);
2012-02-11 23:43:53 +08:00
type = new UserResourceType(newFullName, ResourceTypeCode.UserTypes + dict.Count);
dict[fullName] = type;
dict[newFullName] = type;
return type;
}
2013-01-19 20:03:57 +08:00
static void SplitTypeFullName(string fullName, out string typeName, out string assemblyName) {
2012-02-11 23:43:53 +08:00
int index = fullName.IndexOf(',');
if (index < 0) {
typeName = fullName;
assemblyName = null;
}
else {
typeName = fullName.Substring(0, index);
assemblyName = fullName.Substring(index + 1).Trim();
}
}
2013-01-19 20:03:57 +08:00
string GetRealTypeFullName(string fullName) {
2012-02-12 02:34:07 +08:00
var newFullName = fullName;
string typeName, assemblyName;
2013-01-19 20:03:57 +08:00
SplitTypeFullName(fullName, out typeName, out assemblyName);
2012-02-12 02:34:07 +08:00
if (!string.IsNullOrEmpty(assemblyName))
2013-01-19 20:03:57 +08:00
assemblyName = GetRealAssemblyName(assemblyName);
2012-02-12 02:34:07 +08:00
if (!string.IsNullOrEmpty(assemblyName))
newFullName = string.Format("{0}, {1}", typeName, assemblyName);
return newFullName;
}
2013-01-19 20:03:57 +08:00
string GetRealAssemblyName(string assemblyName) {
2012-02-12 02:34:07 +08:00
string newAsmName;
if (!asmNameToAsmFullName.TryGetValue(assemblyName, out newAsmName))
2013-01-19 20:03:57 +08:00
asmNameToAsmFullName[assemblyName] = newAsmName = TryGetRealAssemblyName(assemblyName);
2012-02-12 02:34:07 +08:00
return newAsmName;
}
2013-01-19 20:03:57 +08:00
string TryGetRealAssemblyName(string assemblyName) {
var simpleName = Utils.GetAssemblySimpleName(assemblyName);
2012-02-11 23:43:53 +08:00
if (moduleMD != null) {
var asmRef = moduleMD.GetAssemblyRef(simpleName);
if (asmRef != null)
return asmRef.FullName;
}
2012-02-11 23:43:53 +08:00
2012-11-02 15:23:20 +08:00
var asm = TheAssemblyResolver.Instance.Resolve(new AssemblyNameInfo(simpleName), module);
2012-11-02 04:09:09 +08:00
return asm == null ? null : asm.FullName;
2012-02-11 23:43:53 +08:00
}
2013-01-19 20:03:57 +08:00
public List<UserResourceType> GetSortedTypes() {
2012-02-11 23:43:53 +08:00
var list = new List<UserResourceType>(dict.Values);
2012-11-01 23:42:02 +08:00
list.Sort((a, b) => ((int)a.Code).CompareTo((int)b.Code));
2012-02-11 23:43:53 +08:00
return list;
}
}
}