ReSharper code reformat to match naming conventions and stuff
This commit is contained in:
parent
1147788154
commit
c6abbfe4d2
45 changed files with 11639 additions and 11342 deletions
|
|
@ -23,150 +23,153 @@ using MySql.Data.MySqlClient;
|
|||
|
||||
namespace TShockAPI.DB
|
||||
{
|
||||
public class BanManager
|
||||
{
|
||||
private IDbConnection database;
|
||||
public class BanManager
|
||||
{
|
||||
private IDbConnection database;
|
||||
|
||||
public BanManager(IDbConnection db)
|
||||
{
|
||||
database = db;
|
||||
public BanManager(IDbConnection db)
|
||||
{
|
||||
database = db;
|
||||
|
||||
var table = new SqlTable("Bans",
|
||||
new SqlColumn("IP", MySqlDbType.String, 16) { Primary = true },
|
||||
new SqlColumn("Name", MySqlDbType.Text),
|
||||
new SqlColumn("Reason", MySqlDbType.Text)
|
||||
);
|
||||
var creator = new SqlTableCreator(db, db.GetSqlType() == SqlType.Sqlite ? (IQueryBuilder)new SqliteQueryCreator() : new MysqlQueryCreator());
|
||||
creator.EnsureExists(table);
|
||||
var table = new SqlTable("Bans",
|
||||
new SqlColumn("IP", MySqlDbType.String, 16) {Primary = true},
|
||||
new SqlColumn("Name", MySqlDbType.Text),
|
||||
new SqlColumn("Reason", MySqlDbType.Text)
|
||||
);
|
||||
var creator = new SqlTableCreator(db,
|
||||
db.GetSqlType() == SqlType.Sqlite
|
||||
? (IQueryBuilder) new SqliteQueryCreator()
|
||||
: new MysqlQueryCreator());
|
||||
creator.EnsureExists(table);
|
||||
|
||||
String file = Path.Combine(TShock.SavePath, "bans.txt");
|
||||
if (File.Exists(file))
|
||||
{
|
||||
using (StreamReader sr = new StreamReader(file))
|
||||
{
|
||||
String line;
|
||||
while ((line = sr.ReadLine()) != null)
|
||||
{
|
||||
String[] info = line.Split('|');
|
||||
string query;
|
||||
if (TShock.Config.StorageType.ToLower() == "sqlite")
|
||||
query = "INSERT OR IGNORE INTO Bans (IP, Name, Reason) VALUES (@0, @1, @2);";
|
||||
else
|
||||
query = "INSERT IGNORE INTO Bans SET IP=@0, Name=@1, Reason=@2;";
|
||||
db.Query(query, info[0].Trim(), info[1].Trim(), info[2].Trim());
|
||||
}
|
||||
}
|
||||
String path = Path.Combine(TShock.SavePath, "old_configs");
|
||||
String file2 = Path.Combine(path, "bans.txt");
|
||||
if (!Directory.Exists(path))
|
||||
Directory.CreateDirectory(path);
|
||||
if (File.Exists(file2))
|
||||
File.Delete(file2);
|
||||
File.Move(file, file2);
|
||||
}
|
||||
}
|
||||
String file = Path.Combine(TShock.SavePath, "bans.txt");
|
||||
if (File.Exists(file))
|
||||
{
|
||||
using (StreamReader sr = new StreamReader(file))
|
||||
{
|
||||
String line;
|
||||
while ((line = sr.ReadLine()) != null)
|
||||
{
|
||||
String[] info = line.Split('|');
|
||||
string query;
|
||||
if (TShock.Config.StorageType.ToLower() == "sqlite")
|
||||
query = "INSERT OR IGNORE INTO Bans (IP, Name, Reason) VALUES (@0, @1, @2);";
|
||||
else
|
||||
query = "INSERT IGNORE INTO Bans SET IP=@0, Name=@1, Reason=@2;";
|
||||
db.Query(query, info[0].Trim(), info[1].Trim(), info[2].Trim());
|
||||
}
|
||||
}
|
||||
String path = Path.Combine(TShock.SavePath, "old_configs");
|
||||
String file2 = Path.Combine(path, "bans.txt");
|
||||
if (!Directory.Exists(path))
|
||||
Directory.CreateDirectory(path);
|
||||
if (File.Exists(file2))
|
||||
File.Delete(file2);
|
||||
File.Move(file, file2);
|
||||
}
|
||||
}
|
||||
|
||||
public Ban GetBanByIp(string ip)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var reader = database.QueryReader("SELECT * FROM Bans WHERE IP=@0", ip))
|
||||
{
|
||||
if (reader.Read())
|
||||
return new Ban(reader.Get<string>("IP"), reader.Get<string>("Name"), reader.Get<string>("Reason"));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public Ban GetBanByIp(string ip)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var reader = database.QueryReader("SELECT * FROM Bans WHERE IP=@0", ip))
|
||||
{
|
||||
if (reader.Read())
|
||||
return new Ban(reader.Get<string>("IP"), reader.Get<string>("Name"), reader.Get<string>("Reason"));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Ban GetBanByName(string name, bool casesensitive = true)
|
||||
{
|
||||
if (!TShock.Config.EnableBanOnUsernames)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
try
|
||||
{
|
||||
var namecol = casesensitive ? "Name" : "UPPER(Name)";
|
||||
if (!casesensitive)
|
||||
name = name.ToUpper();
|
||||
using (var reader = database.QueryReader("SELECT * FROM Bans WHERE " + namecol + "=@0", name))
|
||||
{
|
||||
if (reader.Read())
|
||||
return new Ban(reader.Get<string>("IP"), reader.Get<string>("Name"), reader.Get<string>("Reason"));
|
||||
public Ban GetBanByName(string name, bool casesensitive = true)
|
||||
{
|
||||
if (!TShock.Config.EnableBanOnUsernames)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
try
|
||||
{
|
||||
var namecol = casesensitive ? "Name" : "UPPER(Name)";
|
||||
if (!casesensitive)
|
||||
name = name.ToUpper();
|
||||
using (var reader = database.QueryReader("SELECT * FROM Bans WHERE " + namecol + "=@0", name))
|
||||
{
|
||||
if (reader.Read())
|
||||
return new Ban(reader.Get<string>("IP"), reader.Get<string>("Name"), reader.Get<string>("Reason"));
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public bool AddBan(string ip, string name = "", string reason = "")
|
||||
{
|
||||
try
|
||||
{
|
||||
return database.Query("INSERT INTO Bans (IP, Name, Reason) VALUES (@0, @1, @2);", ip, name, reason) != 0;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool AddBan(string ip, string name = "", string reason = "")
|
||||
{
|
||||
try
|
||||
{
|
||||
return database.Query("INSERT INTO Bans (IP, Name, Reason) VALUES (@0, @1, @2);", ip, name, reason) != 0;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public bool RemoveBan(string ip)
|
||||
{
|
||||
try
|
||||
{
|
||||
return database.Query("DELETE FROM Bans WHERE IP=@0", ip) != 0;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool RemoveBan(string ip)
|
||||
{
|
||||
try
|
||||
{
|
||||
return database.Query("DELETE FROM Bans WHERE IP=@0", ip) != 0;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public bool ClearBans()
|
||||
{
|
||||
try
|
||||
{
|
||||
return database.Query("DELETE FROM Bans") != 0;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public bool ClearBans()
|
||||
{
|
||||
try
|
||||
{
|
||||
return database.Query("DELETE FROM Bans") != 0;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public class Ban
|
||||
{
|
||||
public string IP { get; set; }
|
||||
public class Ban
|
||||
{
|
||||
public string IP { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
public string Name { get; set; }
|
||||
|
||||
public string Reason { get; set; }
|
||||
public string Reason { get; set; }
|
||||
|
||||
public Ban(string ip, string name, string reason)
|
||||
{
|
||||
IP = ip;
|
||||
Name = name;
|
||||
Reason = reason;
|
||||
}
|
||||
public Ban(string ip, string name, string reason)
|
||||
{
|
||||
IP = ip;
|
||||
Name = name;
|
||||
Reason = reason;
|
||||
}
|
||||
|
||||
public Ban()
|
||||
{
|
||||
IP = string.Empty;
|
||||
Name = string.Empty;
|
||||
Reason = string.Empty;
|
||||
}
|
||||
}
|
||||
public Ban()
|
||||
{
|
||||
IP = string.Empty;
|
||||
Name = string.Empty;
|
||||
Reason = string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,254 +7,258 @@ using MySql.Data.MySqlClient;
|
|||
|
||||
namespace TShockAPI.DB
|
||||
{
|
||||
public class GroupManager
|
||||
{
|
||||
private IDbConnection database;
|
||||
public class GroupManager
|
||||
{
|
||||
private IDbConnection database;
|
||||
|
||||
public List<Group> groups = new List<Group>();
|
||||
public List<Group> groups = new List<Group>();
|
||||
|
||||
public GroupManager(IDbConnection db)
|
||||
{
|
||||
database = db;
|
||||
public GroupManager(IDbConnection db)
|
||||
{
|
||||
database = db;
|
||||
|
||||
var table = new SqlTable("GroupList",
|
||||
new SqlColumn("GroupName", MySqlDbType.VarChar, 32) { Primary = true },
|
||||
new SqlColumn("Parent", MySqlDbType.VarChar, 32),
|
||||
new SqlColumn("Commands", MySqlDbType.Text),
|
||||
new SqlColumn("ChatColor", MySqlDbType.Text),
|
||||
new SqlColumn("Prefix", MySqlDbType.Text),
|
||||
new SqlColumn("Suffix", MySqlDbType.Text)
|
||||
);
|
||||
var creator = new SqlTableCreator(db, db.GetSqlType() == SqlType.Sqlite ? (IQueryBuilder)new SqliteQueryCreator() : new MysqlQueryCreator());
|
||||
creator.EnsureExists(table);
|
||||
var table = new SqlTable("GroupList",
|
||||
new SqlColumn("GroupName", MySqlDbType.VarChar, 32) {Primary = true},
|
||||
new SqlColumn("Parent", MySqlDbType.VarChar, 32),
|
||||
new SqlColumn("Commands", MySqlDbType.Text),
|
||||
new SqlColumn("ChatColor", MySqlDbType.Text),
|
||||
new SqlColumn("Prefix", MySqlDbType.Text),
|
||||
new SqlColumn("Suffix", MySqlDbType.Text)
|
||||
);
|
||||
var creator = new SqlTableCreator(db,
|
||||
db.GetSqlType() == SqlType.Sqlite
|
||||
? (IQueryBuilder) new SqliteQueryCreator()
|
||||
: new MysqlQueryCreator());
|
||||
creator.EnsureExists(table);
|
||||
|
||||
//Add default groups
|
||||
AddGroup("guest", "canbuild,canregister,canlogin,canpartychat,cantalkinthird");
|
||||
AddGroup("default", "guest", "warp,canchangepassword");
|
||||
AddGroup("newadmin", "default", "kick,editspawn,reservedslot");
|
||||
AddGroup("admin", "newadmin", "ban,unban,whitelist,causeevents,spawnboss,spawnmob,managewarp,time,tp,pvpfun,kill,logs,immunetokick,tphere");
|
||||
AddGroup("trustedadmin", "admin", "maintenance,cfg,butcher,item,heal,immunetoban,usebanneditem,manageusers");
|
||||
AddGroup("vip", "default", "reservedslot");
|
||||
//Add default groups
|
||||
AddGroup("guest", "canbuild,canregister,canlogin,canpartychat,cantalkinthird");
|
||||
AddGroup("default", "guest", "warp,canchangepassword");
|
||||
AddGroup("newadmin", "default", "kick,editspawn,reservedslot");
|
||||
AddGroup("admin", "newadmin",
|
||||
"ban,unban,whitelist,causeevents,spawnboss,spawnmob,managewarp,time,tp,pvpfun,kill,logs,immunetokick,tphere");
|
||||
AddGroup("trustedadmin", "admin", "maintenance,cfg,butcher,item,heal,immunetoban,usebanneditem,manageusers");
|
||||
AddGroup("vip", "default", "reservedslot");
|
||||
|
||||
String file = Path.Combine(TShock.SavePath, "groups.txt");
|
||||
if (File.Exists(file))
|
||||
{
|
||||
using (StreamReader sr = new StreamReader(file))
|
||||
{
|
||||
String line;
|
||||
while ((line = sr.ReadLine()) != null)
|
||||
{
|
||||
if (!line.Equals("") && !line.Substring(0, 1).Equals("#"))
|
||||
{
|
||||
String[] info = line.Split(' ');
|
||||
String comms = "";
|
||||
int size = info.Length;
|
||||
for (int i = 1; i < size; i++)
|
||||
{
|
||||
if (!comms.Equals(""))
|
||||
comms = comms + ",";
|
||||
comms = comms + info[i].Trim();
|
||||
}
|
||||
String file = Path.Combine(TShock.SavePath, "groups.txt");
|
||||
if (File.Exists(file))
|
||||
{
|
||||
using (StreamReader sr = new StreamReader(file))
|
||||
{
|
||||
String line;
|
||||
while ((line = sr.ReadLine()) != null)
|
||||
{
|
||||
if (!line.Equals("") && !line.Substring(0, 1).Equals("#"))
|
||||
{
|
||||
String[] info = line.Split(' ');
|
||||
String comms = "";
|
||||
int size = info.Length;
|
||||
for (int i = 1; i < size; i++)
|
||||
{
|
||||
if (!comms.Equals(""))
|
||||
comms = comms + ",";
|
||||
comms = comms + info[i].Trim();
|
||||
}
|
||||
|
||||
string query = "";
|
||||
if (TShock.Config.StorageType.ToLower() == "sqlite")
|
||||
query = "INSERT OR IGNORE INTO GroupList (GroupName, Commands) VALUES (@0, @1);";
|
||||
else if (TShock.Config.StorageType.ToLower() == "mysql")
|
||||
query = "INSERT IGNORE INTO GroupList SET GroupName=@0, Commands=@1;";
|
||||
string query = "";
|
||||
if (TShock.Config.StorageType.ToLower() == "sqlite")
|
||||
query = "INSERT OR IGNORE INTO GroupList (GroupName, Commands) VALUES (@0, @1);";
|
||||
else if (TShock.Config.StorageType.ToLower() == "mysql")
|
||||
query = "INSERT IGNORE INTO GroupList SET GroupName=@0, Commands=@1;";
|
||||
|
||||
db.Query(query, info[0].Trim(), comms);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
String path = Path.Combine(TShock.SavePath, "old_configs");
|
||||
String file2 = Path.Combine(path, "groups.txt");
|
||||
if (!Directory.Exists(path))
|
||||
Directory.CreateDirectory(path);
|
||||
if (File.Exists(file2))
|
||||
File.Delete(file2);
|
||||
File.Move(file, file2);
|
||||
}
|
||||
|
||||
}
|
||||
db.Query(query, info[0].Trim(), comms);
|
||||
}
|
||||
}
|
||||
}
|
||||
String path = Path.Combine(TShock.SavePath, "old_configs");
|
||||
String file2 = Path.Combine(path, "groups.txt");
|
||||
if (!Directory.Exists(path))
|
||||
Directory.CreateDirectory(path);
|
||||
if (File.Exists(file2))
|
||||
File.Delete(file2);
|
||||
File.Move(file, file2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public bool GroupExists(string group)
|
||||
{
|
||||
if (group == "superadmin")
|
||||
return true;
|
||||
public bool GroupExists(string group)
|
||||
{
|
||||
if (group == "superadmin")
|
||||
return true;
|
||||
|
||||
|
||||
return groups.Any(g => g.Name.Equals(group));
|
||||
}
|
||||
return groups.Any(g => g.Name.Equals(group));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds group with name and permissions if it does not exist.
|
||||
/// </summary>
|
||||
/// <param name="name">name of group</param>
|
||||
/// <param name="parentname">parent of group</param>
|
||||
/// <param name="permissions">permissions</param>
|
||||
public String AddGroup(String name, string parentname, String permissions, String chatcolor)
|
||||
{
|
||||
String message = "";
|
||||
if (GroupExists(name))
|
||||
return "Error: Group already exists. Use /modGroup to change permissions.";
|
||||
/// <summary>
|
||||
/// Adds group with name and permissions if it does not exist.
|
||||
/// </summary>
|
||||
/// <param name="name">name of group</param>
|
||||
/// <param name="parentname">parent of group</param>
|
||||
/// <param name="permissions">permissions</param>
|
||||
public String AddGroup(String name, string parentname, String permissions, String chatcolor)
|
||||
{
|
||||
String message = "";
|
||||
if (GroupExists(name))
|
||||
return "Error: Group already exists. Use /modGroup to change permissions.";
|
||||
|
||||
var group = new Group(name, null, chatcolor);
|
||||
group.permissions.Add(permissions);
|
||||
if (!string.IsNullOrWhiteSpace(parentname))
|
||||
{
|
||||
var parent = groups.FirstOrDefault(gp => gp.Name == parentname);
|
||||
if (parent == null)
|
||||
{
|
||||
var error = "Invalid parent {0} for group {1}".SFormat(group.Name, parentname);
|
||||
Log.ConsoleError(error);
|
||||
return error;
|
||||
}
|
||||
group.Parent = parent;
|
||||
}
|
||||
var group = new Group(name, null, chatcolor);
|
||||
group.permissions.Add(permissions);
|
||||
if (!string.IsNullOrWhiteSpace(parentname))
|
||||
{
|
||||
var parent = groups.FirstOrDefault(gp => gp.Name == parentname);
|
||||
if (parent == null)
|
||||
{
|
||||
var error = "Invalid parent {0} for group {1}".SFormat(group.Name, parentname);
|
||||
Log.ConsoleError(error);
|
||||
return error;
|
||||
}
|
||||
group.Parent = parent;
|
||||
}
|
||||
|
||||
string query = (TShock.Config.StorageType.ToLower() == "sqlite") ?
|
||||
"INSERT OR IGNORE INTO GroupList (GroupName, Parent, Commands, ChatColor) VALUES (@0, @1, @2, @3);" :
|
||||
"INSERT IGNORE INTO GroupList SET GroupName=@0, Parent=@1, Commands=@2, ChatColor=@3";
|
||||
if (database.Query(query, name, parentname, permissions, chatcolor) == 1)
|
||||
message = "Group " + name + " has been created successfully.";
|
||||
string query = (TShock.Config.StorageType.ToLower() == "sqlite")
|
||||
? "INSERT OR IGNORE INTO GroupList (GroupName, Parent, Commands, ChatColor) VALUES (@0, @1, @2, @3);"
|
||||
: "INSERT IGNORE INTO GroupList SET GroupName=@0, Parent=@1, Commands=@2, ChatColor=@3";
|
||||
if (database.Query(query, name, parentname, permissions, chatcolor) == 1)
|
||||
message = "Group " + name + " has been created successfully.";
|
||||
|
||||
groups.Add(group);
|
||||
groups.Add(group);
|
||||
|
||||
return message;
|
||||
}
|
||||
public String AddGroup(String name, String permissions)
|
||||
{
|
||||
return AddGroup(name, "", permissions, "255,255,255");
|
||||
}
|
||||
public String AddGroup(String name, string parent, String permissions)
|
||||
{
|
||||
return AddGroup(name, parent, permissions, "255,255,255");
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
public String DeleteGroup(String name)
|
||||
{
|
||||
String message = "";
|
||||
if (!GroupExists(name))
|
||||
return "Error: Group doesn't exists.";
|
||||
public String AddGroup(String name, String permissions)
|
||||
{
|
||||
return AddGroup(name, "", permissions, "255,255,255");
|
||||
}
|
||||
|
||||
if (database.Query("DELETE FROM GroupList WHERE GroupName=@0", name) == 1)
|
||||
message = "Group " + name + " has been deleted successfully.";
|
||||
groups.Remove(TShock.Utils.GetGroup(name));
|
||||
public String AddGroup(String name, string parent, String permissions)
|
||||
{
|
||||
return AddGroup(name, parent, permissions, "255,255,255");
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
public String DeleteGroup(String name)
|
||||
{
|
||||
String message = "";
|
||||
if (!GroupExists(name))
|
||||
return "Error: Group doesn't exists.";
|
||||
|
||||
public String AddPermissions(String name, List<String> permissions)
|
||||
{
|
||||
String message = "";
|
||||
if (!GroupExists(name))
|
||||
return "Error: Group doesn't exists.";
|
||||
if (database.Query("DELETE FROM GroupList WHERE GroupName=@0", name) == 1)
|
||||
message = "Group " + name + " has been deleted successfully.";
|
||||
groups.Remove(TShock.Utils.GetGroup(name));
|
||||
|
||||
var group = TShock.Utils.GetGroup(name);
|
||||
//Add existing permissions (without duplicating)
|
||||
permissions.AddRange(group.permissions.Where(s => !permissions.Contains(s)));
|
||||
return message;
|
||||
}
|
||||
|
||||
if (database.Query("UPDATE GroupList SET Commands=@0 WHERE GroupName=@1", String.Join(",", permissions), name) != 0)
|
||||
{
|
||||
message = "Group " + name + " has been modified successfully.";
|
||||
group.SetPermission( permissions );
|
||||
}
|
||||
return message;
|
||||
}
|
||||
public String AddPermissions(String name, List<String> permissions)
|
||||
{
|
||||
String message = "";
|
||||
if (!GroupExists(name))
|
||||
return "Error: Group doesn't exists.";
|
||||
|
||||
public String DeletePermissions(String name, List<String> permissions)
|
||||
{
|
||||
String message = "";
|
||||
if (!GroupExists(name))
|
||||
return "Error: Group doesn't exists.";
|
||||
var group = TShock.Utils.GetGroup(name);
|
||||
//Add existing permissions (without duplicating)
|
||||
permissions.AddRange(group.permissions.Where(s => !permissions.Contains(s)));
|
||||
|
||||
var group = TShock.Utils.GetGroup(name);
|
||||
if (database.Query("UPDATE GroupList SET Commands=@0 WHERE GroupName=@1", String.Join(",", permissions), name) != 0)
|
||||
{
|
||||
message = "Group " + name + " has been modified successfully.";
|
||||
group.SetPermission(permissions);
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
//Only get permissions that exist in the group.
|
||||
var newperms = group.permissions.Except( permissions );
|
||||
public String DeletePermissions(String name, List<String> permissions)
|
||||
{
|
||||
String message = "";
|
||||
if (!GroupExists(name))
|
||||
return "Error: Group doesn't exists.";
|
||||
|
||||
if (database.Query("UPDATE GroupList SET Commands=@0 WHERE GroupName=@1", String.Join(",", newperms), name) != 0)
|
||||
{
|
||||
message = "Group " + name + " has been modified successfully.";
|
||||
group.SetPermission( newperms.ToList() );
|
||||
}
|
||||
return message;
|
||||
}
|
||||
var group = TShock.Utils.GetGroup(name);
|
||||
|
||||
public void LoadPermisions()
|
||||
{
|
||||
//Create a temporary list so if there is an error it doesn't override the currently loaded groups with broken groups.
|
||||
var tempgroups = new List<Group>();
|
||||
tempgroups.Add(new SuperAdminGroup());
|
||||
//Only get permissions that exist in the group.
|
||||
var newperms = group.permissions.Except(permissions);
|
||||
|
||||
if (groups == null || groups.Count < 2)
|
||||
groups = tempgroups;
|
||||
if (database.Query("UPDATE GroupList SET Commands=@0 WHERE GroupName=@1", String.Join(",", newperms), name) != 0)
|
||||
{
|
||||
message = "Group " + name + " has been modified successfully.";
|
||||
group.SetPermission(newperms.ToList());
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var groupsparents = new List<Tuple<Group, string>>();
|
||||
using (var reader = database.QueryReader("SELECT * FROM GroupList"))
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
string groupname = reader.Get<String>("GroupName");
|
||||
var group = new Group(groupname);
|
||||
public void LoadPermisions()
|
||||
{
|
||||
//Create a temporary list so if there is an error it doesn't override the currently loaded groups with broken groups.
|
||||
var tempgroups = new List<Group>();
|
||||
tempgroups.Add(new SuperAdminGroup());
|
||||
|
||||
group.Prefix = reader.Get<String>("Prefix");
|
||||
group.Suffix= reader.Get<String>("Suffix");
|
||||
if (groups == null || groups.Count < 2)
|
||||
groups = tempgroups;
|
||||
|
||||
//Inherit Given commands
|
||||
String[] commands = reader.Get<String>("Commands").Split(',');
|
||||
foreach (var t in commands)
|
||||
{
|
||||
var str = t.Trim();
|
||||
if (str.StartsWith("!"))
|
||||
{
|
||||
group.NegatePermission(str.Substring(1));
|
||||
}
|
||||
else
|
||||
{
|
||||
group.AddPermission(str);
|
||||
}
|
||||
}
|
||||
String[] chatcolour = (reader.Get<String>("ChatColor") ?? "").Split(',');
|
||||
if (chatcolour.Length == 3)
|
||||
{
|
||||
byte.TryParse(chatcolour[0], out group.R);
|
||||
byte.TryParse(chatcolour[1], out group.G);
|
||||
byte.TryParse(chatcolour[2], out group.B);
|
||||
}
|
||||
try
|
||||
{
|
||||
var groupsparents = new List<Tuple<Group, string>>();
|
||||
using (var reader = database.QueryReader("SELECT * FROM GroupList"))
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
string groupname = reader.Get<String>("GroupName");
|
||||
var group = new Group(groupname);
|
||||
|
||||
groupsparents.Add(Tuple.Create(group, reader.Get<string>("Parent")));
|
||||
}
|
||||
}
|
||||
group.Prefix = reader.Get<String>("Prefix");
|
||||
group.Suffix = reader.Get<String>("Suffix");
|
||||
|
||||
foreach (var t in groupsparents)
|
||||
{
|
||||
var group = t.Item1;
|
||||
var parentname = t.Item2;
|
||||
if (!string.IsNullOrWhiteSpace(parentname))
|
||||
{
|
||||
var parent = groupsparents.FirstOrDefault(gp => gp.Item1.Name == parentname);
|
||||
if (parent == null)
|
||||
{
|
||||
Log.ConsoleError("Invalid parent {0} for group {1}".SFormat(group.Name, parentname));
|
||||
return;
|
||||
}
|
||||
group.Parent = parent.Item1;
|
||||
}
|
||||
tempgroups.Add(group);
|
||||
}
|
||||
//Inherit Given commands
|
||||
String[] commands = reader.Get<String>("Commands").Split(',');
|
||||
foreach (var t in commands)
|
||||
{
|
||||
var str = t.Trim();
|
||||
if (str.StartsWith("!"))
|
||||
{
|
||||
group.NegatePermission(str.Substring(1));
|
||||
}
|
||||
else
|
||||
{
|
||||
group.AddPermission(str);
|
||||
}
|
||||
}
|
||||
String[] chatcolour = (reader.Get<String>("ChatColor") ?? "").Split(',');
|
||||
if (chatcolour.Length == 3)
|
||||
{
|
||||
byte.TryParse(chatcolour[0], out group.R);
|
||||
byte.TryParse(chatcolour[1], out group.G);
|
||||
byte.TryParse(chatcolour[2], out group.B);
|
||||
}
|
||||
|
||||
groupsparents.Add(Tuple.Create(group, reader.Get<string>("Parent")));
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var t in groupsparents)
|
||||
{
|
||||
var group = t.Item1;
|
||||
var parentname = t.Item2;
|
||||
if (!string.IsNullOrWhiteSpace(parentname))
|
||||
{
|
||||
var parent = groupsparents.FirstOrDefault(gp => gp.Item1.Name == parentname);
|
||||
if (parent == null)
|
||||
{
|
||||
Log.ConsoleError("Invalid parent {0} for group {1}".SFormat(group.Name, parentname));
|
||||
return;
|
||||
}
|
||||
group.Parent = parent.Item1;
|
||||
}
|
||||
tempgroups.Add(group);
|
||||
}
|
||||
|
||||
|
||||
groups = tempgroups;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
groups = tempgroups;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,42 +7,55 @@ using TShockAPI.Extensions;
|
|||
|
||||
namespace TShockAPI.DB
|
||||
{
|
||||
public interface IQueryBuilder
|
||||
{
|
||||
string CreateTable(SqlTable table);
|
||||
string AlterTable(SqlTable from, SqlTable to);
|
||||
string DbTypeToString(MySqlDbType type, int? length);
|
||||
string UpdateValue(string table, List<SqlValue> values, List<SqlValue> wheres);
|
||||
string InsertValues(string table, List<SqlValue> values);
|
||||
string ReadColumn(string table, List<SqlValue> wheres);
|
||||
string DeleteRow(string table, List<SqlValue> wheres);
|
||||
}
|
||||
public interface IQueryBuilder
|
||||
{
|
||||
string CreateTable(SqlTable table);
|
||||
string AlterTable(SqlTable from, SqlTable to);
|
||||
string DbTypeToString(MySqlDbType type, int? length);
|
||||
string UpdateValue(string table, List<SqlValue> values, List<SqlValue> wheres);
|
||||
string InsertValues(string table, List<SqlValue> values);
|
||||
string ReadColumn(string table, List<SqlValue> wheres);
|
||||
string DeleteRow(string table, List<SqlValue> wheres);
|
||||
}
|
||||
|
||||
public class SqliteQueryCreator : IQueryBuilder
|
||||
{
|
||||
public string CreateTable(SqlTable table)
|
||||
{
|
||||
var columns = table.Columns.Select(c => "'{0}' {1} {2} {3} {4}".SFormat(c.Name, DbTypeToString(c.Type, c.Length), c.Primary ? "PRIMARY KEY" : "", c.AutoIncrement ? "AUTOINCREMENT" : "", c.NotNull ? "NOT NULL" : "", c.Unique ? "UNIQUE" : ""));
|
||||
return "CREATE TABLE '{0}' ({1})".SFormat(table.Name, string.Join(", ", columns));
|
||||
}
|
||||
static Random rand = new Random();
|
||||
/// <summary>
|
||||
/// Alter a table from source to destination
|
||||
/// </summary>
|
||||
/// <param name="from">Must have name and column names. Column types are not required</param>
|
||||
/// <param name="to">Must have column names and column types.</param>
|
||||
/// <returns></returns>
|
||||
public string AlterTable(SqlTable from, SqlTable to)
|
||||
{
|
||||
var rstr = rand.NextString(20);
|
||||
var alter = "ALTER TABLE '{0}' RENAME TO '{1}_{0}'".SFormat(from.Name, rstr);
|
||||
var create = CreateTable(to);
|
||||
//combine all columns in the 'from' variable excluding ones that aren't in the 'to' variable.
|
||||
//exclude the ones that aren't in 'to' variable because if the column is deleted, why try to import the data?
|
||||
var insert = "INSERT INTO '{0}' ({1}) SELECT {1} FROM {2}_{0}".SFormat(from.Name, string.Join(", ", from.Columns.Where(c => to.Columns.Any(c2 => c2.Name == c.Name)).Select(c => c.Name)), rstr);
|
||||
var drop = "DROP TABLE '{0}_{1}'".SFormat(rstr, from.Name);
|
||||
return "{0}; {1}; {2}; {3};".SFormat(alter, create, insert, drop);
|
||||
/*
|
||||
public class SqliteQueryCreator : IQueryBuilder
|
||||
{
|
||||
public string CreateTable(SqlTable table)
|
||||
{
|
||||
var columns =
|
||||
table.Columns.Select(
|
||||
c =>
|
||||
"'{0}' {1} {2} {3} {4}".SFormat(c.Name, DbTypeToString(c.Type, c.Length), c.Primary ? "PRIMARY KEY" : "",
|
||||
c.AutoIncrement ? "AUTOINCREMENT" : "", c.NotNull ? "NOT NULL" : "",
|
||||
c.Unique ? "UNIQUE" : ""));
|
||||
return "CREATE TABLE '{0}' ({1})".SFormat(table.Name, string.Join(", ", columns));
|
||||
}
|
||||
|
||||
private static Random rand = new Random();
|
||||
|
||||
/// <summary>
|
||||
/// Alter a table from source to destination
|
||||
/// </summary>
|
||||
/// <param name="from">Must have name and column names. Column types are not required</param>
|
||||
/// <param name="to">Must have column names and column types.</param>
|
||||
/// <returns></returns>
|
||||
public string AlterTable(SqlTable from, SqlTable to)
|
||||
{
|
||||
var rstr = rand.NextString(20);
|
||||
var alter = "ALTER TABLE '{0}' RENAME TO '{1}_{0}'".SFormat(from.Name, rstr);
|
||||
var create = CreateTable(to);
|
||||
//combine all columns in the 'from' variable excluding ones that aren't in the 'to' variable.
|
||||
//exclude the ones that aren't in 'to' variable because if the column is deleted, why try to import the data?
|
||||
var insert = "INSERT INTO '{0}' ({1}) SELECT {1} FROM {2}_{0}".SFormat(from.Name,
|
||||
string.Join(", ",
|
||||
from.Columns.Where(
|
||||
c =>
|
||||
to.Columns.Any(
|
||||
c2 => c2.Name == c.Name)).Select
|
||||
(c => c.Name)), rstr);
|
||||
var drop = "DROP TABLE '{0}_{1}'".SFormat(rstr, from.Name);
|
||||
return "{0}; {1}; {2}; {3};".SFormat(alter, create, insert, drop);
|
||||
/*
|
||||
ALTER TABLE "main"."Bans" RENAME TO "oXHFcGcd04oXHFcGcd04_Bans"
|
||||
CREATE TABLE "main"."Bans" ("IP" TEXT PRIMARY KEY ,"Name" TEXT)
|
||||
INSERT INTO "main"."Bans" SELECT "IP","Name" FROM "main"."oXHFcGcd04oXHFcGcd04_Bans"
|
||||
|
|
@ -50,238 +63,264 @@ namespace TShockAPI.DB
|
|||
*
|
||||
* Twitchy - Oh. I get it!
|
||||
*/
|
||||
}
|
||||
public string DeleteRow(string table, List<SqlValue> wheres)
|
||||
{
|
||||
var sbwheres = new StringBuilder();
|
||||
int count = 0;
|
||||
foreach (SqlValue where in wheres)
|
||||
{
|
||||
sbwheres.Append(where.Name + "=" + where.Value);
|
||||
if (count != wheres.Count - 1)
|
||||
sbwheres.Append(" AND ");
|
||||
count++;
|
||||
}
|
||||
if (wheres.Count > 0)
|
||||
return "DELETE FROM '{0}' WHERE {1} ".SFormat(table, sbwheres.ToString());
|
||||
else
|
||||
return "DELETE FROM '{0}'".SFormat(table, sbwheres.ToString());
|
||||
}
|
||||
public string UpdateValue(string table, List<SqlValue> values, List<SqlValue> wheres)
|
||||
{
|
||||
var sbvalues = new StringBuilder();
|
||||
var sbwheres = new StringBuilder();
|
||||
int count = 0;
|
||||
foreach (SqlValue value in values)
|
||||
{
|
||||
sbvalues.Append(value.Name + "=" + value.Value);
|
||||
if (count != values.Count - 1)
|
||||
sbvalues.Append(",");
|
||||
count++;
|
||||
}
|
||||
count = 0;
|
||||
foreach (SqlValue where in wheres)
|
||||
{
|
||||
sbwheres.Append(where.Name + "=" + where.Value);
|
||||
if (count != wheres.Count - 1)
|
||||
sbwheres.Append(" AND ");
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
if (wheres.Count > 0)
|
||||
return "UPDATE '{0}' SET {1} WHERE {2}".SFormat(table, sbvalues.ToString(), sbwheres.ToString());
|
||||
else
|
||||
return "UPDATE '{0}' SET {1}".SFormat(table, sbvalues.ToString());
|
||||
}
|
||||
public string InsertValues(string table, List<SqlValue> values)
|
||||
{
|
||||
var sbnames = new StringBuilder();
|
||||
var sbvalues = new StringBuilder();
|
||||
int count = 0;
|
||||
public string DeleteRow(string table, List<SqlValue> wheres)
|
||||
{
|
||||
var sbwheres = new StringBuilder();
|
||||
int count = 0;
|
||||
foreach (SqlValue where in wheres)
|
||||
{
|
||||
sbwheres.Append(where.Name + "=" + where.Value);
|
||||
if (count != wheres.Count - 1)
|
||||
sbwheres.Append(" AND ");
|
||||
count++;
|
||||
}
|
||||
if (wheres.Count > 0)
|
||||
return "DELETE FROM '{0}' WHERE {1} ".SFormat(table, sbwheres.ToString());
|
||||
else
|
||||
return "DELETE FROM '{0}'".SFormat(table, sbwheres.ToString());
|
||||
}
|
||||
|
||||
foreach (SqlValue name in values)
|
||||
{
|
||||
sbnames.Append(name.Name);
|
||||
public string UpdateValue(string table, List<SqlValue> values, List<SqlValue> wheres)
|
||||
{
|
||||
var sbvalues = new StringBuilder();
|
||||
var sbwheres = new StringBuilder();
|
||||
int count = 0;
|
||||
foreach (SqlValue value in values)
|
||||
{
|
||||
sbvalues.Append(value.Name + "=" + value.Value);
|
||||
if (count != values.Count - 1)
|
||||
sbvalues.Append(",");
|
||||
count++;
|
||||
}
|
||||
count = 0;
|
||||
foreach (SqlValue where in wheres)
|
||||
{
|
||||
sbwheres.Append(where.Name + "=" + where.Value);
|
||||
if (count != wheres.Count - 1)
|
||||
sbwheres.Append(" AND ");
|
||||
count++;
|
||||
}
|
||||
|
||||
if (count != values.Count - 1)
|
||||
sbnames.Append(", ");
|
||||
count++;
|
||||
}
|
||||
count = 0;
|
||||
foreach (SqlValue value in values)
|
||||
{
|
||||
sbvalues.Append(value.Value.ToString());
|
||||
if (count != values.Count - 1)
|
||||
sbvalues.Append(", ");
|
||||
count++;
|
||||
}
|
||||
return "INSERT INTO '{0}' ({1}) VALUES ({2})".SFormat(table, sbnames.ToString(), sbvalues.ToString());
|
||||
}
|
||||
public string ReadColumn(string table, List<SqlValue> wheres)
|
||||
{
|
||||
var sbwheres = new StringBuilder();
|
||||
int count = 0;
|
||||
if (wheres.Count > 0)
|
||||
return "UPDATE '{0}' SET {1} WHERE {2}".SFormat(table, sbvalues.ToString(), sbwheres.ToString());
|
||||
else
|
||||
return "UPDATE '{0}' SET {1}".SFormat(table, sbvalues.ToString());
|
||||
}
|
||||
|
||||
foreach (SqlValue where in wheres)
|
||||
{
|
||||
sbwheres.Append(where.Name + "=" + where.Value);
|
||||
if (count != wheres.Count - 1)
|
||||
sbwheres.Append(" AND ");
|
||||
count++;
|
||||
}
|
||||
public string InsertValues(string table, List<SqlValue> values)
|
||||
{
|
||||
var sbnames = new StringBuilder();
|
||||
var sbvalues = new StringBuilder();
|
||||
int count = 0;
|
||||
|
||||
if(wheres.Count > 0)
|
||||
return "SELECT * FROM {0} WHERE {1}".SFormat(table, sbwheres.ToString());
|
||||
else
|
||||
return "SELECT * FROM {0}".SFormat(table);
|
||||
}
|
||||
foreach (SqlValue name in values)
|
||||
{
|
||||
sbnames.Append(name.Name);
|
||||
|
||||
static readonly Dictionary<MySqlDbType, string> TypesAsStrings = new Dictionary<MySqlDbType, string>
|
||||
{
|
||||
{MySqlDbType.VarChar, "TEXT"},
|
||||
{MySqlDbType.String, "TEXT"},
|
||||
{MySqlDbType.Text, "TEXT"},
|
||||
{MySqlDbType.TinyText, "TEXT"},
|
||||
{MySqlDbType.MediumText, "TEXT"},
|
||||
{MySqlDbType.LongText, "TEXT"},
|
||||
{MySqlDbType.Int32, "INTEGER"},
|
||||
};
|
||||
public string DbTypeToString(MySqlDbType type, int? length)
|
||||
{
|
||||
string ret;
|
||||
if (TypesAsStrings.TryGetValue(type, out ret))
|
||||
return ret;
|
||||
throw new NotImplementedException(Enum.GetName(typeof(MySqlDbType), type));
|
||||
}
|
||||
}
|
||||
public class MysqlQueryCreator : IQueryBuilder
|
||||
{
|
||||
public string CreateTable(SqlTable table)
|
||||
{
|
||||
var columns = table.Columns.Select(c => "{0} {1} {2} {3}".SFormat(c.Name, DbTypeToString(c.Type, c.Length), c.Primary ? "PRIMARY KEY" : "", c.AutoIncrement ? "AUTO_INCREMENT" : "", c.NotNull ? "NOT NULL" : ""));
|
||||
var uniques = table.Columns.Where(c => c.Unique).Select(c => c.Name);
|
||||
return "CREATE TABLE {0} ({1} {2})".SFormat(table.Name, string.Join(", ", columns), uniques.Count() > 0 ? ", UNIQUE({0})".SFormat(string.Join(", ", uniques)) : "");
|
||||
}
|
||||
static Random rand = new Random();
|
||||
/// <summary>
|
||||
/// Alter a table from source to destination
|
||||
/// </summary>
|
||||
/// <param name="from">Must have name and column names. Column types are not required</param>
|
||||
/// <param name="to">Must have column names and column types.</param>
|
||||
/// <returns></returns>
|
||||
public string AlterTable(SqlTable from, SqlTable to)
|
||||
{
|
||||
var rstr = rand.NextString(20);
|
||||
var alter = "RENAME TABLE {0} TO {1}_{0}".SFormat(from.Name, rstr);
|
||||
var create = CreateTable(to);
|
||||
//combine all columns in the 'from' variable excluding ones that aren't in the 'to' variable.
|
||||
//exclude the ones that aren't in 'to' variable because if the column is deleted, why try to import the data?
|
||||
var insert = "INSERT INTO {0} ({1}) SELECT {1} FROM {2}_{0}".SFormat(from.Name, string.Join(", ", from.Columns.Where(c => to.Columns.Any(c2 => c2.Name == c.Name)).Select(c => c.Name)), rstr);
|
||||
var drop = "DROP TABLE {0}_{1}".SFormat(rstr, from.Name);
|
||||
return "{0}; {1}; {2}; {3};".SFormat(alter, create, insert, drop);
|
||||
}
|
||||
public string DeleteRow(string table, List<SqlValue> wheres)
|
||||
{
|
||||
var sbwheres = new StringBuilder();
|
||||
int count = 0;
|
||||
foreach (SqlValue where in wheres)
|
||||
{
|
||||
sbwheres.Append(where.Name + "=" + where.Value);
|
||||
if (count != wheres.Count - 1)
|
||||
sbwheres.Append(" AND ");
|
||||
count++;
|
||||
}
|
||||
if (wheres.Count > 0)
|
||||
return "DELETE FROM {0} WHERE {1} ".SFormat(table, sbwheres.ToString());
|
||||
else
|
||||
return "DELETE FROM {0}".SFormat(table, sbwheres.ToString());
|
||||
}
|
||||
public string UpdateValue(string table, List<SqlValue> values, List<SqlValue> wheres)
|
||||
{
|
||||
var sbvalues = new StringBuilder();
|
||||
var sbwheres = new StringBuilder();
|
||||
int count = 0;
|
||||
foreach (SqlValue value in values)
|
||||
{
|
||||
sbvalues.Append(value.Name + "=" + value.Value);
|
||||
if (count != values.Count - 1)
|
||||
sbvalues.Append("AND");
|
||||
count++;
|
||||
}
|
||||
count = 0;
|
||||
foreach (SqlValue where in wheres)
|
||||
{
|
||||
sbwheres.Append(where.Name + "=" + where.Value);
|
||||
if (count != wheres.Count - 1)
|
||||
sbwheres.Append(" AND ");
|
||||
count++;
|
||||
}
|
||||
if (count != values.Count - 1)
|
||||
sbnames.Append(", ");
|
||||
count++;
|
||||
}
|
||||
count = 0;
|
||||
foreach (SqlValue value in values)
|
||||
{
|
||||
sbvalues.Append(value.Value.ToString());
|
||||
if (count != values.Count - 1)
|
||||
sbvalues.Append(", ");
|
||||
count++;
|
||||
}
|
||||
return "INSERT INTO '{0}' ({1}) VALUES ({2})".SFormat(table, sbnames.ToString(), sbvalues.ToString());
|
||||
}
|
||||
|
||||
if (wheres.Count > 0)
|
||||
return "UPDATE {0} SET {1} WHERE {2}".SFormat(table, sbvalues.ToString(), sbwheres.ToString());
|
||||
else
|
||||
return "UPDATE {0} SET {1}".SFormat(table, sbvalues.ToString());
|
||||
}
|
||||
public string InsertValues(string table, List<SqlValue> values)
|
||||
{
|
||||
var sbnames = new StringBuilder();
|
||||
var sbvalues = new StringBuilder();
|
||||
int count = 0;
|
||||
public string ReadColumn(string table, List<SqlValue> wheres)
|
||||
{
|
||||
var sbwheres = new StringBuilder();
|
||||
int count = 0;
|
||||
|
||||
foreach (SqlValue name in values)
|
||||
{
|
||||
sbnames.Append(name.Name);
|
||||
foreach (SqlValue where in wheres)
|
||||
{
|
||||
sbwheres.Append(where.Name + "=" + where.Value);
|
||||
if (count != wheres.Count - 1)
|
||||
sbwheres.Append(" AND ");
|
||||
count++;
|
||||
}
|
||||
|
||||
if (count != values.Count - 1)
|
||||
sbnames.Append(", ");
|
||||
count++;
|
||||
}
|
||||
count = 0;
|
||||
foreach (SqlValue value in values)
|
||||
{
|
||||
sbvalues.Append(value.Value.ToString());
|
||||
if (count != values.Count - 1)
|
||||
sbvalues.Append(", ");
|
||||
count++;
|
||||
}
|
||||
if (wheres.Count > 0)
|
||||
return "SELECT * FROM {0} WHERE {1}".SFormat(table, sbwheres.ToString());
|
||||
else
|
||||
return "SELECT * FROM {0}".SFormat(table);
|
||||
}
|
||||
|
||||
return "INSERT INTO {0} ({1}) VALUES ({2})".SFormat(table, sbnames.ToString(), sbvalues.ToString());
|
||||
}
|
||||
public string ReadColumn(string table, List<SqlValue> wheres)
|
||||
{
|
||||
var sbwheres = new StringBuilder();
|
||||
int count = 0;
|
||||
private static readonly Dictionary<MySqlDbType, string> TypesAsStrings = new Dictionary<MySqlDbType, string>
|
||||
{
|
||||
{MySqlDbType.VarChar, "TEXT"},
|
||||
{MySqlDbType.String, "TEXT"},
|
||||
{MySqlDbType.Text, "TEXT"},
|
||||
{MySqlDbType.TinyText, "TEXT"},
|
||||
{MySqlDbType.MediumText, "TEXT"},
|
||||
{MySqlDbType.LongText, "TEXT"},
|
||||
{MySqlDbType.Int32, "INTEGER"},
|
||||
};
|
||||
|
||||
foreach (SqlValue where in wheres)
|
||||
{
|
||||
sbwheres.Append(where.Name + "=" + where.Value);
|
||||
if (count != wheres.Count - 1)
|
||||
sbwheres.Append(" AND ");
|
||||
count++;
|
||||
}
|
||||
public string DbTypeToString(MySqlDbType type, int? length)
|
||||
{
|
||||
string ret;
|
||||
if (TypesAsStrings.TryGetValue(type, out ret))
|
||||
return ret;
|
||||
throw new NotImplementedException(Enum.GetName(typeof (MySqlDbType), type));
|
||||
}
|
||||
}
|
||||
|
||||
if (wheres.Count > 0)
|
||||
return "SELECT * FROM {0} WHERE {1}".SFormat(table, sbwheres.ToString());
|
||||
else
|
||||
return "SELECT * FROM {0}".SFormat(table);
|
||||
}
|
||||
public class MysqlQueryCreator : IQueryBuilder
|
||||
{
|
||||
public string CreateTable(SqlTable table)
|
||||
{
|
||||
var columns =
|
||||
table.Columns.Select(
|
||||
c =>
|
||||
"{0} {1} {2} {3}".SFormat(c.Name, DbTypeToString(c.Type, c.Length), c.Primary ? "PRIMARY KEY" : "",
|
||||
c.AutoIncrement ? "AUTO_INCREMENT" : "", c.NotNull ? "NOT NULL" : ""));
|
||||
var uniques = table.Columns.Where(c => c.Unique).Select(c => c.Name);
|
||||
return "CREATE TABLE {0} ({1} {2})".SFormat(table.Name, string.Join(", ", columns),
|
||||
uniques.Count() > 0
|
||||
? ", UNIQUE({0})".SFormat(string.Join(", ", uniques))
|
||||
: "");
|
||||
}
|
||||
|
||||
static readonly Dictionary<MySqlDbType, string> TypesAsStrings = new Dictionary<MySqlDbType, string>
|
||||
{
|
||||
{MySqlDbType.VarChar, "VARCHAR"},
|
||||
{MySqlDbType.String, "CHAR"},
|
||||
{MySqlDbType.Text, "TEXT"},
|
||||
{MySqlDbType.TinyText, "TINYTEXT"},
|
||||
{MySqlDbType.MediumText, "MEDIUMTEXT"},
|
||||
{MySqlDbType.LongText, "LONGTEXT"},
|
||||
{MySqlDbType.Int32, "INT"},
|
||||
};
|
||||
public string DbTypeToString(MySqlDbType type, int? length)
|
||||
{
|
||||
string ret;
|
||||
if (TypesAsStrings.TryGetValue(type, out ret))
|
||||
return ret + (length != null ? "({0})".SFormat((int)length) : "");
|
||||
throw new NotImplementedException(Enum.GetName(typeof(MySqlDbType), type));
|
||||
}
|
||||
}
|
||||
}
|
||||
private static Random rand = new Random();
|
||||
|
||||
/// <summary>
|
||||
/// Alter a table from source to destination
|
||||
/// </summary>
|
||||
/// <param name="from">Must have name and column names. Column types are not required</param>
|
||||
/// <param name="to">Must have column names and column types.</param>
|
||||
/// <returns></returns>
|
||||
public string AlterTable(SqlTable from, SqlTable to)
|
||||
{
|
||||
var rstr = rand.NextString(20);
|
||||
var alter = "RENAME TABLE {0} TO {1}_{0}".SFormat(from.Name, rstr);
|
||||
var create = CreateTable(to);
|
||||
//combine all columns in the 'from' variable excluding ones that aren't in the 'to' variable.
|
||||
//exclude the ones that aren't in 'to' variable because if the column is deleted, why try to import the data?
|
||||
var insert = "INSERT INTO {0} ({1}) SELECT {1} FROM {2}_{0}".SFormat(from.Name,
|
||||
string.Join(", ",
|
||||
from.Columns.Where(
|
||||
c =>
|
||||
to.Columns.Any(
|
||||
c2 => c2.Name == c.Name)).Select(
|
||||
c => c.Name)), rstr);
|
||||
var drop = "DROP TABLE {0}_{1}".SFormat(rstr, from.Name);
|
||||
return "{0}; {1}; {2}; {3};".SFormat(alter, create, insert, drop);
|
||||
}
|
||||
|
||||
public string DeleteRow(string table, List<SqlValue> wheres)
|
||||
{
|
||||
var sbwheres = new StringBuilder();
|
||||
int count = 0;
|
||||
foreach (SqlValue where in wheres)
|
||||
{
|
||||
sbwheres.Append(where.Name + "=" + where.Value);
|
||||
if (count != wheres.Count - 1)
|
||||
sbwheres.Append(" AND ");
|
||||
count++;
|
||||
}
|
||||
if (wheres.Count > 0)
|
||||
return "DELETE FROM {0} WHERE {1} ".SFormat(table, sbwheres.ToString());
|
||||
else
|
||||
return "DELETE FROM {0}".SFormat(table, sbwheres.ToString());
|
||||
}
|
||||
|
||||
public string UpdateValue(string table, List<SqlValue> values, List<SqlValue> wheres)
|
||||
{
|
||||
var sbvalues = new StringBuilder();
|
||||
var sbwheres = new StringBuilder();
|
||||
int count = 0;
|
||||
foreach (SqlValue value in values)
|
||||
{
|
||||
sbvalues.Append(value.Name + "=" + value.Value);
|
||||
if (count != values.Count - 1)
|
||||
sbvalues.Append("AND");
|
||||
count++;
|
||||
}
|
||||
count = 0;
|
||||
foreach (SqlValue where in wheres)
|
||||
{
|
||||
sbwheres.Append(where.Name + "=" + where.Value);
|
||||
if (count != wheres.Count - 1)
|
||||
sbwheres.Append(" AND ");
|
||||
count++;
|
||||
}
|
||||
|
||||
if (wheres.Count > 0)
|
||||
return "UPDATE {0} SET {1} WHERE {2}".SFormat(table, sbvalues.ToString(), sbwheres.ToString());
|
||||
else
|
||||
return "UPDATE {0} SET {1}".SFormat(table, sbvalues.ToString());
|
||||
}
|
||||
|
||||
public string InsertValues(string table, List<SqlValue> values)
|
||||
{
|
||||
var sbnames = new StringBuilder();
|
||||
var sbvalues = new StringBuilder();
|
||||
int count = 0;
|
||||
|
||||
foreach (SqlValue name in values)
|
||||
{
|
||||
sbnames.Append(name.Name);
|
||||
|
||||
if (count != values.Count - 1)
|
||||
sbnames.Append(", ");
|
||||
count++;
|
||||
}
|
||||
count = 0;
|
||||
foreach (SqlValue value in values)
|
||||
{
|
||||
sbvalues.Append(value.Value.ToString());
|
||||
if (count != values.Count - 1)
|
||||
sbvalues.Append(", ");
|
||||
count++;
|
||||
}
|
||||
|
||||
return "INSERT INTO {0} ({1}) VALUES ({2})".SFormat(table, sbnames.ToString(), sbvalues.ToString());
|
||||
}
|
||||
|
||||
public string ReadColumn(string table, List<SqlValue> wheres)
|
||||
{
|
||||
var sbwheres = new StringBuilder();
|
||||
int count = 0;
|
||||
|
||||
foreach (SqlValue where in wheres)
|
||||
{
|
||||
sbwheres.Append(where.Name + "=" + where.Value);
|
||||
if (count != wheres.Count - 1)
|
||||
sbwheres.Append(" AND ");
|
||||
count++;
|
||||
}
|
||||
|
||||
if (wheres.Count > 0)
|
||||
return "SELECT * FROM {0} WHERE {1}".SFormat(table, sbwheres.ToString());
|
||||
else
|
||||
return "SELECT * FROM {0}".SFormat(table);
|
||||
}
|
||||
|
||||
private static readonly Dictionary<MySqlDbType, string> TypesAsStrings = new Dictionary<MySqlDbType, string>
|
||||
{
|
||||
{MySqlDbType.VarChar, "VARCHAR"},
|
||||
{MySqlDbType.String, "CHAR"},
|
||||
{MySqlDbType.Text, "TEXT"},
|
||||
{MySqlDbType.TinyText, "TINYTEXT"},
|
||||
{MySqlDbType.MediumText, "MEDIUMTEXT"},
|
||||
{MySqlDbType.LongText, "LONGTEXT"},
|
||||
{MySqlDbType.Int32, "INT"},
|
||||
};
|
||||
|
||||
public string DbTypeToString(MySqlDbType type, int? length)
|
||||
{
|
||||
string ret;
|
||||
if (TypesAsStrings.TryGetValue(type, out ret))
|
||||
return ret + (length != null ? "({0})".SFormat((int) length) : "");
|
||||
throw new NotImplementedException(Enum.GetName(typeof (MySqlDbType), type));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -22,81 +22,86 @@ using MySql.Data.MySqlClient;
|
|||
|
||||
namespace TShockAPI.DB
|
||||
{
|
||||
public class InventoryManager
|
||||
{
|
||||
public IDbConnection database;
|
||||
public class InventoryManager
|
||||
{
|
||||
public IDbConnection database;
|
||||
|
||||
public InventoryManager(IDbConnection db)
|
||||
{
|
||||
database = db;
|
||||
public InventoryManager(IDbConnection db)
|
||||
{
|
||||
database = db;
|
||||
|
||||
var table = new SqlTable("Inventory",
|
||||
new SqlColumn("Account", MySqlDbType.Int32) { Primary = true },
|
||||
new SqlColumn("MaxHealth", MySqlDbType.Int32),
|
||||
new SqlColumn("MaxMana", MySqlDbType.Int32),
|
||||
new SqlColumn("Inventory", MySqlDbType.Text)
|
||||
);
|
||||
var creator = new SqlTableCreator(db, db.GetSqlType() == SqlType.Sqlite ? (IQueryBuilder)new SqliteQueryCreator() : new MysqlQueryCreator());
|
||||
creator.EnsureExists(table);
|
||||
}
|
||||
var table = new SqlTable("Inventory",
|
||||
new SqlColumn("Account", MySqlDbType.Int32) {Primary = true},
|
||||
new SqlColumn("MaxHealth", MySqlDbType.Int32),
|
||||
new SqlColumn("MaxMana", MySqlDbType.Int32),
|
||||
new SqlColumn("Inventory", MySqlDbType.Text)
|
||||
);
|
||||
var creator = new SqlTableCreator(db,
|
||||
db.GetSqlType() == SqlType.Sqlite
|
||||
? (IQueryBuilder) new SqliteQueryCreator()
|
||||
: new MysqlQueryCreator());
|
||||
creator.EnsureExists(table);
|
||||
}
|
||||
|
||||
public PlayerData GetPlayerData(TSPlayer player, int acctid)
|
||||
{
|
||||
PlayerData playerData = new PlayerData(player);
|
||||
public PlayerData GetPlayerData(TSPlayer player, int acctid)
|
||||
{
|
||||
PlayerData playerData = new PlayerData(player);
|
||||
|
||||
try
|
||||
{
|
||||
using (var reader = database.QueryReader("SELECT * FROM Inventory WHERE Account=@0", acctid))
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
playerData.exists = true;
|
||||
playerData.maxHealth = reader.Get<int>("MaxHealth");
|
||||
playerData.inventory = NetItem.Parse(reader.Get<string>("Inventory"));
|
||||
return playerData;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
try
|
||||
{
|
||||
using (var reader = database.QueryReader("SELECT * FROM Inventory WHERE Account=@0", acctid))
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
playerData.exists = true;
|
||||
playerData.maxHealth = reader.Get<int>("MaxHealth");
|
||||
playerData.inventory = NetItem.Parse(reader.Get<string>("Inventory"));
|
||||
return playerData;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
|
||||
return playerData;
|
||||
}
|
||||
return playerData;
|
||||
}
|
||||
|
||||
public bool InsertPlayerData(TSPlayer player)
|
||||
{
|
||||
PlayerData playerData = player.PlayerData;
|
||||
public bool InsertPlayerData(TSPlayer player)
|
||||
{
|
||||
PlayerData playerData = player.PlayerData;
|
||||
|
||||
if (!player.IsLoggedIn)
|
||||
return false;
|
||||
if (!player.IsLoggedIn)
|
||||
return false;
|
||||
|
||||
if (!GetPlayerData(player, player.UserID).exists)
|
||||
{
|
||||
try
|
||||
{
|
||||
database.Query("INSERT INTO Inventory (Account, MaxHealth, Inventory) VALUES (@0, @1, @2);", player.UserID, playerData.maxHealth, NetItem.ToString(playerData.inventory));
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
database.Query("UPDATE Inventory SET MaxHealth = @0, Inventory = @1 WHERE Account = @2;", playerData.maxHealth, NetItem.ToString(playerData.inventory), player.UserID);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!GetPlayerData(player, player.UserID).exists)
|
||||
{
|
||||
try
|
||||
{
|
||||
database.Query("INSERT INTO Inventory (Account, MaxHealth, Inventory) VALUES (@0, @1, @2);", player.UserID,
|
||||
playerData.maxHealth, NetItem.ToString(playerData.inventory));
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
database.Query("UPDATE Inventory SET MaxHealth = @0, Inventory = @1 WHERE Account = @2;", playerData.maxHealth,
|
||||
NetItem.ToString(playerData.inventory), player.UserID);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,119 +7,122 @@ using MySql.Data.MySqlClient;
|
|||
|
||||
namespace TShockAPI.DB
|
||||
{
|
||||
public class ItemManager
|
||||
{
|
||||
private IDbConnection database;
|
||||
public class ItemManager
|
||||
{
|
||||
private IDbConnection database;
|
||||
public List<ItemBan> ItemBans = new List<ItemBan>();
|
||||
|
||||
public ItemManager(IDbConnection db)
|
||||
{
|
||||
database = db;
|
||||
public ItemManager(IDbConnection db)
|
||||
{
|
||||
database = db;
|
||||
|
||||
var table = new SqlTable("ItemBans",
|
||||
new SqlColumn("ItemName", MySqlDbType.VarChar, 50) { Primary = true },
|
||||
new SqlColumn("AllowedGroups", MySqlDbType.Text )
|
||||
);
|
||||
var creator = new SqlTableCreator(db, db.GetSqlType() == SqlType.Sqlite ? (IQueryBuilder)new SqliteQueryCreator() : new MysqlQueryCreator());
|
||||
creator.EnsureExists(table);
|
||||
var table = new SqlTable("ItemBans",
|
||||
new SqlColumn("ItemName", MySqlDbType.VarChar, 50) {Primary = true},
|
||||
new SqlColumn("AllowedGroups", MySqlDbType.Text)
|
||||
);
|
||||
var creator = new SqlTableCreator(db,
|
||||
db.GetSqlType() == SqlType.Sqlite
|
||||
? (IQueryBuilder) new SqliteQueryCreator()
|
||||
: new MysqlQueryCreator());
|
||||
creator.EnsureExists(table);
|
||||
|
||||
String file = Path.Combine(TShock.SavePath, "itembans.txt");
|
||||
if (File.Exists(file))
|
||||
{
|
||||
using (StreamReader sr = new StreamReader(file))
|
||||
{
|
||||
String line;
|
||||
while ((line = sr.ReadLine()) != null)
|
||||
{
|
||||
if (!line.Equals("") && !line.Substring(0, 1).Equals("#"))
|
||||
{
|
||||
String file = Path.Combine(TShock.SavePath, "itembans.txt");
|
||||
if (File.Exists(file))
|
||||
{
|
||||
using (StreamReader sr = new StreamReader(file))
|
||||
{
|
||||
String line;
|
||||
while ((line = sr.ReadLine()) != null)
|
||||
{
|
||||
if (!line.Equals("") && !line.Substring(0, 1).Equals("#"))
|
||||
{
|
||||
string query = (TShock.Config.StorageType.ToLower() == "sqlite")
|
||||
? "INSERT OR IGNORE INTO 'ItemBans' (ItemName, AllowedGroups) VALUES (@0, @1);"
|
||||
: "INSERT IGNORE INTO ItemBans SET ItemName=@0,AllowedGroups=@1 ;";
|
||||
|
||||
string query = (TShock.Config.StorageType.ToLower() == "sqlite") ?
|
||||
"INSERT OR IGNORE INTO 'ItemBans' (ItemName, AllowedGroups) VALUES (@0, @1);" :
|
||||
"INSERT IGNORE INTO ItemBans SET ItemName=@0,AllowedGroups=@1 ;";
|
||||
int id = 0;
|
||||
int.TryParse(line, out id);
|
||||
|
||||
int id = 0;
|
||||
int.TryParse(line, out id);
|
||||
database.Query(query, TShock.Utils.GetItemById(id).name, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
database.Query(query, TShock.Utils.GetItemById(id).name, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
String path = Path.Combine(TShock.SavePath, "old_configs");
|
||||
String file2 = Path.Combine(path, "itembans.txt");
|
||||
if (!Directory.Exists(path))
|
||||
Directory.CreateDirectory(path);
|
||||
if (File.Exists(file2))
|
||||
File.Delete(file2);
|
||||
File.Move(file, file2);
|
||||
}
|
||||
|
||||
String path = Path.Combine(TShock.SavePath, "old_configs");
|
||||
String file2 = Path.Combine(path, "itembans.txt");
|
||||
if (!Directory.Exists(path))
|
||||
Directory.CreateDirectory(path);
|
||||
if (File.Exists(file2))
|
||||
File.Delete(file2);
|
||||
File.Move(file, file2);
|
||||
}
|
||||
UpdateItemBans();
|
||||
}
|
||||
|
||||
UpdateItemBans();
|
||||
}
|
||||
public void UpdateItemBans()
|
||||
{
|
||||
ItemBans.Clear();
|
||||
|
||||
public void UpdateItemBans()
|
||||
{
|
||||
ItemBans.Clear();
|
||||
|
||||
using (var reader = database.QueryReader("SELECT * FROM ItemBans"))
|
||||
{
|
||||
while (reader != null && reader.Read())
|
||||
{
|
||||
using (var reader = database.QueryReader("SELECT * FROM ItemBans"))
|
||||
{
|
||||
while (reader != null && reader.Read())
|
||||
{
|
||||
ItemBan ban = new ItemBan(reader.Get<string>("ItemName"));
|
||||
ban.SetAllowedGroups( reader.Get<string>("AllowedGroups") );
|
||||
ban.SetAllowedGroups(reader.Get<string>("AllowedGroups"));
|
||||
ItemBans.Add(ban);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void AddNewBan(string itemname = "")
|
||||
{
|
||||
try
|
||||
{
|
||||
database.Query("INSERT INTO ItemBans (ItemName, AllowedGroups) VALUES (@0, @1);", TShock.Utils.GetItemByName(itemname)[0].name, "");
|
||||
if (!ItemIsBanned(itemname, null))
|
||||
ItemBans.Add( new ItemBan(itemname) );
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
}
|
||||
public void AddNewBan(string itemname = "")
|
||||
{
|
||||
try
|
||||
{
|
||||
database.Query("INSERT INTO ItemBans (ItemName, AllowedGroups) VALUES (@0, @1);",
|
||||
TShock.Utils.GetItemByName(itemname)[0].name, "");
|
||||
if (!ItemIsBanned(itemname, null))
|
||||
ItemBans.Add(new ItemBan(itemname));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveBan(string itemname)
|
||||
{
|
||||
if (!ItemIsBanned(itemname, null))
|
||||
return;
|
||||
try
|
||||
{
|
||||
database.Query("Delete FROM 'ItemBans' WHERE ItemName=@0;", TShock.Utils.GetItemByName(itemname)[0].name);
|
||||
ItemBans.Remove( new ItemBan(itemname) );
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
}
|
||||
public void RemoveBan(string itemname)
|
||||
{
|
||||
if (!ItemIsBanned(itemname, null))
|
||||
return;
|
||||
try
|
||||
{
|
||||
database.Query("Delete FROM 'ItemBans' WHERE ItemName=@0;", TShock.Utils.GetItemByName(itemname)[0].name);
|
||||
ItemBans.Remove(new ItemBan(itemname));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public bool ItemIsBanned(string name)
|
||||
{
|
||||
if (ItemBans.Contains(new ItemBan(name)))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public bool ItemIsBanned(string name)
|
||||
{
|
||||
if (ItemBans.Contains(new ItemBan(name)))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool ItemIsBanned(string name, TSPlayer ply)
|
||||
{
|
||||
if (ItemBans.Contains( new ItemBan(name) ) )
|
||||
public bool ItemIsBanned(string name, TSPlayer ply)
|
||||
{
|
||||
if (ItemBans.Contains(new ItemBan(name)))
|
||||
{
|
||||
ItemBan b = GetItemBanByName(name);
|
||||
return !b.HasPermissionToUseItem(ply);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool AllowGroup(string item, string name)
|
||||
{
|
||||
|
|
@ -134,7 +137,7 @@ namespace TShockAPI.DB
|
|||
b.SetAllowedGroups(groupsNew);
|
||||
|
||||
int q = database.Query("UPDATE ItemBans SET AllowedGroups=@0 WHERE ItemName=@1", groupsNew,
|
||||
item);
|
||||
item);
|
||||
|
||||
return q > 0;
|
||||
}
|
||||
|
|
@ -150,7 +153,7 @@ namespace TShockAPI.DB
|
|||
b.RemoveGroup(group);
|
||||
string groups = string.Join(",", b.AllowedGroups);
|
||||
int q = database.Query("UPDATE ItemBans SET AllowedGroups=@0 WHERE ItemName=@1", groups,
|
||||
item);
|
||||
item);
|
||||
if (q > 0)
|
||||
return true;
|
||||
}
|
||||
|
|
@ -168,58 +171,58 @@ namespace TShockAPI.DB
|
|||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class ItemBan : IEquatable<ItemBan>
|
||||
{
|
||||
public string Name { get; set; }
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public List<string> AllowedGroups { get; set; }
|
||||
|
||||
public ItemBan(string name)
|
||||
: this()
|
||||
{
|
||||
Name = name;
|
||||
public ItemBan(string name)
|
||||
: this()
|
||||
{
|
||||
Name = name;
|
||||
AllowedGroups = new List<string>();
|
||||
}
|
||||
}
|
||||
|
||||
public ItemBan()
|
||||
{
|
||||
Name = "";
|
||||
public ItemBan()
|
||||
{
|
||||
Name = "";
|
||||
AllowedGroups = new List<string>();
|
||||
}
|
||||
}
|
||||
|
||||
public bool Equals(ItemBan other)
|
||||
{
|
||||
return Name == other.Name;
|
||||
}
|
||||
|
||||
public bool HasPermissionToUseItem(TSPlayer ply)
|
||||
{
|
||||
public bool HasPermissionToUseItem(TSPlayer ply)
|
||||
{
|
||||
if (ply == null)
|
||||
return false;
|
||||
return AllowedGroups.Contains(ply.Group.Name); // could add in the other permissions in this class instead of a giant if switch.
|
||||
}
|
||||
|
||||
public void SetAllowedGroups( String groups )
|
||||
{
|
||||
// prevent null pointer exceptions
|
||||
if (!string.IsNullOrEmpty(groups))
|
||||
{
|
||||
List<String> groupArr = groups.Split(',').ToList();
|
||||
|
||||
for (int i = 0; i < groupArr.Count; i++)
|
||||
{
|
||||
groupArr[i] = groupArr[i].Trim();
|
||||
Console.WriteLine(groupArr[i]);
|
||||
}
|
||||
AllowedGroups = groupArr;
|
||||
}
|
||||
return AllowedGroups.Contains(ply.Group.Name);
|
||||
// could add in the other permissions in this class instead of a giant if switch.
|
||||
}
|
||||
|
||||
public bool RemoveGroup(string groupName)
|
||||
public void SetAllowedGroups(String groups)
|
||||
{
|
||||
// prevent null pointer exceptions
|
||||
if (!string.IsNullOrEmpty(groups))
|
||||
{
|
||||
List<String> groupArr = groups.Split(',').ToList();
|
||||
|
||||
for (int i = 0; i < groupArr.Count; i++)
|
||||
{
|
||||
groupArr[i] = groupArr[i].Trim();
|
||||
Console.WriteLine(groupArr[i]);
|
||||
}
|
||||
AllowedGroups = groupArr;
|
||||
}
|
||||
}
|
||||
|
||||
public bool RemoveGroup(string groupName)
|
||||
{
|
||||
return AllowedGroups.Remove(groupName);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
|
|
@ -23,69 +23,74 @@ using Terraria;
|
|||
|
||||
namespace TShockAPI.DB
|
||||
{
|
||||
public class RemeberedPosManager
|
||||
{
|
||||
public IDbConnection database;
|
||||
public class RemeberedPosManager
|
||||
{
|
||||
public IDbConnection database;
|
||||
|
||||
public RemeberedPosManager(IDbConnection db)
|
||||
{
|
||||
database = db;
|
||||
public RemeberedPosManager(IDbConnection db)
|
||||
{
|
||||
database = db;
|
||||
|
||||
var table = new SqlTable("RememberedPos",
|
||||
new SqlColumn("Name", MySqlDbType.VarChar, 50) { Primary = true },
|
||||
new SqlColumn("IP", MySqlDbType.Text),
|
||||
new SqlColumn("X", MySqlDbType.Int32),
|
||||
new SqlColumn("Y", MySqlDbType.Int32),
|
||||
new SqlColumn("WorldID", MySqlDbType.Text)
|
||||
);
|
||||
var creator = new SqlTableCreator(db, db.GetSqlType() == SqlType.Sqlite ? (IQueryBuilder)new SqliteQueryCreator() : new MysqlQueryCreator());
|
||||
creator.EnsureExists(table);
|
||||
}
|
||||
var table = new SqlTable("RememberedPos",
|
||||
new SqlColumn("Name", MySqlDbType.VarChar, 50) {Primary = true},
|
||||
new SqlColumn("IP", MySqlDbType.Text),
|
||||
new SqlColumn("X", MySqlDbType.Int32),
|
||||
new SqlColumn("Y", MySqlDbType.Int32),
|
||||
new SqlColumn("WorldID", MySqlDbType.Text)
|
||||
);
|
||||
var creator = new SqlTableCreator(db,
|
||||
db.GetSqlType() == SqlType.Sqlite
|
||||
? (IQueryBuilder) new SqliteQueryCreator()
|
||||
: new MysqlQueryCreator());
|
||||
creator.EnsureExists(table);
|
||||
}
|
||||
|
||||
public Vector2 GetLeavePos(string name, string IP)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var reader = database.QueryReader("SELECT * FROM RememberedPos WHERE Name=@0 AND IP=@1", name, IP))
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
return new Vector2(reader.Get<int>("X"), reader.Get<int>("Y"));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
public Vector2 GetLeavePos(string name, string IP)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var reader = database.QueryReader("SELECT * FROM RememberedPos WHERE Name=@0 AND IP=@1", name, IP))
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
return new Vector2(reader.Get<int>("X"), reader.Get<int>("Y"));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
|
||||
return new Vector2();
|
||||
}
|
||||
return new Vector2();
|
||||
}
|
||||
|
||||
public void InsertLeavePos(string name, string IP, int X, int Y)
|
||||
{
|
||||
if (GetLeavePos(name, IP) == Vector2.Zero)
|
||||
{
|
||||
try
|
||||
{
|
||||
database.Query("INSERT INTO RememberedPos (Name, IP, X, Y, WorldID) VALUES (@0, @1, @2, @3, @4);", name, IP, X, Y + 3, Main.worldID.ToString());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
database.Query("UPDATE RememberedPos SET X = @0, Y = @1 WHERE Name = @2 AND IP = @3 AND WorldID = @4;", X, Y + 3, name, IP, Main.worldID.ToString());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public void InsertLeavePos(string name, string IP, int X, int Y)
|
||||
{
|
||||
if (GetLeavePos(name, IP) == Vector2.Zero)
|
||||
{
|
||||
try
|
||||
{
|
||||
database.Query("INSERT INTO RememberedPos (Name, IP, X, Y, WorldID) VALUES (@0, @1, @2, @3, @4);", name, IP, X,
|
||||
Y + 3, Main.worldID.ToString());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
database.Query("UPDATE RememberedPos SET X = @0, Y = @1 WHERE Name = @2 AND IP = @3 AND WorldID = @4;", X, Y + 3,
|
||||
name, IP, Main.worldID.ToString());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,34 +2,35 @@
|
|||
|
||||
namespace TShockAPI.DB
|
||||
{
|
||||
public class SqlColumn
|
||||
{
|
||||
//Required
|
||||
public string Name { get; set; }
|
||||
public MySqlDbType Type { get; set; }
|
||||
public class SqlColumn
|
||||
{
|
||||
//Required
|
||||
public string Name { get; set; }
|
||||
public MySqlDbType Type { get; set; }
|
||||
|
||||
|
||||
//Optional
|
||||
public bool Unique { get; set; }
|
||||
public bool Primary { get; set; }
|
||||
public bool AutoIncrement { get; set; }
|
||||
public bool NotNull { get; set; }
|
||||
public string DefaultValue { get; set; }
|
||||
//Optional
|
||||
public bool Unique { get; set; }
|
||||
public bool Primary { get; set; }
|
||||
public bool AutoIncrement { get; set; }
|
||||
public bool NotNull { get; set; }
|
||||
public string DefaultValue { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Length of the data type, null = default
|
||||
/// </summary>
|
||||
public int? Length { get; set; }
|
||||
/// <summary>
|
||||
/// Length of the data type, null = default
|
||||
/// </summary>
|
||||
public int? Length { get; set; }
|
||||
|
||||
public SqlColumn(string name, MySqlDbType type)
|
||||
: this(name, type, null)
|
||||
{
|
||||
}
|
||||
public SqlColumn(string name, MySqlDbType type, int? length)
|
||||
{
|
||||
Name = name;
|
||||
Type = type;
|
||||
Length = length;
|
||||
}
|
||||
}
|
||||
}
|
||||
public SqlColumn(string name, MySqlDbType type)
|
||||
: this(name, type, null)
|
||||
{
|
||||
}
|
||||
|
||||
public SqlColumn(string name, MySqlDbType type, int? length)
|
||||
{
|
||||
Name = name;
|
||||
Type = type;
|
||||
Length = length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -6,79 +6,86 @@ using MySql.Data.MySqlClient;
|
|||
|
||||
namespace TShockAPI.DB
|
||||
{
|
||||
public class SqlTable
|
||||
{
|
||||
public List<SqlColumn> Columns { get; protected set; }
|
||||
public string Name { get; protected set; }
|
||||
public SqlTable(string name, params SqlColumn[] columns)
|
||||
: this(name, new List<SqlColumn>(columns))
|
||||
{
|
||||
}
|
||||
public SqlTable(string name, List<SqlColumn> columns)
|
||||
{
|
||||
Name = name;
|
||||
Columns = columns;
|
||||
}
|
||||
}
|
||||
public class SqlTable
|
||||
{
|
||||
public List<SqlColumn> Columns { get; protected set; }
|
||||
public string Name { get; protected set; }
|
||||
|
||||
public class SqlTableCreator
|
||||
{
|
||||
IDbConnection database;
|
||||
IQueryBuilder creator;
|
||||
public SqlTableCreator(IDbConnection db, IQueryBuilder provider)
|
||||
{
|
||||
database = db;
|
||||
creator = provider;
|
||||
}
|
||||
public SqlTable(string name, params SqlColumn[] columns)
|
||||
: this(name, new List<SqlColumn>(columns))
|
||||
{
|
||||
}
|
||||
|
||||
public void EnsureExists(SqlTable table)
|
||||
{
|
||||
var columns = GetColumns(table);
|
||||
if (columns.Count > 0)
|
||||
{
|
||||
if (!table.Columns.All(c => columns.Contains(c.Name)) || !columns.All(c => table.Columns.Any(c2 => c2.Name == c)))
|
||||
{
|
||||
var from = new SqlTable(table.Name, columns.Select(s => new SqlColumn(s, MySqlDbType.String)).ToList());
|
||||
database.Query(creator.AlterTable(from, table));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
database.Query(creator.CreateTable(table));
|
||||
}
|
||||
}
|
||||
public SqlTable(string name, List<SqlColumn> columns)
|
||||
{
|
||||
Name = name;
|
||||
Columns = columns;
|
||||
}
|
||||
}
|
||||
|
||||
public List<string> GetColumns(SqlTable table)
|
||||
{
|
||||
var ret = new List<string>();
|
||||
var name = database.GetSqlType();
|
||||
if (name == SqlType.Sqlite)
|
||||
{
|
||||
using (var reader = database.QueryReader("PRAGMA table_info({0})".SFormat(table.Name)))
|
||||
{
|
||||
while (reader.Read())
|
||||
ret.Add(reader.Get<string>("name"));
|
||||
}
|
||||
}
|
||||
else if (name == SqlType.Mysql)
|
||||
{
|
||||
using (var reader = database.QueryReader("SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME=@0 AND TABLE_SCHEMA=@1", table.Name, database.Database))
|
||||
{
|
||||
while (reader.Read())
|
||||
ret.Add(reader.Get<string>("COLUMN_NAME"));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
public class SqlTableCreator
|
||||
{
|
||||
private IDbConnection database;
|
||||
private IQueryBuilder creator;
|
||||
|
||||
return ret;
|
||||
}
|
||||
public SqlTableCreator(IDbConnection db, IQueryBuilder provider)
|
||||
{
|
||||
database = db;
|
||||
creator = provider;
|
||||
}
|
||||
|
||||
public void DeleteRow(string table, List<SqlValue> wheres)
|
||||
{
|
||||
database.Query(creator.DeleteRow(table, wheres));
|
||||
}
|
||||
}
|
||||
}
|
||||
public void EnsureExists(SqlTable table)
|
||||
{
|
||||
var columns = GetColumns(table);
|
||||
if (columns.Count > 0)
|
||||
{
|
||||
if (!table.Columns.All(c => columns.Contains(c.Name)) || !columns.All(c => table.Columns.Any(c2 => c2.Name == c)))
|
||||
{
|
||||
var from = new SqlTable(table.Name, columns.Select(s => new SqlColumn(s, MySqlDbType.String)).ToList());
|
||||
database.Query(creator.AlterTable(from, table));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
database.Query(creator.CreateTable(table));
|
||||
}
|
||||
}
|
||||
|
||||
public List<string> GetColumns(SqlTable table)
|
||||
{
|
||||
var ret = new List<string>();
|
||||
var name = database.GetSqlType();
|
||||
if (name == SqlType.Sqlite)
|
||||
{
|
||||
using (var reader = database.QueryReader("PRAGMA table_info({0})".SFormat(table.Name)))
|
||||
{
|
||||
while (reader.Read())
|
||||
ret.Add(reader.Get<string>("name"));
|
||||
}
|
||||
}
|
||||
else if (name == SqlType.Mysql)
|
||||
{
|
||||
using (
|
||||
var reader =
|
||||
database.QueryReader(
|
||||
"SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_NAME=@0 AND TABLE_SCHEMA=@1", table.Name,
|
||||
database.Database))
|
||||
{
|
||||
while (reader.Read())
|
||||
ret.Add(reader.Get<string>("COLUMN_NAME"));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void DeleteRow(string table, List<SqlValue> wheres)
|
||||
{
|
||||
database.Query(creator.DeleteRow(table, wheres));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,50 +3,50 @@ using System.Data;
|
|||
|
||||
namespace TShockAPI.DB
|
||||
{
|
||||
public class SqlValue
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public object Value { get; set; }
|
||||
public class SqlValue
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public object Value { get; set; }
|
||||
|
||||
public SqlValue(string name, object value)
|
||||
{
|
||||
Name = name;
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
public SqlValue(string name, object value)
|
||||
{
|
||||
Name = name;
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
|
||||
public class SqlTableEditor
|
||||
{
|
||||
IDbConnection database;
|
||||
IQueryBuilder creator;
|
||||
public class SqlTableEditor
|
||||
{
|
||||
private IDbConnection database;
|
||||
private IQueryBuilder creator;
|
||||
|
||||
public SqlTableEditor(IDbConnection db, IQueryBuilder provider)
|
||||
{
|
||||
database = db;
|
||||
creator = provider;
|
||||
}
|
||||
public SqlTableEditor(IDbConnection db, IQueryBuilder provider)
|
||||
{
|
||||
database = db;
|
||||
creator = provider;
|
||||
}
|
||||
|
||||
public void UpdateValues(string table, List<SqlValue> values, List<SqlValue> wheres)
|
||||
{
|
||||
database.Query(creator.UpdateValue(table, values, wheres));
|
||||
}
|
||||
public void UpdateValues(string table, List<SqlValue> values, List<SqlValue> wheres)
|
||||
{
|
||||
database.Query(creator.UpdateValue(table, values, wheres));
|
||||
}
|
||||
|
||||
public void InsertValues(string table, List<SqlValue> values)
|
||||
{
|
||||
database.Query(creator.InsertValues(table, values));
|
||||
}
|
||||
public void InsertValues(string table, List<SqlValue> values)
|
||||
{
|
||||
database.Query(creator.InsertValues(table, values));
|
||||
}
|
||||
|
||||
public List<object> ReadColumn(string table, string column, List<SqlValue> wheres)
|
||||
{
|
||||
List<object> values = new List<object>();
|
||||
public List<object> ReadColumn(string table, string column, List<SqlValue> wheres)
|
||||
{
|
||||
List<object> values = new List<object>();
|
||||
|
||||
using (var reader = database.QueryReader(creator.ReadColumn(table, wheres)))
|
||||
{
|
||||
while (reader.Read())
|
||||
values.Add(reader.Reader.Get<object>(column));
|
||||
}
|
||||
using (var reader = database.QueryReader(creator.ReadColumn(table, wheres)))
|
||||
{
|
||||
while (reader.Read())
|
||||
values.Add(reader.Reader.Get<object>(column));
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
}
|
||||
}
|
||||
return values;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
|
||||
/*
|
||||
/*
|
||||
TShock, a server mod for Terraria
|
||||
Copyright (C) 2011 The TShock Team
|
||||
|
||||
|
|
@ -24,354 +23,366 @@ using MySql.Data.MySqlClient;
|
|||
|
||||
namespace TShockAPI.DB
|
||||
{
|
||||
public class UserManager
|
||||
{
|
||||
private IDbConnection database;
|
||||
public class UserManager
|
||||
{
|
||||
private IDbConnection database;
|
||||
|
||||
public UserManager(IDbConnection db)
|
||||
{
|
||||
database = db;
|
||||
public UserManager(IDbConnection db)
|
||||
{
|
||||
database = db;
|
||||
|
||||
var table = new SqlTable("Users",
|
||||
new SqlColumn("ID", MySqlDbType.Int32) { Primary = true, AutoIncrement = true },
|
||||
new SqlColumn("Username", MySqlDbType.VarChar, 32) { Unique = true },
|
||||
new SqlColumn("Password", MySqlDbType.VarChar, 128),
|
||||
new SqlColumn("Usergroup", MySqlDbType.Text),
|
||||
new SqlColumn("IP", MySqlDbType.VarChar, 16)
|
||||
);
|
||||
var creator = new SqlTableCreator(db, db.GetSqlType() == SqlType.Sqlite ? (IQueryBuilder)new SqliteQueryCreator() : new MysqlQueryCreator());
|
||||
creator.EnsureExists(table);
|
||||
var table = new SqlTable("Users",
|
||||
new SqlColumn("ID", MySqlDbType.Int32) {Primary = true, AutoIncrement = true},
|
||||
new SqlColumn("Username", MySqlDbType.VarChar, 32) {Unique = true},
|
||||
new SqlColumn("Password", MySqlDbType.VarChar, 128),
|
||||
new SqlColumn("Usergroup", MySqlDbType.Text),
|
||||
new SqlColumn("IP", MySqlDbType.VarChar, 16)
|
||||
);
|
||||
var creator = new SqlTableCreator(db,
|
||||
db.GetSqlType() == SqlType.Sqlite
|
||||
? (IQueryBuilder) new SqliteQueryCreator()
|
||||
: new MysqlQueryCreator());
|
||||
creator.EnsureExists(table);
|
||||
|
||||
String file = Path.Combine(TShock.SavePath, "users.txt");
|
||||
if (File.Exists(file))
|
||||
{
|
||||
using (StreamReader sr = new StreamReader(file))
|
||||
{
|
||||
String line;
|
||||
while ((line = sr.ReadLine()) != null)
|
||||
{
|
||||
if (line.Equals("") || line.Substring(0, 1).Equals("#"))
|
||||
continue;
|
||||
String[] info = line.Split(' ');
|
||||
String username = "";
|
||||
String sha = "";
|
||||
String group = "";
|
||||
String ip = "";
|
||||
String file = Path.Combine(TShock.SavePath, "users.txt");
|
||||
if (File.Exists(file))
|
||||
{
|
||||
using (StreamReader sr = new StreamReader(file))
|
||||
{
|
||||
String line;
|
||||
while ((line = sr.ReadLine()) != null)
|
||||
{
|
||||
if (line.Equals("") || line.Substring(0, 1).Equals("#"))
|
||||
continue;
|
||||
String[] info = line.Split(' ');
|
||||
String username = "";
|
||||
String sha = "";
|
||||
String group = "";
|
||||
String ip = "";
|
||||
|
||||
String[] nameSha = info[0].Split(':');
|
||||
String[] nameSha = info[0].Split(':');
|
||||
|
||||
if (nameSha.Length < 2)
|
||||
{
|
||||
username = nameSha[0];
|
||||
ip = nameSha[0];
|
||||
group = info[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
username = nameSha[0];
|
||||
sha = nameSha[1];
|
||||
group = info[1];
|
||||
}
|
||||
if (nameSha.Length < 2)
|
||||
{
|
||||
username = nameSha[0];
|
||||
ip = nameSha[0];
|
||||
group = info[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
username = nameSha[0];
|
||||
sha = nameSha[1];
|
||||
group = info[1];
|
||||
}
|
||||
|
||||
string query = (TShock.Config.StorageType.ToLower() == "sqlite") ?
|
||||
"INSERT OR IGNORE INTO Users (Username, Password, Usergroup, IP) VALUES (@0, @1, @2, @3)" :
|
||||
"INSERT IGNORE INTO Users SET Username=@0, Password=@1, Usergroup=@2, IP=@3";
|
||||
string query = (TShock.Config.StorageType.ToLower() == "sqlite")
|
||||
? "INSERT OR IGNORE INTO Users (Username, Password, Usergroup, IP) VALUES (@0, @1, @2, @3)"
|
||||
: "INSERT IGNORE INTO Users SET Username=@0, Password=@1, Usergroup=@2, IP=@3";
|
||||
|
||||
database.Query(query, username.Trim(), sha.Trim(), group.Trim(), ip.Trim());
|
||||
}
|
||||
}
|
||||
String path = Path.Combine(TShock.SavePath, "old_configs");
|
||||
String file2 = Path.Combine(path, "users.txt");
|
||||
if (!Directory.Exists(path))
|
||||
Directory.CreateDirectory(path);
|
||||
if (File.Exists(file2))
|
||||
File.Delete(file2);
|
||||
File.Move(file, file2);
|
||||
}
|
||||
database.Query(query, username.Trim(), sha.Trim(), group.Trim(), ip.Trim());
|
||||
}
|
||||
}
|
||||
String path = Path.Combine(TShock.SavePath, "old_configs");
|
||||
String file2 = Path.Combine(path, "users.txt");
|
||||
if (!Directory.Exists(path))
|
||||
Directory.CreateDirectory(path);
|
||||
if (File.Exists(file2))
|
||||
File.Delete(file2);
|
||||
File.Move(file, file2);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Adds a given username to the database
|
||||
/// </summary>
|
||||
/// <param name="user">User user</param>
|
||||
public void AddUser(User user)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!TShock.Groups.GroupExists(user.Group))
|
||||
throw new GroupNotExistsException(user.Group);
|
||||
|
||||
/// <summary>
|
||||
/// Adds a given username to the database
|
||||
/// </summary>
|
||||
/// <param name="user">User user</param>
|
||||
public void AddUser(User user)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!TShock.Groups.GroupExists(user.Group))
|
||||
throw new GroupNotExistsException(user.Group);
|
||||
if (
|
||||
database.Query("INSERT INTO Users (Username, Password, UserGroup, IP) VALUES (@0, @1, @2, @3);", user.Name,
|
||||
TShock.Utils.HashPassword(user.Password), user.Group, user.Address) < 1)
|
||||
throw new UserExistsException(user.Name);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new UserManagerException("AddUser SQL returned an error", ex);
|
||||
}
|
||||
}
|
||||
|
||||
if (database.Query("INSERT INTO Users (Username, Password, UserGroup, IP) VALUES (@0, @1, @2, @3);", user.Name, TShock.Utils.HashPassword(user.Password), user.Group, user.Address) < 1)
|
||||
throw new UserExistsException(user.Name);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new UserManagerException("AddUser SQL returned an error", ex);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Removes a given username from the database
|
||||
/// </summary>
|
||||
/// <param name="user">User user</param>
|
||||
public void RemoveUser(User user)
|
||||
{
|
||||
try
|
||||
{
|
||||
int affected = -1;
|
||||
if (!string.IsNullOrEmpty(user.Address))
|
||||
{
|
||||
affected = database.Query("DELETE FROM Users WHERE IP=@0", user.Address);
|
||||
}
|
||||
else
|
||||
{
|
||||
affected = database.Query("DELETE FROM Users WHERE Username=@0", user.Name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a given username from the database
|
||||
/// </summary>
|
||||
/// <param name="user">User user</param>
|
||||
public void RemoveUser(User user)
|
||||
{
|
||||
try
|
||||
{
|
||||
int affected = -1;
|
||||
if (!string.IsNullOrEmpty(user.Address))
|
||||
{
|
||||
affected = database.Query("DELETE FROM Users WHERE IP=@0", user.Address);
|
||||
}
|
||||
else
|
||||
{
|
||||
affected = database.Query("DELETE FROM Users WHERE Username=@0", user.Name);
|
||||
}
|
||||
|
||||
if (affected < 1)
|
||||
throw new UserNotExistException(string.IsNullOrEmpty(user.Address) ? user.Name : user.Address);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new UserManagerException("RemoveUser SQL returned an error", ex);
|
||||
}
|
||||
}
|
||||
if (affected < 1)
|
||||
throw new UserNotExistException(string.IsNullOrEmpty(user.Address) ? user.Name : user.Address);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new UserManagerException("RemoveUser SQL returned an error", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Sets the Hashed Password for a given username
|
||||
/// </summary>
|
||||
/// <param name="user">User user</param>
|
||||
/// <param name="group">string password</param>
|
||||
public void SetUserPassword(User user, string password)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (database.Query("UPDATE Users SET Password = @0 WHERE Username = @1;", TShock.Utils.HashPassword(password), user.Name) == 0)
|
||||
throw new UserNotExistException(user.Name);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new UserManagerException("SetUserPassword SQL returned an error", ex);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Sets the Hashed Password for a given username
|
||||
/// </summary>
|
||||
/// <param name="user">User user</param>
|
||||
/// <param name="group">string password</param>
|
||||
public void SetUserPassword(User user, string password)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (
|
||||
database.Query("UPDATE Users SET Password = @0 WHERE Username = @1;", TShock.Utils.HashPassword(password),
|
||||
user.Name) == 0)
|
||||
throw new UserNotExistException(user.Name);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new UserManagerException("SetUserPassword SQL returned an error", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the group for a given username
|
||||
/// </summary>
|
||||
/// <param name="user">User user</param>
|
||||
/// <param name="group">string group</param>
|
||||
public void SetUserGroup(User user, string group)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!TShock.Groups.GroupExists(group))
|
||||
throw new GroupNotExistsException(group);
|
||||
/// <summary>
|
||||
/// Sets the group for a given username
|
||||
/// </summary>
|
||||
/// <param name="user">User user</param>
|
||||
/// <param name="group">string group</param>
|
||||
public void SetUserGroup(User user, string group)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!TShock.Groups.GroupExists(group))
|
||||
throw new GroupNotExistsException(group);
|
||||
|
||||
if (database.Query("UPDATE Users SET UserGroup = @0 WHERE Username = @1;", group, user.Name) == 0)
|
||||
throw new UserNotExistException(user.Name);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new UserManagerException("SetUserGroup SQL returned an error", ex);
|
||||
}
|
||||
}
|
||||
if (database.Query("UPDATE Users SET UserGroup = @0 WHERE Username = @1;", group, user.Name) == 0)
|
||||
throw new UserNotExistException(user.Name);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new UserManagerException("SetUserGroup SQL returned an error", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public int GetUserID(string username)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var reader = database.QueryReader("SELECT * FROM Users WHERE Username=@0", username))
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
return reader.Get<int>("ID");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.ConsoleError("FetchHashedPasswordAndGroup SQL returned an error: " + ex);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
public int GetUserID(string username)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var reader = database.QueryReader("SELECT * FROM Users WHERE Username=@0", username))
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
return reader.Get<int>("ID");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.ConsoleError("FetchHashedPasswordAndGroup SQL returned an error: " + ex);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a Group for a ip from the database
|
||||
/// </summary>
|
||||
/// <param name="ply">string ip</param>
|
||||
public Group GetGroupForIP(string ip)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var reader = database.QueryReader("SELECT * FROM Users WHERE IP=@0", ip))
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
string group = reader.Get<string>("UserGroup");
|
||||
return TShock.Utils.GetGroup(group);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.ConsoleError("GetGroupForIP SQL returned an error: " + ex);
|
||||
}
|
||||
return TShock.Utils.GetGroup(TShock.Config.DefaultGuestGroupName);
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns a Group for a ip from the database
|
||||
/// </summary>
|
||||
/// <param name="ply">string ip</param>
|
||||
public Group GetGroupForIP(string ip)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var reader = database.QueryReader("SELECT * FROM Users WHERE IP=@0", ip))
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
string group = reader.Get<string>("UserGroup");
|
||||
return TShock.Utils.GetGroup(group);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.ConsoleError("GetGroupForIP SQL returned an error: " + ex);
|
||||
}
|
||||
return TShock.Utils.GetGroup(TShock.Config.DefaultGuestGroupName);
|
||||
}
|
||||
|
||||
public Group GetGroupForIPExpensive(string ip)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var reader = database.QueryReader("SELECT IP, UserGroup FROM Users"))
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
if (TShock.Utils.GetIPv4Address(reader.Get<string>("IP")) == ip)
|
||||
{
|
||||
return TShock.Utils.GetGroup(reader.Get<string>("UserGroup"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.ConsoleError("GetGroupForIP SQL returned an error: " + ex);
|
||||
}
|
||||
return TShock.Utils.GetGroup(TShock.Config.DefaultGuestGroupName);
|
||||
}
|
||||
public Group GetGroupForIPExpensive(string ip)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var reader = database.QueryReader("SELECT IP, UserGroup FROM Users"))
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
if (TShock.Utils.GetIPv4Address(reader.Get<string>("IP")) == ip)
|
||||
{
|
||||
return TShock.Utils.GetGroup(reader.Get<string>("UserGroup"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.ConsoleError("GetGroupForIP SQL returned an error: " + ex);
|
||||
}
|
||||
return TShock.Utils.GetGroup(TShock.Config.DefaultGuestGroupName);
|
||||
}
|
||||
|
||||
|
||||
public User GetUserByName(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
return GetUser(new User { Name = name });
|
||||
}
|
||||
catch (UserManagerException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public User GetUserByID(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return GetUser(new User { ID = id });
|
||||
}
|
||||
catch (UserManagerException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public User GetUserByIP(string ip)
|
||||
{
|
||||
try
|
||||
{
|
||||
return GetUser(new User { Address = ip });
|
||||
}
|
||||
catch (UserManagerException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public User GetUser(User user)
|
||||
{
|
||||
try
|
||||
{
|
||||
QueryResult result;
|
||||
if (string.IsNullOrEmpty(user.Address))
|
||||
{
|
||||
result = database.QueryReader("SELECT * FROM Users WHERE Username=@0", user.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = database.QueryReader("SELECT * FROM Users WHERE IP=@0", user.Address);
|
||||
}
|
||||
public User GetUserByName(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
return GetUser(new User {Name = name});
|
||||
}
|
||||
catch (UserManagerException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
using (var reader = result)
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
user.ID = reader.Get<int>("ID");
|
||||
user.Group = reader.Get<string>("Usergroup");
|
||||
user.Password = reader.Get<string>("Password");
|
||||
user.Name = reader.Get<string>("Username");
|
||||
user.Address = reader.Get<string>("IP");
|
||||
return user;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new UserManagerException("GetUserID SQL returned an error", ex);
|
||||
}
|
||||
throw new UserNotExistException(string.IsNullOrEmpty(user.Address) ? user.Name : user.Address);
|
||||
}
|
||||
}
|
||||
public User GetUserByID(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return GetUser(new User {ID = id});
|
||||
}
|
||||
catch (UserManagerException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public class User
|
||||
{
|
||||
public int ID { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Password { get; set; }
|
||||
public string Group { get; set; }
|
||||
public string Address { get; set; }
|
||||
public User GetUserByIP(string ip)
|
||||
{
|
||||
try
|
||||
{
|
||||
return GetUser(new User {Address = ip});
|
||||
}
|
||||
catch (UserManagerException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public User(string ip, string name, string pass, string group)
|
||||
{
|
||||
Address = ip;
|
||||
Name = name;
|
||||
Password = pass;
|
||||
Group = group;
|
||||
}
|
||||
public User()
|
||||
{
|
||||
Address = "";
|
||||
Name = "";
|
||||
Password = "";
|
||||
Group = "";
|
||||
}
|
||||
}
|
||||
public User GetUser(User user)
|
||||
{
|
||||
try
|
||||
{
|
||||
QueryResult result;
|
||||
if (string.IsNullOrEmpty(user.Address))
|
||||
{
|
||||
result = database.QueryReader("SELECT * FROM Users WHERE Username=@0", user.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = database.QueryReader("SELECT * FROM Users WHERE IP=@0", user.Address);
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class UserManagerException : Exception
|
||||
{
|
||||
public UserManagerException(string message)
|
||||
: base(message)
|
||||
{
|
||||
using (var reader = result)
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
user.ID = reader.Get<int>("ID");
|
||||
user.Group = reader.Get<string>("Usergroup");
|
||||
user.Password = reader.Get<string>("Password");
|
||||
user.Name = reader.Get<string>("Username");
|
||||
user.Address = reader.Get<string>("IP");
|
||||
return user;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new UserManagerException("GetUserID SQL returned an error", ex);
|
||||
}
|
||||
throw new UserNotExistException(string.IsNullOrEmpty(user.Address) ? user.Name : user.Address);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public UserManagerException(string message, Exception inner)
|
||||
: base(message, inner)
|
||||
{
|
||||
public class User
|
||||
{
|
||||
public int ID { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Password { get; set; }
|
||||
public string Group { get; set; }
|
||||
public string Address { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
[Serializable]
|
||||
public class UserExistsException : UserManagerException
|
||||
{
|
||||
public UserExistsException(string name)
|
||||
: base("User '" + name + "' already exists")
|
||||
{
|
||||
}
|
||||
}
|
||||
[Serializable]
|
||||
public class UserNotExistException : UserManagerException
|
||||
{
|
||||
public UserNotExistException(string name)
|
||||
: base("User '" + name + "' does not exist")
|
||||
{
|
||||
}
|
||||
}
|
||||
[Serializable]
|
||||
public class GroupNotExistsException : UserManagerException
|
||||
{
|
||||
public GroupNotExistsException(string group)
|
||||
: base("Group '" + group + "' does not exist")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
public User(string ip, string name, string pass, string group)
|
||||
{
|
||||
Address = ip;
|
||||
Name = name;
|
||||
Password = pass;
|
||||
Group = group;
|
||||
}
|
||||
|
||||
public User()
|
||||
{
|
||||
Address = "";
|
||||
Name = "";
|
||||
Password = "";
|
||||
Group = "";
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class UserManagerException : Exception
|
||||
{
|
||||
public UserManagerException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public UserManagerException(string message, Exception inner)
|
||||
: base(message, inner)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class UserExistsException : UserManagerException
|
||||
{
|
||||
public UserExistsException(string name)
|
||||
: base("User '" + name + "' already exists")
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class UserNotExistException : UserManagerException
|
||||
{
|
||||
public UserNotExistException(string name)
|
||||
: base("User '" + name + "' does not exist")
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class GroupNotExistsException : UserManagerException
|
||||
{
|
||||
public GroupNotExistsException(string group)
|
||||
: base("Group '" + group + "' does not exist")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -27,245 +27,251 @@ using Terraria;
|
|||
|
||||
namespace TShockAPI.DB
|
||||
{
|
||||
public class WarpManager
|
||||
{
|
||||
private IDbConnection database;
|
||||
public class WarpManager
|
||||
{
|
||||
private IDbConnection database;
|
||||
|
||||
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
|
||||
public WarpManager(IDbConnection db)
|
||||
{
|
||||
database = db;
|
||||
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
|
||||
public WarpManager(IDbConnection db)
|
||||
{
|
||||
database = db;
|
||||
|
||||
var table = new SqlTable("Warps",
|
||||
new SqlColumn("WarpName", MySqlDbType.VarChar, 50) { Primary = true },
|
||||
new SqlColumn("X", MySqlDbType.Int32),
|
||||
new SqlColumn("Y", MySqlDbType.Int32),
|
||||
new SqlColumn("WorldID", MySqlDbType.Text),
|
||||
new SqlColumn("Private", MySqlDbType.Text)
|
||||
);
|
||||
var creator = new SqlTableCreator(db, db.GetSqlType() == SqlType.Sqlite ? (IQueryBuilder)new SqliteQueryCreator() : new MysqlQueryCreator());
|
||||
creator.EnsureExists(table);
|
||||
var table = new SqlTable("Warps",
|
||||
new SqlColumn("WarpName", MySqlDbType.VarChar, 50) {Primary = true},
|
||||
new SqlColumn("X", MySqlDbType.Int32),
|
||||
new SqlColumn("Y", MySqlDbType.Int32),
|
||||
new SqlColumn("WorldID", MySqlDbType.Text),
|
||||
new SqlColumn("Private", MySqlDbType.Text)
|
||||
);
|
||||
var creator = new SqlTableCreator(db,
|
||||
db.GetSqlType() == SqlType.Sqlite
|
||||
? (IQueryBuilder) new SqliteQueryCreator()
|
||||
: new MysqlQueryCreator());
|
||||
creator.EnsureExists(table);
|
||||
|
||||
String file = Path.Combine(TShock.SavePath, "warps.xml");
|
||||
String name = "";
|
||||
String world = "";
|
||||
int x1 = 0;
|
||||
int y1 = 0;
|
||||
if (!File.Exists(file))
|
||||
return;
|
||||
String file = Path.Combine(TShock.SavePath, "warps.xml");
|
||||
String name = "";
|
||||
String world = "";
|
||||
int x1 = 0;
|
||||
int y1 = 0;
|
||||
if (!File.Exists(file))
|
||||
return;
|
||||
|
||||
using (var reader = XmlReader.Create(new StreamReader(file), new XmlReaderSettings { CloseInput = true }))
|
||||
{
|
||||
// Parse the file and display each of the nodes.
|
||||
while (reader.Read())
|
||||
{
|
||||
switch (reader.NodeType)
|
||||
{
|
||||
case XmlNodeType.Element:
|
||||
switch (reader.Name)
|
||||
{
|
||||
case "Warp":
|
||||
name = "";
|
||||
world = "";
|
||||
x1 = 0;
|
||||
y1 = 0;
|
||||
break;
|
||||
case "WarpName":
|
||||
while (reader.NodeType != XmlNodeType.Text)
|
||||
reader.Read();
|
||||
name = reader.Value;
|
||||
break;
|
||||
case "X":
|
||||
while (reader.NodeType != XmlNodeType.Text)
|
||||
reader.Read();
|
||||
int.TryParse(reader.Value, out x1);
|
||||
break;
|
||||
case "Y":
|
||||
while (reader.NodeType != XmlNodeType.Text)
|
||||
reader.Read();
|
||||
int.TryParse(reader.Value, out y1);
|
||||
break;
|
||||
case "WorldName":
|
||||
while (reader.NodeType != XmlNodeType.Text)
|
||||
reader.Read();
|
||||
world = reader.Value;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case XmlNodeType.Text:
|
||||
using (var reader = XmlReader.Create(new StreamReader(file), new XmlReaderSettings {CloseInput = true}))
|
||||
{
|
||||
// Parse the file and display each of the nodes.
|
||||
while (reader.Read())
|
||||
{
|
||||
switch (reader.NodeType)
|
||||
{
|
||||
case XmlNodeType.Element:
|
||||
switch (reader.Name)
|
||||
{
|
||||
case "Warp":
|
||||
name = "";
|
||||
world = "";
|
||||
x1 = 0;
|
||||
y1 = 0;
|
||||
break;
|
||||
case "WarpName":
|
||||
while (reader.NodeType != XmlNodeType.Text)
|
||||
reader.Read();
|
||||
name = reader.Value;
|
||||
break;
|
||||
case "X":
|
||||
while (reader.NodeType != XmlNodeType.Text)
|
||||
reader.Read();
|
||||
int.TryParse(reader.Value, out x1);
|
||||
break;
|
||||
case "Y":
|
||||
while (reader.NodeType != XmlNodeType.Text)
|
||||
reader.Read();
|
||||
int.TryParse(reader.Value, out y1);
|
||||
break;
|
||||
case "WorldName":
|
||||
while (reader.NodeType != XmlNodeType.Text)
|
||||
reader.Read();
|
||||
world = reader.Value;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case XmlNodeType.Text:
|
||||
|
||||
break;
|
||||
case XmlNodeType.XmlDeclaration:
|
||||
case XmlNodeType.ProcessingInstruction:
|
||||
break;
|
||||
case XmlNodeType.Comment:
|
||||
break;
|
||||
case XmlNodeType.EndElement:
|
||||
if (reader.Name.Equals("Warp"))
|
||||
{
|
||||
string query = (TShock.Config.StorageType.ToLower() == "sqlite")
|
||||
? "INSERT OR IGNORE INTO Warps VALUES (@0, @1,@2, @3);"
|
||||
: "INSERT IGNORE INTO Warps SET X=@0, Y=@1, WarpName=@2, WorldID=@3;";
|
||||
database.Query(query, x1, y1, name, world);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case XmlNodeType.XmlDeclaration:
|
||||
case XmlNodeType.ProcessingInstruction:
|
||||
break;
|
||||
case XmlNodeType.Comment:
|
||||
break;
|
||||
case XmlNodeType.EndElement:
|
||||
if (reader.Name.Equals("Warp"))
|
||||
{
|
||||
string query = (TShock.Config.StorageType.ToLower() == "sqlite")
|
||||
? "INSERT OR IGNORE INTO Warps VALUES (@0, @1,@2, @3);"
|
||||
: "INSERT IGNORE INTO Warps SET X=@0, Y=@1, WarpName=@2, WorldID=@3;";
|
||||
database.Query(query, x1, y1, name, world);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
String path = Path.Combine(TShock.SavePath, "old_configs");
|
||||
String file2 = Path.Combine(path, "warps.xml");
|
||||
if (!Directory.Exists(path))
|
||||
Directory.CreateDirectory(path);
|
||||
if (File.Exists(file2))
|
||||
File.Delete(file2);
|
||||
File.Move(file, file2);
|
||||
}
|
||||
|
||||
String path = Path.Combine(TShock.SavePath, "old_configs");
|
||||
String file2 = Path.Combine(path, "warps.xml");
|
||||
if (!Directory.Exists(path))
|
||||
Directory.CreateDirectory(path);
|
||||
if (File.Exists(file2))
|
||||
File.Delete(file2);
|
||||
File.Move(file, file2);
|
||||
}
|
||||
public void ConvertDB()
|
||||
{
|
||||
try
|
||||
{
|
||||
database.Query("UPDATE Warps SET WorldID=@0", Main.worldID.ToString());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public void ConvertDB()
|
||||
{
|
||||
try
|
||||
{
|
||||
database.Query("UPDATE Warps SET WorldID=@0", Main.worldID.ToString());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
}
|
||||
public bool AddWarp(int x, int y, string name, string worldid)
|
||||
{
|
||||
try
|
||||
{
|
||||
database.Query("INSERT INTO Warps (X, Y, WarpName, WorldID) VALUES (@0, @1, @2, @3);", x, y, name, worldid);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool AddWarp(int x, int y, string name, string worldid)
|
||||
{
|
||||
try
|
||||
{
|
||||
database.Query("INSERT INTO Warps (X, Y, WarpName, WorldID) VALUES (@0, @1, @2, @3);", x, y, name, worldid);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public bool RemoveWarp(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
database.Query("DELETE FROM Warps WHERE WarpName=@0 AND WorldID=@1", name, Main.worldID.ToString());
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool RemoveWarp(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
database.Query("DELETE FROM Warps WHERE WarpName=@0 AND WorldID=@1", name, Main.worldID.ToString());
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public Warp FindWarp(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (
|
||||
var reader = database.QueryReader("SELECT * FROM Warps WHERE WarpName=@0 AND WorldID=@1", name,
|
||||
Main.worldID.ToString()))
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
try
|
||||
{
|
||||
return new Warp(new Vector2(reader.Get<int>("X"), reader.Get<int>("Y")), reader.Get<string>("WarpName"),
|
||||
reader.Get<string>("WorldID"), reader.Get<string>("Private"));
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new Warp(new Vector2(reader.Get<int>("X"), reader.Get<int>("Y")), reader.Get<string>("WarpName"),
|
||||
reader.Get<string>("WorldID"), "0");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
return new Warp();
|
||||
}
|
||||
|
||||
public Warp FindWarp(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var reader = database.QueryReader("SELECT * FROM Warps WHERE WarpName=@0 AND WorldID=@1", name, Main.worldID.ToString()))
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
try
|
||||
{
|
||||
return new Warp(new Vector2(reader.Get<int>("X"), reader.Get<int>("Y")), reader.Get<string>("WarpName"), reader.Get<string>("WorldID"), reader.Get<string>("Private"));
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new Warp(new Vector2(reader.Get<int>("X"), reader.Get<int>("Y")), reader.Get<string>("WarpName"), reader.Get<string>("WorldID"), "0");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
return new Warp();
|
||||
}
|
||||
/// <summary>
|
||||
/// Gets all the warps names from world
|
||||
/// </summary>
|
||||
/// <param name="worldid">World name to get warps from</param>
|
||||
/// <returns>List of warps with only their names</returns>
|
||||
public List<Warp> ListAllPublicWarps(string worldid)
|
||||
{
|
||||
var warps = new List<Warp>();
|
||||
try
|
||||
{
|
||||
using (var reader = database.QueryReader("SELECT * FROM Warps WHERE WorldID=@0", worldid))
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
try
|
||||
{
|
||||
if (reader.Get<String>("Private") == "0" || reader.Get<String>("Private") == null)
|
||||
warps.Add(new Warp {WarpName = reader.Get<string>("WarpName")});
|
||||
}
|
||||
catch
|
||||
{
|
||||
warps.Add(new Warp {WarpName = reader.Get<string>("WarpName")});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
return warps;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all the warps names from world
|
||||
/// </summary>
|
||||
/// <param name="worldid">World name to get warps from</param>
|
||||
/// <returns>List of warps with only their names</returns>
|
||||
public List<Warp> ListAllPublicWarps(string worldid)
|
||||
{
|
||||
var warps = new List<Warp>();
|
||||
try
|
||||
{
|
||||
using (var reader = database.QueryReader("SELECT * FROM Warps WHERE WorldID=@0", worldid))
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
try
|
||||
{
|
||||
if (reader.Get<String>("Private") == "0" || reader.Get<String>("Private") == null)
|
||||
warps.Add(new Warp { WarpName = reader.Get<string>("WarpName") });
|
||||
}
|
||||
catch
|
||||
{
|
||||
warps.Add(new Warp { WarpName = reader.Get<string>("WarpName") });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
}
|
||||
return warps;
|
||||
}
|
||||
/// <summary>
|
||||
/// Gets all the warps names from world
|
||||
/// </summary>
|
||||
/// <param name="worldid">World name to get warps from</param>
|
||||
/// <returns>List of warps with only their names</returns>
|
||||
public bool HideWarp(string warp, bool state)
|
||||
{
|
||||
try
|
||||
{
|
||||
string query = "UPDATE Warps SET Private=@0 WHERE WarpName=@1 AND WorldID=@2";
|
||||
|
||||
/// <summary>
|
||||
/// Gets all the warps names from world
|
||||
/// </summary>
|
||||
/// <param name="worldid">World name to get warps from</param>
|
||||
/// <returns>List of warps with only their names</returns>
|
||||
public bool HideWarp(string warp, bool state)
|
||||
{
|
||||
try
|
||||
{
|
||||
string query = "UPDATE Warps SET Private=@0 WHERE WarpName=@1 AND WorldID=@2";
|
||||
database.Query(query, state ? "1" : "0", warp, Main.worldID.ToString());
|
||||
|
||||
database.Query(query, state ? "1" : "0", warp, Main.worldID.ToString());
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex.ToString());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
public class Warp
|
||||
{
|
||||
public Vector2 WarpPos { get; set; }
|
||||
public string WarpName { get; set; }
|
||||
public string WorldWarpID { get; set; }
|
||||
public string Private { get; set; }
|
||||
|
||||
public class Warp
|
||||
{
|
||||
public Vector2 WarpPos { get; set; }
|
||||
public string WarpName { get; set; }
|
||||
public string WorldWarpID { get; set; }
|
||||
public string Private { get; set; }
|
||||
public Warp(Vector2 warppos, string name, string worldid, string hidden)
|
||||
{
|
||||
WarpPos = warppos;
|
||||
WarpName = name;
|
||||
WorldWarpID = worldid;
|
||||
Private = hidden;
|
||||
}
|
||||
|
||||
public Warp(Vector2 warppos, string name, string worldid, string hidden)
|
||||
{
|
||||
WarpPos = warppos;
|
||||
WarpName = name;
|
||||
WorldWarpID = worldid;
|
||||
Private = hidden;
|
||||
}
|
||||
|
||||
public Warp()
|
||||
{
|
||||
WarpPos = Vector2.Zero;
|
||||
WarpName = null;
|
||||
WorldWarpID = string.Empty;
|
||||
Private = "0";
|
||||
}
|
||||
}
|
||||
}
|
||||
public Warp()
|
||||
{
|
||||
WarpPos = Vector2.Zero;
|
||||
WarpName = null;
|
||||
WorldWarpID = string.Empty;
|
||||
Private = "0";
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue