|
| 1 | +/* |
| 2 | + * This file is part of uEssentials project. |
| 3 | + * https://uessentials.github.io/ |
| 4 | + * |
| 5 | + * Copyright (C) 2015-2016 Leonardosc |
| 6 | + * |
| 7 | + * This program is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License along |
| 18 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | +*/ |
| 21 | + |
| 22 | +using System; |
| 23 | +using System.Collections.Generic; |
| 24 | +using System.IO; |
| 25 | +using System.Linq; |
| 26 | +using System.Text; |
| 27 | +using System.Threading; |
| 28 | +using Essentials.Api.Event; |
| 29 | +using Essentials.Api.Events; |
| 30 | +using Essentials.Api.Module; |
| 31 | +using Essentials.Common; |
| 32 | + |
| 33 | +namespace Essentials.LogCommands |
| 34 | +{ |
| 35 | + [ModuleInfo( |
| 36 | + Name = "LogCommands", |
| 37 | + Author = "Leonardosc", |
| 38 | + Version = "1.0.0", |
| 39 | + Flags = ModuleFlags.AUTO_REGISTER_EVENTS |
| 40 | + )] |
| 41 | + public class TestModule : EssModule |
| 42 | + { |
| 43 | + public static Dictionary<ulong, List<string>> Cache { get; } = new Dictionary<ulong, List<string>>(); |
| 44 | + public static TestModule Instance { get; private set; } |
| 45 | + private int _checks; |
| 46 | + private char DirSep = Path.DirectorySeparatorChar; |
| 47 | + public string LogFolder |
| 48 | + { |
| 49 | + get |
| 50 | + { |
| 51 | + var path = $"{Folder}{DirSep}logs{DirSep}"; |
| 52 | + if ( !Directory.Exists(path) ) |
| 53 | + { |
| 54 | + Directory.CreateDirectory( path ); |
| 55 | + } |
| 56 | + return path; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public override void OnLoad() |
| 61 | + { |
| 62 | + Instance = this; |
| 63 | + } |
| 64 | + |
| 65 | + public override void OnUnload() |
| 66 | + { |
| 67 | + } |
| 68 | + |
| 69 | + [SubscribeEvent(EventType.ESSENTIALS_COMMAND_POS_EXECUTED)] |
| 70 | + private void OnCommandExecuted( CommandPosExecuteEvent e ) |
| 71 | + { |
| 72 | + if ( e.Source.IsConsole ) return; |
| 73 | + if ( Cache.Count >= 15 || (_checks > 20 && CheckValues()) ) |
| 74 | + { |
| 75 | + SaveCache(); |
| 76 | + _checks = 0; |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + _checks++; |
| 81 | + } |
| 82 | + var playerId = ulong.Parse(e.Source.Id); |
| 83 | + var sb = new StringBuilder(); |
| 84 | + |
| 85 | + sb.Append( $"[{DateTime.Now}] " ) |
| 86 | + .Append( $"[{e.Result.Type}" ) |
| 87 | + .Append( string.IsNullOrEmpty(e.Result.Message) ? "] " : $"({e.Result.Message})] " ) |
| 88 | + .Append( e.Source.DisplayName ) |
| 89 | + .Append( ": " ) |
| 90 | + .Append( $"\"/{e.Command.Name}" ) |
| 91 | + .Append( e.Arguments.IsEmpty ? "\"" : $" {e.Arguments.Join(0)}\"" ); |
| 92 | + var text = sb.ToString(); |
| 93 | + if ( Cache.ContainsKey( playerId ) ) |
| 94 | + { |
| 95 | + Cache[playerId].Add( text ); |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + Cache.Add( playerId, new List<string>{ text } ); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private void SaveCache() |
| 104 | + { |
| 105 | + var contents = new Dictionary<ulong, List<String>>(Cache); |
| 106 | + Cache.Clear(); |
| 107 | + |
| 108 | + new Thread(() => { |
| 109 | + var text = new StringBuilder(); |
| 110 | + contents.ForEach((k) => { |
| 111 | + var playerFolder = $"{LogFolder}{k.Key}{DirSep}"; |
| 112 | + var commandsFile = $"{playerFolder}commands.txt"; |
| 113 | + |
| 114 | + if ( !Directory.Exists( playerFolder ) ) |
| 115 | + { |
| 116 | + Directory.CreateDirectory( playerFolder ); |
| 117 | + } |
| 118 | + if ( !File.Exists( commandsFile ) ) |
| 119 | + { |
| 120 | + File.Create( commandsFile ).Close(); |
| 121 | + } |
| 122 | + |
| 123 | + k.Value.ForEach(t => text.Append( t ).Append( Environment.NewLine ) ); |
| 124 | + File.AppendAllText( commandsFile, text.ToString() ); |
| 125 | + }); |
| 126 | + }).Start(); |
| 127 | + } |
| 128 | + |
| 129 | + /* |
| 130 | + Sum all commands in cache, return true if > 100 |
| 131 | + */ |
| 132 | + private bool CheckValues() |
| 133 | + { |
| 134 | + return Cache.Values.Sum(a => a.Count) > 100; |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments