/// <summary> /// Returns a list of parsed message lines. /// </summary> /// <param name="message">The message to parse</param> public void ParseMessage(string message) { message = message.Replace("\\n", "\n").Replace("$k\n", "$k\\n"); //Split the lines based on the line-end markers var delimiters = new List <string> { "$k$p", "$k\\n" }; var pattern = "(?<=" + string.Join("|", delimiters.Select(Regex.Escape).ToArray()) + ")"; var splits = Regex.Split(message, pattern); foreach (var str in splits) { var newLine = new MessageLine { RawLine = str }; MessageLines.Add(newLine); } }
public string GetParsedCommands(MessageLine line) { //TODO(Robin): Move to CommandTokens? Func <string, bool, string> purify = (s, replaceEnvironment) => { s = s.Replace("\\n", "\n") .Replace("$k\n", "$k\\n"); if (replaceEnvironment) { s = s.Replace("\n", Environment.NewLine); } return(s); }; if (line.SpokenText != string.Empty) { line.UpdateRawWithNewDialogue(); line.RawLine = purify(line.RawLine, false); } line.SpokenText = line.RawLine; for (var i = 0; i < line.SpokenText.Length; i++) { if (line.SpokenText[i] != '$') { continue; } var res = MessageBlock.ParseCommand(line.SpokenText, i); line.SpokenText = res.Item1; var command = res.Item2; var @params = command.Params; if (command.numParams > 0) { if (@params[0] == "ベロア") { @params[0] = "べロア"; // Velour Fix } } switch (command.CommandWithPrefix) { case CommandTokens.E: var emotion = @params[0] != "," ? @params[0] : DefaultEmotion; if (string.IsNullOrEmpty(charActive) || charActive != charB) { emotionA = emotion; } else { Debug.Assert(charActive == charB, $"{charActive} == {charB}"); emotionB = emotion; } break; case CommandTokens.Ws: charActive = @params[0]; break; case CommandTokens.Wm: CharSide = Convert.ToInt32(@params[1]); //NOTE(Robin): Prepare an exception for multiple possible fail states below var unexpectedCharSideException = new ArgumentOutOfRangeException(nameof(CharSide), CharSide, "Unexpected character side parameter."); switch (conversationType) { case ConversationTypes.Type0: { switch (CharSide) { case 0: case 2: charA = @params[0]; emotionA = DefaultEmotion; break; case 6: charB = @params[0]; emotionB = DefaultEmotion; break; default: throw unexpectedCharSideException; } break; } case ConversationTypes.Type1: { switch (CharSide) { case 3: charA = @params[0]; emotionA = DefaultEmotion; break; case 7: charB = @params[0]; emotionB = DefaultEmotion; break; default: throw unexpectedCharSideException; } break; } default: throw new ArgumentOutOfRangeException(nameof(conversationType), conversationType, "Unexpected conversation type."); } break; case CommandTokens.Wd: if (charActive == charB) { charActive = charA; charB = string.Empty; } else { Debug.Assert(charActive == charA, $"{nameof(charActive)} == {nameof(charA)}"); charA = string.Empty; } break; case CommandTokens.a: hasPerms = true; break; case CommandTokens.t0: if (!setType) { conversationType = ConversationTypes.Type0; } setType = true; break; case CommandTokens.t1: if (!setType) { conversationType = ConversationTypes.Type1; } setType = true; break; case CommandTokens.Nu: line.SpokenText = $"{line.SpokenText.Substring(0, i)}$Nu{line.SpokenText.Substring(i)}"; i += 2; break; default: Debug.WriteLine($"Unhandled command: {command.CommandWithPrefix}"); if (!CommandTokens.IsValid(command.CommandWithPrefix)) { throw new ArgumentOutOfRangeException(nameof(command.CommandWithPrefix), command.CommandWithPrefix, "Unexpected command."); } break; } i--; } if (string.IsNullOrWhiteSpace(line.SpokenText)) { line.SpokenText = string.Empty; } line.SpeechIndex = line.RawLine.LastIndexOf(line.SpokenText, StringComparison.Ordinal); line.RawLine = purify(line.RawLine, false); line.SpokenText = purify(line.SpokenText, true); return(line.SpokenText); }