Skip to content

Commit

Permalink
Use language files instead of hard coding english text (I18n update)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustRed23 committed Jan 24, 2024
1 parent d125888 commit e6a9ed9
Show file tree
Hide file tree
Showing 13 changed files with 241 additions and 63 deletions.
17 changes: 16 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,20 @@ configurations {
transitive {
extendsFrom(implementation)
}
includeInJar
}

dependencies {
compileOnly 'org.jetbrains:annotations:23.0.0'
compileOnly 'org.jetbrains:annotations:24.0.0'
implementation 'org.kettingpowered:kettingcommon:2.2.0'
implementation 'com.google.code.gson:gson:2.10.1' //Used in Patcher
implementation 'me.tongfei:progressbar:0.10.0' //Used to display progress
transitive 'org.jline:jline-reader:3.21.0' //needed for progressbar
transitive 'org.jline:jline-terminal:3.21.0' //needed for progressbar

//Include gson in the jar
includeInJar 'com.google.code.gson:gson:2.10.1'

//Used in Processors
implementation 'net.minecraftforge:binarypatcher:1.2.0:fatjar'
implementation 'net.minecraftforge:installertools:1.4.0'
Expand All @@ -60,10 +64,21 @@ tasks.register('generateKettingLauncherLibraries', org.kettingpowered.task.Gener
configuration = 'transitive'
}

tasks.register('generateJarIncludes', Copy) {
from configurations.includeInJar.collect { zipTree(it) }
into project.buildDir.path + '/includeInJar'
exclude 'META-INF/**'
}

jar{
dependsOn generateKettingLauncherLibraries
mustRunAfter generateKettingLauncherLibraries

dependsOn generateJarIncludes
mustRunAfter generateJarIncludes

from project.buildDir.path + '/includeInJar'

from(generateKettingLauncherLibraries.output) {
rename {
"data/launcher_libraries.txt"
Expand Down
41 changes: 18 additions & 23 deletions src/main/java/org/kettingpowered/launcher/KettingLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.kettingpowered.launcher.dependency.*;
import org.kettingpowered.launcher.info.MCVersion;
import org.kettingpowered.launcher.internal.utils.HashUtils;
import org.kettingpowered.launcher.lang.I18n;
import org.kettingpowered.launcher.utils.FileUtils;
import org.kettingpowered.ketting.internal.hacks.JavaHacks;

Expand Down Expand Up @@ -61,9 +62,7 @@ public class KettingLauncher {
Bundled_ForgeVersion = Bundled_ForgeVersion1;
Bundled_McVersion = Bundled_McVersion1;
Bundled = Bundled1;
if (Main.DEBUG && Bundled){
System.out.println("We are in a bundled Jar, with McVersion: "+Bundled_McVersion + " ForgeVersion: "+Bundled_ForgeVersion+" KettingVersion: "+Bundled_KettingVersion);
}
if (Main.DEBUG && Bundled) I18n.log("debug.bundled.extra", Bundled_McVersion, Bundled_ForgeVersion, Bundled_KettingVersion);
}

public static final String ArtifactID = "kettinglauncher";
Expand Down Expand Up @@ -100,7 +99,7 @@ public class KettingLauncher {
*/
public void init() throws Exception {
final String mc_version;
if (Main.DEBUG && Bundled) System.out.println("We are in a bundled jar. Read Version information from manifest.");
if (Main.DEBUG && Bundled) I18n.log("debug.bundled");

//Cache ketting versions
final List<String> depVersions = new MavenManifest(KettingConstants.KETTINGSERVER_GROUP, Main.FORGE_SERVER_ARTIFACT_ID).getDepVersions();
Expand All @@ -120,7 +119,7 @@ public void init() throws Exception {
//This cannot get moved past the ensureOneServerAndUpdate call.
//It will cause the just downloaded server to be removed, which causes issues.
if (Patcher.checkUpdateNeeded()) {
if (Main.DEBUG) System.out.println("Patcher needs updating.");
if (Main.DEBUG) I18n.log("debug.patcher.update");
//prematurely delete files to prevent errors
FileUtils.deleteDir(KettingFiles.NMS_BASE_DIR);
FileUtils.deleteDir(KettingFiles.KETTINGSERVER_BASE_DIR);
Expand All @@ -138,7 +137,7 @@ public void init() throws Exception {
}

private static void extractBundledContent() throws Exception {
Exception exception = new Exception("Failed to extract some of the Bundled Jar content");
Exception exception = new Exception(I18n.get("error.launcher.bundle_extract_failed"));
boolean failed = false;
final String version = Bundled_McVersion+"-"+Bundled_ForgeVersion+"-"+Bundled_KettingVersion;
deleteOtherVersions(version);
Expand Down Expand Up @@ -187,7 +186,7 @@ public static void extractJarContent(@NotNull String from, @NotNull File to) thr
public static void extractJarContent(@NotNull String from, @NotNull File to, boolean add_to_cp) throws IOException {
try (InputStream internalFile = KettingLauncher.class.getClassLoader().getResourceAsStream(from)) {
if (internalFile == null)
throw new IOException("Failed to extract file '" + from + "', file not found");
throw new IOException(I18n.get("error.launcher.bundled_file_not_found", from));

byte[] internalFileContent = internalFile.readAllBytes();
if (!to.exists() || !HashUtils.getHash(to, "SHA3-512").equals(HashUtils.getHash(new ByteArrayInputStream(internalFileContent), "SHA3-512"))) {
Expand All @@ -201,7 +200,7 @@ public static void extractJarContent(@NotNull String from, @NotNull File to, boo
}
}
} catch (NoSuchAlgorithmException e) {
throw new IOException("Failed to extract file '" + from + "', failed to get hash algorithm", e);
throw new IOException(I18n.get("error.launcher.hash_algorithm_not_found"), e);
}
if (add_to_cp) Main.INST.appendToSystemClassLoaderSearch(new JarFile(to));
}
Expand All @@ -218,22 +217,18 @@ private void updateLauncher() throws Exception {
if(Main.DEBUG) System.out.println(launcherVersions.stream().map(MajorMinorPatchVersion::toString).collect(Collectors.joining("\n")));
MajorMinorPatchVersion<Integer> version = MajorMinorPatchVersion.parse(Version).convertMMP(Integer::parseInt);
final int index = launcherVersions.indexOf(version);
if (index<0) {
System.err.println("Using unrecognised Launcher version.");
}
if (index<0) I18n.logError("error.launcher.unrecognized_version");
//if we are the latest version, then no need to do anything
else if (index==launcherVersions.size()-1) {
System.out.println("Already on newest Launcher version. Nothing to do.");
I18n.log("info.launcher.up_to_date");
return;
}

//else update the launcher jar. We cannot (and shouldn't) apply the update. It will be done on the next server boot.
final MavenArtifact dep = new MavenArtifact(KettingConstants.KETTING_GROUP, ArtifactID, launcherVersions.get(launcherVersions.size()-1).toString(), Optional.empty(), Optional.of("jar"));
if (dep.download().renameTo(Main.LauncherJar)) {
System.err.println("Downloaded a Launcher update. A restart is required to apply the launcher update.");
}else{
System.err.println("Something went wrong whilst replacing the Launcher Jar.");
}

if (dep.download().renameTo(Main.LauncherJar)) I18n.log("info.launcher.updated");
else I18n.logError("error.launcher.update_failed");
}
private static void deleteOtherVersions(final String version){
Arrays.stream(Objects.requireNonNullElse(KettingFiles.KETTINGSERVER_FORGE_DIR.listFiles(File::isDirectory), new File[0]))
Expand Down Expand Up @@ -273,16 +268,16 @@ else if(versions.size() > 1) {

//if we don't have a ketting server version for the given minecraft version or there is a new ketting server version for the given minecraft version and server updates are enabled:
// download the newest version and launch that.
if (needsDownload) System.out.println("Downloading Server, since there is none currently present. Using determined Minecraft version: "+ mc_version);
if (needsDownload) I18n.log("info.launcher.server_download", mc_version);
// get the newest version
if (args.enableServerUpdator() || needsDownload) {
final List<Tuple<MajorMinorPatchVersion<Integer>, MajorMinorPatchVersion<Integer>>> parsedServerVersions = MajorMinorPatchVersion.parseKettingServerVersionList(serverVersions.stream()).getOrDefault(mc_mmp, new ArrayList<>());
if (Main.DEBUG) {
System.out.println("Available Ketting versions");
I18n.log("debug.launcher.available_server_versions");
System.out.println(String.join("\n", serverVersions));
}
if (parsedServerVersions.isEmpty()) {
System.err.println("Found no Ketting version for the requested Minecraft Version: " + mc_version);
I18n.logError("error.launcher.no_server_version", mc_version);
System.exit(1);
}
serverVersion = parsedServerVersions.get(0);
Expand Down Expand Up @@ -312,7 +307,7 @@ else if(versions.size() > 1) {
.forEach(element->{
try{
if (element == serverBinPatchesArtifact && !serverBinPatchesArtifact.download().renameTo(KettingFiles.SERVER_LZMA))
System.err.println("An error occurred, whilst moving Server Binary Patches to it's correct location. There might be errors Patching!");
I18n.logError("error.launcher.bin_patches_move");
else element.download();
} catch (Exception e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -377,12 +372,12 @@ void prepareLaunch() throws Exception {
JavaHacks.loadExternalFileSystems(KettingLauncher.class.getClassLoader());

if (args.installOnly()) {
System.out.println("Server has been installed.");
I18n.log("info.launcher.install_only.success");
System.exit(0);
}
}
void launch() throws Exception {
System.out.println("Launching Ketting...");
I18n.log("info.launcher.launching");
final List<String> arg_list = new ArrayList<>(args.args());
arg_list.add("--launchTarget");
arg_list.add(args.launchTarget());
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/kettingpowered/launcher/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.kettingpowered.ketting.internal.KettingConstants;
import org.kettingpowered.launcher.dependency.*;
import org.kettingpowered.launcher.lang.I18n;

import java.io.BufferedReader;
import java.io.File;
Expand Down Expand Up @@ -37,6 +38,10 @@ public class Main {

public static void agentmain(String agentArgs, Instrumentation inst) throws Exception {
if (DEBUG) System.out.println("[Agent] premain lib load start");

//Load language files
I18n.load();

INST = inst;
URL urlKettingCommon;
List<Object> dependencyList;
Expand All @@ -63,6 +68,9 @@ public static void agentmain(String agentArgs, Instrumentation inst) throws Exce

@SuppressWarnings("unused")
public static Object[] agent_pre_kettingcommon() throws Exception{
//Damn classloaders
I18n.load(true);

List<Dependency<MavenArtifact>> dependencyList;
//Download all needed libs for the Launcher itself
try (BufferedReader stream = new BufferedReader(new InputStreamReader(Objects.requireNonNull(Main.class.getClassLoader().getResourceAsStream("data/launcher_libraries.txt"))))){
Expand All @@ -88,6 +96,9 @@ public static Object[] agent_pre_kettingcommon() throws Exception{

@SuppressWarnings("unused")
public static void agent_post_kettingcommon(List<Object> dependencyList, Instrumentation inst) {
//Damn classloaders
I18n.load(true);

final Libraries libs = new Libraries();
List<Dependency<MavenArtifact>> deps = dependencyList.stream()
.map(obj->{
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/org/kettingpowered/launcher/Patcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.kettingpowered.launcher.betterui.BetterUI;
import org.kettingpowered.launcher.internal.utils.HashUtils;
import org.kettingpowered.launcher.internal.utils.NetworkUtils;
import org.kettingpowered.launcher.lang.I18n;
import org.kettingpowered.launcher.utils.Processors;

import java.io.BufferedReader;
Expand Down Expand Up @@ -50,7 +51,7 @@ private void downloadServer() throws IOException {

final String manifest = NetworkUtils.readFile("https://launchermeta.mojang.com/mc/game/version_manifest.json");
if (manifest == null) {
System.err.println("Failed to download version_manifest.json");
I18n.logError("error.patcher.no_version_manifest");
System.exit(1);
}

Expand All @@ -66,13 +67,13 @@ private void downloadServer() throws IOException {
}

if (currentRelease == null) {
System.err.println("Failed to find release for version " + KettingConstants.MINECRAFT_VERSION);
I18n.logError("error.patcher.no_release", KettingConstants.MINECRAFT_VERSION);
System.exit(1);
}

final String releaseManifest = NetworkUtils.readFile(currentRelease.get("url").getAsString());
if (releaseManifest == null) {
System.err.println("Failed to download release manifest");
I18n.logError("error.patcher.no_release_manifest");
System.exit(1);
}

Expand All @@ -86,7 +87,7 @@ private void downloadServer() throws IOException {
try {
NetworkUtils.downloadFile(serverUrl, serverJar, serverHash, "sha1");
} catch (Exception e) {
throw new IOException("Failed to download server jar", e);
throw new IOException(I18n.get("error.patcher.failed_download"), e);
}
}

Expand All @@ -103,7 +104,7 @@ private void readInstallScript() {

processors.remove(0); //Remove the extracting processor, we'll handle that ourselves
}catch (IOException exception){
System.err.println("Failed to load/read installscript.json");
I18n.logError("error.patcher.failed_read_install_script");
System.exit(1);
}
}
Expand Down Expand Up @@ -183,7 +184,7 @@ public void write(int b) {
progressBar.step();
} catch (IOException e) {
unmute();
throw new RuntimeException("A processor ran into an error", e);
throw new RuntimeException(I18n.get("error.patcher.processor_fault"), e);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.kettingpowered.launcher.KettingLauncher;
import org.kettingpowered.launcher.Main;
import org.kettingpowered.launcher.betterui.BetterUI;
import org.kettingpowered.launcher.lang.I18n;

import java.io.File;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -56,14 +57,14 @@ public void downloadExternal(List<Dependency<MavenArtifact>> dependencies, boole

private File downloadDep(Dependency<MavenArtifact> dep) {
if (KettingLauncher.Bundled && KettingConstants.KETTINGSERVER_GROUP.equals(dep.maven().group())) {
if (Main.DEBUG) System.out.println("Skipping download of "+dep+", since it should be bundled.");
if (Main.DEBUG) I18n.log("info.libraries.skip_bundled", dep);
return Maven.getDependencyPath(dep.maven().getPath()).toFile().getAbsoluteFile();
}
File depFile;
try{
if (!KettingLauncher.Bundled && KettingLauncher.AUTO_UPDATE_LIBS.stream().anyMatch(artifact -> dep.maven().equalsIgnoringVersion(artifact))) {
MavenArtifact artifact = dep.maven().getLatestMinorPatch();
if (Main.DEBUG) System.out.println("Using "+artifact+" instead of "+dep.maven());
if (Main.DEBUG) I18n.log("info.libraries.using_different", artifact, dep.maven());
if (!artifact.equals(dep.maven())) depFile = artifact.download();
else depFile = dep.download();
}else {
Expand Down Expand Up @@ -98,7 +99,7 @@ public void addLoadedLib(URL url){

public URL[] getLoadedLibs() {
if (loadedLibs.stream().anyMatch(Objects::isNull)) {
System.err.println("Failed to load libraries, please try again");
I18n.logError("error.libraries.failed_to_load");
System.exit(1);
}

Expand Down
Loading

0 comments on commit e6a9ed9

Please sign in to comment.