-
-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathControllerSpecify.java
More file actions
73 lines (60 loc) · 2.44 KB
/
Copy pathControllerSpecify.java
File metadata and controls
73 lines (60 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package dev.isxander.controlify.hid;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import dev.isxander.controlify.platform.main.PlatformMainUtil;
import dev.isxander.controlify.utils.CUtil;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale;
public class ControllerSpecify {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static final Path CONFIG_PATH = PlatformMainUtil.getConfigDir().resolve("controlify-controllerspecify.json");
private static ControllerSpecify instance;
private String assignedControllerMac = "";
public static ControllerSpecify get() {
if (instance == null) {
instance = load();
}
return instance;
}
private static ControllerSpecify load() {
if (Files.exists(CONFIG_PATH)) {
try {
String json = Files.readString(CONFIG_PATH);
ControllerSpecify loaded = GSON.fromJson(json, ControllerSpecify.class);
if (loaded != null) {
CUtil.LOGGER.log("Loaded ControllerSpecify config: assignedControllerMac='{}'", loaded.assignedControllerMac);
return loaded;
}
} catch (IOException e) {
CUtil.LOGGER.error("Failed to read controlify-controllerspecify.json, using default", e);
}
}
ControllerSpecify fresh = new ControllerSpecify();
fresh.save();
CUtil.LOGGER.log("Created default controlify-controllerspecify.json at {} - set assignedControllerMac to restrict this instance to one controller", CONFIG_PATH);
return fresh;
}
public void save() {
try {
Files.writeString(CONFIG_PATH, GSON.toJson(this));
} catch (IOException e) {
CUtil.LOGGER.error("Failed to write controlify-controllerspecify.json", e);
}
}
public boolean isFilteringEnabled() {
return assignedControllerMac != null && !assignedControllerMac.isBlank();
}
public String getAssignedControllerMac() {
return assignedControllerMac;
}
public void setAssignedControllerMac(String mac) {
this.assignedControllerMac = mac;
save();
}
public static String normalizeMac(String mac) {
if (mac == null) return "";
return mac.replace(":", "").replace("-", "").trim().toLowerCase(Locale.ROOT);
}
}