Example #1
0
        /// <summary>
        /// Tries to load a map.
        /// </summary>
        /// <param name="fileName">The path to the map file.</param>
        /// <returns>Wheter it successded in loading the map.</returns>
        private bool TryLoadMap(string fileName)
        {
            // if the map is already loaded.
            if (Globals.loadedMaps.Exists(x => x.path == fileName) == true)
            {
                return(true);
            }

            try
            {
                // read the map and deserialize it
                string readFile = File.ReadAllText(fileName);
                Map    map      = IOManager.DeSerializeWithoutLayers(readFile, out string layerData);

                // Get the tilesets that the map needs
                List <Tileset> toLoad = map.tilesets;
                for (int i = 0; i < toLoad.Count; i++)
                {
                    Tileset tileset = Globals.loadedTilesets.FirstOrDefault(x => x.path == toLoad[i].path);
                    // is the tileset not loaded?
                    if (tileset == null)
                    {
                        try
                        {
                            // try to load the map.
                            if (TryLoadTileset(toLoad[i].path, out tileset) == false)
                            {
                                tileset = null;
                            }
                        }
                        catch (Exception) { }

                        if (tileset == null) // could not load the tileset. Tilesets should now be reselected.
                        {
                            if (MessageBox.Show(this, "Tileset not found! Please reselect the exact tileset!\n" +
                                                "If an incorrect one is selected, then the map might not load or load incorrectly!\n" +
                                                "The needed tileset is called " + toLoad[i].name +
                                                "\nAnd was previously at this location:\n" + toLoad[i].path,
                                                "Error", MessageBoxButtons.OKCancel) != DialogResult.OK)
                            {
                                return(false);
                            }

                            if (TrySelectTileset(out tileset) == false)
                            {
                                return(false);
                            }
                        }
                    }

                    toLoad[i] = tileset;
                } // end for load all tilesets

                // attach missing fileds to the map.
                map.tilesets = toLoad;
                map.path     = fileName;
                IOManager.AddLayerData(map, layerData);

                Globals.loadedMaps.Add(map);
            }
            catch (Exception) // if anything went wrong show an error that the map could not be loaded.
            {
                MessageBox.Show(this, "Could not load the map.", "Error", MessageBoxButtons.OK);
                return(false);
            }

            // Notify all relevant objects that a map was loaded.
            RecentFiles.OpenedMap(fileName);
            UpdateTabs(Globals.loadedTilesets, tilesetTabControl, TilesetTabControl_SelectedIndexChanged);
            UpdateTabs(Globals.loadedMaps, mapTabControl, MapTabControl_SelectedIndexChanged);
            mapTabControl.SelectedIndex = Globals.loadedMaps.Count - 1;
            return(true);
        }