public DatabaseChangeResult ChangeObject(MeshNetworkIdentity i, bool publishChange) { if (objectList.ContainsKey(i.GetObjectID()) == false) { Debug.LogError("Object that was requested to be changed does not exist!"); return(new DatabaseChangeResult(false, "Object does not exist")); } if (i.GetObjectID() == (ushort)ReservedObjectIDs.DatabaseObject) { Debug.LogError("Tried to change database. This action is prohibited."); //maybe not in the future... return(new DatabaseChangeResult(false, "Can't change database")); } objectList[i.GetObjectID()].DeepCopyAndApply(i); if (publishChange) { if (GetAuthorized()) { SendObjectUpdate(i, StateChange.Change); return(new DatabaseChangeResult(true, i.GetObjectID())); } else { Debug.LogError("Publishing changes not authorized."); return(new DatabaseChangeResult(false, "Publishing changes not authorized")); } } else { return(new DatabaseChangeResult(true, i.GetObjectID())); } }
public DatabaseChangeResult RemoveObject(MeshNetworkIdentity i, bool publishChange) { if (objectList.ContainsKey(i.GetObjectID()) == false) { return(new DatabaseChangeResult(false, "Object does not exist")); } if (i.GetObjectID() == (ushort)ReservedObjectIDs.DatabaseObject) { Debug.LogError("Tried to remove database. Bad idea."); return(new DatabaseChangeResult(false, "Can't remove database.")); } objectList.Remove(i.GetObjectID()); if (publishChange) { if (GetAuthorized()) { SendObjectUpdate(i, StateChange.Removal); return(new DatabaseChangeResult(true, i.GetObjectID())); } else { Debug.LogError("Publishing changes not authorized."); return(new DatabaseChangeResult(false, "Publishing changes not authorized")); } } else { return(new DatabaseChangeResult(true, i.GetObjectID())); } }
public GameObject SpawnDatabase(MeshNetworkIdentity i) { if (meshnet == null) { Debug.LogError("Trying to spawn object when underlying mesh network not intialized."); return(null); } if (i.GetObjectID() != (ushort)ReservedObjectIDs.DatabaseObject) { Debug.LogError("Trying to use database spawning method for non-database object"); return(null); } i.SetMeshnetReference(meshnet); //set a reference to the mesh network if (networkPrefabs.ContainsKey(i.GetPrefabID()) == false) { Debug.LogError("NetworkPrefab registry error: Requested prefab ID does not exist."); return(null); } GameObject g = Instantiate(networkPrefabs[i.GetPrefabID()]); IdentityContainer c = g.GetComponent <IdentityContainer>(); if (c == null) { Debug.LogError("NetworkPrefab error: spawned prefab does not contain IdentityContainer"); return(null); } c.SetIdentity(i); activeObjects.Add(i.GetObjectID(), g); return(g); }
//Performs a deep copy of the given MNI and applies it to the existing object, preserving active pointers //Preserves the existing MeshNetwork reference and the existing list of attached components //These are not serialized across the network (obviously) and thus should not be deepcopied public void DeepCopyAndApply(MeshNetworkIdentity i) { SetLocked(i.GetLocked()); SetObjectID(i.GetObjectID()); SetOwnerID(i.GetOwnerID()); SetPrefabID(i.GetPrefabID()); }
IEnumerator SpawnCapsule() { float timeStart = Time.time; MeshNetworkIdentity requestedID = new MeshNetworkIdentity(0, prefabID, meshnet.GetLocalPlayerID(), false); IDContainer returnedObjectID = new IDContainer((ushort)ReservedObjectIDs.Unspecified); scheduler.ScheduleChange(requestedID, StateChange.Addition, ref returnedObjectID); while (returnedObjectID.id == (ushort)ReservedObjectIDs.Unspecified) { if (Time.time - timeStart > SPAWN_TIMEOUT) { Debug.LogError("Spawn timeout"); yield break; } yield return(new WaitForEndOfFrame()); } MeshNetworkIdentity newIdentity = meshnet.database.LookupObject(returnedObjectID.id); GameObject g = meshnet.game.GetObjectByIdentity(newIdentity.GetObjectID()); if (prefabID != 4) { g.AddComponent <BasicMovementTest>(); } yield break; yield return(new WaitForSeconds(1)); timeStart = Time.time; returnedObjectID = new IDContainer((ushort)ReservedObjectIDs.Unspecified); scheduler.ScheduleChange(newIdentity, StateChange.Removal, ref returnedObjectID); while (returnedObjectID.id == (ushort)ReservedObjectIDs.Unspecified) { if (Time.time - timeStart > SPAWN_TIMEOUT) { Debug.LogError("Spawn timeout"); yield break; } yield return(new WaitForEndOfFrame()); } }
//Object modification methods public DatabaseChangeResult AddObject(MeshNetworkIdentity i, bool publishChange) { if (objectList.ContainsKey(i.GetObjectID())) { return(new DatabaseChangeResult(false, "Object already exists")); } if (i.GetObjectID() == (ushort)ReservedObjectIDs.DatabaseObject) { } //If the object isn't the database, we need to assign an available objectID if (i.GetObjectID() != (ushort)ReservedObjectIDs.DatabaseObject) { IDAssignmentResult idresult = GetAvailableObjectID(); //Look for an available object id if (idresult.success) { i.SetObjectID(idresult.id); } else { return(new DatabaseChangeResult(false, "No object id available for use. Too many objects?")); } } objectList.Add(i.GetObjectID(), i); if (publishChange) { if (GetAuthorized()) { SendObjectUpdate(i, StateChange.Addition); return(new DatabaseChangeResult(true, i.GetObjectID())); } else { Debug.LogError("Publishing changes not authorized."); return(new DatabaseChangeResult(false, "Publishing changes not authorized")); } } else { return(new DatabaseChangeResult(true, i.GetObjectID())); } }
/* * Users SHOULD be able to: * Request object creation and assign the object to themselves * Request object ownership change of others' objects if the object is unlocked * Request object deletion of their own objects * * * Users SHOULD NOT be able to: * Request object deletion of others' objects * Create objects and make other players the owner * Request object ownership change of others' objects if the object is locked * Request changing objects' objectID, prefabID, or any other * */ public void ConsiderChangeRequest(MeshPacket p) { if (GetAuthorized() == false) { Debug.LogError("Being asked to change the database when not authorized!"); return; } if (LookupPlayer(p.GetSourcePlayerId()) == null) { Debug.LogError("Change request source player does not exist on the database"); return; } StateChangeTransaction transaction = StateChangeTransaction.ParseSerializedBytes(p.GetContents()); if (transaction.GetChangeType() == StateChange.Addition) { //Check if the desired owner is the packet sender if (p.GetSourcePlayerId() != transaction.GetObjectData().GetOwnerID()) { Debug.LogError("Requested object creation tries to assign to new player: prohibited"); return; } DatabaseChangeResult result = AddObject(transaction.GetObjectData(), true); if (result.success == false) { Debug.LogError("Requested object addition failed: " + result.error); return; } else { game.SpawnObject(result.data); EchoChangeRequest(transaction.GetTransactionID(), result.data, p.GetSourcePlayerId()); } } else if (transaction.GetChangeType() == StateChange.Removal) { //Check if requesting user owns the object if (p.GetSourcePlayerId() != transaction.GetObjectData().GetOwnerID()) { Debug.LogError("User trying to remove an object that doesn't belong to them"); return; } MeshNetworkIdentity localObjectToRemove = LookupObject(transaction.GetObjectData().GetObjectID()); if (localObjectToRemove == null) { Debug.LogError("Couldn't find the object requested to be removed."); return; } DatabaseChangeResult result = RemoveObject(LookupObject(transaction.GetObjectData().GetObjectID()), true); if (result.success == false) { Debug.LogError("Requested object removal failed: " + result.error); return; } else { game.RemoveObject(result.data); EchoChangeRequest(transaction.GetTransactionID(), localObjectToRemove.GetObjectID(), p.GetSourcePlayerId()); } } else if (transaction.GetChangeType() == StateChange.Change) { if (p.GetSourcePlayerId() != transaction.GetObjectData().GetOwnerID()) { Debug.LogError("User trying to change an object that doesn't belong to them"); return; } MeshNetworkIdentity localObjectToChange = LookupObject(transaction.GetObjectData().GetObjectID()); if (localObjectToChange == null) { Debug.LogError("Couldn't find the object requested to be changed."); return; } DatabaseChangeResult result = ChangeObject(LookupObject(transaction.GetObjectData().GetObjectID()), true); if (result.success == false) { Debug.LogError("Requested object change failed: " + result.error); return; } else { EchoChangeRequest(transaction.GetTransactionID(), localObjectToChange.GetObjectID(), p.GetSourcePlayerId()); } } }