From 19ebae62c2666a571eeb7a1019be3357a7e6efc1 Mon Sep 17 00:00:00 2001 From: Duy Tran Khanh Date: Sun, 21 Aug 2022 21:34:02 +0700 Subject: [PATCH] Make switches functional + warnings + some fixes --- .../src/main/java/org/lwjgl/glfw/GLFW.java | 3 +- Natives/LauncherPreferences.m | 6 +- Natives/LauncherPreferencesViewController2.m | 72 +++++++++++++++++-- Natives/en.lproj/Localizable.strings | 8 ++- Natives/main.m | 2 +- 5 files changed, 78 insertions(+), 13 deletions(-) diff --git a/JavaApp/src/main/java/org/lwjgl/glfw/GLFW.java b/JavaApp/src/main/java/org/lwjgl/glfw/GLFW.java index 0de30691..32f760b9 100644 --- a/JavaApp/src/main/java/org/lwjgl/glfw/GLFW.java +++ b/JavaApp/src/main/java/org/lwjgl/glfw/GLFW.java @@ -15,6 +15,7 @@ import net.kdt.pojavlaunch.Tools; import org.lwjgl.*; import org.lwjgl.system.*; +import org.lwjgl.system.macosx.*; import static org.lwjgl.system.APIUtil.*; import static org.lwjgl.system.Checks.*; @@ -569,7 +570,7 @@ public class GLFW throw new UnsupportedOperationException(); } - private static final SharedLibrary GLFW = Library.loadNative(GLFW.class, "org.lwjgl.glfw", System.getenv("BUNDLE_PATH") + "/PojavLauncher", true); + private static final SharedLibrary GLFW = new MacOSXLibraryDL("PojavLauncher", DynamicLinkLoader.RTLD_DEFAULT); /** Contains the function pointers loaded from the glfw {@link SharedLibrary}. */ diff --git a/Natives/LauncherPreferences.m b/Natives/LauncherPreferences.m index 1253411e..6505b5a4 100644 --- a/Natives/LauncherPreferences.m +++ b/Natives/LauncherPreferences.m @@ -84,12 +84,12 @@ void loadPreferences() { stringByAppendingPathComponent:@"launcher_preferences.plist"]; NSFileManager *fileManager = [NSFileManager defaultManager]; - if (![fileManager fileExistsAtPath:prefPath]) { + prefDict = [NSMutableDictionary dictionaryWithContentsOfFile:prefPath]; + if (![fileManager fileExistsAtPath:prefPath] || [prefDict[@"reset_settings"] boolValue]) { prefDict = [[NSMutableDictionary alloc] init]; envPrefDict = [[NSMutableDictionary alloc] init]; warnPrefDict = [[NSMutableDictionary alloc] init]; } else { - prefDict = [NSMutableDictionary dictionaryWithContentsOfFile:prefPath]; envPrefDict = prefDict[@"env_vars"]; warnPrefDict = prefDict[@"warnings"]; } @@ -124,7 +124,7 @@ void loadPreferences() { setDefaultValueForPref(prefDict, @"slideable_hotbar", @NO); setDefaultValueForPref(prefDict, @"virtmouse_enable", @NO); setDefaultValueForPref(prefDict, @"check_sha", @YES); - setDefaultValueForPref(prefDict, @"ram_unjb_enable", @NO); + setDefaultValueForPref(prefDict, @"auto_ram", @(getenv("POJAV_DETECTEDJB") == NULL)); if (0 != [fileManager fileExistsAtPath:@"/var/mobile/Documents/.pojavlauncher"]) { setDefaultValueForPref(prefDict, @"disable_home_symlink", @NO); diff --git a/Natives/LauncherPreferencesViewController2.m b/Natives/LauncherPreferencesViewController2.m index f8439b69..90f62740 100644 --- a/Natives/LauncherPreferencesViewController2.m +++ b/Natives/LauncherPreferencesViewController2.m @@ -1,4 +1,5 @@ #import +#import #import "DBNumberedSlider.h" #import "LauncherPreferences.h" @@ -55,7 +56,11 @@ typedef void(^CreateView)(UITableViewCell *, NSString *, NSDictionary *); }, @"reset_settings": @{ @"icon": @"trash", - @"type": self.typeSwitch + @"type": self.typeSwitch, + @"warnCondition": ^BOOL(UISwitch *view){ + return view.isOn; + }, + @"warnAlways": @YES } }, @{ // Video and renderer settings @@ -122,11 +127,11 @@ typedef void(^CreateView)(UITableViewCell *, NSString *, NSDictionary *); @"auto_ram": @{ @"icon": @"slider.horizontal.3", @"type": self.typeSwitch, - @"hidden": @(getenv("POJAV_DETECTEDJB") != NULL), + //@"hidden": @(getenv("POJAV_DETECTEDJB") == NULL), + @"requestReload": @YES, @"warnCondition": ^BOOL(UISwitch *view){ - return view.isOn; + return !view.isOn; }, - //@"warnMessage": @"TODO", @"warnAlways": @YES }, @"allocated_memory": @{ @@ -134,6 +139,9 @@ typedef void(^CreateView)(UITableViewCell *, NSString *, NSDictionary *); @"type": self.typeSlider, @"min": @(NSProcessInfo.processInfo.physicalMemory / 1048576 * 0.25), @"max": @(NSProcessInfo.processInfo.physicalMemory / 1048576 * 0.85), + @"enableCondition": ^BOOL(){ + return ![getPreference(@"auto_ram") boolValue]; + }, @"warnAlways": @NO, @"warnCondition": ^BOOL(DBNumberedSlider *view){ return view.value >= NSProcessInfo.processInfo.physicalMemory / 1048576 * 0.4; @@ -165,6 +173,8 @@ typedef void(^CreateView)(UITableViewCell *, NSString *, NSDictionary *); NSDictionary *item = self.prefContents[indexPath.section].allValues[indexPath.row]; CreateView createView = item[@"type"]; createView(cell, key, item); + objc_setAssociatedObject(cell.accessoryView, @"key", key, OBJC_ASSOCIATION_ASSIGN); + objc_setAssociatedObject(cell.accessoryView, @"item", item, OBJC_ASSOCIATION_ASSIGN); // Set general properties if (@available(iOS 13.0, *)) { @@ -172,10 +182,16 @@ typedef void(^CreateView)(UITableViewCell *, NSString *, NSDictionary *); } cell.textLabel.text = NSLocalizedString(([NSString stringWithFormat:@"preference.title.%@", key]), nil); + // Check if one has enable condition and call if it does + BOOL(^checkEnable)(void) = item[@"enableCondition"]; + cell.userInteractionEnabled = !checkEnable || checkEnable(); + return cell; } - (void)initViewCreation { + __weak LauncherPreferencesViewController2 *weakSelf = self; + self.typePickField = ^void(UITableViewCell *cell, NSString *key, NSDictionary *item) { cell.accessoryView = [[UITextField alloc] init]; [(id)(cell.accessoryView) setText:getPreference(key)]; @@ -201,9 +217,53 @@ typedef void(^CreateView)(UITableViewCell *, NSString *, NSDictionary *); }; self.typeSwitch = ^void(UITableViewCell *cell, NSString *key, NSDictionary *item) { - cell.accessoryView = [[UISwitch alloc] init]; - [(id)(cell.accessoryView) setOn:[getPreference(key) boolValue] animated:NO]; + UISwitch *view = [[UISwitch alloc] init]; + [view setOn:[getPreference(key) boolValue] animated:NO]; + [view addTarget:weakSelf action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; + cell.accessoryView = view; }; } +- (void)checkWarn:(UIView *)view { + NSString *key = objc_getAssociatedObject(view, @"key"); + NSDictionary *item = objc_getAssociatedObject(view, @"item"); + + BOOL(^isWarnable)(UIView *) = item[@"warnCondition"]; + NSString *warnKey = item[@"warnKey"]; + if (isWarnable && isWarnable(view) && ![getPreference(warnKey) boolValue]) { + if (warnKey) { + setPreference(warnKey, @YES); + } + + NSString *message = NSLocalizedString(([NSString stringWithFormat:@"preference.warn.%@", key]), nil); + UIAlertController *warnAlert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Warning", nil) message:message preferredStyle:UIAlertControllerStyleActionSheet]; + warnAlert.popoverPresentationController.sourceView = view; + warnAlert.popoverPresentationController.sourceRect = view.bounds; + UIAlertAction *ok = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleCancel handler:nil]; + [warnAlert addAction:ok]; + [self presentViewController:warnAlert animated:YES completion:nil]; + } +} + +- (void)switchChanged:(UISwitch *)sender { + [self checkWarn:sender]; + NSString *key = objc_getAssociatedObject(sender, @"key"); + NSDictionary *item = objc_getAssociatedObject(sender, @"item"); + + // Special switches may define custom value instead of NO/YES + NSArray *customSwitchValue = objc_getAssociatedObject(sender, @"customSwitchValue"); + if (customSwitchValue != nil) { + setPreference(key, customSwitchValue[sender.isOn]); + } else { + setPreference(key, @(sender.isOn)); + } + + // Some settings may affect the availability of other settings + // In this case, a switch may request to reload to apply user interaction change + if ([item[@"requestReload"] boolValue]) { + // TODO: only reload needed rows + [self.tableView reloadData]; + } +} + @end diff --git a/Natives/en.lproj/Localizable.strings b/Natives/en.lproj/Localizable.strings index 6dde7815..6bc7e7cc 100644 --- a/Natives/en.lproj/Localizable.strings +++ b/Natives/en.lproj/Localizable.strings @@ -71,6 +71,7 @@ "preference.title.cosmetica" = "Enable Cosmetica integration"; "preference.title.reset_warnings" = "Reset all warnings"; "preference.title.reset_settings" = "Reset all settings"; +"preference.warn.reset_settings" = "The next time you open PojavLauncher, all of the default settings will be restored. If you did this by accident, you can turn this off back."; "preference.title.renderer" = "Game renderer"; "preference.title.resolution" = "Game resolution"; "preference.title.disable_gesture" = "Disable gestures"; @@ -80,9 +81,12 @@ "preference.title.mouse_speed" = "On-screen mouse speed"; "preference.title.virtmouse_enable" = "Enable on-screen mouse"; "preference.title.slideable_hotbar" = "Enable slideable hotbar"; -"preference.title.java_home" = "Java runtime selector"; +"preference.title.java_home" = "Use Java 17 for version < 1.17"; +// FIXME: is this filled well in the subtitle? +// "preference.subtitle.java_home" = "Java 8 is used by default on Minecraft 1.16.5 and older. This switch forces old versions to use Java 17."; "preference.title.java_args" = "Custom java arguments"; -"preference.title.auto_ram" = "Enable custom memory slider"; +"preference.title.auto_ram" = "Auto determine RAM amount"; +"preference.warn.auto_ram" = "Due to limitations in the operating system itself, high amount may cause instability. Proceed with caution."; "preference.title.allocated_memory" = "Allocated memory"; // sha check // home symlink diff --git a/Natives/main.m b/Natives/main.m index 9f0249b0..ece20178 100644 --- a/Natives/main.m +++ b/Natives/main.m @@ -234,7 +234,7 @@ int main(int argc, char * argv[]) { } else { setenv("POJAV_HOME", [NSString stringWithFormat:@"%s/Documents", getenv("HOME")].UTF8String, 1); - init_hookFunctions(); + //init_hookFunctions(); } [fm createDirectoryAtPath:@(getenv("POJAV_HOME")) withIntermediateDirectories:NO attributes:nil error:nil];