using System;
using System.Runtime.Serialization.Json;
using System.Text;
using UnityEngine;
using VketCloudGUITools.Serialization;
namespace VketCloudGUITools.Runtime
{
///
/// CanvasJSONのGUIItemの読み取り失敗時に生成される
/// エラー確認を目的としており、これのエクスポート対応はしない
/// 結果的に、インポートとエクスポートを繰り返すとUnknownはデータ喪失して消えるが、仕様とする
///
public class VCUnknown : MonoBehaviour, IVCGUIItem
{
public bool isComment = false;
[TextArea(minLines: 5, maxLines: 999)]
public string json = string.Empty;
public bool IsValid()
{
return false;
}
public bool Visible
{
get;
set;
}
public bool Show
{
get;
set;
}
public Vector2Int Pos
{
get;
set;
}
public Vector2Int Size
{
get;
set;
}
public int Z
{
get;
set;
}
public Vector3Int Pos3D
{
get => new Vector3Int(Pos.x, Pos.y, -Z);
set
{
if (Pos.x != value.x || Pos.y != value.y)
{
Pos = new Vector2Int(value.x, value.y);
}
if (Z != value.z)
{
Z = value.z;
}
}
}
public void UpdateVisible()
{
}
public void OnParentLayerShowChanged()
{
}
public void ToggleChange(string name)
{
}
public void ToggleDefault(string name)
{
}
///
/// UnityInspectorで可視化できるようにJSON化
///
///
public void ToJson(VCContentDef guiContent, ILogger logger)
{
// エディタ以外で使用する場合にエラーとなるため処理をスキップする
if (Application.platform != RuntimePlatform.WindowsEditor
|| Application.platform != RuntimePlatform.OSXEditor
|| Application.platform != RuntimePlatform.LinuxEditor)
{
return;
}
try
{
using (var memoryStream = new System.IO.MemoryStream())
{
var knownTypes = new Type[] { typeof(System.Int32[]) };
var settings = new DataContractJsonSerializerSettings()
{
UseSimpleDictionaryFormat = true,
KnownTypes = knownTypes,
};
var serializer = new DataContractJsonSerializer(typeof(VCContentDef), settings);
using (var writer = JsonReaderWriterFactory.CreateJsonWriter(memoryStream, Encoding.UTF8, false, true))
{
serializer.WriteObject(writer, guiContent);
writer.Flush();
}
using (var streamReader = new System.IO.StreamReader(memoryStream))
{
memoryStream.Position = 0;
json = streamReader.ReadToEnd();
}
}
}
catch (System.Exception e)
{
// もともと読めないJSONデータをエラー分析用に可読化する関数なので、あえてExceptionは握りつぶしてドライバを止めない
logger.LogWarning(nameof(VCUnknown), $"JSON serialize failed.\n{e}");
}
}
public void OnResolveReferences(VCLayerList parentLayer)
{
}
}
}