From a446ee282a476e569083f558a15ca288fa352906 Mon Sep 17 00:00:00 2001 From: aristocratos <71286392+hammzat@users.noreply.github.com> Date: Tue, 24 Dec 2024 22:31:57 +0300 Subject: [PATCH] Hook Documents improvement --- docs/hooks/[category]/[hook].paths.ts | 54 ++++++++++++++++++++++----- entities/hooks/hook.d.ts | 1 + util/hooks.ts | 14 +++++++ 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/docs/hooks/[category]/[hook].paths.ts b/docs/hooks/[category]/[hook].paths.ts index c8389ca..3cf1f1d 100644 --- a/docs/hooks/[category]/[hook].paths.ts +++ b/docs/hooks/[category]/[hook].paths.ts @@ -119,12 +119,11 @@ function getUsageReturn(hookData: IHook) { } if (hookData.ReturnBehavior === ReturnBehaviour.ExitWhenNonNull) { - return "* Return a non-null value or bool to override default behavior"; + return `* Return a non-null value or ${hookData.NeedReturnType ? `**${hookData.NeedReturnType}**` : 'bool'} to override default behavior`; } - //TODO: Get the return type of the hook if (hookData.ReturnBehavior === ReturnBehaviour.UseArgumentString) { - return "* Return TYPE to prevent default behavior"; + return `* Return **${hookData.NeedReturnType || 'TYPE'}** to prevent default behavior`; } return "* No return behavior"; @@ -148,13 +147,48 @@ function getExamplesMarkdown(hooks: IHook[]) { }, [] as IHook[]); for (const hook of hooks) { - output += `\`\`\`csharp`; - //TODO: Use proper return type instead of void - output += `\nprivate void ${hook.HookName}( ${getArgumentString(hook.HookParameters)} )`; - output += `\n{`; - output += `\n Puts( "${hook.HookName} works!" );`; - output += `\n}`; - output += `\n\`\`\`\n`; + const returnType = hook.NeedReturnType || "object"; + + switch (hook.ReturnBehavior) { + case ReturnBehaviour.Continue: + output += `\`\`\`csharp`; + output += `\nprivate void ${hook.HookName}( ${getArgumentString(hook.HookParameters)} )`; + output += `\n{`; + output += `\n Puts( "${hook.HookName} works!" );`; + output += `\n}`; + output += `\n\`\`\`\n`; + break; + + case ReturnBehaviour.ExitWhenValidType: + output += `\`\`\`csharp`; + output += `\nprivate object ${hook.HookName}( ${getArgumentString(hook.HookParameters)} )`; + output += `\n{`; + output += `\n Puts( "${hook.HookName} works!" );`; + output += `\n return false; // Or return null`; + output += `\n}`; + output += `\n\`\`\`\n`; + break; + + case ReturnBehaviour.ExitWhenNonNull: + output += `\`\`\`csharp`; + output += `\nprivate ${returnType} ${hook.HookName}( ${getArgumentString(hook.HookParameters)} )`; + output += `\n{`; + output += `\n Puts( "${hook.HookName} works!" );`; + output += `\n return null;`; + output += `\n}`; + output += `\n\`\`\`\n`; + break; + + case ReturnBehaviour.UseArgumentString: + output += `\`\`\`csharp`; + output += `\nprivate ${returnType}? ${hook.HookName}( ${getArgumentString(hook.HookParameters)} )`; + output += `\n{`; + output += `\n Puts( "${hook.HookName} works!" );`; + output += `\n return ${hook.NeedReturnType ? `default(${hook.NeedReturnType})` : "null"}; // Or return null`; + output += `\n}`; + output += `\n\`\`\`\n`; + break; + } } return output; diff --git a/entities/hooks/hook.d.ts b/entities/hooks/hook.d.ts index 0753e47..0e9e0d4 100644 --- a/entities/hooks/hook.d.ts +++ b/entities/hooks/hook.d.ts @@ -8,6 +8,7 @@ export default interface IHook { HookParameters?: {[key: string]: string}; ReturnBehavior: ReturnBehaviour; TargetType: string; + NeedReturnType?: string; Category: string; MethodData: { MethodName: string; diff --git a/util/hooks.ts b/util/hooks.ts index ea207e7..083f7c0 100644 --- a/util/hooks.ts +++ b/util/hooks.ts @@ -8,12 +8,26 @@ export function getHookJson() { return hooks.Hooks.filter(hook => hook.Category !== "_Patches" && !hook.HookName.includes("[")); } +// Get the return type of the hook from the CodeAfterInjection +function getReturnTypeFromCodeAfterInjection(hook: IHook): string | null { + if (!hook.CodeAfterInjection) return null; + + const code = hook.CodeAfterInjection.replace(/global::/g, ''); + const typeMatch = /\breturnvar is ([a-zA-Z0-9:\.]+)/.exec(code); + if (typeMatch && typeMatch[1]) return typeMatch[1]; + return null; +} + export function getGroupedHooks() { const hooksJson = getHookJson(); var out = {} as { [key: string]: { [key: string]: IHook[] } }; hooksJson.forEach((hook) => { + const returnType = getReturnTypeFromCodeAfterInjection(hook); + if (returnType) { + hook.NeedReturnType = returnType; + } if (!out[hook.Category]) { out[hook.Category] = {}; }