Skip to content

Commit

Permalink
Merge pull request #19 from KSP2Community/dev
Browse files Browse the repository at this point in the history
0.6.0
  • Loading branch information
cheese3660 authored Dec 21, 2023
2 parents e6fb681 + a196d2a commit ef7f6e7
Show file tree
Hide file tree
Showing 37 changed files with 1,304 additions and 190 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

<PropertyGroup Label="Build and namespace configuration">
<SolutionDir Condition="'$(SolutionDir)'==''">$(MSBuildThisFileDirectory)</SolutionDir>
<PluginBinPath>$(SolutionDir)build/bin/plugin</PluginBinPath>
<PatcherBinPath>$(SolutionDir)build/bin/patcher</PatcherBinPath>
<PluginBinPath>$(SolutionDir)build/bin/plugin/$(Configuration)</PluginBinPath>
<PatcherBinPath>$(SolutionDir)build/bin/patcher/$(Configuration)</PatcherBinPath>
<BaseOutputPath>$(PluginBinPath)/$(MSBuildProjectName)</BaseOutputPath>
<BaseIntermediateOutputPath>$(SolutionDir)build/obj/plugin/$(MSBuildProjectName)</BaseIntermediateOutputPath>
<AssemblyName>$(MSBuildProjectName)</AssemblyName>
Expand Down
10 changes: 10 additions & 0 deletions PatchManager.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PatchManager.Planets", "src
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PatchManager.Science", "src\PatchManager.Science\PatchManager.Science.csproj", "{E6AD9C2A-D53E-47DD-B48F-7A9AF6ACDC55}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PatchManager.Missions", "src\PatchManager.Missions\PatchManager.Missions.csproj", "{67F415B1-5651-4426-8D4C-F607CA31A338}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -128,5 +130,13 @@ Global
{E6AD9C2A-D53E-47DD-B48F-7A9AF6ACDC55}.Deploy|Any CPU.Build.0 = Deploy|Any CPU
{E6AD9C2A-D53E-47DD-B48F-7A9AF6ACDC55}.DeployAndRun|Any CPU.ActiveCfg = DeployAndRun|Any CPU
{E6AD9C2A-D53E-47DD-B48F-7A9AF6ACDC55}.DeployAndRun|Any CPU.Build.0 = DeployAndRun|Any CPU
{67F415B1-5651-4426-8D4C-F607CA31A338}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{67F415B1-5651-4426-8D4C-F607CA31A338}.Debug|Any CPU.Build.0 = Debug|Any CPU
{67F415B1-5651-4426-8D4C-F607CA31A338}.Release|Any CPU.ActiveCfg = Release|Any CPU
{67F415B1-5651-4426-8D4C-F607CA31A338}.Release|Any CPU.Build.0 = Release|Any CPU
{67F415B1-5651-4426-8D4C-F607CA31A338}.Deploy|Any CPU.ActiveCfg = Deploy|Any CPU
{67F415B1-5651-4426-8D4C-F607CA31A338}.Deploy|Any CPU.Build.0 = Deploy|Any CPU
{67F415B1-5651-4426-8D4C-F607CA31A338}.DeployAndRun|Any CPU.ActiveCfg = DeployAndRun|Any CPU
{67F415B1-5651-4426-8D4C-F607CA31A338}.DeployAndRun|Any CPU.Build.0 = DeployAndRun|Any CPU
EndGlobalSection
EndGlobal
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.5.0",
"version": "0.6.0",
"version_check": "https://raw.githubusercontent.com/KSP2Community/PatchManager/main/plugin_template/swinfo.json",
"ksp2_version": {
"min": "0.1.5",
Expand Down
2 changes: 1 addition & 1 deletion src/PatchManager.Core/PatchManager.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<!-- References -->
<ItemGroup Label="NuGet package references">
<PackageReference Include="BepInEx.AssemblyPublicizer.MSBuild" Version="0.4.1" PrivateAssets="all" />
<PackageReference Include="KerbalSpaceProgram2.GameLibs" Version="0.1.5" PrivateAssets="all" />
<PackageReference Include="KerbalSpaceProgram2.GameLibs" Version="0.2.0" PrivateAssets="all" Publicize="true" />
<PackageReference Include="SpaceWarp" Version="1.5.2" PrivateAssets="all" Publicize="true"/>
</ItemGroup>
<ItemGroup Label="Project references">
Expand Down
18 changes: 18 additions & 0 deletions src/PatchManager.Missions/Builtins/MissionsBuiltins.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using JetBrains.Annotations;
using PatchManager.SassyPatching.Attributes;

namespace PatchManager.Missions.Builtins;

[SassyLibrary("builtin","missions")]
[UsedImplicitly]
public class MissionsBuiltins
{

[SassyMethod("get-property-watcher"), UsedImplicitly]
public static string GetPropertyWatcher([SassyName("property-watcher-name")] string propertyWatcherName) =>
MissionsTypes.PropertyWatchers[propertyWatcherName].AssemblyQualifiedName!;

[SassyMethod("get-message"), UsedImplicitly]
public static string GetMessage([SassyName("message-name")] string messageName) =>
MissionsTypes.Messages[messageName].AssemblyQualifiedName!;
}
9 changes: 9 additions & 0 deletions src/PatchManager.Missions/MissionsModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using JetBrains.Annotations;
using PatchManager.Shared.Modules;

namespace PatchManager.Missions;

[UsedImplicitly]
public class MissionsModule : BaseModule
{
}
39 changes: 39 additions & 0 deletions src/PatchManager.Missions/MissionsTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using KSP.Game.Missions;
using KSP.Messages;
using KSP.Messages.PropertyWatchers;

namespace PatchManager.Missions;

public static class MissionsTypes
{
public static Dictionary<string, Type> Conditions = new();
public static Dictionary<string, Type> Actions = new();
public static Dictionary<string, Type> Messages = new();
public static Dictionary<string, Type> PropertyWatchers = new();

static MissionsTypes()
{
foreach (var type in AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()).Where(t => !t.IsAbstract))
{
if (type.IsSubclassOf(typeof(Condition)))
{
Conditions.Add(type.Name,type);
}

if (typeof(IMissionAction).IsAssignableFrom(type))
{
Actions.Add(type.Name, type);
}

if (type.IsSubclassOf(typeof(MessageCenterMessage)))
{
Messages.Add(type.Name, type);
}

if (type.IsSubclassOf(typeof(PropertyWatcher)))
{
PropertyWatchers.Add(type.Name, type);
}
}
}
}
24 changes: 24 additions & 0 deletions src/PatchManager.Missions/Modifiables/MissionModifiable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using PatchManager.Missions.Selectables;
using PatchManager.SassyPatching;
using PatchManager.SassyPatching.Modifiables;

