Skip to content

Commit

Permalink
final fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
FoseFx committed Aug 25, 2024
1 parent 39e4e53 commit 2e0851f
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 5 deletions.
122 changes: 122 additions & 0 deletions mod/app/src/main/java/bttv/Tokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
import tv.twitch.android.shared.chat.pub.model.messages.MessageToken.EmoticonToken;
import tv.twitch.android.shared.chat.pub.messages.ui.ChatMessageInterface;
import tv.twitch.android.shared.chat.ChatMessageDelegate;
import tv.twitch.chat.AutoModFlags;
import tv.twitch.chat.ChatEmoticonToken;
import tv.twitch.chat.ChatMessageToken;
import tv.twitch.chat.ChatMessageTokenType;
import tv.twitch.chat.ChatTextToken;
import tv.twitch.chat.library.model.ChatMessageInfo;

public class Tokenizer {
Expand Down Expand Up @@ -102,6 +107,95 @@ public static void retokenizeLiveChatMessage(ChatMessageInterface chatMessageInt
}
}

/** @noinspection unused */
public static void retokenizeLiveChatMessage(tv.twitch.chat.ChatMessageInfo info) {
try {
Context ctx = Data.ctx;
int channel = Data.currentBroadcasterId;

ArrayList<tv.twitch.chat.ChatMessageToken> newTokens = new ArrayList<>(info.tokens.length + 10);

boolean shouldHighlight = false;
boolean shouldBlock = false;

for (tv.twitch.chat.ChatMessageToken token : info.tokens) {
Log.d("LBTTV", "retokenizeLiveChatMessage: " + token);
if (!isTextTokenOld(token)) {
if (isEmoteTokenOld(token) && !newTokens.isEmpty()) {
tv.twitch.chat.ChatMessageToken prevToken = newTokens.get(newTokens.size() - 1);
if (prevToken != null && !endsWithSpaceOld(prevToken)) {
ChatTextToken spaceToken = new ChatTextToken();
spaceToken.text = " ";
spaceToken.autoModFlags = new AutoModFlags();
newTokens.add(spaceToken);
}
}
newTokens.add(token);
continue;
}

ChatTextToken textToken = (ChatTextToken) token;
String text = textToken.text;

if (text.equals(" ")) {
// " ".split(" ") will produce an empty array
// this is why we need to handle this edge-case
newTokens.add(textToken);
continue;
}
String[] tokens = text.split(" ");

StringBuilder currentText = new StringBuilder();
for (String word : tokens) {
if (Blacklist.shouldBlock(word)) {
shouldBlock = true;
}
if (Highlight.shouldHighlight(word)) {
shouldHighlight = true;
}
Emote emote = Emotes.getEmote(ctx, word, channel);
if (emote == null) {
currentText.append(word).append(" ");
continue;
}
// emote found
String before = currentText.toString();
if (!before.isEmpty()) {
ChatTextToken everythingBeforeEmote = new ChatTextToken();
everythingBeforeEmote.text = before;
everythingBeforeEmote.autoModFlags = textToken.autoModFlags;
newTokens.add(everythingBeforeEmote);
}
ChatEmoticonToken emoteToken = new ChatEmoticonToken();
emoteToken.emoticonText = word;
emoteToken.emoticonId = "BTTV-" + emote.id;
newTokens.add(emoteToken);

// prepare next TextToken
currentText.setLength(0);
currentText.append(' ');
}
String before = currentText.toString();
if (!before.trim().isEmpty()) {
ChatTextToken everything = new ChatTextToken();
everything.text = before;
everything.autoModFlags = textToken.autoModFlags;
newTokens.add(everything);
}
}

info.tokens = newTokens.toArray(new ChatMessageToken[0]);

if (shouldBlock) {
info.messageType = "bttv-blocked-message";
} else if (shouldHighlight) {
info.messageType = "bttv-highlighted-message";
}
} catch (Throwable throwable) {
Log.e("LBTTV", "retokenizeLiveChatMessage: " + throwable.getMessage());
}
}

public static void retokenizeLiveChatMessage(ChatMessageInfo info) {
Context ctx = Data.ctx;
int channel = Data.currentBroadcasterId;
Expand Down Expand Up @@ -196,17 +290,45 @@ private static boolean endsWithSpace(@NotNull tv.twitch.chat.library.model.Messa
return lastChar == ' ';
}

private static boolean endsWithSpaceOld(@NotNull tv.twitch.chat.ChatMessageToken token) {
if (token.type.getValue() != ChatMessageTokenType.Text.getValue()) {
return false;
}
ChatTextToken textToken = (ChatTextToken) token;
String text = textToken.text;
if (text.isEmpty()) {
return false;
}
char lastChar = text.charAt(text.length() - 1);
return lastChar == ' ';
}

private static boolean isTextToken(tv.twitch.chat.library.model.MessageToken token) {
if (token == null) {
return false;
}
return token instanceof tv.twitch.chat.library.model.MessageToken.TextToken;
}

private static boolean isTextTokenOld(tv.twitch.chat.ChatMessageToken token) {
if (token == null) {
return false;
}
return token.type.getValue() == ChatMessageTokenType.Text.getValue();
}


private static boolean isEmoteToken(tv.twitch.chat.library.model.MessageToken token) {
if (token == null) {
return false;
}
return token instanceof tv.twitch.chat.library.model.MessageToken.EmoteToken;
}

private static boolean isEmoteTokenOld(tv.twitch.chat.ChatMessageToken token) {
if (token == null) {
return false;
}
return token.type.getValue() == ChatMessageTokenType.Emoticon.getValue();
}
}
6 changes: 6 additions & 0 deletions mod/twitch/src/main/java/tv/twitch/chat/ChatMessageInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package tv.twitch.chat;

public class ChatMessageInfo {
public tv.twitch.chat.ChatMessageToken[] tokens;
public String messageType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package tv.twitch.chat.library.model;

import java.util.List;
import java.util.Map;

public class ChatMessageInfo {
public List<MessageToken> tokens;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
diff --git a/smali_classes6/tv/twitch/android/shared/chat/messagefactory/SubNoticeChatMessageFactory.smali b/smali_classes6/tv/twitch/android/shared/chat/messagefactory/SubNoticeChatMessageFactory.smali
--- a/smali_classes6/tv/twitch/android/shared/chat/messagefactory/SubNoticeChatMessageFactory.smali
+++ b/smali_classes6/tv/twitch/android/shared/chat/messagefactory/SubNoticeChatMessageFactory.smali
@@ -318,6 +318,13 @@
@@ -318,6 +318,11 @@
.line 62
iget-object v4, v1, Ltv/twitch/chat/ChatSubscriptionNotice;->userMessage:Ltv/twitch/chat/ChatMessageInfo;

+ # BTTV
+ # Add bttv emotes
+ # expects:
+ # v4: tv.twitch.chat.ChatMessageInfo
+ invoke-static {v4}, Lbttv/Tokenizer;->retokenizeLiveChatMessage(Ltv/twitch/chat/ChatMessageInfo;)V
+ # /BTTV
+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ diff --git a/smali_classes8/tv/twitch/chat/library/model/ChatMessageInfo.smali b
.field private final timestamp:Ljava/lang/Long;

-.field private final tokens:Ljava/util/List;
+.field public final tokens:Ljava/util/List;
+.field public tokens:Ljava/util/List;
.annotation system Ldalvik/annotation/Signature;
value = {
"Ljava/util/List<",

0 comments on commit 2e0851f

Please sign in to comment.