Ejemplo n.º 1
0
    public static void ParseTest()
    {
        Tsvio     tsvio    = new Tsvio(Tsvio.Mode.TreeText);
        TextAsset txt      = Resources.Load("Tsv/TreeTextSample", typeof(TextAsset)) as TextAsset;
        string    tsv_text = Encoding.UTF8.GetString(txt.bytes);

        tsvio.debugMode    = false;
        tsvio.debugParsing = true;

        // Parse the tree structured tsv.
        if (tsvio.Parse(tsv_text))
        {
            //#######################################################
            //
            // GetAttributes sample
            //
            //#######################################################

            // Get the tsv element's attribute by selector string.
            string[] res = tsvio.GetAttributes("TreeTextSample > String sample > Single line");

            if (res != null)
            {
                // Attributes was given as string[].
                Debug.Log("attributes: " + string.Join(", ", res));
            }

            // If element have the same name more than one target, you can select one by index.
            res = tsvio.GetAttributes("TreeTextSample > String sample > Single line[1]");

            if (res != null)
            {
                // Attributes was given as string[].
                Debug.Log("attributes: " + string.Join(", ", res));
            }



            //#######################################################
            //
            // GetElement sample
            //
            //#######################################################

            // This case is to get the root element.
            TsvElement elem = tsvio.GetElement("TreeTextSample");

            if (elem != null)
            {
                // Access attribute in the element.
                Debug.Log("Element attributes: " + string.Join(", ", elem.attr));
            }


            // Get the elements having multi line string.
            elem = tsvio.GetElement("TreeTextSample > String sample > Multiple");

            if (elem != null)
            {
                // Multi-line string is set on other buffer "multistr".
                Debug.Log("Element multiline: " + elem.multistr);
            }


            // Be able to get the child elements.
            elem = tsvio.GetElement("TreeTextSample > String sample");

            if (elem != null)
            {
                // Caution! If you accessed elements directly, Then must be added to index of the List. like [0]
                // This element is same as selector of "TreeTextSample > String sample > Single line[0]"
                Debug.Log("Element array access: " + string.Join(", ", elem.childs["Single line"][0].attr));

                // This element is same as selector of "TreeTextSample > String sample > Single line[1]"
                Debug.Log("Element array access: " + string.Join(", ", elem.childs["Single line"][1].attr));
            }


            // And, You can access directly by the indexer.
            // This sample is same to GetElement("TreeTextSample > String sample")
            elem = tsvio.rootObject["TreeTextSample > String sample"];

            if (elem != null)
            {
                // This element is same as selector of "TreeTextSample > String sample > Single line[0]"
                Debug.Log("Direct access: " + string.Join(", ", elem.childs["Single line"][0].attr));

                // This element is same as selector of "TreeTextSample > String sample > Single line[1]"
                Debug.Log("Direct access: " + string.Join(", ", elem.childs["Single line"][1].attr));
            }

            //#######################################################
            //
            // Ways of add elements, and SaveTo sample
            //
            //#######################################################

            // Add element to current tree.
            string[]   attributes = new string[] { "This", "is", "added", "element." };
            TsvElement addElem    = Tsvio.CreateElement(attributes);


            // Child element add to the parent element.
            addElem.AddTo("AddToSample", tsvio.GetElement("TreeTextSample"));

            // Child element add from the parent element.
            tsvio.GetElement("TreeTextSample").AddChild("AddChildSample", addElem);


            // Get root object. ( Elements list dictionary. )
            TsvElement root = tsvio.rootObject;

            // Make new tree in root.
            root["NewHeader(only header)"]     = Tsvio.CreateElement();                                       // Only header
            root["NewHeader(with attributes)"] = Tsvio.CreateElement(new string[] { "Attribute", "Sample" }); // With attributes

            // Make new tree with new levels.
            root["AAAAA>BBBBB>CCCCC>DDDDD>EEEEE"] = Tsvio.CreateElement("This is multi line string.\nNew line test.\nNew level making sample.");

            string path = Application.persistentDataPath + "/test.tsv";
            tsvio.SaveTo(path);

            Debug.Log("Sample TSV saved to " + path);
            Debug.Log("Check out the written tsv!");

            // If you want get only text from the TreeText,
            // then call ToString () and be use the returned value.
            string treeToStr = tsvio.ToString();              // or tsvio.TreeToString ()

            // Your customize (encrypt etc...) and your save method add to here.
        }
    }
