diff --git a/CHANGELOG.md b/CHANGELOG.md index 267c8ece..91b371a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Validated tile placement on PlaceObject; clients can no longer place frames, paintings etc with dirt blocks (@bartico6, @ProfessorXZ) ## TShock 4.3.24 +* API: Changed `PlayerHooks` permission hook mechanisms to allow negation from hooks (@deadsurgeon42) * Updated OpenTerraria API to 1.3.5.3 (@DeathCradle) * Updated Terraria Server API to 1.3.5.3 (@WhiteXZ, @hakusaro) * Updated TShock core components to 1.3.5.3 (@hakusaro) diff --git a/TShockAPI/DB/ItemManager.cs b/TShockAPI/DB/ItemManager.cs index 0ff89b40..c3d92840 100644 --- a/TShockAPI/DB/ItemManager.cs +++ b/TShockAPI/DB/ItemManager.cs @@ -201,8 +201,9 @@ namespace TShockAPI.DB if (ply.HasPermission(Permissions.usebanneditem)) return true; - if (PlayerHooks.OnPlayerItembanPermission(ply, this)) - return true; + PermissionHookResult hookResult = PlayerHooks.OnPlayerItembanPermission(ply, this); + if (hookResult != PermissionHookResult.Unhandled) + return hookResult == PermissionHookResult.Granted; var cur = ply.Group; var traversed = new List(); diff --git a/TShockAPI/DB/ProjectileManager.cs b/TShockAPI/DB/ProjectileManager.cs index 79bf9a37..4214fce6 100644 --- a/TShockAPI/DB/ProjectileManager.cs +++ b/TShockAPI/DB/ProjectileManager.cs @@ -206,8 +206,9 @@ namespace TShockAPI.DB if (ply.HasPermission(Permissions.canusebannedprojectiles)) return true; - if (PlayerHooks.OnPlayerProjbanPermission(ply, this)) - return true; + PermissionHookResult hookResult = PlayerHooks.OnPlayerProjbanPermission(ply, this); + if (hookResult != PermissionHookResult.Unhandled) + return hookResult == PermissionHookResult.Granted; var cur = ply.Group; var traversed = new List(); diff --git a/TShockAPI/DB/TileManager.cs b/TShockAPI/DB/TileManager.cs index 90a235f8..c291b21a 100644 --- a/TShockAPI/DB/TileManager.cs +++ b/TShockAPI/DB/TileManager.cs @@ -206,8 +206,9 @@ namespace TShockAPI.DB if (ply.HasPermission(Permissions.canusebannedtiles)) return true; - if (PlayerHooks.OnPlayerTilebanPermission(ply, this)) - return true; + PermissionHookResult hookResult = PlayerHooks.OnPlayerTilebanPermission(ply, this); + if (hookResult != PermissionHookResult.Unhandled) + return hookResult == PermissionHookResult.Granted; var cur = ply.Group; var traversed = new List(); diff --git a/TShockAPI/Hooks/PlayerHooks.cs b/TShockAPI/Hooks/PlayerHooks.cs index 062a78b8..91f9612c 100644 --- a/TShockAPI/Hooks/PlayerHooks.cs +++ b/TShockAPI/Hooks/PlayerHooks.cs @@ -143,7 +143,7 @@ namespace TShockAPI.Hooks /// /// EventArgs used for the event. /// - public class PlayerPermissionEventArgs : HandledEventArgs + public class PlayerPermissionEventArgs { /// /// The player who fired the event. @@ -155,6 +155,11 @@ namespace TShockAPI.Hooks /// public string Permission { get; set; } + /// + /// of the hook. + /// + public PermissionHookResult Result { get; set; } + /// /// Initializes a new instance of the PlayerPermissionEventArgs class. /// @@ -164,13 +169,14 @@ namespace TShockAPI.Hooks { Player = player; Permission = permission; + Result = PermissionHookResult.Unhandled; } } /// /// EventArgs used for the event. /// - public class PlayerItembanPermissionEventArgs : HandledEventArgs + public class PlayerItembanPermissionEventArgs { /// /// The player who fired the event. @@ -182,22 +188,28 @@ namespace TShockAPI.Hooks /// public ItemBan BannedItem { get; set; } + /// + /// of the hook. + /// + public PermissionHookResult Result { get; set; } + /// /// Initializes a new instance of the PlayerItembanPermissionEventArgs class. /// /// The player who fired the event. - /// The permission being checked. + /// The banned item being checked. public PlayerItembanPermissionEventArgs(TSPlayer player, ItemBan bannedItem) { Player = player; BannedItem = bannedItem; + Result = PermissionHookResult.Unhandled; } } /// /// EventArgs used for the event. /// - public class PlayerProjbanPermissionEventArgs : HandledEventArgs + public class PlayerProjbanPermissionEventArgs { /// /// The player who fired the event. @@ -209,6 +221,11 @@ namespace TShockAPI.Hooks /// public ProjectileBan BannedProjectile { get; set; } + /// + /// of the hook. + /// + public PermissionHookResult Result { get; set; } + /// /// Initializes a new instance of the PlayerProjbanPermissionEventArgs class. /// @@ -218,13 +235,14 @@ namespace TShockAPI.Hooks { Player = player; BannedProjectile = checkedProjectile; + Result = PermissionHookResult.Unhandled; } } /// /// EventArgs used for the event. /// - public class PlayerTilebanPermissionEventArgs : HandledEventArgs + public class PlayerTilebanPermissionEventArgs { /// /// The player who fired the event. @@ -236,6 +254,11 @@ namespace TShockAPI.Hooks /// public TileBan BannedTile { get; set; } + /// + /// of the hook. + /// + public PermissionHookResult Result { get; set; } + /// /// Initializes a new instance of the PlayerTilebanPermissionEventArgs class. /// @@ -245,6 +268,7 @@ namespace TShockAPI.Hooks { Player = player; BannedTile = checkedTile; + Result = PermissionHookResult.Unhandled; } } @@ -439,60 +463,79 @@ namespace TShockAPI.Hooks /// Fires the event. /// /// The player firing the event. - /// True if the event has been handled. - public static bool OnPlayerPermission(TSPlayer player, string permission) + /// Event result if the event has been handled, otherwise . + public static PermissionHookResult OnPlayerPermission(TSPlayer player, string permission) { if (PlayerPermission == null) - return false; + return PermissionHookResult.Unhandled; var args = new PlayerPermissionEventArgs(player, permission); PlayerPermission(args); - return args.Handled; + + return args.Result; } /// /// Fires the event. /// /// The player firing the event. - /// True if the event has been handled. - public static bool OnPlayerItembanPermission(TSPlayer player, ItemBan bannedItem) + /// Event result if the event has been handled, otherwise . + public static PermissionHookResult OnPlayerItembanPermission(TSPlayer player, ItemBan bannedItem) { if (PlayerItembanPermission == null) - return false; + return PermissionHookResult.Unhandled; var args = new PlayerItembanPermissionEventArgs(player, bannedItem); PlayerItembanPermission(args); - return args.Handled; + + return args.Result; } /// /// Fires the event. /// /// The player firing the event. - /// True if the event has been handled. - public static bool OnPlayerProjbanPermission(TSPlayer player, ProjectileBan bannedProj) + /// Event result if the event has been handled, otherwise . + public static PermissionHookResult OnPlayerProjbanPermission(TSPlayer player, ProjectileBan bannedProj) { if (PlayerProjbanPermission == null) - return false; + return PermissionHookResult.Unhandled; var args = new PlayerProjbanPermissionEventArgs(player, bannedProj); PlayerProjbanPermission(args); - return args.Handled; + + return args.Result; } /// /// Fires the event. /// /// The player firing the event. - /// True if the event has been handled. - public static bool OnPlayerTilebanPermission(TSPlayer player, TileBan bannedTile) + /// Event result if the event has been handled, otherwise . + public static PermissionHookResult OnPlayerTilebanPermission(TSPlayer player, TileBan bannedTile) { if (PlayerTilebanPermission == null) - return false; + return PermissionHookResult.Unhandled; var args = new PlayerTilebanPermissionEventArgs(player, bannedTile); PlayerTilebanPermission(args); - return args.Handled; + + return args.Result; } + } + + /// + /// Defines the possible outcomes of handlers. + /// + public enum PermissionHookResult + { + /// Hook doesn't return a result on the permission check. + Unhandled, + /// Permission is explicitly denied by a hook. + Denied, + /// Permission is explicitly granted by a hook. + Granted + } + } diff --git a/TShockAPI/StatTracker.cs b/TShockAPI/StatTracker.cs index 78aa10f5..d1c01c1c 100644 --- a/TShockAPI/StatTracker.cs +++ b/TShockAPI/StatTracker.cs @@ -22,11 +22,13 @@ using System.Threading; using System.Web; using TerrariaApi.Server; using System.Diagnostics; +using System.Linq; using System.Runtime.InteropServices; using System.Net.Http; using System.Threading.Tasks; using TShockAPI.Extensions; + namespace TShockAPI { /// @@ -52,10 +54,11 @@ namespace TShockAPI /// public bool OptOut = false; + private PluginItem[] plugins; + private long totalMem = 0; private string serverId = ""; private HttpClient _client; - private const string TrackerUrl = "http://stats.tshock.co/submit/{0}"; /// @@ -103,11 +106,9 @@ namespace TShockAPI private async Task SendUpdateAsync() { JsonData data = PrepareJsonData(); - var serialized = Newtonsoft.Json.JsonConvert.SerializeObject(data); var encoded = HttpUtility.UrlEncode(serialized); - var uri = String.Format(TrackerUrl, encoded); - + var uri = string.Format(TrackerUrl, encoded); try { HttpResponseMessage msg = await _client.GetAsync(uri); @@ -139,49 +140,81 @@ namespace TShockAPI private JsonData PrepareJsonData() { - if (ServerApi.RunningMono) - { - if (totalMem == 0) - { - //Only do this if we haven't already cached the total physicaly memory - var pc = new PerformanceCounter("Mono Memory", "Total Physical Memory"); - totalMem = (pc.RawValue / 1024 / 1024 / 1024); - } - - return new JsonData - { - port = Terraria.Netplay.ListenPort, - currentPlayers = TShock.Utils.ActivePlayers(), - maxPlayers = TShock.Config.MaxSlots, - systemRam = totalMem, - version = TShock.VersionNum.ToString(), - terrariaVersion = Terraria.Main.versionNumber2, - providerId = ProviderToken, - serverId = serverId, - mono = true - }; - } - - if (totalMem == 0) - { - //Again, only call this if it hasn't previously been cached - GetPhysicallyInstalledSystemMemory(out totalMem); - totalMem = (totalMem / 1024 / 1024); // Super hardcore maths to convert to Gb from Kb - } - - return new JsonData + return new JsonData() { port = Terraria.Netplay.ListenPort, currentPlayers = TShock.Utils.ActivePlayers(), maxPlayers = TShock.Config.MaxSlots, - systemRam = totalMem, + systemRam = GetTotalSystemRam(ServerApi.RunningMono), version = TShock.VersionNum.ToString(), terrariaVersion = Terraria.Main.versionNumber2, providerId = ProviderToken, serverId = serverId, - mono = false + mono = ServerApi.RunningMono, + ignorePluginVersion = ServerApi.IgnoreVersion, + loadedPlugins = GetLoadedPlugins() }; } + + private PluginItem[] GetLoadedPlugins() + { + if (plugins != null) + { + return plugins;//Return early + } + + plugins = new PluginItem[ServerApi.Plugins.Count];//Initialize with enough room to store the ammount of plugins loaded. + for (var i = 0; i < ServerApi.Plugins.Count; i++) + { + var pluginItem = new PluginItem(); + var apiAttribute = (ApiVersionAttribute)ServerApi.Plugins[i].Plugin.GetType().GetCustomAttributes(typeof(ApiVersionAttribute), false).FirstOrDefault(); + //The current implementation of loading plugins doesn't allow for a plugin to be loaded without an ApiVersion, the UNKNOWN is there incase a change is made to allow it + pluginItem.apiVersion = apiAttribute?.ApiVersion.ToString() ?? "UNKNOWN"; + pluginItem.name = ServerApi.Plugins[i].Plugin.Name; + pluginItem.version = ServerApi.Plugins[i].Plugin.Version.ToString(); + plugins[i] = pluginItem; + } + return plugins; + } + + private long GetTotalSystemRam(bool isMono) + { + if (totalMem != 0) + { + return totalMem;//Return early + } + + if (isMono)//Set totalMem so it can be returned later + { + var pc = new PerformanceCounter("Mono Memory", "Total Physical Memory"); + totalMem = (pc.RawValue / 1024 / 1024 / 1024); + } + else + { + GetPhysicallyInstalledSystemMemory(out totalMem); + totalMem = (totalMem / 1024 / 1024); // Super hardcore maths to convert to Gb from Kb + } + + return totalMem; + } + } + /// + /// Holding information regarding loaded plugins + /// + public struct PluginItem + { + /// + /// Plugin name + /// + public string name; + /// + /// Assembly version + /// + public string version; + /// + /// Api version or UNKNOWN if attribute is missing, which is currently impossible + /// + public string apiVersion; } /// @@ -210,6 +243,14 @@ namespace TShockAPI /// public string version; /// + /// Whether or not server was started with --ignoreversion + /// + public bool ignorePluginVersion; + /// + /// List of loaded plugins and version name: + /// + public PluginItem[] loadedPlugins; + /// /// The Terraria version supported by the server /// public string terrariaVersion; @@ -226,4 +267,4 @@ namespace TShockAPI /// public bool mono; } -} +} \ No newline at end of file diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index 8d0819bf..8f7b831e 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -1265,8 +1265,10 @@ namespace TShockAPI /// True if the player has that permission. public bool HasPermission(string permission) { - if (PlayerHooks.OnPlayerPermission(this, permission)) - return true; + PermissionHookResult hookResult = PlayerHooks.OnPlayerPermission(this, permission); + + if (hookResult != PermissionHookResult.Unhandled) + return hookResult == PermissionHookResult.Granted; if (tempGroup != null) return tempGroup.HasPermission(permission);