namespace PatchManager.Missions.Modifiables;

public class MissionModifiable : JTokenModifiable
{
private MissionSelectable _missionSelectable;

public MissionModifiable(MissionSelectable selectable) : base(selectable.MissionObject, selectable.SetModified)
{
_missionSelectable = selectable;
}

public override void Set(DataValue dataValue)
{
if (dataValue.IsDeletion)
{
_missionSelectable.SetDeleted();
}
base.Set(dataValue);
}
}
17 changes: 17 additions & 0 deletions src/PatchManager.Missions/PatchManager.Missions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\PatchManager.SassyPatching\PatchManager.SassyPatching.csproj" />
<ProjectReference Include="..\PatchManager.Shared\PatchManager.Shared.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="KerbalSpaceProgram2.GameLibs" Version="0.2.0" />
</ItemGroup>

</Project>
29 changes: 29 additions & 0 deletions src/PatchManager.Missions/Rulesets/MissionRuleset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using KSP.Game.Missions.Definitions;
using Newtonsoft.Json.Linq;
using PatchManager.Missions.Selectables;
using PatchManager.SassyPatching;
using PatchManager.SassyPatching.Attributes;
using PatchManager.SassyPatching.Interfaces;
using PatchManager.SassyPatching.NewAssets;

namespace PatchManager.Missions.Rulesets;

[PatcherRuleset("missions","missions")]
public class MissionRuleset : IPatcherRuleSet
{
public bool Matches(string label) => label == "missions";

public ISelectable ConvertToSelectable(string type, string name, string jsonData) =>
new MissionSelectable(JObject.Parse(jsonData));

public INewAsset CreateNew(List<DataValue> dataValues)
{
var missionDataObject = new MissionData
{
ID = dataValues[0].String
};

return new NewGenericAsset("missions", dataValues[0].String,
new MissionSelectable(JObject.FromObject(missionDataObject)));
}
}
93 changes: 93 additions & 0 deletions src/PatchManager.Missions/Selectables/ActionsSelectable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using Newtonsoft.Json.Linq;
using PatchManager.SassyPatching;
using PatchManager.SassyPatching.Interfaces;
using PatchManager.SassyPatching.Modifiables;
using PatchManager.SassyPatching.Selectables;

namespace PatchManager.Missions.Selectables;

