check for error/bugs using CounterStrikeSharp.API.Core; using…
- User
check for error/bugs
using CounterStrikeSharp.API.Core; using Microsoft.Extensions.Logging;
namespace RemoveOldFiles;
public class RemoveOldFilesPlugin : BasePlugin, IPluginConfig { public override string ModuleName => "RemoveOldFiles"; public override string ModuleVersion => "1.0"; public override string ModuleAuthor => "Muinez"; public DeletionConfig Config { get; set; } = null!;
public override void Load(bool hotReload) { RegisterListener<Listeners.OnMapStart>(OnMapStart); } private void OnMapStart(string map) { var time = DateTimeOffset.Now.ToUnixTimeSeconds(); foreach (var config in Config.Directories) { if (!Directory.Exists(config.Path)) { Logger.LogWarning("Directory doesn't exist: {path}", config.Path); continue; } var deleteCount = 0; foreach (var filePath in Directory.EnumerateFiles(config.Path, config.Filter)) { var fileInfo = new FileInfo(filePath); if (time - new DateTimeOffset(fileInfo.LastWriteTime).ToUnixTimeSeconds() > config.MaxFileTimeSeconds) { File.Delete(fileInfo.FullName); deleteCount++; } } Logger.LogInformation("Deleted {deleteCount} files", deleteCount); } } public void OnConfigParsed(DeletionConfig deletionConfig) { Config = deletionConfig; }}
public class DeletionConfig : IBasePluginConfig { public int Version { get; set; }
public List<DirectoryConfig> Directories { get; set; } = new() { new() { Path = "/csgo/logs", Filter = "*.log", MaxFileTimeSeconds = 86400 * 7 } };}
public class DirectoryConfig { public string Path { get; set; } public string Filter { get; set; } public int MaxFileTimeSeconds { get; set; } }
- Assistant
Your plugin code looks mostly correct, but I'll highlight a few issues and suggestions:
-
Namespace Naming Conventions: Ensure that your namespace follows the standard naming conventions. Namespaces are typically PascalCase.
namespace RemoveOldFiles; // This is fine, but just a reminder. -
Nullability Warning: You are initializing the
Configproperty withnull!, which bypasses null-checks. While you set it usingOnConfigParsed, it's a good practice to avoid this if possible. Consider initializing it properly or ensuring null safety checks. -
Logging Dependency: Ensure that the
Loggerinstance is initialized properly in the base classBasePlugin. The logging functions likeLogger.LogWarningandLogger.LogInformationare dependent on this. -
Directory.Delete: When deleting a file, you might want to handle exceptions to log any issues during deletion, such as lack of permissions or file locks.
-
Plugin Registration and Configuration: Make sure your plugin registration system calls
LoadandOnConfigParsedin the right order. This will ensureConfigis not null when used inOnMapStart. -
Unknown
RegisterListenermethod: Without seeing the implementation ofRegisterListener, ensure it exists in theBasePluginand works as expected for yourListeners.OnMapStartevent.
Here's the modified code with these considerations:
using System; using System.Collections.Generic; using System.IO; using CounterStrikeSharp.API.Core; using Microsoft.Extensions.Logging; namespace RemoveOldFiles { public class RemoveOldFilesPlugin : BasePlugin, IPluginConfig<DeletionConfig> { public override string ModuleName => "RemoveOldFiles"; public override string ModuleVersion => "1.0"; public override string ModuleAuthor => "Muinez"; public DeletionConfig Config { get; set; } = new DeletionConfig(); public override void Load(bool hotReload) { RegisterListener<Listeners.OnMapStart>(OnMapStart); } private void OnMapStart(string map) { var time = DateTimeOffset.Now.ToUnixTimeSeconds(); foreach (var config in Config.Directories) { if (!Directory.Exists(config.Path)) { Logger.LogWarning("Directory doesn't exist: {path}", config.Path); continue; } var deleteCount = 0; foreach (var filePath in Directory.EnumerateFiles(config.Path, config.Filter)) { var fileInfo = new FileInfo(filePath); if (time - new DateTimeOffset(fileInfo.LastWriteTime).ToUnixTimeSeconds() > config.MaxFileTimeSeconds) { try { File.Delete(fileInfo.FullName); deleteCount++; } catch (Exception ex) { Logger.LogError(ex, "Failed to delete file: {filePath}", fileInfo.FullName); } } } Logger.LogInformation("Deleted {deleteCount} files from {path}", deleteCount, config.Path); } } public void OnConfigParsed(DeletionConfig deletionConfig) { Config = deletionConfig ?? throw new ArgumentNullException(nameof(deletionConfig)); } } public class DeletionConfig : IBasePluginConfig { public int Version { get; set; } public List<DirectoryConfig> Directories { get; set; } = new() { new() { Path = "/csgo/logs", Filter = "*.log", MaxFileTimeSeconds = 86400 * 7 } }; } public class DirectoryConfig { public string Path { get; set; } = string.Empty; public string Filter { get; set; } = "*.*"; public int MaxFileTimeSeconds { get; set; } } }Key Changes:
- Removed
null!fromConfiginitialization to avoid nullability bypass issues. - Added exception handling to
File.Deletefor better logging and fault tolerance. - Updated logging message in
Logger.LogInformationto include directory path for more context. - Added a null check in the
OnConfigParsedmethod to ensure the configuration object is not null.
These changes should help in making your plugin more robust and easier to troubleshoot if something goes wrong.
-