public static void UpdateVesselProtoPartModules(VesselPartSyncMsgData msgData) { if (AllPlayerVessels.TryGetValue(msgData.VesselId, out var vesselProtoUpd)) { if (vesselProtoUpd.ProtoVessel == null) { return; } var partSnapshot = VesselCommon.FindProtoPartInProtovessel(vesselProtoUpd.ProtoVessel, msgData.PartFlightId); if (partSnapshot != null) { var module = VesselCommon.FindProtoPartModuleInProtoPart(partSnapshot, msgData.ModuleName); module?.moduleValues.SetValue(msgData.FieldName, msgData.Value); } } }
private static void UpdateVesselValues(ProtoVessel protoVessel, VesselPartSyncMsgData msgData) { if (protoVessel == null) { return; } var part = VesselCommon.FindProtoPartInProtovessel(protoVessel, msgData.PartFlightId); if (part != null) { var module = VesselCommon.FindProtoPartModuleInProtoPart(part, msgData.ModuleName); if (module != null) { module.moduleValues.SetValue(msgData.FieldName, msgData.Value); UpdateVesselModuleIfNeeded(protoVessel.vesselID, part.flightID, msgData, module, part); } } }
private static void UpdateVesselModuleIfNeeded(Guid vesselId, uint partFlightId, VesselPartSyncMsgData msgData, ProtoPartModuleSnapshot module, ProtoPartSnapshot part) { if (module.moduleRef == null) { return; } switch (CustomizationsHandler.SkipModule(vesselId, partFlightId, msgData.BaseModuleName, msgData.FieldName, true, out var customization)) { case CustomizationResult.TooEarly: case CustomizationResult.Ignore: break; case CustomizationResult.Ok: if (customization.IgnoreSpectating && FlightGlobals.ActiveVessel?.id == vesselId) { break; } module.moduleRef?.Load(module.moduleValues); if (customization.CallOnAwake) { module.moduleRef?.OnAwake(); } if (customization.CallOnLoad) { module.moduleRef?.OnLoad(module.moduleValues); } if (customization.CallOnStart) { module.moduleRef?.OnStart(part.partRef.GetModuleStartState()); } break; } }
/// <summary> /// Updates the proto vessel with the values we received about a part module change of a vessel /// </summary> private static string UpdateProtoVesselFileWithNewPartModulesData(string vesselData, VesselPartSyncMsgData msgData) { var document = new XmlDocument(); document.LoadXml(vesselData); var module = $@"/{ConfigNodeXmlParser.StartElement}/{ConfigNodeXmlParser.ParentNode}[@name='PART']/{ConfigNodeXmlParser.ValueNode}[@name='uid' and text()=""{msgData.PartFlightId}""]/" + $"following-sibling::{ConfigNodeXmlParser.ParentNode}[@name='MODULE']/{ConfigNodeXmlParser.ValueNode}" + $@"[@name='name' and text()=""{msgData.ModuleName}""]/parent::{ConfigNodeXmlParser.ParentNode}[@name='MODULE']/"; var xpath = $"{module}/{ConfigNodeXmlParser.ValueNode}[@name='{msgData.FieldName}']"; var fieldNode = document.SelectSingleNode(xpath); if (fieldNode != null) { fieldNode.InnerText = msgData.Value; } return(document.ToIndentedString()); }
private static void UpdateVesselModuleIfNeeded(Guid vesselId, uint partFlightId, VesselPartSyncMsgData msgData, ProtoPartModuleSnapshot module, ProtoPartSnapshot part) { if (module.moduleRef == null) { return; } switch (CustomizationsHandler.SkipModule(vesselId, partFlightId, msgData.BaseModuleName, msgData.FieldName, true, out var customization)) { case CustomizationResult.TooEarly: case CustomizationResult.Ignore: break; case CustomizationResult.Ok: if (customization.IgnoreSpectating && FlightGlobals.ActiveVessel?.id == vesselId) { break; } if (customization.SetValueInModule) { if (FieldModuleStore.ModuleFieldsDictionary.TryGetValue(msgData.BaseModuleName, out var moduleDef)) { var fieldDef = moduleDef.PersistentModuleField.FirstOrDefault(f => f.Name == msgData.FieldName); if (fieldDef != null) { var convertedVal = Convert.ChangeType(msgData.Value, fieldDef.FieldType); if (convertedVal != null) { fieldDef.SetValue(module.moduleRef, convertedVal); } } } } if (customization.CallLoad) { module.moduleRef.Load(module.moduleValues); } if (customization.CallOnAwake) { module.moduleRef.OnAwake(); } if (customization.CallOnLoad) { module.moduleRef.OnLoad(module.moduleValues); } if (customization.CallOnStart) { module.moduleRef.OnStart(part.partRef.GetModuleStartState()); } break; } }