Designer:

- Re-wrote the Object Delete code. Reduced the code by 52% and increased readability.
 - Added additional search criteria to GetNodeType()
 - Removed the Application from running within a Try/Catch as it made debugging difficult.
This commit is contained in:
Scionwest_cp 2010-01-19 07:01:44 -08:00
parent a98e2bc069
commit af4dc2ad7a
2 changed files with 49 additions and 86 deletions

View file

@ -83,6 +83,20 @@ namespace MudDesigner
//find if we can get it's current type. //find if we can get it's current type.
TreeNode parentNode = node.Parent; TreeNode parentNode = node.Parent;
//Root searching.
switch (node.Text)
{
case "Currencies":
return ObjectType.Currency;
case "Realms":
return ObjectType.Realm;
case "Zones":
if (parentNode.Text == "Realms")
return ObjectType.ZoneWithinRealm;
else
return ObjectType.ZoneRoot;
}
//switch is only for ProjectInfo, Currency, un-owned Zones & Rooms //switch is only for ProjectInfo, Currency, un-owned Zones & Rooms
switch (parentNode.Text) switch (parentNode.Text)
{ {
@ -92,6 +106,13 @@ namespace MudDesigner
break; break;
case "Currencies": case "Currencies":
return ObjectType.Currency; return ObjectType.Currency;
case "Realms":
return ObjectType.Realm;
case "Zones":
if ((parentNode.Parent.Parent != null) && (parentNode.Parent.Parent.Text == "Realms"))
return ObjectType.ZoneWithinRealm;
else
return ObjectType.ZoneRoot;
} }
//Basic Root items where not found, do a deeper search now. //Basic Root items where not found, do a deeper search now.
@ -425,99 +446,48 @@ namespace MudDesigner
} }
DialogResult result; DialogResult result;
ObjectType objectType = new ObjectType();
//Check if we are deleting a realm or zone, if so inform the user that
//all zones/rooms within the object will be deleted as well.
string fullPath = treeExplorer.SelectedNode.FullPath;
TreeNode selectedNode = treeExplorer.SelectedNode; TreeNode selectedNode = treeExplorer.SelectedNode;
bool IsFile = true;
if (fullPath.Contains("Realms") || fullPath.Contains("Zones")) result = MessageBox.Show("Are you sure you want to delete this object?\n\nAll objects contained within it will be deleted too!", "Mud Designer", MessageBoxButtons.YesNo);
{
//ask if we want to delete this
result = MessageBox.Show("Are you sure you want to delete"
+ treeExplorer.SelectedNode.Text + "?\nAll Rooms or Zones within this item will be deleted!", "Mud Designer", MessageBoxButtons.YesNo);
}
else
//ask if we want to delete this
result = MessageBox.Show("Are you sure you want to delete"
+ treeExplorer.SelectedNode.Text + "?", "Mud Designer", MessageBoxButtons.YesNo);
//User hit no, cancel //User hit no, cancel
if (result == DialogResult.No) if (result == DialogResult.No)
return; return;
//Find out what we are deleting //Check if the object is a Zone owned by a Realm.
if (Path.GetExtension(fullPath) == "") //If so, remove the zone from the realms zone collection
{ if (GetNodeType(selectedNode) == ObjectType.ZoneWithinRealm)
if (selectedNode.Text == "Zones")
objectType = ObjectType.Zone;
else if (selectedNode.Text == "Rooms")
objectType = ObjectType.Room;
}
else if (Path.GetExtension(fullPath) == ".Room")
{
objectType = ObjectType.Room;
}
else if (Path.GetExtension(fullPath) == "Zone")
{
objectType = ObjectType.Zone;
}
if (objectType == ObjectType.Zone)
{
Zone z = new Zone();
string filename = Path.Combine(Application.StartupPath, treeExplorer.SelectedNode.FullPath);
if (Path.GetExtension(treeExplorer.SelectedNode.FullPath) == "")
{
string[] zone = Directory.GetFiles(filename, "*.zone");
if (zone.Length != 0)
{
filename = zone[0];
}
else
{
Directory.Delete(treeExplorer.SelectedNode.FullPath, true);
return;
}
}
z = (Zone)FileManager.Load(filename, z);
if (z.Realm != "")
{
string projectPath = Path.Combine(Application.StartupPath, "Project");
string[] files = Directory.GetFiles(Path.Combine(projectPath, "Realms"), "*.realm", SearchOption.AllDirectories);
foreach (string file in files)
{ {
Realm r = new Realm(); Realm r = new Realm();
r = (Realm)FileManager.Load(file, r); string filename = "";
if (r.Name == z.Realm) if (IsFile)
{ {
r.Zones.Remove(z.Filename); string realmName = selectedNode.Parent.Parent.Parent.Text;
FileManager.Save(file, r); filename = Path.Combine(realmName, realmName + ".realm");
break; filename = Path.Combine(FileManager.GetDataPath(SaveDataTypes.Realms), filename);
}
}
}
}//end if(object is zone)
else if (objectType == ObjectType.Room)
{
//TODO Delete rooms from owning zone
}
//Its a directory to delete if we have no extension assigned to it
if (Path.GetExtension(treeExplorer.SelectedNode.FullPath) == "")
{
Directory.Delete(Path.Combine(Application.StartupPath, treeExplorer.SelectedNode.FullPath), true);
} }
else else
{ {
string filename = Path.GetFileName(treeExplorer.SelectedNode.FullPath); string realmName = selectedNode.Parent.Parent.Text;
fullPath = treeExplorer.SelectedNode.FullPath; filename = Path.Combine(realmName, realmName + ".realm");
string deletePath = fullPath.Substring(0, fullPath.Length - filename.Length); filename = Path.Combine(FileManager.GetDataPath(SaveDataTypes.Realms), filename);
File.Delete(Path.Combine(Application.StartupPath, treeExplorer.SelectedNode.FullPath));
Directory.Delete(Path.Combine(Application.StartupPath, deletePath), true);
} }
filename = Path.Combine(Application.StartupPath, filename);
r = (Realm)r.Load(filename);
if (IsFile)
r.Zones.Remove(selectedNode.Parent.Text);
else
r.Zones.Remove(selectedNode.Text);
}
//Delete the object.
if (IsFile)
Directory.Delete(selectedNode.Parent.FullPath, true);
else
Directory.Delete(selectedNode.FullPath, true);
//Just incase we have the zone or the realm selected that the zone belonged too. //Just incase we have the zone or the realm selected that the zone belonged too.
//users can re-save the current realm and if it contained the zone we just deleted //users can re-save the current realm and if it contained the zone we just deleted
//the zone will be still be saved as part of the realm. //the zone will be still be saved as part of the realm.

View file

@ -29,15 +29,8 @@ namespace MudDesigner
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
//Run the toolkit //Run the toolkit
try
{
CurrentEditor = new Designer(); CurrentEditor = new Designer();
Application.Run(CurrentEditor); Application.Run(CurrentEditor);
} }
catch (Exception msg)
{
MessageBox.Show("Critical error!\n\n" + msg + "\n\nMud Designer shut down occured.", "Mud Designer", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
} }
} }