Ejemplo n.º 1
0
        public string GenerateScript(byte[] serialized_object, string entry_class_name, string additional_script, RuntimeVersion version, bool enable_debug)
        {
            string[] lines = JScriptGenerator.BinToBase64Lines(serialized_object);

            return(VBShared.GetScriptHeader(version, enable_debug ? "WScript.Echo" : String.Empty) + Properties.Resources.vbs_template.Replace("%SERIALIZED%",
                                                                                                                                               String.Join(Environment.NewLine + "s = s & ", lines)).Replace("%CLASS%", entry_class_name).Replace("%ADDEDSCRIPT%", additional_script));
        }
Ejemplo n.º 2
0
        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;
                Guid           clsid = Guid.Empty;

                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) },
                    { "clsid=", "Specify a CLSID for the scriptlet", v => clsid = new Guid(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, clsid);
                }

                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);
                }
            }
        }