Пример #1
0
        public static int ConvertToJSON(ConvertFormatOptions options)
        {
            foreach (var file in options.files)
            {
                if (YarnSpinnerLoader.GetFormatFromFileName(file) == NodeFormat.JSON)
                {
                    MerinoDebug.LogFormat(LoggingLevel.Warning, "Not converting file {0}, because its name implies it's already in JSON format", file);
                    continue;
                }

                ConvertNodesInFile(options, file, "json", (IEnumerable <YarnSpinnerLoader.NodeInfo> nodes) => JsonConvert.SerializeObject(nodes, Formatting.Indented));
            }
            return(0);
        }
Пример #2
0
        public static int ConvertToYarn(ConvertFormatOptions options)
        {
            foreach (var file in options.files)
            {
                if (YarnSpinnerLoader.GetFormatFromFileName(file) == NodeFormat.Text)
                {
                    MerinoDebug.LogFormat(LoggingLevel.Warning, "Not converting file {0}, because its name implies it's already in Yarn format", file);
                    continue;
                }

                ConvertNodesInFile(options, file, "yarn.txt", ConvertNodesToYarnText);
            }
            return(0);
        }
Пример #3
0
        public static void ConvertNodesInFile(ConvertFormatOptions options, string file, string fileExtension, ConvertNodesToText convert)
        {
            //	var d = new Dialogue(null);

            var text = File.ReadAllText(file);

            IEnumerable <YarnSpinnerLoader.NodeInfo> nodes;

            try {
                nodes = YarnSpinnerLoader.GetNodesFromText(text, YarnSpinnerLoader.GetFormatFromFileName(file));
            } catch (FormatException e) {
                MerinoDebug.Log(LoggingLevel.Error, e.Message);
                return;
            }

            var serialisedText = convert(nodes);

            var destinationDirectory = options.outputDirectory;

            if (destinationDirectory == null)
            {
                destinationDirectory = Path.GetDirectoryName(file);
            }

            var fileName = Path.GetFileName(file);

            // ChangeExtension thinks that the file "Foo.yarn.txt" has the extension "txt", so
            // to simplify things, just lop that extension off right away if it's there
            fileName = fileName.Replace(".yarn.txt", "");

            // change the filename's extension
            fileName = Path.ChangeExtension(fileName, fileExtension);

            // figure out where we're writing this file
            var destinationFilePath = Path.Combine(destinationDirectory, fileName);

            File.WriteAllText(destinationFilePath, serialisedText);

            if (options.verbose)
            {
                MerinoDebug.Log(LoggingLevel.Verbose, "Wrote " + destinationFilePath);
            }
        }
Пример #4
0
        public static IList <MerinoTreeElement> GetDataFromFile(TextAsset source, int startID = 1, bool useFastMode = false)
        {
            var treeElements = new List <MerinoTreeElement>();

            if (!useFastMode)
            {
                AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(source)); // TODO: only reload assets that need it? how to do that
                //var format = YarnSpinnerLoader.GetFormatFromFileName(AssetDatabase.GetAssetPath(currentFile)); // TODO: add JSON and ByteCode support?
            }

            // ROOT: create a root node for the file itself
            var fileRoot = new MerinoTreeElement(source.name, 0, startID);

            fileRoot.leafType = MerinoTreeElement.LeafType.File;
            fileRoot.children = new List <TreeElement>();
            treeElements.Add(fileRoot);
            if (MerinoData.FileToNodeID.ContainsKey(source))
            {
                MerinoData.FileToNodeID[source] = startID;
            }
            else
            {
                MerinoData.FileToNodeID.Add(source, startID);
            }

            // load nodes

            // if there's no header sentinel in the text file, then just return an empty list
            if (!source.text.Contains("---"))
            {
                return(treeElements);
            }

            // otherwise, load nodes from file
            var nodes   = YarnSpinnerLoader.GetNodesFromText(source.text, NodeFormat.Text);
            var parents = new Dictionary <MerinoTreeElement, string>();

            foreach (var node in nodes)
            {
                // clean some of the stuff to help prevent file corruption
                string cleanName   = MerinoUtils.CleanYarnField(node.title, true);
                string cleanBody   = MerinoUtils.CleanYarnField(node.body);
                string cleanTags   = MerinoUtils.CleanYarnField(node.tags, true);
                string cleanParent = string.IsNullOrEmpty(node.parent) ? "" : MerinoUtils.CleanYarnField(node.parent, true);

                // write data to the objects
                var newItem = new MerinoTreeElement(cleanName, 0, startID + treeElements.Count);
                newItem.nodeBody     = cleanBody;
                newItem.nodePosition = new Vector2Int(node.position.x, node.position.y);
                newItem.nodeTags     = cleanTags;
                if (string.IsNullOrEmpty(cleanParent) || cleanParent == "Root")
                {
                    newItem.parent         = fileRoot;
                    newItem.cachedParentID = fileRoot.id;
                    fileRoot.children.Add(newItem);
                }
                else
                {
                    parents.Add(newItem, cleanParent);                     // we have to assign parents in a second pass later on, not right now
                }
                treeElements.Add(newItem);
            }

            // second pass: now that all nodes have been created, we can finally assign parents
            foreach (var kvp in parents)
            {
                var parent = treeElements.Find(x => x.name == kvp.Value);
                if (parent == null)
                {
                    MerinoDebug.LogFormat(LoggingLevel.Error, "Merino couldn't assign parent for node {0}: can't find a parent called {1}", kvp.Key.name, kvp.Value);
                }
                else
                {
                    // tell child about it's parent
                    kvp.Key.parent         = parent;
                    kvp.Key.cachedParentID = parent.id;
                    // tell parent about it's child
                    if (kvp.Key.parent.children == null)                     // init parent's list of children if not already initialized
                    {
                        kvp.Key.parent.children = new List <TreeElement>();
                    }
                    kvp.Key.parent.children.Add(kvp.Key);
                }
            }
            return(treeElements);
        }