Skip to content

Commit febc62c

Browse files
committed
Resizable loginscreen POC
1 parent ccf6397 commit febc62c

File tree

9 files changed

+833
-550
lines changed

9 files changed

+833
-550
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

src/main/java/org/runejs/Configuration.java

Lines changed: 92 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -24,88 +24,100 @@ public static void read() {
2424

2525
final Map<String, Object> obj = yaml.load(inputStream);
2626

27-
final Map<String, Object> net = (Map<String, Object>) obj.get("net");
28-
final Map<String, Object> cache = (Map<String, Object>) obj.get("cache");
29-
final Map<String, Object> rsa = (Map<String, Object>) obj.get("rsa");
30-
final Map<String, Object> login = (Map<String, Object>) obj.get("login");
31-
final Map<String, Object> game = (Map<String, Object>) obj.get("game");
32-
33-
SERVER_ADDRESS = (String) net.get("address");
34-
GAME_PORT = (int) net.get("game_port");
35-
CACHE_NAME = (String) cache.get("cacheDir");
36-
RSA_PUBLIC_KEY = new BigInteger(String.valueOf((int) rsa.get("rsaPub")));
37-
RSA_MODULUS = (BigInteger) rsa.get("rsaModulus");
38-
USE_STATIC_DETAILS = (boolean) login.get("useStaticCredentials");
39-
USERNAME = (String) login.get("username");
40-
PASSWORD = (String) login.get("password");
41-
ROOFS_ENABLED = (boolean) game.get("roofsEnabled");
42-
SHIFT_CLICK_MODIFIER = (boolean) game.get("shiftClickModifier");
43-
SOUND_MUTED = (boolean) game.get("soundMuted");
44-
FREE_TELEPORTS = (boolean) game.get("freeTeleports");
45-
DEBUG_CONTEXT = (boolean) game.get("debugContextMenu");
46-
RESIZABLE = (boolean) game.get("resizable");
47-
RENDER_FLAMES = (boolean) game.get("renderFlames");
48-
SERVER_DISPLAY_NAME = (String) obj.get("serverDisplayName");
49-
50-
if (USERNAME == null) {
51-
USERNAME = "";
52-
}
53-
54-
if (PASSWORD == null) {
55-
PASSWORD = "";
56-
}
57-
58-
if (SERVER_DISPLAY_NAME == null) {
59-
SERVER_DISPLAY_NAME = "Build 435";
60-
}
27+
final Map<String, Object> net = getOrDefaultMap(obj, "net");
28+
final Map<String, Object> cache = getOrDefaultMap(obj, "cache");
29+
final Map<String, Object> rsa = getOrDefaultMap(obj, "rsa");
30+
final Map<String, Object> login = getOrDefaultMap(obj, "login");
31+
final Map<String, Object> game = getOrDefaultMap(obj, "game");
32+
33+
SERVER_ADDRESS = getOrDefault(net, "address", SERVER_ADDRESS);
34+
GAME_PORT = getOrDefault(net, "game_port", GAME_PORT);
35+
CACHE_NAME = getOrDefault(cache, "cacheDir", CACHE_NAME);
36+
RSA_PUBLIC_KEY = new BigInteger(String.valueOf(getOrDefault(rsa, "rsaPub", RSA_PUBLIC_KEY.toString())));
37+
RSA_MODULUS = new BigInteger(String.valueOf(getOrDefault(rsa, "rsaModulus", RSA_MODULUS.toString())));
38+
USE_STATIC_DETAILS = getOrDefault(login, "useStaticCredentials", USE_STATIC_DETAILS);
39+
USERNAME = getOrDefault(login, "username", USERNAME);
40+
PASSWORD = getOrDefault(login, "password", PASSWORD);
41+
ROOFS_ENABLED = getOrDefault(game, "roofsEnabled", ROOFS_ENABLED);
42+
SHIFT_CLICK_MODIFIER = getOrDefault(game, "shiftClickModifier", SHIFT_CLICK_MODIFIER);
43+
SOUND_MUTED = getOrDefault(game, "soundMuted", SOUND_MUTED);
44+
FREE_TELEPORTS = getOrDefault(game, "freeTeleports", FREE_TELEPORTS);
45+
DEBUG_CONTEXT = getOrDefault(game, "debugContextMenu", DEBUG_CONTEXT);
46+
RESIZABLE = getOrDefault(game, "resizable", RESIZABLE);
47+
RENDER_FLAMES = getOrDefault(game, "renderFlames", RENDER_FLAMES);
48+
SERVER_DISPLAY_NAME = getOrDefault(obj, "serverDisplayName", SERVER_DISPLAY_NAME);
6149

6250
} catch (Exception e) {
63-
e.printStackTrace();
6451
System.out.println("Unable to load client config - using defaults.");
65-
Map<String, Object> net = new HashMap<String, Object>();
66-
67-
net.put("address", SERVER_ADDRESS);
68-
net.put("game_port", GAME_PORT);
52+
e.printStackTrace();
53+
writeDefaultConfig();
54+
}
55+
}
6956

70-
Map<String, Object> cache = new HashMap<String, Object>();
71-
cache.put("cacheDir", CACHE_NAME);
57+
private static Map<String, Object> getOrDefaultMap(Map<String, Object> map, String key) {
58+
Object value = map.get(key);
59+
return (value instanceof Map) ? (Map<String, Object>) value : new HashMap<>();
60+
}
7261

