Files
lity3d/EditorCS/SceneIO.cs
T

57 lines
2.1 KiB
C#

using System.Globalization;
namespace Lity3DEditor;
public static class SceneIO
{
public static List<SceneNode> Load(string path)
{
var nodes = new List<SceneNode>();
using var reader = new StreamReader(path);
string? header = reader.ReadLine();
if (header == null || !header.StartsWith("LITY3D_SCENE"))
throw new InvalidDataException("不是有效的 Lity3D 场景文件");
int lineIdx = 1;
string? line;
while ((line = reader.ReadLine()) != null)
{
if (string.IsNullOrWhiteSpace(line)) continue;
var parts = line.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 13) continue;
var node = new SceneNode
{
Name = parts[0],
ParentIndex = int.Parse(parts[1]),
Mesh = Enum.Parse<MeshType>(parts[2], true),
MeshPath = parts[3],
PosX = float.Parse(parts[4], CultureInfo.InvariantCulture),
PosY = float.Parse(parts[5], CultureInfo.InvariantCulture),
PosZ = float.Parse(parts[6], CultureInfo.InvariantCulture),
RotX = float.Parse(parts[7], CultureInfo.InvariantCulture),
RotY = float.Parse(parts[8], CultureInfo.InvariantCulture),
RotZ = float.Parse(parts[9], CultureInfo.InvariantCulture),
ScaleX = float.Parse(parts[10], CultureInfo.InvariantCulture),
ScaleY = float.Parse(parts[11], CultureInfo.InvariantCulture),
ScaleZ = float.Parse(parts[12], CultureInfo.InvariantCulture),
LineIndex = lineIdx
};
nodes.Add(node);
lineIdx++;
}
return nodes;
}
public static void Save(string path, List<SceneNode> nodes)
{
using var writer = new StreamWriter(path);
writer.WriteLine("LITY3D_SCENE 1");
foreach (var node in nodes)
{
int parent = node.ParentIndex;
writer.WriteLine(node.ToSceneLine(parent));
}
}
}