-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCustomSyncedValuesSynchronizer.cs
69 lines (59 loc) · 2.46 KB
/
CustomSyncedValuesSynchronizer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections;
using System.Collections.Generic;
using ServerSync;
using UnityEngine;
namespace Seasons
{
internal static class CustomSyncedValuesSynchronizer
{
private static readonly Queue<IEnumerator> coroutines = new Queue<IEnumerator>();
private static readonly WaitWhile waitForServerUpdate = new WaitWhile(() => ConfigSync.ProcessingServerUpdate);
public static void AssignValueSafe<T>(this CustomSyncedValue<T> syncedValue, T value)
{
AddToQueue(AssignAfterServerUpdate(syncedValue, value, assignIfChanged: false));
}
public static void AssignValueSafe<T>(this CustomSyncedValue<T> syncedValue, Func<T> function)
{
AddToQueue(AssignAfterServerUpdate(syncedValue, function, assignIfChanged: false));
}
public static void AssignValueIfChanged<T>(this CustomSyncedValue<T> syncedValue, T value)
{
AddToQueue(AssignAfterServerUpdate(syncedValue, value, assignIfChanged: true));
}
public static void AssignValueIfChanged<T>(this CustomSyncedValue<T> syncedValue, Func<T> function)
{
AddToQueue(AssignAfterServerUpdate(syncedValue, function, assignIfChanged: true));
}
private static IEnumerator AssignAfterServerUpdate<T>(CustomSyncedValue<T> syncedValue, Func<T> function, bool assignIfChanged)
{
yield return AssignAfterServerUpdate(syncedValue, function(), assignIfChanged);
}
private static IEnumerator AssignAfterServerUpdate<T>(CustomSyncedValue<T> syncedValue, T value, bool assignIfChanged)
{
if (assignIfChanged && syncedValue.Value.Equals(value))
yield break;
yield return waitForServerUpdate;
syncedValue.AssignLocalValue(value);
}
private static void AddToQueue(IEnumerator coroutine)
{
coroutines.Enqueue(coroutine);
if (coroutines.Count == 1)
Seasons.instance.StartCoroutine(CoroutineCoordinator());
}
private static IEnumerator CoroutineCoordinator()
{
while (true)
{
while (coroutines.Count > 0)
{
yield return Seasons.instance.StartCoroutine(coroutines.Peek());
coroutines.Dequeue();
}
if (coroutines.Count == 0)
yield break;
}
}
}
}