|
| 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 |
0 commit comments