Skip to content

Commit

Permalink
Possible zer0Kerbal#81 Fix of applying inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
roxik0 committed Jan 2, 2024
1 parent 870a3a0 commit 73390bd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 41 deletions.
72 changes: 38 additions & 34 deletions source/ScrapYard/InventoryPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public InventoryPart(Part originPart)
{
_dryCost -= (float)(resource.maxAmount * PartResourceLibrary.Instance.GetDefinition(resource.resourceName).unitCost);
}

ID = originPart.persistentId;
//Save modules
if (originPart.Modules != null)
{
Expand All @@ -137,6 +137,7 @@ public InventoryPart(Part originPart)
{
string name = module.moduleName;
bool isTracker = name.Equals("ModuleSYPartTracker");

if (isTracker)
{
ConfigNode saved = new ConfigNode("MODULE");
Expand All @@ -145,13 +146,14 @@ public InventoryPart(Part originPart)
if (isTracker)
{
TrackerModule = new TrackerModuleWrapper(saved);
ID = TrackerModule.ID ?? ID;
}
}
_cachedModules.Add(module);
}
}

ID = originPart.persistentId;

}

/// <summary>
Expand All @@ -160,33 +162,34 @@ public InventoryPart(Part originPart)
/// <param name="originPartSnapshot">The <see cref="ProtoPartSnapshot"/> to use as the basis of the <see cref="InventoryPart"/>.</param>
public InventoryPart(ProtoPartSnapshot originPartSnapshot)
{
_name = originPartSnapshot.partInfo.name;
if (ScrapYard.Instance.Settings.PartBlacklist.Contains(Name))
{
DoNotStore = true;
}
float fuelCost;
ShipConstruction.GetPartCosts(originPartSnapshot, originPartSnapshot.partInfo, out _dryCost, out fuelCost);

//Save modules
if (originPartSnapshot.modules != null)
_name = originPartSnapshot.partInfo.name;
if (ScrapYard.Instance.Settings.PartBlacklist.Contains(Name))
{
DoNotStore = true;
}
float fuelCost;
ShipConstruction.GetPartCosts(originPartSnapshot, originPartSnapshot.partInfo, out _dryCost, out fuelCost);
ID = originPartSnapshot.persistentId;
//Save modules
if (originPartSnapshot.modules != null)
{
foreach (ProtoPartModuleSnapshot module in originPartSnapshot.modules)
{
foreach (ProtoPartModuleSnapshot module in originPartSnapshot.modules)
string name = module.moduleName;
bool isTracker = name.Equals("ModuleSYPartTracker");
if (isTracker || moduleNameMatchesAnything(name)) //only save if there is a potential match
{
string name = module.moduleName;
bool isTracker = name.Equals("ModuleSYPartTracker");
if (isTracker || moduleNameMatchesAnything(name)) //only save if there is a potential match
ConfigNode saved = module.moduleValues;
_allModules.Add(saved);
if (isTracker)
{
ConfigNode saved = module.moduleValues;
_allModules.Add(saved);
if (isTracker)
{
TrackerModule = new TrackerModuleWrapper(saved);
}
TrackerModule = new TrackerModuleWrapper(saved);
ID = TrackerModule.ID ?? ID;
}
}
}
ID = originPartSnapshot.persistentId;
}

}

/// <summary>
Expand All @@ -213,7 +216,15 @@ public InventoryPart(ConfigNode originPartConfigNode)
float dryMass, fuelMass, fuelCost;
ShipConstruction.GetPartCostsAndMass(originPartConfigNode, availablePartForNode, out _dryCost, out fuelCost, out dryMass, out fuelMass);
}

uint id = 0;
if (originPartConfigNode.TryGetValue("persistentId", ref id))
{
ID = id;
}
else
{
Logging.Log($"Could not find a persistent ID for part {_name}", Logging.LogType.ERROR);
}
if (originPartConfigNode.HasNode("MODULE"))
{
foreach (ConfigNode module in originPartConfigNode.GetNodes("MODULE"))
Expand All @@ -224,19 +235,12 @@ public InventoryPart(ConfigNode originPartConfigNode)
if (isTracker)
{
TrackerModule = new TrackerModuleWrapper(module);
ID = TrackerModule.ID ?? ID;
}
}
}

uint id = 0;
if (originPartConfigNode.TryGetValue("persistentId", ref id))
{
ID = id;
}
else
{
Logging.Log($"Could not find a persistent ID for part {_name}", Logging.LogType.ERROR);
}

}
}

Expand Down
16 changes: 9 additions & 7 deletions source/ScrapYard/Utilities/InventoryManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void SplitParts(IEnumerable<Part> input, out IList<InventoryPart>
/// <param name="input">The vessel as a list of parts</param>
public static void ApplyInventoryToVessel(IEnumerable<Part> input)
{
PartInventory copy = ScrapYard.Instance.TheInventory.Copy();
PartInventory copy = ScrapYard.Instance.TheInventory.Copy();
foreach (Part part in input)
{
//convert it to an inventorypart
Expand All @@ -57,19 +57,20 @@ public static void ApplyInventoryToVessel(IEnumerable<Part> input)
//if one was found...
if (inInventory != null)
{
Logging.DebugLog("Found a part in inventory for " + inInventory.Name);
Logging.DebugLog("A: Found a part in inventory for " + inInventory.Name);
//copy it's part tracker over
if (inInventory.TrackerModule != null && part.Modules?.Contains("ModuleSYPartTracker") == true)
{
ModuleSYPartTracker tracker = part.Modules["ModuleSYPartTracker"] as ModuleSYPartTracker;
tracker.TimesRecovered = inInventory.TrackerModule.TimesRecovered;
tracker.Inventoried = inInventory.TrackerModule.Inventoried;
Logging.Log($"Copied tracker. Recovered {tracker.TimesRecovered} times with id {inInventory.ID}");
tracker.ID = inInventory.ID;
Logging.Log($"A: Copied tracker. Recovered {tracker.TimesRecovered} times with id {inInventory.ID}");
}
}
}

ScrapYardEvents.OnSYInventoryAppliedToVessel.Fire();
ScrapYardEvents.OnSYInventoryAppliedToVessel.Fire();
GameEvents.onEditorShipModified.Fire(EditorLogic.fetch.ship);
}

Expand All @@ -94,7 +95,7 @@ public static void ApplyInventoryToVessel(IEnumerable<ConfigNode> input)
//if one was found...
if (inInventory != null)
{
Logging.DebugLog("Found a part in inventory for " + inInventory.Name);
Logging.DebugLog("B:Found a part in inventory for " + inInventory.Name);
//copy it's part tracker over
ConfigNode trackerNode;
if (inInventory.TrackerModule != null && (trackerNode = partNode.GetModuleNode("ModuleSYPartTracker")) != null)
Expand All @@ -105,11 +106,12 @@ public static void ApplyInventoryToVessel(IEnumerable<ConfigNode> input)
trackerNode.SetValue("ID", id);
trackerNode.SetValue("TimesRecovered", recovered);
trackerNode.SetValue("Inventoried", inventoried);
Logging.DebugLog($"Copied tracker. Recovered {recovered} times with id {id}");
Logging.DebugLog($"B:Copied tracker. Recovered {recovered} times with id {id}");

}
}
}
ScrapYardEvents.OnSYInventoryAppliedToVessel.Fire();
ScrapYardEvents.OnSYInventoryAppliedToVessel.Fire();
GameEvents.onEditorShipModified.Fire(EditorLogic.fetch.ship);
}

Expand Down

0 comments on commit 73390bd

Please sign in to comment.