Files
sbox-public/engine/Sandbox.Engine/Systems/Project/ProjectSettings/ConfigData.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

55 lines
1.3 KiB
C#

using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
namespace Sandbox;
/// <summary>
/// Project configuration data is derived from this class
/// </summary>
public abstract class ConfigData
{
[JsonPropertyName( "__guid" ), Hide]
public Guid Guid { get; set; } = Guid.NewGuid();
[JsonIgnore, Hide]
public virtual int Version => 1;
public JsonObject Serialize()
{
OnValidate();
var obj = Json.SerializeAsObject( this );
obj["__schema"] = "configdata";
obj["__type"] = GetType().Name;
obj["__version"] = Version;
return obj;
}
public void Deserialize( string json )
{
var jso = Json.ParseToJsonObject( json );
if ( jso is null ) return;
// read schema, version etc, upgrade if needed
var serializedVersion = (int)(jso["__version"] ?? 1);
if ( serializedVersion < Version )
{
// Log.Warning( $"{this} needs an API update, running upgraders (from version {serializedVersion} to {Version})" );
JsonUpgrader.Upgrade( serializedVersion, jso, GetType() );
}
Json.DeserializeToObject( this, jso );
OnValidate();
}
/// <summary>
/// Called after deserialization, and before serialization. A place to error check and make sure everything is fine.
/// </summary>
protected virtual void OnValidate()
{
// nothing
}
}