示例#1
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));
        }
        private void InitializeModuleIfNeeded(ResourceData data, IKITModule ikitModule)
        {
            // don't call KITFixedUpdate more than once
            if (data.FixedUpdateCalledMods.Contains(ikitModule))
            {
                return;
            }

            data.FixedUpdateCalledMods.Add(ikitModule);

            UnityEngine.Profiling.Profiler.BeginSample($"ResourceManager.InitializeModuleIfNeeded.Init.{ikitModule.KITPartName()}");


            IResourceManager resourceInterface;

            if (_localResourceDataInformation.TryGetValue(ikitModule, out var localData))
            {
                localData.OverheatMultiplier = data.OverheatMultiplier;
                resourceInterface            = localData;
            }
            else
            {
                resourceInterface = data;
            }

            try
            {
                ikitModule.KITFixedUpdate(resourceInterface);
            }
            catch (Exception ex)
            {
                Debug.Log($"[KITResourceManager.InitializeModuleIfNeeded] Exception when processing [{ikitModule.KITPartName()}, {(ikitModule as PartModule)?.ClassName}]: {ex}");
            }
            finally
            {
                UnityEngine.Profiling.Profiler.EndSample();
            }
        }