Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update stuff
  • Loading branch information
Bishbash777 committed Nov 1, 2020
commit f52ddd0262ad9d765e3eba081b31309bd63e913b
63 changes: 54 additions & 9 deletions Essentials/Commands/RanksModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,57 @@
using System.IO;

namespace Essentials.Commands {
[Category("rank")]
[Category("pr")]
public class RanksModule : CommandModule {
public static readonly Logger Log = LogManager.GetCurrentClassLogger();
public PlayerAccountModule AccModule = new PlayerAccountModule();
public RanksAndPermissionsModule RanksAndPermissions = new RanksAndPermissionsModule();

[Command("permission add")]

[Command("createrank")]
[Permission(MyPromoteLevel.Admin)]
public void CreateRank(string name) {
if (!RanksAndPermissions.GenerateRank(name)) {
Context.Respond("Rank already exists!");
return;
}
Context.Respond("Rank Created!");
}

[Command("delrank")]
[Permission(MyPromoteLevel.Admin)]
public void DeleteRank(string name) {
RanksAndPermissionsModule.RankData rank = RanksAndPermissions.GetRankData(name);
if (rank == null) {
Context.Respond($"Rank '{name}' does not exist!");
return;
}
RanksAndPermissionsModule.Ranks.Remove(rank);
RanksAndPermissions.SaveRankData();

}

[Command("renamerank")]
[Permission(MyPromoteLevel.Admin)]
private void AddPermission(string rankName, string command) {
public void RenameRank(string oldName, string newName) {
RanksAndPermissionsModule.RankData rank = RanksAndPermissions.GetRankData(oldName);
if (rank == null) {
Context.Respond($"Rank '{oldName}' does not exist!");
return;
}
rank.RankName = newName;
RanksAndPermissions.UpdateRankObject(rank);
}

[Command("setdefaultrank")]
[Permission(MyPromoteLevel.Admin)]
public void SetDefaultRank(string name) {

}

[Command("addperm")]
[Permission(MyPromoteLevel.Admin)]
public void AddPermission(string rankName, string command) {
RanksAndPermissionsModule.RankData data = new RanksAndPermissionsModule.RankData();
bool found = false;
foreach (var Rank in RanksAndPermissionsModule.Ranks) {
Expand All @@ -48,22 +90,24 @@ private void AddPermission(string rankName, string command) {
if (data.Allowed.Contains(stringAfterChar)) {
data.Allowed.Remove(stringAfterChar);
Context.Respond($"Permission to use command '{command}' has been removed from the {data.RankName} rank!");
RanksAndPermissions.UpdateRankObject(data);
return;
}
}

if (!data.Allowed.Contains(command)) {
data.Allowed.Add(command);
Context.Respond($"Permission to use command '{command}' has been added to the {data.RankName} rank!");
RanksAndPermissions.UpdateRankObject(data);
return;
}

Context.Respond($"The rank '{data.RankName}' already has permission to use '{command}'");
}

[Command("permission remove")]
[Command("delperm")]
[Permission(MyPromoteLevel.Admin)]
private void RemovePermission(string rankName, string command) {
public void RemovePermission(string rankName, string command) {
RanksAndPermissionsModule.RankData data = new RanksAndPermissionsModule.RankData();
bool found = false;
foreach (var Rank in RanksAndPermissionsModule.Ranks) {
Expand All @@ -83,19 +127,20 @@ private void RemovePermission(string rankName, string command) {
string stringAfterChar = command.Substring(command.IndexOf("-") + 1);
if (data.Disallowed.Contains(stringAfterChar)) {
data.Disallowed.Remove(stringAfterChar);
Context.Respond($"Permission to use command '{command}' has been removed from the {data.RankName} rank!");
Context.Respond($"Updated rank");
RanksAndPermissions.UpdateRankObject(data);
return;
}
}

if (!data.Disallowed.Contains(command)) {
data.Disallowed.Add(command);
Context.Respond($"Permission to use command '{command}' has been actively revolked from the {data.RankName} rank!");
Context.Respond($"Permission to use command '{command}' has been actively revoked from the {data.RankName} rank!");
RanksAndPermissions.UpdateRankObject(data);
return;
}

Context.Respond($"Permission to use command '{command}' is already being actively revolked from the {data.RankName} rank!");

Context.Respond($"Permission to use command '{command}' is already being actively revoked from the {data.RankName} rank!");
}
}
}
1 change: 0 additions & 1 deletion Essentials/Essentials.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@
<Compile Include="GridFinder.cs" />
<Compile Include="InfoCommand.cs" />
<Compile Include="Commands\InfoModule.cs" />
<Compile Include="Patches\CommandPermissionPatch.cs" />
<Compile Include="Patches\SessionDownloadPatch.cs" />
<Compile Include="PlayerAccountModule.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
12 changes: 6 additions & 6 deletions Essentials/EssentialsPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class EssentialsPlugin : TorchPluginBase, IWpfPlugin

public static EssentialsPlugin Instance { get; private set; }
public PlayerAccountModule AccModule = new PlayerAccountModule();

RanksAndPermissionsModule RanksAndPermissions = new RanksAndPermissionsModule();

/// <inheritdoc />
public UserControl GetControl() => _control ?? (_control = new PropertyGrid(){DataContext=Config/*, IsEnabled = false*/});
Expand All @@ -73,15 +73,13 @@ public override void Init(ITorchBase torch)
_sessionManager.SessionStateChanged += SessionChanged;
else
Log.Warn("No session manager. MOTD won't work");
Log.Info("Attempting reading of player data");
homeDataPath = Path.Combine(StoragePath, "players.json");
if (!File.Exists(homeDataPath)) {
File.Create(homeDataPath);
}



Log.Info("Attempting reading of rank data");
rankDataPath = Path.Combine(StoragePath, "ranks.json");
if (!File.Exists(rankDataPath)) {
File.Create(rankDataPath);
Expand All @@ -98,6 +96,7 @@ public override void Init(ITorchBase torch)
private void SessionChanged(ITorchSession session, TorchSessionState state)
{
var mpMan = Torch.CurrentSession.Managers.GetManager<IMultiplayerManagerServer>();
var cmdMan = Torch.CurrentSession.Managers.GetManager<CommandManager>();
switch (state)
{
case TorchSessionState.Loading:
Expand All @@ -108,17 +107,18 @@ private void SessionChanged(ITorchSession session, TorchSessionState state)

string rankdata = File.ReadAllText(rankDataPath);
if (!string.IsNullOrEmpty(rankdata)) {
PlayerAccountModule.PlayersAccounts = JsonConvert.DeserializeObject<List<PlayerAccountModule.PlayerAccountData>>(File.ReadAllText(rankDataPath));
RanksAndPermissionsModule.Ranks = JsonConvert.DeserializeObject<List<RanksAndPermissionsModule.RankData>>(File.ReadAllText(rankDataPath));
}

RanksAndPermissionsModule RAP = new RanksAndPermissionsModule();
RAP.GenerateRank("Default");
RanksAndPermissions.GenerateRank("Default");
break;

case TorchSessionState.Loaded:
mpMan.PlayerJoined += AccModule.GenerateAccount;
mpMan.PlayerJoined += MotdOnce;
mpMan.PlayerJoined += RanksAndPermissions.RegisterInheritedRanks;
mpMan.PlayerLeft += ResetMotdOnce;
cmdMan.OnCommandExecuting +=RanksAndPermissions.HasCommandPermission;
MyEntities.OnEntityAdd += EntityAdded;
if(Config.StopShipsOnStart)
StopShips();
Expand Down
28 changes: 22 additions & 6 deletions Essentials/Patches/CommandPermissionPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,54 @@
using NLog;
using Torch.Mod;
using Torch.Mod.Messages;
using Torch.API.Managers;
using static Torch.Commands.CommandTree;

namespace Essentials.Patches {
[PatchShim]
public static class CommandPermissionPatch {
private static readonly Dictionary<string, CommandNode> _root = new Dictionary<string, CommandNode>();
public static PlayerAccountModule PlayerAccountData = new PlayerAccountModule();
public static RanksAndPermissionsModule RanksAndPermissions = new RanksAndPermissionsModule();

public static readonly Logger Log = LogManager.GetCurrentClassLogger();

internal static readonly MethodInfo update =
typeof(CommandManager).GetMethod(nameof(CommandManager.HasPermission), BindingFlags.Instance | BindingFlags.Public) ??
throw new Exception("Failed to find HasPermission method");
public static readonly MethodInfo[] methods = typeof(CommandManager).GetMethods();

public static MethodInfo FindOverLoadMethod(string name, int parameterLenth) {
MethodInfo method = null;
foreach (var DecalredMethod in methods) {
if (DecalredMethod.GetParameters().Length == parameterLenth && DecalredMethod.Name == name) {
method = DecalredMethod;
break;
}
}
return method;
}

internal static readonly MethodInfo update = FindOverLoadMethod("HasPermission", 2);

internal static readonly MethodInfo updatePatch =
typeof(CommandPermissionPatch).GetMethod(nameof(CheckPermission), BindingFlags.Static | BindingFlags.Public) ??
throw new Exception("Failed to find CheckPermission patch method");

public static void Patch(PatchContext ctx) {

ctx.GetPattern(update).Prefixes.Add(updatePatch);
Log.Info("Patched CommandManager.HasPermission()");
Log.Info("Patched CommandManager.HandleCommand()");
}

public static bool CheckPermission(ulong steamId, Command command) {
public static bool CheckPermission(ulong steamId, Command command, ref bool __result) {
string cmd = "";
foreach (var part in command.Path) {
cmd += part + " ";
}
cmd = cmd.TrimEnd();
Log.Fatal($"Checking {cmd}");
string playersRank = PlayerAccountData.GetRank(steamId);
if (!RanksAndPermissions.RankHasPermission(playersRank, cmd)) {
Log.Info($"{steamId} tried to use the blocked command '{cmd}'");
ModCommunication.SendMessageTo(new NotificationMessage($"You do not have permission to use that command!", 10000, "Red"), steamId);
__result = false;
return false;
}
return true;
Expand Down
4 changes: 4 additions & 0 deletions Essentials/PlayerAccountModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Torch.Commands;
using Torch.Mod;
using Torch.Mod.Messages;
using VRage.Game.ModAPI;
using VRage.Groups;
using VRageMath;

namespace Essentials {
public class PlayerAccountModule {
public static List<PlayerAccountData> PlayersAccounts = new List<PlayerAccountData>();
public RanksAndPermissionsModule RanksAndPermissions = new RanksAndPermissionsModule();
public static readonly Logger Log = LogManager.GetCurrentClassLogger();

public class PlayerAccountData {
Expand Down
Loading