Newer
Older
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
public class StartServer : MonoBehaviour
{
[SerializeField]
TMPro.TextMeshProUGUI WaitingText;
bool ServerRunning = false;
// Start is called before the first frame update
void Start()
{
StartCoroutine(ServerRoutine());
}
void PrepareGame()
{
WaitingText.text = "Press any key to start the game";
ServerRunning= true;
UnityEngine.Debug.Log("server fin");
}
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
string command = "\"" + Application.streamingAssetsPath + "\"/start.BAT "+ "\"" + Application.streamingAssetsPath + "\"" ;
command = command.Replace("/", @"\");
command = "\"" + command + "\"";
UnityEngine.Debug.Log(command);
ProcessStartInfo processInfo;
Process process;
//processInfo = new ProcessStartInfo("cmd.exe", "/C " + command);
bool cmd = true;
if (cmd)
{
processInfo = new ProcessStartInfo("cmd.exe", "/C " + command);
// processInfo.CreateNoWindow = false;
// processInfo.UseShellExecute = true;
process = Process.Start(processInfo);
}else
/*
*/
Process.Start("powershell.exe", command);
// *** Read the streams ***
// Warning: This approach can lead to deadlocks, see Edit #2
//string output = process.StandardOutput.ReadToEnd();
//string error = process.StandardError.ReadToEnd();
// exitCode = process.ExitCode;
// UnityEngine.Debug.Log(output);
// UnityEngine.Debug.Log(error);
// Console.WriteLine("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output));
// Console.WriteLine("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error));
// Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand");
// process.Close();
yield return null;
}
IEnumerator ServerRoutine()
{
UnityWebRequest request = UnityWebRequest.Get("localhost:8081/scroll/list");
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
UnityEngine.Debug.Log("no running server "+ request.error);
ProcessStartInfo processInfo;
Process process;
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = "bash";
// proc.WorkingDirectory = Application.streamingAssetsPath;
proc.Arguments = Application.streamingAssetsPath+"/startServer.sh";// + " \"" +Application.streamingAssetsPath + "\"";
proc.WindowStyle = ProcessWindowStyle.Normal;
proc.CreateNoWindow = false;
proc.UseShellExecute = true;
process = Process.Start(proc);
#elif UNITY_STANDALONE_OSX
/*
ProcessStartInfo proc = new ProcessStartInfo();//"open", "sh startServer.sh");// + " \"" +Application.streamingAssetsPath + "\"");
proc.FileName = "/bin/bash";
proc.WorkingDirectory = Application.streamingAssetsPath;
proc.Arguments = "sh " + Application.streamingAssetsPath+"/startServer.sh";
proc.CreateNoWindow = false;
proc.UseShellExecute = true;
process = Process.Start(proc);
*/
ProcessStartInfo proc = new ProcessStartInfo();
String startServerPath = Application.streamingAssetsPath + "/startServer.sh";
String runWithTerminalPath = Application.streamingAssetsPath + "/startInTerminal.sh";
proc.FileName = "/bin/bash";
proc.Arguments = runWithTerminalPath + " /bin/bash " + startServerPath;
proc.CreateNoWindow = false;
proc.UseShellExecute = true;
process = Process.Start(proc);
#else
string command = "";
command = "\"" + Application.streamingAssetsPath + "\"/start.BAT " + "\"" + Application.streamingAssetsPath + "\"";
command = command.Replace("/", @"\");
command = "\"" + command + "\"";
processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
processInfo.CreateNoWindow = false;
processInfo.UseShellExecute = true;
// *** Redirect the output ***
// processInfo.RedirectStandardError = true;
//processInfo.RedirectStandardOutput = true;
while (true)
{
request = UnityWebRequest.Get("localhost:8081/scroll/list");
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
// UnityEngine.Debug.Log("no running server");
}
else
{
break;
}
yield return null;
}
// *** Read the streams ***
// Warning: This approach can lead to deadlocks, see Edit #2
//string output = process.StandardOutput.ReadToEnd();
//string error = process.StandardError.ReadToEnd();
// UnityEngine.Debug.Log(output);
// UnityEngine.Debug.Log(error);
// Console.WriteLine("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output));
// Console.WriteLine("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error));
// Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand");
}
PrepareGame();
yield return null;
}
// Update is called once per frame
void Update()
{
if(ServerRunning && Input.anyKey)
{
SceneManager.LoadScene(1);
}
//if(!ServerRunning) UnityEngine.Debug.Log("waiting " + ServerRunning);
}
}