-
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.
Merge pull request #19 from KSP2Community/dev
0.6.0
- Loading branch information
Showing
37 changed files
with
1,304 additions
and
190 deletions.
There are no files selected for viewing
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
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
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
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
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,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!; | ||
} |
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,9 @@ | ||
using JetBrains.Annotations; | ||
using PatchManager.Shared.Modules; | ||
|
||
namespace PatchManager.Missions; | ||
|
||
[UsedImplicitly] | ||
public class MissionsModule : BaseModule | ||
{ | ||
} |
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,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
24
src/PatchManager.Missions/Modifiables/MissionModifiable.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,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); | ||
} | ||
} |
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,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> |
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,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
93
src/PatchManager.Missions/Selectables/ActionsSelectable.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,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
97
src/PatchManager.Missions/Selectables/ConditionSetSelectable.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,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"; | ||
} |
Oops, something went wrong.