public ProtoVessel CreateProtoVessel() { var configNode = ConfigNodeSerializer.Deserialize(RawData, NumBytes); if (configNode == null || VesselCommon.VesselHasNaNPosition(configNode)) { LunaLog.LogError($"Received a malformed vessel from SERVER. Id {VesselId}"); VesselRemoveSystem.Singleton.KillVessel(VesselId, "Malformed vessel"); VesselRemoveSystem.Singleton.AddToKillList(VesselId, "Malformed vessel"); return(null); } var newProto = VesselSerializer.CreateSafeProtoVesselFromConfigNode(configNode, VesselId); if (newProto == null) { LunaLog.LogError($"Received a malformed vessel from SERVER. Id {VesselId}"); VesselRemoveSystem.Singleton.KillVessel(VesselId, "Malformed vessel"); VesselRemoveSystem.Singleton.AddToKillList(VesselId, "Malformed vessel"); return(null); } if (VesselCommon.ProtoVesselHasInvalidParts(newProto)) { return(null); } return(newProto); }
public void HandleMessage(IMessageData messageData) { var msgData = messageData as VesselDockMsgData; if (msgData == null) { return; } LunaLog.Log("[LMP]: Docking message received!"); if (FlightGlobals.ActiveVessel?.id == msgData.WeakVesselId) { LunaLog.Log("[LMP]: Docking NOT detected. We DON'T OWN the dominant vessel"); SystemsContainer.Get <VesselRemoveSystem>().AddToKillList(FlightGlobals.ActiveVessel, true); SystemsContainer.Get <VesselSwitcherSystem>().SwitchToVessel(msgData.DominantVesselId); } if (FlightGlobals.ActiveVessel?.id == msgData.DominantVesselId && !VesselCommon.IsSpectating) { var newProto = VesselSerializer.DeserializeVessel(msgData.FinalVesselData); if (VesselCommon.ProtoVesselHasChanges(FlightGlobals.ActiveVessel.protoVessel, newProto)) { LunaLog.Log("[LMP]: Docking NOT detected. We OWN the dominant vessel"); //We own the dominant vessel and dind't detected the docking event so we need to reload our OWN vessel //so if we send our own protovessel later, we send the updated definition SystemsContainer.Get <VesselProtoSystem>().VesselLoader.ReloadVessel(newProto); } } //Some other 2 players docked so just remove the weak vessel. SystemsContainer.Get <VesselRemoveSystem>().AddToKillList(FlightGlobals.FindVessel(msgData.WeakVesselId), true); SystemsContainer.Get <VesselProtoSystem>().HandleVesselProtoData(msgData.FinalVesselData, msgData.DominantVesselId); }
public void ControlAwake(MemoryBridge memoryBridge) { this.memoryBridge = memoryBridge; ToggleAutoPilot(true); mirrorMat = Resources.Load("MirrorMat", typeof(Material)) as Material; targetMat = Resources.Load("TargetMat", typeof(Material)) as Material; inRangeMat = Resources.Load("InRangeMat", typeof(Material)) as Material; // gimbalOffset = Vector3.zero; mirrorVessel = gameObject.AddComponent(typeof(VesselSerializer)) as VesselSerializer; vessel = mirrorVessel.DrawVessel(this.memoryBridge, mirrorMat, "Mirror Vessel COM"); Debug.Log("Mirror Vessel Built with Part Count : " + vessel.parts.Count); targetVessel = gameObject.AddComponent(typeof(VesselSerializer)) as VesselSerializer; targetVessel.DrawVessel(this.memoryBridge, targetMat, "Target Vessel COM"); // Debug.Log("Target Vessel Copied from Mirror"); vessel.transform.SetParent(transform.Find("Gimbal").transform); PIDpitch = new PID(PIDvaluesPitchHigh, PIDvaluesPitchLow); PIDyaw = new PID(PIDvaluesYawHigh, PIDvaluesYawLow); PIDroll = new PID(PIDvaluesRollHigh, PIDvaluesRollLow); GameObject gimbalObject = new GameObject(); gimbalObject.name = "Adjusted Gimbal"; adjustedGimbal = gimbalObject.transform; adjustedGimbal.SetParent(GameObject.Find("Gimbal").transform); // DebugVector.DrawVector(adjustedGimbal); targetVessel.vessel.SetParent(transform.Find("Gimbal").transform); memoryBridge.SetFloat("VesselYaw", 1.1f); memoryBridge.SetFloat("VesselPitch", 1.1f); memoryBridge.SetFloat("VesselRoll", 1.1f); //memoryBridge.SetFloat("testnull" , null); // targetVessel.gameObject.SetActive(false); // GameObject COMobject = new GameObject(); // COMobject.name = "COM"; // COM = COMobject.transform; // var vesselCOM = memoryBridge.LocalCOM; // var convertedCOM = new Vector3(vesselCOM.x, -vesselCOM.z, vesselCOM.y); // COM.localPosition = convertedCOM; //COM.SetParent(transform.parent); //transform.SetParent(COM); //COM.position = Vector3.zero; if (controlMode == FlightControlMode.RawStick) { targetVessel.vessel.gameObject.SetActive(false); } }
public void HandleMessage(IServerMessageBase msg) { if (!(msg.Data is VesselDockMsgData msgData)) { return; } LunaLog.Log("Docking message received!"); //Add the new vessel data to the store VesselsProtoStore.HandleVesselProtoData(msgData.FinalVesselData, msgData.NumBytes, msgData.DominantVesselId); if (FlightGlobals.ActiveVessel?.id == msgData.WeakVesselId) { LunaLog.Log($"Docking NOT detected. We DON'T OWN the dominant vessel. Switching to {msgData.DominantVesselId}"); SystemsContainer.Get <VesselRemoveSystem>().AddToKillList(FlightGlobals.ActiveVessel.id); SystemsContainer.Get <VesselSwitcherSystem>().SwitchToVessel(msgData.DominantVesselId); WarpSystem.WarpIfSubspaceIsMoreAdvanced(msgData.SubspaceId); } if (FlightGlobals.ActiveVessel?.id == msgData.DominantVesselId && !VesselCommon.IsSpectating) { var newProto = VesselSerializer.DeserializeVessel(msgData.FinalVesselData, msgData.NumBytes); /* This is a strange case were we didn't detect the docking on time and the weak vessel send the new definition... * Usually it happens when a vessel in a future subspace docks with a vessel in the past and the vessel in the past is the dominant vessel * The reason why is bad is because the ModuleDockingNode sent by the WEAK vessel will tell us that we are * NOT the dominant (because we received the vesselproto from the WEAK vessel) so we won't be able to undock properly... * This will be fixed if we go to the space center and reload again the vessel... */ LunaLog.Log("Docking NOT detected. We OWN the dominant vessel"); if (FlightGlobals.FindVessel(msgData.WeakVesselId) != null) { LunaLog.Log($"Weak vessel {msgData.WeakVesselId} still exists in our game. Removing it now"); SystemsContainer.Get <VesselRemoveSystem>().AddToKillList(msgData.WeakVesselId); SystemsContainer.Get <VesselRemoveSystem>().KillVessel(msgData.WeakVesselId); } /* We own the dominant vessel and dind't detected the docking event so we need to reload our OWN vessel * so if we send our own protovessel later, we send the updated definition */ LunaLog.Log($"Creating the missing parts in our own vessel. Current: {FlightGlobals.ActiveVessel.parts.Count} Expected: {newProto.protoPartSnapshots.Count}"); //ProtoToVesselRefresh.CreateMissingPartsInCurrentProtoVessel(FlightGlobals.ActiveVessel, newProto); VesselLoader.ReloadVessel(newProto); LunaLog.Log("Force sending the new proto vessel"); SystemsContainer.Get <VesselProtoSystem>().MessageSender.SendVesselMessage(FlightGlobals.ActiveVessel, true); WarpSystem.WarpIfSubspaceIsMoreAdvanced(msgData.SubspaceId); return; } //Some other 2 players docked so just remove the weak vessel. SystemsContainer.Get <VesselRemoveSystem>().AddToKillList(msgData.WeakVesselId); }
public void SendDockInformation(VesselDockStructure dock, int subspaceId) { var vesselBytes = VesselSerializer.SerializeVessel(dock.DominantVessel.BackupVessel()); if (vesselBytes.Length > 0) { CreateAndSendDockMessage(dock, subspaceId, vesselBytes); } }
public void SendDockInformation(Guid weakVesselId, Vessel dominantVessel, int subspaceId) { var vesselBytes = VesselSerializer.SerializeVessel(dominantVessel.BackupVessel()); if (vesselBytes.Length > 0) { CreateAndSendDockMessage(weakVesselId, dominantVessel.id, subspaceId, vesselBytes); } }
/// <summary> /// Adds a vessel manually to the dictionary. Use this to add your own spawned vessels. /// </summary> public static void AddOrUpdateVesselToDictionary(Vessel vessel) { var ownVesselData = VesselSerializer.SerializeVessel(vessel.protoVessel); if (ownVesselData.Length > 0) { var newProtoUpdate = new VesselProtoUpdate(ownVesselData, ownVesselData.Length, vessel.id); AllPlayerVessels.AddOrUpdate(vessel.id, newProtoUpdate, (key, existingVal) => newProtoUpdate); } }
/// <summary> /// This method uses a lot of memory so try to call it as less as possible and only when needed /// </summary> public void DeserializeVesselBytes() { lock (_vesselDataSyncLock) { _needToDeserializeData = false; var newVesselNode = ConfigNodeSerializer.Deserialize(_vesselData, _numBytes); if (!VesselCommon.VesselHasNaNPosition(newVesselNode)) { //In case there's a deserialization error skip it and keep the older node _vesselNode = newVesselNode; } if (_vesselNode == null) { LunaLog.LogError($"Received a malformed vessel from SERVER. Id {VesselId}"); VesselRemoveSystem.Singleton.KillVessel(VesselId, "Malformed vessel"); VesselRemoveSystem.Singleton.AddToKillList(VesselId, "Malformed vessel"); return; } var newProto = VesselSerializer.CreateSafeProtoVesselFromConfigNode(_vesselNode, VesselId); //In case there's a deserialization error skip it and keep the older proto if (newProto != null) { HasInvalidParts = VesselCommon.ProtoVesselHasInvalidParts(newProto); if (newProto.vesselID != VesselId) { LunaLog.LogError($"Tried to update the Vessel with a proto from a different vessel ID. Proto: {newProto.vesselID} CorrectId: {VesselId}"); } else { _deserializedProtoVessel = newProto; } } //If protovessel is still null then unfortunately we must remove that vessel as the server sent us a bad vessel if (_deserializedProtoVessel == null) { LunaLog.LogError($"Received a malformed vessel from SERVER. Id {VesselId}"); VesselRemoveSystem.Singleton.KillVessel(VesselId, "Malformed vessel"); VesselRemoveSystem.Singleton.AddToKillList(VesselId, "Malformed vessel"); } else { _vesselParts.Clear(); foreach (var protoPart in _deserializedProtoVessel.protoPartSnapshots) { _vesselParts.TryAdd(protoPart.flightID, protoPart); } } } }
public void SendDockInformation(VesselDockStructure dock) { var vesselBytes = VesselSerializer.SerializeVessel(dock.DominantVessel.protoVessel); if (vesselBytes.Length > 0) { SendMessage(new VesselDockMsgData { WeakVesselId = dock.WeakVesselId, DominantVesselId = dock.DominantVesselId, FinalVesselData = vesselBytes }); } }
public void SendDockInformation(VesselDockStructure dock, int subspaceId, ProtoVessel finalDominantVesselProto) { if (finalDominantVesselProto != null) { var vesselBytes = VesselSerializer.SerializeVessel(finalDominantVesselProto); if (vesselBytes.Length > 0) { CreateAndSendDockMessage(dock, subspaceId, vesselBytes); } } else { SendDockInformation(dock, subspaceId); } }
public void SendDockInformation(Guid weakVesselId, Vessel dominantVessel, int subspaceId, ProtoVessel finalDominantVesselProto) { if (finalDominantVesselProto != null) { var vesselBytes = VesselSerializer.SerializeVessel(finalDominantVesselProto); if (vesselBytes.Length > 0) { CreateAndSendDockMessage(weakVesselId, dominantVessel.id, subspaceId, vesselBytes); } } else { SendDockInformation(weakVesselId, dominantVessel, subspaceId); } }
public void HandleMessage(IServerMessageBase msg) { if (!(msg.Data is VesselDockMsgData msgData)) { return; } LunaLog.Log("Docking message received!"); if (msgData.WeakVesselId == CurrentDockEvent.WeakVesselId && msgData.DominantVesselId == CurrentDockEvent.DominantVesselId && (LunaNetworkTime.UtcNow - CurrentDockEvent.DockingTime) < TimeSpan.FromSeconds(5)) { LunaLog.Log("Docking message received was detected so ignore it"); return; } var dominantProto = VesselSerializer.DeserializeVessel(msgData.FinalVesselData, msgData.NumBytes); VesselLoader.LoadVessel(dominantProto); WarpSystem.WarpIfSubspaceIsMoreAdvanced(msgData.SubspaceId); if (FlightGlobals.ActiveVessel && FlightGlobals.ActiveVessel.id == msgData.WeakVesselId) { LunaLog.Log($"Docking NOT detected. We DON'T OWN the dominant vessel. Switching to {msgData.DominantVesselId}"); if (dominantProto.vesselRef != null) { dominantProto.vesselRef.Load(); dominantProto.vesselRef.GoOffRails(); FlightGlobals.ForceSetActiveVessel(dominantProto.vesselRef); } } else if (FlightGlobals.ActiveVessel && FlightGlobals.ActiveVessel.id == msgData.DominantVesselId) { LunaLog.Log("Docking NOT detected. We OWN the dominant vessel"); } VesselRemoveSystem.Singleton.KillVessel(msgData.WeakVesselId, "Killing weak (active) vessel during a docking that was not detected"); CurrentDockEvent.Reset(); }
public ProtoVessel CreateProtoVessel() { var configNode = RawData.DeserializeToConfigNode(NumBytes); if (configNode == null || configNode.VesselHasNaNPosition()) { LunaLog.LogError($"Received a malformed vessel from SERVER. Id {VesselId}"); VesselRemoveSystem.Singleton.KillVessel(VesselId, true, "Malformed vessel"); return(null); } var newProto = VesselSerializer.CreateSafeProtoVesselFromConfigNode(configNode, VesselId); if (newProto == null) { LunaLog.LogError($"Received a malformed vessel from SERVER. Id {VesselId}"); VesselRemoveSystem.Singleton.KillVessel(VesselId, true, "Malformed vessel"); return(null); } return(newProto); }
/// <summary> /// This method prepares the protovessel class and send the message, it's intended to be run in another thread /// </summary> private void PrepareAndSendProtoVessel(ProtoVessel protoVessel, bool forceReload) { //Never send empty vessel id's (it happens with flags...) if (protoVessel.vesselID == Guid.Empty) { return; } //VesselSerializedBytes is shared so lock it! lock (VesselArraySyncLock) { VesselSerializer.SerializeVesselToArray(protoVessel, VesselSerializedBytes, out var numBytes); if (numBytes > 0) { var msgData = NetworkMain.CliMsgFactory.CreateNewMessageData <VesselProtoMsgData>(); msgData.GameTime = TimeSyncSystem.UniversalTime; msgData.VesselId = protoVessel.vesselID; msgData.NumBytes = numBytes; msgData.ForceReload = forceReload; if (msgData.Data.Length < numBytes) { Array.Resize(ref msgData.Data, numBytes); } Array.Copy(VesselSerializedBytes, 0, msgData.Data, 0, numBytes); SendMessage(msgData); } else { if (protoVessel.vesselType == VesselType.Debris) { LunaLog.Log($"Serialization of debris vessel: {protoVessel.vesselID} name: {protoVessel.vesselName} failed. Adding to kill list"); VesselRemoveSystem.Singleton.KillVessel(protoVessel.vesselID, true, "Serialization of debris failed"); } } } }