private static void DownloadMapFromUrl(LoadScene sourceCommand, JSONNode args, string name, int?seed, string url) { //Remove url from args, so download won't be retried args.Remove("url"); var localPath = WebUtilities.GenerateLocalPath("Maps"); DownloadManager.AddDownloadToQueue(new Uri(url), localPath, null, (success, ex) => { if (success) { var map = new MapModel() { Name = name, Url = url, LocalPath = localPath }; using (var db = DatabaseManager.Open()) { db.Insert(map); } ApiManager.Instance.StartCoroutine(LoadMapAssets(sourceCommand, map, name, seed)); } else { Debug.LogError( $"Vehicle '{name}' is not available. Error occured while downloading from url: {ex}."); ApiManager.Instance.SendError(sourceCommand, $"Vehicle '{name}' is not available"); ApiManager.Instance.ActionsSemaphore.Unlock(); } }); }
static IEnumerator LoadMapAssets(LoadScene sourceCommand, MapDetailData map, string localPath, string userMapId, int?seed = null) { var api = ApiManager.Instance; AssetBundle textureBundle = null; AssetBundle mapBundle = null; ZipFile zip = new ZipFile(localPath); try { Manifest manifest; ZipEntry entry = zip.GetEntry("manifest.json"); using (var ms = zip.GetInputStream(entry)) { int streamSize = (int)entry.Size; byte[] buffer = new byte[streamSize]; streamSize = ms.Read(buffer, 0, streamSize); manifest = Newtonsoft.Json.JsonConvert.DeserializeObject <Manifest>(Encoding.UTF8.GetString(buffer)); } if (manifest.assetFormat != BundleConfig.Versions[BundleConfig.BundleTypes.Environment]) { api.SendError(sourceCommand, "Out of date Map AssetBundle. Please check content website for updated bundle or rebuild the bundle."); sourceCommand.Executed?.Invoke(sourceCommand); yield break; } if (zip.FindEntry($"{manifest.assetGuid}_environment_textures", true) != -1) { var texStream = zip.GetInputStream(zip.GetEntry($"{manifest.assetGuid}_environment_textures")); textureBundle = AssetBundle.LoadFromStream(texStream, 0, 1 << 20); } string platform = SystemInfo.operatingSystemFamily == OperatingSystemFamily.Windows ? "windows" : "linux"; var mapStream = zip.GetInputStream(zip.GetEntry($"{manifest.assetGuid}_environment_main_{platform}")); mapBundle = AssetBundle.LoadFromStream(mapStream, 0, 1 << 20); if (mapBundle == null) { api.SendError(sourceCommand, $"Failed to load environment from '{map.AssetGuid}' asset bundle '{map.Name}'"); sourceCommand.Executed?.Invoke(sourceCommand); yield break; } textureBundle?.LoadAllAssets(); var scenes = mapBundle.GetAllScenePaths(); if (scenes.Length != 1) { api.SendError(sourceCommand, $"Unsupported environment in '{map.AssetGuid}' asset bundle '{map.Name}', only 1 scene expected"); sourceCommand.Executed?.Invoke(sourceCommand); yield break; } var sceneName = Path.GetFileNameWithoutExtension(scenes[0]); var loader = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Single); yield return(new WaitUntil(() => loader.isDone)); if (Loader.Instance.SimConfig != null) { Loader.Instance.SimConfig.Seed = seed; Loader.Instance.SimConfig.MapName = map.Name; Loader.Instance.SimConfig.MapAssetGuid = map.AssetGuid; } var sim = Loader.CreateSimulatorManager(); sim.Init(seed); if (Loader.Instance.CurrentSimulation != null) { Loader.Instance.reportStatus(SimulatorStatus.Running); } } finally { textureBundle?.Unload(false); mapBundle?.Unload(false); zip.Close(); } var resetTask = api.Reset(); while (!resetTask.IsCompleted) { yield return(null); } api.CurrentSceneId = map.Id; api.CurrentSceneName = map.Name; api.CurrentScene = userMapId; sourceCommand.Executed?.Invoke(sourceCommand); api.SendResult(sourceCommand); }
static IEnumerator LoadMapAssets(LoadScene sourceCommand, MapModel map, string name, int?seed = null) { var api = ApiManager.Instance; AssetBundle textureBundle = null; AssetBundle mapBundle = null; ZipFile zip = new ZipFile(map.LocalPath); try { Manifest manifest; ZipEntry entry = zip.GetEntry("manifest"); using (var ms = zip.GetInputStream(entry)) { int streamSize = (int)entry.Size; byte[] buffer = new byte[streamSize]; streamSize = ms.Read(buffer, 0, streamSize); manifest = new Deserializer().Deserialize <Manifest>(Encoding.UTF8.GetString(buffer)); } if (manifest.bundleFormat != BundleConfig.MapBundleFormatVersion) { api.SendError(sourceCommand, "Out of date Map AssetBundle. Please check content website for updated bundle or rebuild the bundle."); api.ActionsSemaphore.Unlock(); yield break; } if (zip.FindEntry($"{manifest.assetGuid}_environment_textures", true) != -1) { var texStream = zip.GetInputStream(zip.GetEntry($"{manifest.assetGuid}_environment_textures")); textureBundle = AssetBundle.LoadFromStream(texStream, 0, 1 << 20); } string platform = SystemInfo.operatingSystemFamily == OperatingSystemFamily.Windows ? "windows" : "linux"; var mapStream = zip.GetInputStream(zip.GetEntry($"{manifest.assetGuid}_environment_main_{platform}")); mapBundle = AssetBundle.LoadFromStream(mapStream, 0, 1 << 20); if (mapBundle == null) { api.SendError(sourceCommand, $"Failed to load environment from '{map.Name}' asset bundle"); api.ActionsSemaphore.Unlock(); yield break; } textureBundle?.LoadAllAssets(); var scenes = mapBundle.GetAllScenePaths(); if (scenes.Length != 1) { api.SendError(sourceCommand, $"Unsupported environment in '{map.Name}' asset bundle, only 1 scene expected"); api.ActionsSemaphore.Unlock(); yield break; } var sceneName = Path.GetFileNameWithoutExtension(scenes[0]); var clusters = Loader.Instance.SimConfig?.Clusters; var isMasterSimulation = clusters != null && clusters.Length != 0; var loadAdditive = isMasterSimulation && SceneManager.GetSceneByName(Loader.Instance.LoaderScene).isLoaded; var loader = SceneManager.LoadSceneAsync(sceneName, loadAdditive ? LoadSceneMode.Additive : LoadSceneMode.Single); yield return(new WaitUntil(() => loader.isDone)); if (loadAdditive) { SceneManager.SetActiveScene(SceneManager.GetSceneByName(sceneName)); } SIM.LogAPI(SIM.API.SimulationLoad, sceneName); if (Loader.Instance.SimConfig != null) { Loader.Instance.SimConfig.Seed = seed; Loader.Instance.SimConfig.MapName = name; Loader.Instance.SimConfig.MapUrl = map.Url; } var sim = Loader.CreateSimulatorManager(); sim.Init(seed); if (isMasterSimulation) { Loader.Instance.Network.Master.InitializeSimulation(sim.gameObject); } else if (Loader.Instance.Network.IsClient) { Loader.Instance.Network.Client.InitializeSimulation(sim.gameObject); } } finally { textureBundle?.Unload(false); mapBundle?.Unload(false); zip.Close(); } api.Reset(); api.CurrentScene = name; api.ActionsSemaphore.Unlock(); api.SendResult(sourceCommand); }