提交 91ac5dbd 编写于 作者: T tanghai

增加ProcessHelper类,所有启动进程的地方都使用这个类

上级 c600fd0a
无法预览此类型文件
无法预览此类型文件
文件已添加
......@@ -56,10 +56,7 @@ namespace ETModel
Log.Info($"{exe} {arguments}");
try
{
bool useShellExecute = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
ProcessStartInfo info = new ProcessStartInfo { FileName = exe, Arguments = arguments, CreateNoWindow = true, UseShellExecute = useShellExecute };
Process process = Process.Start(info);
Process process = ProcessHelper.Run(exe, arguments);
this.processes.Add(startConfig.AppId, process);
}
catch (Exception e)
......
......@@ -50,6 +50,9 @@
<Compile Include="..\..\Unity\Assets\Model\Base\Object\IDeserializeSystem.cs">
<Link>Base\Base\IDeserializeSystem.cs</Link>
</Compile>
<Compile Include="..\..\Unity\Assets\Model\Base\ProcessHelper.cs">
<Link>Base/ProcessHelper.cs</Link>
</Compile>
<Compile Include="..\..\Unity\Assets\Model\Component\Config\StartConfig.cs">
<Link>Component\Config\StartConfig.cs</Link>
</Compile>
......
......@@ -2,7 +2,9 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using ETModel;
namespace ETTools
{
......@@ -16,8 +18,17 @@ namespace ETTools
{
public static void Main()
{
Run("protoc.exe", "--csharp_out=\"../Unity/Assets/Model/Module/Message/\" --proto_path=\"./\" OuterMessage.proto");
Run("protoc.exe", "--csharp_out=\"../Unity/Assets/Hotfix/Module/Message/\" --proto_path=\"./\" HotfixMessage.proto");
string protoc = "";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
protoc = "protoc.exe";
}
else
{
protoc = "protoc";
}
ProcessHelper.Run(protoc, "--csharp_out=\"../Unity/Assets/Model/Module/Message/\" --proto_path=\"./\" OuterMessage.proto");
ProcessHelper.Run(protoc, "--csharp_out=\"../Unity/Assets/Hotfix/Module/Message/\" --proto_path=\"./\" HotfixMessage.proto");
// InnerMessage.proto生成cs代码
InnerProto2CS.Proto2CS();
......@@ -37,28 +48,6 @@ namespace ETTools
private static readonly char[] splitChars = { ' ', '\t' };
private static readonly List<OpcodeInfo> msgOpcode = new List<OpcodeInfo>();
public static void Run(string exe, string arguments)
{
Console.WriteLine($"execute: {exe} {arguments}");
ProcessStartInfo info = new ProcessStartInfo
{
FileName = exe,
Arguments = arguments,
CreateNoWindow = true,
UseShellExecute = false,
WorkingDirectory = ".",
RedirectStandardOutput = true,
RedirectStandardError = true,
};
Process process = Process.Start(info);
process.WaitForExit();
if (process.ExitCode != 0)
{
throw new Exception(process.StandardOutput.ReadToEnd() + process.StandardError.ReadToEnd());
}
}
public static void Proto2CS(string ns, string protoName, string outputPath, string opcodeClassName, int startOpcode, bool isClient = true)
{
msgOpcode.Clear();
......
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
......@@ -19,6 +19,9 @@
<ItemGroup>
<Compile Remove=".vs\**" />
<Compile Include="..\..\Unity\Assets\Model\Base\ProcessHelper.cs">
<Link>ProcessHelper.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
......
......@@ -10,38 +10,11 @@ namespace ETEditor
public static string BuildFolder = "../Release/{0}/StreamingAssets/";
//[MenuItem("Tools/编译Hotfix")]
public static void BuildHotfix()
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
string unityDir = System.Environment.GetEnvironmentVariable("Unity");
if (string.IsNullOrEmpty(unityDir))
{
Log.Error("没有设置Unity环境变量!");
return;
}
process.StartInfo.FileName = $@"{unityDir}\Editor\Data\MonoBleedingEdge\bin\mono.exe";
process.StartInfo.Arguments = $@"{unityDir}\Editor\Data\MonoBleedingEdge\lib\mono\xbuild\14.0\bin\xbuild.exe .\Hotfix\Unity.Hotfix.csproj";
process.StartInfo.UseShellExecute = false;
process.StartInfo.WorkingDirectory = @".\";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
string info = process.StandardOutput.ReadToEnd();
process.Close();
Log.Info(info);
}
[MenuItem("Tools/web资源服务器")]
public static void OpenFileServer()
{
string currentDir = System.Environment.CurrentDirectory;
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "dotnet";
process.StartInfo.Arguments = "FileServer.dll";
process.StartInfo.WorkingDirectory = "../FileServer/";
process.StartInfo.CreateNoWindow = true;
process.Start();
ProcessHelper.Run("dotnet", "FileServer.dll", "../FileServer/");
}
public static void Build(PlatformType type, BuildAssetBundleOptions buildAssetBundleOptions, BuildOptions buildOptions, bool isBuildExe, bool isContainAB)
......
using System.Diagnostics;
using UnityEngine;
namespace ETEditor {
public static class ShellHelper {
public static void Bash(this string cmd, string workingDirectory, bool startTerminal = false) {
ProcessStartInfo startInfo = new ProcessStartInfo("/bin/bash") {
WorkingDirectory = workingDirectory,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true
};
Process process = new Process {
StartInfo = startInfo
};
process.Start();
string code = "";
if(startTerminal) {
code = "osascript -e 'tell application \"Terminal\" to do script \"" +
"" + cmd + "\" in selected tab of the front window'";
} else {
code = cmd;
}
process.StandardInput.WriteLine(code);
process.StandardInput.WriteLine("exit");
process.StandardInput.Flush();
string line = process.StandardOutput.ReadLine();
while(line != null) {
UnityEngine.Debug.Log("line:" + line);
line = process.StandardOutput.ReadLine();
}
process.WaitForExit();
}
}
}
\ No newline at end of file
using System.Reflection;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditorInternal;
using UnityEngine;
/// <summary>
/// 日志重定向相关的实用函数。
/// author: NOW
/// Time: 20180226
/// </summary>
internal static class LogRedirection
{
private static readonly Regex LogRegex = new Regex(@" \(at (.+)\:(\d+)\)\r?\n");
[OnOpenAsset(0)]
private static bool OnOpenAsset(int instanceId, int line)
{
string selectedStackTrace = GetSelectedStackTrace();
if (string.IsNullOrEmpty(selectedStackTrace))
{
return false;
}
if (!selectedStackTrace.Contains("ETModel.Log"))
{
return false;
}
Match match = LogRegex.Match(selectedStackTrace);
if (!match.Success)
{
return false;
}
// 跳过第一次匹配的堆栈
match = match.NextMatch();
if (!match.Success)
{
return false;
}
if (!selectedStackTrace.Contains("ETHotfix.Log"))
{
InternalEditorUtility.OpenFileAtLineExternal(Application.dataPath.Replace("Assets", "") + match.Groups[1].Value, int.Parse(match.Groups[2].Value));
return true;
}
else
{
// 跳过第2次匹配的堆栈
match = match.NextMatch();
if (!match.Success)
{
return false;
}
InternalEditorUtility.OpenFileAtLineExternal(Application.dataPath.Replace("Assets", "") + match.Groups[1].Value, int.Parse(match.Groups[2].Value));
return true;
}
}
private static string GetSelectedStackTrace()
{
Assembly editorWindowAssembly = typeof(EditorWindow).Assembly;
if (editorWindowAssembly == null)
{
return null;
}
System.Type consoleWindowType = editorWindowAssembly.GetType("UnityEditor.ConsoleWindow");
if (consoleWindowType == null)
{
return null;
}
FieldInfo consoleWindowFieldInfo = consoleWindowType.GetField("ms_ConsoleWindow", BindingFlags.Static | BindingFlags.NonPublic);
if (consoleWindowFieldInfo == null)
{
return null;
}
EditorWindow consoleWindow = consoleWindowFieldInfo.GetValue(null) as EditorWindow;
if (consoleWindow == null)
{
return null;
}
if (consoleWindow != EditorWindow.focusedWindow)
{
return null;
}
FieldInfo activeTextFieldInfo = consoleWindowType.GetField("m_ActiveText", BindingFlags.Instance | BindingFlags.NonPublic);
if (activeTextFieldInfo == null)
{
return null;
}
return (string)activeTextFieldInfo.GetValue(consoleWindow);
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 311d256107e00ed4bbdd3f0d81922b82
timeCreated: 1525772883
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System;
using System.Diagnostics;
using System.Diagnostics;
using ETModel;
using UnityEditor;
......@@ -10,42 +9,15 @@ namespace ETEditor
public string Name;
public int Opcode;
}
public class Proto2CSEditor : EditorWindow
public class Proto2CSEditor: EditorWindow
{
[MenuItem("Tools/Proto2CS")]
public static void AllProto2CS()
{
CommandRun();
Process process = ProcessHelper.Run("dotnet", "Proto2CS.dll", "../Proto/", true);
Log.Info(process.StandardOutput.ReadToEnd());
AssetDatabase.Refresh();
}
public static void CommandRun()
{
try
{
ProcessStartInfo info = new ProcessStartInfo
{
CreateNoWindow = true,
FileName = "dotnet",
Arguments = "Proto2CS.dll",
UseShellExecute = false,
WorkingDirectory = "../Proto/",
RedirectStandardOutput = true,
RedirectStandardError = true,
};
Process process = Process.Start(info);
process.WaitForExit();
if (process.ExitCode != 0)
{
throw new Exception(process.StandardOutput.ReadToEnd() + process.StandardError.ReadToEnd());
}
Log.Info(process.StandardOutput.ReadToEnd());
}
catch (Exception e)
{
Log.Error(e);
}
}
}
}
......@@ -82,14 +82,7 @@ namespace ETEditor
{
string arguments =
$"-vzrtopg --password-file=./Tools/cwRsync/Config/rsync.secrets --exclude-from=./Tools/cwRsync/Config/exclude.txt --delete ./ {this.rsyncConfig.Account}@{this.rsyncConfig.Host}::Upload/{this.rsyncConfig.RelativePath} --chmod=ugo=rwX";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @".\Tools\cwRsync\rsync.exe";
startInfo.Arguments = arguments;
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = @"..\";
Process p = Process.Start(startInfo);
p.WaitForExit();
Log.Info($"{startInfo.FileName} {startInfo.Arguments}");
ProcessHelper.Run(@"./Tools/cwRsync/rsync.exe", arguments, @"..\", waitExit: true);
Log.Info("同步完成!");
}
}
......
......@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using ETModel;
using MongoDB.Bson;
using UnityEditor;
......@@ -403,9 +404,7 @@ namespace ETEditor
}
string arguments = $"App.dll --appId={startConfig.AppId} --appType={startConfig.AppType} --config=../Config/StartConfig/{this.fileName}";
ProcessStartInfo info = new ProcessStartInfo("dotnet", arguments) { UseShellExecute = true, WorkingDirectory = @"../Bin/" };
Process.Start(info);
ProcessHelper.Run("dotnet", arguments, "../Bin/");
}
GUILayout.EndHorizontal();
}
......
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ETModel
{
public static class ProcessHelper
{
public static Process Run(string exe, string arguments, string workingDirectory = ".", bool waitExit = false)
{
try
{
bool redirectStandardOutput = true;
bool redirectStandardError = true;
bool useShellExecute = false;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
redirectStandardOutput = false;
redirectStandardError = false;
useShellExecute = true;
}
if (waitExit)
{
redirectStandardOutput = true;
redirectStandardError = true;
useShellExecute = false;
}
ProcessStartInfo info = new ProcessStartInfo
{
FileName = exe,
Arguments = arguments,
CreateNoWindow = true,
UseShellExecute = useShellExecute,
WorkingDirectory = workingDirectory,
RedirectStandardOutput = redirectStandardOutput,
RedirectStandardError = redirectStandardError,
};
Process process = Process.Start(info);
if (waitExit)
{
process.WaitForExit();
if (process.ExitCode != 0)
{
throw new Exception(process.StandardOutput.ReadToEnd() + process.StandardError.ReadToEnd() + "\n"
+ $"请在terminal中执行,目录{workingDirectory}, 命令{exe} {arguments}");
}
}
return process;
}
catch (Exception e)
{
throw new Exception($"请在terminal中执行,目录{workingDirectory}, 命令{exe} {arguments}", e);
}
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 7c53f2d83b6153a47ab75bf18c7a96f1
guid: 2526ac32d65b645b3bc103ac14a1953a
MonoImporter:
externalObjects: {}
serializedVersion: 2
......
......@@ -103,9 +103,7 @@
<Compile Include="Assets\Editor\ExcelExporterEditor\ExcelExporterEditor.cs" />
<Compile Include="Assets\Editor\GlobalConfigEditor\GlobalConfigEditor.cs" />
<Compile Include="Assets\Editor\Helper\EditorResHelper.cs" />
<Compile Include="Assets\Editor\Helper\ShellHelper.cs" />
<Compile Include="Assets\Editor\ILRuntimeEditor\ILRuntimeCLRBinding.cs" />
<Compile Include="Assets\Editor\LogRedirection.cs" />
<Compile Include="Assets\Editor\Proto2CsEditor\InnerProto2CS.cs" />
<Compile Include="Assets\Editor\Proto2CsEditor\Proto2CSEditor.cs" />
<Compile Include="Assets\Editor\ReferenceCollectorEditor\ReferenceCollectorEditor.cs" />
......
......@@ -115,6 +115,7 @@
<Compile Include="Assets\Model\Base\Object\ObjectPool.cs" />
<Compile Include="Assets\Model\Base\Object\ObjectSystemAttribute.cs" />
<Compile Include="Assets\Model\Base\OneThreadSynchronizationContext.cs" />
<Compile Include="Assets\Model\Base\ProcessHelper.cs" />
<Compile Include="Assets\Model\Base\QueueDictionary.cs" />
<Compile Include="Assets\Model\Base\RecyclableMemoryStream\Events.cs" />
<Compile Include="Assets\Model\Base\RecyclableMemoryStream\RecyclableMemoryStream.cs" />
......
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=TypesAndNamespaces/@EntryIndexedValue"></s:String>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=TypesAndNamespaces/@EntryIndexRemoved">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=Interfaces/@EntryIndexRemoved">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=TypeParameters/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=Method/@EntryIndexRemoved">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=Property/@EntryIndexRemoved">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=Event/@EntryIndexedValue"></s:String>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=Event/@EntryIndexRemoved">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=Locals/@EntryIndexRemoved">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=LocalConstants/@EntryIndexedValue"></s:String>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=LocalConstants/@EntryIndexRemoved">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=Parameters/@EntryIndexRemoved">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=PublicFields/@EntryIndexedValue"></s:String>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=PublicFields/@EntryIndexRemoved">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=PrivateInstanceFields/@EntryIndexedValue"></s:String>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=PrivateInstanceFields/@EntryIndexRemoved">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=PrivateStaticFields/@EntryIndexedValue"></s:String>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=PrivateStaticFields/@EntryIndexRemoved">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=Constants/@EntryIndexedValue"></s:String>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=Constants/@EntryIndexRemoved">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=PrivateConstants/@EntryIndexedValue"></s:String>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=PrivateConstants/@EntryIndexRemoved">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=StaticReadonly/@EntryIndexedValue"></s:String>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=StaticReadonly/@EntryIndexRemoved">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue"></s:String>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=PrivateStaticReadonly/@EntryIndexRemoved">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpNaming/AutoDetectedNamingRules/=EnumMember/@EntryIndexRemoved">True</s:Boolean>
<s:Int64 x:Key="/Default/CodeStyle/Naming/CSharpAutoNaming/AutoNamingCompletedVersion/@EntryValue">2</s:Int64></wpf:ResourceDictionary>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册