Exemplo n.º 1
0
        public static List <LogInfo> ExtractAndRun(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

            Debug.Assert(cmd.Info.GetType() == typeof(CodeInfo_ExtractAndRun));
            CodeInfo_ExtractAndRun info = cmd.Info as CodeInfo_ExtractAndRun;

            string        pluginFile = StringEscaper.Preprocess(s, info.PluginFile);
            string        dirName    = StringEscaper.Preprocess(s, info.DirName);
            string        fileName   = StringEscaper.Preprocess(s, info.FileName);
            List <string> parameters = StringEscaper.Preprocess(s, info.Params);

            Plugin p = Engine.GetPluginInstance(s, cmd, s.CurrentPlugin.FullPath, pluginFile, out bool inCurrentPlugin);

            string destPath = Path.GetTempFileName();

            if (StringEscaper.PathSecurityCheck(destPath, out string errorMsg) == false)
            {
                logs.Add(new LogInfo(LogState.Error, errorMsg));
                return(logs);
            }

            using (MemoryStream ms = EncodedFile.ExtractFile(p, dirName, info.FileName))
                using (FileStream fs = new FileStream(destPath, FileMode.Create, FileAccess.Write))
                {
                    ms.Position = 0;
                    ms.CopyTo(fs);
                }

            Process proc = new Process();

            proc.StartInfo.FileName        = destPath;
            proc.StartInfo.UseShellExecute = true;
            proc.StartInfo.Verb            = "Open";
            proc.Start();

            logs.Add(new LogInfo(LogState.Success, $"Encoded file [{fileName}] extracted and executed"));

            return(logs);
        }
Exemplo n.º 2
0
        public static List <LogInfo> ExtractAndRun(EngineState s, CodeCommand cmd)
        {
            List <LogInfo> logs = new List <LogInfo>();

            CodeInfo_ExtractAndRun info = cmd.Info.Cast <CodeInfo_ExtractAndRun>();

            string scriptFile = StringEscaper.Preprocess(s, info.ScriptFile);
            string dirName    = StringEscaper.Preprocess(s, info.DirName);
            string fileName   = StringEscaper.Preprocess(s, info.FileName);

            Script sc = Engine.GetScriptInstance(s, s.CurrentScript.RealPath, scriptFile, out _);

            // Check if encoded file exist
            if (!EncodedFile.ContainsFile(sc, dirName, fileName))
            {
                return(LogInfo.LogErrorMessage(logs, $"Encoded file [{dirName}\\{fileName}] not found in script [{sc.RealPath}]."));
            }

            string tempDir  = FileHelper.GetTempDir();
            string tempPath = Path.Combine(tempDir, fileName);

            s.MainViewModel.SetBuildCommandProgress("ExtractAndRun Progress", 1);
            try
            {
                object             progressLock = new object();
                IProgress <double> progress     = new Progress <double>(x =>
                {
                    lock (progressLock)
                    {
                        s.MainViewModel.BuildCommandProgressValue = x;
                        if (x < EncodedFile.Base64ReportFactor)
                        { // [Stage 1] Base64
                            s.MainViewModel.BuildCommandProgressText = $"Reading \"{fileName}\" from script\r\n({x * 100:0.0}%)";
                        }
                        else
                        { // [Stage 2] Decompress
                            s.MainViewModel.BuildCommandProgressText = $"Decompressing \"{fileName}\"\r\n({x * 100:0.0}%)";
                        }
                    }
                });

                using (FileStream fs = new FileStream(tempPath, FileMode.Create, FileAccess.Write))
                {
                    EncodedFile.ExtractFile(sc, dirName, info.FileName, fs, progress);
                }
            }
            finally
            {
                s.MainViewModel.ResetBuildCommandProgress();
            }

            string _params = null;

            using (Process proc = new Process())
            {
                proc.EnableRaisingEvents = true;
                proc.StartInfo           = new ProcessStartInfo
                {
                    FileName        = tempPath,
                    UseShellExecute = true,
                };

                if (!string.IsNullOrEmpty(info.Params))
                {
                    _params = StringEscaper.Preprocess(s, info.Params);
                    proc.StartInfo.Arguments = _params;
                }

                proc.Exited += (object sender, EventArgs e) =>
                {
                    if (Directory.Exists(tempDir))
                    {
                        Directory.Delete(tempDir, true);
                    }

                    // ReSharper disable once AccessToDisposedClosure
                    proc.Dispose();
                };

                proc.Start();
            }

            if (_params == null)
            {
                logs.Add(new LogInfo(LogState.Success, $"Extracted and executed [{fileName}]"));
            }
            else
            {
                logs.Add(new LogInfo(LogState.Success, $"Extracted and executed [{fileName} {_params}]"));
            }

            return(logs);
        }