Cleaned up the adding of default groups
This commit is contained in:
parent
1c2cead3c9
commit
85dac355be
1 changed files with 62 additions and 79 deletions
|
|
@ -28,101 +28,84 @@ namespace TShockAPI.DB
|
||||||
"CREATE TABLE IF NOT EXISTS GroupList (GroupName VARCHAR(255) PRIMARY, Commands VARCHAR(255), OrderBy VARCHAR(255));";
|
"CREATE TABLE IF NOT EXISTS GroupList (GroupName VARCHAR(255) PRIMARY, Commands VARCHAR(255), OrderBy VARCHAR(255));";
|
||||||
|
|
||||||
com.ExecuteNonQuery();
|
com.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
|
||||||
if (TShock.Config.StorageType.ToLower() == "sqlite")
|
//Add default groups
|
||||||
com.CommandText = "INSERT OR IGNORE INTO GroupList (GroupName, Commands, OrderBy) VALUES (@groupname, @commands, @order);";
|
AddGroup("trustedadmin", "admin,maintenance,cfg,butcher,item,heal,immunetoban,ignorecheatdetection,ignoregriefdetection,usebanneditem,manageusers");
|
||||||
else if (TShock.Config.StorageType.ToLower() == "mysql")
|
AddGroup("admin", "newadmin,ban,unban,whitelist,causeevents,spawnboss,spawnmob,managewarp,time,tp,pvpfun,kill,logs,immunetokick,tphere");
|
||||||
com.CommandText = "INSERT IGNORE INTO GroupList SET GroupName=@groupname, Commands=@commands, OrderBy=@order;";
|
AddGroup("newadmin", "default,kick,editspawn,reservedslot");
|
||||||
com.AddParameter("@groupname", "trustedadmin");
|
AddGroup("default", "canwater,canlava,warp,canbuild");
|
||||||
com.AddParameter("@commands", "admin,maintenance,cfg,butcher,item,heal,immunetoban,ignorecheatdetection,ignoregriefdetection,usebanneditem,manageusers");
|
AddGroup("vip", "default,canwater,canlava,warp,canbuild,reservedslot");
|
||||||
com.AddParameter("@order", "0");
|
|
||||||
com.ExecuteNonQuery();
|
|
||||||
com.Parameters.Clear();
|
|
||||||
|
|
||||||
if (TShock.Config.StorageType.ToLower() == "sqlite")
|
|
||||||
com.CommandText = "INSERT OR IGNORE INTO GroupList (GroupName, Commands, OrderBy) VALUES (@groupname, @commands, @order);";
|
|
||||||
else if (TShock.Config.StorageType.ToLower() == "mysql")
|
|
||||||
com.CommandText = "INSERT IGNORE INTO GroupList SET GroupName=@groupname, Commands=@commands, OrderBy=@order;";
|
|
||||||
com.AddParameter("@groupname", "admin");
|
|
||||||
com.AddParameter("@commands", "newadmin,ban,unban,whitelist,causeevents,spawnboss,spawnmob,managewarp,time,tp,pvpfun,kill,logs,immunetokick,tphere");
|
|
||||||
com.AddParameter("@order", "0");
|
|
||||||
com.ExecuteNonQuery();
|
|
||||||
com.Parameters.Clear();
|
|
||||||
|
|
||||||
if (TShock.Config.StorageType.ToLower() == "sqlite")
|
|
||||||
com.CommandText = "INSERT OR IGNORE INTO GroupList (GroupName, Commands, OrderBy) VALUES (@groupname, @commands, @order);";
|
|
||||||
else if (TShock.Config.StorageType.ToLower() == "mysql")
|
|
||||||
com.CommandText = "INSERT IGNORE INTO GroupList SET GroupName=@groupname, Commands=@commands, OrderBy=@order;";
|
|
||||||
com.AddParameter("@groupname", "newadmin");
|
|
||||||
com.AddParameter("@commands", "default,kick,editspawn,reservedslot");
|
|
||||||
com.AddParameter("@order", "0");
|
|
||||||
com.ExecuteNonQuery();
|
|
||||||
com.Parameters.Clear();
|
|
||||||
|
|
||||||
if (TShock.Config.StorageType.ToLower() == "sqlite")
|
|
||||||
com.CommandText = "INSERT OR IGNORE INTO GroupList (GroupName, Commands, OrderBy) VALUES (@groupname, @commands, @order);";
|
|
||||||
else if (TShock.Config.StorageType.ToLower() == "mysql")
|
|
||||||
com.CommandText = "INSERT IGNORE INTO GroupList SET GroupName=@groupname, Commands=@commands, OrderBy=@order;";
|
|
||||||
com.AddParameter("@groupname", "default");
|
|
||||||
com.AddParameter("@commands", "canwater,canlava,warp,canbuild");
|
|
||||||
com.AddParameter("@order", "0");
|
|
||||||
com.ExecuteNonQuery();
|
|
||||||
com.Parameters.Clear();
|
|
||||||
|
|
||||||
|
|
||||||
if (TShock.Config.StorageType.ToLower() == "sqlite")
|
String file = Path.Combine(TShock.SavePath, "groups.txt");
|
||||||
com.CommandText = "INSERT OR IGNORE INTO GroupList (GroupName, Commands, OrderBy) VALUES (@groupname, @commands, @order);";
|
if (File.Exists(file))
|
||||||
else if (TShock.Config.StorageType.ToLower() == "mysql")
|
{
|
||||||
com.CommandText = "INSERT IGNORE INTO GroupList SET GroupName=@groupname, Commands=@commands, OrderBy=@order;";
|
using (StreamReader sr = new StreamReader(file))
|
||||||
com.AddParameter("@groupname", "vip");
|
|
||||||
com.AddParameter("@commands", "default,canwater,canlava,warp,canbuild,reservedslot");
|
|
||||||
com.AddParameter("@order", "0");
|
|
||||||
com.ExecuteNonQuery();
|
|
||||||
com.Parameters.Clear();
|
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
String line;
|
if (!line.Equals("") && !line.Substring(0, 1).Equals("#"))
|
||||||
while ((line = sr.ReadLine()) != null)
|
|
||||||
{
|
{
|
||||||
if( !line.Equals("") && !line.Substring( 0,1 ).Equals( "#" ) )
|
String[] info = line.Split(' ');
|
||||||
|
String comms = "";
|
||||||
|
int size = info.Length;
|
||||||
|
int test = 0;
|
||||||
|
bool hasOrder = int.TryParse(info[info.Length - 1], out test);
|
||||||
|
if (hasOrder)
|
||||||
|
size = info.Length - 1;
|
||||||
|
for (int i = 1; i < size; i++)
|
||||||
|
{
|
||||||
|
if (!comms.Equals(""))
|
||||||
|
comms = comms + ",";
|
||||||
|
comms = comms + info[i].Trim();
|
||||||
|
}
|
||||||
|
using (var com = database.CreateCommand())
|
||||||
{
|
{
|
||||||
String[] info = line.Split(' ');
|
|
||||||
if (TShock.Config.StorageType.ToLower() == "sqlite")
|
if (TShock.Config.StorageType.ToLower() == "sqlite")
|
||||||
com.CommandText = "INSERT OR IGNORE INTO GroupList (GroupName, Commands, OrderBy) VALUES (@groupname, @commands, @order);";
|
com.CommandText = "INSERT OR IGNORE INTO GroupList (GroupName, Commands, OrderBy) VALUES (@groupname, @commands, @order);";
|
||||||
else if (TShock.Config.StorageType.ToLower() == "mysql")
|
else if (TShock.Config.StorageType.ToLower() == "mysql")
|
||||||
com.CommandText = "INSERT IGNORE INTO GroupList SET GroupName=@groupname, Commands=@commands, OrderBy=@order;";
|
com.CommandText = "INSERT IGNORE INTO GroupList SET GroupName=@groupname, Commands=@commands, OrderBy=@order;";
|
||||||
String comms = "";
|
|
||||||
int size = info.Length;
|
com.AddParameter("@groupname", info[0].Trim());
|
||||||
int test = 0;
|
|
||||||
bool hasOrder = int.TryParse(info[info.Length - 1], out test);
|
|
||||||
if( hasOrder )
|
|
||||||
size = info.Length - 1;
|
|
||||||
for (int i = 1; i < size; i++)
|
|
||||||
{
|
|
||||||
if (!comms.Equals(""))
|
|
||||||
comms = comms + ",";
|
|
||||||
comms = comms + info[i].Trim();
|
|
||||||
}
|
|
||||||
com.AddParameter("@groupname", info[0].Trim().ToString());
|
|
||||||
com.AddParameter("@commands", comms);
|
com.AddParameter("@commands", comms);
|
||||||
com.AddParameter("@order", hasOrder ? info[info.Length-1] : "0");
|
com.AddParameter("@order", hasOrder ? info[info.Length - 1] : "0");
|
||||||
com.ExecuteNonQuery();
|
com.ExecuteNonQuery();
|
||||||
com.Parameters.Clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
String path = Path.Combine(TShock.SavePath, "old_configs");
|
|
||||||
String file2 = Path.Combine(path, "groups.txt");
|
|
||||||
if (!Directory.Exists(path))
|
|
||||||
System.IO.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, "groups.txt");
|
||||||
|
if (!Directory.Exists(path))
|
||||||
|
System.IO.Directory.CreateDirectory(path);
|
||||||
|
if (File.Exists(file2))
|
||||||
|
File.Delete(file2);
|
||||||
|
File.Move(file, file2);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds group with name and permissions if it does not exist.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">name of group</param>
|
||||||
|
/// <param name="commands">permissions</param>
|
||||||
|
public void AddGroup(string name, string commands)
|
||||||
|
{
|
||||||
|
using (var com = database.CreateCommand())
|
||||||
|
{
|
||||||
|
if (TShock.Config.StorageType.ToLower() == "sqlite")
|
||||||
|
com.CommandText =
|
||||||
|
"INSERT OR IGNORE INTO GroupList (GroupName, Commands, OrderBy) VALUES (@groupname, @commands, @order);";
|
||||||
|
else if (TShock.Config.StorageType.ToLower() == "mysql")
|
||||||
|
com.CommandText =
|
||||||
|
"INSERT IGNORE INTO GroupList SET GroupName=@groupname, Commands=@commands, OrderBy=@order;";
|
||||||
|
com.AddParameter("@groupname", name);
|
||||||
|
com.AddParameter("@commands", commands);
|
||||||
|
com.AddParameter("@order", "0");
|
||||||
|
com.ExecuteNonQuery();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -166,7 +149,7 @@ namespace TShockAPI.DB
|
||||||
|
|
||||||
//Inherit Given commands
|
//Inherit Given commands
|
||||||
String[] commands = reader.Get<String>("Commands").Split(',');
|
String[] commands = reader.Get<String>("Commands").Split(',');
|
||||||
for( int i = 0; i < commands.Length; i++ )
|
for (int i = 0; i < commands.Length; i++)
|
||||||
{
|
{
|
||||||
group.AddPermission(commands[i].Trim());
|
group.AddPermission(commands[i].Trim());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue