From e7ca5e7b9aff859241af6a2fd0f1f27962b1b26e Mon Sep 17 00:00:00 2001 From: Florian Meier Date: Sun, 5 Apr 2020 13:44:40 +0200 Subject: [PATCH] Make message parsing more robust --- .../com/mercury/platform/core/ChatHelper.java | 76 ++++++++++--------- .../core/utils/MessageFileHandler.java | 26 ++++++- 2 files changed, 65 insertions(+), 37 deletions(-) diff --git a/app-core/src/main/java/com/mercury/platform/core/ChatHelper.java b/app-core/src/main/java/com/mercury/platform/core/ChatHelper.java index 42277c51..d8381779 100644 --- a/app-core/src/main/java/com/mercury/platform/core/ChatHelper.java +++ b/app-core/src/main/java/com/mercury/platform/core/ChatHelper.java @@ -66,37 +66,45 @@ private void executeTradeMessage() { result.contains("Bonjour") || result.contains("์•ˆ๋…•") || result.contains("ะ—ะด"))) { - this.gameToFront(); - MercuryStoreCore.blockHotkeySubject.onNext(true); - robot.keyRelease(KeyEvent.VK_ALT); - robot.keyPress(KeyEvent.VK_ENTER); - robot.keyRelease(KeyEvent.VK_ENTER); - - robot.keyPress(KeyEvent.VK_CONTROL); - robot.keyPress(KeyEvent.VK_A); - robot.keyRelease(KeyEvent.VK_CONTROL); - robot.keyRelease(KeyEvent.VK_A); - - robot.keyPress(KeyEvent.VK_BACK_SPACE); - robot.keyRelease(KeyEvent.VK_BACK_SPACE); - - robot.keyPress(KeyEvent.VK_CONTROL); - robot.keyPress(KeyEvent.VK_V); - robot.keyRelease(KeyEvent.VK_V); - robot.keyRelease(KeyEvent.VK_CONTROL); - robot.keyPress(KeyEvent.VK_ENTER); - robot.keyRelease(KeyEvent.VK_ENTER); - - Timer timer = new Timer(300, action -> { - StringSelection selection = new StringSelection(""); - clipboard.setContents(selection, null); - }); - timer.setRepeats(false); - timer.start(); - - MercuryStoreCore.blockHotkeySubject.onNext(false); + if (this.gameToFront() == true) { + MercuryStoreCore.blockHotkeySubject.onNext(true); + robot.keyRelease(KeyEvent.VK_ALT); + robot.keyPress(KeyEvent.VK_ENTER); + robot.keyRelease(KeyEvent.VK_ENTER); + + robot.keyPress(KeyEvent.VK_CONTROL); + robot.keyPress(KeyEvent.VK_A); + robot.keyRelease(KeyEvent.VK_CONTROL); + robot.keyRelease(KeyEvent.VK_A); + + robot.keyPress(KeyEvent.VK_BACK_SPACE); + robot.keyRelease(KeyEvent.VK_BACK_SPACE); + + robot.keyPress(KeyEvent.VK_CONTROL); + robot.keyPress(KeyEvent.VK_V); + robot.keyRelease(KeyEvent.VK_V); + robot.keyRelease(KeyEvent.VK_CONTROL); + robot.keyPress(KeyEvent.VK_ENTER); + robot.keyRelease(KeyEvent.VK_ENTER); + + Timer timer = new Timer(300, action -> { + StringSelection selection = new StringSelection(""); + clipboard.setContents(selection, null); + }); + timer.setRepeats(false); + timer.start(); + + MercuryStoreCore.blockHotkeySubject.onNext(false); + } else { + System.out.println("Failed to bring game to front!"); + } + } - } catch (UnsupportedFlavorException | IOException e) { + } catch (UnsupportedFlavorException e) { + System.out.println("Unsupported Flavor Exception occured, logging it"); + MercuryStoreCore.errorHandlerSubject.onNext(new MercuryError(e)); + } catch (IOException e) { + System.out.println("IOException occured, logging it"); MercuryStoreCore.errorHandlerSubject.onNext(new MercuryError(e)); } } @@ -128,8 +136,8 @@ private void openChat(String whisper) { MercuryStoreCore.blockHotkeySubject.onNext(false); } - private void gameToFront() { - User32.INSTANCE.EnumWindows((hWnd, arg1) -> { + private boolean gameToFront() { + return !User32.INSTANCE.EnumWindows((hWnd, arg1) -> { char[] className = new char[512]; User32.INSTANCE.GetClassName(hWnd, className, 512); String wText = Native.toString(className); @@ -137,9 +145,9 @@ private void gameToFront() { if (wText.isEmpty()) { return true; } + //System.out.println(wText); if (wText.equals("POEWindowClass")) { - User32.INSTANCE.SetForegroundWindow(hWnd); - return false; + return !User32.INSTANCE.SetForegroundWindow(hWnd); } return true; }, null); diff --git a/app-core/src/main/java/com/mercury/platform/core/utils/MessageFileHandler.java b/app-core/src/main/java/com/mercury/platform/core/utils/MessageFileHandler.java index 254873b1..007d7cd1 100644 --- a/app-core/src/main/java/com/mercury/platform/core/utils/MessageFileHandler.java +++ b/app-core/src/main/java/com/mercury/platform/core/utils/MessageFileHandler.java @@ -2,6 +2,7 @@ import com.mercury.platform.core.utils.interceptor.*; import com.mercury.platform.shared.AsSubscriber; +import com.mercury.platform.shared.entity.message.MercuryError; import com.mercury.platform.shared.store.MercuryStoreCore; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; @@ -9,6 +10,9 @@ import java.io.File; import java.io.RandomAccessFile; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; @@ -73,9 +77,19 @@ public void parse() { .collect(Collectors.toList()); List resultMessages = filteredMessages.stream().filter(message -> { + if (message == null) { + return false; + } if ( message.contains("2019") || message.contains("2020" ) || message.contains("2021" )) { //todo - Date date = new Date(StringUtils.substring(message, 0, 20)); - return date.after(lastMessageDate); + try { + DateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); + Date date = df.parse(StringUtils.substring(message, 0, 20).trim()); + return date.after(lastMessageDate); + } catch (ParseException e) { + System.out.println("Error while parsing message, #datefix in message: " + message); + MercuryStoreCore.errorHandlerSubject.onNext(new MercuryError(e)); + return false; + } } else { return false; } @@ -84,7 +98,13 @@ public void parse() { this.interceptors.forEach(interceptor -> { resultMessages.forEach(message -> { if (interceptor.match(message)) { - this.lastMessageDate = new Date(StringUtils.substring(message, 0, 20)); + try { + DateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); + this.lastMessageDate = df.parse(StringUtils.substring(message, 0, 20).trim()); + } catch (ParseException e) { + System.out.println("Error while parsing message, #datefix in message: " + message); + MercuryStoreCore.errorHandlerSubject.onNext(new MercuryError(e)); + } } }); });