public sealed class ActionsSelectable : BaseSelectable
{
public MissionSelectable Selectable;
public JArray Actions;

private static string TrimTypeName(string typeName)
{
var comma = typeName.IndexOf(',');
if (comma != -1)
{
typeName = typeName[..comma];
}

var period = typeName.LastIndexOf('.');
if (period != -1)
{
typeName = typeName[(period + 1)..];
}

return typeName;
}

public ActionsSelectable(MissionSelectable selectable, JArray actions)
{
Selectable = selectable;
Actions = actions;
Children = new();
Classes = new();
foreach (var action in actions)
{
var obj = (JObject)action;
var type = obj["$type"]!.Value<string>()!;
var trimmedType = TrimTypeName(type);
Classes.Add(trimmedType);
Children.Add(new JTokenSelectable(Selectable.SetModified,obj,token => TrimTypeName(((JObject)token)!.Value<string>()!),trimmedType));
}
}
public override List<ISelectable> Children { get; }
public override string Name => "actions";
public override List<string> Classes { get; }

public override bool MatchesClass(string @class, out DataValue classValue)
{
foreach (var action in Actions)
{
var obj = (JObject)action;
var type = obj["$type"]!.Value<string>()!;
var trimmedType = TrimTypeName(type);
if (trimmedType != @class) continue;
classValue = DataValue.FromJToken(action);
return true;
}
classValue = DataValue.Null;
return false;
}

public override bool IsSameAs(ISelectable other) =>
other is ActionsSelectable actionsSelectable && actionsSelectable.Actions == Actions;

public override IModifiable OpenModification() => new JTokenModifiable(Actions, Selectable.SetModified);

public override ISelectable AddElement(string elementType)
{
var actualType = MissionsTypes.Actions[elementType];
var elementObject = new JObject()
{
["$type"] = actualType.AssemblyQualifiedName
};
foreach (var (key, value) in JObject.FromObject(Activator.CreateInstance(actualType)))
{
elementObject[key] = value;
}
var selectable = new JTokenSelectable(Selectable.SetModified, elementObject, token => TrimTypeName(((JObject)token)!.Value<string>()!), elementType);
Children.Add(selectable);
Classes.Add(elementType);
Actions.Add(elementObject);
return selectable;
}

public override string Serialize() => Actions.ToString();

public override DataValue GetValue() => DataValue.FromJToken(Actions);

public override string ElementType => "actions";
}
97 changes: 97 additions & 0 deletions src/PatchManager.Missions/Selectables/ConditionSetSelectable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using KSP.Game.Missions;
using Newtonsoft.Json.Linq;
using PatchManager.SassyPatching;
using PatchManager.SassyPatching.Interfaces;
using PatchManager.SassyPatching.Modifiables;
using PatchManager.SassyPatching.Selectables;

namespace PatchManager.Missions.Selectables;

public sealed class ConditionSetSelectable : BaseSelectable
{
public MissionSelectable MissionSelectable;
public JObject ConditionSet;
private JArray _children;
public ConditionSetSelectable(MissionSelectable missionSelectable, JObject conditionSet)
{
MissionSelectable = missionSelectable;
ConditionSet = conditionSet;
_children = (JArray)conditionSet["Children"]!;
Children = new();
Classes = new();
Classes.Add("ConditionType");
Classes.Add("ConditionMode");
// We aren't going to add the condition type as a child, it will still be editable tho
foreach (var child in _children)
{
var condition = (JObject)child;
var type = condition["ConditionType"]!.Value<string>()!;
Classes.Add(type);
if (type == "ConditionSet")
{
Children.Add(new ConditionSetSelectable(missionSelectable,condition));
}
else
{
Children.Add(new JTokenSelectable(MissionSelectable.SetModified, condition,
token => ((JObject)token)["ConditionType"]!.Value<string>()!, type));
}
}
}

public override List<ISelectable> Children { get; }
public override string Name => "ConditionSet";
public override List<string> Classes { get; }

public override bool MatchesClass(string @class, out DataValue classValue)
{
foreach (var child in _children)
{
var condition = (JObject)child;
var type = condition["ConditionType"]!.Value<string>()!;
if (type != @class)
continue;
classValue = DataValue.FromJToken(child);
}
classValue = DataValue.Null;
return false;
}

public override bool IsSameAs(ISelectable other) => other is ConditionSetSelectable conditionSetSelectable &&
conditionSetSelectable.ConditionSet == ConditionSet;

public override IModifiable OpenModification() => new JTokenModifiable(ConditionSet, MissionSelectable.SetModified);

public override ISelectable AddElement(string elementType)
{
var conditionType = MissionsTypes.Conditions[elementType];
var conditionObject = new JObject()
{
["$type"] = conditionType.AssemblyQualifiedName
};
foreach (var (key, value) in JObject.FromObject(Activator.CreateInstance(conditionType)))
{
conditionObject[key] = value;
}
_children.Add(conditionObject);
if (conditionType == typeof(ConditionSet))
{
var selectable = new ConditionSetSelectable(MissionSelectable, conditionObject);
Children.Add(selectable);
return selectable;
}
else
{
var selectable = new JTokenSelectable(MissionSelectable.SetModified, conditionObject,
"scriptableCondition", "scriptableCondition");
Children.Add(selectable);
return selectable;
}
}

public override string Serialize() => ConditionSet.ToString();

public override DataValue GetValue() => DataValue.FromJToken(ConditionSet);

public override string ElementType => "ConditionSet";
}
Loading

0 comments on commit ef7f6e7

Please sign in to comment.