Skip to content

Commit

Permalink
Discoverables and Regions patching :3
Browse files Browse the repository at this point in the history
  • Loading branch information
cheese3660 committed Jan 3, 2024
1 parent 40159e3 commit ed00941
Show file tree
Hide file tree
Showing 7 changed files with 392 additions and 3 deletions.
32 changes: 32 additions & 0 deletions src/PatchManager.Science/Modifiables/DiscoverablesModifiable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using PatchManager.SassyPatching;
using PatchManager.SassyPatching.Modifiables;
using PatchManager.Science.Selectables;

namespace PatchManager.Science.Modifiables;
/// <summary>
/// Modifiable for <see cref="DiscoverablesSelectable"/>. This is used to modify the discoverables object.
/// </summary>
public class DiscoverablesModifiable : JTokenModifiable
{
private DiscoverablesSelectable _discoverablesSelectable;

/// <summary>
/// Creates a new <see cref="DiscoverablesModifiable"/> for the given <see cref="DiscoverablesSelectable"/>.
/// </summary>
/// <param name="selectable">The selectable to modify.</param>
public DiscoverablesModifiable(DiscoverablesSelectable selectable) : base(selectable.DiscoverablesObject, selectable.SetModified)
{
_discoverablesSelectable = selectable;
}

/// <inheritdoc/>
public override void Set(DataValue dataValue)
{
if (dataValue.IsDeletion)
{
_discoverablesSelectable.SetDeleted();
return;
}
base.Set(dataValue);
}
}
33 changes: 33 additions & 0 deletions src/PatchManager.Science/Modifiables/RegionsModifiable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using PatchManager.SassyPatching;
using PatchManager.SassyPatching.Modifiables;
using PatchManager.Science.Selectables;

namespace PatchManager.Science.Modifiables;

/// <summary>
/// Modifiable for <see cref="RegionsSelectable"/>. This is used to modify the discoverables object.
/// </summary>
public class RegionsModifiable : JTokenModifiable
{
private RegionsSelectable _regionsSelectable;

/// <summary>
/// Creates a new <see cref="RegionsModifiable"/> for the given <see cref="RegionsSelectable"/>.
/// </summary>
/// <param name="selectable">The selectable to modify.</param>
public RegionsModifiable(RegionsSelectable selectable) : base(selectable.RegionsObject, selectable.SetModified)
{
_regionsSelectable = selectable;
}

/// <inheritdoc/>
public override void Set(DataValue dataValue)
{
if (dataValue.IsDeletion)
{
_regionsSelectable.SetDeleted();
return;
}
base.Set(dataValue);
}
}
38 changes: 38 additions & 0 deletions src/PatchManager.Science/Rulesets/DiscoverablesRuleset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using KSP.Game.Science;
using Newtonsoft.Json.Linq;
using PatchManager.SassyPatching;
using PatchManager.SassyPatching.Attributes;
using PatchManager.SassyPatching.Interfaces;
using PatchManager.SassyPatching.NewAssets;
using PatchManager.Science.Selectables;

namespace PatchManager.Science.Rulesets;

/// <summary>
/// A ruleset for modifying discoverable positions in the game
/// </summary>
[PatcherRuleset("discoverables","science_region_discoverables")]
public class DiscoverablesRuleset : IPatcherRuleSet
{
/// <inheritdoc />
public bool Matches(string label) => label == "science_region_discoverables";

/// <inheritdoc />
public ISelectable ConvertToSelectable(string type, string name, string jsonData) =>
new DiscoverablesSelectable(JObject.Parse(jsonData));

/// <inheritdoc />
public INewAsset CreateNew(List<DataValue> dataValues)
{
var bodyName = dataValues[0].String;
var bakedDiscoverables = new CelestialBodyBakedDiscoverables
{
BodyName = bodyName,
Discoverables = [],
Version = dataValues.Count > 1 ? dataValues[1].String : "0.1"
};
return new NewGenericAsset("science_region_discoverables",
$"{bodyName.ToLowerInvariant()}_science_regions_discoverables",
new DiscoverablesSelectable(JObject.FromObject(bakedDiscoverables)));
}
}
38 changes: 38 additions & 0 deletions src/PatchManager.Science/Rulesets/RegionsRuleset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using KSP.Game.Science;
using Newtonsoft.Json.Linq;
using PatchManager.SassyPatching;
using PatchManager.SassyPatching.Attributes;
using PatchManager.SassyPatching.Interfaces;
using PatchManager.SassyPatching.NewAssets;
using PatchManager.Science.Selectables;