73-
Map<String, Object> rsa = new HashMap<String, Object>();
74-
rsa.put("rsaPub", RSA_PUBLIC_KEY);
75-
rsa.put("rsaModulus", RSA_MODULUS);
62+
private static <T> T getOrDefault(Map<String, Object> map, String key, T defaultValue) {
63+
Object value = map.get(key);
64+
if (value == null) {
65+
return defaultValue;
66+
}
67+
if (defaultValue instanceof Boolean) {
68+
return (T) Boolean.valueOf(String.valueOf(value));
69+
}
70+
if (defaultValue instanceof Integer) {
71+
return (T) Integer.valueOf(String.valueOf(value));
72+
}
73+
return (T) value;
74+
}
7675

77-
Map<String, Object> login = new HashMap<String, Object>();
78-
login.put("useStaticCredentials", USE_STATIC_DETAILS);
79-
login.put("username", USERNAME);
80-
login.put("password", PASSWORD);
76+
private static void writeDefaultConfig() {
77+
final File configFile = new File(clientConfigPath);
78+
Map<String, Object> clientConfig = new HashMap<>();
79+
80+
Map<String, Object> net = new HashMap<>();
81+
net.put("address", SERVER_ADDRESS);
82+
net.put("game_port", GAME_PORT);
83+
84+
Map<String, Object> cache = new HashMap<>();
85+
cache.put("cacheDir", CACHE_NAME);
86+
87+
Map<String, Object> rsa = new HashMap<>();
88+
rsa.put("rsaPub", RSA_PUBLIC_KEY.toString());
89+
rsa.put("rsaModulus", RSA_MODULUS.toString());
90+
91+
Map<String, Object> login = new HashMap<>();
92+
login.put("useStaticCredentials", USE_STATIC_DETAILS);
93+
login.put("username", USERNAME);
94+
login.put("password", PASSWORD);
95+
96+
Map<String, Object> game = new HashMap<>();
97+
game.put("roofsEnabled", ROOFS_ENABLED);
98+
game.put("shiftClickModifier", SHIFT_CLICK_MODIFIER);
99+
game.put("freeTeleports", FREE_TELEPORTS);
100+
game.put("debugContextMenu", DEBUG_CONTEXT);
101+
game.put("soundMuted", SOUND_MUTED);
102+
game.put("resizable", RESIZABLE);
103+
game.put("renderFlames", RENDER_FLAMES);
104+
105+
clientConfig.put("serverDisplayName", SERVER_DISPLAY_NAME);
106+
clientConfig.put("net", net);
107+
clientConfig.put("cache", cache);
108+
clientConfig.put("rsa", rsa);
109+
clientConfig.put("login", login);
110+
clientConfig.put("game", game);
81111

82-
Map<String, Object> game = new HashMap<String, Object>();
83-
game.put("roofsEnabled", ROOFS_ENABLED);
84-
game.put("shiftClickModifier", SHIFT_CLICK_MODIFIER);
85-
game.put("freeTeleports", FREE_TELEPORTS);
86-
game.put("debugContextMenu", DEBUG_CONTEXT);
87-
game.put("soundMuted", SOUND_MUTED);
88-
game.put("resizable", RESIZABLE);
89-
game.put("renderFlames", RENDER_FLAMES);
90-
91-
Map<String, Object> clientConfig = new HashMap<String, Object>();
92-
clientConfig.put("serverDisplayName", SERVER_DISPLAY_NAME);
93-
clientConfig.put("net", net);
94-
clientConfig.put("cache", cache);
95-
clientConfig.put("rsa", rsa);
96-
clientConfig.put("login", login);
97-
clientConfig.put("game", game);
98-
final DumperOptions options = new DumperOptions();
99-
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
100-
options.setPrettyFlow(true);
101-
Yaml yaml = new Yaml(options);
102-
try {
103-
FileWriter writer = new FileWriter(configFile);
104-
yaml.dump(clientConfig, writer);
105-
} catch (Exception writeExeption) {
106-
System.out.println("Failed to write default configuration to disk.");
107-
108-
}
112+
final DumperOptions options = new DumperOptions();
113+
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
114+
options.setPrettyFlow(true);
115+
Yaml yaml = new Yaml(options);
116+
try {
117+
FileWriter writer = new FileWriter(configFile);
118+
yaml.dump(clientConfig, writer);
119+
} catch (Exception writeException) {
120+
System.out.println("Failed to write default configuration to disk.");
109121
}
110122
}
111123

@@ -236,7 +248,7 @@ public static void setSoundMuted(boolean soundMuted) {
236248
final Map<String, Object> obj = yaml.load(inputStream);
237249
final Map<String, Object> game = (Map<String, Object>) obj.get("game");
238250
game.put("soundMuted", SOUND_MUTED);
239-
obj.put("game",game);
251+
obj.put("game", game);
240252
FileWriter writer = new FileWriter(configFile);
241253
yaml.dump(obj, writer);
242254

@@ -248,18 +260,18 @@ public static void setSoundMuted(boolean soundMuted) {
248260
}
249261

250262
public static RSString getUsername() {
251-
if(USE_STATIC_DETAILS) {
263+
if (USE_STATIC_DETAILS) {
252264
return RSString.CreateString(USERNAME);
253-
} else{
265+
} else {
254266
return Native.string_blank;
255267
}
256268
}
257269

258270

259271
public static RSString getPassword() {
260-
if(USE_STATIC_DETAILS) {
272+
if (USE_STATIC_DETAILS) {
261273
return RSString.CreateString(PASSWORD);
262-
} else{
274+
} else {
263275
return Native.string_blank;
264276
}
265277
}

0 commit comments

Comments
 (0)