Replace static fields with instance fields
This commit is contained in:
@@ -64,21 +64,40 @@ import static java.nio.charset.StandardCharsets.UTF_8;
|
|||||||
import static java.util.stream.Collectors.toSet;
|
import static java.util.stream.Collectors.toSet;
|
||||||
import static org.jackhuang.hmcl.Metadata.HMCL_DIRECTORY;
|
import static org.jackhuang.hmcl.Metadata.HMCL_DIRECTORY;
|
||||||
import static org.jackhuang.hmcl.util.Logging.LOG;
|
import static org.jackhuang.hmcl.util.Logging.LOG;
|
||||||
import static org.jackhuang.hmcl.util.SelfDependencyPatcher.DependencyDescriptor.JFX_DEPENDENCIES;
|
|
||||||
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
|
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
|
||||||
import static org.jackhuang.hmcl.util.platform.JavaVersion.CURRENT_JAVA;
|
import static org.jackhuang.hmcl.util.platform.JavaVersion.CURRENT_JAVA;
|
||||||
|
|
||||||
// From: https://github.com/Col-E/Recaf/blob/7378b397cee664ae81b7963b0355ef8ff013c3a7/src/main/java/me/coley/recaf/util/self/SelfDependencyPatcher.java
|
// From: https://github.com/Col-E/Recaf/blob/7378b397cee664ae81b7963b0355ef8ff013c3a7/src/main/java/me/coley/recaf/util/self/SelfDependencyPatcher.java
|
||||||
public final class SelfDependencyPatcher {
|
public final class SelfDependencyPatcher {
|
||||||
private SelfDependencyPatcher() {
|
private final List<DependencyDescriptor> dependencies = DependencyDescriptor.readDependencies();
|
||||||
|
private final List<Repository> repositories;
|
||||||
|
private final Repository defaultRepository;
|
||||||
|
|
||||||
|
private SelfDependencyPatcher() throws IncompatibleVersionException {
|
||||||
|
// We can only self-patch JavaFX on specific platform.
|
||||||
|
if (dependencies == null) {
|
||||||
|
throw new IncompatibleVersionException();
|
||||||
|
}
|
||||||
|
|
||||||
|
final String customUrl = System.getProperty("hmcl.openjfx.repo");
|
||||||
|
if (customUrl == null) {
|
||||||
|
if (System.getProperty("user.country", "").equalsIgnoreCase("CN")) {
|
||||||
|
defaultRepository = Repository.ALIYUN_MIRROR;
|
||||||
|
} else {
|
||||||
|
defaultRepository = Repository.MAVEN_CENTRAL;
|
||||||
|
}
|
||||||
|
repositories = Collections.unmodifiableList(Arrays.asList(Repository.MAVEN_CENTRAL, Repository.ALIYUN_MIRROR));
|
||||||
|
} else {
|
||||||
|
defaultRepository = new Repository(String.format(i18n("repositories.custom"), customUrl), customUrl);
|
||||||
|
repositories = Collections.unmodifiableList(Arrays.asList(Repository.MAVEN_CENTRAL, Repository.ALIYUN_MIRROR, defaultRepository));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class DependencyDescriptor {
|
private static final class DependencyDescriptor {
|
||||||
|
private static final String DEPENDENCIES_LIST_FILE = "/assets/openjfx-dependencies.json";
|
||||||
private static final Path DEPENDENCIES_DIR_PATH = HMCL_DIRECTORY.resolve("dependencies").resolve(Platform.getPlatform().toString()).resolve("openjfx");
|
private static final Path DEPENDENCIES_DIR_PATH = HMCL_DIRECTORY.resolve("dependencies").resolve(Platform.getPlatform().toString()).resolve("openjfx");
|
||||||
public static final List<DependencyDescriptor> JFX_DEPENDENCIES = readDependencies();
|
|
||||||
|
|
||||||
private static List<DependencyDescriptor> readDependencies() {
|
static List<DependencyDescriptor> readDependencies() {
|
||||||
//noinspection ConstantConditions
|
//noinspection ConstantConditions
|
||||||
try (Reader reader = new InputStreamReader(SelfDependencyPatcher.class.getResourceAsStream(DEPENDENCIES_LIST_FILE), UTF_8)) {
|
try (Reader reader = new InputStreamReader(SelfDependencyPatcher.class.getResourceAsStream(DEPENDENCIES_LIST_FILE), UTF_8)) {
|
||||||
Map<String, List<DependencyDescriptor>> allDependencies =
|
Map<String, List<DependencyDescriptor>> allDependencies =
|
||||||
@@ -109,32 +128,10 @@ public final class SelfDependencyPatcher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static final class Repository {
|
private static final class Repository {
|
||||||
public static final List<Repository> REPOSITORIES;
|
|
||||||
|
|
||||||
public static final Repository CUSTOM;
|
|
||||||
public static final Repository MAVEN_CENTRAL = new Repository(i18n("repositories.maven_central"), "https://repo1.maven.org/maven2");
|
public static final Repository MAVEN_CENTRAL = new Repository(i18n("repositories.maven_central"), "https://repo1.maven.org/maven2");
|
||||||
public static final Repository ALIYUN_MIRROR = new Repository(i18n("repositories.aliyun_mirror"), "https://maven.aliyun.com/repository/central");
|
public static final Repository ALIYUN_MIRROR = new Repository(i18n("repositories.aliyun_mirror"), "https://maven.aliyun.com/repository/central");
|
||||||
|
|
||||||
public static final Repository DEFAULT;
|
|
||||||
|
|
||||||
static {
|
|
||||||
final String customUrl = System.getProperty("hmcl.openjfx.repo");
|
|
||||||
if (customUrl == null) {
|
|
||||||
CUSTOM = null;
|
|
||||||
if (System.getProperty("user.country", "").equalsIgnoreCase("CN")) {
|
|
||||||
DEFAULT = Repository.ALIYUN_MIRROR;
|
|
||||||
} else {
|
|
||||||
DEFAULT = Repository.MAVEN_CENTRAL;
|
|
||||||
}
|
|
||||||
REPOSITORIES = Collections.unmodifiableList(Arrays.asList(MAVEN_CENTRAL, ALIYUN_MIRROR));
|
|
||||||
} else {
|
|
||||||
CUSTOM = new Repository(String.format(i18n("repositories.custom"), customUrl), customUrl);
|
|
||||||
DEFAULT = CUSTOM;
|
|
||||||
REPOSITORIES = Collections.unmodifiableList(Arrays.asList(MAVEN_CENTRAL, ALIYUN_MIRROR, CUSTOM));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
private final String url;
|
private final String url;
|
||||||
|
|
||||||
@@ -152,8 +149,6 @@ public final class SelfDependencyPatcher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final String DEPENDENCIES_LIST_FILE = "/assets/openjfx-dependencies.json";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Patch in any missing dependencies, if any.
|
* Patch in any missing dependencies, if any.
|
||||||
*/
|
*/
|
||||||
@@ -177,19 +172,16 @@ public final class SelfDependencyPatcher {
|
|||||||
throw new IncompatibleVersionException();
|
throw new IncompatibleVersionException();
|
||||||
}
|
}
|
||||||
|
|
||||||
// We can only self-patch JavaFX on specific platform.
|
SelfDependencyPatcher patcher = new SelfDependencyPatcher();
|
||||||
if (JFX_DEPENDENCIES == null) {
|
|
||||||
throw new IncompatibleVersionException();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise we're free to download in Java 11+
|
// Otherwise we're free to download in Java 11+
|
||||||
LOG.info("Missing JavaFX dependencies, attempting to patch in missing classes");
|
LOG.info("Missing JavaFX dependencies, attempting to patch in missing classes");
|
||||||
|
|
||||||
// Download missing dependencies
|
// Download missing dependencies
|
||||||
List<DependencyDescriptor> missingDependencies = checkMissingDependencies();
|
List<DependencyDescriptor> missingDependencies = patcher.checkMissingDependencies();
|
||||||
if (!missingDependencies.isEmpty()) {
|
if (!missingDependencies.isEmpty()) {
|
||||||
try {
|
try {
|
||||||
fetchDependencies(missingDependencies);
|
patcher.fetchDependencies(missingDependencies);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new PatchException("Failed to download dependencies", e);
|
throw new PatchException("Failed to download dependencies", e);
|
||||||
}
|
}
|
||||||
@@ -197,7 +189,7 @@ public final class SelfDependencyPatcher {
|
|||||||
|
|
||||||
// Add the dependencies
|
// Add the dependencies
|
||||||
try {
|
try {
|
||||||
loadFromCache();
|
patcher.loadFromCache();
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
throw new PatchException("Failed to load JavaFX cache", ex);
|
throw new PatchException("Failed to load JavaFX cache", ex);
|
||||||
} catch (ReflectiveOperationException | NoClassDefFoundError ex) {
|
} catch (ReflectiveOperationException | NoClassDefFoundError ex) {
|
||||||
@@ -206,7 +198,7 @@ public final class SelfDependencyPatcher {
|
|||||||
LOG.info(" - Done!");
|
LOG.info(" - Done!");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Repository showChooseRepositoryDialog() {
|
private Repository showChooseRepositoryDialog() {
|
||||||
final JPanel panel = new JPanel();
|
final JPanel panel = new JPanel();
|
||||||
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
|
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
|
||||||
|
|
||||||
@@ -216,12 +208,12 @@ public final class SelfDependencyPatcher {
|
|||||||
|
|
||||||
final ButtonGroup buttonGroup = new ButtonGroup();
|
final ButtonGroup buttonGroup = new ButtonGroup();
|
||||||
|
|
||||||
for (Repository repository : Repository.REPOSITORIES) {
|
for (Repository repository : repositories) {
|
||||||
final JRadioButton button = new JRadioButton(repository.name);
|
final JRadioButton button = new JRadioButton(repository.name);
|
||||||
button.putClientProperty("repository", repository);
|
button.putClientProperty("repository", repository);
|
||||||
buttonGroup.add(button);
|
buttonGroup.add(button);
|
||||||
panel.add(button);
|
panel.add(button);
|
||||||
if (repository == Repository.DEFAULT) {
|
if (repository == defaultRepository) {
|
||||||
button.setSelected(true);
|
button.setSelected(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -249,14 +241,14 @@ public final class SelfDependencyPatcher {
|
|||||||
* @throws IOException When the locally cached dependency urls cannot be resolved.
|
* @throws IOException When the locally cached dependency urls cannot be resolved.
|
||||||
* @throws ReflectiveOperationException When the call to add these urls to the system classpath failed.
|
* @throws ReflectiveOperationException When the call to add these urls to the system classpath failed.
|
||||||
*/
|
*/
|
||||||
private static void loadFromCache() throws IOException, ReflectiveOperationException {
|
private void loadFromCache() throws IOException, ReflectiveOperationException {
|
||||||
LOG.info(" - Loading dependencies...");
|
LOG.info(" - Loading dependencies...");
|
||||||
|
|
||||||
Set<String> modules = JFX_DEPENDENCIES.stream()
|
Set<String> modules = dependencies.stream()
|
||||||
.map(it -> it.module)
|
.map(it -> it.module)
|
||||||
.collect(toSet());
|
.collect(toSet());
|
||||||
|
|
||||||
Path[] jars = JFX_DEPENDENCIES.stream()
|
Path[] jars = dependencies.stream()
|
||||||
.map(DependencyDescriptor::localPath)
|
.map(DependencyDescriptor::localPath)
|
||||||
.toArray(Path[]::new);
|
.toArray(Path[]::new);
|
||||||
|
|
||||||
@@ -268,11 +260,11 @@ public final class SelfDependencyPatcher {
|
|||||||
*
|
*
|
||||||
* @throws IOException When the files cannot be fetched or saved.
|
* @throws IOException When the files cannot be fetched or saved.
|
||||||
*/
|
*/
|
||||||
private static void fetchDependencies(List<DependencyDescriptor> dependencies) throws IOException {
|
private void fetchDependencies(List<DependencyDescriptor> dependencies) throws IOException {
|
||||||
boolean isFirstTime = true;
|
boolean isFirstTime = true;
|
||||||
|
|
||||||
byte[] buffer = new byte[IOUtils.DEFAULT_BUFFER_SIZE];
|
byte[] buffer = new byte[IOUtils.DEFAULT_BUFFER_SIZE];
|
||||||
Repository repository = Repository.DEFAULT;
|
Repository repository = defaultRepository;
|
||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
@@ -346,10 +338,10 @@ public final class SelfDependencyPatcher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<DependencyDescriptor> checkMissingDependencies() {
|
private List<DependencyDescriptor> checkMissingDependencies() {
|
||||||
List<DependencyDescriptor> missing = new ArrayList<>();
|
List<DependencyDescriptor> missing = new ArrayList<>();
|
||||||
|
|
||||||
for (DependencyDescriptor dependency : JFX_DEPENDENCIES) {
|
for (DependencyDescriptor dependency : dependencies) {
|
||||||
if (!Files.exists(dependency.localPath())) {
|
if (!Files.exists(dependency.localPath())) {
|
||||||
missing.add(dependency);
|
missing.add(dependency);
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
Reference in New Issue
Block a user