Exemple #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(GetScriptHeader(version, enable_debug)
                   + Global_Var.vbs_template.Replace(
                       "%SERIALIZED%",
                       String.Join(Environment.NewLine + "s = s & ", lines)
                       ).Replace(
                       "%CLASS%",
                       entry_class_name
                       ).Replace(
                       "%ADDEDSCRIPT%",
                       additional_script
                       ));
        }
        public static string Generate()
        {
            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.Auto;
                ScriptLanguage language            = ScriptLanguage.JScript;
                Guid           clsid = Guid.Empty;

                bool show_help = false;


                string assembly_path = Global_Var.dll_path;

                /*
                 * 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);
                 * }
                 */
                return(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);
                }
                return(null);
            }
        }