public static HashSet <ProtoPartResourceSnapshot> AddResource(VesselData data, float amount, string name, HashSet <ProtoPartResourceSnapshot> modified)
        {
            BackgroundProcessing.Debug("AddResource called, adding " + amount + " " + name, DebugLevel.ALL);

            if (!data.storage.ContainsKey(name))
            {
                return(modified);
            }

            bool reduce = amount < 0;

            List <ProtoPartResourceSnapshot> relevantStorage = data.storage[name];

            for (int i = 0; i < relevantStorage.Count; ++i)
            {
                ProtoPartResourceSnapshot r = relevantStorage[i];

                if (amount == 0)
                {
                    break;
                }
                float n; float m;

                n = (float)r.amount;
                m = (float)r.maxAmount;
                if (
                    !(float.IsInfinity(n) || float.IsInfinity(m))
                    )
                {
                    n += amount; amount = 0;

                    if (reduce)
                    {
                        if (n < 0 && i < relevantStorage.Count - 1)
                        {
                            amount = n; n = 0;
                        }
                    }
                    else
                    {
                        if (n > m && i < relevantStorage.Count - 1)
                        {
                            amount = n - m; n = m;
                        }
                    }

                    r.amount = n;
                }

                modified.Add(r);
            }

            return(modified);
        }
Example #2
0
        public void Awake()
        {
            LoadConfigFile();

            if (loaded || !IsMostRecentAssembly())
            {
                Debug("BackgroundProcessing: Assembly " + Assembly.GetExecutingAssembly().Location + " (" + Assembly.GetExecutingAssembly().GetName().Version + ") not running because:", DebugLevel.ALL);
                if (loaded) { Debug("BackgroundProcessing already loaded", DebugLevel.ALL); }
                else { Debug("IsMostRecentAssembly returned false", DebugLevel.ALL); }
                Destroy(gameObject);
                return;
            }

            Debug("BackgroundProcessing: Running assembly at " + Assembly.GetExecutingAssembly().Location + " (" + Assembly.GetExecutingAssembly().GetName().Version + ")", DebugLevel.WARNING);

            Instance = this;
            loaded = true;
            DontDestroyOnLoad(this);

            interestingResources.Add("ElectricCharge");

            HashSet<String> processed = new HashSet<String>();

            foreach (AvailablePart a in PartLoader.LoadedPartsList)
            {
                if (a.partPrefab.Modules != null)
                {
                    foreach (PartModule m in a.partPrefab.Modules)
                    {
                        if (processed.Contains(m.moduleName)) continue;
                        processed.Add(m.moduleName);

                        MethodInfo lf = m.GetType().GetMethod("BackgroundLoad", BindingFlags.Public | BindingFlags.Static, null, new Type[3] { typeof(Vessel), typeof(uint), typeof(System.Object).MakeByRefType() }, null);
                        MethodInfo sf = m.GetType().GetMethod("BackgroundSave", BindingFlags.Public | BindingFlags.Static, null, new Type[3] { typeof(Vessel), typeof(uint), typeof(System.Object) }, null);

                        MethodInfo fbu = m.GetType().GetMethod("FixedBackgroundUpdate", BindingFlags.Public | BindingFlags.Static, null, new Type[3] { typeof(Vessel), typeof(uint), typeof(System.Object).MakeByRefType() }, null);
                        MethodInfo fbur = m.GetType().GetMethod("FixedBackgroundUpdate", BindingFlags.Public | BindingFlags.Static, null, new Type[4] { typeof(Vessel), typeof(uint), typeof(ResourceRequestFunc), typeof(System.Object).MakeByRefType() }, null);
                        if (fbur != null)
                        {
                            moduleHandlers[m.moduleName] = new UpdateHelper
                            (
                              (BackgroundUpdateResourceFunc)Delegate.CreateDelegate(typeof(BackgroundUpdateResourceFunc), fbur),
                                lf != null ? (BackgroundLoadFunc)Delegate.CreateDelegate(typeof(BackgroundLoadFunc), lf) : null,
                                sf != null ? (BackgroundSaveFunc)Delegate.CreateDelegate(typeof(BackgroundSaveFunc), sf) : null
                            );
                        }
                        else
                        {
                            if (fbu != null)
                            {
                                moduleHandlers[m.moduleName] = new UpdateHelper
                                (
                                    (BackgroundUpdateFunc)Delegate.CreateDelegate(typeof(BackgroundUpdateFunc), fbu),
                                    lf != null ? (BackgroundLoadFunc)Delegate.CreateDelegate(typeof(BackgroundLoadFunc), lf) : null,
                                    sf != null ? (BackgroundSaveFunc)Delegate.CreateDelegate(typeof(BackgroundSaveFunc), sf) : null
                                );
                            }
                        }

                        MethodInfo ir = m.GetType().GetMethod("GetInterestingResources", BindingFlags.Public | BindingFlags.Static, null, Type.EmptyTypes, null);
                        if (ir != null && ir.ReturnType == typeof(List<string>))
                        {
                            interestingResources.UnionWith((List<String>)ir.Invoke(null, null));
                        }

                        MethodInfo prc = m.GetType().GetMethod("GetBackgroundResourceCount", BindingFlags.Public | BindingFlags.Static, null, Type.EmptyTypes, null);
                        MethodInfo pr = m.GetType().GetMethod("GetBackgroundResource", BindingFlags.Public | BindingFlags.Static, null, new Type[3] { typeof(int), typeof(string).MakeByRefType(), typeof(float).MakeByRefType() }, null);
                        if (prc != null && pr != null && prc.ReturnType == typeof(int))
                        {
                            System.Object[] resourceParams = new System.Object[3];
                            resourceParams[1] = "";
                            resourceParams[2] = 0.0f;

                            resourceData[m.moduleName] = new List<ResourceModuleHandler>();
                            for (int count = (int)prc.Invoke(null, null); count > 0; --count)
                            {
                                resourceParams[0] = count;
                                pr.Invoke(null, resourceParams);

                                resourceData[m.moduleName].Add(new ResourceHandlers.CustomHandler((string)resourceParams[1], (float)resourceParams[2]));
                            }
                        }
                    }
                }
            }

            GameEvents.onLevelWasLoaded.Add(ClearVesselData);
            GameEvents.onGameStateSave.Add(OnSave);
        }
