示例#1
0
    void ParseDatabaseText(string str)
    {
        str = str + @"[TheGameBroke]
You Died because the game developers didn’t write a scenario to handle that last choice.
<>I guess I’ll start over![Start]
;";
        currentDatabase.Clear();
        var eventRegex      = new Regex(@"\[[\s\S]+?(?=;)", RegexOptions.Multiline);
        var keyAndTextRegex = new Regex(@"\[(?<key>.+?)\] ?(?<flags>{[\w,. !:/]+})?(\r\n)?(?<text>[\s\S]+?)(?=<)", RegexOptions.Multiline);
        var optionRegex     = new Regex("(?<flags><[!,\\w ]*>) ?(?<text>[\\s\\S]+?\\]) *(?:\n|\r|\r\n)", RegexOptions.Multiline);
        var targetRegex     = new Regex(@"\[(?<target>.+?)\]", RegexOptions.Multiline);
        var choiceTextRegex = new Regex(@"^([\s\S]+?(?=\[))", RegexOptions.Multiline);
        var flagsRegex      = new Regex(@"!?[\w:./]+");
        //var optionFlagsRegex = new Regex("<(?:!?\\w+,?)+>");

        var eventMatches = eventRegex.Matches(str);

        foreach (Match em in eventMatches)
        {
            var e  = new GameEvent();
            var km = keyAndTextRegex.Match(em.Value);
            e.Key  = km.Groups["key"].Value;
            e.Text = km.Groups["text"].Value;

            string flags       = km.Groups["flags"].Value;
            var    flagMatches = flagsRegex.Matches(flags);
            foreach (Match fm in flagMatches)
            {
                var flag = fm.Value.TrimStart('!');
                if (flag.Contains(':'))
                {
                    if (flag.StartsWith("s", StringComparison.InvariantCultureIgnoreCase))
                    {
                        // Doing two separate trims here as we only want to trim the s before the colon, inscase the soudn name starts with s
                        flag = flag.TrimStart('s', 'S').TrimStart(':');
                        var v = 1.0f;
                        if (flag.Contains(':'))
                        {
                            var vol = flag.Substring(flag.IndexOf(':') + 1);
                            v = float.Parse(vol);
                            if (v < 0 || v > 1)
                            {
                                Debug.LogError("Sound Volume must be between 0 and 1 in event " + e.Key);
                            }
                            v = Mathf.Clamp01(v);

                            flag = flag.Substring(0, flag.IndexOf(':'));
                        }
                        e.SoundVolumes.Add(v);
                        e.SoundIds.Add(flag);
                    }
                    else if (flag.StartsWith("i", StringComparison.InvariantCultureIgnoreCase))
                    {
                        // Doing two separate trims here as we only want to trim the s before the colon, inscase the soudn name starts with i
                        flag      = flag.TrimStart('i', 'I').TrimStart(':');
                        e.ImageId = flag;
                    }
                    else
                    {
                        Debug.LogError("Unrecognized flag in event " + e.Key);
                    }
                }
                else
                {
                    if (fm.Value.StartsWith("!"))
                    {
                        e.ClearFlags.Add(flag);
                    }
                    else
                    {
                        e.Flags.Add(flag);
                    }
                }
            }

            var optionMatches = optionRegex.Matches(em.Value);
            foreach (Match om in optionMatches)
            {
                var eo     = new EventOption();
                var omText = om.Groups["text"];
                eo.Text = choiceTextRegex.Match(omText.Value).Value;

                var flagMatches2 = flagsRegex.Matches(om.Groups["flags"].Value);
                foreach (Match fm in flagMatches2)
                {
                    var flag = fm.Value.TrimStart('!');
                    if (fm.Value.StartsWith("!"))
                    {
                        eo.NotAllowedFlags.Add(flag);
                    }
                    else
                    {
                        eo.RequiredFlags.Add(flag);
                    }
                }

                var targetMatches = targetRegex.Matches(omText.Value);
                foreach (Match tm in targetMatches)
                {
                    eo.Targets.Add(tm.Groups["target"].Value);
                }
                e.Options.Add(eo);
            }
            currentDatabase.Add(e);
        }

        EditorUtility.SetDirty(currentDatabase);
    }