Fixed internal object and DB inconsitencies for modified default groups
Fixed console errors for failed parent dependencies of default groups that already exist caused by fb11729547 consistency fixes
Made GroupManager.groups readonly to discourage overwriting it. Could still do with better encapsulation to prevent consistency problems if modified externally.
Added UpdateGroup method to GroupManager
Refactored Group handling of permissions and chatcolor to prevent inconsistent data
Notably:-
* AddPermission now does the right thing when passed the string "!permission"
* Converted ChatColor from method to getter / setter
Added RemovePermission to Group
Refactored GroupManager Permissions handling to ensure consistent data between DB and internal objects
Removed redundent AddGroup method in preference to a default value on the main method
Correct some tabs vs spaces
Added ParentName helper to Group
Fixed inverted parameters to error "Invalid parent {0} for group {1}"
This commit is contained in:
parent
0108795f27
commit
098363a9cc
3 changed files with 162 additions and 99 deletions
|
|
@ -30,23 +30,57 @@ namespace TShockAPI
|
|||
public int Order { get; set; }
|
||||
public string Prefix { get; set; }
|
||||
public string Suffix { get; set; }
|
||||
public string ParentName { get { return (null == Parent) ? "" : Parent.Name; } }
|
||||
public string ChatColor
|
||||
{
|
||||
get { return string.Format("{0}{1}{2}", R.ToString("X2"), G.ToString("X2"), B.ToString("X2")); }
|
||||
set
|
||||
{
|
||||
if (null != value)
|
||||
{
|
||||
string[] parts = value.Split(',');
|
||||
if (3 == parts.Length)
|
||||
{
|
||||
byte r, g, b;
|
||||
if (byte.TryParse(parts[0], out r) && byte.TryParse(parts[1], out g) && byte.TryParse(parts[2], out b))
|
||||
{
|
||||
R = r;
|
||||
G = g;
|
||||
B = b;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string Permissions
|
||||
{
|
||||
get
|
||||
{
|
||||
List<string> all = new List<string>(permissions);
|
||||
permissions.ForEach(p => all.Add("!" + p));
|
||||
return string.Join(",", all);
|
||||
}
|
||||
set
|
||||
{
|
||||
permissions.Clear();
|
||||
negatedpermissions.Clear();
|
||||
if (null != value)
|
||||
value.Split(',').ForEach(p => AddPermission(p.Trim()));
|
||||
}
|
||||
}
|
||||
|
||||
public byte R = 255;
|
||||
public byte G = 255;
|
||||
public byte B = 255;
|
||||
|
||||
public Group(string groupname, Group parentgroup = null, string chatcolor = "255,255,255")
|
||||
public Group(string groupname, Group parentgroup = null, string chatcolor = "255,255,255", string permissions = null)
|
||||
{
|
||||
Name = groupname;
|
||||
Parent = parentgroup;
|
||||
byte.TryParse(chatcolor.Split(',')[0], out R);
|
||||
byte.TryParse(chatcolor.Split(',')[1], out G);
|
||||
byte.TryParse(chatcolor.Split(',')[2], out B);
|
||||
}
|
||||
|
||||
public string ChatColor()
|
||||
{
|
||||
return string.Format("{0}{1}{2}", R.ToString("X2"), G.ToString("X2"), B.ToString("X2"));
|
||||
ChatColor = chatcolor;
|
||||
Permissions = permissions;
|
||||
}
|
||||
|
||||
public virtual bool HasPermission(string permission)
|
||||
|
|
@ -73,21 +107,44 @@ namespace TShockAPI
|
|||
|
||||
public void NegatePermission(string permission)
|
||||
{
|
||||
negatedpermissions.Add(permission);
|
||||
// Avoid duplicates
|
||||
if (!negatedpermissions.Contains(permission))
|
||||
{
|
||||
negatedpermissions.Add(permission);
|
||||
permissions.Remove(permission); // Ensure we don't have conflicting definitions for a permissions
|
||||
}
|
||||
}
|
||||
|
||||
public void AddPermission(string permission)
|
||||
{
|
||||
permissions.Add(permission);
|
||||
if (permission.StartsWith("!"))
|
||||
{
|
||||
NegatePermission(permission.Substring(1));
|
||||
return;
|
||||
}
|
||||
// Avoid duplicates
|
||||
if (!permissions.Contains(permission))
|
||||
{
|
||||
permissions.Add(permission);
|
||||
negatedpermissions.Remove(permission); // Ensure we don't have conflicting definitions for a permissions
|
||||
}
|
||||
}
|
||||
|
||||
public void SetPermission(List<string> permission)
|
||||
{
|
||||
permissions.Clear();
|
||||
foreach (string s in permission)
|
||||
negatedpermissions.Clear();
|
||||
permission.ForEach(p => AddPermission(p));
|
||||
}
|
||||
|
||||
public void RemovePermission(string permission)
|
||||
{
|
||||
if (permission.StartsWith("!"))
|
||||
{
|
||||
permissions.Add(s);
|
||||
negatedpermissions.Remove(permission.Substring(1));
|
||||
return;
|
||||
}
|
||||
permissions.Remove(permission);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue