Skip to content

Commit

Permalink
1.5.4 feature: External chrome user dir (#32)
Browse files Browse the repository at this point in the history
* Specify Chrome User Dir via key `external.mini-client.user.dir`

* upgrade version to 1.5.4

Co-authored-by: 9-9-9-9 <9-9-9-9>
  • Loading branch information
9-9-9-9 authored Sep 5, 2021
1 parent 5a47073 commit f9ea10d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.release.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Upgrade notes:
- Move the following file and directory from previous bot version's directory
- `chrome-user-dir` directory, that folder was created by chrome to do temp cache and also save your game's setting
- `user-config.properties` files, it contains your configurations
- `chrome-user-dir` directory, that folder was created by chrome to do temp cache and also save your game's setting. By specify an external directory into the key `external.mini-client.user.dir` of the `user-config.properties` file, next time when you upgrade this bot, you don't need to move this folder
___
## Bit Heroes bot
##### on Linux / Windows / ~~MacOS~~
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>bh.bot</groupId>
<artifactId>BitHeroes</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>

<dependencies>
<dependency>
Expand Down
3 changes: 3 additions & 0 deletions release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ cat <<EOF > ./release/user-config.properties
# Google Chrome path, for Windows only
#external.application.chrome.path=C:\\\\Program Files (x86)\\\\Google\\\\Chrome\\\\Application\\\\chrome.exe
# Google Chrome user dir, specify an external directory help you dont need to move the 'chrome-user-dir' folder next time when you update this Bit Heroes bot
#external.mini-client.user.dir=D:\\\\Data\\\\chrome-user-dir
EOF

# Copy files for mini client
Expand Down
82 changes: 77 additions & 5 deletions src/main/java/bh/bot/app/GenMiniClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import bh.bot.Main;
import bh.bot.common.Configuration;
import bh.bot.common.Log;
import bh.bot.common.OS;
import bh.bot.common.exceptions.InvalidDataException;
import bh.bot.common.types.annotations.AppMeta;
Expand All @@ -15,17 +16,17 @@
import java.util.List;

import static bh.bot.Main.colorFormatInfo;
import static bh.bot.common.Log.err;
import static bh.bot.common.Log.info;
import static bh.bot.common.Log.*;
import static bh.bot.common.utils.StringUtil.isBlank;
import static bh.bot.common.utils.StringUtil.isNotBlank;

@AppMeta(code = "client", name = "Generate mini-client", displayOrder = 4)
public class GenMiniClient extends AbstractApplication {
private static final File chromeUserDir = new File("chrome-user-dir");
private static final String requireChromeUserDirName = "chrome-user-dir";

public static final int supportMaximumNumberOfAccounts = 10;
private static final String keyChromePath = "external.application.chrome.path";
private static final String keyChromeUserDirPath = "external.mini-client.user.dir";

@SuppressWarnings("SpellCheckingInspection")
@Override
Expand Down Expand Up @@ -72,6 +73,14 @@ protected void internalRun(String[] args) {
Files.write(Paths.get("bh-client/holodeck_javascripts.js"), readFromInputStream(fileHolo).getBytes());
Files.write(Paths.get("bh-client/sitewide_javascripts.js"), readFromInputStream(fileSiteWide).getBytes());

final String chromeUserDir = getChromeUserDir();
if (chromeUserDir == null) {
System.exit(Main.EXIT_CODE_EXTERNAL_REASON);
return;
}

info(fWarning, "Chrome user directory: %s", chromeUserDir);

String chromePathOnWindows = getChromePathOnWindows();

for (GameAccount gameAccount : gameAccounts) {
Expand All @@ -96,13 +105,13 @@ protected void internalRun(String[] args) {
chromeArgs.add(String.format("\"--app=file://%s\"", pathIndex.toAbsolutePath().toString()));
} else if (OS.isWin) {
app = String.format("\"%s\"", chromePathOnWindows);
chromeArgs.add(String.format("\"--user-data-dir=%s\"", chromeUserDir.getAbsolutePath()));
chromeArgs.add(String.format("\"--user-data-dir=%s\"", chromeUserDir));
chromeArgs.add("--window-size=820,565");
chromeArgs.add("--window-position=0,0");
chromeArgs.add(String.format("\"--app=file://%s\"", pathIndex.toAbsolutePath().toString()));
} else {
app = "google-chrome";
chromeArgs.add(String.format("'--user-data-dir=%s'", chromeUserDir.getAbsolutePath()));
chromeArgs.add(String.format("'--user-data-dir=%s'", chromeUserDir));
chromeArgs.add("--window-size=800,520");
chromeArgs.add("--window-position=0,0");
chromeArgs.add(String.format("'--app=file://%s'", pathIndex.toAbsolutePath().toString()));
Expand Down Expand Up @@ -159,6 +168,7 @@ protected void internalRun(String[] args) {
}
} catch (Exception e) {
e.printStackTrace();
err("Unable to generate mini-client");
}
}

Expand Down Expand Up @@ -186,6 +196,68 @@ private ArrayList<GameAccount> getGameAccountsConfig() {
return result;
}

private String getChromeUserDir() {
final String defaultChromeUserDir = requireChromeUserDirName;
try {
String cfgChromeUserDir = Configuration.read(keyChromeUserDirPath);
if (isBlank(cfgChromeUserDir)) {
info(colorFormatInfo, "FYI: you can specify a chrome user's directory so next time when you download a new version of this Bit Heroes bot, you don't need to copy the '%s' folder", defaultChromeUserDir);
info(colorFormatInfo, "To do it, edit file `user-config.properties` and set path to key `%s`", keyChromeUserDirPath);
return new File(defaultChromeUserDir).getAbsolutePath();
}

File dir = new File(cfgChromeUserDir);
if (dir.exists()) {
if (!dir.isDirectory()) {
err("The directory you have specified by the value of key %s in user-config.properties is NOT a directory", keyChromeUserDirPath);
err("%s is a file, it's NOT a directory. Please specific an empty directory instead", dir.getAbsolutePath());
return null;
}

if (dir.getName().equalsIgnoreCase(requireChromeUserDirName)) {
return dir.getAbsolutePath();
}

return appendRequiredDirNamePart(dir);
}

if (dir.getName().equalsIgnoreCase(requireChromeUserDirName)) {
if (!dir.mkdir()) {
err("Unable to create directory %s", dir.getAbsolutePath());
err("May be related to permission issue, please specify another directory to the key '%s' in the user-config.properties file", keyChromeUserDirPath);
return null;
}
return dir.getAbsolutePath();
}

return appendRequiredDirNamePart(dir);
} catch (Exception ex) {
ex.printStackTrace();
err("An error occurs while trying to get chrome user dir based on configuration of key `%s` in user-config.properties file", keyChromeUserDirPath);
String result = new File(defaultChromeUserDir).getAbsolutePath();
err("The following directory will be used by default: %s", result);
return result;
}
}

private String appendRequiredDirNamePart(File dir) {
warn("Custom chrome's user directory must ends with '%s', this folder name will be automatically appended", requireChromeUserDirName);
dir = Paths.get(dir.getAbsolutePath(), requireChromeUserDirName).toFile();
warn("Directory after appended: %s", dir.getAbsolutePath());
if (dir.exists()) {
if (!dir.isDirectory()) {
err("But it's NOT a directory, please specify another directory to the key '%s' in the user-config.properties file", keyChromeUserDirPath);
return null;
}
} else {
if (!dir.mkdir()) {
err("Unable to create that directory, may be related to permission issue, please specify another directory to the key '%s' in the user-config.properties file", keyChromeUserDirPath);
return null;
}
}
return dir.getAbsolutePath();
}

private String getChromePathOnWindows() {
if (!OS.isWin)
return null;
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/bh/bot/app/dev/TestApp.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package bh.bot.app.dev;

import bh.bot.Main;
import bh.bot.app.AbstractApplication;
import bh.bot.common.Configuration;
import bh.bot.common.types.AttendablePlace;
import bh.bot.common.types.AttendablePlaces;
import bh.bot.common.types.annotations.AppMeta;
import bh.bot.common.types.tuples.Tuple2;
import bh.bot.common.types.tuples.Tuple3;
import bh.bot.common.utils.InteractionUtil;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.Win32Exception;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.RECT;

import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.util.Arrays;
import java.util.List;

import static bh.bot.Main.readInput;
import static bh.bot.common.Log.*;
import static bh.bot.common.Log.info;
import static bh.bot.common.utils.InteractionUtil.Mouse.clickRadioButton;

@SuppressWarnings("unused")
Expand All @@ -32,12 +29,7 @@ public class TestApp extends AbstractApplication {
protected void internalRun(String[] args) {
adjustScreenOffset();

warn("Hello darkness my old friend");
err("Something wrong here %d", 1);
String str1, str2;
BufferedReader br = Main.getBufferedReader();
str1 = readInput("ask1", null, s -> new Tuple3<>(true, null, "1"));
str2 = readInput("ask2", null, s -> new Tuple3<>(true, null, "2"));
info("%s", new File("/home/hungpv/Dev/Personal/nginx/mhpt/site-content").getName());
}

public static class JnaTest {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/bh/bot/common/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static void info(AnsiFormat cFormat, String format, Object... objs) {
println(color(String.format(format, objs), cFormat));
}

private static final AnsiFormat fWarning = new AnsiFormat(YELLOW_TEXT(), BOLD());
public static final AnsiFormat fWarning = new AnsiFormat(YELLOW_TEXT(), BOLD());

public static void warn(String format, Object... objs) {
if (StringUtil.isBlank(format))
Expand Down

0 comments on commit f9ea10d

Please sign in to comment.