feat(examples): add hook subscription to example mods
- JavaScript example now subscribes to greg.PLAYER.CoinChanged hook - Python example simplified and updated to use hook system - Go example restructured with proper hook callback implementation - Rust example extended with on_hook API usage and cleanup functions - All examples now demonstrate bidirectional communication with the core system
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
// example_mod/main.go
|
||||||
package main
|
package main
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -23,9 +24,10 @@ typedef struct {
|
|||||||
} GregModInfo;
|
} GregModInfo;
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
import "unsafe"
|
import (
|
||||||
|
"fmt"
|
||||||
var api *C.GregCoreAPI
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
//export greg_mod_info
|
//export greg_mod_info
|
||||||
func greg_mod_info() C.GregModInfo {
|
func greg_mod_info() C.GregModInfo {
|
||||||
@@ -39,19 +41,34 @@ func greg_mod_info() C.GregModInfo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//export onHookCallback
|
||||||
|
func onHookCallback(hookName, trigger, jsonData *C.char) {
|
||||||
|
hookNameStr := C.GoString(hookName)
|
||||||
|
triggerStr := C.GoString(trigger)
|
||||||
|
jsonDataStr := C.GoString(jsonData)
|
||||||
|
|
||||||
|
fmt.Printf("Go Hook received: %s (Trigger: %s) - Data: %s\n", hookNameStr, triggerStr, jsonDataStr)
|
||||||
|
}
|
||||||
|
|
||||||
//export greg_mod_init
|
//export greg_mod_init
|
||||||
func greg_mod_init(api_ptr *C.GregCoreAPI) bool {
|
func greg_mod_init(api *GregCoreAPI) bool {
|
||||||
api = api_ptr
|
// Subscribe to a hook
|
||||||
msg := C.CString("Go Mod Initialized!")
|
hookName := C.CString("greg.PLAYER.CoinChanged")
|
||||||
defer C.free(unsafe.Pointer(msg))
|
defer C.free(unsafe.Pointer(hookName))
|
||||||
C.bridge_log_info(api.log_info, msg)
|
|
||||||
|
api.OnHook(hookName, (unsafe.Pointer)(C.onHookCallback))
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper to call C function pointers
|
//export greg_mod_update
|
||||||
//go:uintptrescapes
|
func greg_mod_update(dt float32) {
|
||||||
func callLog(fn unsafe.Pointer, msg *C.char) {
|
// Update logic
|
||||||
// This requires cgo bridge helpers usually
|
}
|
||||||
|
|
||||||
|
//export greg_mod_shutdown
|
||||||
|
func greg_mod_shutdown() {
|
||||||
|
fmt.Println("Go Example Mod shutdown.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {}
|
func main() {}
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
// Plugins/Js/ExampleMod.js
|
||||||
|
greg.logInfo("JS Example Mod initializing...");
|
||||||
|
|
||||||
|
// Subscribe to the player's coin changed hook
|
||||||
|
greg.on("greg.PLAYER.CoinChanged", (payload) => {
|
||||||
|
const amount = payload.Data.Amount;
|
||||||
|
const total = payload.Data.Total;
|
||||||
|
greg.logInfo(`JS received money update: ${amount} (Total: ${total})`);
|
||||||
|
|
||||||
|
// Fire a custom hook back
|
||||||
|
greg.fire("greg.CUSTOM.JsResponse", {
|
||||||
|
msg: "JS heard that!",
|
||||||
|
received_total: total
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
greg.logInfo("JS Example Mod initialized!");
|
||||||
@@ -1,17 +1,24 @@
|
|||||||
|
# example_mod/main.py
|
||||||
def on_init():
|
def on_init():
|
||||||
greg.log_info("Python Example Mod initialized!")
|
greg.log_info("Python Example Mod initialized!")
|
||||||
greg.show_notification("Python Mod Active")
|
|
||||||
|
# Subscribe to coin changed hook
|
||||||
|
def on_coin_changed(payload):
|
||||||
|
amount = payload["data"]["Amount"]
|
||||||
|
total = payload["data"]["Total"]
|
||||||
|
greg.log_info(f"Python received money update: {amount} (Total: {total})")
|
||||||
|
|
||||||
|
# Fire a custom hook back
|
||||||
|
greg.fire("greg.CUSTOM.PythonResponse", {
|
||||||
|
"msg": "Python heard that!",
|
||||||
|
"received_total": total
|
||||||
|
})
|
||||||
|
|
||||||
|
greg.on("greg.PLAYER.CoinChanged", on_coin_changed)
|
||||||
|
|
||||||
def on_update(dt):
|
def on_update(dt):
|
||||||
# dt is deltaTime
|
# Update logic
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def on_event(event_id, data):
|
|
||||||
if event_id == 100: # MoneyChanged
|
|
||||||
greg.log_info("Money changed! Current: " + str(greg.get_player_money()))
|
|
||||||
|
|
||||||
def on_scene_loaded(name):
|
|
||||||
greg.log_info("Entered scene: " + name)
|
|
||||||
|
|
||||||
def on_shutdown():
|
def on_shutdown():
|
||||||
greg.log_info("Python Mod shutting down.")
|
greg.log_info("Python Example Mod shutdown.")
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ pub struct GregCoreAPI {
|
|||||||
pub log_warning: extern "C" fn(*const c_char),
|
pub log_warning: extern "C" fn(*const c_char),
|
||||||
pub log_error: extern "C" fn(*const c_char),
|
pub log_error: extern "C" fn(*const c_char),
|
||||||
pub get_player_money: extern "C" fn() -> f64,
|
pub get_player_money: extern "C" fn() -> f64,
|
||||||
|
pub on_hook: extern "C" fn(*const c_char, *const c_void),
|
||||||
// ... restliche Felder
|
// ... restliche Felder
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,17 +38,32 @@ pub extern "C" fn greg_mod_info() -> GregModInfo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Example of a Rust hook subscription
|
||||||
|
extern "C" fn on_hook_callback(hook_name: *const i8, trigger: *const i8, json_data: *const i8) {
|
||||||
|
let hook_name = unsafe { CStr::from_ptr(hook_name).to_string_lossy() };
|
||||||
|
let trigger = unsafe { CStr::from_ptr(trigger).to_string_lossy() };
|
||||||
|
let json_data = unsafe { CStr::from_ptr(json_data).to_string_lossy() };
|
||||||
|
|
||||||
|
println!("Rust Hook received: {} (Trigger: {}) - Data: {}", hook_name, trigger, json_data);
|
||||||
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn greg_mod_init(api: *const GregCoreAPI) -> bool {
|
pub extern "C" fn greg_mod_init(api: *const GregCoreAPI) -> bool {
|
||||||
unsafe {
|
let api = unsafe { &*api };
|
||||||
API = Some(&*api);
|
|
||||||
let msg = CString::new("Rust Mod Initialisiert!").unwrap();
|
// Subscribe to a hook
|
||||||
((*api).log_info)(msg.as_ptr());
|
let hook_name = CString::new("greg.PLAYER.CoinChanged").unwrap();
|
||||||
}
|
(api.on_hook)(hook_name.as_ptr(), on_hook_callback as *const ());
|
||||||
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn greg_mod_update(dt: f32) {
|
pub extern "C" fn greg_mod_update(dt: f32) {
|
||||||
// Logik pro Frame
|
// Update logic
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn greg_mod_shutdown() {
|
||||||
|
println!("Rust Example Mod shutdown.");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user