Skip to content

Commit f012dad

Browse files
committed
Initial commit.
1 parent b3fc87e commit f012dad

20 files changed

+607
-0
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[*.cs]
2+
indent_style = space
3+
dotnet_sort_system_directives_first = true
4+
csharp_space_between_method_declaration_parameter_list_parentheses = false
5+
dotnet_style_require_accessibility_modifiers = for_non_interface_members:suggestion
6+
csharp_preferred_modifier_order = public,protected,static,override:suggestion
7+
csharp_style_namespace_declarations = file_scoped
8+
9+
dotnet_diagnostic.CS1591.severity = suggestion
10+
dotnet_diagnostic.IDE0005.severity = none
11+
dotnet_diagnostic.IDE0161.severity = suggestion

.run/Attach to the Mono.run.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="Attach to the Mono" type="ConnectRemote" factoryName="Mono Remote" show_console_on_std_err="false" show_console_on_std_out="false" port="55555" address="localhost">
3+
<option name="allowRunningInParallel" value="false" />
4+
<option name="listenPortForConnections" value="false" />
5+
<option name="projectPathOnTarget" />
6+
<option name="selectedOptions">
7+
<list />
8+
</option>
9+
<method v="2" />
10+
</configuration>
11+
</component>

API/ICommandInfo.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace SCommandLogger.API;
2+
3+
public interface ICommandInfo
4+
{
5+
string Name { get; }
6+
string[] Arguments { get; }
7+
}

API/ICommandLogger.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace SCommandLogger.API;
2+
3+
public interface ICommandLogger : IDisposable
4+
{
5+
ICommandLoggerEntry Produce(IRocketPlayer caller, IRocketCommand command, string format, string[] args);
6+
ICommandLoggerEntry Insert(ICommandLoggerEntry entry);
7+
void Remove(ICommandLoggerEntry entry);
8+
void Clear();
9+
}

API/ICommandLoggerEntry.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace SCommandLogger.API;
2+
3+
public interface ICommandLoggerEntry
4+
{
5+
ICommandInfo Command { get; set; }
6+
CSteamID Executor { get; set; }
7+
DateTime Time { get; set; }
8+
}

Config.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace SCommandLogger;
2+
3+
public class LogFormatter
4+
{
5+
[XmlAttribute]
6+
public string
7+
Command,
8+
Format;
9+
}
10+
public partial class Config : IRocketPluginConfiguration
11+
{
12+
public List<LogFormatter> Formatters = new List<LogFormatter>();
13+
public string
14+
LogType = "json",
15+
LogFile = "{time:dd'.'MM'.'yyyy}.log";
16+
public double LogSaveDelay = 15;
17+
public bool LogNotFormatted = false;
18+
public LogFormatter FindFormatter(string cmd) => Formatters.FirstOrDefault(x => x.Command.Equals(cmd, StringComparison.InvariantCultureIgnoreCase) || x.Command == "*");
19+
public void LoadDefaults()
20+
{
21+
Formatters = new()
22+
{
23+
new() { Command = "rocket", Format = "{caller}[{id}] executed {name} with arguments: {0}" },
24+
new() { Command = "*", Format = "[{time:dd'.'MM'.'yyyy' 'hh':'mm':'ss}] {id} executed {name} command" }
25+
};
26+
}
27+
}

Implementation/CommandInfo.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace SCommandLogger;
2+
3+
public class CommandInfo : ICommandInfo
4+
{
5+
public CommandInfo() { }
6+
public CommandInfo(string name, string[] arguments)
7+
{
8+
Name = name;
9+
Arguments = arguments;
10+
}
11+
12+
public string Name { get; set; }
13+
public string[] Arguments { get; set; }
14+
}

