/// <summary>
        /// Saves objects to file
        /// </summary>
        /// <param name="objects">
        /// List of objects to save
        /// </param>
        public Task Save(Dictionary <TKey, T> objects)
        {
            List <T> list = ListFromDictionary(objects);
            string   data = _stringConverter.ConvertToString(list);

            return(_stringPersistence.SaveAsync(_fileName, data));
        }
Exemple #2
0
        /// <summary>
        /// 分析对话内容
        /// </summary>
        /// <param name="runtime">脚本运行环境</param>
        /// <param name="raw">原始对话内容</param>
        /// <param name="language">语言(不提供则使用脚本运行环境的语言)</param>
        /// <returns></returns>
        public static (List <IDialogueItem> Content, bool NoWait, bool NoClear) CreateDialogueContent(ScriptRuntime runtime, IStringConverter raw, string language = null)
        {
            var result  = new List <IDialogueItem>();
            var content = new StringBuilder();
            var style   = new StyleList();
            var noWait  = false;
            var noClear = false;
            var i       = -1;

            language = language ?? runtime.ActiveLanguage;
            var data = raw.ConvertToString(language);

            if (data.StartsWith("[noclear]", StringComparison.OrdinalIgnoreCase))
            {
                noClear = true;
                i       = 8;
            }

            void FlushCache()
            {
                result.Add(style.Combine(content.ToString()));
                content.Clear();
            }

            void ApplyDefault(string command)
            {
                content.Append($"[{command}]");
            }

            for (; ++i < data.Length;)
            {
                switch (data[i])
                {
                // 转义字符
                case '\\' when i < data.Length - 1:
                    switch (data[i + 1])
                    {
                    case 'n':
                        content.Append('\n');
                        break;

                    case 't':
                        content.Append('\t');
                        break;

                    case 's':
                        content.Append(' ');
                        break;

                    default:
                        content.Append(data[i + 1]);
                        break;
                    }
                    ++i;
                    break;

                // 最后一个字符是[时的容错处理
                case '[' when i == data.Length - 1:
                    content.Append('[');
                    break;

                // 指令
                case '[':
                    var endIndex = data.IndexOf(']', i + 1);
                    if (endIndex <= i)       // 容错处理
                    {
                        content.Append('[');
                        break;
                    }
                    var command = data.Substring(i + 1, endIndex - i - 1);
                    i = endIndex;
                    if (string.IsNullOrEmpty(command))       // 空指令不处理
                    {
                        content.Append("[]");
                    }
                    else if (command.StartsWith("@#"))         // 常量
                    {
                        var constantName = command.Substring(2);
                        if (string.IsNullOrEmpty(constantName))
                        {
                            throw new ArgumentException($"Unable to create dialog command: missing constant name at {i}");
                        }
                        var constant = runtime.ActiveScope?.FindVariable(constantName, true, VariableSearchMode.OnlyConstant);
                        if (constant == null)
                        {
                            ApplyDefault(command);
                        }
                        else
                        {
                            content.Append(constant.Value is IStringConverter stringConverter ? stringConverter.ConvertToString() : constant.Value.ToString());
                        }
                    }
                    else if (command.StartsWith("@"))         // 变量
                    {
                        var variableName = command.Substring(1);
                        if (string.IsNullOrEmpty(variableName))
                        {
                            throw new ArgumentException($"Unable to create dialog command: missing variable name at {i}");
                        }
                        var variable = runtime.ActiveScope?.FindVariable(variableName, true, VariableSearchMode.All);
                        if (variable == null)
                        {
                            ApplyDefault(command);
                        }
                        else
                        {
                            content.Append(variable.Value is IStringConverter stringConverter ? stringConverter.ConvertToString() : variable.Value.ToString());
                        }
                    }
                    else
                    {
                        var commandContent = command.ToLower().Trim();
                        if (commandContent.StartsWith("size"))       // 字号
                        {
                            var parameter = ExtractParameter(commandContent);
                            var relative  = parameter.Value.Value.StartsWith("+") || parameter.Value.Value.StartsWith("-");
                            if (parameter.HasValue && float.TryParse(parameter.Value.Value, out var number))
                            {
                                FlushCache();
                                style.Size.AddLast((Mathf.RoundToInt(number), relative));
                            }
                            else
                            {
                                ApplyDefault(command);
                            }
                        }
                        else if (commandContent == "/size")         // 取消字号
                        {
                            if (style.Size.Any())
                            {
                                FlushCache();
                                style.Size.RemoveLast();
                            }
                            else
                            {
                                ApplyDefault(command);
                            }
                        }
                        else if (commandContent.StartsWith("color"))         // 颜色
                        {
                            var parameter = ExtractParameter(commandContent);
                            if (parameter.HasValue)
                            {
                                FlushCache();
                                style.Color.AddLast(parameter.Value.Value);
                            }
                            else
                            {
                                ApplyDefault(command);
                            }
                        }
                        else if (commandContent == "/color")         // 取消颜色
                        {
                            if (style.Color.Any())
                            {
                                FlushCache();
                                style.Color.RemoveLast();
                            }
                            else
                            {
                                ApplyDefault(command);
                            }
                        }
                        else if (commandContent.StartsWith("pause"))         // 暂停
                        {
                            if (commandContent == "pause")
                            {
                                FlushCache();
                                result.Add(new PauseDialogueItem {
                                    Time = null
                                });
                            }
                            else
                            {
                                var parameter = ExtractParameter(commandContent);
                                if (parameter.HasValue && float.TryParse(parameter.Value.Value, out var number))
                                {
                                    FlushCache();
                                    result.Add(new PauseDialogueItem {
                                        Time = number
                                    });
                                    break;
                                }
                                ApplyDefault(command);
                            }
                        }
                        else
                        {
                            switch (commandContent)
                            {
                            case "clear":     // 清空
                                FlushCache();
                                result.Add(new ClearDialogueItem());
                                break;

                            case "nowait":
                                if (i == data.Length - 1)
                                {
                                    noWait = true;
                                }
                                break;

                            default:
                                switch (command)
                                {
                                case "b":         // 粗体
                                    FlushCache();
                                    style.Bold = true;
                                    break;

                                case "/b":         // 取消粗体
                                    FlushCache();
                                    style.Bold = false;
                                    break;

                                case "i":         // 斜体
                                    FlushCache();
                                    style.Italic = true;
                                    break;

                                case "/i":         // 取消斜体
                                    FlushCache();
                                    style.Italic = false;
                                    break;

                                case "s":         // 删除线
                                    FlushCache();
                                    style.Strikethrough = true;
                                    break;

                                case "/s":         // 取消删除线
                                    FlushCache();
                                    style.Strikethrough = false;
                                    break;

                                case "u":         // 下划线
                                    FlushCache();
                                    style.Underline = true;
                                    break;

                                case "/u":         // 取消下划线
                                    FlushCache();
                                    style.Underline = false;
                                    break;

                                default:
                                    ApplyDefault(command);
                                    break;
                                }
                                break;
                            }
                        }
                    }
                    break;

                default:
                    content.Append(data[i]);
                    break;
                }
            }
            if (content.Length > 0)
            {
                result.Add(style.Combine(content.ToString()));
            }
            return(result, noWait, noClear);
        }
Exemple #3
0
 /// <inheritdoc />
 public bool EqualsWith(SerializableValue target, string language = TranslationManager.DefaultLanguage)
 {
     return(target is CharacterValue characterValue &&
            characterValue.name.ConvertToString(language) == name.ConvertToString(language) &&
            characterValue.avatar.ConvertToString(language) == avatar.ConvertToString(language));
 }
Exemple #4
0
        /// <summary>
        /// Saves objects to file
        /// </summary>
        /// <param name="objects">
        /// List of objects to save
        /// </param>
        public Task Save(List <T> objects)
        {
            string data = _stringConverter.ConvertToString(objects);

            return(_stringPersistence.SaveAsync(_fileName, data));
        }
Exemple #5
0
 /// <inheritdoc />
 public string ConvertToString(string language = TranslationManager.DefaultLanguage)
 {
     return(name.ConvertToString(language));
 }