Example #3
0
        public static VesselData GetVesselData(
            Vessel v,
            Dictionary <String, UpdateHelper> moduleHandlers,
            Dictionary <String, List <ResourceModuleHandler> > resourceData,
            HashSet <String> interestingResources
            )
        {
            VesselData ret = new VesselData();

            if (v.protoVessel != null)
            {
                foreach (ProtoPartSnapshot p in v.protoVessel.protoPartSnapshots)
                {
                    Part part = PartLoader.getPartInfoByName(p.partName).partPrefab;

                    if (part == null)
                    {
                        BackgroundProcessing.Debug("BackgroundProcessing: Couldn't find PartPrefab for part " + p.partName, DebugLevel.WARNING);
                        continue;
                    }

                    if (part.Modules == null)
                    {
                        continue;
                    }

                    for (int i = 0; i < p.modules.Count; ++i)
                    {
                        if (p.modules[i].moduleName == null)
                        {
                            BackgroundProcessing.Debug("BackgroundProcessing: Null moduleName for module " + i + "/" + p.modules.Count, DebugLevel.WARNING);
                            BackgroundProcessing.Debug("BackgroundProcessing: Module values: " + p.modules[i].moduleValues, DebugLevel.WARNING);
                            continue;
                        }

                        if (moduleHandlers.ContainsKey(p.modules[i].moduleName))
                        {
                            ret.callbacks.Add(new CallbackPair(p.modules[i].moduleName, p.flightID), new ObjectHolder());
                        }

                        int j = i;
                        if (j >= part.Modules.Count || part.Modules[j].moduleName != p.modules[i].moduleName)
                        {
                            if (j < part.Modules.Count)
                            {
                                BackgroundProcessing.Debug("BackgroundProcessing: Expected " + p.modules[i].moduleName + " at index " + i + ", got " + part.Modules[j].moduleName, DebugLevel.WARNING);

                                for (j = i; j < part.Modules.Count; ++j)
                                {
                                    if (part.Modules[j].moduleName == p.modules[i].moduleName)
                                    {
                                        BackgroundProcessing.Debug("BackgroundProcessing: Found " + p.modules[i].moduleName + " at index " + j, DebugLevel.WARNING);
                                        break;
                                    }
                                }
                            }
                        }

                        if (j < part.Modules.Count)
                        {
                            if (HasResourceGenerationData(part.Modules[j], p.modules[i], resourceData, interestingResources))
                            {
                                ret.resourceModules.AddRange(GetResourceGenerationData(part.Modules[j], p, resourceData, interestingResources));
                            }
                        }
                        else
                        {
                            BackgroundProcessing.Debug("BackgroundProcessing: Ran out of modules before finding module " + p.modules[i].moduleName, DebugLevel.WARNING);

                            if (HasResourceGenerationData(null, p.modules[i], resourceData, interestingResources))
                            {
                                ret.resourceModules.AddRange(GetResourceGenerationData(null, p, resourceData, interestingResources));
                            }
                        }
                    }

                    foreach (ProtoPartResourceSnapshot r in p.resources)
                    {
                        if (r.resourceName == null)
                        {
                            BackgroundProcessing.Debug("BackgroundProcessing: Null resourceName.", DebugLevel.WARNING);
                            continue;
                        }

                        if (!r.flowState)
                        {
                            continue;
                        }

                        if (!ret.storage.ContainsKey(r.resourceName))
                        {
                            ret.storage.Add(r.resourceName, new List <ProtoPartResourceSnapshot>());
                        }
                        ret.storage[r.resourceName].Add(r);
                    }
                }
            }

            return(ret);
        }
