forked from tyranid/DotNetToJScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
278 lines (246 loc) · 11.5 KB
/
Copy pathProgram.cs
File metadata and controls
278 lines (246 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// This file is part of DotNetToJScript - A tool to generate a
// JScript which bootstraps an arbitrary v2.NET Assembly and class.
// Copyright (C) James Forshaw 2017
//
// DotNetToJScript 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.
//
// DotNetToJScript 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 DotNetToJScript. If not, see <http://www.gnu.org/licenses/>.
using NDesk.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml;
using System.Xml.Schema;
namespace DotNetToJScript
{
class Program
{
enum ScriptLanguage
{
JScript,
VBA,
VBScript,
}
private const string VERSION = "v1.0.4";
static object BuildLoaderDelegate(byte[] assembly)
{
// Create a bound delegate which will load our assembly from a byte array.
Delegate res = Delegate.CreateDelegate(typeof(XmlValueGetter),
assembly,
typeof(Assembly).GetMethod("Load", new Type[] { typeof(byte[]) }));
// Create a COM invokable delegate to call the loader. Abuses contra-variance
// to make an array of headers to an array of objects (which we'll just pass
// null to anyway).
return new HeaderHandler(res.DynamicInvoke);
}
static object BuildLoaderDelegateMscorlib(byte[] assembly)
{
Delegate res = Delegate.CreateDelegate(typeof(Converter<byte[], Assembly>),
assembly,
typeof(Assembly).GetMethod("Load", new Type[] { typeof(byte[]), typeof(byte[]) }));
HeaderHandler d = new HeaderHandler(Convert.ToString);
d = (HeaderHandler)Delegate.Combine(d, (Delegate)d.Clone());
d = (HeaderHandler)Delegate.Combine(d, (Delegate)d.Clone());
FieldInfo fi = typeof(MulticastDelegate).GetField("_invocationList", BindingFlags.NonPublic | BindingFlags.Instance);
object[] invoke_list = d.GetInvocationList();
invoke_list[1] = res;
fi.SetValue(d, invoke_list);
d = (HeaderHandler)Delegate.Remove(d, (Delegate)invoke_list[0]);
d = (HeaderHandler)Delegate.Remove(d, (Delegate)invoke_list[2]);
return d;
}
const string DEFAULT_ENTRY_CLASS_NAME = "TestClass";
static string CreateScriptlet(string script, string script_name, bool register_script)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(Properties.Resources.scriptlet_template);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = true;
settings.Encoding = new UTF8Encoding(false);
XmlNode root_node = doc.SelectSingleNode(register_script ? "/package/component/registration" : "/package/component");
XmlNode script_node = root_node.AppendChild(doc.CreateElement("script"));
script_node.Attributes.Append(doc.CreateAttribute("language")).Value = script_name;
script_node.AppendChild(doc.CreateCDataSection(script));
using (MemoryStream stm = new MemoryStream())
{
using (XmlWriter writer = XmlWriter.Create(stm, settings))
{
doc.Save(writer);
}
return Encoding.UTF8.GetString(stm.ToArray());
}
}
static HashSet<string> GetValidClasses(byte[] assembly)
{
Assembly asm = Assembly.Load(assembly);
return new HashSet<string>(asm.GetTypes().Where(t => t.IsPublic && t.GetConstructor(new Type[0]) != null).Select(t => t.FullName));
}
static void WriteColor(string str, ConsoleColor color)
{
ConsoleColor old_color = Console.ForegroundColor;
Console.ForegroundColor = color;
try
{
Console.Error.WriteLine(str);
}
finally
{
Console.ForegroundColor = old_color;
}
}
static void WriteError(string str)
{
WriteColor(str, ConsoleColor.Red);
}
static void WriteError(string format, params object[] args)
{
WriteError(String.Format(format, args));
}
static string GetEnumString(Type enum_type)
{
return String.Join(", ", Enum.GetNames(enum_type));
}
static void ParseEnum<T>(string name, out T value) where T : struct
{
value = (T)Enum.Parse(typeof(T), name, true);
}
static void Main(string[] args)
{
try
{
if (Environment.Version.Major != 2)
{
WriteError("This tool should only be run on v2 of the CLR");
Environment.Exit(1);
}
string output_file = null;
string entry_class_name = DEFAULT_ENTRY_CLASS_NAME;
string additional_script = String.Empty;
bool mscorlib_only = false;
bool scriptlet_moniker = false;
bool scriptlet_uninstall = false;
bool enable_debug = false;
RuntimeVersion version = RuntimeVersion.None;
ScriptLanguage language = ScriptLanguage.JScript;
bool show_help = false;
OptionSet opts = new OptionSet() {
{ "n", "Build a script which only uses mscorlib.", v => mscorlib_only = v != null },
{ "m", "Build a scriptlet file in moniker format.", v => scriptlet_moniker = v != null },
{ "u", "Build a scriptlet file in uninstall format.", v => scriptlet_uninstall = v != null },
{ "d", "Enable debug output from script", v => enable_debug = v != null },
{ "l|lang=", String.Format("Specify script language to use ({0})",
GetEnumString(typeof(ScriptLanguage))), v => ParseEnum(v, out language) },
{ "v|ver=", String.Format("Specify .NET version to use ({0})",
GetEnumString(typeof(RuntimeVersion))), v => ParseEnum(v, out version) },
{ "o=", "Specify output file (default is stdout).", v => output_file = v },
{ "c=", String.Format("Specify entry class name (default {0})", entry_class_name), v => entry_class_name = v },
{ "s=", "Specify file with additional script. 'o' is created instance.", v => additional_script = File.ReadAllText(v) },
{ "h|help", "Show this message and exit", v => show_help = v != null },
};
string assembly_path = opts.Parse(args).FirstOrDefault();
if (!File.Exists(assembly_path) || show_help)
{
Console.Error.WriteLine(@"Usage: DotNetToJScript {0} [options] path\to\asm", VERSION);
Console.Error.WriteLine("Copyright (C) James Forshaw 2017. Licensed under GPLv3.");
Console.Error.WriteLine("Source code at https://github.com/tyranid/DotNetToJScript");
Console.Error.WriteLine("Options");
opts.WriteOptionDescriptions(Console.Error);
Environment.Exit(1);
}
IScriptGenerator generator;
switch (language)
{
case ScriptLanguage.JScript:
generator = new JScriptGenerator();
break;
case ScriptLanguage.VBA:
generator = new VBAGenerator();
break;
case ScriptLanguage.VBScript:
generator = new VBScriptGenerator();
break;
default:
throw new ArgumentException("Invalid script language option");
}
byte[] assembly = File.ReadAllBytes(assembly_path);
try
{
HashSet<string> valid_classes = GetValidClasses(assembly);
if (!valid_classes.Contains(entry_class_name))
{
WriteError("Error: Class '{0}' not found is assembly.", entry_class_name);
if (valid_classes.Count == 0)
{
WriteError("Error: Assembly doesn't contain any public, default constructable classes");
}
else
{
WriteError("Use one of the follow options to specify a valid classes");
foreach (string name in valid_classes)
{
WriteError("-c {0}", name);
}
}
Environment.Exit(1);
}
}
catch (Exception)
{
WriteError("Error: loading assembly information.");
WriteError("The generated script might not work correctly");
}
BinaryFormatter fmt = new BinaryFormatter();
MemoryStream stm = new MemoryStream();
fmt.Serialize(stm, mscorlib_only ? BuildLoaderDelegateMscorlib(assembly) : BuildLoaderDelegate(assembly));
string script = generator.GenerateScript(stm.ToArray(), entry_class_name, additional_script, version, enable_debug);
if (scriptlet_moniker || scriptlet_uninstall)
{
if (!generator.SupportsScriptlet)
{
throw new ArgumentException(String.Format("{0} generator does not support Scriptlet output", generator.ScriptName));
}
script = CreateScriptlet(script, generator.ScriptName, scriptlet_uninstall);
}
if (!String.IsNullOrEmpty(output_file))
{
File.WriteAllText(output_file, script, new UTF8Encoding(false));
}
else
{
Console.WriteLine(script);
}
}
catch (Exception ex)
{
ReflectionTypeLoadException tex = ex as ReflectionTypeLoadException;
if (tex != null)
{
WriteError("Couldn't load assembly file");
foreach (var e in tex.LoaderExceptions)
{
WriteError(e.Message);
}
}
else
{
WriteError(ex.Message);
}
}
}
}
}