You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.7 KiB
105 lines
3.7 KiB
// C# example.
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using System.Runtime.InteropServices;
|
|
using System.IO;
|
|
using System.Diagnostics;
|
|
using System;
|
|
|
|
public class ScriptBatch
|
|
{
|
|
[MenuItem("Protobuf/Build C# GRPC connecting code")]
|
|
public static void BuildGRPC()
|
|
{
|
|
string arch = null;
|
|
if (RuntimeInformation.ProcessArchitecture == Architecture.Arm || RuntimeInformation.ProcessArchitecture == Architecture.X86)
|
|
{
|
|
arch = "x86";
|
|
}
|
|
else
|
|
{
|
|
arch = "x64";
|
|
}
|
|
string protoc = null;
|
|
string plugin = null;
|
|
string basePath = Application.dataPath.Substring(0, Application.dataPath.Length - "/Assets".Length);
|
|
string protobufPath = Path.Combine(Application.dataPath.Substring(0, Application.dataPath.Length - "/unity/Assets".Length), "protobuf");
|
|
string outDir = Path.Combine(Application.dataPath, "Scripts", "grpc");
|
|
string includeDir = null;
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
|
|
{
|
|
includeDir = basePath + "/Packages/Grpc.Tools.2.33.1/tools/include";
|
|
protoc = basePath + "/Packages/Grpc.Tools.2.33.1/tools/linux_" + arch + "/protoc";
|
|
plugin = basePath + "/Packages/Grpc.Tools.2.33.1/tools/linux_" + arch + "/grpc_csharp_plugin";
|
|
}
|
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
{
|
|
includeDir = basePath + "\\Packages\\Grpc.Tools.2.33.1\\tools\\include";
|
|
protoc = basePath + "\\Packages\\Grpc.Tools.2.33.1\\tools\\windows_" + arch + "\\protoc.exe";
|
|
plugin = basePath + "\\Packages\\Grpc.Tools.2.33.1\\tools\\windows_" + arch + "\\grpc_csharp_plugin.exe";
|
|
}
|
|
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
|
{
|
|
includeDir = basePath + "/Packages/Grpc.Tools.2.33.1/tools/include";
|
|
protoc = basePath + "/Packages/Grpc.Tools.2.33.1/tools/macosx_" + arch + "/protoc";
|
|
plugin = basePath + "/Packages/Grpc.Tools.2.33.1/tools/macosx_" + arch + "/grpc_csharp_plugin";
|
|
}
|
|
|
|
UnityEngine.Debug.Log(protoc);
|
|
UnityEngine.Debug.Log(plugin);
|
|
|
|
// // For the example
|
|
// const string ex1 = "C:\\";
|
|
// const string ex2 = "C:\\Dir";
|
|
|
|
// // Use ProcessStartInfo class
|
|
|
|
|
|
|
|
|
|
try{
|
|
var protoFiles = Directory.EnumerateFiles(protobufPath, "*.proto");
|
|
|
|
foreach (string currentFile in protoFiles)
|
|
{
|
|
compile_file(protoc, plugin, protobufPath, includeDir, outDir, currentFile);
|
|
}
|
|
} catch (Exception e) {
|
|
UnityEngine.Debug.LogException(e);
|
|
}
|
|
|
|
// try
|
|
// {
|
|
// // Start the process with the info we specified.
|
|
// // Call WaitForExit and then the using statement will close.
|
|
// using (Process exeProcess = Process.Start(startInfo))
|
|
// {
|
|
// exeProcess.WaitForExit();
|
|
// }
|
|
// }
|
|
// catch
|
|
// {
|
|
// // Log error.
|
|
// }
|
|
}
|
|
|
|
public static void compile_file(string protoc, string plugin, string protobufPath, string includeDir, string outDir, string file) {
|
|
// -I protobuf --csharp_out=unity/Assets/Scripts/grpc --grpc_out=unity/Assets/Scripts/grpc --plugin=protoc-gen-grpc=unity/Packages/Grpc.Tools.2.33.1/tools/linux_x64/grpc_csharp_plugin file
|
|
|
|
Process myProcess = new Process();
|
|
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
|
|
myProcess.StartInfo.CreateNoWindow = true;
|
|
myProcess.StartInfo.UseShellExecute = false;
|
|
myProcess.StartInfo.FileName = protoc;
|
|
myProcess.StartInfo.Arguments = $"-I \"{protobufPath}\" -I \"{includeDir}\" --csharp_out=\"{outDir}\" --grpc_out=\"{outDir}\" --plugin=protoc-gen-grpc=\"{plugin}\" \"{file}\"";
|
|
myProcess.EnableRaisingEvents = true;
|
|
myProcess.StartInfo.RedirectStandardError = true;
|
|
myProcess.Start();
|
|
myProcess.WaitForExit();
|
|
if (myProcess.ExitCode != 0) {
|
|
throw new IOException($"Protoc error: {myProcess.StandardError.ReadToEnd()}");
|
|
}else{
|
|
UnityEngine.Debug.Log($"Compiled {file}");
|
|
}
|
|
}
|
|
}
|