📄 filedisassemblerhelper.cs
字号:
}
AddToProjectFiles(fileName);
return 0;
}
catch (Exception ex)
{
WriteLine("Error in " + resource.Name + " : " + ex.Message);
}
}
return WriteOtherResource(resource);
}
private int WriteOtherResource(IResource resource)
{
byte[] buffer = null;
IEmbeddedResource embeddedResource = resource as IEmbeddedResource;
if (embeddedResource != null)
{
buffer = embeddedResource.Value;
}
IFileResource fileResource = resource as IFileResource;
if (fileResource != null)
{
string location = Path.Combine(Path.GetDirectoryName(fileResource.Module.Location), fileResource.Location);
location = Environment.ExpandEnvironmentVariables(location);
if (File.Exists(location))
{
using (Stream stream = new FileStream(location, FileMode.Open, FileAccess.Read))
{
if (fileResource.Offset == 0)
{
buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
}
else
{
BinaryReader reader = new BinaryReader(stream);
int size = reader.ReadInt32();
buffer = new byte[size];
stream.Read(buffer, 0, size);
}
}
}
}
if (buffer != null)
{
string fileName = Path.Combine(_outputDirectory, resource.Name);
using (Stream stream = File.Create(fileName))
{
stream.Write(buffer, 0, buffer.Length);
}
WriteLine(fileName);
}
return 0;
}
private void AddToProjectFiles(string fileName)
{
WriteLine(fileName);
string relPath = fileName.Remove(0, _outputDirectory.Length + 1);
_projectFiles.Add(relPath);
}
/// <summary>
/// Generates Visual Studio project files
/// </summary>
/// <param name="ProjectName"></param>
/// <param name="ProjectPath"></param>
/// <param name="References"></param>
/// <remarks>This code was provided by Jens Andersson, kobingo@gmail.com</remarks>
private void GenerateVisualStudioProject(string ProjectName, string ProjectPath, IAssemblyReferenceCollection References)
{
if (_projectType < 1 || _projectType > 3)
return;
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
string projectExt;
string template = assembly.GetName().Name;
ILanguage language = LanguageManager.ActiveLanguage;
if (language.Name == "C#")
{
projectExt = ".csproj";
template += ".VSProjects.CSharpProject.xml";
}
else if (language.Name == "Visual Basic")
{
projectExt = ".vbproj";
template += ".VSProjects.VBProject.xml";
}
else if (language.Name == "MC++")
{
projectExt = ".vcproj";
if (_projectType == 1)
template += ".VSProjects.CppProject_dll.xml";
else
template += ".VSProjects.CppProject_exe.xml";
}
else
{
WriteLine("Cannot create project file, " + language.Name + " language is not supported.");
return;
}
Stream stream1 = assembly.GetManifestResourceStream(template);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(stream1);
if (language.Name == "MC++")
UpdateProjectTemplateCpp(xmlDoc, ProjectName, References);
else
UpdateProjectTemplateCsVb(xmlDoc, ProjectName, References);
string fileName;
fileName = Path.Combine(ProjectPath, ProjectName + projectExt);
xmlDoc.Save(fileName);
WriteLine(fileName);
}
private void UpdateProjectTemplateCsVb(XmlDocument xmlDoc, string ProjectName, IAssemblyReferenceCollection References)
{
string text2 = "CSHARP";
if (LanguageManager.ActiveLanguage.Name == "Visual Basic")
text2 = "VisualBasic";
XmlNode node0 = xmlDoc.SelectSingleNode("/VisualStudioProject/" + text2);
node0.Attributes["ProjectGuid"].Value = Guid.NewGuid().ToString().ToUpper();
string text3 = "/VisualStudioProject/" + text2 + "/Build/Settings";
XmlNode node1 = xmlDoc.SelectSingleNode(text3);
node1.Attributes["AssemblyName"].Value = ProjectName;
node1.Attributes["RootNamespace"].Value = ProjectName;
switch (_projectType)
{
case 1: node1.Attributes["OutputType"].Value = "Library"; break;
case 2: node1.Attributes["OutputType"].Value = "WinExe"; break;
case 3: node1.Attributes["OutputType"].Value = "Exe"; break;
}
string text4 = "/VisualStudioProject/" + text2 + "/Files/Include";
XmlNode node2 = xmlDoc.SelectSingleNode(text4);
node2.RemoveAll();
foreach (string filePath in _projectFiles)
{
if ((filePath != null) && (filePath != string.Empty))
{
XmlNode node3 = xmlDoc.CreateElement("", "File", "");
XmlAttribute attribute1 = xmlDoc.CreateAttribute("RelPath");
XmlAttribute attribute2 = xmlDoc.CreateAttribute("SubType");
XmlAttribute attribute3 = xmlDoc.CreateAttribute("BuildAction");
attribute1.Value = filePath;
node3.Attributes.Append(attribute1);
if (filePath.EndsWith(".cs") || filePath.EndsWith(".vb"))
{
attribute2.Value = "Code";
node3.Attributes.Append(attribute2);
attribute3.Value = "Compile";
}
else // gotta be a resource
{
attribute3.Value = "EmbeddedResource";
}
node3.Attributes.Append(attribute3);
node2.AppendChild(node3);
}
}
string text6 = "/VisualStudioProject/" + text2 + "/Build/References";
XmlNode node4 = xmlDoc.SelectSingleNode(text6);
node4.RemoveAll();
if (References != null)
{
StringCollection collection1 = new StringCollection();
foreach (IAssemblyReference name1 in References)
{
if (!collection1.Contains(name1.Name))
{
XmlNode node5 = xmlDoc.CreateElement("", "Reference", "");
XmlAttribute attribute4 = xmlDoc.CreateAttribute("Name");
attribute4.Value = name1.Name;
node5.Attributes.Append(attribute4);
XmlAttribute attribute5 = xmlDoc.CreateAttribute("AssemblyName");
attribute5.Value = name1.Name;
node5.Attributes.Append(attribute5);
if(name1 != null && name1.Resolve() != null)
{
XmlAttribute attribute6 = xmlDoc.CreateAttribute("HintPath");
attribute6.Value = Path.GetFileName(name1.Resolve().Location);
node5.Attributes.Append(attribute6);
}
node4.AppendChild(node5);
collection1.Add(name1.Name);
}
}
}
}
private void UpdateProjectTemplateCpp(XmlDocument xmlDoc, string ProjectName, IAssemblyReferenceCollection References)
{
string text1 = "/VisualStudioProject";
XmlNode node1 = xmlDoc.SelectSingleNode(text1);
node1.Attributes["ProjectGUID"].Value = Guid.NewGuid().ToString().ToUpper();
node1.Attributes["Name"].Value = ProjectName;
node1.Attributes["RootNamespace"].Value = ProjectName;
string text2 = "/VisualStudioProject/Files";
XmlNode node2 = xmlDoc.SelectSingleNode(text2);
node2.RemoveAll();
Hashtable folders = new Hashtable();
foreach (string filePath in _projectFiles)
{
if ((filePath != null) && (filePath != string.Empty))
{
string pathName = Path.GetDirectoryName(filePath);
if (folders[pathName] == null)
{
if (pathName.Length == 0)
{
folders[pathName] = node2;
}
else
{
XmlNode pathNode = xmlDoc.CreateElement("", "Filter", "");
XmlAttribute nameAttr = xmlDoc.CreateAttribute("Name");
nameAttr.Value = pathName;
pathNode.Attributes.Append(nameAttr);
node2.AppendChild(pathNode);
folders[pathName] = pathNode;
}
}
XmlNode fileNode = xmlDoc.CreateElement("", "File", "");
XmlAttribute pathAttr = xmlDoc.CreateAttribute("RelativePath");
pathAttr.Value = filePath;
fileNode.Attributes.Append(pathAttr);
XmlNode node3 = (XmlNode)folders[pathName];
node3.AppendChild(fileNode);
}
}
string text4 = "/VisualStudioProject/References";
XmlNode node4 = xmlDoc.SelectSingleNode(text4);
node4.RemoveAll();
if (References != null)
{
StringCollection collection1 = new StringCollection();
foreach (IAssemblyReference name1 in References)
{
if (!collection1.Contains(name1.Name))
{
XmlNode refNode = xmlDoc.CreateElement("", "AssemblyReference", "");
XmlAttribute pathAttr = xmlDoc.CreateAttribute("RelativePath");
pathAttr.Value = Path.GetFileName(name1.Resolve().Location);
refNode.Attributes.Append(pathAttr);
node4.AppendChild(refNode);
collection1.Add(name1.Name);
}
}
}
}
private class LanguageWriterConfiguration : ILanguageWriterConfiguration
{
private IVisibilityConfiguration visibility = new VisibilityConfiguration();
public IVisibilityConfiguration Visibility
{
get
{
return this.visibility;
}
}
public string this[string name]
{
get
{
switch (name)
{
case "ShowDocumentation":
case "ShowCustomAttributes":
case "ShowNamespaceImports":
case "ShowNamespaceBody":
case "ShowTypeDeclarationBody":
case "ShowMethodDeclarationBody":
return "true";
}
return "false";
}
}
}
private class VisibilityConfiguration : IVisibilityConfiguration
{
public bool Public { get { return true; } }
public bool Private { get { return true; } }
public bool Family { get { return true; } }
public bool Assembly { get { return true; } }
public bool FamilyAndAssembly { get { return true; } }
public bool FamilyOrAssembly { get { return true; } }
}
private class AssemblyResolver : IAssemblyResolver
{
private IDictionary _assemblyTable;
private IAssemblyResolver _assemblyResolver;
public AssemblyResolver(IAssemblyResolver assemblyResolver)
{
_assemblyTable = new Hashtable();
_assemblyResolver = assemblyResolver;
}
public IAssembly Resolve(IAssemblyReference assemblyName, string localPath)
{
if (_assemblyTable.Contains(assemblyName))
{
return (IAssembly) _assemblyTable[assemblyName];
}
IAssembly assembly = _assemblyResolver.Resolve(assemblyName, localPath);
_assemblyTable.Add(assemblyName, assembly);
return assembly;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -