From efa20e64215e0620ccfdc566993b3dadc013d8b4 Mon Sep 17 00:00:00 2001 From: Beu Date: Wed, 23 Aug 2023 20:52:30 +0200 Subject: [PATCH] use MLhook to detect update of the UIModules --- CountdownMover/info.toml | 7 ++++-- CountdownMover/main.as | 54 +++++++++++++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/CountdownMover/info.toml b/CountdownMover/info.toml index 08ba429..eb31c2d 100644 --- a/CountdownMover/info.toml +++ b/CountdownMover/info.toml @@ -2,5 +2,8 @@ name = "Countdown Mover" author = "Beu" category = "Overlay" -version = "1.0" -siteid = 431 \ No newline at end of file +version = "1.1" +siteid = 431 + +[script] +dependencies = ["MLHook"] \ No newline at end of file diff --git a/CountdownMover/main.as b/CountdownMover/main.as index 142d002..ac2f1a5 100644 --- a/CountdownMover/main.as +++ b/CountdownMover/main.as @@ -4,10 +4,39 @@ vec2 Setting_CountdownOffset = vec2(155. , 4.); const string C_Class_UIModules = 'component-modelibs-uimodules-module'; const string C_Id_Countdown = 'Race_Countdown'; +const string C_MLID_UIModuleUpdate = 'MLHook_CustomizableModule'; +const string C_ML_UIModuleUpdate = """ +main() { + declare netread Integer Net_LibUI3_CustomizableModule_PropertiesUpdate for Teams[0]; + declare Integer Last_PropertiesUpdate; + while (True) { + yield; + + if (Last_PropertiesUpdate != Net_LibUI3_CustomizableModule_PropertiesUpdate) { + Last_PropertiesUpdate = Net_LibUI3_CustomizableModule_PropertiesUpdate; + SendCustomEvent("MLHook_CustomizableModule_Update", []); + } + } +} +"""; + +class HookCustomizableModuleEvents: MLHook::HookMLEventsByType { + HookCustomizableModuleEvents() { + super(C_MLID_UIModuleUpdate); + } + + // override this method to avoid reload crash? + void OnEvent(MLHook::PendingEvent@ Event) override { + trace("CustomizableModule updated"); + G_Update = true; + } +} + +HookCustomizableModuleEvents@ HookEvents = null; string G_Last_ServerLogin = ""; uint G_Last_UILayers_Length = 0; CGameManialinkControl@ G_Control; -bool Update; +bool G_Update; // Search and return the CMlFrame of the Race_Countdown UIModule CGameManialinkControl@ GetControl(CGameManiaAppPlayground@ _ManiaApp) { @@ -28,10 +57,15 @@ CGameManialinkControl@ GetControl(CGameManiaAppPlayground@ _ManiaApp) { } void OnSettingsChanged() { - Update = true; + trace("Settings updated"); + G_Update = true; } void Main() { + @HookEvents = HookCustomizableModuleEvents(); + MLHook::RegisterMLHook(HookEvents, C_MLID_UIModuleUpdate + "_Update", true); + MLHook::InjectManialinkToPlayground(C_MLID_UIModuleUpdate, C_ML_UIModuleUpdate, true); + while(true) { CTrackMania@ App = cast(GetApp()); CTrackManiaNetwork@ Network = cast(App.Network); @@ -40,23 +74,31 @@ void Main() { if (Network !is null && ServerInfo !is null && ManiaApp !is null) { if (G_Last_ServerLogin != ServerInfo.ServerLogin || G_Last_UILayers_Length != ManiaApp.UILayers.Length) { + trace("Server or UILayers updated"); G_Last_ServerLogin = ServerInfo.ServerLogin; G_Last_UILayers_Length = ManiaApp.UILayers.Length; @G_Control = GetControl(ManiaApp); - Update = true; + G_Update = true; } - if (G_Control !is null && Update) { - Update = false; + if (G_Control !is null && G_Update) { + G_Update = false; G_Control.RelativePosition_V3 = Setting_CountdownOffset; } } else { G_Last_ServerLogin = ""; G_Last_UILayers_Length = 0; @G_Control = null; - Update = true; + G_Update = true; } sleep(1000); } } + +void OnDestroyed() { _Unload(); } +void OnDisabled() { _Unload(); } +void _Unload() { + trace('_Unload, unloading all hooks and removing all injected ML'); + MLHook::UnregisterMLHooksAndRemoveInjectedML(); +} \ No newline at end of file