https://github.com/kasukusakura/jvmhookframework
[WIP] Framework for jvm hooking
https://github.com/kasukusakura/jvmhookframework
framework hook hooks-api java jvm
Last synced: 2 months ago
JSON representation
[WIP] Framework for jvm hooking
- Host: GitHub
- URL: https://github.com/kasukusakura/jvmhookframework
- Owner: KasukuSakura
- License: mit
- Created: 2021-06-26T04:56:31.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2021-07-14T12:53:29.000Z (almost 4 years ago)
- Last Synced: 2025-04-04T08:11:37.657Z (3 months ago)
- Topics: framework, hook, hooks-api, java, jvm
- Language: Java
- Homepage:
- Size: 307 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Jvm Hook Framework
[简体中文](README.zh.md)
----
# Desc
`JVM Hook Framework` is designed for injecting java classes with friendly apis using `SpongePowered/mixin`
[](https://search.maven.org/search?q=g:io.github.karlatemp.jhf)
# Using
You need add `Maven Central` and `https://repo.spongepowered.org/repository/maven-public/` into your repositories.
```groovy
plugins {
id 'java-library'
}repositories {
mavenCentral()
maven { url = 'https://repo.spongepowered.org/repository/maven-public/' }
maven { url = 'https://files.minecraftforge.net/maven/' }
}dependencies {
api 'io.github.karlatemp.jhf:api:'
}
```## Developing a plugin
### Project setup
- Create a new class named `myjhfplugin.Startup` (or other you like)
- Create a new file in project resources named `jhf-main.txt`
- Write `myjhfplugin.Start` in `jhf-main.txt`
- Add a **static** method in `myjhfplugin.Startup` named **launch** with **no parameter** and **void return type**
> `private static void launch() throws Throwable {}`### Using mixin
You need create a class module of class you want to inject.
```java
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;@Mixin(targets = "testunit.RunTester")
public class MixinRunTester {
@Overwrite
public static void error() {
}
}
```Then, you need create a mixin configuration file in your project resources. Suggested path format
is `${project-name}/mixin/${config-name}.json````json5
{
"minVersion": 0,
"package": "tester.mixin",
// The package storing mixin classes
"mixins": [
"MixinRunTester"
],
// Class name that relative to ${package}
"client": [],
// no effect
"server": []
// no effect
}
```In your `Plugin Main Point` add following code
```java
public class MainPlugin {
private static void launch() throws Throwable {
Mixins.addConfiguration("myplugin/mixin/main.json");
}
}```
> Example: [MixinRunTester](tester/src/main/java/tester/mixin/MixinRunTester.java)
## Listen `JavaLangReflectInvokeEvent`
`JavaLangReflectInvokeEvent` will broadcast when application invoking reflection of `java.lang.reflect`.
```java
JavaLangReflectInvokeEvent.EVENT_LINE.register(EventPriority.NORMAL,event-> {
if (event.target instanceof Method) {
if (event.target.getDeclaringClass().getName().endsWith("EskPermissionDenied")) {
throw new IllegalAccessException("Permission Denied");
}
}
});
```## Modify bytecodes
When a new class before loading, a `TransformBytecodeEvent` will broadcast to event-pipeline.
Listen and modify `TransformBytecodeEvent.bytecode` to change bytecode with the lowest way.
## Find an example
The folder `tester` is a plugin for `JvmHookFramework Testing`. You can refer to it to write your own plugin.
## Plugin configuration
JHF using `SpongePowered/Configuration` to manage configurations. Using it by
```java
public class Main {
public static final File DATA_FOLDER = JvmHookFramework.getInstance().getDataFolder("my-plugin");
public static final HoconConfigurationLoader LOADER = JvmHookFramework.getInstance().newConfigLoader(
ConfigurationOptions.defaults()
.shouldCopyDefaults(true)
,
new File(DATA_FOLDER).toPath()
);
}
```