Skip to content

Commit 2b8803e

Browse files
authored
Merge pull request #22 from HerrMagiic/dev
v1.0.9
2 parents cec9467 + 606177a commit 2b8803e

11 files changed

+193
-28
lines changed

Diff for: CustomCommands/Commands/Others.example.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
},
1818
"PrintTo": 7
1919
}
20-
]
20+
]

Diff for: CustomCommands/CustomCommands.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace CustomCommands;
99
public partial class CustomCommands : BasePlugin, IPluginConfig<CustomCommandsConfig>
1010
{
1111
public override string ModuleName => "CustomCommands";
12-
public override string ModuleVersion => "1.0.8";
12+
public override string ModuleVersion => "1.0.9";
1313
public override string ModuleAuthor => "HerrMagic";
1414
public override string ModuleDescription => "Create your own commands per config";
1515

@@ -47,8 +47,14 @@ public override void Load(bool hotReload)
4747

4848
PluginGlobals.Config = Config;
4949

50-
var comms = LoadJson.LoadCommandsFromJson(ModuleDirectory);
50+
var comms = LoadJson.GetCommandsFromJsonFiles(ModuleDirectory);
5151

52+
if (comms == null)
53+
{
54+
Logger.LogError("No commands found please create a config file");
55+
return;
56+
}
57+
5258
EventManager.RegisterListeners();
5359

5460
if (comms != null)

Diff for: CustomCommands/CustomCommands.csproj

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.142" />
13+
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.142">
14+
<PrivateAssets>none</PrivateAssets>
15+
<ExcludeAssets>runtime</ExcludeAssets>
16+
<IncludeAssets>compile; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
</PackageReference>
1418
<PackageReference Include="Scrutor" Version="4.2.2" />
1519
</ItemGroup>
1620

@@ -22,6 +26,7 @@
2226

2327
<ItemGroup>
2428
<None Update="lang\**\*.*" CopyToOutputDirectory="PreserveNewest" />
29+
<None Update="Commands.example.json" CopyToOutputDirectory="PreserveNewest" />
2530
</ItemGroup>
2631

2732
</Project>

Diff for: CustomCommands/Interfaces/ILoadJson.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@ namespace CustomCommands.Interfaces;
44

55
public interface ILoadJson
66
{
7-
List<Commands>? LoadCommandsFromJson(string path);
7+
List<Commands> GetCommandsFromJsonFiles(string path);
8+
void CheckForExampleFile(string path);
9+
bool IsValidJsonSyntax(string jsonString);
10+
bool ValidateObject(List<Commands>? comms, string path);
11+
void LogCommandDetails(Commands comms);
12+
bool PrintToCheck(Commands comms);
13+
bool ValidateMessage(dynamic message);
814
}

Diff for: CustomCommands/Model/CommandsConfig.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
public class Commands
44
{
5-
public required string Title { get; set; } = "";
5+
public string Title { get; set; } = "";
66
public string Description { get; set; } = "Description";
7-
public required string Command { get; set; } = "testtesttest";
7+
public string Command { get; set; } = "";
88
public dynamic Message { get; set; } = "";
99
public CenterElement CenterMessage { get; set; } = new();
10-
public required Sender PrintTo { get; set; } = Sender.ClientChat;
10+
public Sender PrintTo { get; set; } = Sender.ClientChat;
1111
public List<string> ServerCommands { get; set; } = new();
1212
public Permission Permission { get; set; } = new();
1313
}

Diff for: CustomCommands/Services/EventManager.cs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using CounterStrikeSharp.API.Core.Attributes.Registration;
55
using CounterStrikeSharp.API.Core.Plugin;
66
using CustomCommands.Interfaces;
7-
using Microsoft.Extensions.Logging;
87

98
namespace CustomCommands.Services;
109

Diff for: CustomCommands/Services/LoadJson.cs

+159-15
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,173 @@ public LoadJson(ILogger<CustomCommands> Logger)
1414
this.Logger = Logger;
1515
}
1616

