summaryrefslogtreecommitdiff
path: root/VRCSDK3Worlds/Assets/Udon/Editor/ProgramSources/SerializedUdonProgramAssetEditor.cs
blob: 33ecd07fac8f8bf8ee4949aa35e1de2596d5c707 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEditor;
using VRC.Udon.Common.Interfaces;
using VRC.Udon.ProgramSources;
using VRC.Udon.Serialization.OdinSerializer;

namespace VRC.Udon.Editor.ProgramSources
{
    [CustomEditor(typeof(SerializedUdonProgramAsset))]
    public class SerializedUdonProgramAssetEditor : UnityEditor.Editor
    {
        private SerializedProperty _serializedProgramBytesStringSerializedProperty;
        private SerializedProperty _serializationDataFormatSerializedProperty;

        private void OnEnable()
        {
            _serializedProgramBytesStringSerializedProperty = serializedObject.FindProperty("serializedProgramBytesString");
            _serializationDataFormatSerializedProperty = serializedObject.FindProperty("serializationDataFormat");
        }

        public override void OnInspectorGUI()
        {
            DrawSerializationDebug();
        }

        [Conditional("UDON_DEBUG")]
        private void DrawSerializationDebug()
        {
            EditorGUILayout.LabelField($"DataFormat: {(DataFormat)_serializationDataFormatSerializedProperty.enumValueIndex}");
            
            if(string.IsNullOrEmpty(_serializedProgramBytesStringSerializedProperty.stringValue))
            {
                return;
            }

            if(_serializationDataFormatSerializedProperty.enumValueIndex == (int)DataFormat.JSON)
            {
                using(new EditorGUI.DisabledScope(true))
                {
                    EditorGUILayout.TextArea(System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(_serializedProgramBytesStringSerializedProperty.stringValue)));
                }
            }
            else
            {
                using(new EditorGUI.DisabledScope(true))
                {
                    SerializedUdonProgramAsset serializedUdonProgramAsset = (SerializedUdonProgramAsset)target;
                    IUdonProgram udonProgram = serializedUdonProgramAsset.RetrieveProgram();
                    byte[] serializedBytes = SerializationUtility.SerializeValue(udonProgram, DataFormat.JSON, out List<UnityEngine.Object> _);
                    EditorGUILayout.TextArea(System.Text.Encoding.UTF8.GetString(serializedBytes));
                }
            }
        }
    }
}