public CustomLevelSelectSection() { levelList = new List <CustomLevel>(); isLoading = true; isLoadingAnimation = 1f; ThreadPool.UnsafeQueueUserWorkItem(_ => { JsonParser json = new JsonParser(); try { string path = PathOp.Combine(DualityApp.DataDirectory, "Episodes", "unknown"); if (DirectoryOp.Exists(path)) { foreach (string levelPath in DirectoryOp.GetFiles(path)) { if (api == null) { break; } if (!levelPath.EndsWith(".level")) { continue; } IFileSystem levelPackage = new CompressedContent(levelPath); using ( Stream s = levelPackage.OpenFile(".res", FileAccessMode.Read)) { string levelToken = PathOp.GetFileNameWithoutExtension(levelPath); LevelConfigJson config = json.Parse <LevelConfigJson>(s); string icon = ""; #if DEBUG if ((config.Description.Flags & LevelFlags.FastCamera) != 0) { icon += " Fast"; } if ((config.Description.Flags & LevelFlags.HasPit) != 0) { icon += " Pit"; } #endif if ((config.Description.Flags & LevelFlags.Multiplayer) != 0 && (config.Description.Flags & (LevelFlags.MultiplayerRace | LevelFlags.MultiplayerFlags)) == 0) { icon += " Battle"; } if ((config.Description.Flags & LevelFlags.MultiplayerRace) != 0) { icon += " Race"; } if ((config.Description.Flags & LevelFlags.MultiplayerFlags) != 0) { icon += " CTF"; } levelList.Add(new CustomLevel(config.Description.Name, "unknown", levelToken, icon)); } } levelList.Sort((x, y) => { string xs = BitmapFont.StripFormatting(x.DisplayName); string ys = BitmapFont.StripFormatting(y.DisplayName); int i = string.Compare(xs, ys, StringComparison.InvariantCulture); if (i != 0) { return(i); } return(string.Compare(x.LevelName, y.LevelName, StringComparison.InvariantCulture)); }); } } catch { // ToDo: Handle exceptions } finally { isLoading = false; } }, null); }
private void LoadLevel(string level, string episode) { string levelPath = PathOp.Combine(DualityApp.DataDirectory, "Episodes", episode, level); using (Stream s = FileOp.Open(PathOp.Combine(levelPath, ".res"), FileAccessMode.Read)) { // ToDo: Cache parser, move JSON parsing to ContentResolver JsonParser json = new JsonParser(); LevelConfigJson config = json.Parse <LevelConfigJson>(s); if (config.Version.LayerFormat > LayerFormatVersion || config.Version.EventSet > EventSetVersion) { throw new NotSupportedException("Version not supported"); } Console.WriteLine("Loading level \"" + config.Description.Name + "\"..."); root.Title = BitmapFont.StripFormatting(config.Description.Name); root.Immersive = false; defaultNextLevel = config.Description.NextLevel; defaultSecretLevel = config.Description.SecretLevel; ambientLightDefault = config.Description.DefaultLight; ambientLightCurrent = ambientLightTarget = ambientLightDefault * 0.01f; if (config.Description.DefaultDarkness != null && config.Description.DefaultDarkness.Count >= 4) { darknessColor = new Vector4(config.Description.DefaultDarkness[0] / 255f, config.Description.DefaultDarkness[1] / 255f, config.Description.DefaultDarkness[2] / 255f, config.Description.DefaultDarkness[3] / 255f); } else { darknessColor = new Vector4(0, 0, 0, 1); } // Palette { ColorRgba[] tileMapPalette; string levelPalette = PathOp.Combine(levelPath, ".palette"); if (FileOp.Exists(levelPalette)) { tileMapPalette = TileSet.LoadPalette(levelPalette); } else { string tilesetPath = PathOp.Combine(DualityApp.DataDirectory, "Tilesets", config.Description.DefaultTileset); tileMapPalette = TileSet.LoadPalette(PathOp.Combine(tilesetPath, ".palette")); } ContentResolver.Current.ApplyBasePalette(tileMapPalette); } // Tileset tileMap = new TileMap(this, config.Description.DefaultTileset, (config.Description.Flags & LevelFlags.HasPit) != 0); // Additional tilesets if (config.Tilesets != null) { for (int i = 0; i < config.Tilesets.Count; i++) { LevelConfigJson.TilesetSection part = config.Tilesets[i]; tileMap.ReadTilesetPart(part.Name, part.Offset, part.Count); } } // Read all layers config.Layers.Add("Sprite", new LevelConfigJson.LayerSection { XSpeed = 1, YSpeed = 1 }); foreach (var layer in config.Layers.OrderBy(layer => layer.Value.Depth)) { LayerType type; if (layer.Key == "Sprite") { type = LayerType.Sprite; } else if (layer.Key == "Sky") { type = LayerType.Sky; if (layer.Value.BackgroundStyle != 0 /*Plain*/ && layer.Value.BackgroundColor != null && layer.Value.BackgroundColor.Count >= 3) { camera.GetComponent <Camera>().ClearColor = new ColorRgba((byte)layer.Value.BackgroundColor[0], (byte)layer.Value.BackgroundColor[1], (byte)layer.Value.BackgroundColor[2]); } } else { type = LayerType.Other; } tileMap.ReadLayerConfiguration(type, levelPath, layer.Key, layer.Value); } // Read animated tiles string animTilesPath = PathOp.Combine(levelPath, "Animated.tiles"); if (FileOp.Exists(animTilesPath)) { tileMap.ReadAnimatedTiles(animTilesPath); } CameraController controller = camera.GetComponent <CameraController>(); controller.ViewRect = new Rect(tileMap.Size * tileMap.Tileset.TileSize); // Read events eventMap = new EventMap(this, tileMap.Size); string eventsPath = PathOp.Combine(levelPath, "Events.layer"); if (FileOp.Exists(animTilesPath)) { eventMap.ReadEvents(eventsPath, config.Version.LayerFormat, difficulty); } levelTexts = config.TextEvents ?? new Dictionary <int, string>(); GameObject tilemapHandler = new GameObject("TilemapHandler"); tilemapHandler.Parent = rootObject; tilemapHandler.AddComponent(tileMap); // Load default music musicPath = PathOp.Combine(DualityApp.DataDirectory, "Music", config.Description.DefaultMusic); music = DualityApp.Sound.PlaySound(new OpenMptStream(musicPath)); music.BeginFadeIn(0.5f); if (config.Description.DefaultWeather != WeatherType.None) { ApplyWeather( config.Description.DefaultWeather, config.Description.DefaultWeatherIntensity, config.Description.DefaultWeatherOutdoors); } } }