namespace PatchManager.Science.Rulesets;
/// <summary>
/// Ruleset for science regions
/// </summary>
[PatcherRuleset("regions","science_region")]
public class RegionsRuleset : IPatcherRuleSet
{
/// <inheritdoc />
public bool Matches(string label) => label == "science_region";

/// <inheritdoc />
public ISelectable ConvertToSelectable(string type, string name, string jsonData) =>
new RegionsSelectable(JObject.Parse(jsonData));

/// <inheritdoc />
public INewAsset CreateNew(List<DataValue> dataValues)
{
var bodyName = dataValues[0].String;
var bakedDiscoverables = new CelestialBodyScienceRegionsData
{
BodyName = bodyName,
SituationData = new CBSituationData(),
Regions = [],
Version = dataValues.Count > 1 ? dataValues[1].String : "0.1"
};
return new NewGenericAsset("science_region",
$"{bodyName.ToLowerInvariant()}_science_regions",
new RegionsSelectable(JObject.FromObject(bakedDiscoverables)));
}
}
125 changes: 125 additions & 0 deletions src/PatchManager.Science/Selectables/DiscoverablesSelectable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using KSP.Game.Science;
using Newtonsoft.Json.Linq;
using PatchManager.SassyPatching;
using PatchManager.SassyPatching.Interfaces;
using PatchManager.SassyPatching.Selectables;
using PatchManager.Science.Modifiables;

namespace PatchManager.Science.Selectables;

/// <summary>
/// A selectable that represents discoverables
/// </summary>
public sealed class DiscoverablesSelectable : BaseSelectable
{
#pragma warning disable CS0414 // Field is assigned but its value is never used
private bool _modified;
#pragma warning restore CS0414 // Field is assigned but its value is never used
private bool _deleted;

/// <summary>
/// Marks this part selectable as having been modified any level down
/// </summary>
public void SetModified()
{
_modified = true;
}

/// <summary>
/// Marks this part as goneso
/// </summary>
public void SetDeleted()
{
SetModified();
_deleted = true;
}


/// <summary>
/// The main object that represents this discoverable object
/// </summary>
public JObject DiscoverablesObject;
private JArray _discoverablesArray;

/// <summary>
/// Create a new discoverables selectable from a JObject
/// </summary>
/// <param name="discoverablesObject">Discoverable </param>
public DiscoverablesSelectable(JObject discoverablesObject)
{
DiscoverablesObject = discoverablesObject;
ElementType = "discoverables";
Name = discoverablesObject["BodyName"].Value<string>();
Children = [];
Classes =
["BodyName"];
_discoverablesArray = (JArray)discoverablesObject["Discoverables"];
foreach (var jToken in _discoverablesArray)
{
var discoverable = (JObject)jToken;
var name = discoverable["ScienceRegionId"]!.Value<string>();
Classes.Add(name);
Children.Add(new JTokenSelectable(SetModified, discoverable,
disc => ((JObject)disc)["ScienceRegionId"]!.Value<string>(), "discoverable"));
}
}

/// <inheritdoc />
public override List<ISelectable> Children { get; }

/// <inheritdoc />
public override string Name { get; }

/// <inheritdoc />
public override List<string> Classes { get; }

/// <inheritdoc />
public override bool MatchesClass(string @class, out DataValue classValue)
{
foreach (var jToken in _discoverablesArray)
{
var discoverable = (JObject)jToken;
var name = discoverable["ScienceRegionId"]!.Value<string>();
if (name != @class) continue;
classValue = DataValue.FromJToken(discoverable);
return true;
}
classValue = DataValue.Null;
return false;
}

/// <inheritdoc />
public override bool IsSameAs(ISelectable other) => other is DiscoverablesSelectable discoverablesSelectable &&
discoverablesSelectable.DiscoverablesObject ==
DiscoverablesObject;

/// <inheritdoc />
public override IModifiable OpenModification() => new DiscoverablesModifiable(this);

/// <inheritdoc />
public override ISelectable AddElement(string elementType)
{
var position = new CelestialBodyDiscoverablePosition
{
ScienceRegionId = elementType,
Position = new Vector3d(),
Radius = 0.0
};
var jObject = JObject.FromObject(position);
_discoverablesArray.Add(jObject);
var selectable = new JTokenSelectable(SetModified, jObject,
disc => ((JObject)disc)["ScienceRegionId"]!.Value<string>(), "discoverable");
Classes.Add(elementType);
Children.Add(selectable);
return selectable;
}

/// <inheritdoc />
public override string Serialize() => _deleted ? "" : DiscoverablesObject.ToString();

/// <inheritdoc />
public override DataValue GetValue() => DataValue.FromJToken(DiscoverablesObject);

/// <inheritdoc />
public override string ElementType { get; }
}
5 changes: 2 additions & 3 deletions src/PatchManager.Science/Selectables/ExperimentSelectable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public ExperimentSelectable(JObject scienceData)
ElementType = "scienceExperiment";
ScienceObject = scienceData;
DataObject = (JObject)scienceData["data"]!;
Classes = new List<string>();
Children = new List<ISelectable>();
Classes = [];
Children = [];
foreach (var subToken in DataObject)
{
Classes.Add(subToken.Key);
Expand Down Expand Up @@ -81,7 +81,6 @@ public override bool MatchesClass(string @class, out DataValue classValue)

classValue = DataValue.FromJToken(DataObject[@class]);
return true;

}

/// <inheritdoc />
Expand Down
Loading

0 comments on commit ed00941

Please sign in to comment.