jpw1991 / chebs-necromancy

Cheb's Necromancy adds Necromancy to Valheim via craftable wands and structures. Minions will follow you, guard your base, and perform menial tasks.
The Unlicense
10 stars 4 forks source link

Add LIVE Server Sync #42

Closed Dracbjorn closed 1 year ago

Dracbjorn commented 1 year ago

Description

When a config is changed during run-time, it should automatically update on the server without requiring a restart to reload the config.

Is the feature related to a problem?

No.

Solutions

Use the Jotunn SynchronizationManager:

https://valheim-modding.github.io/Jotunn/tutorials/config.html#config-synced-event

SynchronizationManager.OnConfigurationSynchronized += (obj, attr) =>
{
    if (attr.InitialSynchronization)
    {
        Jotunn.Logger.LogMessage("Initial Config sync event received");
    }
    else
    {
        Jotunn.Logger.LogMessage("Config sync event received");
    }
};
jpw1991 commented 1 year ago

I think this is fine as a 1.4.X release because if the client has the code, but the server is outdated, it will simply never request a sync. So nothing should break.

Dracbjorn commented 1 year ago

So, I couldn't get the Jotuun version to work. Not sure why, so I attempted a different method and it works. However, I'm sure the Jotunn method is probably far simpler. I couldn't figure out exactly how to use it and where to use it. This is the alternative method I implemented:

        private void SetupWatcher()
        {
            FileSystemWatcher watcher = new FileSystemWatcher(BepInEx.Paths.ConfigPath, ConfigFileName);
            watcher.Changed += ReadConfigValues;
            watcher.Created += ReadConfigValues;
            watcher.Renamed += ReadConfigValues;
            watcher.IncludeSubdirectories = true;
            watcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
            watcher.EnableRaisingEvents = true;
        }

        private void ReadConfigValues(object sender, FileSystemEventArgs e)
        {
            if (!File.Exists(ConfigFileFullPath)) return;
            try
            {
                Jotunn.Logger.LogInfo("Read updated config values");
                Config.Reload();
            }
            catch
            {
                Jotunn.Logger.LogError($"There was an issue loading your {ConfigFileName}");
                Jotunn.Logger.LogError("Please check your config entries for spelling and format!");
            }
        }

Then I just call SetupWatcher(); at the end of the Awake() method.