Ejemplo n.º 1
0
        public bool 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 void ProtectMap(ISender sender, ArgumentList args)
        {
            Vector2 Start = new Vector2(0, 0);
            Vector2 End = new Vector2(Main.maxTilesX, Main.maxTilesY);

            bool Restrict = args.TryPop("-res");
            bool RestrictNPC = args.TryPop("-npcres");

            string rgnName = "all";
            int count = 0;

            while (regionManager.ContainsRegion(rgnName))
            {
                //if(regionManager.ContainsRegion(rgnName))
                //{
                rgnName = "all" + count.ToString();
                count++;
                //}
                //else
                //    break;
            }

            Region rgn = new Region();
            rgn.Name = rgnName;
            rgn.Description = "A Region that protects the entire map";
            rgn.Point1 = Start;
            rgn.Point2 = End;
            rgn.Restricted = Restrict;
            rgn.RestrictedNPCs = RestrictNPC;

            if (rgn.IsValidRegion())
            {
                regionManager.Regions.Add(rgn);
                if (regionManager.SaveRegion(rgn))
                    sender.sendMessage("Region '" + rgnName + "' was successfully created.");
                else
                    sender.sendMessage("There was an issue while saving the region");
            }
            else
            {
                sender.sendMessage("There was an issue while creating the region");
            }
        }
        public void Create(ISender sender, ArgumentList args)
        {
            if (sender is Player)
            {
                string Name = String.Empty;
                string Desc = String.Empty;
                bool Restrict = args.TryPop("-res");
                bool RestrictNPC = args.TryPop("-npcres");

                string tempDesc = String.Empty;
                if (!TryFindArg(args, "-desc", out Desc))
                {
                    Desc = tempDesc;
                }

                if (TryFindArg(args, "-name", out Name) && Name.Trim().Length > 0)
                {
                    var player = sender as Player;
                    if (selection.isInSelectionlist(player))
                    {
                        Vector2[] regionAxis = selection.GetSelection(player);

                        Region rgn = new Region();
                        rgn.Name = Name;
                        rgn.Description = Desc;
                        rgn.Point1 = regionAxis[0];
                        rgn.Point2 = regionAxis[1];
                        rgn.Restricted = Restrict;
                        rgn.RestrictedNPCs = RestrictNPC;

                        if (rgn.IsValidRegion())
                        {
                            regionManager.Regions.Add(rgn);
                            if (regionManager.SaveRegion(rgn))
                                player.sendMessage("Region '" + Name + "' was successfully created.", ChatColor.Green);
                            else
                                player.sendMessage("There was an issue while saving the region", ChatColor.Red);
                        }
                        else
                        {
                            player.sendMessage("There was an issue while creating the region", ChatColor.Red);
                        }
                    }
                    else
                    {
                        player.sendMessage("You need to select a region first!", ChatColor.Red);
                    }
                }
                else
                {
                    throw new CommandError("You have not specified certain arguments");
                }
            }
        }
Ejemplo n.º 4
0
        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>();
            List <String> Projectiles    = new List <String>();
            bool          Restricted     = false;
            bool          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 "projectiles":
                    {
                        string userlist = line.Remove(0, line.IndexOf(":") + 1).Trim();
                        if (userlist.Length > 0)
                        {
                            Projectiles = userlist.Split(' ').ToList <String>();
                        }
                        break;
                    }

                    case "restricted":
                    {
                        string restricted = line.Remove(0, line.IndexOf(":") + 1).Trim();
                        bool   restrict;
                        if (Boolean.TryParse(restricted, out restrict))
                        {
                            Restricted = restrict;
                        }
                        break;
                    }

                    case "npcrestrict":
                    {
                        string restricted = line.Remove(0, line.IndexOf(":") + 1).Trim();
                        bool   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.ProjectileList = Projectiles;
            region.Restricted     = Restricted;
            region.RestrictedNPCs = RestrictedNPCs;

            return(region.IsValidRegion() ? region : null);
        }
        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>();
            List<String> Projectiles = new List<String>();
            bool Restricted = false;
            bool 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 "projectiles":
                            {
                                string userlist = line.Remove(0, line.IndexOf(":") + 1).Trim();
                                if(userlist.Length > 0)
                                    Projectiles = userlist.Split(' ').ToList<String>();
                                break;
                            }
                        case "restricted":
                            {
                                string restricted = line.Remove(0, line.IndexOf(":") + 1).Trim();
                                bool restrict;
                                if (Boolean.TryParse(restricted, out restrict))
                                    Restricted = restrict;
                                break;
                            }
                        case "npcrestrict":
                            {
                                string restricted = line.Remove(0, line.IndexOf(":") + 1).Trim();
                                bool 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.ProjectileList = Projectiles;
            region.Restricted = Restricted;
            region.RestrictedNPCs = RestrictedNPCs;

            return region.IsValidRegion() ? region : null;
        }
        public bool 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;
        }
