public Boolean SaveRegion(Region region) { FileStream fs = null; try { if (region != null && region.IsValidRegion()) { String file = SaveFolder + Path.DirectorySeparatorChar + region.Name + ".rgn"; if (File.Exists(file)) File.Delete(file); fs = File.Open(file, FileMode.CreateNew); String toWrite = region.ToString(); fs.Write(ASCIIEncoding.ASCII.GetBytes(toWrite), 0, toWrite.Length); fs.Flush(); fs.Close(); return true; } else ProgramLog.Error.Log("Region '{0}' was either null or has an issue.", (region != null && region.Name != null) ? region.Name : "??"); } catch (Exception e) { ProgramLog.Error.Log("Error saving Region {0}\n{1}", region.Name, e.Message); } finally { if (fs != null) fs.Close(); } return false; }
public Region LoadRegion(String location) { Region region = new Region(); String Name = ""; String Description = ""; Vector2 Point1 = default(Vector2); Vector2 Point2 = default(Vector2); List<String> Users = new List<String>(); Boolean Restricted = false; Boolean RestrictedNPCs = false; foreach (String line in File.ReadAllLines(location)) { if (line.Contains(":")) { String key = line.Split(':')[0]; switch (key) { case "name": { Name = line.Remove(0, line.IndexOf(":") + 1).Trim(); break; } case "description": { Description = line.Remove(0, line.IndexOf(":") + 1).Trim(); break; } case "point1": { String[] xy = line.Remove(0, line.IndexOf(":") + 1).Trim().Split(','); float x, y; if (!(float.TryParse(xy[0], out x) && float.TryParse(xy[1], out y))) Point1 = default(Vector2); else Point1 = new Vector2(x, y); break; } case "point2": { String[] xy = line.Remove(0, line.IndexOf(":") + 1).Trim().Split(','); float x, y; if (!(float.TryParse(xy[0], out x) && float.TryParse(xy[1], out y))) Point2 = default(Vector2); else Point2 = new Vector2(x, y); break; } case "users": { String userlist = line.Remove(0, line.IndexOf(":") + 1).Trim(); if(userlist.Length > 0) Users = userlist.Split(' ').ToList<String>(); break; } case "restricted": { String restricted = line.Remove(0, line.IndexOf(":") + 1).Trim(); Boolean restrict; if (Boolean.TryParse(restricted, out restrict)) Restricted = restrict; break; } case "npcrestrict": { String restricted = line.Remove(0, line.IndexOf(":") + 1).Trim(); Boolean restrict; if (Boolean.TryParse(restricted, out restrict)) RestrictedNPCs = restrict; break; } default: continue; } } } region.Name = Name; region.Description = Description; region.Point1 = Point1; region.Point2 = Point2; region.UserList = Users; region.Restricted = Restricted; region.RestrictedNPCs = RestrictedNPCs; return region.IsValidRegion() ? region : null; }
public static void Create(Server server, ISender sender, ArgumentList args) { if (sender is Player) { String Name = ""; String Desc = ""; Boolean Restrict = args.TryPop("-res"); Boolean RestrictNPC = args.TryPop("-npcres"); if (args.TryParseTwo<String, String>("-name", out Name, "-desc", out Desc) && Name.Trim().Length > 0) { var player = sender as Player; if (Selection.isInSelectionlist(player)) { Vector2[] regionAxis = Selection.GetSelection(player); Region.Region rgn = new Region.Region(); rgn.Name = Name; rgn.Description = Desc; rgn.Point1 = regionAxis[0]; rgn.Point2 = regionAxis[1]; rgn.Restricted = Restrict; rgn.RestrictedNPCs = RestrictNPC; if (rgn.IsValidRegion()) { Regions.regionManager.Regions.Add(rgn); if(Regions.regionManager.SaveRegion(rgn)) player.sendMessage("Region '" + Name + "' was successfully created.", ChatColour.Green); else player.sendMessage("There was an issue while saving the region", ChatColour.Red); } else { player.sendMessage("There was an issue while creating the region", ChatColour.Red); } } else { player.sendMessage("You need to select a region first!", ChatColour.Red); } } else { throw new CommandError("You have not specified certain arguments"); } } }