public bool ScheduleChange(MeshNetworkIdentity id, StateChange change, ref IDContainer idReference) { //Debug.Log("Schedule change: my identity has objectID = " + GetIdentity().GetObjectID()); if (GetIdentity().meshnetReference.database == null) { Debug.LogError("Can't schedule a state change without an active database"); return(false); } ushort transactionID = GetNextTransactionID(); callbackRegistry.Add(transactionID, idReference); callbackTimers.Add(transactionID, Time.time); StateChangeTransaction transaction = new StateChangeTransaction(transactionID, change, id); MeshPacket p = new MeshPacket(); p.SetContents(transaction.GetSerializedBytes()); p.SetPacketType(PacketType.DatabaseChangeRequest); //Debug.Log("Setting packet with source object ID = " + GetIdentity().GetObjectID()); p.SetSourceObjectId(GetIdentity().GetObjectID()); p.SetSourcePlayerId(GetIdentity().meshnetReference.GetLocalPlayerID()); p.SetTargetObjectId((ushort)ReservedObjectIDs.DatabaseObject); p.SetTargetPlayerId(netDB.GetIdentity().GetOwnerID()); GetIdentity().RoutePacket(p); return(true); }
/* * 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()); } } }