Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 50 additions & 7 deletions src/main/java/io/github/turtleisaac/pokeditor/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -91,4 +94,44 @@ private static void installUncaughtExceptionHandler()
"PokEditor", JOptionPane.ERROR_MESSAGE));
});
}
}

/** 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);
}
}
58 changes: 58 additions & 0 deletions src/test/java/io/github/turtleisaac/pokeditor/MainTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading