2121
2222using System ;
2323using System . Collections . Generic ;
24+ using System . Diagnostics ;
2425using System . IO ;
25- using System . Linq ;
2626using System . Text ;
27- using System . Threading ;
27+ using Essentials . Api . Configuration ;
2828using Essentials . Api . Event ;
2929using Essentials . Api . Events ;
3030using Essentials . Api . Module ;
31+ using Essentials . Api . Task ;
3132using Essentials . Common ;
3233
3334namespace Essentials . Modules . LogCommands {
@@ -38,22 +39,30 @@ namespace Essentials.Modules.LogCommands {
3839 Version = "$ASM_VERSION" ,
3940 Flags = LoadFlags . AUTO_REGISTER_EVENTS
4041 ) ]
41- public class LogCommands : EssModule {
42+ public class LogCommands : EssModule < LogCommandsConfig > {
4243
4344 public static Dictionary < ulong , List < string > > Cache { get ; } = new Dictionary < ulong , List < string > > ( ) ;
44- private int _checks ;
4545
4646 public string LogFolder {
4747 get {
4848 var path = Path . Combine ( Folder , "logs" ) ;
49- if ( ! Directory . Exists ( path ) )
50- Directory . CreateDirectory ( path ) ;
49+ if ( ! Directory . Exists ( path ) ) {
50+ Directory . CreateDirectory ( path ) ;
51+ }
5152 return path ;
5253 }
5354 }
5455
5556 public override void OnLoad ( ) {
5657 Logger . LogInfo ( $ "Enabled (v{ this . Info . Version } )!") ;
58+
59+ Task . Create ( )
60+ . Id ( "LogCommands Save Cache" )
61+ . Async ( )
62+ . Action ( SaveCache )
63+ . Interval ( TimeSpan . FromSeconds ( Configuration . SaveInterval ) )
64+ . UseIntervalAsDelay ( )
65+ . Submit ( ) ;
5766 }
5867
5968 public override void OnUnload ( ) {
@@ -65,14 +74,6 @@ public override void OnUnload() {
6574 private void OnCommandExecuted ( CommandPosExecuteEvent e ) {
6675 if ( e . Source . IsConsole ) return ;
6776
68- // TODO: Improve saving...
69- if ( Cache . Count >= 15 || ( _checks > 20 && CheckValues ( ) ) ) {
70- SaveCache ( ) ;
71- _checks = 0 ;
72- } else {
73- _checks ++ ;
74- }
75-
7677 var playerId = ulong . Parse ( e . Source . Id ) ;
7778 var sb = new StringBuilder ( ) ;
7879
@@ -85,45 +86,56 @@ private void OnCommandExecuted(CommandPosExecuteEvent e) {
8586 . Append ( e . Arguments . IsEmpty ? "\" " : $ " { e . Arguments . Join ( 0 ) } \" ") ;
8687
8788 var text = sb . ToString ( ) ;
88-
89- if ( Cache . ContainsKey ( playerId ) )
90- Cache [ playerId ] . Add ( text ) ;
91- else
92- Cache . Add ( playerId , new List < string > { text } ) ;
93-
94- #if DEBUG
95- SaveCache ( ) ;
96- #endif
89+ if ( Cache . ContainsKey ( playerId ) ) {
90+ Cache [ playerId ] . Add ( text ) ;
91+ } else {
92+ Cache . Add ( playerId , new List < string > { text } ) ;
93+ }
9794 }
9895
9996 private void SaveCache ( ) {
100- var copyOfCache = new Dictionary < ulong , List < String > > ( Cache ) ;
101- Cache . Clear ( ) ;
102-
103- new Thread ( ( ) => {
104- var text = new StringBuilder ( ) ;
105- copyOfCache . ForEach ( ( k ) => {
106- var playerFolder = Path . Combine ( LogFolder , k . Key . ToString ( ) ) ;
107- var commandsFile = Path . Combine ( playerFolder , "commands.txt" ) ;
97+ lock ( Cache ) {
98+ #if DEBUG
99+ var sw = Stopwatch . StartNew ( ) ;
100+ #endif
101+ var text = new StringBuilder ( ) ;
102+ Cache . ForEach ( ( k ) => {
103+ var playerFolder = Path . Combine ( LogFolder , k . Key . ToString ( ) ) ;
104+ var commandsFile = Path . Combine ( playerFolder , "commands.txt" ) ;
105+
106+ if ( ! Directory . Exists ( playerFolder ) )
107+ Directory . CreateDirectory ( playerFolder ) ;
108+
109+ if ( ! File . Exists ( commandsFile ) )
110+ File . Create ( commandsFile ) . Close ( ) ;
111+
112+ k . Value . ForEach ( t => text . Append ( t ) . Append ( Environment . NewLine ) ) ;
113+ File . AppendAllText ( commandsFile , text . ToString ( ) ) ;
114+ } ) ;
108115
109- if ( ! Directory . Exists ( playerFolder ) )
110- Directory . CreateDirectory ( playerFolder ) ;
116+ Cache . Clear ( ) ;
117+ #if DEBUG
118+ sw . Stop ( ) ;
119+ Logger . LogDebug ( $ "Save cache took: { sw . ElapsedMilliseconds } ms") ;
120+ #endif
121+ }
122+ }
111123
112- if ( ! File . Exists ( commandsFile ) )
113- File . Create ( commandsFile ) . Close ( ) ;
124+ }
114125
115- k . Value . ForEach ( t => text . Append ( t ) . Append ( Environment . NewLine ) ) ;
116- File . AppendAllText ( commandsFile , text . ToString ( ) ) ;
117- } ) ;
118- } ) . Start ( ) ;
119- }
126+ public class LogCommandsConfig : JsonConfig {
120127
121- /*
122- Sum all commands in cache, return true if > 100
123- */
128+ /// <summary>
129+ /// Interval to save cache. In seconds.
130+ /// </summary>
131+ public int SaveInterval { get ; set ; }
124132
125- private static bool CheckValues ( ) {
126- return Cache . Values . Sum ( a => a . Count ) > 100 ;
133+ public override void LoadDefaults ( ) {
134+ #if DEBUG
135+ SaveInterval = 10 ;
136+ #else
137+ SaveInterval = 60 ; // 2min
138+ #endif
127139 }
128140
129141 }
0 commit comments