From 7337d90eeeee0cddf7b927ed3257eaab8c400a16 Mon Sep 17 00:00:00 2001 From: Duy Tran Khanh Date: Fri, 27 May 2022 09:56:06 +0700 Subject: [PATCH] All options are now applied without restarting Instances manager: cleanup, now explicitly state the error message Moved init_loadCustomEnv() back to JavaLauncher.m --- Natives/JavaLauncher.m | 39 +++++++++ Natives/LauncherPreferencesViewController.m | 97 ++++++--------------- Natives/main.m | 38 -------- Natives/utils.h | 2 +- 4 files changed, 68 insertions(+), 108 deletions(-) diff --git a/Natives/JavaLauncher.m b/Natives/JavaLauncher.m index 8814d8b6..1f815bb0 100644 --- a/Natives/JavaLauncher.m +++ b/Natives/JavaLauncher.m @@ -46,6 +46,43 @@ NSString *renderer_pre; NSString *allocmem_pre; NSString *multidir_pre; +void init_loadCustomEnv() { + /* Define default env */ + + // I accidentally patched a bit wrong, the value should be a path containing libawt_xawt.dylib, but here is libawt.dylib path (no need to exist) + setenv("JAVA_AWT_PATH", [NSString stringWithFormat:@"%s/Frameworks/libawt.dylib", getenv("BUNDLE_PATH")].UTF8String, 1); + + // Disable overloaded functions hack for Minecraft 1.17+ + setenv("LIBGL_NOINTOVLHACK", "1", 1); + + // Override OpenGL version to 4.1 for Zink + setenv("MESA_GL_VERSION_OVERRIDE", "4.1", 1); + + /* Load custom env */ + FILE *envFile = fopen([NSString stringWithFormat:@"%s/custom_env.txt", getenv("POJAV_HOME")].UTF8String, "r"); + + regLog("[Pre-init] Reading custom environment variables (custom_env.txt), opened=%d\n", envFile != NULL); + + if (envFile) { + char *line = NULL; + size_t len = 0; + ssize_t read; + while ((read = getline(&line, &len, envFile)) != -1) { + if (line[0] == '#' || line[0] == '\n') continue; + if (line[read-1] == '\n') { + line[read-1] = '\0'; + } + if (strchr(line, '=') != NULL) { + regLog("[Pre-init] Added custom env: %s", line); + setenv(strtok(line, "="), strtok(NULL, "="), 1); + } else { + regLog("[Pre-init] Warning: skipped empty value custom env: %s", line); + } + } + fclose(envFile); + } +} + void init_loadCustomJvmFlags() { NSString *jvmargs = getPreference(@"java_args"); BOOL isFirstArg = YES; @@ -94,6 +131,8 @@ NSString* environmentFailsafes(int minVersion) { int launchJVM(NSString *username, NSString *selectedVersion, int width, int height, int minVersion) { sprintf((char*) java_libs_path, "%s/libs", getenv("BUNDLE_PATH")); + init_loadCustomEnv(); + regLog("[JavaLauncher] Beginning JVM launch\n"); NSString *javaHome_pre = getPreference(@"java_home"); diff --git a/Natives/LauncherPreferencesViewController.m b/Natives/LauncherPreferencesViewController.m index 7dc40ed0..39ed507c 100644 --- a/Natives/LauncherPreferencesViewController.m +++ b/Natives/LauncherPreferencesViewController.m @@ -319,14 +319,6 @@ int tempIndex; jhomeTextField.inputAccessoryView = jhomePickerToolbar; jhomeTextField.inputView = jhomePickerView; - if ([getPreference(@"option_warn") boolValue] == YES) { - UIAlertController *preferenceWarn = [UIAlertController alertControllerWithTitle:@"Restart required" message:@"Some options in this menu will require that you restart the launcher for them to take effect."preferredStyle:UIAlertControllerStyleAlert]; - UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil]; - [self presentViewController:preferenceWarn animated:YES completion:nil]; - [preferenceWarn addAction:ok]; - setPreference(@"option_warn", @NO); - } - UILabel *gdirTextView = [[UILabel alloc] initWithFrame:CGRectMake(16.0, currY+=44.0, 0.0, 0.0)]; gdirTextView.text = @"Game directory"; gdirTextView.numberOfLines = 0; @@ -358,14 +350,6 @@ int tempIndex; gdirTextField.inputAccessoryView = gdirPickerToolbar; gdirTextField.inputView = gdirPickerView; - - if ([getPreference(@"option_warn") boolValue] == YES) { - UIAlertController *preferenceWarn = [UIAlertController alertControllerWithTitle:@"Restart required" message:@"Some options in this menu will require that you restart the launcher for them to take effect."preferredStyle:UIAlertControllerStyleAlert]; - UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil]; - [self presentViewController:preferenceWarn animated:YES completion:nil]; - [preferenceWarn addAction:ok]; - setPreference(@"option_warn", @NO); - } UILabel *slideHotbarTextView = [[UILabel alloc] initWithFrame:CGRectMake(16.0, currY+=44.0, 0.0, 0.0)]; slideHotbarTextView.text = @"Slideable hotbar"; @@ -647,7 +631,7 @@ int tempIndex; setenv("JAVA_HOME", [JRE8_HOME_SB cStringUsingEncoding:NSUTF8StringEncoding], 1); } if (![textField.text containsString:JRE8_NAME_JB] && ![textField.text containsString:JRE8_NAME_SB] && [getPreference(@"java_warn") boolValue] == YES) { - UIAlertController *javaAlert = [UIAlertController alertControllerWithTitle:@"Java version is not Java 8" message:@"Minecraft versions below 1.6, modded below 1.16.4, and the mod installer will not work unless you have Java 8 installed on your device."preferredStyle:UIAlertControllerStyleAlert]; + UIAlertController *javaAlert = [UIAlertController alertControllerWithTitle:@"Java version is not Java 8" message:@"Minecraft versions below 1.6, modded below 1.16.4, and the mod installer will not work unless you have Java 8 installed on your device." preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil]; [self presentViewController:javaAlert animated:YES completion:nil]; [javaAlert addAction:ok]; @@ -655,6 +639,7 @@ int tempIndex; } } else if (textField.tag == TAG_GDIR) { setPreference(@"game_directory", textField.text); + init_setupMultiDir(); } } @@ -717,24 +702,14 @@ int tempIndex; }]; UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSString *directory = [NSString stringWithFormat:@"%s/instances/%@", getenv("POJAV_HOME"), [[manageDirAC textFields][0] text]]; + NSError *error; BOOL isDir = NO; - BOOL isFailed = NO; - if(![[NSFileManager defaultManager] fileExistsAtPath:directory isDirectory:&isDir]) { - if([[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:NULL]) { - isFailed = NO; - [self instanceDirCont]; - gdirTextField.text = [[manageDirAC textFields][0] text]; - [gdirTextField endEditing:YES]; - [self manageDirResult:(UIButton *)sender success:isFailed directory:[[manageDirAC textFields][0] text] type:type]; - - } else { - isFailed = YES; - [self manageDirResult:(UIButton *)sender success:isFailed directory:[[manageDirAC textFields][0] text] type:type]; - } - } else { - isFailed = YES; - [self manageDirResult:(UIButton *)sender success:isFailed directory:[[manageDirAC textFields][0] text] type:type]; + if([[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:NO attributes:nil error:&error]) { + [self instanceDirCont]; + gdirTextField.text = [[manageDirAC textFields][0] text]; + [gdirTextField endEditing:YES]; } + [self manageDirResult:(UIButton *)sender error:error directory:[[manageDirAC textFields][0] text] type:type]; }]; [manageDirAC addAction:confirm]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]; @@ -748,24 +723,14 @@ int tempIndex; UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSString *directory = [NSString stringWithFormat:@"%s/instances/%@", getenv("POJAV_HOME"), gdirTextField.text]; NSLog(@"%@", directory); + NSError *error; BOOL isDir = NO; - BOOL isFailed = NO; - - if([[NSFileManager defaultManager] fileExistsAtPath:directory isDirectory:&isDir]) { - if([[NSFileManager defaultManager] removeItemAtPath:directory error:NULL]) { - isFailed = NO; - [self instanceDirCont]; - gdirTextField.text = @"default"; - [gdirTextField endEditing:YES]; - [self manageDirResult:(UIButton *)sender success:isFailed directory:gdirTextField.text type:type]; - } else { - isFailed = YES; - [self manageDirResult:(UIButton *)sender success:isFailed directory:gdirTextField.text type:type]; - } - } else { - isFailed = YES; - [self manageDirResult:(UIButton *)sender success:isFailed directory:gdirTextField.text type:type]; - } + if([[NSFileManager defaultManager] removeItemAtPath:directory error:&error]) { + [self instanceDirCont]; + gdirTextField.text = @"default"; + [gdirTextField endEditing:YES]; + } + [self manageDirResult:(UIButton *)sender error:error directory:gdirTextField.text type:type]; }]; [manageDirAC addAction:confirm]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]; @@ -780,27 +745,20 @@ int tempIndex; } } -- (void)manageDirResult:(UIButton *)sender success:(BOOL)boolean directory:(NSString *)dirName type:(int)type { +- (void)manageDirResult:(UIButton *)sender error:(NSError *)error directory:(NSString *)dirName type:(int)type { NSString *title; NSString *message; - if(type == MKGDIR) { - if(boolean == YES) { - title = @"An error occurred."; - message = [NSString stringWithFormat:@"Ensure that a file with the name %@ does not exist, and that the instances directory is writeable.", dirName]; - } else if(boolean == NO) { + if (error == nil) { + if(type == MKGDIR) { title = @"Successfully changed directory."; - message = @""; - } - } else if (type == RMGDIR) { - if(boolean == YES) { - title = @"An error occurred."; - message = @"Ensure that the instances directory is writeable."; - } else if(boolean == NO) { + } else if (type == RMGDIR) { title = @"Successfully removed directory."; - message = @""; } + } else { + title = @"An error occurred."; + message = error.localizedDescription; } - + UIAlertController *gdirAlert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet]; [self setPopoverProperties:gdirAlert.popoverPresentationController sender:sender]; UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil]; @@ -867,19 +825,19 @@ int tempIndex; message = @"This option allows you to downscale the resolution of the game. Reducing resolution reduces GPU workload for better performance. The minimum resolution scale is 25%."; } else if(setting == ALLOCMEM) { title = @"Allocated RAM"; - message = @"This option allows you to change the amount of memory used by the game. The minimum is 25% (default) and the maximum is 85%. Any value over 40% will require that you use a tool like overb0ard to prevent jetsam crashes. Take note that Java uses some of the memory itself, so the value you set will be lowered slightly in game. This option also requires a restart of the launcher to take effect."; + message = @"This option allows you to change the amount of memory used by the game. The minimum is 25% (default) and the maximum is 85%. Any value over 40% will require that you use a tool like overb0ard to prevent jetsam crashes. Take note that Java uses some of the memory itself, so the value you set will be lowered slightly in game."; } else if(setting == JARGS) { title = @"Java arguments"; - message = @"This option allows you to edit arguments that can be passed to Minecraft. Not all arguments work with PojavLauncher, so be aware. This option also requires a restart of the launcher to take effect."; + message = @"This option allows you to edit arguments that can be passed to Minecraft. Not all arguments work with PojavLauncher, so be aware."; } else if(setting == REND) { title = @"Renderer"; message = @"This option allows you to change the renderer in use. Choosing tinygl4angle will allow 1.17+ to be played, but will also not work with older versions."; } else if(setting == JHOME) { title = @"Java version"; - message = @"This option allows you to change the Java executable directory. Choosing Java 16 or Java 17 may allow you to play 1.17, however older versions and most versions of modded Minecraft, as well as the mod installer, will not work. This option also requires a restart of the launcher to take effect."; + message = @"This option allows you to change the Java executable directory. Choosing Java 16 or Java 17 may allow you to play 1.17, however older versions and most versions of modded Minecraft, as well as the mod installer, will not work."; } else if(setting == GDIRECTORY) { title = @"Game directory"; - message = @"This option allows you to change where your Minecraft settings and saves are stored in a MultiMC like fashion. Useful for when you want to have multiple mod loaders installed but do not want to delete/move mods around to switch. This option also requires a restart of the launcher to take effect."; + message = @"This option allows you to change where your Minecraft settings and saves are stored in a MultiMC like fashion. Useful for when you want to have multiple mod loaders installed but do not want to delete/move mods around to switch."; } else if(setting == SLIDEHOTBAR) { title = @"Slideable hotbar"; message = @"This option allows you to use a finger to slide between hotbar slots."; @@ -1080,6 +1038,7 @@ int tempIndex; } else if(pickerView.tag == TAG_PICKER_GDIR) { gdirTextField.text = [self pickerView:pickerView titleForRow:row forComponent:component]; setPreference(@"game_directory", gdirTextField.text); + init_setupMultiDir(); } } diff --git a/Natives/main.m b/Natives/main.m index bfa6a7a2..f60fbdc1 100644 --- a/Natives/main.m +++ b/Natives/main.m @@ -31,43 +31,6 @@ #define fm NSFileManager.defaultManager -void init_loadCustomEnv() { - /* Define default env */ - - // I accidentally patched a bit wrong, the value should be a path containing libawt_xawt.dylib, but here is libawt.dylib path (no need to exist) - setenv("JAVA_AWT_PATH", [NSString stringWithFormat:@"%s/Frameworks/libawt.dylib", getenv("BUNDLE_PATH")].UTF8String, 1); - - // Disable overloaded functions hack for Minecraft 1.17+ - setenv("LIBGL_NOINTOVLHACK", "1", 1); - - // Override OpenGL version to 4.1 for Zink - setenv("MESA_GL_VERSION_OVERRIDE", "4.1", 1); - - /* Load custom env */ - FILE *envFile = fopen([NSString stringWithFormat:@"%s/custom_env.txt", getenv("POJAV_HOME")].UTF8String, "r"); - - regLog("[Pre-init] Reading custom environment variables (custom_env.txt), opened=%d\n", envFile != NULL); - - if (envFile) { - char *line = NULL; - size_t len = 0; - ssize_t read; - while ((read = getline(&line, &len, envFile)) != -1) { - if (line[0] == '#' || line[0] == '\n') continue; - if (line[read-1] == '\n') { - line[read-1] = '\0'; - } - if (strchr(line, '=') != NULL) { - regLog("[Pre-init] Added custom env: %s", line); - setenv(strtok(line, "="), strtok(NULL, "="), 1); - } else { - regLog("[Pre-init] Warning: skipped empty value custom env: %s", line); - } - } - fclose(envFile); - } -} - void init_logDeviceAndVer(char *argument) { struct utsname systemInfo; uname(&systemInfo); @@ -264,7 +227,6 @@ int main(int argc, char * argv[]) { init_redirectStdio(); init_logDeviceAndVer(argv[0]); - init_loadCustomEnv(); loadPreferences(); init_setupMultiDir(); init_setupLauncherProfiles(); diff --git a/Natives/utils.h b/Natives/utils.h index f4187280..73d54057 100644 --- a/Natives/utils.h +++ b/Natives/utils.h @@ -53,7 +53,7 @@ BOOL isControlModifiable; // Init functions void init_hookFunctions(); - +void init_setupMultiDir(); NSMutableDictionary* parseJSONFromFile(NSString *path); NSError* saveJSONToFile(NSDictionary *dict, NSString *path);