Skip to content

Commit cbd360a

Browse files
committed
chore: rewrite MCOptionUtils to ObjC
1 parent 00678b0 commit cbd360a

11 files changed

Lines changed: 183 additions & 115 deletions

File tree

JavaApp/src/launcher/net/kdt/pojavlaunch/PojavLauncher.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,6 @@ public static void launchMinecraft(String[] args) throws Throwable {
6161

6262
String sizeStr = System.getProperty("cacio.managed.screensize");
6363
System.setProperty("glfw.windowSize", sizeStr);
64-
String[] size = sizeStr.split("x");
65-
MCOptionUtils.load();
66-
MCOptionUtils.set("fullscreen", "false");
67-
MCOptionUtils.set("overrideWidth", size[0]);
68-
MCOptionUtils.set("overrideHeight", size[1]);
69-
// Default settings for performance
70-
MCOptionUtils.setDefault("mipmapLevels", "0");
71-
MCOptionUtils.setDefault("particles", "1");
72-
MCOptionUtils.setDefault("renderDistance", "2");
73-
MCOptionUtils.setDefault("simulationDistance", "5");
74-
MCOptionUtils.save();
7564

7665
// Setup Forge splash.properties
7766
File forgeSplashFile = new File(Tools.DIR_GAME_NEW, "config/splash.properties");

JavaApp/src/launcher/net/kdt/pojavlaunch/uikit/UIKit.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.*;
44
import java.lang.reflect.*;
55
import java.util.jar.*;
6-
import net.kdt.pojavlaunch.utils.MCOptionUtils;
76
import net.kdt.pojavlaunch.*;
87
import org.lwjgl.glfw.*;
98

@@ -13,8 +12,6 @@ public class UIKit {
1312
public static final int ACTION_MOVE = 2;
1413
public static final int ACTION_MOVE_MOTION = 3;
1514

16-
private static int guiScale;
17-
1815
private static void patch_FlatLAF_setLinux() {
1916
String osName = System.getProperty("os.name");
2017
System.setProperty("os.name", "Linux");
@@ -48,18 +45,6 @@ public static void callback_JavaGUIViewController_launchJarFile(final String fil
4845
method.invoke(null, new Object[]{args});
4946
}
5047

51-
public static void updateMCGuiScale() {
52-
MCOptionUtils.load();
53-
String str = MCOptionUtils.get("guiScale");
54-
guiScale = (str == null ? 0 :Integer.parseInt(str));
55-
56-
int scale = Math.max(Math.min(GLFW.mGLFWWindowWidth / 320, GLFW.mGLFWWindowHeight / 240), 1);
57-
if(scale < guiScale || guiScale == 0){
58-
guiScale = scale;
59-
}
60-
updateMCGuiScale(guiScale);
61-
}
62-
6348
static {
6449
System.load(System.getenv("BUNDLE_PATH") + "/AngelAuraAmethyst");
6550
}
@@ -68,6 +53,4 @@ public static void updateMCGuiScale() {
6853
// public static native void runOnUIThread(UIKitCallback callback);
6954

7055
public static native void showError(String title, String message, boolean exitIfOk);
71-
72-
private static native void updateMCGuiScale(int scale);
7356
}

JavaApp/src/launcher/net/kdt/pojavlaunch/utils/MCOptionUtils.java

Lines changed: 0 additions & 81 deletions
This file was deleted.

JavaApp/src/lwjgl/org/lwjgl/glfw/GLFW.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,6 @@ public static void glfwSetInputMode(@NativeType("GLFWwindow *") long window, int
10951095
if (mode == GLFW_CURSOR) {
10961096
switch (value) {
10971097
case GLFW_CURSOR_DISABLED:
1098-
net.kdt.pojavlaunch.uikit.UIKit.updateMCGuiScale();
10991098
CallbackBridge.nativeSetGrabbing(true);
11001099
break;
11011100
default: CallbackBridge.nativeSetGrabbing(false);

Natives/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ add_executable(AngelAuraAmethyst
155155
LauncherProfileEditorViewController.m
156156
LauncherProfilesViewController.m
157157
LauncherSplitViewController.m
158+
MinecraftOptionUtils.m
158159
MinecraftResourceDownloadTask.m
159160
MinecraftResourceUtils.m
160161
PickTextField.m

Natives/JavaLauncher.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#import "ios_uikit_bridge.h"
1515
#import "JavaLauncher.h"
1616
#import "LauncherPreferences.h"
17+
#import "MinecraftOptionUtils.h"
1718
#import "PLLogOutputView.h"
1819
#import "PLProfiles.h"
1920

@@ -222,6 +223,9 @@ int launchJVM(NSString *username, id launchTarget, int width, int height, int mi
222223
return 1;
223224
}
224225

226+
// Setup options.txt
227+
[MinecraftOptionUtils setupOptionsAtGameDir:gameDir];
228+
225229
int margc = -1;
226230
const char *margv[1000];
227231

Natives/MinecraftOptionUtils.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#import <Foundation/Foundation.h>
2+
3+
NS_ASSUME_NONNULL_BEGIN
4+
5+
@interface MinecraftOptionUtils : NSObject
6+
7+
@property(nonatomic) NSString *optionsPath;
8+
9+
+ (void)setupOptionsAtGameDir:(NSString *)gameDir;
10+
+ (instancetype)sharedInstance;
11+
12+
- (void)loadFromPath:(NSString *)optionsPath;
13+
- (void)setKey:(NSString *)key value:(NSString *)value;
14+
- (void)setDefaultForKey:(NSString *)key value:(NSString *)value;
15+
- (nullable NSString *)getValueForKey:(NSString *)key;
16+
- (void)updateMCGuiScale;
17+
- (void)save;
18+
19+
@end
20+
21+
NS_ASSUME_NONNULL_END

Natives/MinecraftOptionUtils.m

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#import "MinecraftOptionUtils.h"
2+
#import "environ.h"
3+
4+
@interface MinecraftOptionUtils ()
5+
@property(nonatomic) NSMutableArray<NSString *> *lineList;
6+
@end
7+
8+
@implementation MinecraftOptionUtils
9+
10+
+ (void)setupOptionsAtGameDir:(NSString *)gameDir {
11+
NSAssert(windowWidth > 0 && windowHeight > 0, @"called before setting windowWidth/windowHeight?");
12+
MinecraftOptionUtils *options = [MinecraftOptionUtils sharedInstance];
13+
[options loadFromPath:[gameDir stringByAppendingPathComponent:@"options.txt"]];
14+
// initial gui scale, also implicitly calls load
15+
[options updateMCGuiScale];
16+
[options setKey:@"fullscreen" value:@"false"];
17+
[options setKey:@"overrideWidth" value:@(windowWidth)];
18+
[options setKey:@"overrideHeight" value:@(windowHeight)];
19+
// Default settings for performance
20+
[options setDefaultForKey:@"mipmapLevels" value:@"0"];
21+
[options setDefaultForKey:@"particles" value:@"1"];
22+
[options setDefaultForKey:@"renderDistance" value:@"2"];
23+
[options setDefaultForKey:@"simulationDistance" value:@"5"];
24+
[options save];
25+
}
26+
27+
+ (instancetype)sharedInstance {
28+
static MinecraftOptionUtils *sharedInstance = nil;
29+
static dispatch_once_t onceToken;
30+
31+
dispatch_once(&onceToken, ^{
32+
sharedInstance = [[MinecraftOptionUtils alloc] init];
33+
});
34+
35+
return sharedInstance;
36+
}
37+
38+
- (void)load {
39+
NSAssert(self.optionsPath.length, @"optionsPath is not set");
40+
self.lineList = [NSMutableArray array];
41+
42+
NSError *error = nil;
43+
NSString *contents = [NSString stringWithContentsOfFile:self.optionsPath
44+
encoding:NSUTF8StringEncoding
45+
error:&error];
46+
47+
if (error != nil) {
48+
NSLog(@"Could not load options.txt: %@", error);
49+
return;
50+
}
51+
52+
self.lineList = [contents componentsSeparatedByCharactersInSet:
53+
[NSCharacterSet newlineCharacterSet]];
54+
}
55+
56+
- (void)ensureLoaded {
57+
NSAssert(self.lineList != nil, @"Unitialized MinecraftOptionUtils");
58+
}
59+
60+
- (void)setKey:(NSString *)key value:(NSString *)value {
61+
[self ensureLoaded];
62+
63+
NSString *prefix = [key stringByAppendingString:@":"];
64+
65+
for (NSUInteger i = 0; i < self.lineList.count; i++) {
66+
NSString *line = self.lineList[i];
67+
68+
if ([line hasPrefix:prefix]) {
69+
self.lineList[i] = [NSString stringWithFormat:@"%@:%@", key, value];
70+
return;
71+
}
72+
}
73+
74+
[self.lineList addObject:[NSString stringWithFormat:@"%@:%@", key, value]];
75+
}
76+
77+
- (void)setDefaultForKey:(NSString *)key value:(NSString *)value {
78+
if ([self getValueForKey:key] == nil) {
79+
[self.lineList addObject:[NSString stringWithFormat:@"%@:%@", key, value]];
80+
}
81+
}
82+
83+
- (nullable NSString *)getValueForKey:(NSString *)key {
84+
[self ensureLoaded];
85+
86+
NSString *prefix = [key stringByAppendingString:@":"];
87+
88+
for (NSString *line in self.lineList) {
89+
if ([line hasPrefix:prefix]) {
90+
NSRange range = [line rangeOfString:@":"];
91+
92+
if (range.location != NSNotFound) {
93+
return [line substringFromIndex:range.location + 1];
94+
}
95+
}
96+
}
97+
98+
return nil;
99+
}
100+
101+
- (void)removeValueForKey:(NSString *)key {
102+
[self ensureLoaded];
103+
104+
NSString *prefix = [key stringByAppendingString:@":"];
105+
106+
NSIndexSet *indexes = [self.lineList indexesOfObjectsPassingTest:^BOOL(NSString *line, NSUInteger idx, BOOL *stop) {
107+
return [line hasPrefix:prefix];
108+
}];
109+
110+
if (indexes.count > 0) {
111+
[self.lineList removeObjectsAtIndexes:indexes];
112+
}
113+
}
114+
115+
- (void)updateMCGuiScale {
116+
[self load];
117+
guiScale = [self getValueForKey:@"guiScale"].intValue;
118+
//guiScale = (str == null ? 0 :Integer.parseInt(str));
119+
120+
int scale = MAX(MIN(windowWidth / 320, windowHeight / 240), 1);
121+
if(scale < guiScale || guiScale == 0){
122+
guiScale = scale;
123+
}
124+
}
125+
126+
- (void)save {
127+
[self ensureLoaded];
128+
129+
if (self.optionsPath.length == 0) {
130+
NSLog(@"Could not save options.txt: optionsPath is not set");
131+
return;
132+
}
133+
134+
NSString *result = [self.lineList componentsJoinedByString:@"\n"];
135+
136+
NSError *error = nil;
137+
BOOL success = [result writeToFile:self.optionsPath
138+
atomically:YES
139+
encoding:NSUTF8StringEncoding
140+
error:&error];
141+
142+
if (!success) {
143+
NSLog(@"Could not save options.txt: %@", error);
144+
}
145+
146+
self.lineList = nil;
147+
}
148+
149+
@end

Natives/environ.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ jmethodID method_SystemClipboardDataReceived;
7575
#undef ADD_CALLBACK_WWIN
7676
//};
7777

78+
int guiScale;
7879
float resolutionScale;
7980
BOOL virtualMouseEnabled, isControlModifiable;
8081
uint64_t hwRedirectOrig[6], hwRedirectTarget[6];

Natives/input_bridge_v3.m

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import <UIKit/UIKit.h>
1111
#import "AppDelegate.h"
1212
#import "SurfaceViewController.h"
13+
#import "MinecraftOptionUtils.h"
1314

1415
#include <assert.h>
1516
#include <dlfcn.h>
@@ -379,7 +380,6 @@ void closeGLFWWindow() {
379380
GLFW_KEY_4, GLFW_KEY_5, GLFW_KEY_6,
380381
GLFW_KEY_7, GLFW_KEY_8, GLFW_KEY_9
381382
};
382-
int guiScale = 1;
383383
int mcscale(CGFloat input) {
384384
return (int)((guiScale * input)/resolutionScale);
385385
}
@@ -399,17 +399,17 @@ int callback_SurfaceViewController_touchHotbar(CGFloat x, CGFloat y) {
399399
return hotbarKeys[(int) MathUtils_map(x, barX, barX + barWidth, 0, 9)];
400400
}
401401

402-
JNIEXPORT void JNICALL Java_net_kdt_pojavlaunch_uikit_UIKit_updateMCGuiScale(JNIEnv* env, jclass clazz, jint scale) {
403-
guiScale = scale;
404-
}
405-
406402
JNIEXPORT jstring JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeClipboard(JNIEnv* env, jclass clazz, jint action, jstring copySrc) {
407403
NSDebugLog(@"Debug: Clipboard access is going on\n");
408404
return UIKit_accessClipboard(env, action, copySrc);
409405
}
410406

411407
JNIEXPORT void JNICALL Java_org_lwjgl_glfw_CallbackBridge_nativeSetGrabbing(JNIEnv* env, jclass clazz, jboolean grabbing, jfloat xset, jfloat yset) {
412408
isGrabbing = grabbing;
409+
410+
if(grabbing) {
411+
[MinecraftOptionUtils.sharedInstance updateMCGuiScale];
412+
}
413413

414414
dispatch_async(dispatch_get_main_queue(), ^{
415415
SurfaceViewController *vc = ((SurfaceViewController *)UIWindow.mainWindow.rootViewController);

0 commit comments

Comments
 (0)