Implementation/CommandLogger.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Threading.Tasks;
2+
3+
namespace SCommandLogger;
4+
5+
public abstract class CommandLogger : ICommandLogger
6+
{
7+
protected List<PlayerLogs> List { get; } = new();
8+
9+
public virtual void Clear() => List.Clear();
10+
public virtual ICommandLoggerEntry Insert(ICommandLoggerEntry entry)
11+
{
12+
if (entry is not PlayerLogEntry e)
13+
return null;
14+
GetOrAdd(e.Executor).Entries.Add(e);
15+
return entry;
16+
}
17+
public PlayerLogs GetOrAdd(CSteamID id)
18+
{
19+
var logs = List.FirstOrDefault(x => x.Id.m_SteamID == id.m_SteamID);
20+
if(logs is null)
21+
List.Add(logs = new() { Id = id });
22+
return logs;
23+
}
24+
public virtual void Remove(ICommandLoggerEntry entry)
25+
{
26+
if (entry is not PlayerLogEntry e)
27+
return;
28+
GetOrAdd(entry.Executor).Entries.Remove(e);
29+
}
30+
public ICommandLoggerEntry Produce(IRocketPlayer caller, IRocketCommand command, string format, string[] args)
31+
{
32+
var fullArgs = string.Join(" ", args);
33+
var id = CSteamID.Nil;
34+
if (caller is not ConsolePlayer && ulong.TryParse(caller.Id, out var uid))
35+
id = new CSteamID(uid);
36+
var formattedMessage = format;
37+
args = args.Prepend(fullArgs).ToArray();
38+
for (int i = 0; i < args.Length; i++)
39+
formattedMessage = formattedMessage.Replace($"{{{i}}}", args[i]);
40+
formattedMessage = formattedMessage.FormatWith(new { Name = command.Name, Id = id, Caller = caller.DisplayName, Time = DateTime.Now });
41+
args = args.Skip(1).ToArray();
42+
return new PlayerLogEntry(id, new CommandInfo(command.Name, args), formattedMessage);
43+
}
44+
protected virtual void OnDispose() { }
45+
public void Dispose()
46+
{
47+
OnDispose();
48+
Clear();
49+
}
50+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Threading.Tasks;
2+
3+
namespace SCommandLogger;
4+
5+
public abstract class FileCommandLogger : CommandLogger
6+
{
7+
readonly string filePath;
8+
public virtual string FilePath => filePath.FormatWith(new { Time = DateTime.Now });
9+
public FileCommandLogger(string filePath, TimeSpan? saveDelay = null)
10+
{
11+
this.filePath = filePath;
12+
Load();
13+
SaveDelay = saveDelay ?? TimeSpan.FromSeconds(15);
14+
SaveSchedule();
15+
}
16+
17+
async void SaveSchedule() => await SaveTask();
18+
async Task SaveTask()
19+
{
20+
while (true)
21+
{
22+
await Task.Delay(SaveDelay);
23+
if (Disposed || CLogger is null || !ReferenceEquals(CLogger, this))
24+
return;
25+
Save();
26+
}
27+
}
28+
readonly TimeSpan SaveDelay;
29+
public bool Disposed { get; protected set; } = false;
30+
protected abstract void OnSave();
31+
protected abstract void OnLoad();
32+
public void Save()
33+
{
34+
if (Disposed)
35+
return;
36+
OnSave();
37+
}
38+
public void Load()
39+
{
40+
if (Disposed)
41+
return;
42+
OnLoad();
43+
}
44+
45+
protected override void OnDispose()
46+
{
47+
if (Disposed)
48+
return;
49+
Save();
50+
Disposed = true;
51+
}
52+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Newtonsoft.Json;
2+
3+
namespace SCommandLogger;
4+
5+
public class JSONCommandLogger : FileCommandLogger
6+
{
7+
public JSONCommandLogger(string filePath, TimeSpan? saveDelay = null) : base(filePath, saveDelay)
8+
{
9+
}
10+
11+
protected override void OnLoad()
12+
{
13+
try
14+
{
15+
if (File.Exists(FilePath))
16+
List.AddRange(JsonConvert.DeserializeObject<List<PlayerLogs>>(File.ReadAllText(FilePath)));
17+
}
18+
catch { }
19+
}
20+
21+
protected override void OnSave()
22+
{
23+
File.WriteAllText(FilePath, JsonConvert.SerializeObject(List, Formatting.Indented));
24+
}
25+
}

0 commit comments

Comments
 (0)