JSONHandler.cs 3.04 KB
Newer Older
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System.IO;
using System.Collections.Generic;
using SimpleJSON;
using UnityEngine;

namespace dlu
{

public class JSONHandler
{

    private JSONHandler()
    {
        // Note that these are static!
        JSONNode.VectorContainerType = JSONContainerType.Object;
        JSONNode.QuaternionContainerType = JSONContainerType.Object;
    }

    private static JSONHandler _instance = null;

    public static JSONHandler instance
    {
        get
        {
            if (JSONHandler._instance == null)
            {
                JSONHandler._instance = new JSONHandler();
                JSONHandler._instance.RefreshSemanticLabels();
            }
            return JSONHandler._instance;
        }
    }

    private Dictionary<string, GameObject> _sceneDict;
    public Dictionary<string, GameObject> sceneDict
    {
        get
        {
            return this._sceneDict;
        }

        private set
        {
            this._sceneDict = value;
        }
    }


    private SemanticBehaviour[] _semanticBehaviours;
    [HideInInspector] public SemanticBehaviour[] semanticBehaviours
    {
        get
        {
            return this._semanticBehaviours;
        }

        set
        {
            sceneDict = new Dictionary<string, GameObject>();
            foreach (SemanticBehaviour sb in value)
            {
                sceneDict[sb.GetComponent<InstanceIdentifier>().name] = sb.gameObject;
            }
            this._semanticBehaviours = value;
        }
    }

    public void RefreshSemanticLabels()
    {
        JSONHandler._instance.semanticBehaviours = GameObject.FindObjectsOfType<SemanticBehaviour>();
    }

    public JSONArray LoadInstruction(string json)
    {
        return JSON.Parse(File.ReadAllText(json)) as JSONArray;
    }

    public JSONNode GenerateContextJSON()
    {
        string screenShotPath = $"{Application.persistentDataPath}/context.png";
        ScreenCapture.CaptureScreenshot(screenShotPath, 3);

        Dictionary<string, JSONObject> sceneDict = new Dictionary<string, JSONObject>();

        JSONArray sceneArray = new JSONArray();
        foreach (SemanticBehaviour sb in this.semanticBehaviours)
        {
            JSONObject jsonObject = null;
            string name = sb.GetComponent<InstanceIdentifier>().name;

            if (!sceneDict.ContainsKey(name))
            {
                jsonObject = new JSONObject();
                jsonObject.Add("name", name);
                jsonObject.Add("is_a", new JSONArray());
                jsonObject.Add("components", new JSONObject());

                sceneDict[name] = jsonObject;
                sceneArray.Add(sceneDict[name]);
            }

            jsonObject = sceneDict[name];
            jsonObject["is_a"].Add(sb.is_a);

105
            sb.WriteTo(jsonObject);
106
107
108
109
110
111
112
113
114
115
116
117
        }

        JSONObject metaObject = new JSONObject();
        metaObject.Add("image", screenShotPath);
        JSONObject contextObject = new JSONObject();
        contextObject.Add("meta", metaObject);
        contextObject.Add("context", sceneArray);
        return contextObject;
    }
}

}