Skip to content

Commit

Permalink
Allow for rulesets to return null to make them be able to match any l…
Browse files Browse the repository at this point in the history
…abel but only assets that fit their structure
  • Loading branch information
cheese3660 committed Dec 29, 2023
1 parent 8d39079 commit 823e8b8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public bool Matches(string label)
/// <inheritdoc />
public ISelectable ConvertToSelectable(string type, string name, string jsonData)
{
Console.WriteLine($"Converting to JTokenSelectable {type}:{name}");
return new JTokenSelectable(() => { }, JToken.Parse(jsonData), name, type);
}

Expand Down
10 changes: 8 additions & 2 deletions src/PatchManager.Planets/Rulesets/GalaxyRuleset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ public class GalaxyRuleset
public bool Matches(string label) => true;

/// <inheritdoc />
public ISelectable ConvertToSelectable(string type, string name, string jsonData) =>
new GalaxySelectable(JObject.Parse(jsonData), type);
public ISelectable ConvertToSelectable(string type, string name, string jsonData)
{
var obj = JObject.Parse(jsonData);
if (!obj.ContainsKey("Name") || !obj.ContainsKey("Version") || !obj.ContainsKey("CelestialBodies"))
return null;
return new GalaxySelectable(JObject.Parse(jsonData), type);

}
/// <inheritdoc />
public INewAsset CreateNew(List<DataValue> dataValues)
{
Expand Down
54 changes: 29 additions & 25 deletions src/PatchManager.SassyPatching/Nodes/Selectors/RulesetSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,48 @@ public override List<SelectableWithEnvironment> SelectAll(List<SelectableWithEnv
/// <inheritdoc />
public override List<SelectableWithEnvironment> SelectAllTopLevel(string type, string name, string data, Environment baseEnvironment, out ISelectable rulesetMatchingObject)
{
if (Universe.RuleSets.TryGetValue(RulesetName, out var ruleSet))
if (!Universe.RuleSets.TryGetValue(RulesetName, out var ruleSet))
{
if (ruleSet.Matches(type))
throw new InterpreterException(Coordinate, $"Ruleset: {RulesetName} does not exist!");
}

if (ruleSet.Matches(type))
{
rulesetMatchingObject = ruleSet.ConvertToSelectable(type, name,data);
if (rulesetMatchingObject != null)
{
rulesetMatchingObject = ruleSet.ConvertToSelectable(type, name,data);
return new List<SelectableWithEnvironment>
{
new()
return
[
new SelectableWithEnvironment
{
Selectable = rulesetMatchingObject,
Environment = new Environment(baseEnvironment.GlobalEnvironment,baseEnvironment)
Environment = new Environment(baseEnvironment.GlobalEnvironment, baseEnvironment)
}
};
];
}
rulesetMatchingObject = null;
return new();
}
else
{
throw new InterpreterException(Coordinate, $"Ruleset: {RulesetName} does not exist!");
}
rulesetMatchingObject = null;
return [];

}

public override List<SelectableWithEnvironment> CreateNew(List<DataValue> rulesetArguments, Environment baseEnvironment, out INewAsset newAsset)
{
if (Universe.RuleSets.TryGetValue(RulesetName, out var ruleSet))
{
var newObject = ruleSet.CreateNew(rulesetArguments);
newAsset = newObject;
return new List<SelectableWithEnvironment> { new()
{
Selectable = newAsset.Selectable,
Environment = new Environment(baseEnvironment.GlobalEnvironment,baseEnvironment)
} };
}
else
if (!Universe.RuleSets.TryGetValue(RulesetName, out var ruleSet))
{
throw new InterpreterException(Coordinate, $"Ruleset: {RulesetName} does not exist!");
}

var newObject = ruleSet.CreateNew(rulesetArguments);
newAsset = newObject;
return
[
new SelectableWithEnvironment
{
Selectable = newAsset.Selectable,
Environment = new Environment(baseEnvironment.GlobalEnvironment, baseEnvironment)
}
];

}
}

0 comments on commit 823e8b8

Please sign in to comment.