17-
/// <summary>
18-
/// Load the commands from the JSON file
19-
/// </summary>
20-
/// <returns>Returns the Commands as a List</returns>
21-
public List<Commands>? LoadCommandsFromJson(string path)
17+
public List<Commands> GetCommandsFromJsonFiles(string path)
2218
{
23-
string jsonPath = Path.Combine(path, "Commands.json");
24-
if (File.Exists(jsonPath))
19+
var comms = new List<Commands>();
20+
21+
CheckForExampleFile(path);
22+
23+
var pathofcommands = Path.Combine(path, "Commands");
24+
var defaultconfigpath = Path.Combine(path, "Commands.json");
25+
26+
var files = new List<string>();
27+
28+
if (Directory.Exists(pathofcommands))
29+
files.AddRange(Directory.GetFiles(pathofcommands, "*.json", SearchOption.AllDirectories));
30+
31+
// Check if the default config file exists in plugins/CustomCommands
32+
if (File.Exists(defaultconfigpath))
33+
{
34+
files.Add(defaultconfigpath);
35+
Logger.LogInformation("Found default config file.");
36+
}
37+
//
38+
else if (!File.Exists(defaultconfigpath) && files.Count == 0)
39+
{
40+
Logger.LogWarning("No Config file found. Please create plugins/CustomCommands/Commands.json or in plugins/CustomCommands/Commands/<name>.json");
41+
return comms;
42+
}
43+
44+
foreach (var file in files)
45+
{
46+
var json = File.ReadAllText(file);
47+
48+
// Validate the JSON file
49+
if (!IsValidJsonSyntax(file))
50+
continue;
51+
52+
var commands = JsonSerializer.Deserialize<List<Commands>>(json);
53+
if (ValidateObject(commands, file))
54+
comms.AddRange(commands!);
55+
}
56+
return comms;
57+
}
58+
// Check if the Command.json file exists. If not replace it with the example file
59+
public void CheckForExampleFile(string path)
60+
{
61+
var defaultconfigpath = Path.Combine(path, "Commands.json");
62+
var exampleconfigpath = Path.Combine(path, "Commands.example.json");
63+
if (!File.Exists(defaultconfigpath))
64+
{
65+
File.Copy(exampleconfigpath, defaultconfigpath);
66+
Logger.LogInformation("Created default config file.");
67+
}
68+
69+
}
70+
public bool IsValidJsonSyntax(string path)
71+
{
72+
try
73+
{
74+
var json = File.ReadAllText(path);
75+
var document = JsonDocument.Parse(json);
76+
return true;
77+
}
78+
catch (JsonException ex)
2579
{
26-
var json = File.ReadAllText(jsonPath);
27-
return JsonSerializer.Deserialize<List<Commands>>(json);
80+
Logger.LogError($"Invalid JSON syntax in {path}. Please check the docs on how to create a valid JSON file");
81+
Logger.LogError(ex.Message);
82+
return false;
2883
}
29-
else if (File.Exists(Path.Combine(path, "Commands.example.json")))
84+
}
85+
public bool ValidateObject(List<Commands>? comms, string path)
86+
{
87+
if (comms == null)
88+
{
89+
Logger.LogError($"Invalid JSON format in {path}. Please check the docs on how to create a valid JSON file");
90+
return false;
91+
}
92+
bool commandstatus = true;
93+
for (int i = 0; i < comms.Count; i++)
94+
{
95+
commandstatus = true;
96+
// Title
97+
if (string.IsNullOrEmpty(comms[i].Title))
98+
{
99+
Logger.LogWarning($"Title not set in {path}. Title is not required but recommended");
100+
commandstatus = false;
101+
}
102+
// Description
103+
if (string.IsNullOrEmpty(comms[i].Description))
104+
{
105+
Logger.LogWarning($"Description not set in {path}. Description is not required but recommended. This will be shown in the help command");
106+
commandstatus = false;
107+
}
108+
// Command
109+
if (string.IsNullOrEmpty(comms[i].Command))
110+
{
111+
Logger.LogError($"Command not set in {path}");
112+
commandstatus = false;
113+
}
114+
if (!PrintToCheck(comms[i]))
115+
commandstatus = false;
116+
117+
if (!commandstatus)
118+
{
119+
Logger.LogError($"Command {comms[i].Command} will not be loaded. Index: {i}");
120+
LogCommandDetails(comms[i]);
121+
}
122+
}
123+
if (!commandstatus)
124+
return false;
125+
return true;
126+
}
127+
128+
public void LogCommandDetails(Commands comms)
129+
{
130+
Logger.LogInformation($"-- Title: {comms.Title}");
131+
Logger.LogInformation($"-- Description: {comms.Description}");
132+
Logger.LogInformation($"-- Command: {comms.Command}");
133+
Logger.LogInformation($"-- Message: {comms.Message}");
134+
Logger.LogInformation($"-- CenterMessage: {comms.CenterMessage.Message}");
135+
Logger.LogInformation($"-- CenterMessageTime: {comms.CenterMessage.Time}");
136+
Logger.LogInformation($"-- PrintTo: {comms.PrintTo}");
137+
Logger.LogInformation($"-- ServerCommands: {JsonSerializer.Serialize(comms.ServerCommands)}");
138+
Logger.LogInformation($"-- PermissionList: {JsonSerializer.Serialize(comms.Permission)}");
139+
Logger.LogInformation("--------------------------------------------------");
140+
}
141+
142+
public bool PrintToCheck(Commands comms)
143+
{
144+
if (comms.PrintTo == Sender.ClientChat || comms.PrintTo == Sender.AllChat)
30145
{
31-
Logger.LogWarning("No Config file found. Please rename Commands.example.json to Commands.json");
32-
return null;
146+
147+
if (!ValidateMessage(comms.Message))
148+
{
149+
Logger.LogError($"Message not set but needs to be set because PrintTo is set to {comms.PrintTo}");
150+
return false;
151+
}
33152
}
153+
else if (comms.PrintTo == Sender.ClientCenter || comms.PrintTo == Sender.AllCenter)
154+
{
155+
if (string.IsNullOrEmpty(comms.CenterMessage.Message))
156+
{
157+
Logger.LogError($"CenterMessage is not set but needs to be set because PrintTo is set to {comms.PrintTo}");
158+
return false;
159+
}
160+
}
34161
else
35162
{
36-
Logger.LogWarning("No Config file found. Please create one");
37-
return null;
163+
if (!ValidateMessage(comms.Message) && string.IsNullOrEmpty(comms.CenterMessage.Message))
164+
{
165+
Logger.LogError($"Message and CenterMessage are not set but needs to be set because PrintTo is set to {comms.PrintTo}");
166+
return false;
167+
}
38168
}
169+
return true;
39170
}
40171

