/// <summary> /// Finds and deletes the MapIds for maps that exist in the database but not on file, or vise versa. /// </summary> public static MapID[] DeleteMissingMapIds(bool showConfirmation = true) { MapID[] missingIds = FindMissingMapIds().OrderBy(x => x).ToArray(); if (missingIds.Length == 0) { return(new MapID[0]); } if (showConfirmation) { const string confirmMsgDelete = "The following MapIds exist on file but not in the database, or vise versa." + " This normally will happen because you deleted or moved the map files directory instead of through the editor." + " Do you wish to delete them completely?{0}{0}{1}"; if (MessageBox.Show(string.Format(confirmMsgDelete, Environment.NewLine, string.Join(", ", missingIds)), "Delete incomplete maps", MessageBoxButtons.YesNo) == DialogResult.No) { return(new MapID[0]); } } var deleteMapQuery = GlobalState.Instance.DbController.GetQuery <DeleteMapQuery>(); // Delete List <MapID> deletedIds = new List <MapID>(); foreach (MapID id in missingIds) { try { DeleteMap(id, deleteMapQuery); deletedIds.Add(id); DeleteMiniMap(id); } catch { } } MainForm.SetStatusMessage(string.Format("Deleted {0} missing maps: {1}", deletedIds.Count, string.Join(", ", deletedIds))); return(deletedIds.ToArray()); }
/// <summary> /// Saves a map. /// </summary> /// <param name="map">The map to save.</param> /// <param name="showConfirmation">If true, a confirmation will be shown to make sure the user wants to /// perform this operation.</param> public static void SaveMap(EditorMap map, bool showConfirmation = true) { try { if (map == null) { return; } // Show confirmation if (showConfirmation) { const string confirmMsg = "Are you sure you wish to save the changes to map `{0}`?"; if (MessageBox.Show(string.Format(confirmMsg, map), "Save map?", MessageBoxButtons.YesNo) == DialogResult.No) { return; } // Allow specifying a new map ID bool firstEnterIDAttempt = true; while (true) { string displayText = "Save as map id:"; if (!firstEnterIDAttempt) { displayText = "Invalid map id entered. Please enter a numeric value greater than 0." + Environment.NewLine + Environment.NewLine + displayText; } firstEnterIDAttempt = false; int newId; if (int.TryParse(InputBox.Show("Save as...", displayText, map.ID.ToString()), out newId)) { if (newId >= 0) { try { map.ChangeID((MapID)newId); break; } catch (Exception ex) { MessageBox.Show(string.Format("Failed to change map ID to `{0}`. Exception: {1}", newId, ex)); } } } } } // Add the MapGrh-bound walls var mapGrhs = map.Spatial.GetMany <MapGrh>().Distinct(); var extraWalls = GlobalState.Instance.MapGrhWalls.CreateWallList(mapGrhs); foreach (var wall in extraWalls) { map.AddEntity(wall); } try { // Save the map map.Save(ContentPaths.Dev, EditorDynamicEntityFactory.Instance); SaveMiniMap(map.ID, ContentPaths.Dev.Grhs + "MiniMap"); // Update the database GlobalState.Instance.DbController.GetQuery <InsertMapQuery>().Execute(map); } finally { // Pull the MapGrh-bound walls back out foreach (var wall in extraWalls) { Debug.Assert(wall.BoundGrhIndex > 0); map.RemoveEntity(wall); } } // Save successful const string savedMsg = "Successfully saved the changes to map id {0}: {1}"; MainForm.SetStatusMessage(string.Format(savedMsg, map.ID, map.Name ?? string.Empty)); } catch (Exception ex) { const string errmsg = "Failed to save map `{0}`. Exception: {1}"; if (log.IsErrorEnabled) { log.ErrorFormat(errmsg, map, ex); } Debug.Fail(string.Format(errmsg, map, ex)); } }