Skip to content

Commit 16f6538

Browse files
authored
Merge pull request #28 from KSP2Community/dev
0.9.0
2 parents 1240cdb + 22d1e45 commit 16f6538

File tree

9 files changed

+401
-8
lines changed

9 files changed

+401
-8
lines changed

.github/workflows/on-release.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Publish NuGet and upload release
22
env:
33
MOD_ID: 3482
4-
LATEST_GAME_VERSION: 0.2.0.0
4+
KSP2_ID: 22407
55

66
on:
77
release:
@@ -35,7 +35,7 @@ jobs:
3535
echo "artifact_name=PatchManager-$version.zip" >> $GITHUB_ENV
3636
echo "zip=$(ls -1 dist/PatchManager-*.zip | head -n 1)" >> $GITHUB_ENV
3737
echo "upload_url=$(wget -qO- https://api.github.com/repos/$GITHUB_REPOSITORY/releases | jq '.[0].upload_url' | tr -d \")" >> $GITHUB_ENV
38-
echo "changelog=$(wget -qO- https://api.github.com/repos/$GITHUB_REPOSITORY/releases | jq '.[0].body' | tr -d \" | sed 's/@/%40/g')" >> $GITHUB_ENV
38+
echo "changelog=$(wget -qO- https://api.github.com/repos/$GITHUB_REPOSITORY/releases | jq '.[0].body' | tr -d \" | jq -Rr @uri)" >> $GITHUB_ENV
3939
4040
- name: Check if version exists
4141
id: check-version
@@ -79,10 +79,14 @@ jobs:
7979
else
8080
echo "Login to space dock successful"
8181
fi
82-
82+
83+
- name: Query latest game version
84+
run: |
85+
echo "LATEST_GAME_VERSION=$(curl 'https://spacedock.info/api/${{ env.KSP2_ID }}/versions' | jq '.[0].friendly_version' | tr -d \")" >> $GITHUB_ENV
86+
8387
- name: Update mod on spacedock
8488
run: |
85-
result=$(curl -b ./cookies -F "version=${{ env.version }}" -F "changelog=${{ env.changelog }}" -F "game-version=${{ env.LATEST_GAME_VERSION}}" -F "notify-followers=yes" -F "zipball=@${{ env.zip }}" "https://spacedock.info/api/mod/${{ env.MOD_ID }}/update")
89+
result=$(curl -b ./cookies -F "version=${{ env.version }}" -F "changelog=${{ env.changelog }}" -F "game-version=${{ env.LATEST_GAME_VERSION }}" -F "notify-followers=yes" -F "zipball=@${{ env.zip }}" "https://spacedock.info/api/mod/${{ env.MOD_ID }}/update")
8690
errored=$(echo $result | jq .error)
8791
if [ "$errored" == "true" ]; then
8892
echo "Upload to space dock errored: $(echo $result | jq .reason)"

plugin_template/swinfo.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"name": "Patch Manager",
66
"description": "A mod for generic patching needs similar to KSP 1's Module Manager.",
77
"source": "https://github.com/KSP2Community/PatchManager",
8-
"version": "0.8.0",
8+
"version": "0.9.0",
99
"version_check": "https://raw.githubusercontent.com/KSP2Community/PatchManager/main/plugin_template/swinfo.json",
1010
"ksp2_version": {
1111
"min": "0.2.0",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using PatchManager.SassyPatching;
2+
using PatchManager.SassyPatching.Modifiables;
3+
using PatchManager.Science.Selectables;
4+
5+
namespace PatchManager.Science.Modifiables;
6+
/// <summary>
7+
/// Modifiable for <see cref="DiscoverablesSelectable"/>. This is used to modify the discoverables object.
8+
/// </summary>
9+
public class DiscoverablesModifiable : JTokenModifiable
10+
{
11+
private DiscoverablesSelectable _discoverablesSelectable;
12+
13+
/// <summary>
14+
/// Creates a new <see cref="DiscoverablesModifiable"/> for the given <see cref="DiscoverablesSelectable"/>.
15+
/// </summary>
16+
/// <param name="selectable">The selectable to modify.</param>
17+
public DiscoverablesModifiable(DiscoverablesSelectable selectable) : base(selectable.DiscoverablesObject, selectable.SetModified)
18+
{
19+
_discoverablesSelectable = selectable;
20+
}
21+
22+
/// <inheritdoc/>
23+
public override void Set(DataValue dataValue)
24+
{
25+
if (dataValue.IsDeletion)
26+
{
27+
_discoverablesSelectable.SetDeleted();
28+
return;
29+
}
30+
base.Set(dataValue);
31+
}
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using PatchManager.SassyPatching;
2+
using PatchManager.SassyPatching.Modifiables;
3+
using PatchManager.Science.Selectables;
4+
5+
namespace PatchManager.Science.Modifiables;
6+
7+
/// <summary>
8+
/// Modifiable for <see cref="RegionsSelectable"/>. This is used to modify the discoverables object.
9+
/// </summary>
10+
public class RegionsModifiable : JTokenModifiable
11+
{
12+
private RegionsSelectable _regionsSelectable;
13+
14+
/// <summary>
15+
/// Creates a new <see cref="RegionsModifiable"/> for the given <see cref="RegionsSelectable"/>.
16+
/// </summary>
17+
/// <param name="selectable">The selectable to modify.</param>
18+
public RegionsModifiable(RegionsSelectable selectable) : base(selectable.RegionsObject, selectable.SetModified)
19+
{
20+
_regionsSelectable = selectable;
21+
}
22+
23+
/// <inheritdoc/>
24+
public override void Set(DataValue dataValue)
25+
{
26+
if (dataValue.IsDeletion)
27+
{
28+
_regionsSelectable.SetDeleted();
29+
return;
30+
}
31+
base.Set(dataValue);
32+
}
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using KSP.Game.Science;
2+
using Newtonsoft.Json.Linq;
3+
using PatchManager.SassyPatching;
4+
using PatchManager.SassyPatching.Attributes;
5+
using PatchManager.SassyPatching.Interfaces;
6+
using PatchManager.SassyPatching.NewAssets;
7+
using PatchManager.Science.Selectables;
8+
9+
namespace PatchManager.Science.Rulesets;
10+
11+
/// <summary>
12+
/// A ruleset for modifying discoverable positions in the game
13+
/// </summary>
14+
[PatcherRuleset("discoverables","science_region_discoverables")]
15+
public class DiscoverablesRuleset : IPatcherRuleSet
16+
{
17+
/// <inheritdoc />
18+
public bool Matches(string label) => label == "science_region_discoverables";
19+
20+
/// <inheritdoc />
21+
public ISelectable ConvertToSelectable(string type, string name, string jsonData) =>
22+
new DiscoverablesSelectable(JObject.Parse(jsonData));
23+
24+
/// <inheritdoc />
25+
public INewAsset CreateNew(List<DataValue> dataValues)
26+
{
27+
var bodyName = dataValues[0].String;
28+
var bakedDiscoverables = new CelestialBodyBakedDiscoverables
29+
{
30+
BodyName = bodyName,
31+
Discoverables = [],
32+
Version = dataValues.Count > 1 ? dataValues[1].String : "0.1"
33+
};
34+
return new NewGenericAsset("science_region_discoverables",
35+
$"{bodyName.ToLowerInvariant()}_science_regions_discoverables",
36+
new DiscoverablesSelectable(JObject.FromObject(bakedDiscoverables)));
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using KSP.Game.Science;
2+
using Newtonsoft.Json.Linq;
3+
using PatchManager.SassyPatching;
4+
using PatchManager.SassyPatching.Attributes;
5+
using PatchManager.SassyPatching.Interfaces;
6+
using PatchManager.SassyPatching.NewAssets;
7+
using PatchManager.Science.Selectables;
8+
9+
namespace PatchManager.Science.Rulesets;
10+
/// <summary>
11+
/// Ruleset for science regions
12+
/// </summary>
13+
[PatcherRuleset("regions","science_region")]
14+
public class RegionsRuleset : IPatcherRuleSet
15+
{
16+
/// <inheritdoc />
17+
public bool Matches(string label) => label == "science_region";
18+
19+
/// <inheritdoc />
20+
public ISelectable ConvertToSelectable(string type, string name, string jsonData) =>
21+
new RegionsSelectable(JObject.Parse(jsonData));
22+
23+
/// <inheritdoc />
24+
public INewAsset CreateNew(List<DataValue> dataValues)
25+
{
26+
var bodyName = dataValues[0].String;
27+
var bakedDiscoverables = new CelestialBodyScienceRegionsData
28+
{
29+
BodyName = bodyName,
30+
SituationData = new CBSituationData(),
31+
Regions = [],
32+
Version = dataValues.Count > 1 ? dataValues[1].String : "0.1"
33+
};
34+
return new NewGenericAsset("science_region",
35+
$"{bodyName.ToLowerInvariant()}_science_regions",
36+
new RegionsSelectable(JObject.FromObject(bakedDiscoverables)));
37+
}
38+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using KSP.Game.Science;
2+
using Newtonsoft.Json.Linq;
3+
using PatchManager.SassyPatching;
4+
using PatchManager.SassyPatching.Interfaces;
5+
using PatchManager.SassyPatching.Selectables;
6+
using PatchManager.Science.Modifiables;
7+
8+
namespace PatchManager.Science.Selectables;
9+
10+
/// <summary>
11+
/// A selectable that represents discoverables
12+
/// </summary>
13+
public sealed class DiscoverablesSelectable : BaseSelectable
14+
{
15+
#pragma warning disable CS0414 // Field is assigned but its value is never used
16+
private bool _modified;
17+
#pragma warning restore CS0414 // Field is assigned but its value is never used
18+
private bool _deleted;
19+
20+
/// <summary>
21+
/// Marks this part selectable as having been modified any level down
22+
/// </summary>
23+
public void SetModified()
24+
{
25+
_modified = true;
26+
}
27+
28+
/// <summary>
29+
/// Marks this part as goneso
30+
/// </summary>
31+
public void SetDeleted()
32+
{
33+
SetModified();
34+
_deleted = true;
35+
}
36+
37+
38+
/// <summary>
39+
/// The main object that represents this discoverable object
40+
/// </summary>
41+
public JObject DiscoverablesObject;
42+
private JArray _discoverablesArray;
43+
44+
/// <summary>
45+
/// Create a new discoverables selectable from a JObject
46+
/// </summary>
47+
/// <param name="discoverablesObject">Discoverable </param>
48+
public DiscoverablesSelectable(JObject discoverablesObject)
49+
{
50+
DiscoverablesObject = discoverablesObject;
51+
ElementType = "discoverables";
52+
Name = discoverablesObject["BodyName"].Value<string>();
53+
Children = [];
54+
Classes =
55+
["BodyName"];
56+
_discoverablesArray = (JArray)discoverablesObject["Discoverables"];
57+
foreach (var jToken in _discoverablesArray)
58+
{
59+
var discoverable = (JObject)jToken;
60+
var name = discoverable["ScienceRegionId"]!.Value<string>();
61+
Classes.Add(name);
62+
Children.Add(new JTokenSelectable(SetModified, discoverable,
63+
disc => ((JObject)disc)["ScienceRegionId"]!.Value<string>(), "discoverable"));
64+
}
65+
}
66+
67+
/// <inheritdoc />
68+
public override List<ISelectable> Children { get; }
69+
70+
/// <inheritdoc />
71+
public override string Name { get; }
72+
73+
/// <inheritdoc />
74+
public override List<string> Classes { get; }
75+
76+
/// <inheritdoc />
77+
public override bool MatchesClass(string @class, out DataValue classValue)
78+
{
79+
foreach (var jToken in _discoverablesArray)
80+
{
81+
var discoverable = (JObject)jToken;
82+
var name = discoverable["ScienceRegionId"]!.Value<string>();
83+
if (name != @class) continue;
84+
classValue = DataValue.FromJToken(discoverable);
85+
return true;
86+
}
87+
classValue = DataValue.Null;
88+
return false;
89+
}
90+
91+
/// <inheritdoc />
92+
public override bool IsSameAs(ISelectable other) => other is DiscoverablesSelectable discoverablesSelectable &&
93+
discoverablesSelectable.DiscoverablesObject ==
94+
DiscoverablesObject;
95+
96+
/// <inheritdoc />
97+
public override IModifiable OpenModification() => new DiscoverablesModifiable(this);
98+
99+
/// <inheritdoc />
100+
public override ISelectable AddElement(string elementType)
101+
{
102+
var position = new CelestialBodyDiscoverablePosition
103+
{
104+
ScienceRegionId = elementType,
105+
Position = new Vector3d(),
106+
Radius = 0.0
107+
};
108+
var jObject = JObject.FromObject(position);
109+
_discoverablesArray.Add(jObject);
110+
var selectable = new JTokenSelectable(SetModified, jObject,
111+
disc => ((JObject)disc)["ScienceRegionId"]!.Value<string>(), "discoverable");
112+
Classes.Add(elementType);
113+
Children.Add(selectable);
114+
return selectable;
115+
}
116+
117+
/// <inheritdoc />
118+
public override string Serialize() => _deleted ? "" : DiscoverablesObject.ToString();
119+
120+
/// <inheritdoc />
121+
public override DataValue GetValue() => DataValue.FromJToken(DiscoverablesObject);
122+
123+
/// <inheritdoc />
124+
public override string ElementType { get; }
125+
}

src/PatchManager.Science/Selectables/ExperimentSelectable.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public ExperimentSelectable(JObject scienceData)
5252
ElementType = "scienceExperiment";
5353
ScienceObject = scienceData;
5454
DataObject = (JObject)scienceData["data"]!;
55-
Classes = new List<string>();
56-
Children = new List<ISelectable>();
55+
Classes = [];
56+
Children = [];
5757
foreach (var subToken in DataObject)
5858
{
5959
Classes.Add(subToken.Key);
@@ -81,7 +81,6 @@ public override bool MatchesClass(string @class, out DataValue classValue)
8181

8282
classValue = DataValue.FromJToken(DataObject[@class]);
8383
return true;
84-
8584
}
8685

8786
/// <inheritdoc />

0 commit comments

Comments
 (0)