Commit refactoring

This commit is contained in:
zontreck 2024-12-17 12:10:01 -07:00
parent f735bce4e1
commit d33d11123d
51 changed files with 348 additions and 18 deletions

View file

@ -0,0 +1,67 @@
using System;
using System.Security.Cryptography;
using System.Text;
using ForgeCoreAPI.Utilities;
using LibAC.NBT;
using LibAC.NBT.API;
namespace ForgeCoreAPI.ACS;
/// <summary>
/// This represents the access token which would be utilized to authenticate commands
/// </summary>
public class AccessToken
{
public string TokenName { get; set; } = "";
public AccessTokenType TokenType { get; set; } = AccessTokenType.READ;
private string AccessTokenSeed { get; set; } = "";
public DateTime Expiration { get; set; } = DateTime.Now.AddDays(30);
public static AccessToken Create(string tokenName, AccessTokenType tokenType, bool expires = true)
{
AccessToken AT = new AccessToken();
AT.TokenName = tokenName;
AT.TokenType = tokenType;
if (!expires)
AT.Expiration = DateTime.MinValue;
var bytes = HashHelpers.EncodeMD5(HashHelpers.MakeRandomBytes(255));
AT.AccessTokenSeed = Convert.ToHexString(bytes);
return AT;
}
public override string ToString()
{
return Convert.ToHexString(HashHelpers.EncodeMD5(Encoding.UTF8.GetBytes($"{TokenName}:${AccessTokenSeed}")));
}
public CompoundTag Save()
{
CompoundTag tag = new CompoundTag();
tag.Put("name", StringTag.ValueOf(TokenName));
tag.Put("type", ByteTag.ValueOf((byte)TokenType));
tag.Put("seed", StringTag.ValueOf(AccessTokenSeed));
NbtUtils.WriteBoolean(tag, "expires", Expiration == DateTime.MinValue);
tag.Put("expiration", StringTag.ValueOf(Expiration.ToString()));
return tag;
}
public static AccessToken Load(CompoundTag tag)
{
AccessToken AT = new AccessToken();
AT.TokenName = tag["name"].AsString();
AT.TokenType = (AccessTokenType)tag["type"].AsByte();
AT.AccessTokenSeed = tag["seed"].AsString();
AT.Expiration = DateTime.Parse(tag["expiration"].AsString());
return AT;
}
}
public enum AccessTokenType : byte
{
FULL = 0,
READ = 1
}

1
ForgeCoreAPI/Commands.cs Normal file
View file

@ -0,0 +1 @@
namespace ForgeCoreAPI;

View file

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\External\LibAC\LibAC.csproj" />
</ItemGroup>
</Project>

23
ForgeCoreAPI/IMessage.cs Normal file
View file

@ -0,0 +1,23 @@
using LibAC.Arguments;
namespace ForgeCoreAPI;
/// <summary>
/// A simple interface representing a message passed into the bot server
/// </summary>
public interface IMessage
{
/// <summary>
/// Get the stored message
/// </summary>
/// <returns>Raw message</returns>
public string GetMessage();
/// <summary>
/// Set the stored raw message
/// </summary>
/// <param name="Message">Raw string message</param>
public void SetMessage(string Message);
public Arguments ParseCommand();
}

59
ForgeCoreAPI/IPlugin.cs Normal file
View file

@ -0,0 +1,59 @@
using LibAC.NBT;
namespace ForgeCoreAPI;
public interface IPlugin
{
/// <summary>
/// Called on plugin load.
/// <br/>
/// DOES NOT INDICATE PLUGIN ENABLE / START
/// </summary>
void Initialize();
/// <summary>
/// Called when the plugin is enabled by the server
/// </summary>
void Enable();
/// <summary>
/// Called when the plugin is disabled by the server.
///<br/>
/// NOTE: This action also indicates the plugin will no longer receive calls to the tick method.
/// </summary>
void Disable();
/// <summary>
/// Called every 5 seconds by the master server
/// </summary>
void tick();
/// <summary>
/// Provides the plugin with a copy of its saved configuration data
/// </summary>
/// <param name="config">A final readonly configuration object</param>
void LoadConfig(CompoundTag config);
/// <summary>
/// Requested by the master server when the state is being saved. This does not indicate shutdown of the server.
/// </summary>
/// <returns>A serialized copy of the plugin configuration data. This should include all settings and data.</returns>
CompoundTag SaveConfig();
/// <summary>
/// This is called when no plugin memory exists, or when a reset has been called for this plugin.
/// </summary>
void ResetMemory();
}
/// <summary>
/// Marks a class as a valid plugin. Plugin Class must also implement IPlugin
/// </summary>
public class ForgeCorePluginAttribute : Attribute
{
public string pluginName;
public ForgeCorePluginAttribute(string PluginName)
{
this.pluginName = PluginName;
}
}

View file

@ -0,0 +1,52 @@
using System.Reflection;
using LibAC.NBT;
using LibAC.NBT.API;
namespace ForgeCoreAPI;
public class PluginSystem
{
public static List<IPlugin> Plugins = new List<IPlugin>();
public static void InitializeSystem()
{
CompoundTag pluginsData = NbtIo.Read("PluginStorage.dat");
// Search the Plugins directory for DLL files
string[] files = Directory.GetFiles("Plugins", "*.dll");
// Begin loading assemblies
foreach (var file in files)
{
Assembly asm = Assembly.LoadFile(file);
// Scan types for ForgeCorePlugin attribute
foreach (var types in asm.GetTypes())
{
if (types.GetCustomAttribute<ForgeCorePluginAttribute>() != null)
{
IPlugin plugin = Activator.CreateInstance(types) as IPlugin;
plugin.Initialize();
ForgeCorePluginAttribute attrib = types.GetCustomAttribute<ForgeCorePluginAttribute>();
Tag? pluginStore = pluginsData.Get(attrib.pluginName);
if (pluginStore == null)
{
plugin.ResetMemory();
}
else
{
CompoundTag tag = (CompoundTag) pluginStore;
CompoundTag dataTag = tag["data"] as CompoundTag;
plugin.LoadConfig(dataTag);
if(tag.Get("enabled").AsByte() == 1) plugin.Enable();
else plugin.Disable();
}
Plugins.Add(plugin);
}
}
}
}
}

View file

@ -0,0 +1,19 @@
using System.Security.Cryptography;
namespace ForgeCoreAPI.Utilities;
public class HashHelpers
{
public static byte[] EncodeMD5(byte[] input)
{
MD5 md5 = MD5.Create();
return md5.ComputeHash(input);
}
public static byte[] MakeRandomBytes(int length)
{
ByteLayer A = new ByteLayer();
A.InsertRandomBytes(length);
return A.Bytes;
}
}