41-
172+
public bool ValidateMessage(dynamic message)
173+
{
174+
if (message is JsonElement jsonElement)
175+
{
176+
if (jsonElement.ValueKind == JsonValueKind.String)
177+
return true;
178+
179+
if (jsonElement.ValueKind == JsonValueKind.Array)
180+
return true;
181+
182+
return false;
183+
}
184+
return false;
185+
}
42186
}

Diff for: CustomCommands/Services/MessageManager.cs

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using CounterStrikeSharp.API.Core.Plugin;
44
using CustomCommands.Interfaces;
55
using CustomCommands.Model;
6-
using Microsoft.Extensions.Logging;
76

87
namespace CustomCommands.Services;
98
public class MessageManager : IMessageManager

Diff for: CustomCommands/Services/PermissionsManager.cs

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using CounterStrikeSharp.API.Modules.Admin;
33
using CustomCommands.Interfaces;
44
using CustomCommands.Model;
5-
using Microsoft.Extensions.Logging;
6-
using Serilog;
75

86
namespace CustomCommands.Services;
97

Diff for: CustomCommands/Services/ReplaceTagsFunction.cs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using CounterStrikeSharp.API.Modules.Entities;
77
using CounterStrikeSharp.API.Modules.Utils;
88
using CustomCommands.Interfaces;
9-
using CounterStrikeSharp.API.Core.Translations;
109
using Microsoft.Extensions.Logging;
1110
using CounterStrikeSharp.API.Core.Plugin;
1211

Diff for: README.md

+9
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,12 @@ Defines if the command requires specific permissions to execute:
112112
### Colorlist
113113

114114
![CS2Colors](.github/img/ColorsCS2.png)
115+
116+
### Multiple Files
117+
118+
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.
119+
120+
121+
## Contributing
122+
123+
You are welcome to contribute to this project! Simply create a pull request, and it will be promptly reviewed.

0 commit comments

Comments
 (0)