Example #1
0
        private async Task LoadMap(JSONNode args, string userMapId, int?seed = null)
        {
            var           api     = ApiManager.Instance;
            MapDetailData mapData = await ConnectionManager.API.GetByIdOrName <MapDetailData>(userMapId);

            var ret = await DownloadManager.GetAsset(BundleConfig.BundleTypes.Environment, mapData.AssetGuid, mapData.Name);

            api.StartCoroutine(LoadMapAssets(this, mapData, ret.LocalPath, userMapId, seed));
        }
Example #2
0
        private async Task LoadMap(JSONNode args, string userMapId, int?seed = null)
        {
            var           api     = ApiManager.Instance;
            MapDetailData mapData = await ConnectionManager.API.GetByIdOrName <MapDetailData>(userMapId);

            var progressUpdate = new Progress <Tuple <string, float> > (p => { ConnectionUI.instance?.UpdateDownloadProgress(p.Item1, p.Item2); });
            var ret            = await DownloadManager.GetAsset(BundleConfig.BundleTypes.Environment, mapData.AssetGuid, mapData.Name, progressUpdate);

            api.StartCoroutine(LoadMapAssets(this, mapData, ret.LocalPath, userMapId, seed));
        }
Example #3
0
        MapDetailData GetMapDetailData(Resource res, ZkDataContext db)
        {
            var data = new MapDetailData
            {
                Resource = res,
                MyRating = res.MapRatings.SingleOrDefault(x => x.AccountID == Global.AccountID) ?? new MapRating()
            };

            // load map info from disk - or used cached copy if its in memory
            var cachedEntry = HttpContext.Application["mapinfo_" + res.ResourceID] as Map;

            if (cachedEntry != null)
            {
                data.MapInfo = cachedEntry;
            }
            else
            {
                var path = Server.MapPath("~/Resources/") + res.MetadataName;

                if (System.IO.File.Exists(path))
                {
                    try {
                        data.MapInfo =
                            (Map) new XmlSerializer(typeof(Map)).Deserialize(new MemoryStream(System.IO.File.ReadAllBytes(path).Decompress()));
                        HttpContext.Application["mapinfo_" + res.ResourceID] = data.MapInfo;
                    } catch (Exception ex) {
                        Trace.TraceWarning("Failed to get map metedata {0}:{1}", res.MetadataName, ex);
                        data.MapInfo = new Map();
                        HttpContext.Application["mapinfo_" + res.ResourceID] = data.MapInfo;
                    }
                }
            }

            if (res.ForumThread != null)
            {
                res.ForumThread.UpdateLastRead(Global.AccountID, false);
                db.SaveChanges();
            }

            return(data);
        }
        MapDetailData GetMapDetailData(Resource res, ZkDataContext db) {
            var data = new MapDetailData
            {
                Resource = res,
                MyRating = res.MapRatings.SingleOrDefault(x => x.AccountID == Global.AccountID) ?? new MapRating()
            };

            // load map info from disk - or used cached copy if its in memory
            var cachedEntry = HttpContext.Application["mapinfo_" + res.ResourceID] as Map;
            if (cachedEntry != null) data.MapInfo = cachedEntry;
            else {
                var path = Server.MapPath("~/Resources/") + res.MetadataName;
                if (System.IO.File.Exists(path)) {
                    try {
                        data.MapInfo =
                            (Map)new XmlSerializer(typeof(Map)).Deserialize(new MemoryStream(System.IO.File.ReadAllBytes(path).Decompress()));
                        HttpContext.Application["mapinfo_" + res.ResourceID] = data.MapInfo;
                    } catch (Exception ex) {
                        Trace.TraceWarning("Failed to get map metedata {0}:{1}", res.MetadataName, ex);
                        data.MapInfo = new Map();
                    }
                }
            }

            if (res.ForumThread != null) {
                res.ForumThread.UpdateLastRead(Global.AccountID, false);
                db.SubmitChanges();
            }

            return data;
        }
Example #5
0
        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);
        }