suck it unity collab

This commit is contained in:
LadyEbony 2020-08-21 22:41:26 -07:00
parent 2630db1920
commit dad44ef0cf
456 changed files with 1861 additions and 128 deletions

View file

@ -0,0 +1,757 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
using System.Linq;
using ExitGames.Client.Photon.LoadBalancing;
using ExitGames.Client.Photon;
using Hashtable = ExitGames.Client.Photon.Hashtable;
using PConst = PhotonConstants;
using Attribute = System.Attribute;
using Type = System.Type;
using Action = System.Action;
using EntityNetwork;
public abstract class EntityBase : MonoBehaviour {
/// <summary>
/// Entity ID for the entity, all entities require a unique ID.
/// </summary>
//[System.NonSerialized]
public int EntityID;
/// <summary>
/// Player ID number who is considered the authority for the object.
/// isMine / isRemote / isUnclaimed are determined by the authority holder.
/// Defaults to -1 if unassigned.
/// </summary>
//[System.NonSerialized]
public int authorityID = -1;
#region Helpers
/// <summary>
/// A helper that determines if the object is owned locally.
/// </summary>
/// <seealso cref="isRemote"/>
public bool isMine {
get {
// Everything is ours when we're not connected
if (NetworkManager.net == null) return true;
// If we're the master and have the appropriate interface, ingore the Authority ID and use the master status
if (this is IMasterOwnsUnclaimed && isUnclaimed) {
return NetworkManager.isMaster;
}
return NetworkManager.localID == authorityID;
}
}
/// <summary>
/// A helper to determine if the object is remote.
/// Returns false if we're disconnected
/// </summary>
/// <seealso cref="isMine"/>
public bool isRemote {
get {
if (NetworkManager.net == null) return false;
// Similar to isMine, ignore the master status if unclaimed
if (this is IMasterOwnsUnclaimed && isUnclaimed) {
return !NetworkManager.isMaster;
}
return NetworkManager.localID != authorityID;
}
}
/// <summary>
/// Helper to evaluate our authority ID being -1. It should be -1 if unclaimed.
/// </summary>
public bool isUnclaimed {
get {
return authorityID == -1;
}
}
/// <summary>
/// Query to see if we're registered. This is slightly expensive.
/// </summary>
/// <value><c>true</c> if is registered; otherwise, <c>false</c>.</value>
public bool isRegistered {
get {
return EntityManager.Entity(authorityID) != null;
}
}
public void AppendIDs(Hashtable h) {
h.Add(PConst.eidChar, EntityID);
h.Add(PConst.athChar, authorityID);
}
public void Register() {
EntityManager.Register(this);
}
public void RaiseEvent(char c, bool includeLocal, params object[] parameters) {
var h = new Hashtable();
AppendIDs(h);
h.Add(0, c);
if (parameters != null)
h.Add(1, parameters);
NetworkManager.netMessage(PhotonConstants.EntityEventCode, h, true);
if (includeLocal) {
InternallyInvokeEvent(c, parameters);
}
}
public static void RaiseStaticEvent<T>(char c, bool includeLocal, params object[] parameters) where T : EntityBase{
var h = new Hashtable();
// Given we have no instance ID's, we don't append IDs
h.Add(0, c);
if (parameters != null)
h.Add(1, parameters);
//var name = typeof(T).
h.Add(2,IDfromType(typeof(T)));
NetworkManager.netMessage(PhotonConstants.EntityEventCode, h, true);
if (includeLocal)
InternallyInvokeStatic(typeof(T),c, parameters);
}
static Dictionary<int,Type> EBTypeIDs;
static Dictionary<Type,int> IDToEBs;
static void buildTypeIDs(){ // Build a bidirectional lookup of all EntityBase's in the assembly and assign them unique ID's
EBTypeIDs = new Dictionary<int,Type>();
IDToEBs = new Dictionary<Type,int>();
var ebType = typeof(EntityBase);
var derivedTypes = System.AppDomain.CurrentDomain.GetAssemblies().SelectMany(t => t.GetTypes()).Where(t=>ebType.IsAssignableFrom(t));
var sorted = derivedTypes.OrderBy(t => t.FullName);
int newID = 0;
foreach(var type in sorted) {
EBTypeIDs.Add(newID, type);
IDToEBs.Add(type, newID);
++newID;
}
if (Debug.isDebugBuild || Application.isEditor) {
var debugString = new System.Text.StringBuilder();
foreach(var pair in EBTypeIDs) {
debugString.AppendFormat("{0} -> {1} \n", pair.Value, pair.Key);
}
Debug.Log(debugString.ToString());
}
}
/// <summary>
/// Get a unique ID for this objects class that dervives from EntityBase
/// </summary>
public int typeID {
get {
return IDfromType(this.GetType());
}
}
public static int IDfromType(Type t) {
if (IDToEBs != null)
return IDToEBs[t];
buildTypeIDs();
return IDfromType(t);
}
public static Type TypeFromID(int id) {
if (EBTypeIDs != null)
return EBTypeIDs[id];
buildTypeIDs();
return TypeFromID(id); // Return the original request
}
#endregion
#region Serializers
/// <summary>
/// Serialize all tokens with the given label into HashTable h
/// Returns true if any contained token requires a reliable update
/// If you use IAutoSerialize, this is only neccessary for manual tokens
/// </summary>
protected bool SerializeToken(Hashtable h, params char[] ca) {
bool needsReliable = false;
var tH = tokenHandler;
foreach(char c in ca) {
h.Add(c, tH.get(c, this));
// If we're not already reliable, check if we need reliable
if (!needsReliable)
needsReliable = tH.alwaysReliable[c];
}
return needsReliable;
}
/// <summary>
/// Internally used for building and dispatching entity updates, build a full serialization of auto tokens and ID's
/// Due to inconsistent handling/calling contexts, ID's are added safely
/// </summary>
/// <returns>0 if nothing is sent, 1 if there is content to send, 2 if content should be sent reliably</returns>
public int SerializeAuto(Hashtable h) {
var tH = tokenHandler;
var time = Time.realtimeSinceStartup;
bool reliableFlag = false;
bool isSending = false;
foreach (var c in tH.autoTokens) {
if (this[c] < time) {
this[c] = time + tH.updateTimes[c];
isSending = true;
h.Add(c, tH.get(c, this));
if (!reliableFlag)
reliableFlag = tH.reliableTokens.Contains(c);
}
}
if (isSending) {
SerializeAlwaysTokensSafely(h);
//toUpdate.AddRange(tH.alwaysSendTokens);
}
// If none of the tokens actually updated, return 0
// Otherwise, return 1 for a normal update, 2 for a reliable update
if (!isSending)
return 0;
h.AddOrSet(PConst.eidChar, EntityID);
h.AddOrSet(PConst.athChar, authorityID);
//SerializeToken(toUpdate.Distinct().ToArray());
return reliableFlag ? 2 : 1;
}
/// <summary>
/// Read specified values out of the hashtable
/// In most cases, you'll want to use DeserializeFull instead
/// </summary>
protected void DeserializeToken(Hashtable h, params char[] ca) {
var tH = tokenHandler;
foreach(char c in ca) {
object value;
if (h.TryGetValue(c, out value))
tH.set(c, this, value);
}
}
/// <summary>
/// Read all attributed tokens fields of the hashtable and update corresponding values.
/// This will be called automatically if implementing IAutoDeserialize
/// </summary>
public void DeserializeFull(Hashtable h) {
var tH = tokenHandler;
foreach(char c in TokenList()) {
object value;
if (h.TryGetValue(c, out value))
tH.set(c, this, value);
}
}
/// <summary>
/// Key function describing what to serialize. Be sure to call Base.Serialize(h)
/// Helper SerializeToken will automatically write fields with matching tokens into the table
/// </summary>
public virtual void Serialize(Hashtable h) {
AppendIDs(h);
}
/// <summary>
/// Deserialize the entity out of the provided hashtable.
/// Use helper function DeserializeToken automatically unpack any tokens
/// </summary>
public virtual void Deserialize(Hashtable h) {
h.SetOnKey(PConst.eidChar, ref EntityID);
h.SetOnKey(PConst.athChar, ref authorityID);
}
/// <summary>
/// Check to see if the hashtable already contains each always send token, and if not, add it.
/// </summary>
private bool SerializeAlwaysTokensSafely(Hashtable h) {
var tH = tokenHandler;
foreach(var c in tH.alwaysSendTokens) {
// If the hashtable doesn't contain our token, add it in
if (!h.ContainsKey(c)) {
h.Add(c, tH.get(c,this));
}
}
return tH.alwaysIsRelaible;
}
#endregion
/// <summary>
/// Send a reliable update with <b>only</b> the provided tokens, immediately.
/// This does not send the alwaysSend autotokens, and exists solely so that you can have a field update as soon as possible,
/// such as menu or input events
/// </summary>
public void UpdateExclusively(params char[] ca) {
var h = new Hashtable();
AppendIDs(h);
SerializeToken(h, ca);
NetworkManager.netMessage(PConst.EntityUpdateCode, h, true);
}
/// <summary>
/// Immediately sent a network update with our current state. This includes auto tokens if IAutoSerialize is implemented.
/// Reliable flag, though it defaults to false, may be forced true when sending always or reliable tokens.
/// </summary>
public void UpdateNow(bool reliable = false) {
var h = new Hashtable();
Serialize(h);
if (this is IAutoSerialize) {
int autoCode = SerializeAuto(h);
if (autoCode == 2) reliable = true;
} else {
if (SerializeAlwaysTokensSafely(h))
reliable = true;
}
NetworkManager.netMessage(PConst.EntityUpdateCode, h, reliable);
}
#region timing
// updateTimers coordinates when each value's server representation 'expires' and should be resent
// For values which didn't specify an update time, this value is set to +inf, so that it will always be greater than the current time
private Dictionary<char,float> updateTimers = new Dictionary<char,float>();
float this[char c] {
get {
float t;
if(!updateTimers.TryGetValue(c, out t)) {
var updateTime = tokenHandler.updateTimes[c];
updateTimers.Add(c, updateTime >= 0 ? 0 : Mathf.Infinity);
}
return updateTimers[c];
}
set {
updateTimers[c] = value;
}
}
#endregion
// Token management is a system that assigns a character token to each field for serialization, via attributes
// This is used to automatically pull get/set for variables to assist in auto serializing as much as possible and reducing the amount of manual network messaging
[ContextMenu("Claim as mine")]
public bool ClaimAsMine() {
if (!NetworkManager.inRoom && NetworkManager.isReady) return false;
authorityID = NetworkManager.localID;
UpdateNow(true);
return true;
}
#region TokenManagement
/// TODO: Modifying a token at this level needs to clone the TokenHandler specially for this EntityBase object so changes don't propegate to other entities
/// <summary>
/// Runtime modify the parameters of a token. Modifying the reliability of a token is slightly intensive.
/// </summary>
/// <param name="token">The token to be modified</param>
/// <param name="updateMs">Milliseconds between updates. 0 is every frame, use cautiously. Set negative to unsubcribe automaic updates.</param>
/// <param name="alwaysSend">If the token should always be sent with other tokens</param>
/// <param name="isReliable">If the token needs to be sent reliably.</param>
public void ModifyToken(char token, int? updateMs = null, bool? alwaysSend = null, bool? isReliable = null) {
var tH = tokenHandler;
if (tH.shared) {
_tokenHandler = tH.DeepClone();
tH = _tokenHandler;
}
// If we have a value for reliability
if (isReliable.HasValue) {
if (tH.reliableTokens.Contains(token)){
if (!isReliable.Value)
tH.reliableTokens.Remove(token);
} else {
if (isReliable.Value)
tH.reliableTokens.Add(token);
}
}
// If we have a value for always sending
if (alwaysSend.HasValue) {
if (tH.alwaysSend.ContainsKey(token)){
if (!alwaysSend.Value)
tH.alwaysSend.Remove(token);
} else {
if (alwaysSend.Value)
tH.alwaysSend.Add(token,alwaysSend.Value);
}
}
if (alwaysSend.HasValue || isReliable.HasValue)
tH.ReEvalAlwaysIsReliable();
if (updateMs.HasValue) {
float fUpdateTime = updateMs.Value / 1000f;
tH.updateTimes[token] = fUpdateTime;
// Unsubscribing
if (fUpdateTime < 0) {
if (tH.autoTokens.Contains(token))
tH.autoTokens.Remove(token);
if (!tH.manualTokens.Contains(token))
tH.manualTokens.Add(token);
this[token] = Mathf.Infinity; // Never auto-update
} else {
if (!tH.autoTokens.Contains(token))
tH.autoTokens.Add(token);
if (tH.manualTokens.Contains(token))
tH.manualTokens.Remove(token);
this[token] = 0; // Auto update next check
}
}
}
/// <summary>
/// Invoke a labeled character event. You should never need to use this method manually.
/// </summary>
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public void InternallyInvokeEvent(char c, params object[] parameters) {
tokenHandler.NetEvents[c].Invoke(this, parameters);
}
/// <summary>
/// Invoke a labeled character event when the event is static. You should hopefully never need to use this method manually.
/// </summary>
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static void InternallyInvokeStatic(Type T, char c, params object[] parameters) {
if (!handlers.ContainsKey(T)) {
BuildTokenList(T);
}
TokenHandler th = handlers[T];
th.NetEvents[c].Invoke(null, parameters);
}
private static Dictionary<Type,List<char>> tokens = new Dictionary<Type,List<char>>();
private static Dictionary<Type,TokenHandler> handlers = new Dictionary<Type,TokenHandler>();
/// <summary>
/// Cached token handler reference
/// </summary>
private TokenHandler _tokenHandler;
/// <summary>
/// Gets the token for class in question. Will generate the token list if it doesn't exist.
/// If the object modifies it's tokens parameters, it will clone a new handler specific to the object
/// </summary>
protected TokenHandler tokenHandler {
get {
if (_tokenHandler != null) return _tokenHandler;
var T = GetType();
if (handlers.ContainsKey(T))
return _tokenHandler = handlers[T];
BuildTokenList(T);
return _tokenHandler = handlers[T];
}
}
protected class TokenHandler {
private Dictionary<char,System.Action<EntityBase,object>> setters;
private Dictionary<char,System.Func<EntityBase,object>> getters;
public Dictionary<char,MethodInfo> NetEvents = new Dictionary<char,MethodInfo>();
public TokenHandler() {
setters = new Dictionary<char,System.Action<EntityBase,object>>();
getters = new Dictionary<char,System.Func<EntityBase,object>>();
}
public TokenHandler DeepClone() {
TokenHandler nTH = new TokenHandler();
nTH.setters = new Dictionary<char,System.Action<EntityBase,object>>(this.setters);
nTH.getters = new Dictionary<char,System.Func<EntityBase,object>>(this.getters);
nTH.alwaysSend = new Dictionary<char,bool>(this.alwaysSend);
nTH.alwaysReliable = new Dictionary<char,bool>(this.alwaysReliable);
nTH.alwaysIsRelaible = this.alwaysIsRelaible;
nTH.reliableTokens.AddRange(this.reliableTokens);
nTH.alwaysSendTokens.AddRange(this.alwaysSendTokens);
nTH.autoTokens.AddRange(this.autoTokens);
nTH.manualTokens.AddRange(this.manualTokens);
nTH.NetEvents = this.NetEvents; // This one we keep the reference for
// Flag the new TokenHandler as not being shared
nTH.shared = false;
return nTH;
}
public bool shared = true;
public object get(char c, EntityBase eb) {
return getter(c)(eb);
}
public void set(char c, EntityBase eb, object o) {
setter(c)(eb, o);
}
public System.Func<EntityBase,object> getter(char c){
return getters[c];
}
public System.Action<EntityBase,object> setter(char c) {
return setters[c];
}
public Dictionary<char,bool>
alwaysSend = new Dictionary<char,bool>(),
alwaysReliable = new Dictionary<char,bool>();
/// <summary>
/// If any always send tokens are reliable, implicity, all of them are.
/// </summary>
public bool alwaysIsRelaible = false;
public Dictionary<char,float> updateTimes = new Dictionary<char,float>();
/// <summary>
/// Tokens that should always be sent reliably
/// </summary>
public List<char> reliableTokens = new List<char>();
/// <summary>
/// Tokens that should always be sent whenever another token is sent
/// </summary>
public List<char> alwaysSendTokens = new List<char>();
/// <summary>
/// Tokens that are automatically dispatched according to the update timer
/// </summary>
public List<char> autoTokens = new List<char>();
/// <summary>
/// Tokens that are not always send or auto tokens. Useful for getting the subsection of tokens to serialize manually
/// </summary>
public List<char> manualTokens = new List<char>();
/// <summary>
/// Check each reliable token for if it needs to always be sent, and if any do, always sent tokens require reliable updates.
/// </summary>
public void ReEvalAlwaysIsReliable() {
alwaysIsRelaible = false;
foreach(var token in reliableTokens) {
if (alwaysSend.ContainsKey(token)) {
alwaysIsRelaible = true;
return;
}
}
}
public void RegisterField(char c, FieldInfo fi, NetVar nv) {
getters.Add(c, (e)=> { return fi.GetValue(e); });
setters.Add(c, (e,v)=> { fi.SetValue(e,v); });
alwaysSend.Add(c,nv.alwaysSend);
alwaysReliable.Add(c,nv.alwaysReliable);
updateTimes.Add(c, nv.updateTime);
if (nv.alwaysSend)
alwaysSendTokens.Add(c);
if (nv.alwaysReliable)
reliableTokens.Add(c);
if (nv.updateTime >= 0f) {
autoTokens.Add(c);
} else if (!nv.alwaysSend) {
manualTokens.Add(c);
}
if(nv.alwaysSend && nv.alwaysReliable) {
alwaysIsRelaible = true;
}
}
}
/// <summary>
/// Get a list of all tokens used in this class.
/// </summary>
public List<char> TokenList() {
var T = this.GetType();
if (tokens.ContainsKey(T)) {
return tokens[T];
}
BuildTokenList(T);
return tokens[T];
}
static void BuildTokenList(Type T) {
if (!T.IsSubclassOf(typeof(EntityBase)))
throw new System.Exception("Cannot build a token list for a class that doesn't derive EntityBase");
// Setup the char list
List<char> charList;
TokenHandler th;
// Establish the token handler
if (!tokens.ContainsKey(T)) {
charList = new List<char>();
th = new TokenHandler();
tokens.Add(T,charList);
handlers.Add(T,th);
} else {
charList = tokens[T];
th = handlers[T];
}
var fields = T.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach(FieldInfo fi in fields) {
var fieldInfo = fi; // Closure fi to prevent variable capture in lambdas
var netVar = fieldInfo.GetCustomAttributes(typeof(NetVar),true).FirstOrDefault() as NetVar;
if (netVar == null) continue; // This field has no netvar associated, skip it
//Debug.LogFormat("{0} Field {1} -> {2}",T.Name,fi.Name,netVar.token);
charList.Add(netVar.token);
th.RegisterField(netVar.token, fi, netVar);
}
var methods = T.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
// Store all event method infos for remote invocation
foreach(MethodInfo mi in methods) {
var methodInfo = mi; // Closure
var netEvent = methodInfo.GetCustomAttributes(typeof(NetEvent),true).FirstOrDefault() as NetEvent;
if (netEvent == null) {
//Debug.LogFormat("Skipping {0}'s {1}", T.Name, methodInfo.Name);
continue;
}
//Debug.LogFormat("EVENT {0}'s {1} -> {2}", T.Name, methodInfo.Name, netEvent.token);
th.NetEvents.Add(netEvent.token, methodInfo);
}
// Search for all static events on this type; In theory this could be merged with the non-static search, but at time of implementing I thought I may process them seperately.
var staticMethods = T.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
foreach (MethodInfo mi in staticMethods) {
var smi = mi; // Closure
var netEvent = smi.GetCustomAttributes(typeof(NetEvent),true).FirstOrDefault() as NetEvent;
if (netEvent == null) continue;
th.NetEvents.Add(netEvent.token, smi);
}
var autoTok = string.Join(",", th.autoTokens.Select(t => t.ToString()).ToArray());
//Debug.LogFormat("{0} Auto Tokens: {1}", T.Name, autoTok);
var alTok = string.Join(",", th.alwaysSendTokens.Select(t => t.ToString()).ToArray());
//Debug.LogFormat("{0} Alwy Tokens: {1}", T.Name, alTok);
}
public virtual void Awake() {
if (this is IEarlyAutoRegister)
Register();
else if (this is IAutoRegister)
StartCoroutine(DeferredRegister());
}
IEnumerator DeferredRegister() {
while (!NetworkManager.inRoom)
yield return null;
Register();
}
/// <summary>
/// Network variable attribute, specifying the desired token.
/// Set alwaysReliable to hint that a reliable update is required
/// Set alwaysSend to always include the variable in all dispatches
/// </summary>
/// <remarks>
/// Always Reliable -> Token must be sent reliably every time
/// Always Send -> Token will be sent whenever any other token is sent
/// updateMs -> If set, the token will automatically dispatch every updateMs milliseconds
/// </remarks>
[System.AttributeUsage(System.AttributeTargets.Field,AllowMultiple=false)]
public class NetVar : Attribute {
public readonly char token;
public readonly bool alwaysReliable, alwaysSend;
public readonly float updateTime;
public NetVar(char token, bool alwaysReliable = false, bool alwaysSend = false, int updateMs = -1) {
this.token = token;
this.alwaysReliable = alwaysReliable;
this.alwaysSend = alwaysSend;
this.updateTime = updateMs / 1000f; // Convert milliseconds to seconds
}
}
/// <summary>
/// This attribute describes a networked event function; This function must be non-static and is called on a specific entity.
/// It may have any network serializable parameters
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Method,AllowMultiple=false)]
public class NetEvent : Attribute {
public readonly char token;
public NetEvent(char token) {
this.token = token;
}
}
/// <summary>
/// Attach to a static field returning either a GameObject or EntityBase
/// This function will be called to create a networked entity.
/// The method may contain any number of parameters that are serializable
/// The EntityID
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Method)]
public class Instantiation : Attribute { }
#endregion
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4c9b2b5002413374ca9dd3d2f5d26bcf
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,476 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.Reflection;
using ExitGames.Client.Photon;
using ExitGames.Client.Photon.LoadBalancing;
using Hashtable = ExitGames.Client.Photon.Hashtable;
using Type = System.Type;
using Action = System.Action;
namespace EntityNetwork {
public static class EntityManager {
public static Dictionary<int,EntityBase> entities = new Dictionary<int,EntityBase>();
/// <summary>
/// Get an unused EntityID for a given PlayerID.
/// This ID is ensured to be unique for the client, assuming each client has a different PlayerID it will not collide.
/// Each playerID's ID space is about two hundred and sixty eight million IDs.
/// </summary>
/// <returns>An unused EntityBase ID number</returns>
/// <param name="playerID">The player number, ranged [0,127]</param>
public static int GetUnusedID(int playerID) {
if (playerID > 127)
throw new System.ArgumentOutOfRangeException("playerID cannot exceed 127");
if (playerID < 0)
throw new System.ArgumentOutOfRangeException("playerID cannot be less than zero");
// Fill all but the topmost byte randomly, then the topmost byte will be an sbyte for player id
int player = playerID << 28;
int randomInt = Random.Range(0, 0x0FFFFFFF);
int proposedID = player | randomInt;
// Recursively dig for new ID's on collision
while (entities.ContainsKey(proposedID)) {
proposedID = GetUnusedID(playerID);
}
return proposedID;
}
/// <summary>
/// Get a reference to an entity of a given ID.
/// There is a chance that this entity may have been deleted.
/// </summary>
/// <param name="id">Entity ID</param>
public static EntityBase Entity(int id) {
EntityBase eb;
return entities.TryGetValue(id, out eb) ? eb : null;
}
/// <summary>
/// Get a reference to an entity of a given ID.
/// There is a chance that this entity may have been deleted.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="id"></param>
/// <returns></returns>
public static T Entity<T>(int id) where T : EntityBase{
return Entity(id) as T;
}
private static void CleanEntities() {
var toRemove = new List<int>(entities.Count);
foreach(var pair in entities)
if (!pair.Value) toRemove.Add(pair.Key);
foreach (var key in toRemove)
entities.Remove(key);
}
/// <summary>
/// Register an entity to receive network events/updates.
/// This will fail if the EntityID is already in use.
/// </summary>
/// <remarks>Registering an entity validates all existing entities and will unsubcribe dead entities.</remarks>
public static void Register(EntityBase eb) {
CleanEntities();
//Debug.LogFormat("Registered Entity {0} : {1}",eb.name,eb.EntityID);
//Debug.LogFormat("{0} -> {1}", eb.name, string.Join(", ", eb.GetType().GetInterfaces().Select(t => t.Name).ToArray()));
if (eb is IAutoSerialize) {
eb.StartCoroutine(autoDispatchEntity(eb));
}
if (entities.ContainsKey(eb.EntityID)) {
var otherEntity = Entity(eb.EntityID);
Debug.LogErrorFormat(eb, "{0} has attempted to register over an existing ID {1}, Which belongs to {2}",
eb.gameObject.name, eb.EntityID, otherEntity.gameObject.name);
throw new System.Exception("Entity ID already in use!");
}
entities.Add(eb.EntityID, eb);
}
/// <summary>
/// Deregister an EntityBase. This requires enumeraing all entities and is slower than just destroying the EntityBase
/// However, in certain cases, like re-registering as a new pooled object or if the object must exist after being removed, it's worth using
/// </summary>
public static void DeRegister(EntityBase eb) {
// Grab all keyvaluepairs where the entity is the entity base being deregistered - ToArray is used to collapse the linq to avoid sync issues
var toRemove = entities.Select(t => new {id = t.Key, Entity = t.Value}).Where(t => t.Entity == eb).ToArray();
foreach(var removal in toRemove) {
entities.Remove(removal.id);
}
}
public static IEnumerator autoDispatchEntity(EntityBase eb) {
Debug.LogFormat("Creating Serial Dispatcher for {0} : {1}", eb.name,eb.EntityID);
Hashtable h = new Hashtable();
while (true) {
h.Clear();
if (eb.isMine) {
int code = eb.SerializeAuto(h);
if (code != 0) {
// If code is 2, message should be reliable
// Debug.LogFormat("Dispatching {0}/{2}: {1}", eb.name, h.ToStringFull(), PhotonConstants.EntityUpdateCode);
NetworkManager.netMessage(PhotonConstants.EntityUpdateCode, h, code == 2);
}
}
yield return null;
}
}
static EntityManager() {
NetworkManager.netHook += OnNet;
NetworkManager.onLeave += AllowOrphanSuicidesAndCalls;
}
// Hook the main events
static void OnNet(EventData ev) {
if (ev.Code == PhotonConstants.EntityUpdateCode) {
var h = (Hashtable)ev[ParameterCode.Data];
// Reject self-aimed events
if ((int)ev[ParameterCode.ActorNr] == NetworkManager.localID){
// Show a red particle for an outgoing signal, before rejecting the event
var ebs = Entity((int)h[PhotonConstants.eidChar]);
NetworkManager.netParticle(ebs, Color.red);
return;
}
var eb = Entity((int)h[PhotonConstants.eidChar]);
if (eb) {
// Show a blue particle for an incoming singal
NetworkManager.netParticle(eb, Color.blue);
if (eb is IAutoDeserialize) {
eb.DeserializeFull(h);
}
eb.Deserialize(h);
}
}
if (ev.Code == PhotonConstants.EntityEventCode) {
var h = (Hashtable)ev[ParameterCode.Data];
// --- Static Events ---
// Param labeled 2 in the hashtable is the EntityBase's ID Type, if a static event call, so if the table contains key 2, run it as a static event
object idObject;
if (h.TryGetValue(2,out idObject)) {
var typeID = (int)idObject;
Type entityType;
try {
entityType = EntityBase.TypeFromID(typeID);
} catch {
throw new System.Exception("Attempting to call static event on a non-existant type");
}
var controlChar = (char)h[0];
object paramObject;
if (h.TryGetValue(1,out paramObject)) {
EntityBase.InternallyInvokeStatic(entityType, controlChar,(object[])paramObject);
} else {
EntityBase.InternallyInvokeStatic(entityType, controlChar, null);
}
return;
}
// --- Instance Events ---
var eb = Entity((int)h[PhotonConstants.eidChar]);
if (eb) {
var controlChar = (char)h[0];
object paramObject;
if (h.TryGetValue(1,out paramObject)) {
eb.InternallyInvokeEvent(controlChar,(object[])paramObject);
} else {
eb.InternallyInvokeEvent(controlChar, null);
}
}
}
if (ev.Code == PhotonConstants.EntityInstantiateCode) {
var h = (Hashtable)ev[ParameterCode.Data];
DeserializeInstantiate(h);
}
}
/// <summary>
/// Generate a hashtable describing an object instantiaton for use with DeserializeInstantiate
/// Use helper method Instantiate to automatically call and fire this as an event.
/// </summary>
/// <seealso cref="DeserializeInstantiate"/>
public static Hashtable SerializeInstantiate<T>(int authID, Vector3 pos, Quaternion rot, params object[] param) {
var H = new Hashtable();
//H.Add('T', typeof(T).ToString());
H.Add('O', authID);
H.Add('I', GetUnusedID(authID));
H.Add('T', typeof(T).FullName);
H.Add('P', pos);
H.Add('R', rot);
H.Add('p', param);
return H;
}
/// <summary>
/// Locally creates the instantiated object described in Hashtable H.
/// </summary>
/// <seealso cref="Instantiate"/>
public static void DeserializeInstantiate(Hashtable H) {
CheckInstantiators();
//Debug.Log(H.ToStringFull());
var type = typeLookup[H['T'] as string];
var eid = (int)H['I'];
var ID = (int)H['O'];
var pos = (Vector3)H['P'];
var rot = (Quaternion)H['R'];
var options = H['p'] as object[];
ActionInstantiate(ID, eid, type, pos, rot, options);
//Instantiate<type>(pos, rot, options);
}
#region Instantiation
// Instantiation uses InstanceGameObject / InstanceGameEntity attributes
// Actually construct an instantiator object
private static void ActionInstantiate(int authID, int entityID, Type T, Vector3 pos, Quaternion rot, object[] param) {
MethodInfo mi;
if (!InstantiateMethods.TryGetValue(T, out mi)) {
throw new System.Exception(string.Format("Type {0} doesn't have an Instantiate Attributed method and isn't Instantiable.", T.Name));
}
if (typeof(GameObject).IsAssignableFrom(mi.ReturnType)) {
var val = mi.Invoke(null, param) as GameObject;
var go = Object.Instantiate<GameObject>(val, pos, rot);
// Attempt to set the ID of the entitybase
var eb = go.GetComponentInChildren<EntityBase>();
if (eb) {
eb.EntityID = entityID;
eb.authorityID = authID;
}
go.SendMessage("OnInstantiate", SendMessageOptions.DontRequireReceiver);
return;
}
var rt = mi.ReturnType;
if (typeof(EntityBase).IsAssignableFrom(rt)) {
var eb = mi.Invoke(null, param) as EntityBase;
eb.authorityID = authID;
eb.EntityID = entityID;
var go = eb.gameObject;
var t = eb.transform;
if (pos != Vector3.zero)
t.position = pos;
if (rot != Quaternion.identity)
t.rotation = rot;
go.SendMessage("OnInstantiate", SendMessageOptions.DontRequireReceiver);
return;
}
throw new System.Exception(string.Format("Type {0}'s Instantiate Method doesn't return an EntityBase or GameObject", T.Name));
}
// Helper dictionaries. typeLookup is to help us send types over the wire, InstantiateMethods stores each types instantiator
static Dictionary<string,System.Type> typeLookup;
static Dictionary<System.Type,MethodInfo> InstantiateMethods;
// This is a mess of autodocumentation, mostly due to usage of params and overloads.
/// <summary>
/// Activate type T's EntityBase.Instantation attribute remotely with given parameters, Generating and assigning the appropriate actor ID
/// This method returns the HashTable describing the instantation request that can be used to also create the object locally.
/// </summary>
/// <seealso cref="DeserializeInstantiate"/>
public static Hashtable Instantiate<T>(int authID, params object[] param) { return Instantiate<T>(authID, Vector3.zero, Quaternion.identity, param); }
/// <summary>
/// Activate type T's EntityBase.Instantation attribute remotely with given parameters, Generating and assigning the appropriate actor ID
/// This method returns the HashTable describing the instantation request that can be used to also create the object locally.
/// </summary>
/// <seealso cref="DeserializeInstantiate"/>
public static Hashtable Instantiate<T>(int authID, Vector3 pos, params object[] param) { return Instantiate<T>(authID, pos, Quaternion.identity, param); }
/// <summary>
/// Activate type T's EntityBase.Instantation attribute remotely with given parameters, Generating and assigning the appropriate actor ID
/// This method returns the HashTable describing the instantation request that can be used to also create the object locally.
/// </summary>
/// <seealso cref="DeserializeInstantiate"/>
public static Hashtable Instantiate<T>(int authID, Vector3 pos, Quaternion rot) { return Instantiate<T>(authID, pos, rot, null); }
/// <summary>
/// Activate type T's EntityBase.Instantation attribute remotely with given parameters, Generating and assigning the appropriate actor ID
/// This method returns the HashTable describing the instantation request that can be used to also create the object locally.
/// </summary>
/// <seealso cref="DeserializeInstantiate"/>
public static Hashtable Instantiate<T>(int authID, Vector3 pos, Quaternion rot,params object[] param) {
var table = SerializeInstantiate<T>(authID, pos, rot, param);
if (NetworkManager.isReady) {
NetworkManager.net.OpRaiseEvent(PhotonConstants.EntityInstantiateCode, table, true, RaiseEventOptions.Default);
}
return table;
}
static bool InstantiatorsBuilt = false;
static void CheckInstantiators() {
if (InstantiatorsBuilt) return;
BuildInstantiators();
InstantiatorsBuilt = true;
}
// Gather all the instantiaton attributes on entity classes
static void BuildInstantiators() {
Debug.Log("Buiding Instantiator cache");
InstantiateMethods = new Dictionary<System.Type,MethodInfo>();
typeLookup = new Dictionary<string,System.Type>();
var ebT = typeof(EntityBase);
var AllEntityTypes =
System.AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(t => ebT.IsAssignableFrom(t));
//var AllEntityTypes = Assembly.GetTypes().Where(t => ebT.IsAssignableFrom(t));
foreach(var entityType in AllEntityTypes) {
var methods = entityType.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
typeLookup.Add(entityType.FullName, entityType);
//Debug.LogFormat("Scanning Type {0}", entityType);
foreach(var method in methods) {
//Debug.LogFormat("Scanning Method {0}", method.Name);
// First look for a GameObject instantiator
var ia = method.GetCustomAttributes(typeof(EntityBase.Instantiation),true).FirstOrDefault() as EntityBase.Instantiation;
if (ia != null) {
InstantiateMethods.Add(entityType, method);
Debug.LogFormat("Registering Instantiator {0} for {1} (R: {2})",method.Name,entityType.FullName,method.ReturnType.ToString());
}
}
}
}
static void AllowOrphanSuicidesAndCalls(EventData ev) {
var toKill = new List<int>();
var players = NetworkManager.net.CurrentRoom.Players.Select(t => t.Value.ID);
foreach(var pair in entities) {
var e = pair.Value;
if (e is IAutoKillOrphans) {
if (e.authorityID == -1) continue;
if (players.Contains(e.authorityID)) continue;
toKill.Add(e.EntityID);
}
// Send out the orphan callbacks
if (e is IOrphanCallback) {
if (e.authorityID == -1) return;
if (players.Contains(e.authorityID)) continue;
(e as IOrphanCallback).OnOrphaned();
}
}
// Kill the orphans
foreach(var killable in toKill) {
if (Application.isEditor || Debug.isDebugBuild) {
var killEntity = Entity(killable);
Debug.LogFormat("Destroying orphaned entity {0} as it's owner {1} has left the room.",killEntity.gameObject.name,killEntity.authorityID);
}
Object.Destroy(Entity(killable).gameObject);
}
}
#endregion
}
/// <summary>
/// Specify that deserialization should be automaticly handled.
/// All registered field tokens will be automaticly set using cached setters
/// This is not appropriate if you have custom serialization/deserialization logic
/// </summary>
public interface IAutoDeserialize {}
/// <summary>
/// Specify that automatic token handling should be performed on the entity.
/// In most cases, this should remove the need to write custom serializers
/// This only applies to NetVar's with alwaysSend or updateTime set
/// </summary>
public interface IAutoSerialize {}
/// <summary>
/// Only appropriate for Entities with fixed, pre-determined ID's.
/// The entity will attempt to register itself on Awake()
/// </summary>
public interface IAutoRegister {}
/// <summary>
/// Only appropriate for Entities with fixed, pre-determined ID's.
/// The entity will absolutely to register itself on Awake()
/// </summary>
public interface IEarlyAutoRegister {}
/// <summary>
/// Assign to an EntityBase so that any time an AuthorityID would be checked we instead check if we're the room master
/// Used to clarify network ownership for objects that aren't owned by a player but instead by the room itself
/// </summary>
public interface IMasterOwnsUnclaimed {}
/// <summary>
/// When the authority player disconnects, destroy the entity and attached gameobject that aren't owned by players (EXCEPT with AuthID -1)
/// </summary>
public interface IAutoKillOrphans {}
/// <summary>
/// Adds an OnOrphaned callback - Note this is run whenever a player quits and we are unclaimed without a -1 authority, not just when our authority quits.
/// </summary>
public interface IOrphanCallback {
void OnOrphaned();
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 118662652baff8841bb52034c1795789
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: