Skip to content

Commit

Permalink
BREAKING. Use KSP 1.4's unique part id system instead of custom id sy…
Browse files Browse the repository at this point in the history
…stem.
  • Loading branch information
magico13 committed Mar 7, 2018
1 parent 723f8ca commit 0576f77
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 88 deletions.
25 changes: 13 additions & 12 deletions ScrapYard/API/APIManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,12 @@ public ConfigNode FindInventoryPart_ID(string id)
return null;
}

Guid? guid = Utilities.Utils.StringToGuid(id);
if (!guid.HasValue)

if (uint.TryParse(id, out uint partID))
{
return null;
return ScrapYard.Instance.TheInventory.FindPart(partID)?.State;
}
return ScrapYard.Instance.TheInventory.FindPart(guid.Value)?.State;
return null;
}

/// <summary>
Expand Down Expand Up @@ -301,12 +301,11 @@ public bool ProcessVessel_Parts(IEnumerable<Part> parts)
return true;
}
//try to get the ID out of the list
Guid? guid = Utils.StringToGuid(parts.FirstOrDefault()?.Modules.GetModule<ModuleSYPartTracker>()?.ID);
if (!guid.HasValue)
uint? ID = parts.FirstOrDefault()?.persistentId;
if (!ID.HasValue)
{
return false; //for now we can't process this vessel. Sorry. Maybe later we'll be able to add the module
}
Guid ID = guid.GetValueOrDefault();

//check that it isn't already processed
if (ScrapYard.Instance.ProcessedTracker.IsProcessed(ID))
Expand Down Expand Up @@ -336,13 +335,11 @@ public bool ProcessVessel_Nodes(IEnumerable<ConfigNode> partNodes)
return true;
}
//try to get the ID out of the list
Guid? guid = Utils.StringToGuid(
partNodes.FirstOrDefault()?.GetNodes("MODULE").FirstOrDefault(n => n.GetValue("name").Equals("ModuleSYPartTracker", StringComparison.OrdinalIgnoreCase))?.GetValue("ID"));
if (!guid.HasValue)
uint ID = 0;
if (partNodes.FirstOrDefault()?.TryGetValue("persistentID", ref ID) != true)
{
return false; //for now we can't process this vessel. Sorry. Maybe later we'll be able to add the module
}
Guid ID = guid.GetValueOrDefault();

//check that it isn't already processed
if (ScrapYard.Instance.ProcessedTracker.IsProcessed(ID))
Expand Down Expand Up @@ -386,7 +383,11 @@ public void RecordBuild_Nodes(IEnumerable<ConfigNode> parts)
/// <returns>The previous status</returns>
public bool SetProcessedStatus_ID(string id, bool newStatus)
{
return ScrapYard.Instance.ProcessedTracker.TrackVessel(Utils.StringToGuid(id), newStatus);
if (uint.TryParse(id, out uint vesselID))
{
return ScrapYard.Instance.ProcessedTracker.TrackVessel(vesselID, newStatus);
}
return false;
}
#endregion Vessel Processing

Expand Down
13 changes: 5 additions & 8 deletions ScrapYard/Events/EventListeners.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using KSP.UI.Screens;
using KSP.UI.Screens.SpaceCenter.MissionSummaryDialog;
using ScrapYard.Modules;
using ScrapYard.UI;
using ScrapYard.Utilities;
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

namespace ScrapYard
Expand Down Expand Up @@ -105,8 +101,7 @@ public void VesselRolloutEvent(ShipConstruct vessel)
//If vessel not processed, then take parts
//If already processed, just return

if (ScrapYard.Instance.ProcessedTracker.Remove(Utils.StringToGuid(
vessel.Parts[0].Modules.GetModule<ModuleSYPartTracker>()?.ID)))
if (ScrapYard.Instance.ProcessedTracker.Remove(vessel.Parts[0].persistentId))
{
return;
}
Expand Down Expand Up @@ -185,8 +180,10 @@ private void handleButtonClick(bool enable)
}
else
{
List<Part> selectedParts = new List<Part>(EditorLogic.SelectedPart.children);
selectedParts.Add(EditorLogic.SelectedPart);
List<Part> selectedParts = new List<Part>(EditorLogic.SelectedPart.children)
{
EditorLogic.SelectedPart
};
List<Part> inventoriedParts = new List<Part>();
double cost = 0;
uint count = 0;
Expand Down
45 changes: 30 additions & 15 deletions ScrapYard/InventoryPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ public bool DoNotStore
set { _doNotStore = value; }
}
public TrackerModuleWrapper TrackerModule { get; private set; } = new TrackerModuleWrapper(null);
public Guid? ID
public uint? ID
{
get
{
return TrackerModule?.ID;
}
set
{
TrackerModule.ID = value;
}
}


Expand Down Expand Up @@ -124,6 +128,8 @@ public InventoryPart(Part originPart)
}
}
}

ID = originPart.persistentId;
}

/// <summary>
Expand Down Expand Up @@ -153,6 +159,8 @@ public InventoryPart(ProtoPartSnapshot originPartSnapshot)
//storeModuleNode(_name, module.moduleValues);
}
}

ID = originPartSnapshot.persistentId;
}

/// <summary>
Expand Down Expand Up @@ -192,6 +200,16 @@ public InventoryPart(ConfigNode originPartConfigNode)
}
}
}

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 Expand Up @@ -374,7 +392,7 @@ public bool FullyApplyToPart(Part part)
if (part.Modules.Contains("ModuleSYPartTracker"))
{
ModuleSYPartTracker tracker = part.Modules["ModuleSYPartTracker"] as ModuleSYPartTracker;
tracker.ID = TrackerModule.ID.ToString();
tracker.ID = TrackerModule.ID.GetValueOrDefault();
tracker.TimesRecovered = TrackerModule.TimesRecovered;
tracker.Inventoried = TrackerModule.Inventoried;
}
Expand Down Expand Up @@ -441,10 +459,9 @@ public ConfigNode State
value.TryGetValue("_timesRecovered", ref timesRecovered) |
value.TryGetValue("_inventoried", ref inventoried)) // the single | makes all of them happen, we need at least one to succeed
{
Guid? idGuid = Utilities.Utils.StringToGuid(idStr);
if (idGuid.HasValue)
if (uint.TryParse(idStr, out uint id))
{
TrackerModule = new TrackerModuleWrapper(idGuid.Value, timesRecovered, inventoried);
TrackerModule = new TrackerModuleWrapper(id, timesRecovered, inventoried);
}
}

Expand Down Expand Up @@ -473,11 +490,7 @@ public override int GetHashCode()
{
if (_hash == 0)
{
foreach (char s in ID?.ToString() ?? string.Empty)
{
_hash += s;
}
_hash *= 31;
_hash = ID.GetHashCode();
}
return _hash;
}
Expand All @@ -495,11 +508,13 @@ public override bool Equals(object obj)

public InventoryPart Copy()
{
InventoryPart copy = new InventoryPart();
copy._dryCost = _dryCost;
copy._name = _name;
copy.savedModules = new List<ConfigNode>(savedModules);
copy.TrackerModule = new TrackerModuleWrapper(TrackerModule?.TrackerNode?.CreateCopy());
InventoryPart copy = new InventoryPart
{
_dryCost = _dryCost,
_name = _name,
savedModules = new List<ConfigNode>(savedModules),
TrackerModule = new TrackerModuleWrapper(TrackerModule?.TrackerNode?.CreateCopy())
};
if (!copy.TrackerModule.HasModule && TrackerModule != null)
{
copy.TrackerModule = new TrackerModuleWrapper(TrackerModule.ID.Value, TrackerModule.TimesRecovered, TrackerModule.Inventoried);
Expand Down
36 changes: 24 additions & 12 deletions ScrapYard/Modules/ModuleSYPartTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,22 @@ namespace ScrapYard.Modules
public class ModuleSYPartTracker : PartModule
{
[KSPField(isPersistant = true, guiActive = true, guiActiveEditor = true)]
public string ID = null;
private uint id = 0;
[KSPField(isPersistant = true, guiActive = true, guiActiveEditor = true)]
public int TimesRecovered = 0;
[KSPField(isPersistant = true, guiActive = true, guiActiveEditor = true)]
public bool Inventoried = false;

public uint ID
{
get { return id; }
set
{
id = value;
part.persistentId = value;

This comment has been minimized.

Copy link
@Lisias

Lisias May 28, 2023

Collaborator

See net-lisias-ksp/KSP-Recall#67 where the consequences (and probable reason) for this line are thoughtfully discussed.

}
}

[KSPEvent(guiActiveEditor = true, guiName = "Select From Inventory")]
public void OpenInventory()
{
Expand All @@ -27,18 +37,25 @@ public void OpenInventory()
public override void OnStart(StartState state)
{
base.OnStart(state);
if (state == StartState.Editor && string.IsNullOrEmpty(ID))
if (state == StartState.Editor && id == 0)
{
id = part.persistentId;
}
else if (id != 0)
{
ID = NewID();
ID = id; //set it on the part
}
}
public override void OnInitialize()
{
base.OnInitialize();
if (string.IsNullOrEmpty(ID))
if (id == 0)
{
id = part.persistentId;
}
else
{
//MakeFresh();
ID = NewID();
ID = id; //set it on the part
}
}

Expand All @@ -48,14 +65,9 @@ public override void OnCopy(PartModule fromModule)
MakeFresh();
}

protected string NewID()
{
return Guid.NewGuid().ToString();
}

public void MakeFresh()
{
ID = NewID();
ID = part.persistentId;
TimesRecovered = 0;
Inventoried = false;
}
Expand Down
14 changes: 5 additions & 9 deletions ScrapYard/Modules/TrackerModuleWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public TrackerModuleWrapper(ConfigNode trackerConfigNode)
/// <param name="id">The ID</param>
/// <param name="recovered">The number of times recovered</param>
/// <param name="inventoried">Whether the part is from the inventory</param>
public TrackerModuleWrapper(Guid id, int recovered, bool inventoried)
public TrackerModuleWrapper(uint id, int recovered, bool inventoried)
{
_id = id;
_timesRecovered = recovered;
Expand All @@ -37,26 +37,22 @@ public TrackerModuleWrapper(Guid id, int recovered, bool inventoried)
/// </summary>
public bool HasModule { get { return TrackerNode != null; } }

private Guid? _id = null;
private uint? _id = null;
/// <summary>
/// The unique ID for this part
/// </summary>
public Guid? ID
public uint? ID
{
get
{
if (_id == null && HasModule)
{
string id = null;
uint id = 0;
if (TrackerNode.TryGetValue("ID", ref id))
{
_id = Utils.StringToGuid(id);
_id = id;
}
}
if (_id == null)
{
_id = Guid.NewGuid();
}
return _id;
}
set
Expand Down
20 changes: 10 additions & 10 deletions ScrapYard/PartInventory.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using UnityEngine;
using ScrapYard.Modules;
using ScrapYard.Utilities;
using System.Diagnostics;

namespace ScrapYard
Expand Down Expand Up @@ -95,7 +91,7 @@ public InventoryPart AddPart(ConfigNode partNode)
/// </summary>
/// <param name="id">The ID to search for</param>
/// <returns>The stored InventoryPart or null if not found</returns>
public InventoryPart FindPart(Guid id)
public InventoryPart FindPart(uint id)
{
if (!InventoryEnabled)
{
Expand Down Expand Up @@ -216,7 +212,7 @@ public InventoryPart RemovePart(InventoryPart part, ComparisonStrength strength
/// </summary>
/// <param name="id">The ID of the part to remove</param>
/// <returns>The removed InventoryPart, or null if none found</returns>
public InventoryPart RemovePart(Guid id)
public InventoryPart RemovePart(uint id)
{
if (!InventoryEnabled)
{
Expand Down Expand Up @@ -246,8 +242,10 @@ public PartInventory Copy(bool disableEventsOnCopy = true)
try
{
disableEvents = true;
ret = new PartInventory(disableEventsOnCopy);
ret.internalInventory = new HashSet<InventoryPart>(internalInventory.Select(p => p.Copy()));
ret = new PartInventory(disableEventsOnCopy)
{
internalInventory = new HashSet<InventoryPart>(internalInventory.Select(p => p.Copy()))
};
//ret.State = State;
}
catch (Exception ex)
Expand Down Expand Up @@ -296,8 +294,10 @@ internal set

foreach (ConfigNode inventoryPartNode in value.GetNodes(typeof(InventoryPart).FullName))
{
InventoryPart loading = new InventoryPart();
loading.State = inventoryPartNode;
InventoryPart loading = new InventoryPart
{
State = inventoryPartNode
};
internalInventory.Add(loading);
}
}
Expand Down
Loading

0 comments on commit 0576f77

Please sign in to comment.