Ejemplo n.º 7
0
        public bool SaveRegion(Region region)
        {
            FileStream fs = null;
            try
            {
                if (region != null && region.IsValidRegion())
                {
                    if (Regions.instance.mysqlenabled)
                    {
                        string regionname = region.Name.Replace("'", @"\'");
                        string regiondesc = region.Description.Replace("'", @"\'");
                        string point1 = region.Point1.X + "," + region.Point1.Y;
                        string point2 = region.Point2.X + "," + region.Point2.Y;
                        string userList = region.UserListToString().Replace("'", @"\'");
                        string projectileList = region.ProjectileListToString().Replace("'", @"\'");
                        string sql = "";
                        IDbConnection dbcon;
                        dbcon = new MySqlConnection(Regions.instance.connectionString);
                        dbcon.Open();
                        IDbCommand dbcmd = dbcon.CreateCommand();
                        if (getMysqlRegion(regionname) == null)
                        {
                            sql = "INSERT INTO terraria_regions (Name, Description, Point1, Point2, UserList, ProjectileList, Restricted, RestrictedNPCs) VALUES ('" + regionname + "', '" + regiondesc + "', '" + point1 + "', '" + point2 + "', '" + userList.Trim() + "', '" + projectileList.Trim() + "', " + region.Restricted + ", " + region.RestrictedNPCs + ")";
                        }
                        else
                        {
                            sql = "UPDATE terraria_regions SET Description = '" + regiondesc + "', Point1 = '" + point1 + "' , Point2 = '" + point2 + "', UserList = '" + userList + "', ProjectileList = '" + projectileList + "', Restricted = "+region.Restricted+", RestrictedNPCs = "+region.RestrictedNPCs+" WHERE Name = '" + regionname + "' ";
                        }
                        dbcmd.CommandText = sql;
                        IDataReader reader = dbcmd.ExecuteReader();
                        // clean up
                        reader.Close();
                        reader = null;
                        dbcmd.Dispose();
                        dbcmd = null;
                        dbcon.Close();
                        dbcon = null;

                        return true;
                    }
                    else
                    {
                        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;
        }
Ejemplo n.º 8
0
        private List<Region> LoadMysqlRegions()
        {
            List<Region> regions = new List<Region>();
            Region region;
            Vector2 Point1;
            Vector2 Point2;
            IDbConnection dbcon;
            dbcon = new MySqlConnection(Regions.instance.connectionString);
            dbcon.Open();
            IDbCommand dbcmd = dbcon.CreateCommand();
            string sql = "SELECT * FROM terraria_regions";
            dbcmd.CommandText = sql;
            IDataReader reader = dbcmd.ExecuteReader();
            while (reader.Read())
            {
                region = new Region();
                Point1 = default(Vector2);
                Point2 = default(Vector2);
                region.Name = (string)reader["Name"];
                region.Description = (string)reader["Description"];

                string line = (string)reader["Point1"];
                string[] xy = line.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);

                region.Point1 = Point1;

                string line2 = (string)reader["Point2"];
                string[] xy2 = line2.Split(',');
                float x2, y2;
                if (!(float.TryParse(xy2[0], out x2) && float.TryParse(xy2[1], out y2)))
                    Point2 = default(Vector2);
                else
                    Point2 = new Vector2(x2, y2);

                region.Point2 = Point2;
                string userlist = (string)reader["UserList"];
                region.UserList = userlist.Split(' ').ToList<String>();
                string projlist = (string)reader["ProjectileList"];
                region.ProjectileList = projlist.Split(' ').ToList<String>();
                region.Restricted = (bool)reader["Restricted"];
                region.RestrictedNPCs = (bool)reader["RestrictedNPCs"];

                if(region.IsValidRegion())
                    regions.Add(region);
            }
            // clean up
            reader.Close();
            reader = null;
            dbcmd.Dispose();
            dbcmd = null;
            dbcon.Close();
            dbcon = null;

            return regions;
        }