/// <summary>
    /// Gets a typed array from an array child of the specified node.
    /// </summary>
    public static string[] GetStringArrayChild(this JSONNode parent, string key)
    {
        JSONArray arrayNode = parent.GetArrayChild(key);

        if (arrayNode == null)
        {
            return(null);
        }

        string[] typedArray = new string[arrayNode.Count];
        for (int i = 0; i < typedArray.Length; i++)
        {
            typedArray[i] = arrayNode.GetStringChild(i);
        }
        return(typedArray);
    }
    /// <summary>
    /// Gets a typed array from an array child of the specified node.
    /// </summary>
    public static T[] GetArrayChild <T>(this JSONNode parent, string key) where T : IJsonDeserializable, new()
    {
        JSONArray arrayNode = parent.GetArrayChild(key);

        if (arrayNode == null)
        {
            return(null);
        }

        T[] typedArray = new T[arrayNode.Count];
        for (int i = 0; i < typedArray.Length; i++)
        {
            typedArray[i] = arrayNode.GetObjectChild <T>(i);
        }
        return(typedArray);
    }
Esempio n. 3
0
        /// <summary>
        /// Fills this object's data from a <see cref="JSONNode"/>.
        /// </summary>
        public override void Deserialize(JSONNode node)
        {
            base.Deserialize(node);

            JSONArray characterArrayNode = node.GetArrayChild("character");

            if (characterArrayNode != null)
            {
                Character = characterArrayNode.GetStringChild(0);
            }

            IsBox       = node.GetBoolChild("is_box");
            ObjectPath  = node.GetStringChild("object_path");
            SlideCamera = node.GetBoolChild("slide_camera");
            SpeakerType = (SpeakerType)node.GetIntChild("speaker_type");
            Text        = node.GetObjectChild <NodeText>("text");
        }
        /// <summary>
        /// Deserializes a Dialogue Designer dialogue from a JSON node.
        /// </summary>
        public void Deserialize(JSONNode node)
        {
            Characters    = node.GetStringArrayChild("characters");
            EditorVersion = node.GetStringChild("editor_version");
            FileName      = node.GetStringChild("file_name");
            Languages     = node.GetStringArrayChild("languages");

            List <BaseNode> nodesList      = new List <BaseNode>();
            JSONArray       nodesArrayNode = node.GetArrayChild("nodes");

            foreach (JSONNode arrayElementNode in nodesArrayNode.Values)
            {
                BaseNode newNode  = null;
                string   nodeType = arrayElementNode.GetStringChild("node_type");
                switch (nodeType)
                {
                case "show_message":
                    if (arrayElementNode["choices"] != null)
                    {
                        newNode = CreateInstance <ShowMessageNodeChoice>();
                    }
                    else
                    {
                        newNode = CreateInstance <ShowMessageNodeSimple>();
                    }
                    break;

                case "start":
                    newNode = CreateInstance <StartNode>();
                    break;

                case "condition_branch":
                    newNode = CreateInstance <ConditionBranchNode>();
                    break;

                case "wait":
                    newNode = CreateInstance <WaitNode>();
                    break;

                case "execute":
                    newNode = CreateInstance <ExecuteNode>();
                    break;

                case "random_branch":
                    newNode = CreateInstance <RandomBranchNode>();
                    break;

                case "chance_branch":
                    newNode = CreateInstance <ChanceBranchNode>();
                    break;

                case "repeat":
                    newNode = CreateInstance <RepeatNode>();
                    break;

                case "set_local_variable":
                    JSONNode valueNode = arrayElementNode["value"];
                    if (valueNode is JSONBool)
                    {
                        newNode = CreateInstance <SetLocalVariableBoolNode>();
                    }
                    else if (valueNode is JSONNumber)
                    {
                        newNode = CreateInstance <SetLocalVariableIntNode>();
                    }
                    else
                    {
                        newNode = CreateInstance <SetLocalVariableStringNode>();
                    }
                    break;

                case "comment":
                    // skip
                    break;

                default:
                    Debug.LogErrorFormat("Dialogue deserialization: unknown node type '{0}'", nodeType);
                    break;
                }
                if (newNode != null)
                {
                    newNode.Deserialize(arrayElementNode);
                    nodesList.Add(newNode);
                }
            }
            Nodes = nodesList.ToArray();

            List <Variable> variablesBuffer = new List <Variable>();
            JSONNode        variablesNode   = node["variables"];

            if (variablesNode == null)
            {
                Debug.LogErrorFormat("JSON deserialization: expected 'variables' to be Object.");
            }
            else
            {
                foreach (string key in variablesNode.Keys)
                {
                    Variable newVariable = variablesNode.GetObjectChild <Variable>(key);
                    newVariable.SetName(key);
                    variablesBuffer.Add(newVariable);
                }
            }
            Variables = variablesBuffer.ToArray();

            name = FileName;

            OnAfterDeserialize();
        }
        /// <summary>
        /// Fills this object's data from a <see cref="JSONNode"/>.
        /// </summary>
        public override void Deserialize(JSONNode node)
        {
            base.Deserialize(node);

            Choices = node.GetArrayChild <Choice>("choices");
        }