Example #1
0
        private void FindBackgroundModules()
        {
            var protoPartSnapshots = vessel.protoVessel.protoPartSnapshots;

            foreach (var protoPartSnapshot in protoPartSnapshots)
            {
                // get the part prefab, so we can get the modules on the part
                var prefab = PartLoader.getPartInfoByName(protoPartSnapshot.partName).partPrefab;

                // and get the prefab modules
                var modulePrefabs        = prefab.FindModulesImplementing <IKITModule>();
                var protoModuleSnapshots = protoPartSnapshot.modules;

                var backgroundModules = protoModuleSnapshots.Zip(modulePrefabs, (snapshot, module) =>
                {
                    var backgroundFunctionInfo = BackgroundFunctionInfo.Instance(module);
                    var pm = module as PartModule;

                    System.Diagnostics.Debug.Assert(pm != null, nameof(pm) + " != null");

                    if (snapshot.moduleName != pm.moduleName)
                    {
                        Debug.Log($"[FindBackgroundModule] Uh oh, order of the prefab modules does not match the proto module order :(");
                        throw new Exception(
                            "[FindBackgroundModule] Uh oh, order of the prefab modules does not match the proto module order :(");
                    }

                    return(new BackgroundModule(backgroundFunctionInfo, vessel, protoPartSnapshot, snapshot, prefab,
                                                pm));
                }).ToArray();

                Debug.Log($"got {backgroundModules}");
            }
        }
Example #2
0
 public BackgroundModule(BackgroundFunctionInfo functionInfo, Vessel vessel, ProtoPartSnapshot protoPartSnapshot, ProtoPartModuleSnapshot protoPartModuleSnapshot, Part part, PartModule partModule)
 {
     FunctionInfo            = functionInfo;
     Vessel                  = vessel;
     ProtoPartSnapshot       = protoPartSnapshot;
     ProtoPartModuleSnapshot = protoPartModuleSnapshot;
     Part       = part;
     PartModule = partModule;
 }
Example #3
0
        public static BackgroundFunctionInfo Instance(IKITModule kitModule)
        {
            var type = kitModule.GetType();

            if (PreCached.TryGetValue(type, out var retValue))
            {
                return(retValue);
            }

            #region Standard KIT Module support

            var methodInfo = type.GetMethod("KITPartName", KITPartNameSignature);
            if (methodInfo == null)
            {
                Debug.Log($"[BackgroundFunctionInfo] Can not find KITPartName(ProtoPartModuleSnapshot)");
                return(null);
            }

            var kitPartName =
                (Func <ProtoPartSnapshot, ProtoPartModuleSnapshot, string>)Delegate.CreateDelegate(
                    typeof(Func <ProtoPartSnapshot, ProtoPartModuleSnapshot, string>), methodInfo);

            methodInfo = type.GetMethod("KITBackgroundUpdate", KITBackgroundUpdateSignature);
            if (methodInfo == null)
            {
                Debug.Log($"[BackgroundFunctionInfo] Can not find KITBackgroundUpdate in {kitModule.KITPartName()}");
                return(null);
            }

            var kitBackgroundUpdate =
                (Action <IResourceManager, Vessel, ProtoPartSnapshot, ProtoPartModuleSnapshot, PartModule, Part>)
                Delegate.CreateDelegate(
                    typeof(Action <IResourceManager, Vessel, ProtoPartSnapshot, ProtoPartModuleSnapshot, PartModule, Part
                                   >), methodInfo);


            methodInfo = type.GetMethod("BackgroundModuleConfiguration", BackgroundModuleConfigurationSignature);
            if (methodInfo == null)
            {
                Debug.Log($"[BackgroundFunctionInfo] Can not find BackgroundModuleConfiguration in {kitModule.KITPartName()}");
                return(null);
            }

            var backgroundModuleConfiguration =
                (Func <ProtoPartModuleSnapshot, ModuleConfigurationFlags>)Delegate.CreateDelegate(
                    typeof(Func <ProtoPartModuleSnapshot, ResourceName[]>), methodInfo);

            if (!(kitModule is IKITVariableSupplier))
            {
                var result = new BackgroundFunctionInfo(kitPartName, backgroundModuleConfiguration, kitBackgroundUpdate);
                PreCached[type] = result;
                return(result);
            }

            methodInfo = type.GetMethod("ResourcesProvided", ResourcesProvidedSignature);
            if (methodInfo == null)
            {
                Debug.Log(
                    $"[BackgroundFunctionInfo] Can not find ResourcesProvided in {kitModule.KITPartName()} - disabling module");
                return(null);
            }

            #endregion

            #region Suppliable resources

            var resourcesProvided =
                (Func <ProtoPartModuleSnapshot, ResourceName[]>)Delegate.CreateDelegate(
                    typeof(Func <ProtoPartModuleSnapshot, ResourceName[]>), methodInfo);

            methodInfo = type.GetMethod("BackgroundProvideResource", BackgroundProvideResourceSignature);
            if (methodInfo == null)
            {
                Debug.Log(
                    $"[BackgroundFunctionInfo] Can not find BackgroundProvideResource in {kitModule.KITPartName()} - disabling module");
                return(null);
            }

            var backgroundProvideResource =
                (Func <IResourceManager, Vessel, ProtoPartSnapshot, ProtoPartModuleSnapshot, PartModule, Part,
                       ResourceName, double, bool>)
                Delegate.CreateDelegate(
                    typeof(Func <IResourceManager, Vessel, ProtoPartSnapshot, ProtoPartModuleSnapshot, PartModule, Part,
                                 ResourceName, double, bool>),
                    methodInfo);


            #endregion

            return(new BackgroundFunctionInfo(kitPartName, backgroundModuleConfiguration, kitBackgroundUpdate, resourcesProvided,
                                              backgroundProvideResource));
        }
Example #4
0
 public BackgroundSuppliableModule(BackgroundFunctionInfo functionInfo, Vessel vessel, ProtoPartSnapshot protoPartSnapshot, ProtoPartModuleSnapshot protoPartModuleSnapshot, Part part, PartModule partModule) : base(functionInfo, vessel, protoPartSnapshot, protoPartModuleSnapshot, part, partModule)
 {
 }