From 710433f2fda103d5b79df48d5d103fffd7c5c02e Mon Sep 17 00:00:00 2001 From: Nils Schucka Date: Sun, 7 Jan 2024 13:10:48 +0100 Subject: [PATCH 01/11] Update LanguageSupport.json with Centermessage example --- Examples/LanguageSupport.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Examples/LanguageSupport.json b/Examples/LanguageSupport.json index 6cca537..0a0b53e 100644 --- a/Examples/LanguageSupport.json +++ b/Examples/LanguageSupport.json @@ -4,6 +4,10 @@ "Description": "Send a sentance", "Command": "sentance", "Message": "{LANG=test} test", - "PrintTo": 0 + "PrintTo": 2, + "CenterMessage": { + "Message": "{LANG=test}", + "Time": 10 + } } ] \ No newline at end of file From 622123cc6baa6f35484545f088d75f525f1ca799 Mon Sep 17 00:00:00 2001 From: Nils Schucka Date: Sun, 7 Jan 2024 13:18:22 +0100 Subject: [PATCH 02/11] Adding comment to langsupport json --- Examples/LanguageSupport.json | 1 + 1 file changed, 1 insertion(+) diff --git a/Examples/LanguageSupport.json b/Examples/LanguageSupport.json index 0a0b53e..5bff262 100644 --- a/Examples/LanguageSupport.json +++ b/Examples/LanguageSupport.json @@ -3,6 +3,7 @@ "Title": "Sentance", "Description": "Send a sentance", "Command": "sentance", + // Note that you also need to add the test tag in the language files in CustomCommands/lang "Message": "{LANG=test} test", "PrintTo": 2, "CenterMessage": { From 4a5de885be67ada096c81d857d05f60bc792023e Mon Sep 17 00:00:00 2001 From: Nils Schucka Date: Thu, 11 Jan 2024 16:15:59 +0100 Subject: [PATCH 03/11] update Module loaded message --- CustomCommands/CustomCommands.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CustomCommands/CustomCommands.cs b/CustomCommands/CustomCommands.cs index de6431e..cb74168 100644 --- a/CustomCommands/CustomCommands.cs +++ b/CustomCommands/CustomCommands.cs @@ -43,7 +43,7 @@ public override void Load(bool hotReload) } Logger.LogInformation( - $"CustomCommands has been loaded, and the hot reload flag was {hotReload}, path is {ModulePath}"); + $"{ModuleName} loaded!"); PluginGlobals.Config = Config; From 416dccba4884e3ce720f318dd5b6c4d14f5791cb Mon Sep 17 00:00:00 2001 From: Nils Schucka Date: Thu, 11 Jan 2024 16:16:58 +0100 Subject: [PATCH 04/11] fixed readme image not working --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a0a7ed7..ad6045b 100644 --- a/README.md +++ b/README.md @@ -111,4 +111,4 @@ Defines if the command requires specific permissions to execute: ### Colorlist -![CS2Colors](.github\img\ColorsCS2.png) +![CS2Colors](.github/img/ColorsCS2.png) From c262b778c49383f4dfd3307bfa5ef77a375a2e2e Mon Sep 17 00:00:00 2001 From: HerrMagic Date: Thu, 11 Jan 2024 22:03:07 +0100 Subject: [PATCH 05/11] start implementing multiple jsons --- CustomCommands/CustomCommands.cs | 10 ++++- CustomCommands/Interfaces/ILoadJson.cs | 2 +- CustomCommands/Services/LoadJson.cs | 53 +++++++++++++++++--------- 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/CustomCommands/CustomCommands.cs b/CustomCommands/CustomCommands.cs index cb74168..a2a2f0d 100644 --- a/CustomCommands/CustomCommands.cs +++ b/CustomCommands/CustomCommands.cs @@ -9,7 +9,7 @@ namespace CustomCommands; public partial class CustomCommands : BasePlugin, IPluginConfig { public override string ModuleName => "CustomCommands"; - public override string ModuleVersion => "1.0.8"; + public override string ModuleVersion => "1.0.9"; public override string ModuleAuthor => "HerrMagic"; public override string ModuleDescription => "Create your own commands per config"; @@ -47,8 +47,14 @@ public override void Load(bool hotReload) PluginGlobals.Config = Config; - var comms = LoadJson.LoadCommandsFromJson(ModuleDirectory); + var comms = LoadJson.GettingCommandsFromJsonsFiles(ModuleDirectory); + if (comms == null) + { + Logger.LogError("No commands found please create a config file"); + return; + } + EventManager.RegisterListeners(); if (comms != null) diff --git a/CustomCommands/Interfaces/ILoadJson.cs b/CustomCommands/Interfaces/ILoadJson.cs index 1fdd577..585ad0e 100644 --- a/CustomCommands/Interfaces/ILoadJson.cs +++ b/CustomCommands/Interfaces/ILoadJson.cs @@ -4,5 +4,5 @@ namespace CustomCommands.Interfaces; public interface ILoadJson { - List? LoadCommandsFromJson(string path); + List GettingCommandsFromJsonsFiles(string path); } \ No newline at end of file diff --git a/CustomCommands/Services/LoadJson.cs b/CustomCommands/Services/LoadJson.cs index 55166e1..2e49aee 100644 --- a/CustomCommands/Services/LoadJson.cs +++ b/CustomCommands/Services/LoadJson.cs @@ -2,6 +2,7 @@ using CustomCommands.Interfaces; using CustomCommands.Model; using Microsoft.Extensions.Logging; +using Serilog; namespace CustomCommands.Services; @@ -14,29 +15,47 @@ public LoadJson(ILogger Logger) this.Logger = Logger; } - /// - /// Load the commands from the JSON file - /// - /// Returns the Commands as a List - public List? LoadCommandsFromJson(string path) + public List GettingCommandsFromJsonsFiles(string path) { - string jsonPath = Path.Combine(path, "Commands.json"); - if (File.Exists(jsonPath)) + var comms = new List(); + + var pathofcommands = Path.Combine(path, "Commands"); + var defaultconfigpath = Path.Combine(path, "Commands.json"); + var files = Directory.GetFiles(pathofcommands, "*.json", SearchOption.AllDirectories); + + // Check if the default config file exists in plugins/CustomCommands + if (File.Exists(defaultconfigpath)) { - var json = File.ReadAllText(jsonPath); - return JsonSerializer.Deserialize>(json); + files.Append(defaultconfigpath); + Logger.LogInformation("Found default config file"); } - else if (File.Exists(Path.Combine(path, "Commands.example.json"))) + // + else if (!File.Exists(defaultconfigpath) && files.Length == 0) { - Logger.LogWarning("No Config file found. Please rename Commands.example.json to Commands.json"); - return null; + Logger.LogWarning("No Config file found. Please create plugins/CustomCommands/Commands.json or in plugins/CustomCommands/Commands/.json"); + return comms; } - else + + foreach (var file in files) { - Logger.LogWarning("No Config file found. Please create one"); - return null; + var json = File.ReadAllText(file); + var commands = JsonSerializer.Deserialize>(json); + if (commands != null) + comms.AddRange(commands); + + } + return comms; + } + public bool ValidateObject(Commands comms, string path) + { + switch (comms) + { + case null: + Logger.LogError($"Config files is empty or not valid: {Path.GetFullPath(path)}"); + return false; + case { }: + + default: } } - - } From bf13dfe14f27cf60ac1d61b9173d5652a9ffa737 Mon Sep 17 00:00:00 2001 From: Nils Schucka Date: Sat, 13 Jan 2024 22:38:27 +0100 Subject: [PATCH 06/11] Log Infos --- CustomCommands/Interfaces/ILoadJson.cs | 2 + CustomCommands/Model/CommandsConfig.cs | 6 +- CustomCommands/Services/LoadJson.cs | 110 +++++++++++++++++++++++-- 3 files changed, 106 insertions(+), 12 deletions(-) diff --git a/CustomCommands/Interfaces/ILoadJson.cs b/CustomCommands/Interfaces/ILoadJson.cs index 585ad0e..8129c2c 100644 --- a/CustomCommands/Interfaces/ILoadJson.cs +++ b/CustomCommands/Interfaces/ILoadJson.cs @@ -5,4 +5,6 @@ namespace CustomCommands.Interfaces; public interface ILoadJson { List GettingCommandsFromJsonsFiles(string path); + bool ValidateObject(Commands comms, string path); + void CheckForExampleFile(string path); } \ No newline at end of file diff --git a/CustomCommands/Model/CommandsConfig.cs b/CustomCommands/Model/CommandsConfig.cs index 007de20..18342ad 100644 --- a/CustomCommands/Model/CommandsConfig.cs +++ b/CustomCommands/Model/CommandsConfig.cs @@ -2,12 +2,12 @@ public class Commands { - public required string Title { get; set; } = ""; + public string Title { get; set; } = ""; public string Description { get; set; } = "Description"; - public required string Command { get; set; } = "testtesttest"; + public string Command { get; set; } = ""; public dynamic Message { get; set; } = ""; public CenterElement CenterMessage { get; set; } = new(); - public required Sender PrintTo { get; set; } = Sender.ClientChat; + public Sender PrintTo { get; set; } = Sender.ClientChat; public List ServerCommands { get; set; } = new(); public Permission Permission { get; set; } = new(); } diff --git a/CustomCommands/Services/LoadJson.cs b/CustomCommands/Services/LoadJson.cs index 2e49aee..dfe6db8 100644 --- a/CustomCommands/Services/LoadJson.cs +++ b/CustomCommands/Services/LoadJson.cs @@ -40,22 +40,114 @@ public List GettingCommandsFromJsonsFiles(string path) { var json = File.ReadAllText(file); var commands = JsonSerializer.Deserialize>(json); - if (commands != null) - comms.AddRange(commands); + if (ValidateObject(commands, file)) + comms.AddRange(commands!); } return comms; } - public bool ValidateObject(Commands comms, string path) + public bool ValidateObject(List? comms, string path) { - switch (comms) + if (comms == null) { - case null: - Logger.LogError($"Config files is empty or not valid: {Path.GetFullPath(path)}"); + Logger.LogError($"Invalid object in {path}"); + return false; + } + foreach (var command in comms) + { + bool commandstatus = true; + if (command.Title == null || command.Title == "") + { + Logger.LogWarning($"Title not set in {path}. Title is not required but recommended"); + commandstatus = false; + } + if (command.Description == null || command.Description == "") + { + Logger.LogWarning($"Description not set in {path}. Description is not required but recommended. This will be shown in the help command"); + commandstatus = false; + } + if (command.Command == null || command.Command == "") + { + Logger.LogError($"Command not set in {path}"); + commandstatus = false; + } + if (commad) + if (!PrintToCheck(command)) return false; - case { }: - - default: + + if (!commandstatus) + { + SendCommandInfo(command); + return false; + } + + return true; + } + } + public void SendCommandInfo(Commands comms) + { + Logger.LogInformation($"Title: {comms.Title}"); + Logger.LogInformation($"Description: {comms.Description}"); + Logger.LogInformation($"Command: {comms.Command}"); + Logger.LogInformation($"Message: {comms.Message}"); + Logger.LogInformation($"CenterMessage: {comms.CenterMessage.Message}"); + Logger.LogInformation($"CenterMessageTime: {comms.CenterMessage.Time}"); + Logger.LogInformation($"PrintTo: {comms.PrintTo}"); + Logger.LogInformation($"ServerCommands: {string.Join(", ", comms.ServerCommands)}"); + Logger.LogInformation($"PermissionList: {string.Join(", ", comms.Permission.PermissionList)}"); + Logger.LogInformation($"RequiresAllPermissions: {comms.Permission.RequiresAllPermissions}"); + } + public bool PrintToCheck(Commands comms) + { + if (comms.PrintTo == Sender.ClientChat || comms.PrintTo == Sender.AllChat) + { + if (comms.Message == null || comms.Message == "") + { + Logger.LogError($"Message not set but needs to be set because PrintTo is set to {comms.PrintTo}"); + Logger.LogError($"Title: {comms.Title}"); + Logger.LogError($"Description: {comms.Description}"); + Logger.LogError($"Command: {comms.Command}"); + return false; + } + } + else if (comms.PrintTo == Sender.ClientCenter || comms.PrintTo == Sender.AllCenter) + { + if (comms.CenterMessage.Message == null || comms.CenterMessage.Message == "") + { + Logger.LogError($"CenterMessage is not set but needs to be set because PrintTo is set to {comms.PrintTo}"); + Logger.LogError($"Title: {comms.Title}"); + Logger.LogError($"Description: {comms.Description}"); + Logger.LogError($"Command: {comms.Command}"); + return false; + } + } + else + { + if (comms.Message == null || comms.Message == "" && comms.CenterMessage.Message != null || comms.CenterMessage.Message != "") + { + Logger.LogError($"Message not set but needs to be set because PrintTo is set to {comms.PrintTo}"); + Logger.LogError($"Title: {comms.Title}"); + Logger.LogError($"Description: {comms.Description}"); + Logger.LogError($"Command: {comms.Command}"); + return false; + } + else if (comms.CenterMessage.Message == null || comms.CenterMessage.Message == "" && comms.Message != null || comms.Message != "") + { + Logger.LogError($"CenterMessage is not set but needs to be set because PrintTo is set to {comms.PrintTo}"); + Logger.LogError($"Title: {comms.Title}"); + Logger.LogError($"Description: {comms.Description}"); + Logger.LogError($"Command: {comms.Command}"); + return false; + } + else + { + Logger.LogError($"Message and CenterMessage are not set but needs to be set because PrintTo is set to {comms.PrintTo}"); + Logger.LogError($"Title: {comms.Title}"); + Logger.LogError($"Description: {comms.Description}"); + Logger.LogError($"Command: {comms.Command}"); + return false; + } } + return true; } } From 7db2be035e55b43e1623034bb33f5b18b4bba479 Mon Sep 17 00:00:00 2001 From: HerrMagic Date: Sat, 13 Jan 2024 23:47:29 +0100 Subject: [PATCH 07/11] refactor the code --- CustomCommands/CustomCommands.cs | 2 +- CustomCommands/Interfaces/ILoadJson.cs | 6 +- CustomCommands/Services/LoadJson.cs | 83 +++++++++++++------------- 3 files changed, 45 insertions(+), 46 deletions(-) diff --git a/CustomCommands/CustomCommands.cs b/CustomCommands/CustomCommands.cs index a2a2f0d..82327f8 100644 --- a/CustomCommands/CustomCommands.cs +++ b/CustomCommands/CustomCommands.cs @@ -47,7 +47,7 @@ public override void Load(bool hotReload) PluginGlobals.Config = Config; - var comms = LoadJson.GettingCommandsFromJsonsFiles(ModuleDirectory); + var comms = LoadJson.GetCommandsFromJsonFiles(ModuleDirectory); if (comms == null) { diff --git a/CustomCommands/Interfaces/ILoadJson.cs b/CustomCommands/Interfaces/ILoadJson.cs index 8129c2c..2f19bff 100644 --- a/CustomCommands/Interfaces/ILoadJson.cs +++ b/CustomCommands/Interfaces/ILoadJson.cs @@ -4,7 +4,9 @@ namespace CustomCommands.Interfaces; public interface ILoadJson { - List GettingCommandsFromJsonsFiles(string path); - bool ValidateObject(Commands comms, string path); + List GetCommandsFromJsonFiles(string path); void CheckForExampleFile(string path); + bool ValidateObject(List? comms, string path); + void LogCommandDetails(Commands comms); + bool PrintToCheck(Commands comms); } \ No newline at end of file diff --git a/CustomCommands/Services/LoadJson.cs b/CustomCommands/Services/LoadJson.cs index dfe6db8..2ae8980 100644 --- a/CustomCommands/Services/LoadJson.cs +++ b/CustomCommands/Services/LoadJson.cs @@ -2,7 +2,6 @@ using CustomCommands.Interfaces; using CustomCommands.Model; using Microsoft.Extensions.Logging; -using Serilog; namespace CustomCommands.Services; @@ -15,10 +14,12 @@ public LoadJson(ILogger Logger) this.Logger = Logger; } - public List GettingCommandsFromJsonsFiles(string path) + public List GetCommandsFromJsonFiles(string path) { var comms = new List(); + CheckForExampleFile(path); + var pathofcommands = Path.Combine(path, "Commands"); var defaultconfigpath = Path.Combine(path, "Commands.json"); var files = Directory.GetFiles(pathofcommands, "*.json", SearchOption.AllDirectories); @@ -26,13 +27,13 @@ public List GettingCommandsFromJsonsFiles(string path) // Check if the default config file exists in plugins/CustomCommands if (File.Exists(defaultconfigpath)) { - files.Append(defaultconfigpath); - Logger.LogInformation("Found default config file"); + _ = files.Append(defaultconfigpath); + Logger.LogInformation("Found default config file."); } // else if (!File.Exists(defaultconfigpath) && files.Length == 0) { - Logger.LogWarning("No Config file found. Please create plugins/CustomCommands/Commands.json or in plugins/CustomCommands/Commands/.json"); + Logger.LogWarning("No Config file found. Please create plugins/CustomCommands/Commands.json or in plugins/CustomCommands/Commands/.json"); return comms; } @@ -46,45 +47,62 @@ public List GettingCommandsFromJsonsFiles(string path) } return comms; } + // Check if the Command.json file exists. If not replace it with the example file + public void CheckForExampleFile(string path) + { + var defaultconfigpath = Path.Combine(path, "Commands.json"); + var exampleconfigpath = Path.Combine(path, "Commands.example.json"); + if (!File.Exists(defaultconfigpath)) + { + File.Copy(exampleconfigpath, defaultconfigpath); + Logger.LogInformation("Created default config file."); + } + + } public bool ValidateObject(List? comms, string path) { if (comms == null) { - Logger.LogError($"Invalid object in {path}"); + Logger.LogError($"Invalid JSON format in {path}. Please check the docs on how to create a valid JSON file"); return false; } - foreach (var command in comms) + for (int i = comms.Count - 1; i >= 0 ; i--) { bool commandstatus = true; - if (command.Title == null || command.Title == "") + // Title + if (string.IsNullOrEmpty(comms[i].Title)) { Logger.LogWarning($"Title not set in {path}. Title is not required but recommended"); commandstatus = false; } - if (command.Description == null || command.Description == "") + // Description + if (string.IsNullOrEmpty(comms[i].Description)) { Logger.LogWarning($"Description not set in {path}. Description is not required but recommended. This will be shown in the help command"); commandstatus = false; } - if (command.Command == null || command.Command == "") + // Command + if (string.IsNullOrEmpty(comms[i].Command)) { Logger.LogError($"Command not set in {path}"); commandstatus = false; } - if (commad) - if (!PrintToCheck(command)) - return false; + if (!PrintToCheck(comms[i])) + commandstatus = false; if (!commandstatus) { - SendCommandInfo(command); + Logger.LogError($"Command {comms[i].Command} will not be loaded. Index: {i}"); + LogCommandDetails(comms[i]); return false; } return true; } + return true; } - public void SendCommandInfo(Commands comms) + + public void LogCommandDetails(Commands comms) { Logger.LogInformation($"Title: {comms.Title}"); Logger.LogInformation($"Description: {comms.Description}"); @@ -97,54 +115,33 @@ public void SendCommandInfo(Commands comms) Logger.LogInformation($"PermissionList: {string.Join(", ", comms.Permission.PermissionList)}"); Logger.LogInformation($"RequiresAllPermissions: {comms.Permission.RequiresAllPermissions}"); } + public bool PrintToCheck(Commands comms) { if (comms.PrintTo == Sender.ClientChat || comms.PrintTo == Sender.AllChat) { - if (comms.Message == null || comms.Message == "") + if (string.IsNullOrEmpty(comms.Message)) { Logger.LogError($"Message not set but needs to be set because PrintTo is set to {comms.PrintTo}"); - Logger.LogError($"Title: {comms.Title}"); - Logger.LogError($"Description: {comms.Description}"); - Logger.LogError($"Command: {comms.Command}"); + LogCommandDetails(comms); return false; } } else if (comms.PrintTo == Sender.ClientCenter || comms.PrintTo == Sender.AllCenter) { - if (comms.CenterMessage.Message == null || comms.CenterMessage.Message == "") + if (string.IsNullOrEmpty(comms.CenterMessage.Message)) { Logger.LogError($"CenterMessage is not set but needs to be set because PrintTo is set to {comms.PrintTo}"); - Logger.LogError($"Title: {comms.Title}"); - Logger.LogError($"Description: {comms.Description}"); - Logger.LogError($"Command: {comms.Command}"); + LogCommandDetails(comms); return false; } } else { - if (comms.Message == null || comms.Message == "" && comms.CenterMessage.Message != null || comms.CenterMessage.Message != "") - { - Logger.LogError($"Message not set but needs to be set because PrintTo is set to {comms.PrintTo}"); - Logger.LogError($"Title: {comms.Title}"); - Logger.LogError($"Description: {comms.Description}"); - Logger.LogError($"Command: {comms.Command}"); - return false; - } - else if (comms.CenterMessage.Message == null || comms.CenterMessage.Message == "" && comms.Message != null || comms.Message != "") - { - Logger.LogError($"CenterMessage is not set but needs to be set because PrintTo is set to {comms.PrintTo}"); - Logger.LogError($"Title: {comms.Title}"); - Logger.LogError($"Description: {comms.Description}"); - Logger.LogError($"Command: {comms.Command}"); - return false; - } - else + if (string.IsNullOrEmpty(comms.Message) && string.IsNullOrEmpty(comms.CenterMessage.Message)) { Logger.LogError($"Message and CenterMessage are not set but needs to be set because PrintTo is set to {comms.PrintTo}"); - Logger.LogError($"Title: {comms.Title}"); - Logger.LogError($"Description: {comms.Description}"); - Logger.LogError($"Command: {comms.Command}"); + LogCommandDetails(comms); return false; } } From 010689f0467902e5125aac425f23731db3c6e6ce Mon Sep 17 00:00:00 2001 From: Nils Schucka Date: Sat, 20 Jan 2024 18:55:37 +0100 Subject: [PATCH 08/11] removed unnecessary file building --- CustomCommands/CustomCommands.csproj | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CustomCommands/CustomCommands.csproj b/CustomCommands/CustomCommands.csproj index 4d51773..23f0ccd 100644 --- a/CustomCommands/CustomCommands.csproj +++ b/CustomCommands/CustomCommands.csproj @@ -10,7 +10,11 @@ - + + none + runtime + compile; build; native; contentfiles; analyzers; buildtransitive + From f121c80465de7b1facd8c39072f6c4f246cc0913 Mon Sep 17 00:00:00 2001 From: Nils Schucka Date: Sat, 20 Jan 2024 22:45:18 +0100 Subject: [PATCH 09/11] adding json file validation --- CustomCommands/Commands/Others.example.json | 2 +- CustomCommands/CustomCommands.csproj | 1 + CustomCommands/Interfaces/ILoadJson.cs | 2 + CustomCommands/Services/LoadJson.cs | 84 +++++++++++++++------ README.md | 4 + 5 files changed, 68 insertions(+), 25 deletions(-) diff --git a/CustomCommands/Commands/Others.example.json b/CustomCommands/Commands/Others.example.json index 07d36c6..7eb2c69 100644 --- a/CustomCommands/Commands/Others.example.json +++ b/CustomCommands/Commands/Others.example.json @@ -17,4 +17,4 @@ }, "PrintTo": 7 } - ] \ No newline at end of file +] \ No newline at end of file diff --git a/CustomCommands/CustomCommands.csproj b/CustomCommands/CustomCommands.csproj index 23f0ccd..134187f 100644 --- a/CustomCommands/CustomCommands.csproj +++ b/CustomCommands/CustomCommands.csproj @@ -26,6 +26,7 @@ + diff --git a/CustomCommands/Interfaces/ILoadJson.cs b/CustomCommands/Interfaces/ILoadJson.cs index 2f19bff..e564de8 100644 --- a/CustomCommands/Interfaces/ILoadJson.cs +++ b/CustomCommands/Interfaces/ILoadJson.cs @@ -6,7 +6,9 @@ public interface ILoadJson { List GetCommandsFromJsonFiles(string path); void CheckForExampleFile(string path); + bool IsValidJsonSyntax(string jsonString); bool ValidateObject(List? comms, string path); void LogCommandDetails(Commands comms); bool PrintToCheck(Commands comms); + bool ValidateMessage(dynamic message); } \ No newline at end of file diff --git a/CustomCommands/Services/LoadJson.cs b/CustomCommands/Services/LoadJson.cs index 2ae8980..b729811 100644 --- a/CustomCommands/Services/LoadJson.cs +++ b/CustomCommands/Services/LoadJson.cs @@ -22,16 +22,20 @@ public List GetCommandsFromJsonFiles(string path) var pathofcommands = Path.Combine(path, "Commands"); var defaultconfigpath = Path.Combine(path, "Commands.json"); - var files = Directory.GetFiles(pathofcommands, "*.json", SearchOption.AllDirectories); + + var files = new List(); + + if (Directory.Exists(pathofcommands)) + files.AddRange(Directory.GetFiles(pathofcommands, "*.json", SearchOption.AllDirectories)); // Check if the default config file exists in plugins/CustomCommands if (File.Exists(defaultconfigpath)) { - _ = files.Append(defaultconfigpath); + files.Add(defaultconfigpath); Logger.LogInformation("Found default config file."); } // - else if (!File.Exists(defaultconfigpath) && files.Length == 0) + else if (!File.Exists(defaultconfigpath) && files.Count == 0) { Logger.LogWarning("No Config file found. Please create plugins/CustomCommands/Commands.json or in plugins/CustomCommands/Commands/.json"); return comms; @@ -40,10 +44,14 @@ public List GetCommandsFromJsonFiles(string path) foreach (var file in files) { var json = File.ReadAllText(file); + + // Validate the JSON file + if (!IsValidJsonSyntax(file)) + continue; + var commands = JsonSerializer.Deserialize>(json); if (ValidateObject(commands, file)) comms.AddRange(commands!); - } return comms; } @@ -59,6 +67,21 @@ public void CheckForExampleFile(string path) } } + public bool IsValidJsonSyntax(string path) + { + try + { + var json = File.ReadAllText(path); + var document = JsonDocument.Parse(json); + return true; + } + catch (JsonException ex) + { + Logger.LogError($"Invalid JSON syntax in {path}. Please check the docs on how to create a valid JSON file"); + Logger.LogError(ex.Message); + return false; + } + } public bool ValidateObject(List? comms, string path) { if (comms == null) @@ -66,9 +89,10 @@ public bool ValidateObject(List? comms, string path) Logger.LogError($"Invalid JSON format in {path}. Please check the docs on how to create a valid JSON file"); return false; } - for (int i = comms.Count - 1; i >= 0 ; i--) + bool commandstatus = true; + for (int i = 0; i < comms.Count; i++) { - bool commandstatus = true; + commandstatus = true; // Title if (string.IsNullOrEmpty(comms[i].Title)) { @@ -94,36 +118,35 @@ public bool ValidateObject(List? comms, string path) { Logger.LogError($"Command {comms[i].Command} will not be loaded. Index: {i}"); LogCommandDetails(comms[i]); - return false; } - - return true; } + if (!commandstatus) + return false; return true; } public void LogCommandDetails(Commands comms) { - Logger.LogInformation($"Title: {comms.Title}"); - Logger.LogInformation($"Description: {comms.Description}"); - Logger.LogInformation($"Command: {comms.Command}"); - Logger.LogInformation($"Message: {comms.Message}"); - Logger.LogInformation($"CenterMessage: {comms.CenterMessage.Message}"); - Logger.LogInformation($"CenterMessageTime: {comms.CenterMessage.Time}"); - Logger.LogInformation($"PrintTo: {comms.PrintTo}"); - Logger.LogInformation($"ServerCommands: {string.Join(", ", comms.ServerCommands)}"); - Logger.LogInformation($"PermissionList: {string.Join(", ", comms.Permission.PermissionList)}"); - Logger.LogInformation($"RequiresAllPermissions: {comms.Permission.RequiresAllPermissions}"); + Logger.LogInformation($"-- Title: {comms.Title}"); + Logger.LogInformation($"-- Description: {comms.Description}"); + Logger.LogInformation($"-- Command: {comms.Command}"); + Logger.LogInformation($"-- Message: {comms.Message}"); + Logger.LogInformation($"-- CenterMessage: {comms.CenterMessage.Message}"); + Logger.LogInformation($"-- CenterMessageTime: {comms.CenterMessage.Time}"); + Logger.LogInformation($"-- PrintTo: {comms.PrintTo}"); + Logger.LogInformation($"-- ServerCommands: {JsonSerializer.Serialize(comms.ServerCommands)}"); + Logger.LogInformation($"-- PermissionList: {JsonSerializer.Serialize(comms.Permission)}"); + Logger.LogInformation("--------------------------------------------------"); } public bool PrintToCheck(Commands comms) { if (comms.PrintTo == Sender.ClientChat || comms.PrintTo == Sender.AllChat) { - if (string.IsNullOrEmpty(comms.Message)) + + if (!ValidateMessage(comms.Message)) { Logger.LogError($"Message not set but needs to be set because PrintTo is set to {comms.PrintTo}"); - LogCommandDetails(comms); return false; } } @@ -132,19 +155,32 @@ public bool PrintToCheck(Commands comms) if (string.IsNullOrEmpty(comms.CenterMessage.Message)) { Logger.LogError($"CenterMessage is not set but needs to be set because PrintTo is set to {comms.PrintTo}"); - LogCommandDetails(comms); return false; } } else { - if (string.IsNullOrEmpty(comms.Message) && string.IsNullOrEmpty(comms.CenterMessage.Message)) + if (!ValidateMessage(comms.Message) && string.IsNullOrEmpty(comms.CenterMessage.Message)) { Logger.LogError($"Message and CenterMessage are not set but needs to be set because PrintTo is set to {comms.PrintTo}"); - LogCommandDetails(comms); return false; } } return true; } + + public bool ValidateMessage(dynamic message) + { + if (message is JsonElement jsonElement) + { + if (jsonElement.ValueKind == JsonValueKind.String) + return true; + + if (jsonElement.ValueKind == JsonValueKind.Array) + return true; + + return false; + } + return false; + } } diff --git a/README.md b/README.md index ad6045b..ebd3f13 100644 --- a/README.md +++ b/README.md @@ -112,3 +112,7 @@ Defines if the command requires specific permissions to execute: ### Colorlist ![CS2Colors](.github/img/ColorsCS2.png) + +### Multiple Files + +If you want to have multiple files instead of one big one create a folder in plugins/CustomCommands named Commands there you can create json files and folders as many as you want and with any name. From 726141d2ed67ab40209bedf589679fd8db8a0a91 Mon Sep 17 00:00:00 2001 From: Nils Schucka Date: Sat, 20 Jan 2024 22:58:46 +0100 Subject: [PATCH 10/11] removed unnecessary usings --- CustomCommands/Services/EventManager.cs | 1 - CustomCommands/Services/MessageManager.cs | 1 - CustomCommands/Services/PermissionsManager.cs | 2 -- CustomCommands/Services/ReplaceTagsFunction.cs | 1 - 4 files changed, 5 deletions(-) diff --git a/CustomCommands/Services/EventManager.cs b/CustomCommands/Services/EventManager.cs index 69fb593..5fb1605 100644 --- a/CustomCommands/Services/EventManager.cs +++ b/CustomCommands/Services/EventManager.cs @@ -4,7 +4,6 @@ using CounterStrikeSharp.API.Core.Attributes.Registration; using CounterStrikeSharp.API.Core.Plugin; using CustomCommands.Interfaces; -using Microsoft.Extensions.Logging; namespace CustomCommands.Services; diff --git a/CustomCommands/Services/MessageManager.cs b/CustomCommands/Services/MessageManager.cs index 248b568..bf54ff9 100644 --- a/CustomCommands/Services/MessageManager.cs +++ b/CustomCommands/Services/MessageManager.cs @@ -3,7 +3,6 @@ using CounterStrikeSharp.API.Core.Plugin; using CustomCommands.Interfaces; using CustomCommands.Model; -using Microsoft.Extensions.Logging; namespace CustomCommands.Services; public class MessageManager : IMessageManager diff --git a/CustomCommands/Services/PermissionsManager.cs b/CustomCommands/Services/PermissionsManager.cs index dbc6389..b0ab555 100644 --- a/CustomCommands/Services/PermissionsManager.cs +++ b/CustomCommands/Services/PermissionsManager.cs @@ -2,8 +2,6 @@ using CounterStrikeSharp.API.Modules.Admin; using CustomCommands.Interfaces; using CustomCommands.Model; -using Microsoft.Extensions.Logging; -using Serilog; namespace CustomCommands.Services; diff --git a/CustomCommands/Services/ReplaceTagsFunction.cs b/CustomCommands/Services/ReplaceTagsFunction.cs index 2e7eaa8..81f2a6b 100644 --- a/CustomCommands/Services/ReplaceTagsFunction.cs +++ b/CustomCommands/Services/ReplaceTagsFunction.cs @@ -6,7 +6,6 @@ using CounterStrikeSharp.API.Modules.Entities; using CounterStrikeSharp.API.Modules.Utils; using CustomCommands.Interfaces; -using CounterStrikeSharp.API.Core.Translations; using Microsoft.Extensions.Logging; using CounterStrikeSharp.API.Core.Plugin; From 5a9ac1e3c19c4b599709a3a88a57e0d6f20807e4 Mon Sep 17 00:00:00 2001 From: Nils Schucka Date: Sat, 20 Jan 2024 23:07:29 +0100 Subject: [PATCH 11/11] adding contributing text to readme --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index ebd3f13..c5eb8df 100644 --- a/README.md +++ b/README.md @@ -116,3 +116,8 @@ Defines if the command requires specific permissions to execute: ### Multiple Files If you want to have multiple files instead of one big one create a folder in plugins/CustomCommands named Commands there you can create json files and folders as many as you want and with any name. + + +## Contributing + +You are welcome to contribute to this project! Simply create a pull request, and it will be promptly reviewed.