1
0
mirror of synced 2024-11-28 09:30:51 +01:00

feat: Added logger module to script loader

This commit is contained in:
WerWolv 2024-03-13 19:49:48 +01:00
parent 2c711ea206
commit 458584d778
8 changed files with 161 additions and 20 deletions

View File

@ -38,6 +38,7 @@ if (CoreClrEmbed_FOUND)
source/script_api/v1/mem.cpp source/script_api/v1/mem.cpp
source/script_api/v1/bookmarks.cpp source/script_api/v1/bookmarks.cpp
source/script_api/v1/ui.cpp source/script_api/v1/ui.cpp
source/script_api/v1/logger.cpp
) )
set(EXTRA_BUNDLE_LIBRARY_PATHS "${CoreClrEmbed_FOLDER}" PARENT_SCOPE) set(EXTRA_BUNDLE_LIBRARY_PATHS "${CoreClrEmbed_FOLDER}" PARENT_SCOPE)

View File

@ -1,6 +1,5 @@
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.Loader; using System.Runtime.Loader;
namespace ImHex namespace ImHex
@ -47,6 +46,7 @@ namespace ImHex
{ {
if (type is "LOAD") if (type is "LOAD")
{ {
// If the script has been loaded already, don't do it again
if (loadedPlugins.Contains(path)) if (loadedPlugins.Contains(path))
{ {
return 0; return 0;
@ -65,19 +65,49 @@ namespace ImHex
continue; continue;
} }
// Load the Assembly
try try
{ {
context.LoadFromStream(new MemoryStream(File.ReadAllBytes(file))); context.LoadFromStream(new MemoryStream(File.ReadAllBytes(file)));
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine("[.NET Script] Failed to load assembly: " + file + " - " + e.ToString()); Console.WriteLine("[.NET Script] Failed to load assembly: " + file + " - " + e);
} }
} }
// Load the script assembly // Load the script assembly
var assembly = context.LoadFromStream(new MemoryStream(File.ReadAllBytes(path))); var assembly = context.LoadFromStream(new MemoryStream(File.ReadAllBytes(path)));
// Find ImHexLibrary module
var libraryModule = Array.Find(context.Assemblies.ToArray(), module => module.GetName().Name == "ImHexLibrary");
if (libraryModule == null)
{
Console.WriteLine("[.NET Script] Refusing to load non-ImHex script");
return 1;
}
else
{
// Load Library type
var libraryType = libraryModule.GetType("Library");
if (libraryType == null)
{
Console.WriteLine("[.NET Script] Failed to find Library type in ImHexLibrary");
return 1;
}
// Load Initialize function in the Library type
var initMethod = libraryType.GetMethod("Initialize", BindingFlags.Static | BindingFlags.Public);
if (initMethod == null)
{
Console.WriteLine("[.NET Script] Failed to find Initialize method");
return 1;
}
// Execute it
initMethod.Invoke(null, null);
}
// Find a class named "Script" // Find a class named "Script"
var entryPointType = assembly.GetType("Script"); var entryPointType = assembly.GetType("Script");
if (entryPointType == null) if (entryPointType == null)
@ -100,6 +130,7 @@ namespace ImHex
} }
else if (type == "CHECK") else if (type == "CHECK")
{ {
// Check if the method exists
return entryPointType.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public) != null ? 0 : 1; return entryPointType.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public) != null ? 0 : 1;
} }
else else
@ -109,7 +140,7 @@ namespace ImHex
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine("[.NET Script] Exception in AssemblyLoader: " + e.ToString()); Console.WriteLine("[.NET Script] Exception in AssemblyLoader: " + e);
return 3; return 3;
} }
finally finally

View File

@ -0,0 +1,34 @@
#include <script_api.hpp>
#include <hex/api/imhex_api.hpp>
#include <hex/helpers/logger.hpp>
#define VERSION V1
SCRIPT_API(void logPrint, const char *message) {
hex::log::print("{}", message);
}
SCRIPT_API(void logPrintln, const char *message) {
hex::log::println("{}", message);
}
SCRIPT_API(void logDebug, const char *message) {
hex::log::debug("{}", message);
}
SCRIPT_API(void logInfo, const char *message) {
hex::log::info("{}", message);
}
SCRIPT_API(void logWarn, const char *message) {
hex::log::warn("{}", message);
}
SCRIPT_API(void logError, const char *message) {
hex::log::error("{}", message);
}
SCRIPT_API(void logFatal, const char *message) {
hex::log::fatal("{}", message);
}

View File

@ -1,6 +1,4 @@
#pragma warning disable SYSLIB1054 using System.Drawing;
using System.Drawing;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;

View File

@ -0,0 +1,9 @@
using ImHex;
public class Library
{
public static void Initialize()
{
Logger.RedirectConsole();
}
}

View File

@ -0,0 +1,78 @@
using System.Runtime.InteropServices;
using System.Text;
#pragma warning disable SYSLIB1054
namespace ImHex
{
public partial class Logger
{
[LibraryImport("ImHex")]
private static partial void logPrintV1(byte[] message);
[LibraryImport("ImHex")]
private static partial void logPrintlnV1(byte[] message);
[LibraryImport("ImHex")]
private static partial void logDebugV1(byte[] message);
[LibraryImport("ImHex")]
private static partial void logInfoV1(byte[] message);
[LibraryImport("ImHex")]
private static partial void logWarnV1(byte[] message);
[LibraryImport("ImHex")]
private static partial void logErrorV1(byte[] message);
[LibraryImport("ImHex")]
private static partial void logFatalV1(byte[] message);
public static void Debug(string message)
{
logDebugV1(Encoding.UTF8.GetBytes(message));
}
public static void Info(string message)
{
logInfoV1(Encoding.UTF8.GetBytes(message));
}
public static void Warn(string message)
{
logWarnV1(Encoding.UTF8.GetBytes(message));
}
public static void Error(string message)
{
logErrorV1(Encoding.UTF8.GetBytes(message));
}
public static void Fatal(string message)
{
logFatalV1(Encoding.UTF8.GetBytes(message));
}
private class LoggerWriter : TextWriter
{
public override Encoding Encoding => Encoding.UTF8;
public override void Write(string? value)
{
logPrintV1(Encoding.GetBytes(value ?? string.Empty));
}
public override void WriteLine(string? value)
{
logPrintlnV1(Encoding.GetBytes(value ?? string.Empty));
}
}
public static void RedirectConsole()
{
Console.SetOut(new LoggerWriter());
}
}
}

View File

@ -1,6 +1,4 @@
#pragma warning disable SYSLIB1054 using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using System.Text; using System.Text;
namespace ImHex namespace ImHex

View File

@ -2,17 +2,9 @@
using ImGuiNET; using ImGuiNET;
class Script { class Script {
public static void OnLoad() { public static void OnLoad()
{
// This function is executed the first time the Plugin is loaded // This function is executed the first time the Plugin is loaded
UI.RegisterView(new byte[]{ 0xEE, 0xAC, 0x89 }, "Test View", () =>
{
ImGui.SetCurrentContext(UI.GetImGuiContext());
ImGui.TextUnformatted("Test Text");
if (ImGui.Button("Hello World"))
{
UI.ShowToast("Hello World");
}
});
} }
public static void Main() public static void Main()