- Refactor D3D12 backend and RenderGraph module - Update graphics RHI and core rendering components - Add Random.hlsl shader include - Regenerate API documentation and update user guides
220 lines
8.2 KiB
HTML
220 lines
8.2 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Serialization Pattern | GhostEngine </title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta name="title" content="Serialization Pattern | GhostEngine ">
|
|
|
|
|
|
<link rel="icon" href="../favicon.ico">
|
|
<link rel="stylesheet" href="../public/docfx.min.css">
|
|
<link rel="stylesheet" href="../public/main.css">
|
|
<meta name="docfx:navrel" content="../toc.html">
|
|
<meta name="docfx:tocrel" content="toc.html">
|
|
|
|
<meta name="docfx:rel" content="../">
|
|
|
|
|
|
|
|
<meta name="loc:inThisArticle" content="In this article">
|
|
<meta name="loc:searchResultsCount" content="{count} results for "{query}"">
|
|
<meta name="loc:searchNoResults" content="No results for "{query}"">
|
|
<meta name="loc:tocFilter" content="Filter by title">
|
|
<meta name="loc:nextArticle" content="Next">
|
|
<meta name="loc:prevArticle" content="Previous">
|
|
<meta name="loc:themeLight" content="Light">
|
|
<meta name="loc:themeDark" content="Dark">
|
|
<meta name="loc:themeAuto" content="Auto">
|
|
<meta name="loc:changeTheme" content="Change theme">
|
|
<meta name="loc:copy" content="Copy">
|
|
<meta name="loc:downloadPdf" content="Download PDF">
|
|
|
|
<script type="module" src="./../public/docfx.min.js"></script>
|
|
|
|
<script>
|
|
const theme = localStorage.getItem('theme') || 'auto'
|
|
document.documentElement.setAttribute('data-bs-theme', theme === 'auto' ? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : theme)
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body class="tex2jax_ignore" data-layout="" data-yaml-mime="">
|
|
<header class="bg-body border-bottom">
|
|
<nav id="autocollapse" class="navbar navbar-expand-md" role="navigation">
|
|
<div class="container-xxl flex-nowrap">
|
|
<a class="navbar-brand" href="../index.html">
|
|
<img id="logo" class="svg" src="../logo.svg" alt="GhostEngine">
|
|
GhostEngine
|
|
</a>
|
|
<button class="btn btn-lg d-md-none border-0" type="button" data-bs-toggle="collapse" data-bs-target="#navpanel" aria-controls="navpanel" aria-expanded="false" aria-label="Toggle navigation">
|
|
<i class="bi bi-three-dots"></i>
|
|
</button>
|
|
<div class="collapse navbar-collapse" id="navpanel">
|
|
<div id="navbar">
|
|
<form class="search" role="search" id="search">
|
|
<i class="bi bi-search"></i>
|
|
<input class="form-control" id="search-query" type="search" disabled placeholder="Search" autocomplete="off" aria-label="Search">
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
</header>
|
|
|
|
<main class="container-xxl">
|
|
<div class="toc-offcanvas">
|
|
<div class="offcanvas-md offcanvas-start" tabindex="-1" id="tocOffcanvas" aria-labelledby="tocOffcanvasLabel">
|
|
<div class="offcanvas-header">
|
|
<h5 class="offcanvas-title" id="tocOffcanvasLabel">Table of Contents</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" data-bs-target="#tocOffcanvas" aria-label="Close"></button>
|
|
</div>
|
|
<div class="offcanvas-body">
|
|
<nav class="toc" id="toc"></nav>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="content">
|
|
<div class="actionbar">
|
|
<button class="btn btn-lg border-0 d-md-none" type="button" data-bs-toggle="offcanvas" data-bs-target="#tocOffcanvas" aria-controls="tocOffcanvas" aria-expanded="false" aria-label="Show table of contents">
|
|
<i class="bi bi-list"></i>
|
|
</button>
|
|
|
|
<nav id="breadcrumb"></nav>
|
|
</div>
|
|
|
|
<article data-uid="">
|
|
<h1 id="serialization-pattern">Serialization Pattern</h1>
|
|
|
|
<p>This page documents the manual world serialization pattern demonstrated in <code>src/Test/Ghost.Entities.Test/SerializationTest.cs</code>.</p>
|
|
<h2 id="scope">Scope</h2>
|
|
<p>The current test shows a low-level JSON snapshot workflow for ECS data:</p>
|
|
<ul>
|
|
<li>Enumerate archetypes and chunks.</li>
|
|
<li>Read entity IDs and raw component bytes.</li>
|
|
<li>Convert bytes to managed instances.</li>
|
|
<li>Serialize component payloads as JSON.</li>
|
|
<li>Parse JSON back into typed objects.</li>
|
|
</ul>
|
|
<p>This is useful for debugging, tooling, and prototype save/load workflows.</p>
|
|
<h2 id="write-path-world---json">Write Path (World -> JSON)</h2>
|
|
<p>Core flow from the test:</p>
|
|
<pre><code class="lang-csharp">using var stream = new MemoryStream();
|
|
using var writer = new Utf8JsonWriter(stream, new JsonWriterOptions { Indented = true });
|
|
|
|
writer.WriteStartObject();
|
|
writer.WriteString("Name", "world 1");
|
|
writer.WriteStartArray("Entities");
|
|
|
|
for (var i = 0; i < world.ComponentManager.ArchetypeCount; i++)
|
|
{
|
|
ref var archetype = ref world.ComponentManager.GetArchetypeReference(i);
|
|
|
|
for (var j = 0; j < archetype.ChunkCount; j++)
|
|
{
|
|
ref var chunk = ref archetype.GetChunkReference(j);
|
|
for (var k = 0; k < chunk._count; k++)
|
|
{
|
|
writer.WriteStartObject();
|
|
writer.WriteNumber("ID", entityID);
|
|
writer.WriteStartArray("Components");
|
|
|
|
// For each component in archetype layout:
|
|
// - resolve runtime type
|
|
// - marshal bytes to managed object
|
|
// - JsonSerializer.Serialize(writer, instance, type, options)
|
|
|
|
writer.WriteEndArray();
|
|
writer.WriteEndObject();
|
|
}
|
|
}
|
|
}
|
|
|
|
writer.WriteEndArray();
|
|
writer.WriteEndObject();
|
|
writer.Flush();
|
|
</code></pre>
|
|
<p>Data shape used in the test:</p>
|
|
<pre><code class="lang-json">{
|
|
"Name": "world 1",
|
|
"Entities": [
|
|
{
|
|
"ID": 1,
|
|
"Components": [
|
|
{
|
|
"Type": "Full.AssemblyQualified.TypeName",
|
|
"Data": { }
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
</code></pre>
|
|
<h2 id="read-path-json---typed-data">Read Path (JSON -> Typed Data)</h2>
|
|
<p>The test parses JSON and reconstructs typed component instances:</p>
|
|
<pre><code class="lang-csharp">var root = JsonDocument.ParseValue(ref reader).RootElement;
|
|
var entityData = new List<(int EntityID, Type ComponentType, object Instance)>();
|
|
|
|
foreach (var entityElement in root.GetProperty("Entities").EnumerateArray())
|
|
{
|
|
var id = entityElement.GetProperty("ID").GetInt32();
|
|
|
|
foreach (var componentElement in entityElement.GetProperty("Components").EnumerateArray())
|
|
{
|
|
var typeName = componentElement.GetProperty("Type").GetString();
|
|
var type = Type.GetType(typeName!);
|
|
if (type == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var instance = componentElement.GetProperty("Data").Deserialize(type, options);
|
|
if (instance != null)
|
|
{
|
|
entityData.Add((id, type, instance));
|
|
}
|
|
}
|
|
}
|
|
</code></pre>
|
|
<h2 id="caveats">Caveats</h2>
|
|
<ul>
|
|
<li>The example is intentionally low-level and may use internal layout details.</li>
|
|
<li><code>AssemblyQualifiedName</code> based type resolution is convenient but can be brittle across assembly/version changes.</li>
|
|
<li>For production save/load, prefer stable type identifiers and versioned schemas.</li>
|
|
<li>Reflection + marshaling has overhead; avoid in per-frame hot paths.</li>
|
|
</ul>
|
|
<h2 id="recommended-use-cases">Recommended Use Cases</h2>
|
|
<ul>
|
|
<li>Inspecting world state during development.</li>
|
|
<li>Exporting compact debugging snapshots.</li>
|
|
<li>Prototyping serializer architecture before introducing a dedicated format.</li>
|
|
</ul>
|
|
<p>For public runtime API details, see generated docs in <code>doc/api/</code>.</p>
|
|
|
|
</article>
|
|
|
|
<div class="contribution d-print-none">
|
|
</div>
|
|
|
|
<div class="next-article d-print-none border-top" id="nextArticle"></div>
|
|
|
|
</div>
|
|
|
|
<div class="affix">
|
|
<nav id="affix"></nav>
|
|
</div>
|
|
</main>
|
|
|
|
<div class="container-xxl search-results" id="search-results"></div>
|
|
|
|
<footer class="border-top text-secondary">
|
|
<div class="container-xxl">
|
|
<div class="flex-fill">
|
|
<span>Made with <a href="https://dotnet.github.io/docfx">docfx</a></span>
|
|
</div>
|
|
</div>
|
|
</footer>
|
|
</body>
|
|
</html>
|