ProcessHelper

作者: 2b75747cf703 | 来源:发表于2016-08-12 18:10 被阅读1151次
using UnityEngine;
using System.Collections.Generic;
using System.Diagnostics;
using Debug = UnityEngine.Debug;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System;
using System.IO;

namespace Babybus.Framework.Extension
{
    public class ProcessHelper : MonoBehaviour
    {
#if UNITY_EDITOR
        [UnityEditor.MenuItem("BabybusFrame/打印当前进程ID")]
        static void LogCurrentProcessId()
        {
            Debug.Log(GetCurrentProcessId());
        }
#endif

        public static int GetCurrentProcessId()
        {
            return Process.GetCurrentProcess().Id;
        }

        public static void StartProcess(string fileName, string arguments, bool waitForExit = true, string currentDirectory = "", Action<List<string>, List<string>> exitAction = null, Predicate<string> filterStandardOutput = null, Predicate<string> filterStandardError = null)
        {
            if (fileName.EndsWith(".bat") && Application.platform != RuntimePlatform.WindowsEditor)
            {
                var bash = "./" + Path.GetFileNameWithoutExtension(fileName) + ".sh";

                StartProcess("/bin/chmod", "+x " + bash, true, currentDirectory);

                fileName = "/bin/bash";
                arguments = bash + " " + arguments;
            }


#if UNITY_EDITOR
            if (waitForExit && !Application.isPlaying)
                EditorUtility.DisplayProgressBar("Hold on", fileName + " " + arguments, 0.5f);
#endif

            if (string.IsNullOrEmpty(currentDirectory))
                currentDirectory = Application.dataPath.Replace("Assets", "");

            var lastDirectory = Directory.GetCurrentDirectory();
            Directory.SetCurrentDirectory(currentDirectory);

            var processStartInfo = new ProcessStartInfo();

            processStartInfo.FileName = fileName;
            processStartInfo.Arguments = arguments;
            processStartInfo.CreateNoWindow = true;
            processStartInfo.RedirectStandardInput = false;
            processStartInfo.RedirectStandardOutput = true;
            processStartInfo.RedirectStandardError = true;
            processStartInfo.UseShellExecute = false;

            var process = new Process();
            process.StartInfo = processStartInfo;
            process.EnableRaisingEvents = true;

            var standardOutput = new List<string>();
            process.OutputDataReceived += (sender, e) =>
            {
                if (e.Data != null)
                    standardOutput.Add(e.Data);
            };

            var standardError = new List<string>();
            process.ErrorDataReceived += (sender, e) =>
            {
                if (e.Data != null)
                    standardError.Add(e.Data);
            };

            process.Exited += (sender, e) =>
            {
                if (filterStandardOutput != null)
                    standardOutput = standardOutput.FindAll(filterStandardOutput);

                var log = "";
                foreach (var item in standardOutput)
                    log += item + "\n";

                Debug.Log(log);

                if (filterStandardError != null)
                    standardError = standardError.FindAll(filterStandardError);

                var logError = "";
                foreach (var item in standardError)
                    logError += item + "\n";

                Debug.Log(logError);

                if (exitAction != null)
                    exitAction(standardOutput, standardError);
            };

            process.Start();

            Directory.SetCurrentDirectory(lastDirectory);

            process.BeginOutputReadLine();
            process.BeginErrorReadLine();

            if (waitForExit)
            {
                process.WaitForExit();

#if UNITY_EDITOR
                if (!Application.isPlaying)
                    EditorUtility.ClearProgressBar();
#endif
            }
        }
    }
}

网友评论

    本文标题:ProcessHelper

    本文链接:https://www.haomeiwen.com/subject/miresttx.html