Valheim-Modding / Jotunn

Jötunn, the Valheim Library.
https://valheim-modding.github.io/Jotunn/
MIT License
128 stars 40 forks source link

Synchronization Events Pre-Sync and on Dedicated Server #414

Closed searica closed 1 year ago

searica commented 1 year ago

New Synchronization Event

This pull request adds a new event, OnSyncingConfiguration, that triggers just before applying the synchronized values (on both client and server). This event has it's own class for event args which is currently empty but I thought it was best to keep the same design pattern as the OnConfigurationSynchronized event and prevent changes in API if other devs want to add argument to the event.

Modified OnConfigurationSynchronized to Fire on Server

The event OnConfigurationSynchronized has been altered to fire on both the client and the dedicated server after the configuration has been synchronized. I testing the effects of doing this using a mod list that had two different mods that used Jotunn to sync configurations and one that used Blaxxun's ServerSync.

I set up the two Jotunn dependant mods to save their configuration files whenever the OnConfigurationSynchronized event fired and set up the file-watcher in each mod to reload the configuration file when it was saved so the mod could be updated. when Both of the Jotunn dependent mods used a file-watcher that would reload the configuration file when it was changed and an in-game configuration manager watcher that would save the configuration file when the in-game manager was closed.

I tested changing the configuration via the in-game manager, by editing the server configuration file, and editing the local client configuration file. For all test cases the behaviour on the client-side was identical to when OnConfigurationSynchronized only fired on the client. For the server, configuration changes were saved just like they were on client whenever "OnConfigurationSynchronized" fired on the server. As far as I can tell from those tests firing the "OnConfigurationSynchronized" even on the server does not result in any sort of infinite synchronization loop or duplicate synchronizations for existing mods.

Added Synced PluginGUIDs argument to OnConfigurationSynchronized Event

The OnConfigurationSynchronized has a new argument that is a HashSet containing the PluginGUID for each plugin that received configuration data from the SynchronizationManager. This is so that plugins can check if they actually had their configuration updated by checking if the HashSet contains their plugin's GUID. This also required caching the PluginGUID for each custom configuration file that is registered so that changes to custom configuration files are tracked appropriately.