Changed Vector3Field.cs to derive from ValueControl<Vector3> and use NumberBox controls for better UI handling. Changed EditorControls.xaml to update resource paths for new controls. Changed InternalControls.xaml to simplify the resource dictionary by removing unnecessary references. Changed IComponentEditor.cs to reflect updates in the component editor's lifecycle methods. Changed project files for Ghost.Editor and Ghost.Core to include new dependencies and project references. Changed FileExtensions.cs and IInspectorService.cs to align with the new namespace structure. Changed Result.cs to enhance error handling and success checking methods. Changed TypeHandle.cs to improve type handling compatibility. Changed AssemblyInfo.cs files to include new assembly visibility attributes for better encapsulation. Added new graphics-related classes and interfaces in the Ghost.Engine project, including IGraphicsDevice and DX12GraphicsDevice. Added a new Mesh class to handle 3D mesh data and provide methods for creating geometric shapes. Added GraphicsPipeline.cs to manage the graphics rendering loop and device initialization. Added ScenePage.xaml and ScenePage.xaml.cs to create a new page for rendering scenes. Updated HierarchyPage.xaml.cs and InspectorPage.xaml.cs to use the new service locator pattern for service retrieval. Updated LandingWindow.xaml.cs and EngineEditorWindow.xaml.cs to utilize the new service locator pattern for better service access. Updated Logger.cs to enhance logging capabilities with optional stack traces and assertion logging. Updated QueryFilter.cs and QueryEnumerable.cs to use the new TypeHandle structure for improved efficiency. Updated WorldNode.cs and WorldNodeSerializer.cs to enhance serialization and management of world nodes. Updated AssetDatabase and related classes to improve asset management and metadata generation. Updated Ghost.UnitTest.csproj to include new project references and package dependencies for unit tests.
130 lines
4.8 KiB
C#
130 lines
4.8 KiB
C#
using Ghost.Editor.Core.SceneGraph;
|
|
using Ghost.Engine.Utilities;
|
|
using Ghost.Entities;
|
|
using Ghost.Entities.Components;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace Ghost.Editor.Core.Serializer;
|
|
|
|
internal class WorldNodeSerializer : JsonConverter<WorldNode>
|
|
{
|
|
private static class Property
|
|
{
|
|
public const string NAME = "Name";
|
|
public const string ENTITIES = "Entities";
|
|
public const string ID = "ID";
|
|
public const string ENTITY_ID = "EntityID";
|
|
public const string COMPONENTS = "Components";
|
|
public const string DATA = "Data";
|
|
public const string SYSTEMS = "Systems";
|
|
}
|
|
|
|
public override bool CanConvert(Type typeToConvert)
|
|
{
|
|
return typeToConvert == typeof(WorldNode) || typeToConvert.IsSubclassOf(typeof(WorldNode));
|
|
}
|
|
|
|
public override WorldNode? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
{
|
|
var element = JsonDocument.ParseValue(ref reader).RootElement;
|
|
var name = element.GetProperty(Property.NAME).GetString() ?? "New World";
|
|
|
|
var world = World.Create();
|
|
var result = new WorldNode(world, name);
|
|
|
|
foreach (var entityElement in element.GetProperty(Property.ENTITIES).EnumerateArray())
|
|
{
|
|
var entityName = entityElement.GetProperty(Property.NAME).GetString() ?? "New Entity";
|
|
var entityID = entityElement.GetProperty(Property.ID).GetInt32();
|
|
var entity = new Entity(entityID, 0);
|
|
var node = new EntityNode(result, entity, entityName);
|
|
|
|
world.EntityManager.AddEntityInternal(entity);
|
|
result.EntityNodeLookup[entity] = node;
|
|
}
|
|
|
|
foreach (var componentElement in element.GetProperty(Property.COMPONENTS).EnumerateObject())
|
|
{
|
|
var typeName = componentElement.Name;
|
|
var type = Type.GetType(typeName) ?? throw new Exception($"Type {typeName} not found.");
|
|
|
|
foreach (var dataElement in componentElement.Value.EnumerateArray())
|
|
{
|
|
var entityID = dataElement.GetProperty(Property.ENTITY_ID).GetInt32();
|
|
var entity = new Entity(entityID, 0);
|
|
|
|
var dataProperty = dataElement.GetProperty(Property.DATA);
|
|
var component = JsonSerializer.Deserialize(dataProperty.GetRawText(), type, options);
|
|
if (component is IComponentData data)
|
|
{
|
|
world.EntityManager.AddComponent(entity, data, type);
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (var systemElement in element.GetProperty(Property.SYSTEMS).EnumerateArray())
|
|
{
|
|
var typeString = systemElement.GetString();
|
|
if (string.IsNullOrEmpty(typeString))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var systemType = Type.GetType(typeString);
|
|
if (systemType == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
world.SystemStorage.AddSystem(systemType);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, WorldNode value, JsonSerializerOptions options)
|
|
{
|
|
writer.WriteObject(() =>
|
|
{
|
|
writer.WriteString(Property.NAME, value.Name);
|
|
writer.WriteArray(Property.ENTITIES, value.World.EntityManager.Entities, entity =>
|
|
{
|
|
if (!entity.IsValid)
|
|
{
|
|
return;
|
|
}
|
|
|
|
writer.WriteObject(() =>
|
|
{
|
|
writer.WriteString(Property.NAME, value.EntityNodeLookup[entity].Name);
|
|
writer.WriteNumber(Property.ID, entity.ID);
|
|
});
|
|
});
|
|
|
|
writer.WriteObject(Property.COMPONENTS, () =>
|
|
{
|
|
foreach (var kvp in value.World.ComponentStorage.ComponentPools)
|
|
{
|
|
var type = kvp.Key.ToType() ?? throw new Exception($"Type {kvp.Key} not found.");
|
|
var typeName = type.AssemblyQualifiedName ?? type.Name;
|
|
|
|
writer.WriteArray(typeName, kvp.Value.Enumerate(), data =>
|
|
{
|
|
writer.WriteObject(() =>
|
|
{
|
|
writer.WriteNumber(Property.ENTITY_ID, data.entity.ID);
|
|
writer.WritePropertyName(Property.DATA);
|
|
JsonSerializer.Serialize(writer, data.component, type, options);
|
|
});
|
|
});
|
|
}
|
|
});
|
|
|
|
writer.WriteArray(Property.SYSTEMS, value.World.SystemStorage.Systems, systemType =>
|
|
{
|
|
writer.WriteStringValue(systemType.AssemblyQualifiedName ?? systemType.Name);
|
|
});
|
|
});
|
|
}
|
|
} |