public void Remove() { Task.Run(() => { CacheResource.Remove(); Data = null; }); }
/// <summary> /// Create or update a InterestedVessels entry's CachedResources for a ProtoVessel. /// </summary> /// <param name="vessel"></param> public static void CreatecachedVesselResources(ProtoVessel vessel) { if (UnloadedResources.InterestedVessels == null) { UnloadedResources.InterestedVessels = new DictionaryValueList <ProtoVessel, InterestedVessel>(); } List <CacheResource> cacheresources = new List <CacheResource>(); for (int i = 0; i < vessel.protoPartSnapshots.Count; i++) { ProtoPartSnapshot protoPartSnapshot = vessel.protoPartSnapshots[i]; for (int j = 0; j < protoPartSnapshot.resources.Count; j++) { ProtoPartResourceSnapshot protoPartResourceSnapshot = protoPartSnapshot.resources[j]; bool found = false; for (int k = 0; k < cacheresources.Count; k++) { if (cacheresources[k].resourceName == protoPartResourceSnapshot.resourceName) { cacheresources[k].amount += protoPartResourceSnapshot.amount; cacheresources[k].maxAmount += protoPartResourceSnapshot.maxAmount; cacheresources[k].protoPartResourceSnapshot.Add(CacheResource.GetKey(protoPartSnapshot, protoPartResourceSnapshot), protoPartResourceSnapshot); found = true; break; } } if (!found) { CacheResource newresource = new CacheResource(protoPartSnapshot.craftID, protoPartResourceSnapshot, protoPartResourceSnapshot.resourceName, protoPartResourceSnapshot.amount, protoPartResourceSnapshot.maxAmount); cacheresources.Add(newresource); } } } if (UnloadedResources.InterestedVessels.Contains(vessel)) { UnloadedResources.InterestedVessels[vessel].CachedResources = cacheresources; } else { InterestedVessel iVessel = new InterestedVessel(vessel.vesselRef, vessel); iVessel.CachedResources = cacheresources; UnloadedResources.InterestedVessels.Add(vessel, iVessel); UnloadedResources.InterestedVessels[vessel].TimeLastRefresh = Time.time; } }
public static CacheResource Load(ConfigNode node, ProtoVessel protoVessel) { string resName = ""; node.TryGetValue("resourceName", ref resName); double amt = 0; double maxamt = 0; node.TryGetValue("amount", ref amt); node.TryGetValue("maxAmount", ref maxamt); DictionaryValueList <string, ProtoPartResourceSnapshot> protoresSnapshots = new DictionaryValueList <string, ProtoPartResourceSnapshot>(); ConfigNode[] protoresourcesnapNodes = node.GetNodes("RESOURCE"); for (int rsI = 0; rsI < protoresourcesnapNodes.Length; rsI++) { string keyField = protoresourcesnapNodes[rsI].GetValue("craftID"); ProtoPartResourceSnapshot protoresSnap = new ProtoPartResourceSnapshot(protoresourcesnapNodes[rsI]); ProtoPartResourceSnapshot protoVesselResSnap = GetMatchingResourceSnapShot(keyField, protoresSnap, protoVessel); if (protoVesselResSnap != null) { protoresSnapshots.Add(keyField, protoVesselResSnap); } } if (protoresSnapshots.Count > 0) { Dictionary <string, ProtoPartResourceSnapshot> .Enumerator ppRSenumerator = protoresSnapshots.GetDictEnumerator(); ppRSenumerator.MoveNext(); string resourceKey = ""; uint craftID = CacheResource.RetrieveKey(ppRSenumerator.Current.Key, out resourceKey); CacheResource newCacheResource = new CacheResource(craftID, ppRSenumerator.Current.Value, resName, amt, maxamt); while (ppRSenumerator.MoveNext()) { newCacheResource.protoPartResourceSnapshot.Add(ppRSenumerator.Current.Key, ppRSenumerator.Current.Value); } ppRSenumerator.Dispose(); return(newCacheResource); } return(null); }
public static ProtoPartResourceSnapshot GetMatchingResourceSnapShot(string keyField, ProtoPartResourceSnapshot protoresSnap, ProtoVessel protoVessel) { ProtoPartResourceSnapshot returnSnapshot = null; string resourceKey = ""; uint craftID = CacheResource.RetrieveKey(keyField, out resourceKey); for (int pvPartI = 0; pvPartI < protoVessel.protoPartSnapshots.Count; pvPartI++) { if (protoVessel.protoPartSnapshots[pvPartI].craftID == craftID) { bool found = false; for (int ppSnapI = 0; ppSnapI < protoVessel.protoPartSnapshots[pvPartI].resources.Count; ppSnapI++) { if (protoVessel.protoPartSnapshots[pvPartI].resources[ppSnapI].resourceName == resourceKey) { //Compare the loaded values to the protoVessel snapshot. //If loaded is more then DO WE? update the protoVessel. Let's just report it for now. if (protoVessel.protoPartSnapshots[pvPartI].resources[ppSnapI].amount != protoresSnap.amount) { RSTUtils.Utilities.Log("ProtoVessel resource amounts differ"); } if (protoVessel.protoPartSnapshots[pvPartI].resources[ppSnapI].maxAmount != protoresSnap.maxAmount) { RSTUtils.Utilities.Log("ProtoVessel resource max amounts differ"); } returnSnapshot = protoVessel.protoPartSnapshots[pvPartI].resources[ppSnapI]; break; } } if (found) { break; } } } return(returnSnapshot); }
public ServiceBase(string LocalPath, string OnlinePath) { ApiResource = new ApiResource(OnlinePath); CacheResource = new CacheResource(LocalPath); }
public void Get(string _Input = null) { // Fetch if (_Input == null) { // Loog for stored pass ResourceResponse Response = CacheResource.Get(); if (Response.IsValid) { SeasonPassModel Pass = JsonConvert.DeserializeObject <SeasonPassModel>(Response.Data); if (IsPassValid(Pass.ValidTo)) { Data = Pass; } else { Data = null; IsPassInvalidEvent?.Invoke(this, EventArgs.Empty); } } } else { string InputEncrypted = EncDecService.Hash(_Input); if (ApiResource.Headers.ContainsKey("PassID")) { ApiResource.Headers["PassID"] = InputEncrypted; } else { ApiResource.Headers.Add("PassID", InputEncrypted); } ResourceResponse Response = ApiResource.Get(); if (Response.IsValid) { List <SeasonPassModel> Temp = JsonConvert.DeserializeObject <List <SeasonPassModel> >(Response.Data); if (Temp.Count > 0) { SeasonPassModel Pass = Temp[0]; if (IsPassValid(Pass.ValidTo)) { CacheResource.AddOrUpdate(JsonConvert.SerializeObject(Pass)); Data = Pass; } else { IsPassInvalidEvent?.Invoke(this, EventArgs.Empty); Data = null; } } else { IsPassInvalidEvent?.Invoke(this, EventArgs.Empty); Data = null; } } } // Decrypt if (Data != null) { Data.Holder = EncDecService.Decrypt(Data.Holder); } }
public PassService() { CacheResource = new CacheResource(ConfigurationManager.RemoteResources.Local.Pass); ApiResource = new ApiResource(ConfigurationManager.RemoteResources.Remote.Pass); }