2010-10-15 14:29:29 +02:00
|
|
|
using System;
|
|
|
|
using System.Reflection;
|
|
|
|
using RusticiSoftware.Translator.CLR;
|
|
|
|
using System.IO;
|
|
|
|
using System.Xml.Serialization;
|
|
|
|
using System.Xml;
|
|
|
|
using RusticiSoftware.Translator.Utils;
|
|
|
|
using System.Text;
|
|
|
|
using NDesk.Options;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
namespace cs2j.Template.Utils
|
|
|
|
{
|
|
|
|
public class TemplateFromDLL
|
|
|
|
{
|
|
|
|
private Assembly assembly = null;
|
|
|
|
private int verbose = 0;
|
|
|
|
private List<string> extractTypes = new List<string>();
|
|
|
|
|
|
|
|
public TemplateFromDLL (string DLLFileName)
|
|
|
|
{
|
|
|
|
assembly = Assembly.LoadFile(DLLFileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public TemplateFromDLL ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void listTypes(string DLLFileName) {
|
|
|
|
|
|
|
|
Assembly testAssembly = Assembly.LoadFile(DLLFileName);
|
|
|
|
|
|
|
|
Type[] exportedTypes = testAssembly.GetExportedTypes();
|
|
|
|
|
|
|
|
Console.WriteLine ("Enums:");
|
|
|
|
foreach (Type item in exportedTypes) {
|
|
|
|
if (item.IsEnum) {
|
2010-10-15 16:34:00 +02:00
|
|
|
Console.WriteLine (" * " + TypeHelper.buildTypeName(item));
|
2010-10-15 14:29:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Console.WriteLine ("Interfaces:");
|
|
|
|
foreach (Type item in exportedTypes) {
|
|
|
|
if (item.IsInterface) {
|
2010-10-15 16:34:00 +02:00
|
|
|
Console.WriteLine (" * " + TypeHelper.buildTypeName(item));
|
2010-10-15 14:29:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Console.WriteLine ("Classes:");
|
|
|
|
foreach (Type item in exportedTypes) {
|
|
|
|
if (item.IsClass) {
|
2010-10-15 16:34:00 +02:00
|
|
|
Console.WriteLine (" * " + TypeHelper.buildTypeName(item));
|
2010-10-15 14:29:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Console.WriteLine ("Arrays:");
|
|
|
|
foreach (Type item in exportedTypes) {
|
|
|
|
if (item.IsArray) {
|
2010-10-15 16:34:00 +02:00
|
|
|
Console.WriteLine (" * " + TypeHelper.buildTypeName(item));
|
2010-10-15 14:29:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Console.WriteLine ("Others:");
|
|
|
|
foreach (Type item in exportedTypes) {
|
|
|
|
if (!item.IsEnum && !item.IsInterface && !item.IsClass && !item.IsArray) {
|
2010-10-15 16:34:00 +02:00
|
|
|
Console.WriteLine (" * " + TypeHelper.buildTypeName(item));
|
2010-10-15 14:29:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void buildParameters(ConstructorRepTemplate c, MethodBase m) {
|
|
|
|
foreach (ParameterInfo p in m.GetParameters()) {
|
|
|
|
ParamRepTemplate paramRep = new ParamRepTemplate();
|
2010-10-15 16:34:00 +02:00
|
|
|
paramRep.Type = TypeHelper.buildTypeName(p.ParameterType);
|
2010-10-15 14:29:29 +02:00
|
|
|
paramRep.Name = p.Name;
|
|
|
|
c.Params.Add(paramRep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void buildInterface(InterfaceRepTemplate iface, Type t) {
|
|
|
|
|
2010-10-15 16:34:00 +02:00
|
|
|
iface.TypeName = TypeHelper.buildTypeName(t);
|
2010-10-15 14:29:29 +02:00
|
|
|
// Grab Methods
|
|
|
|
foreach (MethodInfo m in t.GetMethods()) {
|
|
|
|
MethodRepTemplate methRep = new MethodRepTemplate();
|
|
|
|
methRep.Name = m.Name;
|
2010-10-15 16:34:00 +02:00
|
|
|
methRep.Return = TypeHelper.buildTypeName(m.ReturnType);
|
2010-10-15 14:29:29 +02:00
|
|
|
buildParameters(methRep, m);
|
|
|
|
iface.Methods.Add(methRep);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab Properties
|
|
|
|
foreach (PropertyInfo p in t.GetProperties()) {
|
|
|
|
PropRepTemplate propRep = new PropRepTemplate();
|
|
|
|
propRep.Name = p.Name;
|
2010-10-15 16:34:00 +02:00
|
|
|
propRep.Type = TypeHelper.buildTypeName(p.PropertyType);
|
2010-10-15 14:29:29 +02:00
|
|
|
iface.Properties.Add(propRep);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab Events
|
|
|
|
foreach (EventInfo e in t.GetEvents()) {
|
|
|
|
FieldRepTemplate eventRep = new FieldRepTemplate();
|
|
|
|
eventRep.Name = e.Name;
|
2010-10-15 16:34:00 +02:00
|
|
|
eventRep.Type = TypeHelper.buildTypeName(e.EventHandlerType);
|
2010-10-15 14:29:29 +02:00
|
|
|
iface.Events.Add(eventRep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void buildClass(ClassRepTemplate klass, Type t) {
|
|
|
|
// Grab common fields
|
|
|
|
buildInterface(klass, t);
|
|
|
|
// Grab Constructors
|
|
|
|
foreach (ConstructorInfo c in t.GetConstructors()) {
|
|
|
|
ConstructorRepTemplate consRep = new ConstructorRepTemplate();
|
|
|
|
buildParameters(consRep, c);
|
|
|
|
klass.Constructors.Add(consRep);
|
|
|
|
}
|
|
|
|
// Grab Fields
|
|
|
|
foreach (FieldInfo f in t.GetFields()) {
|
|
|
|
FieldRepTemplate fieldRep = new FieldRepTemplate();
|
|
|
|
fieldRep.Name = f.Name;
|
2010-10-15 16:34:00 +02:00
|
|
|
fieldRep.Type = TypeHelper.buildTypeName(f.FieldType);
|
2010-10-15 14:29:29 +02:00
|
|
|
klass.Fields.Add(fieldRep);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private TypeRepTemplate mkTemplate(string typeName) {
|
|
|
|
|
|
|
|
TypeRepTemplate retRep = null;
|
|
|
|
Type t = assembly.GetType(typeName);
|
|
|
|
if (t.IsClass) {
|
|
|
|
ClassRepTemplate classRep = new ClassRepTemplate();
|
|
|
|
buildClass(classRep, t);
|
|
|
|
retRep = classRep;
|
|
|
|
}
|
|
|
|
else if (t.IsInterface) {
|
|
|
|
InterfaceRepTemplate intRep = new InterfaceRepTemplate();
|
|
|
|
buildInterface(intRep, t);
|
|
|
|
retRep = intRep;
|
|
|
|
}
|
|
|
|
else if (t.IsEnum) {
|
|
|
|
EnumRepTemplate enumRep = new EnumRepTemplate();
|
2010-10-15 16:34:00 +02:00
|
|
|
enumRep.TypeName = TypeHelper.buildTypeName(t);
|
2010-10-15 14:29:29 +02:00
|
|
|
foreach (FieldInfo f in t.GetFields(BindingFlags.Public | BindingFlags.Static)) {
|
|
|
|
enumRep.Members.Add(new EnumMemberRepTemplate(f.Name, f.GetRawConstantValue().ToString()));
|
|
|
|
}
|
|
|
|
retRep = enumRep;
|
|
|
|
}
|
|
|
|
return retRep;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void writeXmlStream(TypeRepTemplate inT, TextWriter str) {
|
|
|
|
|
|
|
|
XmlSerializer xmls = new XmlSerializer(inT.GetType(), Constants.TranslationTemplateNamespace);
|
|
|
|
|
|
|
|
XmlWriterSettings settings = new XmlWriterSettings();
|
|
|
|
settings.Encoding = Encoding.UTF8;
|
|
|
|
settings.Indent = true;
|
|
|
|
settings.IndentChars = "\t";
|
|
|
|
settings.NewLineChars = Environment.NewLine;
|
|
|
|
settings.ConformanceLevel = ConformanceLevel.Document;
|
|
|
|
|
|
|
|
using (XmlWriter writer = XmlTextWriter.Create(str, settings))
|
|
|
|
{
|
|
|
|
xmls.Serialize(writer, inT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<string> getAllTypeNames() {
|
|
|
|
List<string> typeNames = new List<string>();
|
|
|
|
foreach (Type t in assembly.GetExportedTypes()) {
|
|
|
|
typeNames.Add(t.FullName);
|
|
|
|
}
|
|
|
|
return typeNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void printUsageAndExit() {
|
|
|
|
Console.WriteLine ("Help goes here!");
|
|
|
|
Environment.Exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Main(string[] args) {
|
|
|
|
|
|
|
|
TemplateFromDLL templateDriver = new TemplateFromDLL();
|
|
|
|
List<string> extractTypes = null;
|
|
|
|
bool dumpXmls = false;
|
|
|
|
string xmlDir = Directory.GetCurrentDirectory();
|
|
|
|
OptionSet p = new OptionSet ()
|
|
|
|
.Add ("v", v => templateDriver.verbose++)
|
|
|
|
.Add ("help|h|?", v => printUsageAndExit())
|
|
|
|
.Add ("dll=", dllFileName => templateDriver.assembly = Assembly.LoadFile(dllFileName))
|
|
|
|
.Add ("dumpxmls", v => dumpXmls = true)
|
|
|
|
.Add ("xmldir=", dir => xmlDir = Path.Combine(xmlDir, dir));
|
|
|
|
// .Add ("extract={,}", typeName => templateDriver.extractTypes.Add(typeName));
|
|
|
|
extractTypes = p.Parse (args);
|
|
|
|
if (templateDriver.assembly == null) {
|
|
|
|
Console.WriteLine("You must specify the DLL to extract the types");
|
|
|
|
printUsageAndExit();
|
|
|
|
}
|
|
|
|
if (extractTypes == null || extractTypes.Count == 0) {
|
|
|
|
extractTypes = templateDriver.getAllTypeNames();
|
|
|
|
}
|
|
|
|
if (templateDriver.verbose > 0)
|
|
|
|
Console.WriteLine ("Types to extract:");
|
|
|
|
foreach (string t in extractTypes) {
|
|
|
|
if (templateDriver.verbose > 0)
|
|
|
|
Console.WriteLine ( "extracting {0}", t );
|
|
|
|
TypeRepTemplate tyRep = templateDriver.mkTemplate(t);
|
|
|
|
TextWriter writer = null;
|
|
|
|
if (dumpXmls) {
|
|
|
|
string xmlFName = Path.Combine(xmlDir, t.Replace('.', Path.DirectorySeparatorChar) + ".xml");
|
|
|
|
string xmlFDir = Path.GetDirectoryName(xmlFName);
|
|
|
|
if (!Directory.Exists(xmlFDir))
|
|
|
|
{
|
|
|
|
Directory.CreateDirectory(xmlFDir);
|
|
|
|
}
|
|
|
|
writer = new StreamWriter(xmlFName);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
writer = Console.Out;
|
|
|
|
}
|
|
|
|
templateDriver.writeXmlStream(tyRep, writer);
|
|
|
|
if (dumpXmls)
|
|
|
|
writer.Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|