|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using MLAPI.MonoBehaviours.Core; |
| 5 | +using MLAPI.NetworkingManagerComponents.Binary; |
| 6 | + |
| 7 | +namespace MLAPI.Data.NetworkedCollections |
| 8 | +{ |
| 9 | + public class NetworkedDictionary<TKey, TValue> : IDictionary<TKey, TValue>, INetworkedVar |
| 10 | + { |
| 11 | + internal struct NetworkedDictionaryEvent<TKey, TValue> |
| 12 | + { |
| 13 | + internal enum NetworkedListEventType |
| 14 | + { |
| 15 | + Add, |
| 16 | + Remove, |
| 17 | + RemovePair, |
| 18 | + Clear, |
| 19 | + Value |
| 20 | + } |
| 21 | + |
| 22 | + internal NetworkedListEventType eventType; |
| 23 | + internal TKey key; |
| 24 | + internal TValue value; |
| 25 | + } |
| 26 | + |
| 27 | + public NetworkedVarSettings Settings = new NetworkedVarSettings(); |
| 28 | + private readonly Dictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>(); |
| 29 | + private NetworkedBehaviour networkedBehaviour; |
| 30 | + private readonly List<NetworkedDictionaryEvent<TKey, TValue>> dirtyEvents = new List<NetworkedDictionaryEvent<TKey, TValue>>(); |
| 31 | + |
| 32 | + public void ResetDirty() |
| 33 | + { |
| 34 | + dirtyEvents.Clear(); |
| 35 | + } |
| 36 | + |
| 37 | + public void SetDeltaFromReader(BitReader reader) |
| 38 | + { |
| 39 | + ushort deltaCount = reader.ReadUShort(); |
| 40 | + for (int i = 0; i < deltaCount; i++) |
| 41 | + { |
| 42 | + NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType eventType = (NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType)reader.ReadBits(3); |
| 43 | + switch (eventType) |
| 44 | + { |
| 45 | + case global::MLAPI.Data.NetworkedCollections.NetworkedDictionary<TKey, TValue>.NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Add: |
| 46 | + { |
| 47 | + //TODO: readKey |
| 48 | + //TODO: readVal |
| 49 | + } |
| 50 | + break; |
| 51 | + case global::MLAPI.Data.NetworkedCollections.NetworkedDictionary<TKey, TValue>.NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Remove: |
| 52 | + { |
| 53 | + //TODO: readKey |
| 54 | + } |
| 55 | + break; |
| 56 | + case global::MLAPI.Data.NetworkedCollections.NetworkedDictionary<TKey, TValue>.NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.RemovePair: |
| 57 | + { |
| 58 | + //TODO: readKey |
| 59 | + //TODO: readVal |
| 60 | + } |
| 61 | + break; |
| 62 | + case global::MLAPI.Data.NetworkedCollections.NetworkedDictionary<TKey, TValue>.NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Clear: |
| 63 | + { |
| 64 | + //read nothing |
| 65 | + dictionary.Clear(); |
| 66 | + } |
| 67 | + break; |
| 68 | + case global::MLAPI.Data.NetworkedCollections.NetworkedDictionary<TKey, TValue>.NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Value: |
| 69 | + { |
| 70 | + //TODO: readKey |
| 71 | + //TODO: readVal |
| 72 | + } |
| 73 | + break; |
| 74 | + default: |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public void SetFieldFromReader(BitReader reader) |
| 81 | + { |
| 82 | + ushort entryCount = reader.ReadUShort(); |
| 83 | + |
| 84 | + for (int i = 0; i < entryCount; i++) |
| 85 | + { |
| 86 | + //TODO: readKey |
| 87 | + //TODO: readVal |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public void SetNetworkedBehaviour(NetworkedBehaviour behaviour) |
| 92 | + { |
| 93 | + networkedBehaviour = behaviour; |
| 94 | + } |
| 95 | + |
| 96 | + public bool TryGetValue(TKey key, out TValue value) |
| 97 | + { |
| 98 | + return dictionary.TryGetValue(key, out value); |
| 99 | + } |
| 100 | + |
| 101 | + public void WriteDeltaToWriter(BitWriter writer) |
| 102 | + { |
| 103 | + writer.WriteUShort((ushort)dirtyEvents.Count); |
| 104 | + for (int i = 0; i < dirtyEvents.Count; i++) |
| 105 | + { |
| 106 | + writer.WriteBits((byte)dirtyEvents[i].eventType, 3); |
| 107 | + switch (dirtyEvents[i].eventType) |
| 108 | + { |
| 109 | + //Fuck me these signatures are proper aids |
| 110 | + case global::MLAPI.Data.NetworkedCollections.NetworkedDictionary<TKey, TValue>.NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Add: |
| 111 | + { |
| 112 | + //TODO: writeKey |
| 113 | + //TODO: writeVal |
| 114 | + } |
| 115 | + break; |
| 116 | + case global::MLAPI.Data.NetworkedCollections.NetworkedDictionary<TKey, TValue>.NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Remove: |
| 117 | + { |
| 118 | + //TODO: writeKey |
| 119 | + } |
| 120 | + break; |
| 121 | + case global::MLAPI.Data.NetworkedCollections.NetworkedDictionary<TKey, TValue>.NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.RemovePair: |
| 122 | + { |
| 123 | + //TODO: writeKey |
| 124 | + //TODO: writeVal |
| 125 | + } |
| 126 | + break; |
| 127 | + case global::MLAPI.Data.NetworkedCollections.NetworkedDictionary<TKey, TValue>.NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Clear: |
| 128 | + { |
| 129 | + //write nothing |
| 130 | + } |
| 131 | + break; |
| 132 | + case global::MLAPI.Data.NetworkedCollections.NetworkedDictionary<TKey, TValue>.NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Value: |
| 133 | + { |
| 134 | + //TODO: writeKey |
| 135 | + //TODO: writeVal |
| 136 | + } |
| 137 | + break; |
| 138 | + default: |
| 139 | + break; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + public void WriteFieldToWriter(BitWriter writer) |
| 145 | + { |
| 146 | + writer.WriteUShort((ushort)dictionary.Count); |
| 147 | + for (int i = 0; i < dictionary.Count; i++) |
| 148 | + { |
| 149 | + //TODO: writeKey |
| 150 | + //TODO: writeVal |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + public bool CanClientWrite(uint clientId) |
| 155 | + { |
| 156 | + switch (Settings.WritePermission) |
| 157 | + { |
| 158 | + case NetworkedVarPermission.Everyone: |
| 159 | + return true; |
| 160 | + case NetworkedVarPermission.ServerOnly: |
| 161 | + return false; |
| 162 | + case NetworkedVarPermission.OwnerOnly: |
| 163 | + return networkedBehaviour.OwnerClientId == clientId; |
| 164 | + case NetworkedVarPermission.Custom: |
| 165 | + { |
| 166 | + if (Settings.WritePermissionCallback == null) return false; |
| 167 | + return Settings.WritePermissionCallback(clientId); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + return true; |
| 172 | + } |
| 173 | + |
| 174 | + public bool CanClientRead(uint clientId) |
| 175 | + { |
| 176 | + switch (Settings.ReadPermission) |
| 177 | + { |
| 178 | + case NetworkedVarPermission.Everyone: |
| 179 | + return true; |
| 180 | + case NetworkedVarPermission.ServerOnly: |
| 181 | + return false; |
| 182 | + case NetworkedVarPermission.OwnerOnly: |
| 183 | + return networkedBehaviour.OwnerClientId == clientId; |
| 184 | + case NetworkedVarPermission.Custom: |
| 185 | + { |
| 186 | + if (Settings.ReadPermissionCallback == null) return false; |
| 187 | + return Settings.ReadPermissionCallback(clientId); |
| 188 | + } |
| 189 | + } |
| 190 | + return true; |
| 191 | + } |
| 192 | + |
| 193 | + public bool IsDirty() |
| 194 | + { |
| 195 | + return dirtyEvents.Count > 0; |
| 196 | + } |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + public TValue this[TKey key] |
| 201 | + { |
| 202 | + get |
| 203 | + { |
| 204 | + return dictionary[key]; |
| 205 | + } |
| 206 | + set |
| 207 | + { |
| 208 | + dictionary[key] = value; |
| 209 | + dirtyEvents.Add(new NetworkedDictionaryEvent<TKey, TValue>() |
| 210 | + { |
| 211 | + eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Value, |
| 212 | + key = key, |
| 213 | + value = value |
| 214 | + }); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + public ICollection<TKey> Keys => ((IDictionary<TKey, TValue>)dictionary).Keys; |
| 219 | + |
| 220 | + public ICollection<TValue> Values => ((IDictionary<TKey, TValue>)dictionary).Values; |
| 221 | + |
| 222 | + public int Count => dictionary.Count; |
| 223 | + |
| 224 | + public bool IsReadOnly => ((IDictionary<TKey, TValue>)dictionary).IsReadOnly; |
| 225 | + |
| 226 | + public void Add(TKey key, TValue value) |
| 227 | + { |
| 228 | + dictionary.Add(key, value); |
| 229 | + dirtyEvents.Add(new NetworkedDictionaryEvent<TKey, TValue>() |
| 230 | + { |
| 231 | + eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Add, |
| 232 | + key = key, |
| 233 | + value = value |
| 234 | + }); |
| 235 | + } |
| 236 | + |
| 237 | + public void Add(KeyValuePair<TKey, TValue> item) |
| 238 | + { |
| 239 | + ((IDictionary<TKey, TValue>)dictionary).Add(item); |
| 240 | + dirtyEvents.Add(new NetworkedDictionaryEvent<TKey, TValue>() |
| 241 | + { |
| 242 | + eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Add, |
| 243 | + key = item.Key, |
| 244 | + value = item.Value |
| 245 | + }); |
| 246 | + } |
| 247 | + |
| 248 | + public void Clear() |
| 249 | + { |
| 250 | + dictionary.Clear(); |
| 251 | + dirtyEvents.Add(new NetworkedDictionaryEvent<TKey, TValue>() |
| 252 | + { |
| 253 | + eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Clear |
| 254 | + }); |
| 255 | + } |
| 256 | + |
| 257 | + public bool Contains(KeyValuePair<TKey, TValue> item) |
| 258 | + { |
| 259 | + return ((IDictionary<TKey, TValue>)dictionary).Contains(item); |
| 260 | + } |
| 261 | + |
| 262 | + public bool ContainsKey(TKey key) |
| 263 | + { |
| 264 | + return dictionary.ContainsKey(key); |
| 265 | + } |
| 266 | + |
| 267 | + public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) |
| 268 | + { |
| 269 | + ((IDictionary<TKey, TValue>)dictionary).CopyTo(array, arrayIndex); |
| 270 | + } |
| 271 | + |
| 272 | + public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() |
| 273 | + { |
| 274 | + return ((IDictionary<TKey, TValue>)dictionary).GetEnumerator(); |
| 275 | + } |
| 276 | + |
| 277 | + public bool Remove(TKey key) |
| 278 | + { |
| 279 | + bool state = dictionary.Remove(key); |
| 280 | + if (state) |
| 281 | + { |
| 282 | + dirtyEvents.Add(new NetworkedDictionaryEvent<TKey, TValue>() |
| 283 | + { |
| 284 | + eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.Remove, |
| 285 | + key = key |
| 286 | + }); |
| 287 | + } |
| 288 | + return state; |
| 289 | + } |
| 290 | + |
| 291 | + public bool Remove(KeyValuePair<TKey, TValue> item) |
| 292 | + { |
| 293 | + bool state = ((IDictionary<TKey, TValue>)dictionary).Remove(item); |
| 294 | + if (state) |
| 295 | + { |
| 296 | + dirtyEvents.Add(new NetworkedDictionaryEvent<TKey, TValue>() |
| 297 | + { |
| 298 | + eventType = NetworkedDictionaryEvent<TKey, TValue>.NetworkedListEventType.RemovePair, |
| 299 | + key = item.Key, |
| 300 | + value = item.Value |
| 301 | + }); |
| 302 | + } |
| 303 | + return state; |
| 304 | + } |
| 305 | + |
| 306 | + IEnumerator IEnumerable.GetEnumerator() |
| 307 | + { |
| 308 | + return ((IDictionary<TKey, TValue>)dictionary).GetEnumerator(); |
| 309 | + } |
| 310 | + } |
| 311 | +} |
0 commit comments