Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion CSync/Lib/SyncedConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,30 @@ public class SyncedConfig<T>(string guid) : SyncedInstance<T>, ISynchronizable w
static void LogErr(string str) => Plugin.Logger.LogError(str);
static void LogDebug(string str) => Plugin.Logger.LogDebug(str);

/// <summary>
/// Invoked on the host when a client requests to sync.
/// </summary>
[field:NonSerialized] public event EventHandler SyncRequested;
internal void OnSyncRequested() => SyncRequested?.Invoke(this, EventArgs.Empty);

/// <summary>
/// Invoked on the client when they receive the host config.
/// </summary>
[field:NonSerialized] public event EventHandler SyncReceived;
internal void OnSyncReceived() => SyncReceived?.Invoke(this, EventArgs.Empty);

/// <summary>
/// The mod name or abbreviation. After being given to the constructor, it cannot be changed.
/// </summary>
public readonly string GUID = guid;

internal SyncedEntry<bool> SYNC_TO_CLIENTS { get; private set; } = null;

/// <summary>
/// Allow the host to control whether clients can use their own config.
/// This MUST be called after binding the entry parameter.
/// </summary>
/// <param name="hostSyncControlOption">The entry for the host to use in your config file.</param>
protected void EnableHostSyncControl(SyncedEntry<bool> hostSyncControlOption) {
SYNC_TO_CLIENTS = hostSyncControlOption;

Expand Down Expand Up @@ -51,8 +68,8 @@ void RequestSync() {
}

internal void OnRequestSync(ulong clientId, FastBufferReader _) {
// Only run if we are host/server.
if (!IsHost) return;
OnSyncRequested();

if (SYNC_TO_CLIENTS != null && SYNC_TO_CLIENTS == false) {
using FastBufferWriter s = new(IntSize, Allocator.Temp);
Expand Down Expand Up @@ -80,6 +97,8 @@ internal void OnRequestSync(ulong clientId, FastBufferReader _) {
}

internal void OnReceiveSync(ulong _, FastBufferReader reader) {
OnSyncReceived();

if (!reader.TryBeginRead(IntSize)) {
LogErr($"{GUID} - Config sync error: Could not begin reading buffer.");
return;
Expand Down