Skip to content

Commit

Permalink
Merge pull request #28 from KSP2Community/dev
Browse files Browse the repository at this point in the history
0.9.0
  • Loading branch information
cheese3660 authored Jan 3, 2024
2 parents 1240cdb + 22d1e45 commit 16f6538
Show file tree
Hide file tree
Showing 9 changed files with 401 additions and 8 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/on-release.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Publish NuGet and upload release
env:
MOD_ID: 3482
LATEST_GAME_VERSION: 0.2.0.0
KSP2_ID: 22407

on:
release:
Expand Down Expand Up @@ -35,7 +35,7 @@ jobs:
echo "artifact_name=PatchManager-$version.zip" >> $GITHUB_ENV
echo "zip=$(ls -1 dist/PatchManager-*.zip | head -n 1)" >> $GITHUB_ENV
echo "upload_url=$(wget -qO- https://api.github.com/repos/$GITHUB_REPOSITORY/releases | jq '.[0].upload_url' | tr -d \")" >> $GITHUB_ENV
echo "changelog=$(wget -qO- https://api.github.com/repos/$GITHUB_REPOSITORY/releases | jq '.[0].body' | tr -d \" | sed 's/@/%40/g')" >> $GITHUB_ENV
echo "changelog=$(wget -qO- https://api.github.com/repos/$GITHUB_REPOSITORY/releases | jq '.[0].body' | tr -d \" | jq -Rr @uri)" >> $GITHUB_ENV
- name: Check if version exists
id: check-version
Expand Down Expand Up @@ -79,10 +79,14 @@ jobs:
else
echo "Login to space dock successful"
fi
- name: Query latest game version
run: |
echo "LATEST_GAME_VERSION=$(curl 'https://spacedock.info/api/${{ env.KSP2_ID }}/versions' | jq '.[0].friendly_version' | tr -d \")" >> $GITHUB_ENV
- name: Update mod on spacedock
run: |
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")
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")
errored=$(echo $result | jq .error)
if [ "$errored" == "true" ]; then
echo "Upload to space dock errored: $(echo $result | jq .reason)"
Expand Down
2 changes: 1 addition & 1 deletion plugin_template/swinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Patch Manager",
"description": "A mod for generic patching needs similar to KSP 1's Module Manager.",
"source": "https://github.com/KSP2Community/PatchManager",
"version": "0.8.0",
"version": "0.9.0",
"version_check": "https://raw.githubusercontent.com/KSP2Community/PatchManager/main/plugin_template/swinfo.json",
"ksp2_version": {
"min": "0.2.0",
Expand Down
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 16f6538

Please sign in to comment.