Fixed logging
This commit is contained in:
54
DiscordBot/Bot/Actions/AddPlugin.cs
Normal file
54
DiscordBot/Bot/Actions/AddPlugin.cs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
using DiscordBotCore;
|
||||||
|
using DiscordBotCore.Interfaces;
|
||||||
|
using DiscordBotCore.Others;
|
||||||
|
using DiscordBotCore.Others.Actions;
|
||||||
|
using DiscordBotCore.Plugin;
|
||||||
|
|
||||||
|
namespace DiscordBot.Bot.Actions
|
||||||
|
{
|
||||||
|
internal class AddPlugin : ICommandAction
|
||||||
|
{
|
||||||
|
public string ActionName => "add-plugin";
|
||||||
|
|
||||||
|
public string Description => "Add a local plugin to the database";
|
||||||
|
|
||||||
|
public string Usage => "add-plugin <path>";
|
||||||
|
|
||||||
|
public IEnumerable<InternalActionOption> ListOfOptions => [
|
||||||
|
new InternalActionOption("path", "The path to the plugin to add")
|
||||||
|
];
|
||||||
|
|
||||||
|
public InternalActionRunType RunType => InternalActionRunType.ON_CALL;
|
||||||
|
|
||||||
|
public async Task Execute(string[] args)
|
||||||
|
{
|
||||||
|
if(args.Length < 1)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Invalid arguments given. Please use the following format:");
|
||||||
|
Console.WriteLine("add-plugin <path>");
|
||||||
|
Console.WriteLine("path: The path to the plugin to add");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var path = args[0];
|
||||||
|
|
||||||
|
if(!System.IO.File.Exists(path))
|
||||||
|
{
|
||||||
|
Console.WriteLine("The file does not exist !!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
FileInfo fileInfo = new FileInfo(path);
|
||||||
|
PluginInfo pluginInfo = new PluginInfo(fileInfo.Name, new(1, 0, 0), [], false, true);
|
||||||
|
await Application.CurrentApplication.PluginManager.AppendPluginToDatabase(pluginInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,8 @@ using System.Collections.Generic;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
|
||||||
namespace DiscordBot;
|
namespace DiscordBot;
|
||||||
@@ -81,4 +83,6 @@ public static class Entry
|
|||||||
|
|
||||||
Program.Startup(args).Wait();
|
Program.Startup(args).Wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.InteropServices.ComTypes;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using DiscordBotCore.Others;
|
using DiscordBotCore.Others;
|
||||||
using Spectre.Console;
|
using Spectre.Console;
|
||||||
using Spectre.Console.Rendering;
|
using Spectre.Console.Rendering;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using DiscordBotCore.Plugin;
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@@ -30,7 +31,6 @@ namespace DiscordBotCore
|
|||||||
public string ServerID => ApplicationEnvironmentVariables["ServerID"];
|
public string ServerID => ApplicationEnvironmentVariables["ServerID"];
|
||||||
public string PluginDatabase => ApplicationEnvironmentVariables["PluginDatabase"] ?? _PluginsDatabaseFile;
|
public string PluginDatabase => ApplicationEnvironmentVariables["PluginDatabase"] ?? _PluginsDatabaseFile;
|
||||||
public string LogFile => $"{ApplicationEnvironmentVariables["LogFolder"]}/{DateTime.Now.ToLongDateString().Replace(" / ", "")}.log";
|
public string LogFile => $"{ApplicationEnvironmentVariables["LogFolder"]}/{DateTime.Now.ToLongDateString().Replace(" / ", "")}.log";
|
||||||
public string DataFolder => _ResourcesFolder;
|
|
||||||
|
|
||||||
public SettingsDictionary<string, string> ApplicationEnvironmentVariables { get; private set; }
|
public SettingsDictionary<string, string> ApplicationEnvironmentVariables { get; private set; }
|
||||||
public InternalActionManager InternalActionManager { get; private set; }
|
public InternalActionManager InternalActionManager { get; private set; }
|
||||||
@@ -81,6 +81,7 @@ namespace DiscordBotCore
|
|||||||
CurrentApplication.InternalActionManager = new InternalActionManager();
|
CurrentApplication.InternalActionManager = new InternalActionManager();
|
||||||
await CurrentApplication.InternalActionManager.Initialize();
|
await CurrentApplication.InternalActionManager.Initialize();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -101,5 +102,12 @@ namespace DiscordBotCore
|
|||||||
CurrentApplication.ApplicationEnvironmentVariables["MaxParallelDownloads"] = _MaxParallelDownloads;
|
CurrentApplication.ApplicationEnvironmentVariables["MaxParallelDownloads"] = _MaxParallelDownloads;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string GetResourceFullPath(string path)
|
||||||
|
{
|
||||||
|
string result = Path.Combine(_ResourcesFolder, path);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string GetResourceFullPath() => _ResourcesFolder;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using DiscordBotCore.Interfaces.Logger;
|
using DiscordBotCore.Interfaces.Logger;
|
||||||
|
|
||||||
@@ -8,6 +10,7 @@ namespace DiscordBotCore.Others.Logger;
|
|||||||
|
|
||||||
public sealed class Logger : ILogger
|
public sealed class Logger : ILogger
|
||||||
{
|
{
|
||||||
|
private readonly FileStream _LogStream;
|
||||||
|
|
||||||
public List<string> LogMessageProperties = typeof(ILogMessage).GetProperties().Select(p => p.Name).ToList();
|
public List<string> LogMessageProperties = typeof(ILogMessage).GetProperties().Select(p => p.Name).ToList();
|
||||||
public string LogMessageFormat { get ; set; }
|
public string LogMessageFormat { get ; set; }
|
||||||
@@ -18,6 +21,7 @@ public sealed class Logger : ILogger
|
|||||||
public Logger(string logMessageFormat)
|
public Logger(string logMessageFormat)
|
||||||
{
|
{
|
||||||
this.LogMessageFormat = logMessageFormat;
|
this.LogMessageFormat = logMessageFormat;
|
||||||
|
_LogStream = File.Open(Application.CurrentApplication.LogFile, FileMode.Append, FileAccess.Write, FileShare.Read);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -37,9 +41,15 @@ public sealed class Logger : ILogger
|
|||||||
return messageAsString;
|
return messageAsString;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void LogToFile(string message)
|
private async void LogToFile(string message)
|
||||||
{
|
{
|
||||||
System.IO.File.AppendAllText(Application.CurrentApplication.LogFile, message);
|
byte[] messageAsBytes = System.Text.Encoding.ASCII.GetBytes(message);
|
||||||
|
await _LogStream.WriteAsync(messageAsBytes, 0, messageAsBytes.Length);
|
||||||
|
|
||||||
|
byte[] newLine = System.Text.Encoding.ASCII.GetBytes(Environment.NewLine);
|
||||||
|
await _LogStream.WriteAsync(newLine, 0, newLine.Length);
|
||||||
|
|
||||||
|
await _LogStream.FlushAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GenerateLogMessage(ILogMessage message, string customFormat)
|
private string GenerateLogMessage(ILogMessage message, string customFormat)
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@@ -90,5 +91,25 @@ public class SettingsDictionary<TKey, TValue>
|
|||||||
set => _Dictionary[key] = value;
|
set => _Dictionary[key] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// First
|
||||||
|
public KeyValuePair<TKey, TValue> FirstOrDefault(Func<KeyValuePair<TKey, TValue>, bool> predicate)
|
||||||
|
{
|
||||||
|
return _Dictionary.FirstOrDefault(predicate);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Where
|
||||||
|
public IEnumerable<KeyValuePair<TKey, TValue>> Where(Func<KeyValuePair<TKey, TValue>, bool> predicate)
|
||||||
|
{
|
||||||
|
return _Dictionary.Where(predicate);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
_Dictionary.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<TKey> Keys => _Dictionary.Keys;
|
||||||
|
public IEnumerable<TValue> Values => _Dictionary.Values;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,15 +13,17 @@ public class PluginInfo
|
|||||||
public string FilePath { get; private set; }
|
public string FilePath { get; private set; }
|
||||||
public Dictionary<string, string> ListOfDependancies {get; private set;}
|
public Dictionary<string, string> ListOfDependancies {get; private set;}
|
||||||
public bool IsMarkedToUninstall {get; internal set;}
|
public bool IsMarkedToUninstall {get; internal set;}
|
||||||
|
public bool IsOfflineAdded { get; internal set; }
|
||||||
|
|
||||||
[JsonConstructor]
|
[JsonConstructor]
|
||||||
public PluginInfo(string pluginName, PluginVersion pluginVersion, Dictionary<string, string> listOfDependancies, bool isMarkedToUninstall)
|
public PluginInfo(string pluginName, PluginVersion pluginVersion, Dictionary<string, string> listOfDependancies, bool isMarkedToUninstall, bool isOfflineAdded)
|
||||||
{
|
{
|
||||||
PluginName = pluginName;
|
PluginName = pluginName;
|
||||||
PluginVersion = pluginVersion;
|
PluginVersion = pluginVersion;
|
||||||
ListOfDependancies = listOfDependancies;
|
ListOfDependancies = listOfDependancies;
|
||||||
IsMarkedToUninstall = isMarkedToUninstall;
|
IsMarkedToUninstall = isMarkedToUninstall;
|
||||||
FilePath = $"{Application.CurrentApplication.ApplicationEnvironmentVariables["PluginFolder"]}/{pluginName}.dll";
|
FilePath = $"{Application.CurrentApplication.ApplicationEnvironmentVariables["PluginFolder"]}/{pluginName}.dll";
|
||||||
|
IsOfflineAdded = isOfflineAdded;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PluginInfo(string pluginName, PluginVersion pluginVersion, Dictionary<string, string> listOfDependancies)
|
public PluginInfo(string pluginName, PluginVersion pluginVersion, Dictionary<string, string> listOfDependancies)
|
||||||
|
|||||||
@@ -52,6 +52,9 @@ public class PluginUpdater
|
|||||||
public async Task<bool> HasUpdate(string pluginName)
|
public async Task<bool> HasUpdate(string pluginName)
|
||||||
{
|
{
|
||||||
var localPluginInfo = await GetLocalPluginInfo(pluginName);
|
var localPluginInfo = await GetLocalPluginInfo(pluginName);
|
||||||
|
if(localPluginInfo.IsOfflineAdded)
|
||||||
|
return false;
|
||||||
|
|
||||||
var pluginInfo = await GetPluginInfo(pluginName);
|
var pluginInfo = await GetPluginInfo(pluginName);
|
||||||
|
|
||||||
return pluginInfo.Version.IsNewerThan(localPluginInfo.PluginVersion);
|
return pluginInfo.Version.IsNewerThan(localPluginInfo.PluginVersion);
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ namespace DiscordBotUI_Windows
|
|||||||
{
|
{
|
||||||
internal class Config
|
internal class Config
|
||||||
{
|
{
|
||||||
internal static DiscordBotCore.Others.SettingsDictionary<string, string> ApplicationSettings = new DiscordBotCore.Others.SettingsDictionary<string, string>(Path.Combine(DiscordBotCore.Application.CurrentApplication.DataFolder, "DiscordBotUI/config.json"));
|
internal static DiscordBotCore.Others.SettingsDictionary<string, string> ApplicationSettings = new DiscordBotCore.Others.SettingsDictionary<string, string>(DiscordBotCore.Application.GetResourceFullPath("DiscordBotUI/config.json"));
|
||||||
internal static ThemeManager ThemeManager = new ThemeManager();
|
internal static ThemeManager ThemeManager = new ThemeManager();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user