private static void HandlePlayMode(PlayModeStateChange state)
 {
     if (state == PlayModeStateChange.ExitingPlayMode)
     {
         Debug.Log("Disconnecting before leaving playmode");
         API.Disconnect();
     }
 }
        async void LinkTask(SimulatorInfo simInfo)
        {
            try
            {
                var stream = await API.Connect(simInfo);

                string line;
                using var reader = new StreamReader(stream);
                while (true)
                {
                    var lineTask = reader.ReadLineAsync();
                    if (await Task.WhenAny(lineTask, Task.Delay(30000)) != lineTask)
                    {
                        Debug.Log("Took to long to link to cluster, aborting");
                        return;
                    }
                    line = lineTask.Result;

                    if (line == null)
                    {
                        break;
                    }

                    if (line.StartsWith("data:") && !string.IsNullOrEmpty(line.Substring(6)))
                    {
                        JObject deserialized = JObject.Parse(line.Substring(5));
                        if (deserialized != null && deserialized.HasValues)
                        {
                            var status = deserialized.GetValue("status");
                            if (status != null)
                            {
                                switch (status.ToString())
                                {
                                case "Unrecognized":
                                    Application.OpenURL(Config.CloudUrl + "/clusters/link?token=" + simInfo.linkToken);
                                    break;

                                case "OK":
                                    Debug.Log("appear to be linked!");
                                    Refresh();
                                    linked = true;
                                    return;

                                default:
                                    return;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Debug.LogError("error linking editor instance to wise");
                Debug.LogException(e);
            }
            finally
            {
                Debug.Log("closing api");
                API.Disconnect();
            }
        }
        async void Refresh()
        {
            if (EditorApplication.isPlayingOrWillChangePlaymode)
            {
                return;
            }

            try
            {
                updating     = true;
                ErrorMessage = "";
                Simulator.Web.Config.ParseConfigFile();

                DatabaseManager.Init();
                var            csservice = new Simulator.Database.Services.ClientSettingsService();
                ClientSettings cls       = csservice.GetOrMake();
                Config.SimID = cls.simid;
                if (String.IsNullOrEmpty(Config.CloudUrl))
                {
                    ErrorMessage = "Cloud URL not set";
                    return;
                }
                if (String.IsNullOrEmpty(Config.SimID))
                {
                    ErrorMessage = "Simulator not linked";
                    return;
                }

                if (API != null)
                {
                    API.Disconnect();
                }

                API = new CloudAPI(new Uri(Config.CloudUrl), Config.SimID);
                var simInfo = CloudAPI.GetInfo();

                var reader = await API.Connect(simInfo);

                await API.EnsureConnectSuccess();

                var ret = await API.GetLibrary <VehicleDetailData>();

                CloudVehicles = ret.ToList();

                string[] guids = AssetDatabase.FindAssets("t:Prefab", new[] { "Assets/External/Vehicles" });
                LocalVehicles = guids.Select(g => AssetDatabase.GUIDToAssetPath(g)).ToList();

                string idOrPath = null;
                if (DeveloperSimulation.Vehicles != null) // get previously selected thing
                {
                    // we abuse VehicleData.Id to store the prefab path
                    idOrPath = DeveloperSimulation.Vehicles[0].Id;
                }

                if (idOrPath != null)
                {
                    // find index of previously selected thing in new dataset
                    var vehicleChoices = VehicleChoices;
                    var selected       = vehicleChoices.FindIndex(v => v.idOrPath == idOrPath);
                    SetVehicleFromSelectionIndex(vehicleChoices, selected);

                    await UpdateCloudVehicleDetails();
                }

                DeveloperSimulation.NPCs = Config.NPCVehicles.Values.ToArray(); // TODO get from cloud and refresh config.cs LoadExternalAssets()
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
                ErrorMessage = ex.Message;
                if (ex.InnerException != null)
                {
                    ErrorMessage += "\n" + ex.InnerException.Message;
                }
                API.Disconnect();
            }
            finally
            {
                updating = false;
                Repaint();
            }
        }
 public void OnBeforeAssemblyReload()
 {
     API.Disconnect();
 }