Example #4
0
		public void Awake() {
			LoadConfigFile();

			if (loaded || !IsMostRecentAssembly()) {
				Debug("BackgroundProcessing: Assembly " + Assembly.GetExecutingAssembly().Location + " (" + Assembly.GetExecutingAssembly().GetName().Version + ") not running because:", DebugLevel.ALL);
				if (loaded) { Debug("BackgroundProcessing already loaded", DebugLevel.ALL); }
				else { Debug("IsMostRecentAssembly returned false", DebugLevel.ALL); }
				Destroy(gameObject);
				return;
			}

			Debug("BackgroundProcessing: Running assembly at " + Assembly.GetExecutingAssembly().Location + " (" + Assembly.GetExecutingAssembly().GetName().Version + ")", DebugLevel.WARNING);

			Instance = this;
			loaded = true;
			DontDestroyOnLoad(this);

			interestingResources.Add("ElectricCharge");

			HashSet<String> processed = new HashSet<String>();

			foreach (AvailablePart a in PartLoader.LoadedPartsList) {
				if (a.partPrefab.Modules != null) {
					foreach (PartModule m in a.partPrefab.Modules) {
						if (processed.Contains(m.moduleName)) continue;
						processed.Add(m.moduleName);

						MethodInfo lf = m.GetType().GetMethod("BackgroundLoad", BindingFlags.Public | BindingFlags.Static, null, new Type[3] { typeof(Vessel), typeof(uint), typeof(System.Object).MakeByRefType() }, null);
						MethodInfo sf = m.GetType().GetMethod("BackgroundSave", BindingFlags.Public | BindingFlags.Static, null, new Type[3] { typeof(Vessel), typeof(uint), typeof(System.Object) }, null);

						MethodInfo fbu = m.GetType().GetMethod("FixedBackgroundUpdate", BindingFlags.Public | BindingFlags.Static, null, new Type[3] { typeof(Vessel), typeof(uint), typeof(System.Object).MakeByRefType() }, null);
						MethodInfo fbur = m.GetType().GetMethod("FixedBackgroundUpdate", BindingFlags.Public | BindingFlags.Static, null, new Type[4] { typeof(Vessel), typeof(uint), typeof(ResourceRequestFunc), typeof(System.Object).MakeByRefType() }, null);
						if (fbur != null) {
							moduleHandlers[m.moduleName] = new UpdateHelper
							(
							  (BackgroundUpdateResourceFunc)Delegate.CreateDelegate(typeof(BackgroundUpdateResourceFunc), fbur),
								lf != null ? (BackgroundLoadFunc)Delegate.CreateDelegate(typeof(BackgroundLoadFunc), lf) : null,
								sf != null ? (BackgroundSaveFunc)Delegate.CreateDelegate(typeof(BackgroundSaveFunc), sf) : null
							);
						}
						else {
							if (fbu != null) {
								moduleHandlers[m.moduleName] = new UpdateHelper
								(
									(BackgroundUpdateFunc)Delegate.CreateDelegate(typeof(BackgroundUpdateFunc), fbu),
									lf != null ? (BackgroundLoadFunc)Delegate.CreateDelegate(typeof(BackgroundLoadFunc), lf) : null,
									sf != null ? (BackgroundSaveFunc)Delegate.CreateDelegate(typeof(BackgroundSaveFunc), sf) : null
								);
							}
						}

						MethodInfo ir = m.GetType().GetMethod("GetInterestingResources", BindingFlags.Public | BindingFlags.Static, null, Type.EmptyTypes, null);
						if (ir != null && ir.ReturnType == typeof(List<string>)) {
							interestingResources.UnionWith((List<String>)ir.Invoke(null, null));
						}

						MethodInfo prc = m.GetType().GetMethod("GetBackgroundResourceCount", BindingFlags.Public | BindingFlags.Static, null, Type.EmptyTypes, null);
						MethodInfo pr = m.GetType().GetMethod("GetBackgroundResource", BindingFlags.Public | BindingFlags.Static, null, new Type[3] { typeof(int), typeof(string).MakeByRefType(), typeof(float).MakeByRefType() }, null);
						if (prc != null && pr != null && prc.ReturnType == typeof(int)) {
							System.Object[] resourceParams = new System.Object[3];
							resourceParams[1] = "";
							resourceParams[2] = 0.0f;

							resourceData[m.moduleName] = new List<ResourceModuleHandler>();
							for (int count = (int)prc.Invoke(null, null); count > 0; --count) {
								resourceParams[0] = count;
								pr.Invoke(null, resourceParams);

								resourceData[m.moduleName].Add(new ResourceHandlers.CustomHandler((string)resourceParams[1], (float)resourceParams[2]));
							}
						}
					}
				}
			}

			GameEvents.onLevelWasLoaded.Add(ClearVesselData);
			GameEvents.onGameStateSave.Add(OnSave);


		}