Added ignoreVersion and loaded plugin list to StatTracking for issue #1444 (#1445)

This commit is contained in:
Jordan Coulam 2017-07-17 03:56:56 +01:00 committed by Lucas Nicodemus
parent ac8fe2a215
commit 1cdd33a78a

View file

@ -22,11 +22,13 @@ using System.Threading;
using System.Web; using System.Web;
using TerrariaApi.Server; using TerrariaApi.Server;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using TShockAPI.Extensions; using TShockAPI.Extensions;
namespace TShockAPI namespace TShockAPI
{ {
/// <summary> /// <summary>
@ -52,10 +54,11 @@ namespace TShockAPI
/// </summary> /// </summary>
public bool OptOut = false; public bool OptOut = false;
private PluginItem[] plugins;
private long totalMem = 0; private long totalMem = 0;
private string serverId = ""; private string serverId = "";
private HttpClient _client; private HttpClient _client;
private const string TrackerUrl = "http://stats.tshock.co/submit/{0}"; private const string TrackerUrl = "http://stats.tshock.co/submit/{0}";
/// <summary> /// <summary>
@ -103,11 +106,9 @@ namespace TShockAPI
private async Task SendUpdateAsync() private async Task SendUpdateAsync()
{ {
JsonData data = PrepareJsonData(); JsonData data = PrepareJsonData();
var serialized = Newtonsoft.Json.JsonConvert.SerializeObject(data); var serialized = Newtonsoft.Json.JsonConvert.SerializeObject(data);
var encoded = HttpUtility.UrlEncode(serialized); var encoded = HttpUtility.UrlEncode(serialized);
var uri = String.Format(TrackerUrl, encoded); var uri = string.Format(TrackerUrl, encoded);
try try
{ {
HttpResponseMessage msg = await _client.GetAsync(uri); HttpResponseMessage msg = await _client.GetAsync(uri);
@ -139,50 +140,82 @@ namespace TShockAPI
private JsonData PrepareJsonData() private JsonData PrepareJsonData()
{ {
if (ServerApi.RunningMono) return new JsonData()
{
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, port = Terraria.Netplay.ListenPort,
currentPlayers = TShock.Utils.ActivePlayers(), currentPlayers = TShock.Utils.ActivePlayers(),
maxPlayers = TShock.Config.MaxSlots, maxPlayers = TShock.Config.MaxSlots,
systemRam = totalMem, systemRam = GetTotalSystemRam(ServerApi.RunningMono),
version = TShock.VersionNum.ToString(), version = TShock.VersionNum.ToString(),
terrariaVersion = Terraria.Main.versionNumber2, terrariaVersion = Terraria.Main.versionNumber2,
providerId = ProviderToken, providerId = ProviderToken,
serverId = serverId, serverId = serverId,
mono = true mono = ServerApi.RunningMono,
ignorePluginVersion = ServerApi.IgnoreVersion,
loadedPlugins = GetLoadedPlugins()
}; };
} }
if (totalMem == 0) 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
{ {
//Again, only call this if it hasn't previously been cached
GetPhysicallyInstalledSystemMemory(out totalMem); GetPhysicallyInstalledSystemMemory(out totalMem);
totalMem = (totalMem / 1024 / 1024); // Super hardcore maths to convert to Gb from Kb totalMem = (totalMem / 1024 / 1024); // Super hardcore maths to convert to Gb from Kb
} }
return new JsonData return totalMem;
{
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 = false
};
} }
} }
/// <summary>
/// Holding information regarding loaded plugins
/// </summary>
public struct PluginItem
{
/// <summary>
/// Plugin name
/// </summary>
public string name;
/// <summary>
/// Assembly version
/// </summary>
public string version;
/// <summary>
/// Api version or UNKNOWN if attribute is missing, which is currently impossible
/// </summary>
public string apiVersion;
}
/// <summary> /// <summary>
/// Contains JSON-Serializable information about a server /// Contains JSON-Serializable information about a server
@ -210,6 +243,14 @@ namespace TShockAPI
/// </summary> /// </summary>
public string version; public string version;
/// <summary> /// <summary>
/// Whether or not server was started with --ignoreversion
/// </summary>
public bool ignorePluginVersion;
/// <summary>
/// List of loaded plugins and version name:
/// </summary>
public PluginItem[] loadedPlugins;
/// <summary>
/// The Terraria version supported by the server /// The Terraria version supported by the server
/// </summary> /// </summary>
public string terrariaVersion; public string terrariaVersion;