Skip to content

Commit

Permalink
add GetValues and some flags to the Container
Browse files Browse the repository at this point in the history
  • Loading branch information
cengelha committed Sep 9, 2023
1 parent 8967605 commit df7aacf
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
38 changes: 34 additions & 4 deletions libNOM.io/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class Container : IComparable<Container>, IEquatable<Container>
public Exception? IncompatibilityException { get; internal set; }

/// <summary>
/// A tag with information why this save is incompatible. To see what reasons are available have a look at <see cref="libNOM.io.Globals.Constants"/>.INCOMPATIBILITY_\d{3}.
/// A tag with information why this save is incompatible. To see what reasons are available have a look at <see cref="Globals.Constants"/>.INCOMPATIBILITY_\d{3}.
/// </summary>
public string? IncompatibilityTag { get; internal set; }

Expand All @@ -60,6 +60,21 @@ public class Container : IComparable<Container>, IEquatable<Container>

// public //

/// <summary>
/// Whether this contains potential user owned bases.
/// </summary>
public bool HasBase => IsLoaded && GetJsonValues<PersistentBaseTypesEnum>("6f=.F?0[*].peI.DPp", "PlayerStateData.PersistentPlayerBases[*].BaseType.PersistentBaseTypes").Any(i => i is PersistentBaseTypesEnum.HomePlanetBase or PersistentBaseTypesEnum.FreighterBase); // { get; }

/// <summary>
/// Whether this contains a user owned freighter.
/// </summary>
public bool HasFreighter => IsLoaded && (GetJsonValues<double>("6f=.lpm[*]", "PlayerStateData.FreighterMatrixPos[*]")?.Any(i => i != 0.0) ?? false); // { get; }

/// <summary>
/// Whether this contains a potential user owned settlement.
/// </summary>
public bool HasSettlement => IsLoaded && (GetJsonValues<string>("6f=.GQA[*].3?K.f5Q", "PlayerStateData.SettlementStatesV2[*].Owner.LID")?.Any(i => !string.IsNullOrEmpty(i)) ?? false); // { get; }

/// <summary>
/// Whether this contains account data and is not a regular save.
/// </summary>
Expand All @@ -75,6 +90,11 @@ public class Container : IComparable<Container>, IEquatable<Container>
/// </summary>
public bool IsCompatible => Exists && string.IsNullOrEmpty(IncompatibilityTag); // { get; }

/// <summary>
/// Whether this is a save with an ongoing expedition (<see cref="PresetGameModeEnum.Seasonal"/>).
/// </summary>
public bool IsExpedition => GameMode == PresetGameModeEnum.Seasonal; // { get; }

/// <summary>
/// Whether this contains loaded JSON data and is ready to use.
/// </summary>
Expand Down Expand Up @@ -153,8 +173,6 @@ public class Container : IComparable<Container>, IEquatable<Container>

public bool IsVersion440Echoes => IsVersion(GameVersionEnum.Echoes); // { get; }

// internal //

#endregion

#region FileInfo
Expand Down Expand Up @@ -426,7 +444,7 @@ public IEnumerable<JToken> GetJsonTokens(params string[] paths)
}

/// <summary>
/// Gets the actual value of the JSON element that matches the JSONPath expression.
/// Gets the actual value of the JSON element that matches the first valid JSONPath expression.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="paths">A collection of JSONPath expressions.</param>
Expand All @@ -437,6 +455,18 @@ public IEnumerable<JToken> GetJsonTokens(params string[] paths)
return _jsonObject!.GetValue<T>(paths);
}

/// <summary>
/// Gets the actual values of all JSON elements that matches the first valid JSONPath expression.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="paths">A collection of JSONPath expressions.</param>
/// <returns>The value of the first valid expression.</returns>
public IEnumerable<T?> GetJsonValues<T>(params string[] paths)
{
ThrowHelperIsLoaded();
return _jsonObject!.GetValues<T>(paths);
}

// private //

private JToken GetJsonTokenWithValue(ReadOnlySpan<int> indices)
Expand Down
31 changes: 27 additions & 4 deletions libNOM.io/Extensions/Newtonsoft.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,39 @@ public static string GetString(this JObject self, bool indent, bool obfuscate)
{
var type = typeof(T);
if (type.IsEnum)
{
var value = jToken.Value<string>();
return value is null ? default : (T)(Enum.Parse(type, value));
}
return jToken.Value<string>() is string stringValue ? (T)(Enum.Parse(type, stringValue)) : default;

return jToken.Value<T>();
}
}
return default;
}

/// <summary>
/// Evaluates a JSONPath expression and converts all values to <typeparamref name="T"/>.
/// Multiple paths can be passed to cover different obfuscation states.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="self"></param>
/// <param name="paths"></param>
/// <returns></returns>
public static IEnumerable<T?> GetValues<T>(this JObject self, params string[] paths)
{
foreach (var path in paths)
{
var jTokens = self.SelectTokens(path);
if (jTokens.Any())
{
var type = typeof(T);
if (type.IsEnum)
return jTokens.Select(i => i.Value<string>()).Where(j => j is not null).Select(k => (T)(Enum.Parse(type, k!)));

return jTokens.Select(i => i.Value<T>());
}
}
return Enumerable.Empty<T>();
}

/// <summary>
/// Evaluates a JSONPath expression and converts its value to <typeparamref name="T"/>.
/// Multiple paths can be passed to cover different obfuscation states.
Expand Down
2 changes: 1 addition & 1 deletion libNOM.io/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,4 @@
<value>{{ secrets.STEAM_API_KEY }}</value>
<comment>https://steamcommunity.com/dev/apikey</comment>
</data>
</root>
</root>
2 changes: 1 addition & 1 deletion libNOM.io/libNOM.io.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

<!-- Package -->
<PropertyGroup>
<Version>0.5.99.2</Version>
<Version>0.5.99.3</Version>
<Authors>cengelha</Authors>
<Description>Provides reading and writing save files from the game No Man's Sky for all possible platforms as well as related actions.</Description>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
2 changes: 1 addition & 1 deletion libNOM.test/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@
<data name="TESTSUITE_PASSWORD" xml:space="preserve">
<value>{{ secrets.TESTSUITE_PASSWORD }}</value>
</data>
</root>
</root>

0 comments on commit df7aacf

Please sign in to comment.