-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuto-Delete-Files-GoldKingZ.cs
122 lines (107 loc) · 6.03 KB
/
Auto-Delete-Files-GoldKingZ.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Commands;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Localization;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using System.Text;
using Newtonsoft.Json.Linq;
using Auto_Delete_Files_GoldKingZ.Config;
namespace Auto_Delete_Files_GoldKingZ;
public class AutoDeleteFilesGoldKingZ : BasePlugin
{
public override string ModuleName => "Auto Delete Files";
public override string ModuleVersion => "1.0.0";
public override string ModuleAuthor => "Gold KingZ";
public override string ModuleDescription => "https://github.com/oqyh";
public override void Load(bool hotReload)
{
Configs.Load(ModuleDirectory);
Configs.Shared.CookiesModule = ModuleDirectory;
RegisterListener<Listeners.OnMapStart>(OnMapStart);
}
private void OnMapStart(string Map)
{
try
{
string jsonFilePath = Path.Combine(ModuleDirectory, "../../plugins/Auto-Delete-Files-GoldKingZ/config/AutoDelete_Settings.json");
string jsonData = File.ReadAllText(jsonFilePath);
JObject jsonObject = JObject.Parse(jsonData);
if (jsonObject == null) return;
var itemKeys = jsonObject.Properties()
.Where(p => p.Name.StartsWith("AutoDelete_"))
.Select(p => p.Name)
.ToList();
bool startsWithItem = jsonObject.Properties()
.Any(p => p.Name.StartsWith("AutoDelete_"));
if(startsWithItem)
{
foreach (var key in itemKeys)
{
var itemMenu = jsonObject[key];
if (itemMenu != null)
{
JObject deleteData = itemMenu.Value<JObject>()!;
string deletePath = deleteData["Delete_Path"]!.ToString();
string deleteFiles = deleteData["Delete_Files"]!.ToString();
int deleteOlderThanXDays = (int)deleteData["Delete_OlderThanXDays"]!;
deletePath = Path.Combine(Server.GameDirectory, deletePath);
if (!Directory.Exists(deletePath))
{
if(Configs.GetConfigData().SendErrorLogsToServerConsole)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"================================================================ E R R O R ================================================================");
Console.WriteLine($"[Auto Delete Files Gold KingZ] Directory does not exist: {deletePath}");
Console.WriteLine($"================================================================ E R R O R ================================================================");
Console.ResetColor();
}
continue;
}
DateTime currentDate = DateTime.Now;
DateTime thresholdDate = currentDate.AddDays(-deleteOlderThanXDays);
string[] files = Directory.GetFiles(deletePath, deleteFiles);
if(Configs.GetConfigData().SendErrorLogsToServerConsole)
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine($"================================================================ D E L E T I N G ================================================================");
Console.ResetColor();
}
foreach (var file in files)
{
FileInfo fileInfo = new FileInfo(file);
if (fileInfo.LastWriteTime < thresholdDate)
{
fileInfo.Delete();
if(Configs.GetConfigData().SendErrorLogsToServerConsole)
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine($"[Auto Delete Files Gold KingZ] Deleted File: {fileInfo.FullName}");
Console.ResetColor();
}
}
}
if(Configs.GetConfigData().SendErrorLogsToServerConsole)
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine($"================================================================ D E L E T I N G ================================================================");
Console.ResetColor();
}
}
}
}
}
catch (Exception ex)
{
if(Configs.GetConfigData().SendErrorLogsToServerConsole)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"================================================================ E R R O R ================================================================");
Console.WriteLine($"[Auto Delete Files Gold KingZ] An error occurred: {ex.Message}");
Console.WriteLine($"================================================================ E R R O R ================================================================");
Console.ResetColor();
}
}
}
}