예제 #1
0
        /// Adds a tagged string table from the yarn asset depending on the variable "textLanguage"
        public void AddStringTable(YarnProgram yarnScript)
        {
            var textToLoad = new TextAsset();

            if (yarnScript.localizations != null || yarnScript.localizations.Length > 0)
            {
                textToLoad = Array.Find(yarnScript.localizations, element => element.languageName == textLanguage)?.text;
            }
            if (textToLoad == null || string.IsNullOrEmpty(textToLoad.text))
            {
                textToLoad = yarnScript.baseLocalisationStringTable;
            }

            // Use the invariant culture when parsing the CSV
            var configuration = new CsvHelper.Configuration.Configuration(
                System.Globalization.CultureInfo.InvariantCulture
                );

            using (var reader = new System.IO.StringReader(textToLoad.text))
                using (var csv = new CsvReader(reader, configuration)) {
                    csv.Read();
                    csv.ReadHeader();

                    while (csv.Read())
                    {
                        strings.Add(csv.GetField("id"), csv.GetField("text"));
                    }
                }
        }
예제 #2
0
 public void AddTrackedProgram(YarnProgram program)
 {
     // No-op if we already have this in the list
     if (_trackedPrograms.Contains(program))
     {
         return;
     }
     _trackedPrograms.Add(program);
 }
예제 #3
0
        private void UpgradeProgram(YarnProgram script, UpgradeType upgradeMode)
        {
            // Get the path for this asset
            var    path     = AssetDatabase.GetAssetPath(script);
            string fileName = Path.GetFileName(path);

            // Get the text from the path
            var originalText = File.ReadAllText(path);

            // Run it through the upgrader
            string upgradedText;

            try
            {
                upgradedText = LanguageUpgrader.UpgradeScript(
                    originalText,
                    fileName,
                    upgradeMode,
                    out var replacements);

                if (replacements.Count() == 0)
                {
                    Debug.Log($"No upgrades required for {fileName}", script);

                    // Exit here - we won't need to modify the file on
                    // disk, so we can save some effort by returning at
                    // this point
                    return;
                }

                // Log some diagnostics about what changes we're making
                foreach (var replacement in replacements)
                {
                    Debug.Log($@"{fileName}:{replacement.StartLine}: ""{replacement.OriginalText}"" -> ""{replacement.ReplacementText}""", script);
                }
            }
            catch (System.Exception e)
            {
                Debug.LogError($"Failed to upgrade {fileName}: {e.GetType()} {e.Message}", script);
                return;
            }

            // Save the text back to disk
            File.WriteAllText(path, upgradedText);

            // Re-import the asset
            AssetDatabase.ImportAsset(path);
        }
예제 #4
0
        /// Adds a tagged string table from the yarn asset depending on the variable "textLanguage"
        public void AddStringTable(YarnProgram yarnScript)
        {
            var textToLoad = new TextAsset();

            if (yarnScript.localizations != null || yarnScript.localizations.Length > 0)
            {
                textToLoad = Array.Find(yarnScript.localizations, element => element.languageName == textLanguage)?.text;
            }
            if (textToLoad == null || string.IsNullOrEmpty(textToLoad.text))
            {
                textToLoad = yarnScript.baseLocalisationStringTable;
            }

            using (var reader = new System.IO.StringReader(textToLoad.text))
                using (var csv = new CsvReader(reader)) {
                    csv.Read();
                    csv.ReadHeader();

                    while (csv.Read())
                    {
                        strings.Add(csv.GetField("id"), csv.GetField("text"));
                    }
                }
        }
예제 #5
0
 /// Adds a program, and all of its nodes
 internal void AddProgram(YarnProgram scriptToLoad)
 {
     this.dialogue.AddProgram(scriptToLoad.GetProgram());
 }
예제 #6
0
 /// Adds a program and its base localisation string table
 internal void Add(YarnProgram scriptToLoad)
 {
     AddProgram(scriptToLoad);
     AddStringTable(scriptToLoad);
 }
예제 #7
0
 public void RemoveTrackedProgram(YarnProgram programContainer)
 {
     _trackedPrograms.Remove(programContainer);
 }
예제 #8
0
 /// Adds a program and its base localisation string table
 internal void Add(YarnProgram scriptToLoad)
 {
     AddProgram(scriptToLoad);
     AddStringTable(scriptToLoad.baseLocalisationStringTable);
 }
예제 #9
0
        private void ImportYarn(AssetImportContext ctx)
        {
            var    sourceText = File.ReadAllText(ctx.assetPath);
            string fileName   = System.IO.Path.GetFileNameWithoutExtension(ctx.assetPath);

            Yarn.Program compiledProgram = null;
            IDictionary <string, Yarn.Compiler.StringInfo> stringTable = null;

            compilationErrorMessage = null;

            try
            {
                // Compile the source code into a compiled Yarn program (or
                // generate a parse error)
                compilationStatus       = Yarn.Compiler.Compiler.CompileString(sourceText, fileName, out compiledProgram, out stringTable);
                isSuccesfullyCompiled   = true;
                compilationErrorMessage = string.Empty;
            }
            catch (Yarn.Compiler.ParseException e)
            {
                isSuccesfullyCompiled   = false;
                compilationErrorMessage = e.Message;
                ctx.LogImportError(e.Message);
            }

            // Create a container for storing the bytes
            if (programContainer == null)
            {
                programContainer = ScriptableObject.CreateInstance <YarnProgram>();
            }

            byte[] compiledBytes = null;

            if (compiledProgram != null)
            {
                using (var memoryStream = new MemoryStream())
                    using (var outputStream = new Google.Protobuf.CodedOutputStream(memoryStream))
                    {
                        // Serialize the compiled program to memory
                        compiledProgram.WriteTo(outputStream);
                        outputStream.Flush();

                        compiledBytes = memoryStream.ToArray();
                    }
            }


            programContainer.compiledProgram = compiledBytes;

            // Add this container to the imported asset; it will be
            // what the user interacts with in Unity
            ctx.AddObjectToAsset("Program", programContainer, YarnEditorUtility.GetYarnDocumentIconTexture());
            ctx.SetMainObject(programContainer);

            if (stringTable?.Count > 0)
            {
                var lines = stringTable.Select(x => new StringTableEntry
                {
                    ID         = x.Key,
                    Language   = baseLanguageID,
                    Text       = x.Value.text,
                    File       = x.Value.fileName,
                    Node       = x.Value.nodeName,
                    LineNumber = x.Value.lineNumber.ToString(),
                    Lock       = GetHashString(x.Value.text, 8),
                }).OrderBy(entry => int.Parse(entry.LineNumber));

                var stringTableCSV = StringTableEntry.CreateCSV(lines);

                var textAsset = new TextAsset(stringTableCSV);
                textAsset.name = $"{fileName} ({baseLanguageID})";

                ctx.AddObjectToAsset("Strings", textAsset);

                programContainer.baseLocalizationId = baseLanguageID;
                baseLanguage = textAsset;
                programContainer.localizations      = localizations.Append(new YarnProgram.YarnTranslation(baseLanguageID, textAsset)).ToArray();
                programContainer.baseLocalizationId = baseLanguageID;

                stringIDs = lines.Select(l => l.ID).ToArray();
            }
        }