/// <summary> /// Opens the file. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> private void OpenFile(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); //openFileDialog.InitialDirectory = // SettingsManager.GetInstance()["OpenFileDialog-Path"].Get( // Environment.GetFolderPath(Environment.SpecialFolder.Personal)); // added correct .NET Property Settings openFileDialog.InitialDirectory = Properties.Settings.Default.PathToMaps; openFileDialog.Filter = "Level Files (*.lvl)|*.lvl|All Files (*.*)|*.*"; if (openFileDialog.ShowDialog(this) == DialogResult.OK) { string FileName = openFileDialog.FileName; //SettingsManager.GetInstance()["OpenFileDialog-Path"].Set(Path.GetDirectoryName(openFileDialog.FileName)); // save the Open-Maps-Path to the settings Properties.Settings.Default.PathToMaps = Path.GetDirectoryName(openFileDialog.FileName); if (FileName.EndsWith("lvl")) { MapManagerXML MapManager = new MapManagerXML(); Map map = MapManager.Load(FileName); if (map != null) // Error occured while loading { // Checking for tilesets List <uint> list = new List <uint>(); for (int y = 0; y < map.Height; y++) // Scan mapData for used tiles { for (int x = 0; x < map.Width; x++) { uint graphicID = (map.GetField(x, y).GetGraphicID()) >> 16; int index = list.IndexOf(graphicID); if (index == -1) // Tile not yet in list { list.Add(graphicID); } } } int ind; // Scan map objects for used tiles IMapObject[] mapobjects = map.GetObjects(); foreach (IMapObject obj in mapobjects) { byte[] data = obj.GetExportData(list); if (data[0] == 1) // Static object { StaticMapObject obj2 = (StaticMapObject)obj; for (int y = 0; y < obj2.SizeY; y++) { for (int x = 0; x < obj2.SizeX; x++) { uint graphicID = (obj2.Tiles.GetField(x, y).GetGraphicID()) >> 16; ind = list.IndexOf(graphicID); if (ind == -1) { list.Add(graphicID); } } } } if (data[0] == 4) // Door object { DoorMapObject obj2 = (DoorMapObject)obj; ind = list.IndexOf((obj2.Graphic_id_open) >> 16); if (ind == -1) { list.Add((obj2.Graphic_id_open) >> 16); } ind = list.IndexOf((obj2.Graphic_id_closed) >> 16); if (ind == -1) { list.Add((obj2.Graphic_id_closed) >> 16); } } if (data[0] == 5) // Container object { ContainerMapObject obj2 = (ContainerMapObject)obj; ind = list.IndexOf((obj2.Graphic_id_open) >> 16); if (ind == -1) { list.Add((obj2.Graphic_id_open) >> 16); } ind = list.IndexOf((obj2.Graphic_id_closed) >> 16); if (ind == -1) { list.Add((obj2.Graphic_id_closed) >> 16); } } if (data[0] == 6) // Breakable object { BreakableMapObject obj2 = (BreakableMapObject)obj; ind = list.IndexOf((obj2.Graphic_id_open) >> 16); if (ind == -1) { list.Add((obj2.Graphic_id_open) >> 16); } ind = list.IndexOf((obj2.Graphic_id_closed) >> 16); if (ind == -1) { list.Add((obj2.Graphic_id_closed) >> 16); } } if (data[0] == 7) // Movable object { MovableMapObject obj2 = (MovableMapObject)obj; ind = list.IndexOf((obj2.Graphic_id) >> 16); if (ind == -1) { list.Add((obj2.Graphic_id) >> 16); } } } // Done scanning used tilesets string message = ""; bool ready = true; foreach (uint tileset in list) { if (!(tilesetToolbox.TileControl.isLoaded((int)tileset))) { ready = false; message = message + tileset.ToString() + " "; } } if (ready) // All tilesets l { CreateMapForm(FileName, map); /* * * MapForm childForm = new MapForm(); * * childForm.Map = map; * * * childForm.MdiParent = this; * childForm.Text = FileName;// "Map " + childFormNumber++; * childForm.Show(dockPanel); * childForm.EditorMDI = this; * childForm.TileControl = tilesetToolbox.TileControl; * childForm.BrushOptions = brushToolbox.BrushOptions; * childForm.UndoControl = undoToolbox.UndoControl; * childForm.MapObjectInspector = mapObjectInspectorToolbox.MapObjectInspector; */ } else // Missing tilesets { //MessageBox.Show("Missing tilesets:" + message); // Todo: Add dialog to allow tileset substitution MapForm childForm = new MapForm(); childForm.Map = map; MissingTilesetForm missingTilesetForm = new MissingTilesetForm(childForm.MapControl, tilesetToolbox.TileControl); DialogResult result = missingTilesetForm.ShowDialog(); if (result == DialogResult.OK) { childForm.MdiParent = this; childForm.Text = FileName;// "Map " + childFormNumber++; childForm.Show(dockPanel); childForm.EditorMDI = this; childForm.TileControl = tilesetToolbox.TileControl; childForm.BrushOptions = brushToolbox.BrushOptions; childForm.UndoControl = undoToolbox.UndoControl; childForm.MapObjectInspector = mapObjectInspectorToolbox.MapObjectInspector; } } } else { MessageBox.Show("Invalid file!"); } } else { MessageBox.Show("Unsupported file format!"); } // TODO: Add more file formats and error checking. } }
/// <summary> /// Tests the saving and loading functions /// </summary> /// <returns></returns> private bool MapSaveLoadTest(Map map) { MapManagerXML mapManager = new MapManagerXML(); try { Console.WriteLine(" - Saving map"); mapManager.Save("UnitTest.lvl", map, null); Console.WriteLine(" - Loading map"); Map newmap = mapManager.Load("UnitTest.lvl"); // Compare maps (Map.Equals() not implemented) bool result = true; bool fieldtest = true; bool paramtest = true; bool objecttest = true; Console.WriteLine(" - Comparing fields"); // Compare fields for (int y = 0; y < map.Height; y++) { for (int x = 0; x < map.Width; x++) { if (!map.GetField(x, y).Equals(newmap.GetField(x, y))) { fieldtest = false; } } } if (!fieldtest) { result = ErrorMessage("Failed: Field test, "); } // Compare parameters if (map.StartX != newmap.StartX) { paramtest = false; } if (map.StartY != newmap.StartY) { paramtest = false; } if (map.Version != newmap.Version) { paramtest = false; } if (map.Width != newmap.Width) { paramtest = false; } if (map.Height != newmap.Height) { paramtest = false; } if (map.Author != newmap.Author) { paramtest = false; } if (!paramtest) { result = ErrorMessage("Failed: Parameters, "); } bool objectexists; Console.Write(" - Comparing objects."); // Compare objects int j = 0; foreach (IMapObject obj in map.GetObjects()) { objectexists = false; j++; if (j % 1000 == 0) { Console.Write("."); } foreach (IMapObject obj2 in newmap.GetObjects()) { if (obj.Equals(obj2)) { objectexists = true; } } if (!objectexists) { objecttest = false; } } Console.WriteLine(); if (!objecttest) { result = ErrorMessage("Failed: Objects, "); } return(result); } catch (Exception ex) { infoMessage += ex.Message; return(false); } }