Simplify some stuff with perms

This commit is contained in:
UnknownShadow200 2018-05-30 11:11:57 +10:00
parent 75a19fe6c6
commit 009556797d
11 changed files with 80 additions and 118 deletions

View File

@ -27,17 +27,9 @@ namespace MCGalaxy.Blocks {
/// <summary> Represents which ranks are allowed (and which are disallowed) to use a block. </summary> /// <summary> Represents which ranks are allowed (and which are disallowed) to use a block. </summary>
public class BlockPerms { public class BlockPerms {
/// <summary> Extended block ID these permissions are for. </summary>
public BlockID ID; public BlockID ID;
/// <summary> Minimum rank normally able to use the block. </summary>
public LevelPermission MinRank; public LevelPermission MinRank;
/// <summary> Ranks specifically allowed to use the block </summary>
public List<LevelPermission> Allowed; public List<LevelPermission> Allowed;
/// <summary> Ranks specifically prevented from using the block. </summary>
public List<LevelPermission> Disallowed; public List<LevelPermission> Disallowed;
public BlockPerms(BlockID id, LevelPermission minRank, List<LevelPermission> allowed, public BlockPerms(BlockID id, LevelPermission minRank, List<LevelPermission> allowed,
@ -54,29 +46,19 @@ namespace MCGalaxy.Blocks {
Disallowed = disallowed; Disallowed = disallowed;
} }
/// <summary> Creates a copy of this instance. </summary>
public BlockPerms Copy() { public BlockPerms Copy() {
List<LevelPermission> allowed = new List<LevelPermission>(Allowed); List<LevelPermission> allowed = new List<LevelPermission>(Allowed);
List<LevelPermission> disallowed = new List<LevelPermission>(Disallowed); List<LevelPermission> disallowed = new List<LevelPermission>(Disallowed);
return new BlockPerms(ID, MinRank, allowed, disallowed); return new BlockPerms(ID, MinRank, allowed, disallowed);
} }
public bool UsableBy(LevelPermission perm) {
return (perm >= MinRank || Allowed.Contains(perm)) && !Disallowed.Contains(perm);
}
public static BlockPerms[] List = new BlockPerms[Block.ExtendedCount]; public static BlockPerms[] List = new BlockPerms[Block.ExtendedCount];
/// <summary> Returns whether the given rank can modify the given block. </summary>
public static bool UsableBy(Player p, BlockID block) {
BlockPerms b = List[block];
LevelPermission perm = p.Rank;
return (perm >= b.MinRank || b.Allowed.Contains(perm)) && !b.Disallowed.Contains(perm);
}
/// <summary> Returns whether the given rank can modify the given block. </summary>
public static bool UsableBy(LevelPermission perm, BlockID block) {
BlockPerms b = List[block];
return (perm >= b.MinRank || b.Allowed.Contains(perm)) && !b.Disallowed.Contains(perm);
}
public static void ResendAllBlockPermissions() { public static void ResendAllBlockPermissions() {
Player[] players = PlayerInfo.Online.Items; Player[] players = PlayerInfo.Online.Items;
foreach (Player pl in players) { pl.SendCurrentBlockPermissions(); } foreach (Player pl in players) { pl.SendCurrentBlockPermissions(); }
@ -94,18 +76,15 @@ namespace MCGalaxy.Blocks {
static readonly object saveLock = new object(); static readonly object saveLock = new object();
/// <summary> Saves the list of all block permissions. </summary>
public static void Save() { public static void Save() {
try { try {
lock (saveLock) lock (saveLock) SaveCore();
SaveCore(List);
} catch (Exception e) { } catch (Exception e) {
Logger.LogError(e); Logger.LogError(e);
} }
} }
static void SaveCore(IEnumerable<BlockPerms> list) { static void SaveCore() {
using (StreamWriter w = new StreamWriter(Paths.BlockPermsFile)) { using (StreamWriter w = new StreamWriter(Paths.BlockPermsFile)) {
w.WriteLine("#Version 2"); w.WriteLine("#Version 2");
w.WriteLine("# This file list the ranks that can use each block"); w.WriteLine("# This file list the ranks that can use each block");
@ -116,7 +95,7 @@ namespace MCGalaxy.Blocks {
w.WriteLine("# lava : 60 : 80,67 : 40,41,55"); w.WriteLine("# lava : 60 : 80,67 : 40,41,55");
w.WriteLine(""); w.WriteLine("");
foreach (BlockPerms perms in list) { foreach (BlockPerms perms in List) {
if (Block.Undefined(perms.ID)) continue; if (Block.Undefined(perms.ID)) continue;
string line = perms.ID + " : " + (int)perms.MinRank + " : " string line = perms.ID + " : " + (int)perms.MinRank + " : "
@ -127,7 +106,6 @@ namespace MCGalaxy.Blocks {
} }
/// <summary> Loads the list of all block permissions. </summary>
public static void Load() { public static void Load() {
SetDefaultPerms(); SetDefaultPerms();

View File

@ -61,9 +61,10 @@ namespace MCGalaxy {
public static List<Command> CopyAll() { return new List<Command>(allCmds); } public static List<Command> CopyAll() { return new List<Command>(allCmds); }
public static void InitAll() { public static void InitAll() {
Type[] types = Assembly.GetExecutingAssembly().GetTypes();
allCmds.Clear(); allCmds.Clear();
coreCmds.Clear(); coreCmds.Clear();
Type[] types = Assembly.GetExecutingAssembly().GetTypes(); foreach (Group grp in Group.GroupList) { grp.Commands.Clear(); }
for (int i = 0; i < types.Length; i++) { for (int i = 0; i < types.Length; i++) {
Type type = types[i]; Type type = types[i];
@ -80,10 +81,19 @@ namespace MCGalaxy {
public static void Register(Command cmd) { public static void Register(Command cmd) {
allCmds.Add(cmd); allCmds.Add(cmd);
CommandPerm[] perms = cmd.ExtraPerms; CommandPerms perms = CommandPerms.Find(cmd.name);
if (perms != null) { if (perms == null) {
for (int i = 0; i < perms.Length; i++) { perms = new CommandPerms(cmd.name, cmd.defaultRank, null, null);
CommandExtraPerms.Set(cmd.name, perms[i].Perm, perms[i].Description, i + 1); CommandPerms.List.Add(perms);
}
foreach (Group grp in Group.GroupList) {
if (perms.UsableBy(grp.Permission)) grp.Commands.Add(cmd);
}
CommandPerm[] extra = cmd.ExtraPerms;
if (extra != null) {
for (int i = 0; i < extra.Length; i++) {
CommandExtraPerms.Set(cmd.name, extra[i].Perm, extra[i].Description, i + 1);
} }
} }
@ -102,11 +112,12 @@ namespace MCGalaxy {
return null; return null;
} }
public static void Unregister(Command cmd) { public static bool Unregister(Command cmd) {
allCmds.Remove(cmd); bool removed = allCmds.Remove(cmd);
foreach (Group grp in Group.GroupList) { foreach (Group grp in Group.GroupList) {
grp.Commands.Remove(cmd); grp.Commands.Remove(cmd);
} }
return removed;
} }
public static void Search(ref string cmdName, ref string cmdArgs) { public static void Search(ref string cmdName, ref string cmdArgs) {
@ -118,6 +129,7 @@ namespace MCGalaxy {
if (!cmd.shortcut.CaselessEq(cmdName)) continue; if (!cmd.shortcut.CaselessEq(cmdName)) continue;
cmdName = cmd.name; return; cmdName = cmd.name; return;
} }
return;
} }
cmdName = alias.Target; cmdName = alias.Target;
@ -130,12 +142,12 @@ namespace MCGalaxy {
} }
} }
// Kept around for backwards compatibility // Kept around for backwards compatibility
public sealed class CommandList { public sealed class CommandList {
[Obsolete("Use Command.Register() instead")] [Obsolete("Use Command.Register() instead")]
public void Add(Command cmd) { Command.allCmds.Add(cmd); } public void Add(Command cmd) { Command.Register(cmd); }
[Obsolete("Use CommandUnregister() instead")] [Obsolete("Use CommandUnregister() instead")]
public bool Remove(Command cmd) { return Command.allCmds.Remove(cmd); } public bool Remove(Command cmd) { return Command.Unregister(cmd); }
[Obsolete("Use Command.Find() instead")] [Obsolete("Use Command.Find() instead")]
public Command FindByName(string name) { return Command.Find(name); } public Command FindByName(string name) { return Command.Find(name); }

View File

@ -186,7 +186,7 @@ namespace MCGalaxy.Commands {
/// <summary> Returns whether the player is allowed to place/modify/delete the given block. </summary> /// <summary> Returns whether the player is allowed to place/modify/delete the given block. </summary>
/// <remarks> Outputs information of which ranks can modify the block if not. </remarks> /// <remarks> Outputs information of which ranks can modify the block if not. </remarks>
public static bool IsBlockAllowed(Player p, string action, BlockID block) { public static bool IsBlockAllowed(Player p, string action, BlockID block) {
if (p == null || BlockPerms.UsableBy(p, block)) return true; if (p == null || p.group.Blocks[block]) return true;
BlockPerms.List[block].MessageCannotUse(p, action); BlockPerms.List[block].MessageCannotUse(p, action);
return false; return false;
} }

View File

@ -25,17 +25,9 @@ namespace MCGalaxy.Commands {
/// <summary> Represents which ranks are allowed (and which are disallowed) to use a command. </summary> /// <summary> Represents which ranks are allowed (and which are disallowed) to use a command. </summary>
public class CommandPerms { public class CommandPerms {
/// <summary> Name of the command these permissions are for. </summary>
public string CmdName; public string CmdName;
/// <summary> Minimum rank normally able to use the command. </summary>
public LevelPermission MinRank; public LevelPermission MinRank;
/// <summary> Ranks specifically allowed to use the command. </summary>
public List<LevelPermission> Allowed; public List<LevelPermission> Allowed;
/// <summary> Ranks specifically prevented from using the command. </summary>
public List<LevelPermission> Disallowed; public List<LevelPermission> Disallowed;
public CommandPerms(string cmd, LevelPermission minRank, List<LevelPermission> allowed, public CommandPerms(string cmd, LevelPermission minRank, List<LevelPermission> allowed,
@ -52,38 +44,32 @@ namespace MCGalaxy.Commands {
Disallowed = disallowed; Disallowed = disallowed;
} }
/// <summary> Creates a copy of this instance. </summary>
public CommandPerms Copy() { public CommandPerms Copy() {
List<LevelPermission> allowed = new List<LevelPermission>(Allowed); List<LevelPermission> allowed = new List<LevelPermission>(Allowed);
List<LevelPermission> disallowed = new List<LevelPermission>(Disallowed); List<LevelPermission> disallowed = new List<LevelPermission>(Disallowed);
return new CommandPerms(CmdName, MinRank, allowed, disallowed); return new CommandPerms(CmdName, MinRank, allowed, disallowed);
} }
static List<CommandPerms> list = new List<CommandPerms>(); public bool UsableBy(LevelPermission perm) {
return (perm >= MinRank || Allowed.Contains(perm)) && !Disallowed.Contains(perm);
}
public static List<CommandPerms> List = new List<CommandPerms>();
/// <summary> Finds the rank permissions for a given command. </summary>
/// <returns> null if rank permissions were not found for the given command. </returns>
public static CommandPerms Find(string cmd) { public static CommandPerms Find(string cmd) {
foreach (CommandPerms perms in list) { foreach (CommandPerms perms in List) {
if (perms.CmdName.CaselessEq(cmd)) return perms; if (perms.CmdName.CaselessEq(cmd)) return perms;
} }
return null; return null;
} }
/// <summary> Returns the lowest rank that can use the given command. </summary>
public static LevelPermission MinPerm(Command cmd) { public static LevelPermission MinPerm(Command cmd) {
CommandPerms perms = Find(cmd.name); CommandPerms perms = Find(cmd.name);
return perms == null ? cmd.defaultRank : perms.MinRank; return perms == null ? cmd.defaultRank : perms.MinRank;
} }
/// <summary> Retrieves a copy of list of all rank permissions for commands. </summary>
public static List<CommandPerms> CopyAll() {
return new List<CommandPerms>(list);
}
/// <summary> Sets the rank permissions for a given command. </summary>
public static void Set(string cmd, LevelPermission min, public static void Set(string cmd, LevelPermission min,
List<LevelPermission> allowed, List<LevelPermission> disallowed) { List<LevelPermission> allowed, List<LevelPermission> disallowed) {
if (min > LevelPermission.Nobody) return; if (min > LevelPermission.Nobody) return;
@ -92,20 +78,18 @@ namespace MCGalaxy.Commands {
CommandPerms perms = Find(cmd); CommandPerms perms = Find(cmd);
if (perms == null) { if (perms == null) {
perms = new CommandPerms(cmd, min, allowed, disallowed); perms = new CommandPerms(cmd, min, allowed, disallowed);
list.Add(perms); List.Add(perms);
} else { } else {
perms.Init(cmd, min, allowed, disallowed); perms.Init(cmd, min, allowed, disallowed);
} }
} }
/// <summary> Joins a list of rank permissions into a single string, comma separated.</summary>
public static string JoinPerms(List<LevelPermission> list) { public static string JoinPerms(List<LevelPermission> list) {
if (list == null || list.Count == 0) return ""; if (list == null || list.Count == 0) return "";
return list.Join(p => ((int)p).ToString(), ","); return list.Join(p => ((int)p).ToString(), ",");
} }
/// <summary> Expands a comma separated string into a list of rank permissions. </summary>
public static List<LevelPermission> ExpandPerms(string input) { public static List<LevelPermission> ExpandPerms(string input) {
List<LevelPermission> perms = new List<LevelPermission>(); List<LevelPermission> perms = new List<LevelPermission>();
if (input == null || input.Length == 0) return perms; if (input == null || input.Length == 0) return perms;
@ -116,20 +100,6 @@ namespace MCGalaxy.Commands {
return perms; return perms;
} }
/// <summary> Gets the list of all loaded commands that the given rank can use. </summary>
public static List<Command> AllCommandsUsableBy(LevelPermission perm) {
List<Command> commands = new List<Command>();
foreach (CommandPerms perms in list) {
bool canUse = perms.MinRank <= perm && !perms.Disallowed.Contains(perm);
if (canUse || perms.Allowed.Contains(perm)) {
Command cmd = Command.Find(perms.CmdName);
if (cmd != null) commands.Add(cmd);
}
}
return commands;
}
public void MessageCannotUse(Player p) { public void MessageCannotUse(Player p) {
StringBuilder builder = new StringBuilder("Only "); StringBuilder builder = new StringBuilder("Only ");
Formatter.PrintRanks(MinRank, Allowed, Disallowed, builder); Formatter.PrintRanks(MinRank, Allowed, Disallowed, builder);
@ -139,37 +109,33 @@ namespace MCGalaxy.Commands {
static readonly object saveLock = new object(); static readonly object saveLock = new object();
/// <summary> Saves the list of all command permissions. </summary>
public static void Save() { public static void Save() {
lock (saveLock) try {
SaveCore(list); lock (saveLock) SaveCore();
} catch (Exception e) {
Logger.Log(LogType.Warning, "SAVE FAILED! command.properties");
Logger.LogError(e);
}
} }
static void SaveCore(List<CommandPerms> givenList) { static void SaveCore() {
try { using (StreamWriter w = new StreamWriter(Paths.CmdPermsFile)) {
using (StreamWriter w = new StreamWriter(Paths.CmdPermsFile)) { w.WriteLine("#Version 2");
w.WriteLine("#Version 2"); w.WriteLine("# This file list the ranks that can use each command.");
w.WriteLine("# This file list the ranks that can use each command."); w.WriteLine("# Disallow and allow can be left empty.");
w.WriteLine("# Disallow and allow can be left empty."); w.WriteLine("# Works entirely on rank permission values, not rank names.");
w.WriteLine("# Works entirely on rank permission values, not rank names."); w.WriteLine("#");
w.WriteLine("#"); w.WriteLine("# Layout: CommandName : LowestRank : Disallow : Allow");
w.WriteLine("# Layout: CommandName : LowestRank : Disallow : Allow"); w.WriteLine("# gun : 60 : 80,67 : 40,41,55");
w.WriteLine("# gun : 60 : 80,67 : 40,41,55"); w.WriteLine("");
w.WriteLine("");
foreach (CommandPerms perms in givenList) { foreach (CommandPerms perms in List) {
w.WriteLine(perms.CmdName + " : " + (int)perms.MinRank + " : " + JoinPerms(perms.Disallowed) + " : " + JoinPerms(perms.Allowed)); w.WriteLine(perms.CmdName + " : " + (int)perms.MinRank + " : " + JoinPerms(perms.Disallowed) + " : " + JoinPerms(perms.Allowed));
}
} }
} catch (Exception ex) {
Logger.Log(LogType.Warning, "SAVE FAILED! command.properties");
Logger.LogError(ex);
} }
} }
/// <summary> Loads the list of all command permissions. </summary>
public static void Load() { public static void Load() {
foreach (Command cmd in Command.CopyAll()) { foreach (Command cmd in Command.CopyAll()) {
Set(cmd.name, cmd.defaultRank, null, null); Set(cmd.name, cmd.defaultRank, null, null);

View File

@ -78,7 +78,7 @@ namespace MCGalaxy.Commands.Info {
static List<BlockID> RankBlocks(LevelPermission perm) { static List<BlockID> RankBlocks(LevelPermission perm) {
List<BlockID> blocks = new List<BlockID>(Block.Count); List<BlockID> blocks = new List<BlockID>(Block.Count);
foreach (BlockPerms perms in BlockPerms.List) { foreach (BlockPerms perms in BlockPerms.List) {
if (!BlockPerms.UsableBy(perm, perms.ID)) continue; if (!perms.UsableBy(perm)) continue;
if (!Block.ExistsGlobal(perms.ID)) continue; if (!Block.ExistsGlobal(perms.ID)) continue;
blocks.Add(perms.ID); blocks.Add(perms.ID);
} }

View File

@ -38,7 +38,6 @@ namespace MCGalaxy.Commands.Scripting {
string error = IScripting.Load(path); string error = IScripting.Load(path);
if (error != null) { Player.Message(p, error); return; } if (error != null) { Player.Message(p, error); return; }
CommandPerms.Load();
Player.Message(p, "Command was successfully loaded."); Player.Message(p, "Command was successfully loaded.");
} }

View File

@ -40,7 +40,7 @@ namespace MCGalaxy.Network {
NetUtils.Write(motd, buffer, 66, p.hasCP437); NetUtils.Write(motd, buffer, 66, p.hasCP437);
} }
buffer[130] = BlockPerms.UsableBy(p, Block.Bedrock) ? (byte)100 : (byte)0; buffer[130] = p.group.Blocks[Block.Bedrock] ? (byte)100 : (byte)0;
return buffer; return buffer;
} }
@ -111,7 +111,7 @@ namespace MCGalaxy.Network {
public static byte[] UserType(Player p) { public static byte[] UserType(Player p) {
byte[] buffer = new byte[2]; byte[] buffer = new byte[2];
buffer[0] = Opcode.SetPermission; buffer[0] = Opcode.SetPermission;
buffer[1] = BlockPerms.UsableBy(p, Block.Bedrock) ? (byte)100 : (byte)0; buffer[1] = p.group.Blocks[Block.Bedrock] ? (byte)100 : (byte)0;
return buffer; return buffer;
} }

View File

@ -70,12 +70,19 @@ namespace MCGalaxy {
public void SetUsableCommands() { public void SetUsableCommands() {
Commands = CommandPerms.AllCommandsUsableBy(Permission); List<Command> commands = new List<Command>();
foreach (CommandPerms perms in CommandPerms.List) {
if (!perms.UsableBy(Permission)) continue;
Command cmd = Command.Find(perms.CmdName);
if (cmd != null) commands.Add(cmd);
}
Commands = commands;
} }
public void SetUsableBlocks() { public void SetUsableBlocks() {
for (int i = 0; i < Blocks.Length; i++) { foreach (BlockPerms perms in BlockPerms.List) {
Blocks[i] = BlockPerms.UsableBy(Permission, (BlockID)i); Blocks[perms.ID] = perms.UsableBy(Permission);
} }
} }

View File

@ -168,8 +168,8 @@ namespace MCGalaxy {
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
BlockID block = Block.FromRaw((BlockID)i); BlockID block = Block.FromRaw((BlockID)i);
bool place = BlockPerms.UsableBy(this, block) && level.CanPlace; bool place = group.Blocks[block] && level.CanPlace;
bool delete = BlockPerms.UsableBy(this, block) && level.CanDelete; bool delete = group.Blocks[block] && level.CanDelete;
// Placing air is the same as deleting existing block at that position in the world // Placing air is the same as deleting existing block at that position in the world
if (block == Block.Air) place &= delete; if (block == Block.Air) place &= delete;

View File

@ -119,7 +119,7 @@ namespace MCGalaxy {
} }
internal bool CheckManualChange(BlockID old, BlockID block, bool deleteMode) { internal bool CheckManualChange(BlockID old, BlockID block, bool deleteMode) {
if (!BlockPerms.UsableBy(this, old) && !level.BuildIn(old) && !Block.AllowBreak(old)) { if (!group.Blocks[old] && !level.BuildIn(old) && !Block.AllowBreak(old)) {
string action = deleteMode ? "delete" : "replace"; string action = deleteMode ? "delete" : "replace";
BlockPerms.List[old].MessageCannotUse(this, action); BlockPerms.List[old].MessageCannotUse(this, action);
return false; return false;

View File

@ -199,8 +199,8 @@ namespace MCGalaxy {
SrvProperties.Load(); SrvProperties.Load();
Group.InitAll(); Group.InitAll();
Command.InitAll();
CommandPerms.Load(); CommandPerms.Load();
Command.InitAll();
Block.SetBlocks(); Block.SetBlocks();
Awards.Load(); Awards.Load();
Economy.Load(); Economy.Load();