-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Discoverables and Regions patching :3
- Loading branch information
1 parent
40159e3
commit ed00941
Showing
7 changed files
with
392 additions
and
3 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/PatchManager.Science/Modifiables/DiscoverablesModifiable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
125
src/PatchManager.Science/Selectables/DiscoverablesSelectable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.