Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardosnt committed Dec 29, 2016
1 parent 3ef73c4 commit 462d4d9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ lib/*.dll
bin/
obj/
.vs/
.idea/
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
102 changes: 57 additions & 45 deletions src/LogCommandsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -38,22 +39,30 @@ namespace Essentials.Modules.LogCommands {
Version = "$ASM_VERSION",
Flags = LoadFlags.AUTO_REGISTER_EVENTS
)]
public class LogCommands : EssModule {
public class LogCommands : EssModule<LogCommandsConfig> {

public static Dictionary<ulong, List<string>> Cache { get; } = new Dictionary<ulong, List<string>>();
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() {
Expand All @@ -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();

Expand All @@ -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<string> { text });

#if DEBUG
SaveCache();
#endif
if (Cache.ContainsKey(playerId)) {
Cache[playerId].Add(text);
} else {
Cache.Add(playerId, new List<string> { text });
}
}

private void SaveCache() {
var copyOfCache = new Dictionary<ulong, List<String>>(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
*/
/// <summary>
/// Interval to save cache. In seconds.
/// </summary>
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
}

}
Expand Down

0 comments on commit 462d4d9

Please sign in to comment.