Skip to content

Commit

Permalink
implement late binding patches
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin committed May 18, 2024
1 parent 634f491 commit a2721bd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
27 changes: 25 additions & 2 deletions flowerful.patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,26 @@ function FindPatch(obj: Patchable, method: string)
obj: obj,
methodName: method,
prefixes: [],
postfixes: []
postfixes: [],
applied: false,
}

patches.push(patch);
return patch;
}

function Apply(patch: FlowerPatch)
/**
* Binds a patch to an object. All patches are accumulated even after initial binding.
* @param patch
* @returns false if this patch has already been bound
*/
function Apply(patch: FlowerPatch): boolean
{

/** Only apply patches once ever */
if (patch.applied)
return false;

/* eslint-disable-next-line @typescript-eslint/ban-types */
const orig = patch.obj[patch.methodName] as Function;

Expand All @@ -49,6 +60,8 @@ function Apply(patch: FlowerPatch)
WriteDebug(`Running detour for ${patch.methodName}`);
// <-- this = obj

patch = FindPatch(patch.obj, patch.methodName)!;

WriteDebug(`Prefixes ${patch.prefixes.length}`);
//patch.prefixes.forEach(prefix => prefix.call(patch.obj, ...args));
//Allow ending the detour early
Expand Down Expand Up @@ -96,8 +109,17 @@ function Apply(patch: FlowerPatch)
}

patch.obj[patch.methodName] = wrapper.bind(patch.obj);
return true;
}

/**
* Registers a new patch with flower
* @param obj any object that contains a method
* @param methodName the string name of the method to patch
* @param patch a function that runs when the method is called
* @param isPrefix if this should run before the method (afterward otherwise)
* @returns true on success
*/
export function RegisterPatch(obj: Patchable, methodName: string, patch: PatchFn, isPrefix: boolean)
{

Expand All @@ -115,5 +137,6 @@ export function RegisterPatch(obj: Patchable, methodName: string, patch: PatchFn
accum.postfixes.push(patch);
}

Apply(accum);
return true;
}
1 change: 1 addition & 0 deletions flowerful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ async function LoadAllPlugins()

}

//This should be completely redundant now
ApplyAllPatches();
}

Expand Down

1 comment on commit a2721bd

@RobynLlama
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adds #10

Please sign in to comment.