예제 #1
0
        private void ImportYarn(AssetImportContext ctx)
        {
            var    sourceText = File.ReadAllText(ctx.assetPath);
            string fileName   = System.IO.Path.GetFileNameWithoutExtension(ctx.assetPath);

            var text = new TextAsset(File.ReadAllText(ctx.assetPath));

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

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

            parseErrorMessage = null;

            try
            {
                // Compile the source code into a compiled Yarn program (or
                // generate a parse error)
                var compilationJob = CompilationJob.CreateFromString(fileName, sourceText, null);
                compilationJob.CompilationType = CompilationJob.Type.StringsOnly;

                var result = Yarn.Compiler.Compiler.Compile(compilationJob);

                LastImportHadImplicitStringIDs = result.ContainsImplicitStringTags;
                LastImportHadAnyStrings        = result.StringTable.Count > 0;

                stringTable          = result.StringTable;
                compiledProgram      = result.Program;
                isSuccessfullyParsed = true;
                parseErrorMessage    = string.Empty;
            }
            catch (Yarn.Compiler.ParseException e)
            {
                isSuccessfullyParsed = false;
                parseErrorMessage    = e.Message;
                ctx.LogImportError($"Error importing {ctx.assetPath}: {e.Message}");
                return;
            }
        }
예제 #2
0
        public override void Diagnose(Yarn.Program program)
        {
            // In each node, find all reads and writes to variables
            foreach (var nodeInfo in program.Nodes)
            {
                var nodeName = nodeInfo.Key;
                var theNode  = nodeInfo.Value;

                foreach (var instruction in theNode.Instructions)
                {
                    switch (instruction.Opcode)
                    {
                    case OpCode.PushVariable:
                    case OpCode.StoreVariable:
                        variables.Add(instruction.Operands[0].StringValue);
                        break;
                    }
                }
            }
        }
예제 #3
0
 public abstract void Diagnose(Yarn.Program program);
예제 #4
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();
            }
        }