Ejemplo n.º 2
0
    //=============================================================

    /**
     *	@brief	TreeText i/o example
     */
    //=============================================================
    void TreeTextExample()
    {
        // Please check the "Tsv/TreeTextExample.tsv".
        Tsvio     tsvio    = new Tsvio(Tsvio.Mode.TreeText);
        string    path     = "Tsv/TreeTextExample";
        TextAsset txt      = Resources.Load(path, typeof(TextAsset)) as TextAsset;
        string    tsv_text = Encoding.UTF8.GetString(txt.bytes);

        parseResult = new StringBuilder();

        // If you want to see the console messages, then set to true below.
        tsvio.debugMode = false;

        // Set log function callback.
        tsvio.DelegateLog = AppendLog;

        AppendLog(
            "***************************\n" +
            "The loaded elements tree made for debugging.\n" +
            "Please open the example tsv file from " + path + ".\n" +
            "and, Compare tree in the hierarchy tab.\n" +
            "***************************\n");

        // For example project.
        if (debugTree != null)
        {
            debugTree.Clear();
        }
        else
        {
            debugTree = new TsvElementDebug();
        }

        // Parse the tree structured tsv string.
        if (tsvio.Parse(tsv_text))
        {
            // Set Element tree visualize debug by the GameObjects tree.
            debugTree.Parse(tsvio.rootObject, "[Tsvio] Root", null);


            //#######################################################
            //
            // GetAttributes example
            //
            //#######################################################

            // Get the tsv element's attribute by selector string.
            string[] res = tsvio.GetAttributes("TreeTextExample > String example > Single line");

            if (res != null)
            {
                // Attributes was given as string[].
                AppendLog("attributes: " + string.Join(", ", res));
            }

            // If element have the same name more than one target, you can select one by index.
            res = tsvio.GetAttributes("TreeTextExample > String example > Single line[1]");

            if (res != null)
            {
                // Attributes was given as string[].
                AppendLog("attributes: " + string.Join(", ", res));
            }



            //#######################################################
            //
            // GetElement example
            //
            //#######################################################

            // This case is to get the first level element.
            TsvElement elem = tsvio.GetElement("TreeTextExample");

            if (elem != null)
            {
                // Access attribute in the element.
                AppendLog("Element attributes: " + string.Join(", ", elem.attr));
            }


            // Get the elements having multi line string.
            elem = tsvio.GetElement("TreeTextExample > String example > Multiple");

            if (elem != null)
            {
                // Multi-line string is set on other buffer "multistr".
                AppendLog("Element multiline: " + elem.multistr);
            }


            // Be able to get the child elements.
            elem = tsvio.GetElement("TreeTextExample > String example");

            if (elem != null)
            {
                // Caution! If you accessed elements directly, Then must be added to index of the List. like [0]
                // This element is same as selector of "TreeTextExample > String example > Single line[0]"
                AppendLog("Element array access: " + string.Join(", ", elem.childs["Single line"][0].attr));

                // This element is same as selector of "TreeTextExample > String example > Single line[1]"
                AppendLog("Element array access: " + string.Join(", ", elem.childs["Single line"][1].attr));
            }


            // And, You can access directly by the TsvElements indexer.
            // This example is same to GetElement("TreeTextExample > String example")
            elem = tsvio.rootObject["TreeTextExample > String example"];

            if (elem != null)
            {
                // This element is same as selector of "TreeTextExample > String example > Single line[0]"
                AppendLog("Direct access: " + string.Join(", ", elem.childs["Single line"][0].attr));

                // This element is same as selector of "TreeTextExample > String example > Single line[1]"
                AppendLog("Direct access: " + string.Join(", ", elem.childs["Single line"][1].attr));
            }

            //#######################################################
            //
            // Ways of add elements, and SaveTo example
            //
            //#######################################################

            // Add element to current tree.
            string[]   attributes = new string[] { "This", "is", "added", "element." };
            TsvElement addElem    = Tsvio.CreateElement(attributes);


            // Child element add to the parent element. ( Call from the child element to the parent element. )
            // This time use the loaded tsv continued.
            addElem.AddTo("AddToExample", tsvio.GetElement("TreeTextExample"));
            //
            // [Root]
            // |
            // +-- TreeTextExample
            //     |
            //     +-- AddToExample
            //

            // Child element add from the parent element. ( Call from the parent element to the child element. )
            tsvio.GetElement("TreeTextExample").AddChild("AddChildExample", addElem);
            //
            // [Root]
            // |
            // +-- TreeTextExample
            //     |
            //     +-- AddToExample
            //     |
            //     +-- AddChildExample
            //

            // Get root object. ( Elements list dictionary. )
            TsvElement root = tsvio.rootObject;


            // Make new tree in root.
            root["NewHeader(only header)"]     = Tsvio.CreateElement();                                            // Only header
            root["NewHeader(with attributes)"] = Tsvio.CreateElement(new string[] { "Attribute\t\n", "Example" }); // With attributes


            // Make new tree levels.
            // If not found an element of a key in the selector, Tsvio will create there keys.
            root["AAAAA > BBBBB > CCCCC > DDDDD > EEEEE"] = Tsvio.CreateElement("This is multi line string.\nNew level making example.");

            path = Application.persistentDataPath + "/TreeWriteExample.tsv";
            tsvio.SaveTo(path);

            AppendLog("Example TSV saved to " + path + "\nPlease check the written tsv!");


            // If you want get only text from the TreeText,
            // then call ToString () and be use the returned value.
            //string treeToStr = tsvio.ToString (); // or tsvio.TreeToString ()

            // Your customize (encrypt etc...) and your save method add to here.
            //treeToStr = YourEncrypt (treeToStr);
            //YourSave (treeToStr);
        }
    }