From 0e23dfb0963b24c34c00fe6620e32900a86b12ce Mon Sep 17 00:00:00 2001 From: turtleisaac Date: Tue, 10 Nov 2026 16:42:12 +0000 Subject: [PATCH] Show a donation notice when PokeHub is running. Co-authored-by: turtleisaac --- .../io/github/turtleisaac/pokeditor/Main.java | 57 +++++++++++++++--- .../turtleisaac/pokeditor/MainTest.java | 58 +++++++++++++++++++ 2 files changed, 108 insertions(+), 7 deletions(-) create mode 100644 src/test/java/io/github/turtleisaac/pokeditor/MainTest.java diff --git a/src/main/java/io/github/turtleisaac/pokeditor/Main.java b/src/main/java/io/github/turtleisaac/pokeditor/Main.java index 1d8229b..2d49e93 100755 --- a/src/main/java/io/github/turtleisaac/pokeditor/Main.java +++ b/src/main/java/io/github/turtleisaac/pokeditor/Main.java @@ -37,7 +37,7 @@ public static void main(String[] args) throws IOException try (InputStream jokesStream = Main.class.getResourceAsStream(jokesPath)) { mainMenuJokes = jokesStream == null - ? new String[] {""} + ? new String[] { "" } : new String(jokesStream.readAllBytes(), StandardCharsets.UTF_8).split("\n"); } @@ -47,17 +47,20 @@ public static void main(String[] args) throws IOException .setName("PokEditor") .setVersion("3.2.0") // .setFlavorText("Did you know that Jay likes Moemon?") - .setFlavorText(mainMenuJokes[(int) (Math.random()*(mainMenuJokes.length))]) + .setFlavorText(mainMenuJokes[(int) (Math.random() * (mainMenuJokes.length))]) .setAuthor("Developed by Turtleisaac") .setGitEnabled(true) .addLookAndFeel(new FlatArcOrangeIJTheme()) .addLookAndFeel(new FlatDarkPurpleIJTheme()) // .addLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel()) .addGame("Pokémon Platinum", "CPU") - .addGame("Pokémon HeartGold","IPK") - .addGame("Pokémon SoulSilver","IPG") - .addPanelManager(() -> new PokeditorManager(tool)) - .init(); + .addGame("Pokémon HeartGold", "IPK") + .addGame("Pokémon SoulSilver", "IPG") + .addPanelManager(() -> new PokeditorManager(tool)); + + // PokeHub stays open when it launches PokEditor, so a process check covers both cases. + showDonationNoticeIfPokeHubIsRunning(); + tool.init(); } /** @@ -91,4 +94,44 @@ private static void installUncaughtExceptionHandler() "PokEditor", JOptionPane.ERROR_MESSAGE)); }); } -} \ No newline at end of file + + /** Base name of a process command, ignoring a Windows {@code .exe} and letter case. */ + static boolean isPokeHub(String command) + { + if (command == null || command.isEmpty()) + return false; + + int cut = Math.max(command.lastIndexOf('/'), command.lastIndexOf('\\')); + String name = cut < 0 ? command : command.substring(cut + 1); + if (name.length() > 4 && name.regionMatches(true, name.length() - 4, ".exe", 0, 4)) + name = name.substring(0, name.length() - 4); + + return name.equalsIgnoreCase("PokeHub") + || name.equalsIgnoreCase("PokeHub.App") + || name.equalsIgnoreCase("PokeHub.Mcp"); + } + + private static void showDonationNoticeIfPokeHubIsRunning() + { + boolean running; + try + { + running = ProcessHandle.allProcesses() + .anyMatch(handle -> isPokeHub(handle.info().command().orElse(null))); + } + catch (Exception ignored) + { + return; + } + + if (!running) + return; + + JOptionPane.showMessageDialog(null, + "We do not ask for donations, and we do not support them.\n\n" + + "Do not donate to any program that launches PokEditor or that takes code from PokEditor. " + + "Any such program that asks for donations does so against our expressed wishes.", + "PokEditor", + JOptionPane.INFORMATION_MESSAGE); + } +} diff --git a/src/test/java/io/github/turtleisaac/pokeditor/MainTest.java b/src/test/java/io/github/turtleisaac/pokeditor/MainTest.java new file mode 100644 index 0000000..07c97dd --- /dev/null +++ b/src/test/java/io/github/turtleisaac/pokeditor/MainTest.java @@ -0,0 +1,58 @@ +package io.github.turtleisaac.pokeditor; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThat; + +class MainTest +{ + @ParameterizedTest + @ValueSource(strings = { + "C:\\Program Files\\PokeHub\\PokeHub.exe", + "C:\\Program Files\\PokeHub\\PokeHub.App.exe", + "C:\\Program Files\\PokeHub\\pokehub.mcp.EXE", + "D:\\tools\\pokehub.exe", + "\\\\server\\share\\PokeHub.App.exe", + "/Applications/PokeHub.app/Contents/MacOS/PokeHub", + "/usr/local/bin/PokeHub.App", + "/opt/pokehub/PokeHub.Mcp", + "PokeHub", + "PokeHub.App", + "PokeHub.Mcp" + }) + @DisplayName("matches PokeHub, PokeHub.App, and PokeHub.Mcp by executable base name") + void matchesPokeHubExecutables(String command) + { + assertThat(Main.isPokeHub(command)) + .isTrue(); + } + + @ParameterizedTest + @ValueSource(strings = { + "C:\\Windows\\System32\\notepad.exe", + "C:\\PokeHub\\notepad.exe", + "/Applications/PokeHub.app/Contents/MacOS/java", + "/usr/bin/java", + "PokeHubEditor", + "NotPokeHub", + "PokeHub.Mcp.Worker", + "" + }) + @DisplayName("rejects other executables, including ones stored inside a PokeHub directory") + void rejectsOtherCommands(String command) + { + assertThat(Main.isPokeHub(command)) + .isFalse(); + } + + @Test + @DisplayName("a missing command is not PokeHub") + void rejectsNull() + { + assertThat(Main.isPokeHub(null)) + .isFalse(); + } +}