private SeString Tran(SeString messageSeString) { var cleanMessage = new SeString(new List <Payload>()); cleanMessage.Append(messageSeString); var originalMessage = new SeString(new List <Payload>()); originalMessage.Append(messageSeString); var run = true; if (messageSeString.Payloads.Count < 2) { } else if (messageSeString.Payloads[0] == new UIForegroundPayload(_pluginInterface.Data, 48) && messageSeString.Payloads[1] == new UIForegroundPayload(_pluginInterface.Data, 0)) { PluginLog.Log("Caught loop B"); run = false; } if (!run) { return(messageSeString); } var tranDone = false; for (var i = 0; i < messageSeString.Payloads.Count; i++) { if (messageSeString.Payloads[i].Type == PayloadType.MapLink || messageSeString.Payloads[i].Type == PayloadType.Item || messageSeString.Payloads[i].Type == PayloadType.Quest) { i += 7; continue; } if (messageSeString.Payloads[i].Type == PayloadType.Player) { i += 2; continue; } if (messageSeString.Payloads[i].Type == PayloadType.Status) { i += 10; continue; } if (messageSeString.Payloads[i].Type != PayloadType.RawText) { continue; } //PluginLog.Log("Type PASS"); var text = (TextPayload)messageSeString.Payloads[i]; var translatedText = Translate(text.Text); if (translatedText == "LOOP") { continue; } messageSeString.Payloads[i] = new TextPayload(translatedText); messageSeString.Payloads.Insert(i, new UIForegroundPayload(_pluginInterface.Data, (ushort)_textColour[0].Option)); messageSeString.Payloads.Insert(i, new UIForegroundPayload(_pluginInterface.Data, 0)); if (i + 3 < messageSeString.Payloads.Count) { messageSeString.Payloads.Insert(i + 3, new UIForegroundPayload(_pluginInterface.Data, 0)); } else { messageSeString.Payloads.Append(new UIForegroundPayload(_pluginInterface.Data, 0)); } i += 2; tranDone = true; } if (!tranDone) { return(messageSeString); } // Adding to the rolling "last translation" list _lastTranslations.Insert(0, cleanMessage.TextValue); if (_lastTranslations.Count > 10) { _lastTranslations.RemoveAt(10); } if (_tranMode == 0) // Append { var tranMessage = new SeString(new List <Payload>()); tranMessage.Payloads.Add(new TextPayload(" | ")); //originalMessage.Payloads.Insert(0, new UIForegroundPayload(_pluginInterface.Data, 0)); //originalMessage.Payloads.Insert(0, new UIForegroundPayload(_pluginInterface.Data, 48)); originalMessage.Append(tranMessage); originalMessage.Append(messageSeString); return(originalMessage); //PrintChat(type, senderName, originalMessage); } return(messageSeString); }
private void Chat_OnChatMessage(XivChatType type, uint senderId, ref SeString sender, ref SeString message, ref bool isHandled) { try { if (isHandled) { return; } if (!_channels.Contains(type)) { return; } var pName = getName(sender, type, message); var run = true; //Catch already translated messages if (message.Payloads.Count < 2) { } else if (message.Payloads[0].Type == PayloadType.UIForeground && message.Payloads[1].Type == PayloadType.UIForeground) { PluginLog.Log("Caught loop A"); run = false; } if (!run) { return; } var messageString = message.TextValue; var predictedLanguage = Lang(messageString); PluginLog.Log($"PRED LANG: {predictedLanguage}"); var yes = true; var pos = Array.IndexOf(_codes, predictedLanguage); //Check for whitelist settings if (_whitelist && !_chosenLanguages.Contains(pos)) { yes = false; } //Check for notSelf setting if (_notSelf && _pluginInterface.ClientState.LocalPlayer.Name == pName) { yes = false; } //Check for blacklist settings if (_blacklist.Contains(messageString)) { yes = false; } if (predictedLanguage == _codes[_languageInt] || !yes) { return; } //Checking if any rawtext to translate exists var rawExists = message.Payloads.Any(payload => payload.Type == PayloadType.RawText); if (!rawExists) { return; } var originalMessage = new SeString(new List <Payload>()); originalMessage.Append(message); var tranSeString = Task.Run(() => Tran(originalMessage)); if (_oneChan && _tranMode == 2) { type = _order[_oneInt]; } if (_tranMode == 2) // is it Append (0), Replace (1), or additional (2) { PrintChat(type, pName, tranSeString.Result); } message = tranSeString.Result; } catch (Exception e) { PluginLog.LogError($"Chat Translator Error: {e}"); } }