Skip to content

Commit d3cdeec

Browse files
committed
add registration method for SyncedConfig2 and mark SyncedConfig overloads as obsolete
1 parent 5433b53 commit d3cdeec

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

CSync/Lib/ConfigManager.cs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,25 @@ internal static ConfigFile GetConfigFile(string relativePath)
7070
/// Register a config with CSync, making it responsible for synchronization.<br></br>
7171
/// After calling this method, all clients will receive the host's config upon joining.
7272
/// </summary>
73+
[Obsolete("Obsolete, use SyncedConfig2")]
7374
public static void Register<T>(T config) where T : SyncedConfig<T> {
75+
Register(config as SyncedConfig<T>);
76+
}
77+
78+
/// <summary>
79+
/// Register a config with CSync, making it responsible for synchronization.<br></br>
80+
/// After calling this method, all clients will receive the host's config upon joining.
81+
/// </summary>
82+
[Obsolete("Obsolete, use SyncedConfig2")]
83+
public static void Register<T>(SyncedConfig<T> config) where T : SyncedConfig<T> {
7484
if (config is null)
7585
{
7686
throw new ArgumentNullException(nameof(config), "Config instance is null, cannot register.");
7787
}
88+
if (config is not T)
89+
{
90+
throw new ArgumentException($"{config.GetType()} declares it extends {nameof(SyncedConfig<T>)}<{typeof(T)}>. It should be declared to extend {nameof(SyncedConfig<T>)}<{config.GetType()}>.", nameof(config));
91+
}
7892

7993
var assemblyQualifiedTypeName = typeof(T).AssemblyQualifiedName ?? throw new ArgumentException(nameof(config));
8094
var key = new InstanceKey(config.GUID, assemblyQualifiedTypeName);
@@ -84,19 +98,52 @@ public static void Register<T>(T config) where T : SyncedConfig<T> {
8498
InitialSyncHandlers.Add(key, config.OnInitialSyncCompleted);
8599
}
86100
catch (ArgumentException exc) {
87-
throw new InvalidOperationException($"Attempted to register config instance of type `{typeof(T)}`, but an instance has already been registered.", exc);
101+
throw new InvalidOperationException($"Attempted to register config instance of type `{typeof(T)}`, but an instance has already been registered with key {key}.", exc);
88102
}
89103

90104
Plugin.Logger.LogDebug($"Successfully registered config instance {key}.");
91105

92-
SyncedInstance<T>.Instance = config;
93-
SyncedInstance<T>.Default = config;
106+
SyncedInstance<T>.Instance = (T)config;
107+
SyncedInstance<T>.Default = (T)config;
94108
OnPopulateEntriesRequested += config.PopulateEntryContainer;
95109

96110
var syncBehaviour = Prefab.AddComponent<ConfigSyncBehaviour>();
97111
syncBehaviour.ConfigInstanceKey = key;
98112
}
99113

114+
/// <summary>
115+
/// Register a config with CSync, making it responsible for synchronization.<br></br>
116+
/// After calling this method, all clients will receive the host's config upon joining.
117+
/// </summary>
118+
public static void Register<T>(SyncedConfig2<T> config) where T : SyncedConfig2<T>
119+
{
120+
if (config is null)
121+
{
122+
throw new ArgumentNullException(nameof(config), "Config instance is null, cannot register.");
123+
}
124+
if (config is not T)
125+
{
126+
throw new ArgumentException($"{config.GetType()} declares it extends {nameof(SyncedConfig2<T>)}<{typeof(T)}>. It should be declared to extend {nameof(SyncedConfig2<T>)}<{config.GetType()}>.", nameof(config));
127+
}
128+
129+
var assemblyQualifiedTypeName = typeof(T).AssemblyQualifiedName ?? throw new ArgumentException(nameof(config));
130+
var key = new InstanceKey(config.GUID, assemblyQualifiedTypeName);
131+
132+
try {
133+
Instances.Add(key, config);
134+
InitialSyncHandlers.Add(key, config.OnInitialSyncCompleted);
135+
}
136+
catch (ArgumentException exc) {
137+
throw new InvalidOperationException($"Attempted to register config instance of type `{typeof(T)}`, but an instance has already been registered with key {key}.", exc);
138+
}
139+
140+
config.PopulateEntryContainer();
141+
Plugin.Logger.LogDebug($"Successfully registered config instance {key}.");
142+
143+
var syncBehaviour = Prefab.AddComponent<ConfigSyncBehaviour>();
144+
syncBehaviour.ConfigInstanceKey = key;
145+
}
146+
100147
[UsedImplicitly]
101148
[Serializable]
102149
public record struct InstanceKey

0 commit comments

Comments
 (0)