From 08d1756a938d512e5f26dd5ca47482fdddb35b6f Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Tue, 15 Oct 2024 19:10:32 -0400 Subject: [PATCH] Adding hook returnType This PR affect Docs.json file generation only Add the new ReturnType parameter for the Hook in Docs.json. This field is needed for the Oxide.Docs project to be able to add a valid return value to function --- src/Docs/DocsHook.cs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/Docs/DocsHook.cs b/src/Docs/DocsHook.cs index 83fe7a5..a111121 100644 --- a/src/Docs/DocsHook.cs +++ b/src/Docs/DocsHook.cs @@ -22,6 +22,7 @@ public class DocsHook public string HookName { get; set; } public string HookDescription { get; set; } public Dictionary HookParameters { get; set; } + public string ReturnType { get; set; } public ReturnBehavior ReturnBehavior { get; set; } = ReturnBehavior.Continue; public string TargetType { get; set; } public string Category { get; set; } @@ -45,6 +46,7 @@ public DocsHook(Hook hook, MethodDefinition methodDef, string targetDirectory) Type = HookType.Simple; ReturnBehavior = simpleHook.ReturnBehavior; HookParameters = GetHookArguments(simpleHook, methodDef); + ReturnType = GetReturnType(simpleHook, methodDef); break; case Modify modifyHook: @@ -165,6 +167,42 @@ private Dictionary GetHookArguments(Simple hook, MethodDefinitio return hookArguments; } + private string GetReturnType(Simple hook, MethodDefinition method) + { + string returnType = ""; + try + { + switch (hook?.ReturnBehavior) + { + case ReturnBehavior.Continue: + returnType = "void"; + break; + + case ReturnBehavior.ExitWhenNonNull: + if (hook?.Signature.ReturnType == "System.Void") returnType = "object"; + else returnType = Utility.TransformType(hook?.Signature.ReturnType); + break; + + case ReturnBehavior.ExitWhenValidType: + if (hook?.Signature.ReturnType == "System.Void") returnType = "object"; + else returnType = Utility.TransformType(hook?.Signature.ReturnType); + break; + + case ReturnBehavior.ModifyRefArg: + if (hook?.Signature.ReturnType == "System.Void") returnType = "object"; + else returnType = Utility.TransformType(hook?.Signature.ReturnType); + break; + + case ReturnBehavior.UseArgumentString: + string[] args = Utility.ParseArgumentString(hook.ArgumentString, out string returnValue); + returnType = Utility.TransformType(GetArgStringType(returnValue, method, out string argName)); + break; + } + } catch { returnType = "ERROR"; } + + return returnType; + } + //Doesn't work if I use the Decompiler class so just do this for now private static string GetSourceCode(MethodDefinition methodDefinition) {