Advertisement
Guest User

Untitled

a guest
Jul 24th, 2022
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Diagnostics;
  4. using Slapshot.Backbone.Messages;
  5.  
  6. public class MtrRunner : MonoBehaviour {
  7.  
  8.     private Process process = null;
  9.  
  10.     public void StartReport(string endpoint, int cycles) {
  11.         if(!Utils.IsSteamPlatform()) {
  12.             UnityEngine.Debug.Log("Unable to start MTR report, platform not supported");
  13.             return;
  14.         }
  15.  
  16.         if(process != null && process.HasExited) {
  17.             process = null;
  18.         }
  19.  
  20.         if(process != null) {
  21.             UnityEngine.Debug.Log("Unable to start MTR report, one is already in progress");
  22.             return;
  23.         }
  24.  
  25.         try {
  26.             process = new Process() {
  27.                 EnableRaisingEvents = false,
  28.                 StartInfo = {
  29. #if UNITY_EDITOR
  30.                     FileName = $"{Application.dataPath}/plugins/SlapshotMTR.exe",
  31. #else
  32.                     FileName = $"{Application.dataPath}/Managed/SlapshotMTR.exe",
  33. #endif
  34.                     Arguments = $"{endpoint} {cycles}",
  35.                     UseShellExecute = false,
  36.                     CreateNoWindow = true,
  37.                     RedirectStandardOutput = true,
  38.                     RedirectStandardInput = true,
  39.                     RedirectStandardError = true,
  40.                     WindowStyle = ProcessWindowStyle.Hidden,
  41.                 },
  42.             };
  43.  
  44.             process.OutputDataReceived += new DataReceivedEventHandler(DataReceived);
  45.             process.ErrorDataReceived += new DataReceivedEventHandler(ErrorReceived);
  46.             process.Exited += (object sender, EventArgs e) => KillProcess();
  47.  
  48.             process.Start();
  49.             process.BeginOutputReadLine();
  50.  
  51.             UnityEngine.Debug.Log("Successfully started MTR Runner...");
  52.         } catch (Exception e) {
  53.             UnityEngine.Debug.LogError("Unable to started MTR Runner: " + e.Message);
  54.         }
  55.     }
  56.  
  57.     private void DataReceived(object sender, DataReceivedEventArgs eventArgs) {
  58.         UnityMainThreadDispatcher.Instance().Enqueue(() => {
  59.             UnityEngine.Debug.Log(eventArgs.Data);
  60.             if(!Utils.IsJsonString(eventArgs.Data)) {
  61.                 return;
  62.             }
  63.             BackboneSocket socket = AppManager.Instance.BackboneSocket;
  64.             if(socket == null || !socket.IsConnected) {
  65.                 UnityEngine.Debug.Log("Not connected to the Slapshot Services, cannot send MTR report");
  66.                 return;
  67.             }
  68.             // Send it over
  69.             UnityEngine.Debug.Log("Sending over latency data");
  70.             socket.Send(new MTRReportMessage(eventArgs.Data));
  71.         });
  72.     }
  73.  
  74.     private void ErrorReceived(object sender, DataReceivedEventArgs eventArgs) {
  75.         UnityMainThreadDispatcher.Instance().Enqueue(() => {
  76.             UnityEngine.Debug.LogError(eventArgs.Data);
  77.         });
  78.     }
  79.  
  80.     public void OnApplicationQuit() {
  81.         KillProcess();
  82.     }
  83.  
  84.     private void KillProcess() {
  85.         if (process != null && !process.HasExited) {
  86.             process.Kill();
  87.         }
  88.         process = null;
  89.     }
  90.  
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement