From 462d4d9ff5680326a3cc575535661f27c39e17cf Mon Sep 17 00:00:00 2001 From: Leonardo Santos Date: Thu, 29 Dec 2016 17:19:27 -0200 Subject: [PATCH] Update --- .gitignore | 1 + Properties/AssemblyInfo.cs | 4 +- src/LogCommandsModule.cs | 102 +++++++++++++++++++++---------------- 3 files changed, 60 insertions(+), 47 deletions(-) diff --git a/.gitignore b/.gitignore index cf148d4..5d3fa29 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ lib/*.dll bin/ obj/ .vs/ +.idea/ diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 743f75c..7419499 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -31,5 +31,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.2")] -[assembly: AssemblyFileVersion("1.0.0.2")] +[assembly: AssemblyVersion("1.0.0.3")] +[assembly: AssemblyFileVersion("1.0.0.3")] diff --git a/src/LogCommandsModule.cs b/src/LogCommandsModule.cs index 35d5593..d400b0a 100644 --- a/src/LogCommandsModule.cs +++ b/src/LogCommandsModule.cs @@ -21,13 +21,14 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; -using System.Linq; using System.Text; -using System.Threading; +using Essentials.Api.Configuration; using Essentials.Api.Event; using Essentials.Api.Events; using Essentials.Api.Module; +using Essentials.Api.Task; using Essentials.Common; namespace Essentials.Modules.LogCommands { @@ -38,22 +39,30 @@ namespace Essentials.Modules.LogCommands { Version = "$ASM_VERSION", Flags = LoadFlags.AUTO_REGISTER_EVENTS )] - public class LogCommands : EssModule { + public class LogCommands : EssModule { public static Dictionary> Cache { get; } = new Dictionary>(); - private int _checks; public string LogFolder { get { var path = Path.Combine(Folder, "logs"); - if (!Directory.Exists(path)) - Directory.CreateDirectory(path); + if (!Directory.Exists(path)) { + Directory.CreateDirectory(path); + } return path; } } public override void OnLoad() { Logger.LogInfo($"Enabled (v{this.Info.Version})!"); + + Task.Create() + .Id("LogCommands Save Cache") + .Async() + .Action(SaveCache) + .Interval(TimeSpan.FromSeconds(Configuration.SaveInterval)) + .UseIntervalAsDelay() + .Submit(); } public override void OnUnload() { @@ -65,14 +74,6 @@ public override void OnUnload() { private void OnCommandExecuted(CommandPosExecuteEvent e) { if (e.Source.IsConsole) return; - // TODO: Improve saving... - if (Cache.Count >= 15 || (_checks > 20 && CheckValues())) { - SaveCache(); - _checks = 0; - } else { - _checks++; - } - var playerId = ulong.Parse(e.Source.Id); var sb = new StringBuilder(); @@ -85,45 +86,56 @@ private void OnCommandExecuted(CommandPosExecuteEvent e) { .Append(e.Arguments.IsEmpty ? "\"" : $" {e.Arguments.Join(0)}\""); var text = sb.ToString(); - - if (Cache.ContainsKey(playerId)) - Cache[playerId].Add(text); - else - Cache.Add(playerId, new List { text }); - - #if DEBUG - SaveCache(); - #endif + if (Cache.ContainsKey(playerId)) { + Cache[playerId].Add(text); + } else { + Cache.Add(playerId, new List { text }); + } } private void SaveCache() { - var copyOfCache = new Dictionary>(Cache); - Cache.Clear(); - - new Thread(() => { - var text = new StringBuilder(); - copyOfCache.ForEach((k) => { - var playerFolder = Path.Combine(LogFolder, k.Key.ToString()); - var commandsFile = Path.Combine(playerFolder, "commands.txt"); + lock (Cache) { +#if DEBUG + var sw = Stopwatch.StartNew(); +#endif + var text = new StringBuilder(); + Cache.ForEach((k) => { + var playerFolder = Path.Combine(LogFolder, k.Key.ToString()); + var commandsFile = Path.Combine(playerFolder, "commands.txt"); + + if (!Directory.Exists(playerFolder)) + Directory.CreateDirectory(playerFolder); + + if (!File.Exists(commandsFile)) + File.Create(commandsFile).Close(); + + k.Value.ForEach(t => text.Append(t).Append(Environment.NewLine)); + File.AppendAllText(commandsFile, text.ToString()); + }); - if (!Directory.Exists(playerFolder)) - Directory.CreateDirectory(playerFolder); + Cache.Clear(); +#if DEBUG + sw.Stop(); + Logger.LogDebug($"Save cache took: {sw.ElapsedMilliseconds}ms"); +#endif + } + } - if (!File.Exists(commandsFile)) - File.Create(commandsFile).Close(); + } - k.Value.ForEach(t => text.Append(t).Append(Environment.NewLine)); - File.AppendAllText(commandsFile, text.ToString()); - }); - }).Start(); - } + public class LogCommandsConfig : JsonConfig { - /* - Sum all commands in cache, return true if > 100 - */ + /// + /// Interval to save cache. In seconds. + /// + public int SaveInterval { get; set; } - private static bool CheckValues() { - return Cache.Values.Sum(a => a.Count) > 100; + public override void LoadDefaults() { +#if DEBUG + SaveInterval = 10; +#else + SaveInterval = 60; // 2min +#endif } }