-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow to config ```json "EnableLogger":true ```
- Loading branch information
Showing
10 changed files
with
304 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Hosihikari.NativeInterop.Hook.ObjectOriented; | ||
using Hosihikari.VanillaScript.QuickJS; | ||
using Hosihikari.VanillaScript.QuickJS.Types; | ||
|
||
namespace Hosihikari.VanillaScript.Hook.JsLog; | ||
|
||
internal class ContextObjectBindPrint : HookBase<ContextObjectBindPrint.HookDelegate> | ||
{ | ||
internal unsafe delegate JsValue HookDelegate(void* contextObject, JsContext* ctx); | ||
|
||
public ContextObjectBindPrint() | ||
: base("_ZN9Scripting7QuickJS13ContextObject10_bindPrintEP9JSContext") { } | ||
|
||
public override unsafe HookDelegate HookedFunc => | ||
(contextObject, ctx) => | ||
{ | ||
if (Config.Data.EnableLogger) | ||
{ | ||
using var globalObject = Native.JS_GetGlobalObject(ctx); | ||
JsLog.Bind(ctx, globalObject.Value); | ||
return globalObject.Value; | ||
} | ||
return Original.Invoke(contextObject, ctx); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using Hosihikari.VanillaScript.QuickJS; | ||
using Hosihikari.VanillaScript.QuickJS.Extensions; | ||
using Hosihikari.VanillaScript.QuickJS.Helper; | ||
using Hosihikari.VanillaScript.QuickJS.Types; | ||
|
||
namespace Hosihikari.VanillaScript.Hook.JsLog; | ||
|
||
internal class JsLog | ||
{ | ||
public static unsafe void Bind(JsContext* ctx, JsValue globalObject) | ||
{ | ||
using var consoleInstance = Native.JS_NewObject(ctx, false); | ||
var console = consoleInstance.Value; | ||
#region Trace | ||
console.DefineFunction(ctx, "trace", &PrintTrace, 1, flags: JsPropertyFlags.Normal); | ||
[UnmanagedCallersOnly] | ||
static JsValue PrintTrace(JsContext* ctx, JsValue thisObj, int argCount, JsValue* argvIn) | ||
{ | ||
try | ||
{ | ||
var (file, line) = GetJsSourceInfo(ctx); | ||
Log.Logger.Trace( | ||
ParseLog(ctx, new ReadOnlySpan<JsValue>(argvIn, argCount)), | ||
sourceFile: file, | ||
sourceLine: line | ||
); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Logger.Error("PrintTrace", "Invoke Failed", ex); | ||
} | ||
return JsValueCreateHelper.Undefined; | ||
} | ||
#endregion | ||
#region Info | ||
globalObject.DefineFunction(ctx, "print", &PrintInfo, 1, flags: JsPropertyFlags.Normal); | ||
console.DefineFunction(ctx, "log", &PrintInfo, 1, flags: JsPropertyFlags.Normal); | ||
console.DefineFunction(ctx, "info", &PrintInfo, 1, flags: JsPropertyFlags.Normal); | ||
[UnmanagedCallersOnly] | ||
static JsValue PrintInfo(JsContext* ctx, JsValue thisObj, int argCount, JsValue* argvIn) | ||
{ | ||
try | ||
{ | ||
Log.Logger.Info(ParseLog(ctx, new ReadOnlySpan<JsValue>(argvIn, argCount))); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Logger.Error("PrintInfo", "Invoke Failed", ex); | ||
} | ||
return JsValueCreateHelper.Undefined; | ||
} | ||
#endregion | ||
#region Debug | ||
console.DefineFunction(ctx, "debug", &PrintDebug, 1, flags: JsPropertyFlags.Normal); | ||
[UnmanagedCallersOnly] | ||
static JsValue PrintDebug(JsContext* ctx, JsValue thisObj, int argCount, JsValue* argvIn) | ||
{ | ||
try | ||
{ | ||
Log.Logger.Debug(ParseLog(ctx, new ReadOnlySpan<JsValue>(argvIn, argCount))); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Logger.Error("PrintDebug", "Invoke Failed", ex); | ||
} | ||
return JsValueCreateHelper.Undefined; | ||
} | ||
#endregion | ||
#region Warn | ||
console.DefineFunction(ctx, "warn", &PrintWarn, 1, flags: JsPropertyFlags.Normal); | ||
[UnmanagedCallersOnly] | ||
static JsValue PrintWarn(JsContext* ctx, JsValue thisObj, int argCount, JsValue* argvIn) | ||
{ | ||
try | ||
{ | ||
Log.Logger.Warn(ParseLog(ctx, new ReadOnlySpan<JsValue>(argvIn, argCount))); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Logger.Error("PrintWarn", "Invoke Failed", ex); | ||
} | ||
return JsValueCreateHelper.Undefined; | ||
} | ||
#endregion | ||
#region Error | ||
console.DefineFunction(ctx, "error", &PrintError, 1, flags: JsPropertyFlags.Normal); | ||
[UnmanagedCallersOnly] | ||
static JsValue PrintError(JsContext* ctx, JsValue thisObj, int argCount, JsValue* argvIn) | ||
{ | ||
try | ||
{ | ||
var (file, line) = GetJsSourceInfo(ctx); | ||
Log.Logger.Error( | ||
ParseLog(ctx, new ReadOnlySpan<JsValue>(argvIn, argCount)), | ||
sourceFile: file, | ||
sourceLine: line | ||
); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Logger.Error("PrintError", "Invoke Failed", ex); | ||
} | ||
return JsValueCreateHelper.Undefined; | ||
} | ||
#endregion | ||
#region Assert | ||
//todo Assert | ||
//console.DefineFunction(ctx, "assert", &Assert, 2, flags: JsPropertyFlags.Normal); | ||
|
||
//[UnmanagedCallersOnly] | ||
//static JsValue Assert(JsContext* ctx, JsValue thisObj, int argCount, JsValue* argvIn) | ||
//{ | ||
// var argv = new ReadOnlySpan<JsValue>(argvIn, argCount); | ||
// if (argv[0].IsFalsey(ctx)) | ||
// { | ||
// Log.Logger.Error(ParseLog(ctx, argv.Slice(1))); | ||
// } | ||
// return JsValueCreateHelper.Undefined; | ||
//} | ||
#endregion | ||
#region Clear | ||
console.DefineFunction(ctx, "clear", &Clear, 0, flags: JsPropertyFlags.Normal); | ||
[UnmanagedCallersOnly] | ||
static JsValue Clear(JsContext* ctx, JsValue thisObj, int argCount, JsValue* argvIn) | ||
{ | ||
Console.Clear(); | ||
return JsValueCreateHelper.Undefined; | ||
} | ||
#endregion | ||
globalObject.DefineProperty(ctx, "console", console, flags: JsPropertyFlags.Normal); | ||
} | ||
|
||
static unsafe (string file, int line) GetJsSourceInfo(JsContext* ctx) | ||
{ | ||
try //get full stack from Error | ||
{ | ||
Native.JS_ThrowError(ctx, ""); | ||
var error = Native.JS_GetException(ctx); | ||
var stack = error.Value.GetStringProperty(ctx, "stack"); | ||
var lines = stack.Split('\n'); | ||
var file = lines[1].Split(' ')[1]; | ||
var line = int.Parse(lines[1].Split(' ')[2]); | ||
return (file, line); | ||
} | ||
catch //get only file name from JS_GetScriptOrModuleName | ||
{ | ||
var file = Native.JS_GetScriptOrModuleName(ctx, 1); | ||
return (file, -1); | ||
} | ||
} | ||
|
||
static unsafe string ParseLog(JsContext* ctx, ReadOnlySpan<JsValue> argv) | ||
{ | ||
var sb = new StringBuilder(); | ||
foreach (var arg in argv) | ||
{ | ||
sb.Append(arg.ToString(ctx)); | ||
if (arg.Data.tag == JsTag.Object && Native.JS_IsError(ctx, arg)) | ||
{ | ||
sb.AppendLine(); | ||
sb.AppendLine(arg.GetStringProperty(ctx, "stack")); | ||
} | ||
else | ||
{ | ||
sb.Append(" "); | ||
} | ||
} | ||
return sb.ToString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Hosihikari.NativeInterop.Hook.ObjectOriented; | ||
|
||
namespace Hosihikari.VanillaScript.Hook | ||
{ | ||
internal class RequestReload : HookBase<RequestReload.HookDelegate> | ||
{ | ||
internal unsafe delegate void HookDelegate(void* minecraft); | ||
|
||
public RequestReload() | ||
: base("_ZN9Minecraft21requestResourceReloadEv") { } | ||
|
||
public override unsafe HookDelegate HookedFunc => | ||
minecraft => | ||
{ | ||
Original.Invoke(minecraft); | ||
Config.Reload(); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.