1010public class Command
1111{
1212 public string action ;
13+ public string uuid ;
1314 public string scenarioName ;
1415}
1516
@@ -33,6 +34,8 @@ public class Main : MonoBehaviour
3334
3435 private const string API_KEY = "a35a2a72bd230ac0aa0f52715bbdc6aa" ;
3536 private string _fixtureConfigFileName = "/fixture_config.json" ;
37+ private string _commandUuidFileName = "/command_uuid.txt" ;
38+ private static string LastCommandUuid ;
3639 public static string MazeHost ;
3740
3841 public ScenarioRunner ScenarioRunner ;
@@ -45,12 +48,36 @@ private void Awake()
4548 public IEnumerator Start ( )
4649 {
4750 Log ( "Maze Runner app started" ) ;
51+ GetLastCommandUuid ( ) ;
4852
4953 yield return GetFixtureConfig ( ) ;
5054
5155 InvokeRepeating ( "DoRunNextMazeCommand" , 0 , 1 ) ;
5256 }
5357
58+ private void GetLastCommandUuid ( )
59+ {
60+ var uuidFilePath = Application . persistentDataPath + _commandUuidFileName ;
61+ if ( File . Exists ( uuidFilePath ) )
62+ {
63+ LastCommandUuid = File . ReadAllText ( uuidFilePath ) ;
64+ }
65+ else
66+ {
67+ LastCommandUuid = "" ;
68+ }
69+ Log ( "Last command UUID is: " + LastCommandUuid ) ;
70+ }
71+
72+ private void SetLastCommandUuid ( String uuid )
73+ {
74+ Log ( "Setting last command UUID: " + uuid ) ;
75+ var uuidFilePath = Application . persistentDataPath + _commandUuidFileName ;
76+ File . WriteAllText ( uuidFilePath , uuid ) ;
77+ LastCommandUuid = uuid ;
78+ Log ( "Command UUID is now: " + LastCommandUuid ) ;
79+ }
80+
5481 private IEnumerator GetFixtureConfig ( )
5582 {
5683 if ( Application . platform == RuntimePlatform . Android ||
@@ -73,7 +100,7 @@ private IEnumerator GetFixtureConfig()
73100 {
74101 Log ( "Mazerunner no fixture config found at path: " + configPath ) ;
75102 numTries ++ ;
76- if ( numTries == timeOut )
103+ if ( numTries == timeOut )
77104 {
78105 Log ( "Timedout looking for config file!" ) ;
79106 }
@@ -105,60 +132,54 @@ private void DoRunNextMazeCommand()
105132
106133 IEnumerator RunNextMazeCommand ( )
107134 {
108- var url = MazeHost + "/command" ;
109- Log ( "Trying to get next mazerunner command with url : " + url ) ;
135+ var url = MazeHost + "/idem- command?after=" + LastCommandUuid ;
136+ Log ( "Requesting Maze Runner command from : " + url ) ;
110137 using ( UnityWebRequest request = UnityWebRequest . Get ( url ) )
111138 {
112139 yield return request . SendWebRequest ( ) ;
113- #if UNITY_2020_1_OR_NEWER
114140 var result = request != null && request . result == UnityWebRequest . Result . Success ;
115- #else
116- var result = request != null &&
117- ! request . isHttpError &&
118- ! request . isNetworkError ;
119- #endif
120141
121142 if ( result )
122143 {
123144 var response = request . downloadHandler ? . text ;
124- if ( response == null || response == "null" || response == "No commands to provide" || response . Contains ( "noop" ) )
145+ if ( response == null || response == "null" )
125146 {
126-
147+ Log ( "No Maze Runner command to process at present" ) ;
127148 }
128149 else
129150 {
130151 var command = JsonUtility . FromJson < Command > ( response ) ;
131152 if ( command != null )
132153 {
133- Log ( "Got Action: " + command . action + " and scenario: " + command . scenarioName ) ;
134- if ( "clear_cache" . Equals ( command . action ) )
135- {
136- ClearUnityCache ( ) ;
137- }
138- else if ( "run_scenario" . Equals ( command . action ) )
139- {
140- ScenarioRunner . RunScenario ( command . scenarioName , API_KEY , MazeHost ) ;
141- }
142- else if ( "close_application" . Equals ( command . action ) )
154+ Log ( "Received Maze Runner command:\n " + response ) ;
155+
156+ switch ( command . action )
143157 {
144- CloseFixture ( ) ;
158+ case "noop" :
159+ break ;
160+ case "reset_uuid" :
161+ SetLastCommandUuid ( "" ) ;
162+ break ;
163+ case "clear_cache" :
164+ ClearUnityCache ( ) ;
165+ SetLastCommandUuid ( command . uuid ) ;
166+ break ;
167+ case "run_scenario" :
168+ SetLastCommandUuid ( command . uuid ) ;
169+ ScenarioRunner . RunScenario ( command . scenarioName , API_KEY , MazeHost ) ;
170+ break ;
145171 }
146172 }
147173 }
148174 }
149175 else
150176 {
151- Log ( "Getting next mazerunner command Failed : " + request . error ) ;
177+ Log ( "Getting next Maze Runner command failed : " + request . error ) ;
152178
153179 }
154180 }
155181 }
156182
157- private void CloseFixture ( )
158- {
159- Application . Quit ( ) ;
160- }
161-
162183 private void ClearUnityCache ( )
163184 {
164185 if ( Directory . Exists ( Application . persistentDataPath + "/bugsnag-performance" ) )
@@ -173,11 +194,6 @@ private void ClearUnityCache()
173194 {
174195 ClearIOSData ( ) ;
175196 }
176- if ( Application . platform != RuntimePlatform . Android &&
177- Application . platform != RuntimePlatform . IPhonePlayer )
178- {
179- Invoke ( "CloseFixture" , 0.25f ) ;
180- }
181197 }
182198
183199 public static void ClearIOSData ( )
@@ -197,8 +213,4 @@ public static void Log(string msg)
197213 catch { }
198214
199215 }
200-
201216}
202-
203-
204-
0 commit comments