Esempio n. 1
0
        /// <summary>
        ///  Called to compile a pattern of script files or a single script file.<para/>
        ///  This method compies the files to a temporary directory so they can be encoded in
        ///  <see cref="CatUtils.ShiftJIS"/>.
        /// </summary>
        /// <param name="patternOrFile">The wildcard pattern or file path to compile.</param>
        /// <param name="outputDir">The output directory for the files.</param>
        /// <param name="type">The type of script to compile.</param>
        /// <returns>The number of successfully compiled scripts.</returns>
        ///
        /// <exception cref="ArgumentNullException">
        ///  <paramref name="patternOrFile"/> or <paramref name="outputDir"/> is null.
        /// </exception>
        private int CompileFiles(string patternOrFile, string outputDir, CompileType type)
        {
            if (patternOrFile == null)
            {
                throw new ArgumentNullException(nameof(patternOrFile));
            }
            if (outputDir == null)
            {
                throw new ArgumentNullException(nameof(outputDir));
            }
            string patternName = Path.GetFileName(patternOrFile);
            string patternDir  = Path.GetDirectoryName(patternOrFile);

            if (patternDir.Length == 0)
            {
                patternDir = ".";
            }
            if (!Path.IsPathRooted(patternDir))
            {
                patternDir = Path.GetFullPath(patternDir);
            }

            // Generate a new temporary compile directory
            // Delete the temporary compile directory when done
            using (var tmp = CreateCompileTempDir()) {
                // Copy and re-encode the files to the tmp directory with ShiftJIS encoding.
                string[] txtFiles = Directory.GetFiles(patternDir, patternName);
                for (int i = 0; i < txtFiles.Length; i++)
                {
                    string file    = txtFiles[i];
                    string tmpFile = Path.Combine(tmp, Path.GetFileName(file));
                    CatUtils.ReEncodeToShiftJIS(file, tmpFile);
                }

                // Run the compiler and move the output files
                return(RunCompiler(type, patternName, tmp, outputDir));
            }
        }
Esempio n. 2
0
        /// <summary>
        ///  Decompiles the CST scene script and writes it to the human readable text writer.
        /// </summary>
        /// <param name="writer">The text writer to write the human readable script to.</param>
        private void HumanReadableInternal(TextWriter writer)
        {
            // Header must state the output .cst script file name
            writer.WriteLine($"#{Path.GetFileNameWithoutExtension(FileName)}");
            writer.WriteLine();

            bool isChoice = false;

            List <string> choices = new List <string>();

            StringBuilder name    = new StringBuilder();
            StringBuilder message = new StringBuilder();

            bool firstInput = true;

            void FlushText()
            {
                if (name.Length != 0 || message.Length != 0 || isChoice)
                {
                    if (firstInput)
                    {
                        firstInput = false;
                    }
                    else
                    {
                        writer.WriteLine();
                    }

                    // The first replace prevents duplicate carraige returns
                    name.Replace("\n\r", "\n").Replace("\n", "\n\r");
                    message.Replace("\n\r", "\n").Replace("\n", "\n\r");

                    if (!isChoice && name.Length != 0)
                    {
                        writer.WriteLine(name);
                    }
                    if (message.Length != 0)
                    {
                        writer.WriteLine(message);
                    }
                    if (isChoice)
                    {
                        if (message.Length != 0)
                        {
                            writer.WriteLine();
                        }
                        writer.WriteLine("Choice:");
                        foreach (string choice in choices)
                        {
                            writer.WriteLine($"• {choice}");
                        }
                    }
                }
                message.Clear();
                name.Clear();
                choices.Clear();
                isChoice = false;
            }

            for (int i = 0; i < Count; i++)
            {
                ISceneLine line = Lines[i];
                switch (line.Type)
                {
                case SceneLineType.Command:
                    Match match;
                    if (line.Content == "fselect")
                    {
                        FlushText();
                        isChoice = true;
                    }
                    else if (isChoice && (match = ChoiceRegex.Match(line.Content)).Success)
                    {
                        //writer.WriteLine($"• {match.Groups["text"].Value}");
                        choices.Add(CatUtils.UnescapeString(match.Groups["text"].Value));
                    }
                    break;

                case SceneLineType.Message:
                    message.Append(CatUtils.UnescapeMessage(line.Content, false));
                    break;

                case SceneLineType.Name:
                    name.Append(CatUtils.UnescapeMessage(line.Content, true));
                    break;

                case SceneLineType.Input:
                    FlushText();
                    break;

                case SceneLineType.Page:
                    FlushText();
                    writer.WriteLine();
                    writer.WriteLine();
                    break;
                }
            }

            FlushText();

            writer.Flush();
        }