public async UniTask <Level> LoadOrInstallBuiltInLevel(string id, LevelType loadType, bool forceInstall = false) { async UniTask <Level> GetLevel() { var levels = await LoadFromMetadataFiles(loadType, new List <string> { $"{loadType.GetDataPath()}/{id}/level.json" }); if (levels.Count > 0) { return(levels.First()); } return(LoadedLocalLevels.ContainsKey(id) ? LoadedLocalLevels[id] : null); } var level = forceInstall ? null : await GetLevel(); if (level == null) { var paths = await Context.LevelManager.CopyBuiltInLevelsToDownloads(new List <string> { id }); await Context.LevelManager.InstallLevels(paths, loadType); level = await GetLevel(); } return(level); }
public async UniTask <List <Level> > LoadLevelsOfType(LevelType type) { try { Directory.CreateDirectory(type.GetDataPath()); } catch (Exception error) { Debug.LogError("Failed to create data folder."); Debug.LogError(error); return(new List <Level>()); } var jsonPaths = Directory.EnumerateDirectories(type.GetDataPath()) .SelectMany(it => Directory.EnumerateFiles(it, "level.json")) .ToList(); Debug.Log($"Found {jsonPaths.Count} levels with type {type}"); return(await LoadFromMetadataFiles(type, jsonPaths)); }
public async UniTask <List <string> > InstallLevels(List <string> packagePaths, LevelType type) { var loadedLevelJsonFiles = new List <string>(); var index = 1; foreach (var levelFile in packagePaths) { var fileName = Path.GetFileNameWithoutExtension(levelFile); OnLevelInstallProgress.Invoke(fileName, index, packagePaths.Count); var destFolder = $"{type.GetDataPath()}/{fileName}"; if (await UnpackLevelPackage(levelFile, destFolder)) { loadedLevelJsonFiles.Add(destFolder + "/level.json"); Debug.Log($"Installed {index}/{packagePaths.Count}: {levelFile}"); } else { Debug.LogWarning($"Could not install {index}/{packagePaths.Count}: {levelFile}"); } try { File.Delete(levelFile); } catch (Exception e) { Debug.LogError(e); Debug.LogError($"Could not delete level file at {levelFile}"); } index++; } return(loadedLevelJsonFiles); }