Esempio n. 1
0
        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;
            }
        }
        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;
            }
        }
Esempio n. 3
0
        public void PartModuleFieldChange(PartModule module, string fieldName)
        {
            var vessel = module.vessel;

            if (vessel == null || !vessel.loaded || vessel.protoVessel == null)
            {
                return;
            }

            var part = module.part;

            if (part == null)
            {
                return;
            }

            if (FieldModuleStore.InheritanceTypeChain.TryGetValue(module.moduleName, out var inheritChain))
            {
                foreach (var baseModuleName in inheritChain)
                {
                    if (FieldModuleStore.ModuleFieldsDictionary.TryGetValue(baseModuleName, out var definition))
                    {
                        if (definition.PersistentModuleField.TryGetValue(fieldName, out var fieldInfo))
                        {
                            var customizationResult = CustomizationsHandler.SkipModule(vessel.id, part.flightID, baseModuleName, fieldInfo.Name, false, out _);
                            if (customizationResult == CustomizationResult.Ignore)
                            {
                                return;
                            }

                            var fieldVal    = fieldInfo.GetValue(module).ToInvariantString();
                            var snapshotVal = module.snapshot?.moduleValues.GetValue(fieldInfo.Name);

                            if (snapshotVal != null && fieldVal != null && fieldVal != snapshotVal)
                            {
                                switch (customizationResult)
                                {
                                case CustomizationResult.TooEarly:     //Do not update anything and wait until next time
                                    break;

                                case CustomizationResult.Ok:
                                    module.snapshot?.moduleValues?.SetValue(fieldInfo.Name, fieldVal);
                                    LunaLog.Log($"Detected a part module change. FlightId: {part.flightID} PartName: {part.name} Module: {module.moduleName} BaseModule: {baseModuleName} " +
                                                $"Field: {fieldInfo.Name} ValueBefore: {snapshotVal} ValueNow: {fieldVal}");
                                    System.MessageSender.SendVesselPartSyncMsg(vessel.id, part.flightID, module.moduleName, baseModuleName, fieldInfo.Name, fieldVal);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        public void CheckAndSendVesselUpdate(Vessel vessel)
        {
            if (vessel == null || !vessel.loaded || vessel.protoVessel == null)
            {
                return;
            }

            for (var i = 0; i < vessel.Parts.Count; i++)
            {
                var part = vessel.parts[i];
                for (var j = 0; j < part.Modules.Count; j++)
                {
                    var module = part.Modules[j];
                    if (FieldModuleStore.InheritanceTypeChain.TryGetValue(module.moduleName, out var inheritChain))
                    {
                        foreach (var baseModuleName in inheritChain)
                        {
                            if (FieldModuleStore.ModuleFieldsDictionary.TryGetValue(baseModuleName, out var definition))
                            {
                                foreach (var fieldInfo in definition.PersistentModuleField)
                                {
                                    var customizationResult = CustomizationsHandler.SkipModule(vessel.id, part.flightID, baseModuleName, fieldInfo.Name, false, out _);

                                    var fieldVal    = fieldInfo.GetValue(module).ToInvariantString();
                                    var snapshotVal = module.snapshot?.moduleValues.GetValue(fieldInfo.Name);

                                    if (snapshotVal != null && fieldVal != null && fieldVal != snapshotVal)
                                    {
                                        switch (customizationResult)
                                        {
                                        case CustomizationResult.TooEarly:     //Do not update anything and wait until next time
                                            break;

                                        case CustomizationResult.Ignore:     //Update our proto only
                                            module.snapshot?.moduleValues?.SetValue(fieldInfo.Name, fieldVal);
                                            break;

                                        case CustomizationResult.Ok:
                                            module.snapshot?.moduleValues?.SetValue(fieldInfo.Name, fieldVal);
                                            LunaLog.Log($"Detected a part module change. FlightId: {part.flightID} PartName: {part.name} Module: {module.moduleName} BaseModule: {baseModuleName} " +
                                                        $"Field: {fieldInfo.Name} ValueBefore: {snapshotVal} ValueNow: {fieldVal}");
                                            MessageSender.SendVesselPartSyncMsg(vessel.id, part.flightID, module.moduleName, baseModuleName, fieldInfo.Name, fieldVal);
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }