From e6ec63a90ee17ac374fba66a8ec3441e4ec8f421 Mon Sep 17 00:00:00 2001 From: quake1337 Date: Wed, 20 Sep 2017 09:27:28 +0200 Subject: [PATCH] Add @ijwu's changes + add /proc/meminfo trick for linux --- TShockAPI/StatTracker.cs | 38 +++++++++++++++++++++++++++++++++++--- TShockAPI/TShock.cs | 6 ++++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/TShockAPI/StatTracker.cs b/TShockAPI/StatTracker.cs index 61f42d14..1e4124b6 100644 --- a/TShockAPI/StatTracker.cs +++ b/TShockAPI/StatTracker.cs @@ -27,7 +27,8 @@ using System.Runtime.InteropServices; using System.Net.Http; using System.Threading.Tasks; using TShockAPI.Extensions; - +using System.IO; +using System.Text.RegularExpressions; namespace TShockAPI { @@ -176,7 +177,38 @@ namespace TShockAPI } return plugins; } - + public long GetFreeSystemRam(bool mono) + { + if (mono) + { + //Temporary in case mono won't work + if (File.Exists("/proc/meminfo")) + { + var l = File.ReadAllLines("/proc/meminfo"); + foreach (string s in l) + { + if (s.StartsWith("MemFree:")) + { + var m = Regex.Match(s, "MemFree:(\\s*)(\\d*) kB"); + if (m.Success) + { + long val; + if (long.TryParse(m.Groups[2].Value, out val)) + { + return val / 1024; + } + } + } + } + } + return -1; + } + else + { + var pc = new PerformanceCounter("Memory", "Available MBytes"); + return pc.RawValue; + } + } public long GetTotalSystemRam(bool isMono) { if (totalMem != 0) @@ -267,4 +299,4 @@ namespace TShockAPI /// public bool mono; } -} \ No newline at end of file +} diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 8a109a08..db15277b 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -329,10 +329,12 @@ namespace TShockAPI Log.ConsoleInfo("TShock {0} ({1}) now running.", Version, VersionCodename); - if (StatTracker.GetTotalSystemRam(ServerApi.RunningMono) < 2) + var systemRam = StatTracker.GetFreeSystemRam(ServerApi.RunningMono); + if (systemRam > -1 && systemRam < 2048) { - Log.ConsoleInfo("This machine has less than 2 gigabytes of RAM installed. Be advised that it might not be enough to run TShock."); + Log.ConsoleError("This machine has less than 2 gigabytes of RAM free. Be advised that it might not be enough to run TShock."); } + ServerApi.Hooks.GamePostInitialize.Register(this, OnPostInit); ServerApi.Hooks.GameUpdate.Register(this, OnUpdate); ServerApi.Hooks.GameHardmodeTileUpdate.Register(this, OnHardUpdate);