@ -1,58 +0,0 @@ | |||||
using UnityEngine; | |||||
using System.Collections.Generic; | |||||
namespace GWConquest | |||||
{ | |||||
public static class BoltEntityCache | |||||
{ | |||||
private static Dictionary<NetworkId, IEntityBehaviour> dictionary = new Dictionary<NetworkId, IEntityBehaviour>(); | |||||
public static T Get<T>(NetworkId id) where T : class, IEntityBehaviour | |||||
{ | |||||
if(id == default(NetworkId)) | |||||
{ | |||||
return null; | |||||
} | |||||
if(dictionary.ContainsKey(id)) | |||||
{ | |||||
var behaviour = dictionary[id]; | |||||
if(behaviour.entity.NetworkId == id) | |||||
{ | |||||
return behaviour as T; | |||||
} | |||||
} | |||||
var entity = BoltNetwork.FindEntity(id); | |||||
if(entity == null) | |||||
{ | |||||
return null; | |||||
} | |||||
else { | |||||
var newBeh = entity.GetComponent<T>(); | |||||
dictionary[id] = newBeh; | |||||
return newBeh; | |||||
} | |||||
} | |||||
public static NetworkId Set<T>(T behaviour) where T : class, IEntityBehaviour | |||||
{ | |||||
if(behaviour == null) | |||||
{ | |||||
return default(NetworkId); | |||||
} | |||||
else { | |||||
return behaviour.entity.NetworkId; | |||||
} | |||||
} | |||||
public static void ClearCache() | |||||
{ | |||||
dictionary.Clear(); | |||||
} | |||||
} | |||||
} |
@ -1,11 +0,0 @@ | |||||
fileFormatVersion: 2 | |||||
guid: c2f033f40a5a03e4ba66c6f9240d2043 | |||||
MonoImporter: | |||||
externalObjects: {} | |||||
serializedVersion: 2 | |||||
defaultReferences: [] | |||||
executionOrder: 0 | |||||
icon: {instanceID: 0} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |
@ -1,240 +0,0 @@ | |||||
| |||||
using System; | |||||
using System.Collections; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using UdpKit; | |||||
namespace GWConquest | |||||
{ | |||||
public abstract class BoltList<T, TokenClass> : IList<T> where TokenClass : BoltListToken<T>, new() | |||||
{ | |||||
private IState state; | |||||
private string propertyName; | |||||
public BoltList(IState _state, string _propertyName) | |||||
{ | |||||
state = _state; | |||||
propertyName = _propertyName; | |||||
} | |||||
private TokenClass Token | |||||
{ | |||||
get | |||||
{ | |||||
TokenClass token = state.GetDynamic(propertyName) as TokenClass; | |||||
if (token == null) | |||||
{ | |||||
return new TokenClass(); | |||||
} | |||||
else | |||||
{ | |||||
return token; | |||||
} | |||||
} | |||||
set => state.SetDynamic(propertyName, value); | |||||
} | |||||
public void FromToken(TokenClass token) | |||||
{ | |||||
Token = token; | |||||
} | |||||
public T this[int index] | |||||
{ | |||||
get => Token.entries[index]; | |||||
set | |||||
{ | |||||
Token.entries[index] = value; | |||||
Token = new TokenClass().SetEntries(Token.entries) as TokenClass; | |||||
} | |||||
} | |||||
public int Count => Token.entries.Length; | |||||
public bool IsReadOnly => false; | |||||
public void Add(T item) | |||||
{ | |||||
if (!Contains(item)) | |||||
{ | |||||
Token = new TokenClass().SetEntries(Token.entries.Append(item)) as TokenClass; | |||||
} | |||||
} | |||||
public void Clear() | |||||
{ | |||||
Token = new TokenClass(); | |||||
} | |||||
public bool Contains(T item) | |||||
{ | |||||
return Token.entries.Contains(item); | |||||
} | |||||
public void CopyTo(T[] array, int arrayIndex) | |||||
{ | |||||
Token.entries.CopyTo(array, arrayIndex); | |||||
} | |||||
public IEnumerator<T> GetEnumerator() | |||||
{ | |||||
return new GenericListEnumerator<T>(this); | |||||
} | |||||
public int IndexOf(T item) | |||||
{ | |||||
return Array.IndexOf(Token.entries, item); | |||||
} | |||||
public void Insert(int index, T item) | |||||
{ | |||||
List<T> list = Token.entries.ToList(); | |||||
list.Insert(index, item); | |||||
Token = new TokenClass().SetEntries(list) as TokenClass; | |||||
} | |||||
public bool Remove(T item) | |||||
{ | |||||
List<T> list = Token.entries.ToList(); | |||||
if (list.Remove(item)) | |||||
{ | |||||
Token = new TokenClass().SetEntries(list) as TokenClass; | |||||
return true; | |||||
} | |||||
else return false; | |||||
} | |||||
public void RemoveAt(int index) | |||||
{ | |||||
List<T> list = Token.entries.ToList(); | |||||
list.RemoveAt(index); | |||||
Token = new TokenClass().SetEntries(list) as TokenClass; | |||||
} | |||||
IEnumerator IEnumerable.GetEnumerator() | |||||
{ | |||||
return new GenericListEnumerator<T>(this); | |||||
} | |||||
public override string ToString() | |||||
{ | |||||
return "BoltList " + Util.PrintEnumerable(this); | |||||
} | |||||
public void SetEntries(IEnumerable<T> entries) | |||||
{ | |||||
Token = new TokenClass().SetEntries(entries) as TokenClass; | |||||
} | |||||
} | |||||
public abstract class BoltListToken<T> : IProtocolToken | |||||
{ | |||||
public BoltListToken() | |||||
{ | |||||
entries = new T[0]; | |||||
} | |||||
public BoltListToken<T> SetEntries(IEnumerable<T> _entities) | |||||
{ | |||||
entries = _entities.ToArray(); | |||||
return this; | |||||
} | |||||
public T[] entries { get; private set; } | |||||
public void Read(UdpPacket packet) | |||||
{ | |||||
//Debug.LogFormat("Reading Bolt list..."); | |||||
int count = packet.ReadInt(); | |||||
entries = new T[count]; | |||||
for (int i = 0; i < count; i++) | |||||
{ | |||||
entries[i] = ReadEntry(packet); | |||||
} | |||||
//Debug.LogFormat("Finished reading Bolt list {0}", this); | |||||
} | |||||
public void Write(UdpPacket packet) | |||||
{ | |||||
//Debug.LogFormat("Writing Bolt list..."); | |||||
packet.WriteInt(entries.Length); | |||||
foreach (T e in entries) | |||||
{ | |||||
WriteEntry(packet, e); | |||||
} | |||||
//Debug.LogFormat("Finished writing Bolt list {0}", this); | |||||
} | |||||
public override string ToString() | |||||
{ | |||||
return "BoltList " + Util.PrintEnumerable(entries); | |||||
} | |||||
public override bool Equals(object obj) | |||||
{ | |||||
if(obj == null || !(obj is BoltListToken<T>)) | |||||
return false; | |||||
var other = obj as BoltListToken<T>; | |||||
if(entries.Length != other.entries.Length) | |||||
return false; | |||||
for(int i = 0; i < entries.Length; i++) | |||||
{ | |||||
if(entries[i] == null) | |||||
{ | |||||
if(other.entries[i] != null) | |||||
return false; | |||||
} | |||||
else { | |||||
if(!entries[i].Equals(other.entries[i])) | |||||
return false; | |||||
} | |||||
} | |||||
return true; | |||||
} | |||||
public abstract T ReadEntry(UdpPacket packet); | |||||
public abstract void WriteEntry(UdpPacket packet, T entry); | |||||
} | |||||
public class ZoneListToken : BoltListToken<Zone> { | |||||
public override Zone ReadEntry(UdpPacket packet) | |||||
{ | |||||
return Zone.GetFromId(packet.ReadInt()); | |||||
} | |||||
public override void WriteEntry(UdpPacket packet, Zone entry) | |||||
{ | |||||
packet.WriteInt(Zone.GetZoneId(entry)); | |||||
} | |||||
} | |||||
public class ZoneList : BoltList<Zone, ZoneListToken> | |||||
{ | |||||
public ZoneList(IState _state, string _propertyName) : base(_state, _propertyName) {} | |||||
} | |||||
public class StringListToken : BoltListToken<string> { | |||||
public override string ReadEntry(UdpPacket packet) | |||||
{ | |||||
return packet.ReadString(); | |||||
} | |||||
public override void WriteEntry(UdpPacket packet, string entry) | |||||
{ | |||||
packet.WriteString(entry); | |||||
} | |||||
} | |||||
public class StringList : BoltList<string, StringListToken> | |||||
{ | |||||
public StringList(IState _state, string _propertyName) : base(_state, _propertyName) {} | |||||
} | |||||
} |
@ -1,11 +0,0 @@ | |||||
fileFormatVersion: 2 | |||||
guid: fda8796fd8800ef46a1ae47fa4de74a7 | |||||
MonoImporter: | |||||
externalObjects: {} | |||||
serializedVersion: 2 | |||||
defaultReferences: [] | |||||
executionOrder: 0 | |||||
icon: {instanceID: 0} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |
@ -1,119 +0,0 @@ | |||||
using UdpKit; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Collections; | |||||
using System; | |||||
namespace GWConquest | |||||
{ | |||||
public class GenericListEnumerator<T> : IEnumerator<T> | |||||
{ | |||||
private int position = -1; | |||||
private IList<T> list; | |||||
public GenericListEnumerator(IList<T> _list) | |||||
{ | |||||
list = _list; | |||||
} | |||||
public T Current => list[position]; | |||||
object IEnumerator.Current => list[position]; | |||||
public void Dispose() { } | |||||
public bool MoveNext() | |||||
{ | |||||
position++; | |||||
return position < list.Count; | |||||
} | |||||
public void Reset() | |||||
{ | |||||
position = -1; | |||||
} | |||||
} | |||||
public class CachedBoltEntity | |||||
{ | |||||
public NetworkId NetworkId { get; private set; } | |||||
private BoltEntity cachedEntity; | |||||
public CachedBoltEntity(NetworkId id) | |||||
{ | |||||
NetworkId = id; | |||||
} | |||||
public CachedBoltEntity(BoltEntity e) | |||||
{ | |||||
NetworkId = e.NetworkId; | |||||
cachedEntity = e; | |||||
} | |||||
public BoltEntity Entity | |||||
{ | |||||
get | |||||
{ | |||||
if (cachedEntity == null) | |||||
{ | |||||
cachedEntity = BoltNetwork.FindEntity(NetworkId); | |||||
} | |||||
return cachedEntity; | |||||
} | |||||
} | |||||
public override bool Equals(object obj) | |||||
{ | |||||
if (obj == null || GetType() != obj.GetType()) | |||||
{ | |||||
return false; | |||||
} | |||||
var other = obj as CachedBoltEntity; | |||||
return NetworkId == other.NetworkId; | |||||
} | |||||
public override int GetHashCode() | |||||
{ | |||||
return NetworkId.PackedValue.GetHashCode(); | |||||
} | |||||
public override string ToString() | |||||
{ | |||||
return Entity?.ToString(); | |||||
} | |||||
} | |||||
public class EntityListToken : BoltListToken<CachedBoltEntity> | |||||
{ | |||||
public override CachedBoltEntity ReadEntry(UdpPacket packet) | |||||
{ | |||||
var id = new NetworkId(packet.ReadULong()); | |||||
return new CachedBoltEntity(id); | |||||
} | |||||
public override void WriteEntry(UdpPacket packet, CachedBoltEntity entry) | |||||
{ | |||||
packet.WriteULong(entry.NetworkId.PackedValue); | |||||
} | |||||
} | |||||
public class EntityList : BoltList<CachedBoltEntity, EntityListToken> | |||||
{ | |||||
public EntityList(IState _state, string _propertyName) : base(_state, _propertyName) { } | |||||
public void Add(BoltEntity e) | |||||
{ | |||||
Add(new CachedBoltEntity(e)); | |||||
} | |||||
public bool Remove(BoltEntity e) | |||||
{ | |||||
return Remove(new CachedBoltEntity(e)); | |||||
} | |||||
} | |||||
} |
@ -1,11 +0,0 @@ | |||||
fileFormatVersion: 2 | |||||
guid: 38983f1af3b8f9446a9a71fc1d5ebfc7 | |||||
MonoImporter: | |||||
externalObjects: {} | |||||
serializedVersion: 2 | |||||
defaultReferences: [] | |||||
executionOrder: 0 | |||||
icon: {instanceID: 0} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |
@ -1,35 +0,0 @@ | |||||
namespace GWConquest { | |||||
public abstract class GWBoltBehaviour<T> : EntityBehaviour<T> where T : class, IState | |||||
{ | |||||
private T _state = null; | |||||
public T State { | |||||
get { | |||||
/*if(_state == null) | |||||
{ | |||||
_state = state; | |||||
} | |||||
return _state;*/ | |||||
return state; | |||||
} | |||||
} | |||||
} | |||||
public abstract class GWBoltEntityListener<T> : EntityEventListener<T> where T : class, IState | |||||
{ | |||||
private T _state = null; | |||||
public T State { | |||||
get { | |||||
/*if(_state == null) | |||||
{ | |||||
_state = state; | |||||
} | |||||
return _state;*/ | |||||
return state; | |||||
} | |||||
} | |||||
} | |||||
} |
@ -1,11 +0,0 @@ | |||||
fileFormatVersion: 2 | |||||
guid: f48b9c6bc6be7154e809c78208e38787 | |||||
MonoImporter: | |||||
externalObjects: {} | |||||
serializedVersion: 2 | |||||
defaultReferences: [] | |||||
executionOrder: 0 | |||||
icon: {instanceID: 0} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |
@ -1,5 +0,0 @@ | |||||
using Unity.Netcode; | |||||
namespace GWConquest { | |||||
} |
@ -1,11 +0,0 @@ | |||||
fileFormatVersion: 2 | |||||
guid: 5893ec9dc0d5802479ca61e4b13ac64f | |||||
MonoImporter: | |||||
externalObjects: {} | |||||
serializedVersion: 2 | |||||
defaultReferences: [] | |||||
executionOrder: 0 | |||||
icon: {instanceID: 0} | |||||
userData: | |||||
assetBundleName: | |||||
assetBundleVariant: |