Added basic entity scripts
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Babushka.scripts.CSharp.GameEntity.LoadSave;
|
||||
|
||||
public static class EntityLoadSaveUtil
|
||||
{
|
||||
private static void AssertTokenType(this JObject json, string key, JTokenType type)
|
||||
{
|
||||
var token = json[key];
|
||||
if (token == null) throw new MalformedJsonException(json, key, "does not exist");
|
||||
if (!token.HasValues) throw new MalformedJsonException(json, key, "has no value");
|
||||
if (token.Type != type) throw new MalformedJsonException(json, key, $"is not of type {type}");
|
||||
}
|
||||
|
||||
public static long GetLongValue(this JObject json, string key)
|
||||
{
|
||||
AssertTokenType(json, key, JTokenType.Integer);
|
||||
return json.Value<long>(key);
|
||||
}
|
||||
|
||||
public static float GetFloatValue(this JObject json, string key)
|
||||
{
|
||||
AssertTokenType(json, key, JTokenType.Float);
|
||||
return json.Value<float>(key);
|
||||
}
|
||||
|
||||
public static string GetStringValue(this JObject json, string key)
|
||||
{
|
||||
AssertTokenType(json, key, JTokenType.String);
|
||||
return json.Value<string>(key)!;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user