diff --git a/BrackeysBot/BrackeysBot.csproj b/BrackeysBot/BrackeysBot.csproj index 7e615b7..777e12f 100644 --- a/BrackeysBot/BrackeysBot.csproj +++ b/BrackeysBot/BrackeysBot.csproj @@ -26,6 +26,18 @@ ResXFileCodeGenerator ExceptionMessages.Designer.cs + + ResXFileCodeGenerator + LoggerMessages.Designer.cs + + + + + + True + True + LoggerMessages.resx + diff --git a/BrackeysBot/Plugins/SimplePluginManager.cs b/BrackeysBot/Plugins/SimplePluginManager.cs index d72fe95..bc14728 100644 --- a/BrackeysBot/Plugins/SimplePluginManager.cs +++ b/BrackeysBot/Plugins/SimplePluginManager.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -58,8 +58,7 @@ public void DisablePlugin(IPlugin plugin) } catch (Exception exception) { - Logger.Error(exception, - $"An exception was thrown when attempting to stop hosted service {hostedService.GetType()}"); + Logger.Error(exception, string.Format(LoggerMessages.ExceptionWhenStoppingService, hostedService.GetType())); } } @@ -69,13 +68,13 @@ public void DisablePlugin(IPlugin plugin) } catch (Exception exception) { - Logger.Error(exception, $"An exception was thrown when attempting to disable {plugin.PluginInfo.Name}"); + Logger.Error(exception, string.Format(LoggerMessages.ExceptionWhenDisablingPlugin, plugin.PluginInfo.Name)); } _loadedPlugins[plugin] = false; monoPlugin.DiscordClient?.DisconnectAsync(); - Logger.Info($"Disabled plugin {plugin.PluginInfo.Name} {plugin.PluginInfo.Version}"); + Logger.Info(string.Format(LoggerMessages.DisabledPlugin, plugin.PluginInfo.Name, plugin.PluginInfo.Version)); } /// @@ -94,8 +93,7 @@ public void EnablePlugin(IPlugin plugin) } catch (Exception exception) { - Logger.Error(exception, - $"An exception was thrown when attempting to start hosted service {hostedService.GetType()}"); + Logger.Error(exception, string.Format(LoggerMessages.ExceptionWhenStartingService, hostedService.GetType())); } } @@ -105,14 +103,14 @@ public void EnablePlugin(IPlugin plugin) } catch (Exception exception) { - Logger.Error(exception, $"An exception was thrown when attempting to enable {plugin.PluginInfo.Name}"); + Logger.Error(exception, string.Format(LoggerMessages.ExceptionWhenEnablingPlugin, plugin.PluginInfo.Name)); return; } monoPlugin.DiscordClient?.ConnectAsync(); _loadedPlugins[plugin] = true; - Logger.Info($"Enabled {plugin.PluginInfo.Name} {plugin.PluginInfo.Version}"); + Logger.Info(string.Format(LoggerMessages.EnabledPlugin, plugin.PluginInfo.Name, plugin.PluginInfo.Version)); } /// @@ -139,7 +137,7 @@ public IPlugin LoadPlugin(string name) _loadedPlugins.Keys.FirstOrDefault(p => string.Equals(p.PluginInfo.Name, name, StringComparison.Ordinal)); if (loadedPlugin is not null) { - Logger.Debug($"Plugin {name} was requested to load, but is already loaded. Skipping!"); + Logger.Debug(string.Format(LoggerMessages.PluginAlreadyLoaded, name)); return loadedPlugin; } @@ -153,7 +151,7 @@ public IPlugin LoadPlugin(string name) Assembly assembly = Assembly.LoadFile(pluginFileName); // DO NOT LoadFrom here. LoadFile does not load into domain if (_loadedAssemblies.Exists(a => a.Location == assembly.Location)) { - Logger.Debug($"Assembly {assembly} already loaded. Using cache"); + Logger.Debug(string.Format(LoggerMessages.AssemblyAlreadyLoaded, assembly)); assembly = _loadedAssemblies.Find(a => a.Location == assembly.Location)!; } else @@ -161,7 +159,7 @@ public IPlugin LoadPlugin(string name) assembly = Assembly.LoadFrom(pluginFileName); // loads into domain _loadedAssemblies.Add(assembly); - Logger.Debug($"Loaded new assembly {assembly}"); + Logger.Debug(string.Format(LoggerMessages.LoadedNewAssembly, assembly)); } Type[] pluginTypes = assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(MonoPlugin))).ToArray(); @@ -184,8 +182,8 @@ public IPlugin LoadPlugin(string name) var dependenciesAttribute = type.GetCustomAttribute(); if (dependenciesAttribute?.Dependencies is {Length: > 0} dependencyNames) { - Logger.Debug( - $"{pluginAttribute.Name} requires {dependencyNames.Length} dependencies: {string.Join(", ", dependencyNames)}"); + Logger.Debug(string.Format(LoggerMessages.PluginRequiresDependencies, pluginAttribute.Name, dependencyNames.Length, + string.Join(", ", dependencyNames))); foreach (string dependencyName in dependencyNames) { @@ -196,7 +194,7 @@ public IPlugin LoadPlugin(string name) } catch (Exception exception) { - Logger.Error(exception, $"Could not load dependency '{dependencyName}'"); + Logger.Error(exception, string.Format(LoggerMessages.CouldNotLoadDependency, dependencyName)); return null!; // return value will be ignored anyway } } @@ -205,8 +203,8 @@ public IPlugin LoadPlugin(string name) string assemblyVersion = assembly.GetName().Version?.ToString(3) ?? pluginAttribute.Version; if (!string.Equals(pluginAttribute.Version, assemblyVersion)) { - Logger.Warn( - $"Plugin version {pluginAttribute.Version} and assembly version {assemblyVersion} do not match for {pluginAttribute.Name}!"); + Logger.Warn(string.Format(LoggerMessages.PluginVersionMismatch, pluginAttribute.Version, assemblyVersion, + pluginAttribute.Name)); } PluginInfo.PluginAuthorInfo? author = null; @@ -256,8 +254,7 @@ public IPlugin LoadPlugin(string name) var token = plugin.Configuration.Get("discord.token"); if (string.IsNullOrWhiteSpace(token)) { - Logger.Warn( - $"No token was specified in the config file for {plugin.PluginInfo.Name}! No client will be created for this plugin."); + Logger.Warn(string.Format(LoggerMessages.NoPluginToken, plugin.PluginInfo.Name)); } else { @@ -289,7 +286,7 @@ public IPlugin LoadPlugin(string name) _loadedPlugins.Add(plugin, false); - Logger.Info($"Loaded plugin {pluginInfo.Name} {pluginInfo.Version}"); + Logger.Info(string.Format(LoggerMessages.LoadedPlugin, plugin.PluginInfo.Name, plugin.PluginInfo.Version)); _pluginLoadStack.Pop(); return plugin; } @@ -303,7 +300,7 @@ public IReadOnlyList LoadPlugins() } catch (IOException exception) { - Logger.Warn(exception, $"The plugin directory '{PluginDirectory.FullName}' could not be created."); + Logger.Warn(exception, string.Format(LoggerMessages.PluginDirectoryCantBeCreated, PluginDirectory.FullName)); return ArraySegment.Empty; } @@ -329,7 +326,7 @@ public IReadOnlyList LoadPlugins() } plugin?.Dispose(); - Logger.Error(exception, $"Could not load plugin {pluginName}"); + Logger.Info(exception, string.Format(LoggerMessages.ExceptionWhenLoadingPlugin, pluginName)); } } @@ -349,14 +346,14 @@ public void UnloadPlugin(IPlugin plugin) } catch (Exception exception) { - Logger.Error(exception, $"An exception was thrown when attempting to unload {plugin.PluginInfo.Name}"); + Logger.Info(exception, string.Format(LoggerMessages.ExceptionWhenUnloadingPlugin, plugin.PluginInfo.Name)); } monoPlugin.DiscordClient?.Dispose(); monoPlugin.DiscordClient = null; plugin.Dispose(); - Logger.Info($"Unloaded plugin {plugin.PluginInfo.Name} {plugin.PluginInfo.Version}"); + Logger.Info(string.Format(LoggerMessages.UnloadedPlugin, plugin.PluginInfo.Name, plugin.PluginInfo.Version)); _loadedPlugins.Remove(plugin); } } diff --git a/BrackeysBot/Resources/LoggerMessages.Designer.cs b/BrackeysBot/Resources/LoggerMessages.Designer.cs new file mode 100644 index 0000000..564fe29 --- /dev/null +++ b/BrackeysBot/Resources/LoggerMessages.Designer.cs @@ -0,0 +1,225 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace BrackeysBot.Resources { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class LoggerMessages { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal LoggerMessages() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("BrackeysBot.Resources.LoggerMessages", typeof(LoggerMessages).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to Assembly {0} already loaded. Using cache. + /// + internal static string AssemblyAlreadyLoaded { + get { + return ResourceManager.GetString("AssemblyAlreadyLoaded", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Could not load dependency '{0}'. + /// + internal static string CouldNotLoadDependency { + get { + return ResourceManager.GetString("CouldNotLoadDependency", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Disabled plugin {0} {1}. + /// + internal static string DisabledPlugin { + get { + return ResourceManager.GetString("DisabledPlugin", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Enabled plugin {0} {1}. + /// + internal static string EnabledPlugin { + get { + return ResourceManager.GetString("EnabledPlugin", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to An exception was thrown when attempting to disable '{0}'. + /// + internal static string ExceptionWhenDisablingPlugin { + get { + return ResourceManager.GetString("ExceptionWhenDisablingPlugin", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to An exception was thrown when attempting to enable '{0}'. + /// + internal static string ExceptionWhenEnablingPlugin { + get { + return ResourceManager.GetString("ExceptionWhenEnablingPlugin", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to An exception was thrown when attempting to load '{0}'. + /// + internal static string ExceptionWhenLoadingPlugin { + get { + return ResourceManager.GetString("ExceptionWhenLoadingPlugin", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to An exception was thrown when attempting to start hosted service '{0}'. + /// + internal static string ExceptionWhenStartingService { + get { + return ResourceManager.GetString("ExceptionWhenStartingService", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to An exception was thrown when attempting to stop hosted service '{0}'. + /// + internal static string ExceptionWhenStoppingService { + get { + return ResourceManager.GetString("ExceptionWhenStoppingService", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to An exception was thrown when attempting to unload '{0}'. + /// + internal static string ExceptionWhenUnloadingPlugin { + get { + return ResourceManager.GetString("ExceptionWhenUnloadingPlugin", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Loaded new assembly {0}. + /// + internal static string LoadedNewAssembly { + get { + return ResourceManager.GetString("LoadedNewAssembly", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Loaded plugin {0} {1}. + /// + internal static string LoadedPlugin { + get { + return ResourceManager.GetString("LoadedPlugin", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to No token was specified in the config file for '{0}'! No client will be created for this plugin.. + /// + internal static string NoPluginToken { + get { + return ResourceManager.GetString("NoPluginToken", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Plugin '{0}' was requested to load, but is already loaded. Skipping!. + /// + internal static string PluginAlreadyLoaded { + get { + return ResourceManager.GetString("PluginAlreadyLoaded", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The plugin directory '{0}' could not be created. + /// + internal static string PluginDirectoryCantBeCreated { + get { + return ResourceManager.GetString("PluginDirectoryCantBeCreated", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Plugin '{0}' requires {1} dependencies: {2}. + /// + internal static string PluginRequiresDependencies { + get { + return ResourceManager.GetString("PluginRequiresDependencies", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Plugin version {0} and assembly version {1} do not match for '{2}'!. + /// + internal static string PluginVersionMismatch { + get { + return ResourceManager.GetString("PluginVersionMismatch", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Unloaded plugin {0} {1}. + /// + internal static string UnloadedPlugin { + get { + return ResourceManager.GetString("UnloadedPlugin", resourceCulture); + } + } + } +} diff --git a/BrackeysBot/Resources/LoggerMessages.resx b/BrackeysBot/Resources/LoggerMessages.resx new file mode 100644 index 0000000..451514b --- /dev/null +++ b/BrackeysBot/Resources/LoggerMessages.resx @@ -0,0 +1,80 @@ + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, + PublicKeyToken=b77a5c561934e089 + + + + Assembly {0} already loaded. Using cache + + + Could not load dependency '{0}' + + + Disabled plugin {0} {1} + + + Enabled plugin {0} {1} + + + An exception was thrown when attempting to disable '{0}' + + + An exception was thrown when attempting to enable '{0}' + + + An exception was thrown when attempting to load '{0}' + + + An exception was thrown when attempting to start hosted service '{0}' + + + An exception was thrown when attempting to stop hosted service '{0}' + + + An exception was thrown when attempting to unload '{0}' + + + Loaded new assembly {0} + + + Loaded plugin {0} {1} + + + No token was specified in the config file for '{0}'! No client will be created for this plugin. + + + Plugin '{0}' was requested to load, but is already loaded. Skipping! + + + The plugin directory '{0}' could not be created + + + Plugin '{0}' requires {1} dependencies: {2} + + + Plugin version {0} and assembly version {1} do not match for '{2}'! + + + Unloaded plugin {0} {1} + +