Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hook Documents improvement #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions docs/hooks/[category]/[hook].paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions entities/hooks/hook.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default interface IHook {
HookParameters?: {[key: string]: string};
ReturnBehavior: ReturnBehaviour;
TargetType: string;
NeedReturnType?: string;
Category: string;
MethodData: {
MethodName: string;
Expand Down
14 changes: 14 additions & 0 deletions util/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {};
}
Expand Down