143 lines
4.6 KiB
C#
143 lines
4.6 KiB
C#
using MessagePack;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
|
|
namespace XED
|
|
{
|
|
[MessagePackObject]
|
|
public class Position
|
|
{
|
|
[Key("PostionX")]
|
|
public float x;
|
|
[Key("PostionY")]
|
|
public float y;
|
|
[Key("PostionZ")]
|
|
public float z;
|
|
}
|
|
[MessagePackObject]
|
|
public class Rotation
|
|
{
|
|
[Key("RotationX")]
|
|
public float x;
|
|
[Key("RotationY")]
|
|
public float y;
|
|
[Key("RotationZ")]
|
|
public float z;
|
|
}
|
|
[MessagePackObject]
|
|
public class TwinObjectData
|
|
{
|
|
[Key("ObjectInfors")]
|
|
public List<ObjectInfor> objectInfors;
|
|
[Key("WallInfors")]
|
|
public List<WallInfor> wallInfors;
|
|
}
|
|
[MessagePackObject]
|
|
public class ObjectInfor
|
|
{
|
|
[Key("Position")]
|
|
public Position position { get; set; }
|
|
|
|
[Key("Rotaion")]
|
|
public Rotation rotation { get; set; }
|
|
|
|
[Key("AssetType")]
|
|
public AssetLabel assetLabel { get; set; }
|
|
[Key("ObjectName")]
|
|
public string address;
|
|
}
|
|
|
|
[MessagePackObject]
|
|
public class WallInfor
|
|
{
|
|
[Key("LinePoints")]
|
|
public Position[] points;
|
|
}
|
|
|
|
public class SaveLoadManager
|
|
{
|
|
public void SaveData(HashSet<TwinObject> twinObjects, string filePath)
|
|
{
|
|
var twinData = new TwinObjectData();
|
|
var facilitys = new List<ObjectInfor>();
|
|
var walls = new List<WallInfor>();
|
|
|
|
foreach (TwinObject twinObject in twinObjects)
|
|
{
|
|
if (twinObject.GetType() == typeof(WallGroup))
|
|
{
|
|
var wallgroup = twinObject as WallGroup;
|
|
foreach (var wallLine in wallgroup.groupWalls)
|
|
{
|
|
var leftPoint = wallLine.leftCenterPoint;
|
|
var rightPoint = wallLine.rightCenterPoint;
|
|
|
|
Position[] points = new Position[2];
|
|
var leftPos = new Position { x = leftPoint.position.x, y = leftPoint.position.y, z = leftPoint.position.z };
|
|
var rightPos = new Position { x = rightPoint.position.x, y = rightPoint.position.y, z = rightPoint.position.z };
|
|
|
|
points[0] = leftPos;
|
|
points[1] = rightPos;
|
|
|
|
WallInfor wallinfor = new WallInfor { points = points };
|
|
walls.Add(wallinfor);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var position = ObjectPos(twinObject);
|
|
var rotation = ObjectRot(twinObject);
|
|
var objname = twinObject.name.Replace("(Clone)", "");
|
|
ObjectInfor facilityInfor = new ObjectInfor
|
|
{
|
|
position = position,
|
|
rotation = rotation,
|
|
assetLabel = twinObject.assetLabel,
|
|
address = twinObject.metaData.address
|
|
};
|
|
facilitys.Add(facilityInfor);
|
|
}
|
|
}
|
|
twinData.objectInfors = facilitys;
|
|
twinData.wallInfors = walls;
|
|
//Serialize<List<ObjectInfor>>(facilitys, twinFilePath);
|
|
//Serialize<List<WallInfor>>(walls, wallFilePath);
|
|
Serialize<TwinObjectData>(twinData, filePath);
|
|
}
|
|
|
|
private Position ObjectPos(TwinObject twin)
|
|
{
|
|
Position pos = new Position();
|
|
pos.x = twin.transform.position.x;
|
|
pos.y = twin.transform.position.y;
|
|
pos.z = twin.transform.position.z;
|
|
|
|
return pos;
|
|
}
|
|
private Rotation ObjectRot(TwinObject twin)
|
|
{
|
|
Rotation rot = new Rotation();
|
|
rot.x = twin.transform.eulerAngles.x;
|
|
rot.y = twin.transform.eulerAngles.y;
|
|
rot.z = twin.transform.eulerAngles.z;
|
|
|
|
return rot;
|
|
}
|
|
|
|
public void Serialize<T>(T type, string filepath)
|
|
{
|
|
var lz4Option = MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4Block);
|
|
var bytes = MessagePackSerializer.Serialize<T>(type, lz4Option);
|
|
File.WriteAllBytes(filepath, bytes);
|
|
}
|
|
public T Deserialize<T>(string filepath)
|
|
{
|
|
var lz4Option = MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4Block);
|
|
var readByte = File.ReadAllBytes(filepath);
|
|
var deserailze = MessagePackSerializer.Deserialize<T>(readByte, lz4Option);
|
|
|
|
return deserailze;
|
|
}
|
|
}
|
|
}
|