/// <summary> /// Method that gets all data /// </summary> /// <param name="risk"></param> /// <returns>List of SectionAPI type (DTO)</returns> public async Task <ShellRequest> GetAll(string lang) { var sections = new List <SectionAPI>(); var data = new ShellRequest(); int n = 0; while (n < 4) { try { using (HttpClient hc = new HttpClient()) { var responseMsg = await hc.GetAsync(string.Format(UriGetAll, Host, App.Current.Properties["password"], lang)); if (responseMsg.IsSuccessStatusCode) { var resultStr = await responseMsg.Content.ReadAsStringAsync(); if (resultStr.StartsWith("Error")) { throw new HttpRequestException(resultStr); } var res = JsonConvert.DeserializeObject <ShellRequest>(resultStr); //var res = JsonConvert.DeserializeAnonymousType(resultStr, // new { Sections = new List<SectionAPI>() }); //sections.AddRange(res.Sections); data = res; if (data.Sections.Count == 0 || data.Sections[0].Id_s == null) { throw new Exception("No info downloaded. Trying to retry"); } } else { throw new HttpRequestException("The server has thrown an error code: " + responseMsg.StatusCode); } } return(data); } catch (Exception ex) { n++; if (n == 4) { throw ex; } } } return(data); }
private static ShellRequest LaunchExternalFile(string filePath, string[] args = null) { string cmd = ""; if (args is null) { args = new string[0]; } #if UNITY_EDITOR_WIN cmd = $"{filePath} {string.Join(" ", args)}"; #elif UNITY_EDITOR_OSX cmd = $"sh {filePath} {string.Join(" ", args)}"; #endif ShellRequest req = ProcessCMD(cmd, "."); req.onLog += debug; return(req); }
private static async Task Destroy(ServerProperties properties) { bool validate = await CredentialList.Validate(properties); if (!validate) { return; } CredentialList credentialList = CredentialList.FindCredentialsAsset(); string gcloudBashFilePath = GetFilePath("local_stop"); string arg; #if UNITY_EDITOR_WIN arg = $"\"{Path.Combine(credentialList.GoogleSDKPath, "google-cloud-sdk\\bin\\gcloud")}\""; #elif UNITY_EDITOR_OSX arg = $"{Path.Combine(credentialList.GoogleSDKPath, "bin/gcloud")}"; #endif ShellRequest req = LaunchExternalFile(gcloudBashFilePath, new[] { arg }); }
public static ShellRequest ProcessCommand(string cmd, string workDirectory, List <string> environmentVars = null) { ShellRequest req = new ShellRequest(); System.Threading.ThreadPool.QueueUserWorkItem(delegate(object state) { Process p = null; try{ ProcessStartInfo start = new ProcessStartInfo(shellApp); #if UNITY_EDITOR_OSX string splitChar = ":"; start.Arguments = "-c"; #elif UNITY_EDITOR_WIN string splitChar = ";"; start.Arguments = "/c"; #endif if (environmentVars != null) { foreach (string var in environmentVars) { start.EnvironmentVariables["PATH"] += (splitChar + var); } } start.Arguments += (" \"" + cmd + " \""); start.CreateNoWindow = true; start.ErrorDialog = true; start.UseShellExecute = false; start.WorkingDirectory = workDirectory; if (start.UseShellExecute) { start.RedirectStandardOutput = false; start.RedirectStandardError = false; start.RedirectStandardInput = false; } else { start.RedirectStandardOutput = true; start.RedirectStandardError = true; start.RedirectStandardInput = true; start.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8; start.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8; } p = Process.Start(start); p.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.Exited += delegate(object sender, System.EventArgs e) { UnityEngine.Debug.LogError(e.ToString()); }; bool hasError = false; do { string line = p.StandardOutput.ReadLine(); if (line == null) { break; } line = line.Replace("\\", "/"); _queue.Add(delegate() { req.Log(0, line); }); }while(true); while (true) { string error = p.StandardError.ReadLine(); if (string.IsNullOrEmpty(error)) { break; } hasError = true; _queue.Add(delegate() { req.Log(1, error); }); } p.Close(); if (hasError) { _queue.Add(delegate() { req.Error(); }); } else { _queue.Add(delegate() { req.NotifyDone(); }); } }catch (System.Exception e) { UnityEngine.Debug.LogException(e); if (p != null) { p.Close(); } } }); return(req); }
public static ShellRequest ProcessFileCommand(string fileName, string parameters, string workDirectory = null) { ShellRequest req = new ShellRequest(); Process p = null; try { ProcessStartInfo start = new ProcessStartInfo(); start.FileName = fileName; start.Arguments = parameters; start.CreateNoWindow = true; start.ErrorDialog = true; start.UseShellExecute = false; if (workDirectory != null) { start.WorkingDirectory = workDirectory; } if (start.UseShellExecute) { start.RedirectStandardOutput = false; start.RedirectStandardError = false; start.RedirectStandardInput = false; } else { start.RedirectStandardOutput = true; start.RedirectStandardError = true; start.RedirectStandardInput = true; start.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8; start.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8; } p = Process.Start(start); p.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.Exited += delegate(object sender, System.EventArgs e) { UnityEngine.Debug.LogError(e.ToString()); }; bool hasError = false; do { string line = p.StandardOutput.ReadLine(); if (line == null) { break; } line = line.Replace("\\", "/"); _queue.Add(delegate() { req.Log(0, line); }); } while (true); while (true) { string error = p.StandardError.ReadLine(); if (string.IsNullOrEmpty(error)) { break; } hasError = true; _queue.Add(delegate() { req.Log(1, error); }); } p.Close(); if (hasError) { _queue.Add(delegate() { req.Error(); }); } else { _queue.Add(delegate() { req.NotifyDone(); }); } } catch (System.Exception e) { UnityEngine.Debug.LogException(e); if (p != null) { p.Close(); } } return(req); }
public static ShellRequest ProcessCommand(string cmd, string workDirectory, List <string> environmentVars = null) { ShellRequest req = new ShellRequest(); System.Threading.ThreadPool.QueueUserWorkItem(delegate(object state) { Process p = null; try { ProcessStartInfo start = new ProcessStartInfo(shellApp); #if UNITY_EDITOR_OSX string splitChar = ":"; start.Arguments = "-c"; start.Arguments += (" \"" + cmd + " \""); #elif UNITY_EDITOR_WIN string splitChar = ";"; start.Arguments = "/c" + cmd; #endif if (environmentVars != null) { foreach (string var in environmentVars) { start.EnvironmentVariables["PATH"] += (splitChar + var); } } start.CreateNoWindow = true; start.ErrorDialog = true; start.UseShellExecute = false; start.WorkingDirectory = workDirectory; #if UNITY_EDITOR_OSX start.RedirectStandardOutput = false; start.RedirectStandardError = false; start.RedirectStandardInput = false; #else if (start.UseShellExecute) { start.RedirectStandardOutput = false; start.RedirectStandardError = false; start.RedirectStandardInput = false; } else { start.RedirectStandardOutput = true; start.RedirectStandardError = true; start.RedirectStandardInput = true; var ci = System.Globalization.CultureInfo.GetCultureInfo(GetSystemDefaultLCID()); start.StandardOutputEncoding = Encoding.GetEncoding(ci.TextInfo.OEMCodePage); start.StandardErrorEncoding = Encoding.GetEncoding(ci.TextInfo.OEMCodePage); } #endif p = Process.Start(start); p.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.Exited += delegate(object sender, System.EventArgs e) { UnityEngine.Debug.LogError(e.ToString()); }; string output = p.StandardOutput.ReadToEnd(); string error = p.StandardError.ReadToEnd(); p.WaitForExit(); p.Close(); if (!string.IsNullOrEmpty(output)) { _queue.Add(delegate() { req.Log(0, EncodingConvert(output, start.StandardErrorEncoding, Encoding.UTF8)); }); } if (!string.IsNullOrEmpty(error)) { _queue.Add(delegate() { req.Log(1, EncodingConvert(error, start.StandardErrorEncoding, Encoding.UTF8)); }); } if (!string.IsNullOrEmpty(error)) { _queue.Add(delegate() { req.Error(); }); } else { _queue.Add(delegate() { req.NotifyDone(); }); } } catch (System.Exception e) { UnityEngine.Debug.LogException(e); if (p != null) { p.Close(); } } }); return(req); }
public static ShellRequest ProcessCommand(string cmd, string workDirectory, List <string> environmentVars = null) { ShellRequest req = new ShellRequest(); System.Threading.Interlocked.Increment(ref numberOfTasks); //var threadFinish = new System.Threading.ManualResetEvent(false); //threadFinishEvents.Add(threadFinish); System.Threading.ThreadPool.QueueUserWorkItem(delegate(object state) { Process p = null; try { ProcessStartInfo start = CreateProcessStartInfo(cmd, workDirectory, environmentVars); p = Process.Start(start); p.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.Exited += delegate(object sender, System.EventArgs e) { UnityEngine.Debug.LogError(e.ToString()); }; bool hasError = false; do { string line = p.StandardOutput.ReadLine(); if (line == null) { break; } line = line.Replace("\\", "/"); lock (_queue) { _queue.Add(delegate() { req.Log(0, line); }); } } while (true); while (true) { string error = p.StandardError.ReadLine(); if (string.IsNullOrEmpty(error)) { break; } hasError = true; lock (_queue) { _queue.Add(delegate() { req.Log(1, error); }); } } p.Close(); lock (_queue) { if (hasError) { _queue.Add(delegate() { req.Error(); }); } else { _queue.Add(delegate() { req.NotifyDone(); }); //直接在线程中触发事件,这个callback中不能用序列化 req.TriggerEdge(); } } } catch (System.Exception e) { UnityEngine.Debug.LogException(e); if (p != null) { p.Close(); } } finally { if (System.Threading.Interlocked.Decrement(ref numberOfTasks) == 0) { threadSignal.Set(); } } }); return(req); }
public static ShellRequest ProcessCommand(string cmd,string workDirectory,List<string> environmentVars = null) { ShellRequest req = new ShellRequest(); System.Threading.ThreadPool.QueueUserWorkItem(delegate(object state) { Process p = null; try{ ProcessStartInfo start = new ProcessStartInfo(shellApp); #if UNITY_EDITOR_OSX string splitChar = ":"; start.Arguments = "-c"; #elif UNITY_EDITOR_WIN string splitChar = ";"; start.Arguments = "/c"; #endif if(environmentVars != null){ foreach(string var in environmentVars){ start.EnvironmentVariables["PATH"] += (splitChar + var); } } start.Arguments += (" \"" + cmd + " \""); start.CreateNoWindow = true; start.ErrorDialog = true; start.UseShellExecute = false; start.WorkingDirectory = workDirectory; if(start.UseShellExecute){ start.RedirectStandardOutput = false; start.RedirectStandardError = false; start.RedirectStandardInput = false; } else{ start.RedirectStandardOutput = true; start.RedirectStandardError = true; start.RedirectStandardInput = true; start.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8; start.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8; } p = Process.Start(start); p.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e) { UnityEngine.Debug.LogError(e.Data); }; p.Exited += delegate(object sender, System.EventArgs e) { UnityEngine.Debug.LogError(e.ToString()); }; bool hasError = false; do{ string line = p.StandardOutput.ReadLine(); if(line == null){ break; } line = line.Replace("\\","/"); _queue.Add(delegate() { req.Log(0,line); }); }while(true); while(true){ string error = p.StandardError.ReadLine(); if(string.IsNullOrEmpty(error)){ break; } hasError = true; _queue.Add(delegate() { req.Log(1,error); }); } p.Close(); if(hasError){ _queue.Add(delegate() { req.Error(); }); } else { _queue.Add(delegate() { req.NotifyDone(); }); } }catch(System.Exception e){ UnityEngine.Debug.LogException(e); if(p != null){ p.Close(); } } }); return req; }