コード例 #1
0
        private void CreateExe()
        {
            using (var exeStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("oscript.StandaloneRunner.exe"))
                using (var output = new MemoryStream())
                {
                    exeStream?.CopyTo(output);

                    var offset = (int)output.Length;

                    var engine = new HostedScriptEngine
                    {
                        CustomConfig = ScriptFileHelper.CustomConfigPath(_codePath)
                    };
                    engine.Initialize();
                    ScriptFileHelper.OnBeforeScriptRead(engine);
                    var source   = engine.Loader.FromFile(_codePath);
                    var compiler = engine.GetCompilerService();
                    engine.SetGlobalEnvironment(new DoNothingHost(), source);
                    var entry = compiler.CreateModule(source);

                    var embeddedContext = engine.GetUserAddedScripts();

                    using (var bw = new BinaryWriter(output))
                    {
                        var userAddedScripts = embeddedContext as IList <UserAddedScript> ?? embeddedContext.ToList();
                        bw.Write(userAddedScripts.Count + 1);

                        var persistor = new ModulePersistor();
                        persistor.Save(new UserAddedScript
                        {
                            Type   = UserAddedScriptType.Module,
                            Symbol = "$entry",
                            Module = entry
                        }, output);

                        foreach (var item in userAddedScripts)
                        {
                            persistor.Save(item, output);
                        }

                        var signature = new byte[]
                        {
                            0x4f,
                            0x53,
                            0x4d,
                            0x44
                        };
                        output.Write(signature, 0, signature.Length);

                        bw.Write(offset);
                        OutputToFile(output);
                    }
                }
        }
コード例 #2
0
        private void CreateDump(Stream output)
        {
            var offset = (int)output.Length;

            var engine = new HostedScriptEngine
            {
                CustomConfig = ScriptFileHelper.CustomConfigPath(_codePath)
            };

            engine.Initialize();
            ScriptFileHelper.OnBeforeScriptRead(engine);
            var source   = engine.Loader.FromFile(_codePath);
            var compiler = engine.GetCompilerService();

            engine.SetGlobalEnvironment(new DoNothingHost(), source);
            var entry = compiler.Compile(source);

            var embeddedContext = engine.GetUserAddedScripts();

            using (var bw = new BinaryWriter(output))
            {
                var userAddedScripts = embeddedContext as IList <UserAddedScript> ?? embeddedContext.ToList();
                bw.Write(userAddedScripts.Count + 1);

                var persistor = new ModulePersistor();
                persistor.Save(new UserAddedScript
                {
                    Type   = UserAddedScriptType.Module,
                    Symbol = "$entry",
                    Image  = entry
                }, output);

                foreach (var item in userAddedScripts)
                {
                    persistor.Save(item, output);
                }

                var signature = new byte[]
                {
                    0x4f,
                    0x53,
                    0x4d,
                    0x44
                };
                output.Write(signature, 0, signature.Length);

                bw.Write(offset);
                OutputToFile(output);
            }
        }
コード例 #3
0
        public int Run()
        {
            var engine = new HostedScriptEngine();
            var src    = new BinaryCodeSource();

            engine.SetGlobalEnvironment(this, src);

            try
            {
                ScriptModuleHandle module;
                engine.Initialize();

                using (var codeStream = LocateCode())
                    using (var binReader = new BinaryReader(codeStream))
                    {
                        var modulesCount = binReader.ReadInt32();

                        var reader = new ModulePersistor();

                        var entry = reader.Read(codeStream);
                        --modulesCount;

                        while (modulesCount-- > 0)
                        {
                            var userScript = reader.Read(codeStream);
                            engine.LoadUserScript(userScript);
                        }

                        module = entry.Module;
                    }

                var process = engine.CreateProcess(this, module, src);

                return(process.Start());
            }
            catch (ScriptInterruptionException e)
            {
                return(e.ExitCode);
            }
            catch (Exception e)
            {
                ShowExceptionInfo(e);
                return(1);
            }
        }
コード例 #4
0
        public int Run()
        {
            if (_sourceStream == null && CommandLineArguments != null && CommandLineArguments.Length > 1)
            {
                var firstArg = CommandLineArguments[0];
                if (firstArg == "-loadDump")
                {
                    var path = CommandLineArguments[1];
                    CommandLineArguments = CommandLineArguments.Skip(2).ToArray();
                    using (var dumpStream = new FileStream(path, FileMode.Open))
                    {
                        _sourceStream = GetCodeStream(dumpStream);
                    }

                    Run(); //ну да, говнокод и лапша, время жмет
                }
            }

            if (_sourceStream == null)
            {
                _sourceStream = LocateCode();
            }

            var engine = new HostedScriptEngine();
            var src    = new BinaryCodeSource();

            engine.SetGlobalEnvironment(this, src);

            try
            {
                ModuleImage module;
                engine.Initialize();

                using (var binReader = new BinaryReader(_sourceStream))
                {
                    var modulesCount = binReader.ReadInt32();

                    var reader = new ModulePersistor();

                    var entry = reader.Read(_sourceStream);
                    --modulesCount;

                    while (modulesCount-- > 0)
                    {
                        var userScript = reader.Read(_sourceStream);
                        engine.LoadUserScript(userScript);
                    }

                    module = entry.Image;
                }

                var process = engine.CreateProcess(this, module, src);

                return(process.Start());
            }
            catch (ScriptInterruptionException e)
            {
                return(e.ExitCode);
            }
            catch (Exception e)
            {
                ShowExceptionInfo(e);
                return(1);
            }
        }