Skip to content

Optional Features

Owen edited this page Apr 18, 2024 · 12 revisions

Allow host to control syncing.

Some mod authors may want to let the host decide if the clients can use their own configs.
To do this, you can use the EnableHostSyncControl method.

Note

This feature is only available in v2.2.0 and above.

[DataMember] public SyncedEntry<bool> SYNC_TO_CLIENTS { get; private set; }

public void ExampleConfig(ConfigFile cfg) : base("MyModName") {
    ConfigManager.Register(this);
    
    SYNC_TO_CLIENTS = cfg.BindSyncedEntry(ConfigCategory.GENERAL, "bSyncToClients", true,
            "As the host, should clients be forced to use our config values?\n" +
            "Setting this to `false` will allow clients to use their own config."
    );
    
    // This MUST be called after binding the entry.
    EnableHostSyncControl(SYNC_TO_CLIENTS);
}

Subcribing to Events

CSync includes a decent amount of handy events you can hook into if needed.
Here is an example of how you would subscribe to one of these.

NOTE: You can subscribe to events anywhere within the constructor - the example below is personal preference.

public ExampleConfig(ConfigFile cfg) : base("MyModName") {
    ConfigManager.Register(this);

    SyncComplete += DoSomethingAfterSync;
    SyncReverted += DoSomethingAfterRevert;

    SyncRequested += DoSomethingOnRequest;
    SyncReceived += DoSomethingOnReceive;

    // Bind entries etc.
}

Event List

While some of these were introduced in v2.x.x, they were of the EventHandler type and less intuitive compared to Actions.
Essentially, the syntax (object sender, EventArgs e) is no longer needed.

The examples below should show you how to easily use the new v3.0.0 events.

SyncComplete

Invoked when synchronization finishes, even if the host disabled it.
To subscribe to this event, a bool parameter is required.

void DoSomethingAfterSync(bool success) {
    if (!success) {
        // Handle fail
        return;
    }

    // Succeeded
}
SyncReverted

Invoked whenever the client instance is set back to default and is no longer synced.
No parameters are required to subscribe to this event.

void DoSomethingAfterRevert() {
   // Your logic here
}
SyncRequested

Invoked on the host when a client requests to sync.
To subscribe to this event, a ulong parameter is required.

void DoSomethingOnRequest(ulong clientId) {
    // Your logic here
}
SyncReceived

Invoked on the client when they receive the host config.
No parameters are required to subscribe to this event.

void DoSomethingOnReceive() {
    // Your logic here
}

Resyncing the config

Warning

This feature is only available in v3.1.0 and above, which is not yet released.

Once the initial config synchronization has finished after the client joins, you are able to do this again for your own mod.

While the initial sync will usually be enough, there may be edge cases where this is useful.
For example if the clients fall out of sync with the host for whatever reason, you can simply call it like so.

[HarmonyPostfix]
[HarmonyPatch("ExampleMethod")]
static void DoSomething() {
    // Your code here

    Plugin.Config.Resync();
}