diff --git a/build.gradle b/build.gradle index 786dc6599..88fbd3ba4 100644 --- a/build.gradle +++ b/build.gradle @@ -45,14 +45,17 @@ dependencies { implementation project(':slime-java-commons') implementation project(":solarxr-protocol") - implementation group: 'com.google.flatbuffers', name: 'flatbuffers-java', version: '2.0.3' - implementation group: 'commons-cli', name: 'commons-cli', version: '1.3.1' // This dependency is used internally, and not exposed to consumers on their own compile classpath. + implementation group: 'com.google.flatbuffers', name: 'flatbuffers-java', version: '2.0.3' + implementation group: 'commons-cli', name: 'commons-cli', version: '1.3.1' + implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.12.6.1' + implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: '2.13.3' + + implementation 'com.github.jonpeterson:jackson-module-model-versioning:1.2.2' implementation 'org.apache.commons:commons-math3:3.6.1' implementation 'org.apache.commons:commons-lang3:3.12.0' - implementation 'org.yaml:snakeyaml:1.30' implementation 'net.java.dev.jna:jna:5.10.0' implementation 'net.java.dev.jna:jna-platform:5.10.0' implementation 'com.illposed.osc:javaosc-core:0.8' diff --git a/solarxr-protocol b/solarxr-protocol index f54ac816e..40e3c5ef8 160000 --- a/solarxr-protocol +++ b/solarxr-protocol @@ -1 +1 @@ -Subproject commit f54ac816e8246893ca063bc50eaffbe97a85cdb2 +Subproject commit 40e3c5ef8431ec7c9b5ea0446a7fe3590bb7596f diff --git a/src/main/java/dev/slimevr/VRServer.java b/src/main/java/dev/slimevr/VRServer.java index 4865132b9..dfb7ed538 100644 --- a/src/main/java/dev/slimevr/VRServer.java +++ b/src/main/java/dev/slimevr/VRServer.java @@ -3,6 +3,7 @@ package dev.slimevr; import dev.slimevr.autobone.AutoBoneHandler; import dev.slimevr.bridge.Bridge; import dev.slimevr.bridge.VMCBridge; +import dev.slimevr.config.ConfigManager; import dev.slimevr.platform.windows.WindowsNamedPipeBridge; import dev.slimevr.poserecorder.BVHRecorder; import dev.slimevr.protocol.ProtocolAPI; @@ -18,12 +19,8 @@ import io.eiren.util.OperatingSystem; import io.eiren.util.ann.ThreadSafe; import io.eiren.util.ann.ThreadSecure; import io.eiren.util.collections.FastList; -import io.eiren.yaml.YamlException; -import io.eiren.yaml.YamlFile; -import io.eiren.yaml.YamlNode; import solarxr_protocol.datatypes.TrackerIdT; -import java.io.*; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.*; @@ -34,13 +31,11 @@ import java.util.function.Consumer; public class VRServer extends Thread { public final HumanPoseProcessor humanPoseProcessor; - public final YamlFile config = new YamlFile(); public final HMDTracker hmdTracker; private final List trackers = new FastList<>(); private final TrackersUDPServer trackersServer; private final List bridges = new FastList<>(); private final Queue tasks = new LinkedBlockingQueue<>(); - private final Map configuration = new HashMap<>(); private final List> newTrackersConsumers = new FastList<>(); private final List onTick = new FastList<>(); private final List shareTrackers; @@ -49,16 +44,21 @@ public class VRServer extends Thread { private final SerialHandler serialHandler; private final AutoBoneHandler autoBoneHandler; private final ProtocolAPI protocolAPI; - private final String configPath; + private final ConfigManager configManager; + + /** + * This function is used by VRWorkout, do not remove! + */ public VRServer() { this("vrconfig.yml"); } public VRServer(String configPath) { super("VRServer"); - this.configPath = configPath; - loadConfig(); + + this.configManager = new ConfigManager(configPath); + this.configManager.loadConfig(); deviceManager = new DeviceManager(this); @@ -81,6 +81,7 @@ public class VRServer extends Thread { // Create named pipe bridge for SteamVR driver WindowsNamedPipeBridge driverBridge = new WindowsNamedPipeBridge( + this, hmdTracker, "steamvr", "SteamVR Driver Bridge", @@ -93,6 +94,7 @@ public class VRServer extends Thread { // Create named pipe bridge for SteamVR input // TODO: how do we want to handle HMD input from the feeder app? WindowsNamedPipeBridge feederBridge = new WindowsNamedPipeBridge( + this, null, "steamvr_feeder", "SteamVR Feeder Bridge", @@ -144,34 +146,6 @@ public class VRServer extends Thread { return null; } - @ThreadSafe - public TrackerConfig getTrackerConfig(Tracker tracker) { - synchronized (configuration) { - TrackerConfig config = configuration.get(tracker.getName()); - if (config == null) { - config = new TrackerConfig(tracker); - configuration.put(tracker.getName(), config); - } - return config; - } - } - - private void loadConfig() { - try { - config.load(new FileInputStream(new File(this.configPath))); - } catch (FileNotFoundException e) { - // Config file didn't exist, is not an error - } catch (YamlException e) { - e.printStackTrace(); - } - List trackersConfig = config.getNodeList("trackers", null); - for (YamlNode node : trackersConfig) { - TrackerConfig cfg = new TrackerConfig(node); - synchronized (configuration) { - configuration.put(cfg.trackerName, cfg); - } - } - } public void addOnTick(Runnable runnable) { this.onTick.add(runnable); @@ -191,9 +165,8 @@ public class VRServer extends Thread { public void trackerUpdated(Tracker tracker) { queueTask(() -> { humanPoseProcessor.trackerUpdated(tracker); - TrackerConfig tc = getTrackerConfig(tracker); - tracker.saveConfig(tc); - saveConfig(); + this.getConfigManager().getVrConfig().writeTrackerConfig(tracker); + this.getConfigManager().saveConfig(); }); } @@ -204,40 +177,6 @@ public class VRServer extends Thread { }); } - @ThreadSafe - public synchronized void saveConfig() { - List nodes = config.getNodeList("trackers", null); - List> trackersConfig = new FastList<>(nodes.size()); - for (YamlNode node : nodes) { - trackersConfig.add(node.root); - } - config.setProperty("trackers", trackersConfig); - synchronized (configuration) { - Iterator iterator = configuration.values().iterator(); - while (iterator.hasNext()) { - TrackerConfig tc = iterator.next(); - Map cfg = null; - for (Map c : trackersConfig) { - if (tc.trackerName.equals(c.get("name"))) { - cfg = c; - break; - } - } - if (cfg == null) { - cfg = new HashMap<>(); - trackersConfig.add(cfg); - } - tc.saveConfig(new YamlNode(cfg)); - } - } - File cfgFile = new File(this.configPath); - try { - config.save(new FileOutputStream(cfgFile)); - } catch (IOException e) { - e.printStackTrace(); - } - } - @Override @VRServerThread public void run() { @@ -282,8 +221,7 @@ public class VRServer extends Thread { @ThreadSecure public void registerTracker(Tracker tracker) { - TrackerConfig config = getTrackerConfig(tracker); - tracker.loadConfig(config); + this.getConfigManager().getVrConfig().readTrackerConfig(tracker); queueTask(() -> { trackers.add(tracker); trackerAdded(tracker); @@ -293,22 +231,6 @@ public class VRServer extends Thread { }); } - public void updateTrackersFilters(TrackerFilters filter, float amount, int ticks) { - config.setProperty("filters.type", filter.name()); - config.setProperty("filters.amount", amount); - config.setProperty("filters.tickCount", ticks); - saveConfig(); - - IMUTracker imu; - for (Tracker t : this.getAllTrackers()) { - Tracker tracker = t.get(); - if (tracker instanceof IMUTracker) { - imu = (IMUTracker) tracker; - imu.setFilter(filter.name(), amount, ticks); - } - } - } - public void resetTrackers() { queueTask(() -> { humanPoseProcessor.resetTrackers(); @@ -376,4 +298,8 @@ public class VRServer extends Thread { public DeviceManager getDeviceManager() { return deviceManager; } + + public ConfigManager getConfigManager() { + return configManager; + } } diff --git a/src/main/java/dev/slimevr/autobone/AutoBone.java b/src/main/java/dev/slimevr/autobone/AutoBone.java index 2dc25a4c0..e84b54ea4 100644 --- a/src/main/java/dev/slimevr/autobone/AutoBone.java +++ b/src/main/java/dev/slimevr/autobone/AutoBone.java @@ -4,6 +4,7 @@ import com.jme3.math.FastMath; import com.jme3.math.Vector3f; import dev.slimevr.VRServer; import dev.slimevr.autobone.errors.*; +import dev.slimevr.config.AutoBoneConfig; import dev.slimevr.poserecorder.PoseFrameIO; import dev.slimevr.poserecorder.PoseFrameSkeleton; import dev.slimevr.poserecorder.PoseFrameTracker; @@ -82,41 +83,28 @@ public class AutoBone { ); protected final VRServer server; + public int cursorIncrement = 2; - public int minDataDistance = 1; - public int maxDataDistance = 1; - public int numEpochs = 100; - public float initialAdjustRate = 10f; - public float adjustRateMultiplier = 0.995f; // #region Error functions public SlideError slideError = new SlideError(); - public float slideErrorFactor = 0.0f; public OffsetSlideError offsetSlideError = new OffsetSlideError(); - public float offsetSlideErrorFactor = 1.0f; public FootHeightOffsetError footHeightOffsetError = new FootHeightOffsetError(); - public float footHeightOffsetErrorFactor = 0.0f; public BodyProportionError bodyProportionError = new BodyProportionError(); - public float bodyProportionErrorFactor = 0.2f; public HeightError heightError = new HeightError(); - public float heightErrorFactor = 0.0f; public PositionError positionError = new PositionError(); - public float positionErrorFactor = 0.0f; public PositionOffsetError positionOffsetError = new PositionOffsetError(); - public float positionOffsetErrorFactor = 0.0f; // #endregion public boolean randomizeFrameOrder = true; public boolean scaleEachStep = true; - public boolean calcInitError = false; - public float targetHeight = -1; // TODO hip tracker stuff... Hip tracker should be around 3 to 5 // centimeters. @@ -137,39 +125,12 @@ public class AutoBone { private final Random rand = new Random(); + private final AutoBoneConfig config; + public AutoBone(VRServer server) { + this.config = server.getConfigManager().getVrConfig().getAutoBone(); this.server = server; reloadConfigValues(); - - this.minDataDistance = server.config - .getInt("autobone.minimumDataDistance", this.minDataDistance); - this.maxDataDistance = server.config - .getInt("autobone.maximumDataDistance", this.maxDataDistance); - - this.numEpochs = server.config.getInt("autobone.epochCount", this.numEpochs); - - this.initialAdjustRate = server.config - .getFloat("autobone.adjustRate", this.initialAdjustRate); - this.adjustRateMultiplier = server.config - .getFloat("autobone.adjustRateMultiplier", this.adjustRateMultiplier); - - this.slideErrorFactor = server.config - .getFloat("autobone.slideErrorFactor", this.slideErrorFactor); - this.offsetSlideErrorFactor = server.config - .getFloat("autobone.offsetSlideErrorFactor", this.offsetSlideErrorFactor); - this.footHeightOffsetErrorFactor = server.config - .getFloat("autobone.offsetErrorFactor", this.footHeightOffsetErrorFactor); - this.bodyProportionErrorFactor = server.config - .getFloat("autobone.proportionErrorFactor", this.bodyProportionErrorFactor); - this.heightErrorFactor = server.config - .getFloat("autobone.heightErrorFactor", this.heightErrorFactor); - this.positionErrorFactor = server.config - .getFloat("autobone.positionErrorFactor", this.positionErrorFactor); - this.positionOffsetErrorFactor = server.config - .getFloat("autobone.positionOffsetErrorFactor", this.positionOffsetErrorFactor); - - this.calcInitError = server.config.getBoolean("autobone.calculateInitialError", true); - this.targetHeight = server.config.getFloat("autobone.manualTargetHeight", -1f); } // Mean square error function @@ -370,8 +331,8 @@ public class AutoBone { if (!applyConfig(skeletonConfig)) return false; - skeletonConfig.saveToConfig(server.config); - server.saveConfig(); + skeletonConfig.save(); + server.getConfigManager().saveConfig(); LogManager.info("[AutoBone] Configured skeleton bone lengths"); return true; @@ -523,12 +484,13 @@ public class AutoBone { } // Epoch loop, each epoch is one full iteration over the full dataset - for (int epoch = calcInitError ? -1 : 0; epoch < numEpochs; epoch++) { + for (int epoch = calcInitError ? -1 : 0; epoch < this.config.numEpochs; epoch++) { float sumError = 0f; int errorCount = 0; float adjustRate = epoch >= 0 - ? (initialAdjustRate * FastMath.pow(adjustRateMultiplier, epoch)) + ? (this.config.initialAdjustRate + * FastMath.pow(this.config.adjustRateMultiplier, epoch)) : 0f; int[] randomFrameIndices = null; @@ -555,7 +517,8 @@ public class AutoBone { // comparing frames a // certain number of frames apart for ( - int cursorOffset = minDataDistance; cursorOffset <= maxDataDistance + int cursorOffset = this.config.minDataDistance; + cursorOffset <= this.config.maxDataDistance && cursorOffset < frameCount; cursorOffset++ ) { @@ -730,7 +693,8 @@ public class AutoBone { applyConfig(legacyConfigs); if (epochCallback != null) { - epochCallback.accept(new Epoch(epoch + 1, numEpochs, avgError, legacyConfigs)); + epochCallback + .accept(new Epoch(epoch + 1, this.config.numEpochs, avgError, legacyConfigs)); } } @@ -750,42 +714,44 @@ public class AutoBone { float totalError = 0f; float sumWeight = 0f; - if (slideErrorFactor > 0f) { - totalError += slideError.getStepError(trainingStep) * slideErrorFactor; - sumWeight += slideErrorFactor; + if (this.config.slideErrorFactor > 0f) { + totalError += slideError.getStepError(trainingStep) * this.config.slideErrorFactor; + sumWeight += this.config.slideErrorFactor; } - if (offsetSlideErrorFactor > 0f) { - totalError += offsetSlideError.getStepError(trainingStep) * offsetSlideErrorFactor; - sumWeight += offsetSlideErrorFactor; + if (this.config.offsetSlideErrorFactor > 0f) { + totalError += offsetSlideError.getStepError(trainingStep) + * this.config.offsetSlideErrorFactor; + sumWeight += this.config.offsetSlideErrorFactor; } - if (footHeightOffsetErrorFactor > 0f) { + if (this.config.footHeightOffsetErrorFactor > 0f) { totalError += footHeightOffsetError.getStepError(trainingStep) - * footHeightOffsetErrorFactor; - sumWeight += footHeightOffsetErrorFactor; + * this.config.footHeightOffsetErrorFactor; + sumWeight += this.config.footHeightOffsetErrorFactor; } - if (bodyProportionErrorFactor > 0f) { + if (this.config.bodyProportionErrorFactor > 0f) { totalError += bodyProportionError.getStepError(trainingStep) - * bodyProportionErrorFactor; - sumWeight += bodyProportionErrorFactor; + * this.config.bodyProportionErrorFactor; + sumWeight += this.config.bodyProportionErrorFactor; } - if (heightErrorFactor > 0f) { - totalError += heightError.getStepError(trainingStep) * heightErrorFactor; - sumWeight += heightErrorFactor; + if (this.config.heightErrorFactor > 0f) { + totalError += heightError.getStepError(trainingStep) * this.config.heightErrorFactor; + sumWeight += this.config.heightErrorFactor; } - if (positionErrorFactor > 0f) { - totalError += positionError.getStepError(trainingStep) * positionErrorFactor; - sumWeight += positionErrorFactor; + if (this.config.positionErrorFactor > 0f) { + totalError += positionError.getStepError(trainingStep) + * this.config.positionErrorFactor; + sumWeight += this.config.positionErrorFactor; } - if (positionOffsetErrorFactor > 0f) { + if (this.config.positionOffsetErrorFactor > 0f) { totalError += positionOffsetError.getStepError(trainingStep) - * positionOffsetErrorFactor; - sumWeight += positionOffsetErrorFactor; + * this.config.positionOffsetErrorFactor; + sumWeight += this.config.positionOffsetErrorFactor; } return sumWeight > 0f ? totalError / sumWeight : 0f; @@ -872,6 +838,10 @@ public class AutoBone { return recordings; } + public AutoBoneConfig getConfig() { + return config; + } + public class Epoch { public final int epoch; diff --git a/src/main/java/dev/slimevr/autobone/AutoBoneHandler.java b/src/main/java/dev/slimevr/autobone/AutoBoneHandler.java index b2dc5c754..076cb1a30 100644 --- a/src/main/java/dev/slimevr/autobone/AutoBoneHandler.java +++ b/src/main/java/dev/slimevr/autobone/AutoBoneHandler.java @@ -88,11 +88,16 @@ public class AutoBoneHandler { private AutoBoneResults processFrames(PoseFrames frames) throws AutoBoneException { return autoBone - .processFrames(frames, autoBone.calcInitError, autoBone.targetHeight, (epoch) -> { - listeners.forEach(listener -> { - listener.onAutoBoneEpoch(epoch); - }); - }); + .processFrames( + frames, + autoBone.getConfig().calcInitError, + autoBone.getConfig().targetHeight, + (epoch) -> { + listeners.forEach(listener -> { + listener.onAutoBoneEpoch(epoch); + }); + } + ); } public boolean startProcessByType(AutoBoneProcessType processType) { @@ -143,8 +148,8 @@ public class AutoBoneHandler { announceProcessStatus(AutoBoneProcessType.RECORD, "Recording..."); // 1000 samples at 20 ms per sample is 20 seconds - int sampleCount = server.config.getInt("autobone.sampleCount", 1000); - long sampleRate = server.config.getLong("autobone.sampleRateMs", 20L); + int sampleCount = this.autoBone.getConfig().sampleCount; + long sampleRate = this.autoBone.getConfig().sampleRateMs; Future framesFuture = poseRecorder .startFrameRecording(sampleCount, sampleRate, progress -> { announceProcessStatus( @@ -156,7 +161,7 @@ public class AutoBoneHandler { PoseFrames frames = framesFuture.get(); LogManager.info("[AutoBone] Done recording!"); - if (server.config.getBoolean("autobone.saveRecordings", false)) { + if (this.autoBone.getConfig().saveRecordings) { announceProcessStatus(AutoBoneProcessType.RECORD, "Saving recording..."); autoBone.saveRecording(frames); } @@ -428,4 +433,5 @@ public class AutoBoneHandler { announceProcessStatus(AutoBoneProcessType.APPLY, "Adjusted values applied!", true, true); // TODO Update GUI values after applying? Is that needed here? } + } diff --git a/src/main/java/dev/slimevr/config/AutoBoneConfig.java b/src/main/java/dev/slimevr/config/AutoBoneConfig.java new file mode 100644 index 000000000..2c2f21ae1 --- /dev/null +++ b/src/main/java/dev/slimevr/config/AutoBoneConfig.java @@ -0,0 +1,28 @@ +package dev.slimevr.config; + +public class AutoBoneConfig { + + public int minDataDistance = 1; + public int maxDataDistance = 1; + public int numEpochs = 100; + public float initialAdjustRate = 10f; + public float adjustRateMultiplier = 0.995f; + public float slideErrorFactor = 0.0f; + public float offsetSlideErrorFactor = 1.0f; + public float footHeightOffsetErrorFactor = 0.0f; + public float bodyProportionErrorFactor = 0.2f; + public float heightErrorFactor = 0.0f; + public float positionErrorFactor = 0.0f; + public float positionOffsetErrorFactor = 0.0f; + public boolean calcInitError = false; + public float targetHeight = -1; + + public int sampleCount = 1000; + + public long sampleRateMs = 20; + + public boolean saveRecordings = false; + + public AutoBoneConfig() { + } +} diff --git a/src/main/java/dev/slimevr/config/BridgeConfig.java b/src/main/java/dev/slimevr/config/BridgeConfig.java new file mode 100644 index 000000000..90e8dbeae --- /dev/null +++ b/src/main/java/dev/slimevr/config/BridgeConfig.java @@ -0,0 +1,32 @@ +package dev.slimevr.config; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.databind.ser.std.StdKeySerializers; +import dev.slimevr.config.serializers.BooleanMapDeserializer; +import dev.slimevr.vr.trackers.TrackerRole; +import java.util.HashMap; +import java.util.Map; + + +public class BridgeConfig { + + @JsonDeserialize(using = BooleanMapDeserializer.class) + @JsonSerialize(keyUsing = StdKeySerializers.StringKeySerializer.class) + public Map trackers = new HashMap<>(); + + public BridgeConfig() { + } + + public boolean getBridgeTrackerRole(TrackerRole role, boolean def) { + return trackers.getOrDefault(role.name().toLowerCase(), def); + } + + public void setBridgeTrackerRole(TrackerRole role, boolean val) { + this.trackers.put(role.name().toLowerCase(), val); + } + + public Map getTrackers() { + return trackers; + } +} diff --git a/src/main/java/dev/slimevr/config/ConfigManager.java b/src/main/java/dev/slimevr/config/ConfigManager.java new file mode 100644 index 000000000..de3efe7cd --- /dev/null +++ b/src/main/java/dev/slimevr/config/ConfigManager.java @@ -0,0 +1,65 @@ +package dev.slimevr.config; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.github.jonpeterson.jackson.module.versioning.VersioningModule; +import com.jme3.math.Quaternion; +import dev.slimevr.config.serializers.QuaternionDeserializer; +import dev.slimevr.config.serializers.QuaternionSerializer; +import io.eiren.util.ann.ThreadSafe; + +import java.io.*; + + +public class ConfigManager { + + private final String configPath; + + private final ObjectMapper om; + + private VRConfig vrConfig; + + + public ConfigManager(String configPath) { + this.configPath = configPath; + om = new ObjectMapper(new YAMLFactory()); + om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + om.registerModule(new VersioningModule()); + SimpleModule quaternionModule = new SimpleModule(); + quaternionModule.addSerializer(Quaternion.class, new QuaternionSerializer()); + quaternionModule.addDeserializer(Quaternion.class, new QuaternionDeserializer()); + om.registerModule(quaternionModule); + } + + public void loadConfig() { + try { + this.vrConfig = om + .readValue(new FileInputStream(new File(configPath)), VRConfig.class); + } catch (FileNotFoundException e) { + // Config file didn't exist, is not an error + } catch (IOException e) { + throw new RuntimeException(e); + } + + if (this.vrConfig == null) { + this.vrConfig = new VRConfig(); + } + } + + @ThreadSafe + public synchronized void saveConfig() { + File cfgFile = new File(configPath); + + try { + om.writeValue(cfgFile, this.vrConfig); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public VRConfig getVrConfig() { + return vrConfig; + } +} diff --git a/src/main/java/dev/slimevr/config/CurrentVRConfigConverter.java b/src/main/java/dev/slimevr/config/CurrentVRConfigConverter.java new file mode 100644 index 000000000..a5bb31e8a --- /dev/null +++ b/src/main/java/dev/slimevr/config/CurrentVRConfigConverter.java @@ -0,0 +1,79 @@ +package dev.slimevr.config; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.github.jonpeterson.jackson.module.versioning.VersionedModelConverter; + +import java.util.Map; + + +public class CurrentVRConfigConverter implements VersionedModelConverter { + + @Override + public ObjectNode convert( + ObjectNode modelData, + String modelVersion, + String targetModelVersion, + JsonNodeFactory nodeFactory + ) { + + int version = Integer.parseInt(modelVersion); + + // Configs less than version 2 need a migration to the latest config + if (version < 2) { + // Move zoom to the window config + ObjectNode windowNode = (ObjectNode) modelData.get("window"); + ObjectNode zoomNode = (ObjectNode) modelData.get("zoom"); + if (windowNode != null && zoomNode != null) { + windowNode.set("zoom", zoomNode); + modelData.remove("zoom"); + } + + // Change trackers list to map + var trackersIter = modelData.withArray("trackers").iterator(); + ObjectNode trackersNode = nodeFactory.objectNode(); + while (trackersIter.hasNext()) { + JsonNode node = trackersIter.next(); + JsonNode resultNode = TrackerConfig.toV2(node, nodeFactory); + trackersNode.set(node.get("name").asText(), resultNode); + } + modelData.set("trackers", trackersNode); + + // Rename bridge to bridges + ObjectNode bridgeNode = (ObjectNode) modelData.get("bridge"); + if (bridgeNode != null) { + modelData.set("bridges", bridgeNode); + modelData.remove("bridge"); + } + + // Move body to skeleton (and merge it to current skeleton) + ObjectNode bodyNode = (ObjectNode) modelData.get("body"); + var bodyIter = bodyNode.fields(); + ObjectNode skeletonNode = (ObjectNode) modelData.get("skeleton"); + if (skeletonNode == null) { + skeletonNode = nodeFactory.objectNode(); + } + ObjectNode offsetsNode = nodeFactory.objectNode(); + while (bodyIter.hasNext()) { + Map.Entry node = bodyIter.next(); + // Filter only number values because other types would be stuff + // that didn't get migrated correctly before + if (node.getValue().isNumber()) { + offsetsNode.set(node.getKey(), node.getValue()); + } + } + // Fix calibration wolf typos + offsetsNode.set("shouldersWidth", bodyNode.get("shoulersWidth")); + offsetsNode.set("shouldersDistance", bodyNode.get("shoulersDistance")); + offsetsNode.remove("shoulersWidth"); + offsetsNode.remove("shoulersDistance"); + + skeletonNode.set("offsets", offsetsNode); + modelData.set("skeleton", skeletonNode); + modelData.remove("body"); + } + + return modelData; + } +} diff --git a/src/main/java/dev/slimevr/config/FiltersConfig.java b/src/main/java/dev/slimevr/config/FiltersConfig.java new file mode 100644 index 000000000..cc40bfaf6 --- /dev/null +++ b/src/main/java/dev/slimevr/config/FiltersConfig.java @@ -0,0 +1,57 @@ +package dev.slimevr.config; + +import dev.slimevr.Main; +import dev.slimevr.vr.trackers.IMUTracker; +import dev.slimevr.vr.trackers.Tracker; +import dev.slimevr.vr.trackers.TrackerFilters; + + +public class FiltersConfig { + + + private String type = "NONE"; + private float amount = 0.3f; + private int tickCount = 1; + + public FiltersConfig() { + } + + public void updateTrackersFilters(TrackerFilters filter, float amount, int ticks) { + setType(filter.name()); + setAmount(amount); + setTickCount(ticks); + + IMUTracker imu; + for (Tracker t : Main.vrServer.getAllTrackers()) { + Tracker tracker = t.get(); + if (tracker instanceof IMUTracker) { + imu = (IMUTracker) tracker; + imu.setFilter(filter.name(), amount, ticks); + } + } + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public float getAmount() { + return amount; + } + + public void setAmount(float amount) { + this.amount = amount; + } + + public int getTickCount() { + return tickCount; + } + + public void setTickCount(int tickCount) { + this.tickCount = tickCount; + } +} diff --git a/src/main/java/dev/slimevr/config/KeybindingsConfig.java b/src/main/java/dev/slimevr/config/KeybindingsConfig.java new file mode 100644 index 000000000..eb3f40482 --- /dev/null +++ b/src/main/java/dev/slimevr/config/KeybindingsConfig.java @@ -0,0 +1,19 @@ +package dev.slimevr.config; + +public class KeybindingsConfig { + + private String resetBinding = "CTRL+ALT+SHIFT+Y"; + + private String quickResetBinding = "CTRL+ALT+SHIFT+U"; + + public KeybindingsConfig() { + } + + public String getResetBinding() { + return resetBinding; + } + + public String getQuickResetBinding() { + return quickResetBinding; + } +} diff --git a/src/main/java/dev/slimevr/config/OverlayConfig.java b/src/main/java/dev/slimevr/config/OverlayConfig.java new file mode 100644 index 000000000..7f564e57d --- /dev/null +++ b/src/main/java/dev/slimevr/config/OverlayConfig.java @@ -0,0 +1,24 @@ +package dev.slimevr.config; + +public class OverlayConfig { + + private boolean isMirrored = false; + private boolean isVisible = false; + + + public boolean isMirrored() { + return isMirrored; + } + + public boolean isVisible() { + return isVisible; + } + + public void setMirrored(boolean mirrored) { + isMirrored = mirrored; + } + + public void setVisible(boolean visible) { + isVisible = visible; + } +} diff --git a/src/main/java/dev/slimevr/config/SkeletonConfig.java b/src/main/java/dev/slimevr/config/SkeletonConfig.java new file mode 100644 index 000000000..07ec694f3 --- /dev/null +++ b/src/main/java/dev/slimevr/config/SkeletonConfig.java @@ -0,0 +1,38 @@ +package dev.slimevr.config; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.databind.ser.std.StdKeySerializers; +import dev.slimevr.config.serializers.BooleanMapDeserializer; +import dev.slimevr.config.serializers.FloatMapDeserializer; + +import java.util.HashMap; +import java.util.Map; + + +public class SkeletonConfig { + + @JsonDeserialize(using = BooleanMapDeserializer.class) + @JsonSerialize(keyUsing = StdKeySerializers.StringKeySerializer.class) + public Map toggles = new HashMap<>(); + + @JsonDeserialize(using = FloatMapDeserializer.class) + @JsonSerialize(keyUsing = StdKeySerializers.StringKeySerializer.class) + public Map values = new HashMap<>(); + + @JsonDeserialize(using = FloatMapDeserializer.class) + @JsonSerialize(keyUsing = StdKeySerializers.StringKeySerializer.class) + public Map offsets = new HashMap<>(); + + public Map getToggles() { + return toggles; + } + + public Map getOffsets() { + return offsets; + } + + public Map getValues() { + return values; + } +} diff --git a/src/main/java/dev/slimevr/config/TrackerConfig.java b/src/main/java/dev/slimevr/config/TrackerConfig.java new file mode 100644 index 000000000..2992aeb94 --- /dev/null +++ b/src/main/java/dev/slimevr/config/TrackerConfig.java @@ -0,0 +1,83 @@ +package dev.slimevr.config; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.jme3.math.Quaternion; +import dev.slimevr.vr.trackers.Tracker; + + +public class TrackerConfig { + + private String customName; + private String designation; + private boolean hide; + + private Quaternion adjustment; + + private Quaternion mountingRotation; + + public TrackerConfig() { + } + + public TrackerConfig(Tracker tracker) { + this.designation = tracker.getBodyPosition() + != null ? tracker.getBodyPosition().designation : null; + this.customName = tracker.getCustomName(); + } + + static JsonNode toV2(JsonNode v1, JsonNodeFactory factory) { + ObjectNode node = factory.objectNode(); + if (v1.has("customName")) + node.set("customName", v1.get("customName")); + if (v1.has("designation")) + node.set("designation", v1.get("designation")); + if (v1.has("hide")) + node.set("hide", v1.get("hide")); + if (v1.has("mountingRotation")) + node.set("mountingRotation", v1.get("mountingRotation")); + if (v1.has("adjustment")) + node.set("adjustment", v1.get("adjustment")); + return node; + } + + public String getCustomName() { + return customName; + } + + public void setCustomName(String customName) { + this.customName = customName; + } + + public String getDesignation() { + return designation; + } + + public void setDesignation(String designation) { + this.designation = designation; + } + + public boolean isHide() { + return hide; + } + + public void setHide(boolean hide) { + this.hide = hide; + } + + public Quaternion getAdjustment() { + return adjustment; + } + + public void setAdjustment(Quaternion adjustment) { + this.adjustment = adjustment; + } + + public Quaternion getMountingRotation() { + return mountingRotation; + } + + public void setMountingRotation(Quaternion mountingRotation) { + this.mountingRotation = mountingRotation; + } +} diff --git a/src/main/java/dev/slimevr/config/VRConfig.java b/src/main/java/dev/slimevr/config/VRConfig.java new file mode 100644 index 000000000..7f28fb1d3 --- /dev/null +++ b/src/main/java/dev/slimevr/config/VRConfig.java @@ -0,0 +1,100 @@ +package dev.slimevr.config; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.databind.ser.std.StdKeySerializers; +import com.github.jonpeterson.jackson.module.versioning.JsonVersionedModel; +import dev.slimevr.config.serializers.BridgeConfigMapDeserializer; +import dev.slimevr.config.serializers.TrackerConfigMapDeserializer; +import dev.slimevr.vr.trackers.Tracker; + +import java.util.HashMap; +import java.util.Map; + + +@JsonVersionedModel( + currentVersion = "2", defaultDeserializeToVersion = "1", toCurrentConverterClass = CurrentVRConfigConverter.class +) +public class VRConfig { + + private WindowConfig window = new WindowConfig(); + + private FiltersConfig filters = new FiltersConfig(); + + private AutoBoneConfig autobone = new AutoBoneConfig(); + + private KeybindingsConfig keybindings = new KeybindingsConfig(); + + private SkeletonConfig skeleton = new SkeletonConfig(); + + @JsonDeserialize(using = TrackerConfigMapDeserializer.class) + @JsonSerialize(keyUsing = StdKeySerializers.StringKeySerializer.class) + private Map trackers = new HashMap<>(); + + @JsonDeserialize(using = BridgeConfigMapDeserializer.class) + @JsonSerialize(keyUsing = StdKeySerializers.StringKeySerializer.class) + private Map bridges = new HashMap<>(); + + private OverlayConfig overlay = new OverlayConfig(); + + public WindowConfig getWindow() { + return window; + } + + public FiltersConfig getFilters() { + return filters; + } + + public AutoBoneConfig getAutoBone() { + return autobone; + } + + public KeybindingsConfig getKeybindings() { + return keybindings; + } + + public Map getTrackers() { + return trackers; + } + + public Map getBridges() { + return bridges; + } + + public SkeletonConfig getSkeleton() { + return skeleton; + } + + public OverlayConfig getOverlay() { + return overlay; + } + + public TrackerConfig getTracker(Tracker tracker) { + TrackerConfig config = trackers.get(tracker.getName()); + if (config == null) { + config = new TrackerConfig(tracker); + trackers.put(tracker.getName(), config); + } + return config; + } + + public void readTrackerConfig(Tracker tracker) { + TrackerConfig config = getTracker(tracker); + tracker.readConfig(config); + } + + public void writeTrackerConfig(Tracker tracker) { + TrackerConfig tc = getTracker(tracker); + tracker.writeConfig(tc); + } + + public BridgeConfig getBrige(String bridgeKey) { + BridgeConfig config = bridges.get(bridgeKey); + if (config == null) { + config = new BridgeConfig(); + bridges.put(bridgeKey, config); + } + return config; + } +} + diff --git a/src/main/java/dev/slimevr/config/WindowConfig.java b/src/main/java/dev/slimevr/config/WindowConfig.java new file mode 100644 index 000000000..8c93f6bdd --- /dev/null +++ b/src/main/java/dev/slimevr/config/WindowConfig.java @@ -0,0 +1,61 @@ +package dev.slimevr.config; + + +public class WindowConfig { + + public static final float INITAL_ZOOM = 1.5f; + + private float zoom = INITAL_ZOOM; + + private int width = 800; + + private int height = 800; + + private int posx = -1; + private int posy = -1; + + public WindowConfig() { + } + + public float getZoom() { + return zoom; + } + + public void setZoom(float zoom) { + this.zoom = zoom; + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public int getPosx() { + return posx; + } + + public void setPosx(int posx) { + this.posx = posx; + } + + public int getPosy() { + return posy; + } + + public void setPosy(int posy) { + this.posy = posy; + } + + +} diff --git a/src/main/java/dev/slimevr/config/serializers/BooleanMapDeserializer.java b/src/main/java/dev/slimevr/config/serializers/BooleanMapDeserializer.java new file mode 100644 index 000000000..f4519126e --- /dev/null +++ b/src/main/java/dev/slimevr/config/serializers/BooleanMapDeserializer.java @@ -0,0 +1,12 @@ +package dev.slimevr.config.serializers; + +/** + * This class allows the use of the utility super class MapDeserializer that + * takes the Value of a map as its Generic parameter. It is so you can use that + * class in a @JsonDeserialize annotation on the Map field inside the config + * instance + * + * @see dev.slimevr.config.VRConfig + */ +public class BooleanMapDeserializer extends MapDeserializer { +} diff --git a/src/main/java/dev/slimevr/config/serializers/BridgeConfigMapDeserializer.java b/src/main/java/dev/slimevr/config/serializers/BridgeConfigMapDeserializer.java new file mode 100644 index 000000000..c6edb74bd --- /dev/null +++ b/src/main/java/dev/slimevr/config/serializers/BridgeConfigMapDeserializer.java @@ -0,0 +1,15 @@ +package dev.slimevr.config.serializers; + +import dev.slimevr.config.BridgeConfig; + + +/** + * This class allows the use of the utility super class MapDeserializer that + * takes the Value of a map as its Generic parameter. It is so you can use that + * class in a @JsonDeserialize annotation on the Map field inside the config + * instance + * + * @see dev.slimevr.config.VRConfig + */ +public class BridgeConfigMapDeserializer extends MapDeserializer { +} diff --git a/src/main/java/dev/slimevr/config/serializers/FloatMapDeserializer.java b/src/main/java/dev/slimevr/config/serializers/FloatMapDeserializer.java new file mode 100644 index 000000000..4d73d7115 --- /dev/null +++ b/src/main/java/dev/slimevr/config/serializers/FloatMapDeserializer.java @@ -0,0 +1,12 @@ +package dev.slimevr.config.serializers; + +/** + * This class allows the use of the utility super class MapDeserializer that + * takes the Value of a map as its Generic parameter. It is so you can use that + * class in a @JsonDeserialize annotation on the Map field inside the config + * instance + * + * @see dev.slimevr.config.VRConfig + */ +public class FloatMapDeserializer extends MapDeserializer { +} diff --git a/src/main/java/dev/slimevr/config/serializers/MapDeserializer.java b/src/main/java/dev/slimevr/config/serializers/MapDeserializer.java new file mode 100644 index 000000000..e7a6de74c --- /dev/null +++ b/src/main/java/dev/slimevr/config/serializers/MapDeserializer.java @@ -0,0 +1,49 @@ +package dev.slimevr.config.serializers; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.type.MapType; +import com.fasterxml.jackson.databind.type.TypeFactory; + +import java.io.IOException; +import java.lang.reflect.ParameterizedType; +import java.util.HashMap; + + +/** + * This class is an utility class that allows to write Map serializers easily to + * be used in the VRConfig (@see {@link dev.slimevr.config.VRConfig}) + * + * @see BooleanMapDeserializer to see how it is used + */ +public class MapDeserializer extends JsonDeserializer> { + + public MapDeserializer() { + super(); + } + + private Class getGenericTypeClass() { + try { + String className = ((ParameterizedType) getClass().getGenericSuperclass()) + .getActualTypeArguments()[0].getTypeName(); + Class clazz = Class.forName(className); + return (Class) clazz; + } catch (Exception e) { + throw new IllegalStateException( + "Class is not parametrized with generic type!!! Please use extends <> " + ); + } + } + + @Override + public HashMap deserialize(JsonParser p, DeserializationContext dc) + throws IOException, JsonProcessingException { + TypeFactory typeFactory = dc.getTypeFactory(); + MapType mapType = typeFactory + .constructMapType(HashMap.class, String.class, getGenericTypeClass()); + HashMap map = dc.readValue(p, mapType); + return map; + } +} diff --git a/src/main/java/dev/slimevr/config/serializers/QuaternionDeserializer.java b/src/main/java/dev/slimevr/config/serializers/QuaternionDeserializer.java new file mode 100644 index 000000000..912164331 --- /dev/null +++ b/src/main/java/dev/slimevr/config/serializers/QuaternionDeserializer.java @@ -0,0 +1,26 @@ +package dev.slimevr.config.serializers; + +import com.fasterxml.jackson.core.JacksonException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.jme3.math.Quaternion; +import com.fasterxml.jackson.databind.JsonDeserializer; + +import java.io.IOException; + + +public class QuaternionDeserializer extends JsonDeserializer { + @Override + public Quaternion deserialize(JsonParser p, DeserializationContext ctxt) + throws IOException, JacksonException { + JsonNode node = p.getCodec().readTree(p); + + return new Quaternion( + (float) node.get("x").asDouble(), + (float) node.get("y").asDouble(), + (float) node.get("z").asDouble(), + (float) node.get("w").asDouble() + ); + } +} diff --git a/src/main/java/dev/slimevr/config/serializers/QuaternionSerializer.java b/src/main/java/dev/slimevr/config/serializers/QuaternionSerializer.java new file mode 100644 index 000000000..04cd888c0 --- /dev/null +++ b/src/main/java/dev/slimevr/config/serializers/QuaternionSerializer.java @@ -0,0 +1,23 @@ +package dev.slimevr.config.serializers; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.jme3.math.Quaternion; + +import java.io.IOException; + + +public class QuaternionSerializer extends JsonSerializer { + + @Override + public void serialize(Quaternion value, JsonGenerator gen, SerializerProvider serializers) + throws IOException { + gen.writeStartObject(); + gen.writeNumberField("x", value.getX()); + gen.writeNumberField("y", value.getY()); + gen.writeNumberField("z", value.getZ()); + gen.writeNumberField("w", value.getW()); + gen.writeEndObject(); + } +} diff --git a/src/main/java/dev/slimevr/config/serializers/TrackerConfigMapDeserializer.java b/src/main/java/dev/slimevr/config/serializers/TrackerConfigMapDeserializer.java new file mode 100644 index 000000000..79c0f161c --- /dev/null +++ b/src/main/java/dev/slimevr/config/serializers/TrackerConfigMapDeserializer.java @@ -0,0 +1,15 @@ +package dev.slimevr.config.serializers; + +import dev.slimevr.config.TrackerConfig; + + +/** + * This class allows the use of the utility super class MapDeserializer that + * takes the Value of a map as its Generic parameter. It is so you can use that + * class in a @JsonDeserialize annotation on the Map field inside the config + * instance + * + * @see dev.slimevr.config.VRConfig + */ +public class TrackerConfigMapDeserializer extends MapDeserializer { +} diff --git a/src/main/java/dev/slimevr/gui/Keybinding.java b/src/main/java/dev/slimevr/gui/Keybinding.java index 7e70b3425..c2036461a 100644 --- a/src/main/java/dev/slimevr/gui/Keybinding.java +++ b/src/main/java/dev/slimevr/gui/Keybinding.java @@ -3,6 +3,7 @@ package dev.slimevr.gui; import com.melloware.jintellitype.HotkeyListener; import com.melloware.jintellitype.JIntellitype; import dev.slimevr.VRServer; +import dev.slimevr.config.KeybindingsConfig; import io.eiren.util.OperatingSystem; import io.eiren.util.ann.AWTThread; import io.eiren.util.logging.LogManager; @@ -13,10 +14,15 @@ public class Keybinding implements HotkeyListener { private static final int QUICK_RESET = 2; public final VRServer server; + + public final KeybindingsConfig config; + @AWTThread public Keybinding(VRServer server) { this.server = server; + this.config = server.getConfigManager().getVrConfig().getKeybindings(); + if (OperatingSystem.getCurrentPlatform() != OperatingSystem.WINDOWS) { LogManager .info( @@ -29,19 +35,11 @@ public class Keybinding implements HotkeyListener { if (JIntellitype.getInstance() instanceof JIntellitype) { JIntellitype.getInstance().addHotKeyListener(this); - String resetBinding = this.server.config.getString("keybindings.reset"); - if (resetBinding == null) { - resetBinding = "CTRL+ALT+SHIFT+Y"; - this.server.config.setProperty("keybindings.reset", resetBinding); - } + String resetBinding = this.config.getResetBinding(); JIntellitype.getInstance().registerHotKey(RESET, resetBinding); LogManager.info("[Keybinding] Bound reset to " + resetBinding); - String quickResetBinding = this.server.config.getString("keybindings.quickReset"); - if (quickResetBinding == null) { - quickResetBinding = "CTRL+ALT+SHIFT+U"; - this.server.config.setProperty("keybindings.quickReset", quickResetBinding); - } + String quickResetBinding = this.config.getQuickResetBinding(); JIntellitype.getInstance().registerHotKey(QUICK_RESET, quickResetBinding); LogManager.info("[Keybinding] Bound quick reset to " + quickResetBinding); } diff --git a/src/main/java/dev/slimevr/gui/SkeletonConfigGUI.java b/src/main/java/dev/slimevr/gui/SkeletonConfigGUI.java index ad17ee4a6..f9d6f79b5 100644 --- a/src/main/java/dev/slimevr/gui/SkeletonConfigGUI.java +++ b/src/main/java/dev/slimevr/gui/SkeletonConfigGUI.java @@ -116,8 +116,8 @@ public class SkeletonConfigGUI extends EJBagNoStretch { // Update config value float current = server.humanPoseProcessor.getSkeletonConfig(joint); server.humanPoseProcessor.setSkeletonConfig(joint, current + diff); - server.humanPoseProcessor.getSkeletonConfig().saveToConfig(server.config); - server.saveConfig(); + server.humanPoseProcessor.getSkeletonConfig().save(); + server.getConfigManager().saveConfig(); // Update GUI labels.get(joint).setText(getBoneLengthString(joint)); @@ -126,8 +126,8 @@ public class SkeletonConfigGUI extends EJBagNoStretch { private void reset(SkeletonConfigOffsets joint) { // Update config value server.humanPoseProcessor.resetSkeletonConfig(joint); - server.humanPoseProcessor.getSkeletonConfig().saveToConfig(server.config); - server.saveConfig(); + server.humanPoseProcessor.getSkeletonConfig().save(); + server.getConfigManager().saveConfig(); // Update GUI labels.get(joint).setText(getBoneLengthString(joint)); @@ -136,8 +136,8 @@ public class SkeletonConfigGUI extends EJBagNoStretch { private void resetAll() { // Update config value server.humanPoseProcessor.resetAllSkeletonConfigs(); - server.humanPoseProcessor.getSkeletonConfig().saveToConfig(server.config); - server.saveConfig(); + server.humanPoseProcessor.getSkeletonConfig().save(); + server.getConfigManager().saveConfig(); // Update GUI refreshAll(); diff --git a/src/main/java/dev/slimevr/gui/TrackersFiltersGUI.java b/src/main/java/dev/slimevr/gui/TrackersFiltersGUI.java index f040e03c3..68818e65f 100644 --- a/src/main/java/dev/slimevr/gui/TrackersFiltersGUI.java +++ b/src/main/java/dev/slimevr/gui/TrackersFiltersGUI.java @@ -3,7 +3,6 @@ package dev.slimevr.gui; import com.jme3.math.FastMath; import dev.slimevr.VRServer; import dev.slimevr.gui.swing.EJBagNoStretch; -import dev.slimevr.vr.trackers.TrackerFiltering; import dev.slimevr.vr.trackers.TrackerFilters; import io.eiren.util.StringUtils; @@ -32,7 +31,8 @@ public class TrackersFiltersGUI extends EJBagNoStretch { setAlignmentY(TOP_ALIGNMENT); add(Box.createVerticalStrut(10)); - filterType = TrackerFilters.valueOf(server.config.getString("filters.type", "NONE")); + filterType = TrackerFilters + .valueOf(server.getConfigManager().getVrConfig().getFilters().getType()); JComboBox filterSelect; add(filterSelect = new JComboBox<>(), s(c(0, row, 2), 4, 1)); @@ -46,7 +46,12 @@ public class TrackersFiltersGUI extends EJBagNoStretch { @Override public void actionPerformed(ActionEvent e) { filterType = TrackerFilters.valueOf(filterSelect.getSelectedItem().toString()); - server.updateTrackersFilters(filterType, filterAmount, filterTicks); + server + .getConfigManager() + .getVrConfig() + .getFilters() + .updateTrackersFilters(filterType, filterAmount, filterTicks); + server.getConfigManager().saveConfig(); } }); add(Box.createVerticalStrut(40)); @@ -54,11 +59,7 @@ public class TrackersFiltersGUI extends EJBagNoStretch { filterAmount = FastMath .clamp( - server.config - .getFloat( - TrackerFiltering.CONFIG_PREFIX + "amount", - TrackerFiltering.DEFAULT_INTENSITY - ), + server.getConfigManager().getVrConfig().getFilters().getAmount(), 0, 1 ); @@ -73,11 +74,7 @@ public class TrackersFiltersGUI extends EJBagNoStretch { row++; filterTicks = (int) FastMath .clamp( - server.config - .getInt( - TrackerFiltering.CONFIG_PREFIX + "tickCount", - TrackerFiltering.DEFAULT_TICK - ), + server.getConfigManager().getVrConfig().getFilters().getTickCount(), 0, 80 ); @@ -105,7 +102,12 @@ public class TrackersFiltersGUI extends EJBagNoStretch { ticksLabel.setText((StringUtils.prettyNumber(filterTicks))); } - server.updateTrackersFilters(filterType, filterAmount, filterTicks); + server + .getConfigManager() + .getVrConfig() + .getFilters() + .updateTrackersFilters(filterType, filterAmount, filterTicks); + server.getConfigManager().saveConfig(); } private class AdjButton extends JButton { diff --git a/src/main/java/dev/slimevr/gui/TrackersList.java b/src/main/java/dev/slimevr/gui/TrackersList.java index 407500461..4a9411981 100644 --- a/src/main/java/dev/slimevr/gui/TrackersList.java +++ b/src/main/java/dev/slimevr/gui/TrackersList.java @@ -4,6 +4,7 @@ import com.jme3.math.FastMath; import com.jme3.math.Quaternion; import com.jme3.math.Vector3f; import dev.slimevr.VRServer; +import dev.slimevr.config.TrackerConfig; import dev.slimevr.gui.swing.EJBagNoStretch; import dev.slimevr.gui.swing.EJBoxNoStretch; import dev.slimevr.vr.trackers.*; @@ -172,7 +173,7 @@ public class TrackersList extends EJBoxNoStretch { row++; if (t.userEditable()) { - TrackerConfig cfg = server.getTrackerConfig(t); + TrackerConfig cfg = server.getConfigManager().getVrConfig().getTracker(t); JComboBox desSelect; add( desSelect = new JComboBox<>(), @@ -182,9 +183,9 @@ public class TrackersList extends EJBoxNoStretch { for (TrackerPosition p : TrackerPosition.values) { desSelect.addItem(p.name()); } - if (cfg.designation != null) { + if (cfg.getDesignation() != null) { TrackerPosition - .getByDesignation(cfg.designation) + .getByDesignation(cfg.getDesignation()) .ifPresentOrElse( trackerPosition -> desSelect.setSelectedItem(trackerPosition.name()), () -> desSelect.setSelectedItem("NONE") diff --git a/src/main/java/dev/slimevr/gui/VRServerGUI.java b/src/main/java/dev/slimevr/gui/VRServerGUI.java index 627281eb9..19e3d6850 100644 --- a/src/main/java/dev/slimevr/gui/VRServerGUI.java +++ b/src/main/java/dev/slimevr/gui/VRServerGUI.java @@ -2,6 +2,7 @@ package dev.slimevr.gui; import dev.slimevr.Main; import dev.slimevr.VRServer; +import dev.slimevr.config.WindowConfig; import dev.slimevr.gui.swing.ButtonTimer; import dev.slimevr.gui.swing.EJBagNoStretch; import dev.slimevr.gui.swing.EJBox; @@ -41,12 +42,15 @@ public class VRServerGUI extends JFrame { private final SkeletonList skeletonList; private final EJBox pane; private JButton resetButton; - private float zoom = 1.5f; - private float initZoom = zoom; + + private WindowConfig config; @AWTThread public VRServerGUI(VRServer server) { super(TITLE); + + this.config = server.getConfigManager().getVrConfig().getWindow(); + try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { @@ -72,9 +76,7 @@ public class VRServerGUI extends JFrame { this.server = server; - this.zoom = server.config.getFloat("zoom", zoom); - this.initZoom = zoom; - setDefaultFontSize(zoom); + setDefaultFontSize(config.getZoom()); // All components should be constructed to the current zoom level by // default @@ -98,16 +100,24 @@ public class VRServerGUI extends JFrame { Rectangle screenBounds = gc.getBounds(); setMinimumSize(new Dimension(100, 100)); setSize( - Math.min(server.config.getInt("window.width", 800), screenBounds.width), - Math.min(server.config.getInt("window.height", 800), screenBounds.height) - ); - setLocation( - server.config - .getInt("window.posx", screenBounds.x + (screenBounds.width - getSize().width) / 2), - screenBounds.y - + server.config.getInt("window.posy", (screenBounds.height - getSize().height) / 2) + Math.min(config.getWidth(), screenBounds.width), + Math.min(config.getHeight(), screenBounds.height) ); + int posx = config.getPosx(); + if (posx == -1) { + posx = screenBounds.x + (screenBounds.width - getSize().width) / 2; + config.setPosx(posx); + } + + int posy = config.getPosy(); + if (posy == -1) { + posy = (screenBounds.height - getSize().height) / 2; + config.setPosy(posy); + } + + setLocation(posx, posy); + // Resize and close listeners to save position and size betwen launcher // starts addComponentListener(new AbstractComponentListener() { @@ -154,15 +164,11 @@ public class VRServerGUI extends JFrame { protected void saveFrameInfo() { Rectangle b = getBounds(); - server.config.setProperty("window.width", b.width); - server.config.setProperty("window.height", b.height); - server.config.setProperty("window.posx", b.x); - server.config.setProperty("window.posy", b.y); - server.saveConfig(); - } - - public float getZoom() { - return this.zoom; + config.setWidth(b.width); + config.setHeight(b.height); + config.setPosx(b.x); + config.setPosy(b.y); + server.getConfigManager().saveConfig(); } public void refresh() { @@ -225,17 +231,25 @@ public class VRServerGUI extends JFrame { } }); add(Box.createHorizontalGlue()); - add(new JButton("GUI Zoom (x" + StringUtils.prettyNumber(zoom, 2) + ")") { - { - addMouseListener(new MouseInputAdapter() { - @Override - public void mouseClicked(MouseEvent e) { - guiZoom(); - setText("GUI Zoom (x" + StringUtils.prettyNumber(zoom, 2) + ")"); - } - }); + add( + new JButton( + "GUI Zoom (x" + StringUtils.prettyNumber(config.getZoom(), 2) + ")" + ) { + { + addMouseListener(new MouseInputAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + guiZoom(); + setText( + "GUI Zoom (x" + + StringUtils.prettyNumber(config.getZoom(), 2) + + ")" + ); + } + }); + } } - }); + ); add(Box.createHorizontalStrut(10)); add(new JButton("WiFi") { { @@ -462,21 +476,20 @@ public class VRServerGUI extends JFrame { // in // the future too private void guiZoom() { - if (zoom <= 1.0f) { - zoom = 1.5f; - } else if (zoom <= 1.5f) { - zoom = 1.75f; - } else if (zoom <= 1.75f) { - zoom = 2.0f; - } else if (zoom <= 2.0f) { - zoom = 2.5f; + if (config.getZoom() <= 1.0f) { + config.setZoom(1.5f); + } else if (config.getZoom() <= 1.5f) { + config.setZoom(1.75f); + } else if (config.getZoom() <= 1.75f) { + config.setZoom(2f); + } else if (config.getZoom() <= 2.0f) { + config.setZoom(2.5f); } else { - zoom = 1.0f; + config.setZoom(1f); } - processNewZoom(zoom / initZoom, pane); + processNewZoom(config.getZoom() / WindowConfig.INITAL_ZOOM, pane); refresh(); - server.config.setProperty("zoom", zoom); - server.saveConfig(); + server.getConfigManager().saveConfig(); } @AWTThread diff --git a/src/main/java/dev/slimevr/platform/windows/WindowsNamedPipeBridge.java b/src/main/java/dev/slimevr/platform/windows/WindowsNamedPipeBridge.java index b1f3bf7b9..dc0ead0ae 100644 --- a/src/main/java/dev/slimevr/platform/windows/WindowsNamedPipeBridge.java +++ b/src/main/java/dev/slimevr/platform/windows/WindowsNamedPipeBridge.java @@ -6,11 +6,13 @@ import com.sun.jna.platform.win32.WinBase; import com.sun.jna.platform.win32.WinError; import com.sun.jna.ptr.IntByReference; import dev.slimevr.Main; +import dev.slimevr.VRServer; import dev.slimevr.bridge.BridgeThread; import dev.slimevr.bridge.PipeState; import dev.slimevr.bridge.ProtobufBridge; import dev.slimevr.bridge.ProtobufMessages.ProtobufMessage; import dev.slimevr.bridge.ProtobufMessages.TrackerAdded; +import dev.slimevr.config.BridgeConfig; import dev.slimevr.util.ann.VRServerThread; import dev.slimevr.vr.Device; import dev.slimevr.vr.trackers.*; @@ -31,7 +33,10 @@ public class WindowsNamedPipeBridge extends ProtobufBridge implements private final List shareableTrackers; protected WindowsPipe pipe; + private final BridgeConfig config; + public WindowsNamedPipeBridge( + VRServer server, HMDTracker hmd, String bridgeSettingsKey, String bridgeName, @@ -43,6 +48,7 @@ public class WindowsNamedPipeBridge extends ProtobufBridge implements this.bridgeSettingsKey = bridgeSettingsKey; this.runnerThread = new Thread(this, "Named pipe thread"); this.shareableTrackers = shareableTrackers; + this.config = server.getConfigManager().getVrConfig().getBrige(bridgeSettingsKey); } @Override @@ -51,22 +57,14 @@ public class WindowsNamedPipeBridge extends ProtobufBridge implements for (TrackerRole role : defaultRoles) { changeShareSettings( role, - Main.vrServer.config - .getBoolean( - "bridge." + bridgeSettingsKey + ".trackers." + role.name().toLowerCase(), - true - ) + this.config.getBridgeTrackerRole(role, true) ); } for (ShareableTracker tr : shareableTrackers) { TrackerRole role = tr.getTrackerRole(); changeShareSettings( role, - Main.vrServer.config - .getBoolean( - "bridge." + bridgeSettingsKey + ".trackers." + role.name().toLowerCase(), - false - ) + this.config.getBridgeTrackerRole(role, false) ); } runnerThread.start(); @@ -93,12 +91,8 @@ public class WindowsNamedPipeBridge extends ProtobufBridge implements } else { removeSharedTracker(tr); } - Main.vrServer.config - .setProperty( - "bridge." + bridgeSettingsKey + ".trackers." + role.name().toLowerCase(), - share - ); - Main.vrServer.saveConfig(); + config.setBridgeTrackerRole(role, share); + Main.vrServer.getConfigManager().saveConfig(); } } } diff --git a/src/main/java/dev/slimevr/poserecorder/PoseFrameTracker.java b/src/main/java/dev/slimevr/poserecorder/PoseFrameTracker.java index 9dbf315b4..09472e0f8 100644 --- a/src/main/java/dev/slimevr/poserecorder/PoseFrameTracker.java +++ b/src/main/java/dev/slimevr/poserecorder/PoseFrameTracker.java @@ -2,9 +2,9 @@ package dev.slimevr.poserecorder; import com.jme3.math.Quaternion; import com.jme3.math.Vector3f; +import dev.slimevr.config.TrackerConfig; import dev.slimevr.vr.Device; import dev.slimevr.vr.trackers.Tracker; -import dev.slimevr.vr.trackers.TrackerConfig; import dev.slimevr.vr.trackers.TrackerPosition; import dev.slimevr.vr.trackers.TrackerStatus; import io.eiren.util.collections.FastList; @@ -173,14 +173,14 @@ public class PoseFrameTracker implements Tracker, Iterable { } @Override - public void loadConfig(TrackerConfig config) { + public void readConfig(TrackerConfig config) { throw new UnsupportedOperationException( "PoseFrameTracker does not implement configuration" ); } @Override - public void saveConfig(TrackerConfig config) { + public void writeConfig(TrackerConfig config) { throw new UnsupportedOperationException( "PoseFrameTracker does not implement configuration" ); diff --git a/src/main/java/dev/slimevr/poserecorder/TrackerFrame.java b/src/main/java/dev/slimevr/poserecorder/TrackerFrame.java index 60b85c0a6..cfdf9fd88 100644 --- a/src/main/java/dev/slimevr/poserecorder/TrackerFrame.java +++ b/src/main/java/dev/slimevr/poserecorder/TrackerFrame.java @@ -2,9 +2,9 @@ package dev.slimevr.poserecorder; import com.jme3.math.Quaternion; import com.jme3.math.Vector3f; +import dev.slimevr.config.TrackerConfig; import dev.slimevr.vr.Device; import dev.slimevr.vr.trackers.Tracker; -import dev.slimevr.vr.trackers.TrackerConfig; import dev.slimevr.vr.trackers.TrackerPosition; import dev.slimevr.vr.trackers.TrackerStatus; @@ -116,12 +116,12 @@ public final class TrackerFrame implements Tracker { } @Override - public void loadConfig(TrackerConfig config) { + public void readConfig(TrackerConfig config) { throw new UnsupportedOperationException("TrackerFrame does not implement configuration"); } @Override - public void saveConfig(TrackerConfig config) { + public void writeConfig(TrackerConfig config) { throw new UnsupportedOperationException("TrackerFrame does not implement configuration"); } diff --git a/src/main/java/dev/slimevr/protocol/RPCHandler.java b/src/main/java/dev/slimevr/protocol/RPCHandler.java index 33fa349aa..664c8f306 100644 --- a/src/main/java/dev/slimevr/protocol/RPCHandler.java +++ b/src/main/java/dev/slimevr/protocol/RPCHandler.java @@ -6,6 +6,7 @@ import com.jme3.math.Quaternion; import dev.slimevr.autobone.AutoBone.Epoch; import dev.slimevr.autobone.AutoBoneListener; import dev.slimevr.autobone.AutoBoneProcessType; +import dev.slimevr.config.FiltersConfig; import dev.slimevr.platform.windows.WindowsNamedPipeBridge; import dev.slimevr.poserecorder.PoseFrames; import dev.slimevr.serial.SerialListener; @@ -119,7 +120,7 @@ public class RPCHandler extends ProtocolHandler return; this.api.server.humanPoseProcessor.getSkeletonConfig().resetConfigs(); - this.api.server.saveConfig(); + this.api.server.getConfigManager().saveConfig(); // might not be a good idea maybe let the client ask again FlatBufferBuilder fbb = new FlatBufferBuilder(300); @@ -154,8 +155,8 @@ public class RPCHandler extends ProtocolHandler SkeletonConfigOffsets joint = SkeletonConfigOffsets.getById(req.bone()); this.api.server.humanPoseProcessor.setSkeletonConfig(joint, req.value()); - this.api.server.humanPoseProcessor.getSkeletonConfig().saveToConfig(this.api.server.config); - this.api.server.saveConfig(); + this.api.server.humanPoseProcessor.getSkeletonConfig().save(); + this.api.server.getConfigManager().saveConfig(); } public void onRecordBVHRequest(GenericConnection conn, RpcMessageHeader messageHeader) { @@ -248,24 +249,20 @@ public class RPCHandler extends ProtocolHandler && bridge.getShareSetting(TrackerRole.RIGHT_ELBOW) ); + FiltersConfig filtersConfig = this.api.server + .getConfigManager() + .getVrConfig() + .getFilters(); + int filterSettings = FilteringSettings .createFilteringSettings( fbb, TrackerFilters .valueOf( - this.api.server.config - .getString(TrackerFiltering.CONFIG_PREFIX + "type", "NONE") + filtersConfig.getType() ).id, - (int) (this.api.server.config - .getFloat( - TrackerFiltering.CONFIG_PREFIX + "amount", - TrackerFiltering.DEFAULT_INTENSITY - ) * 100), - this.api.server.config - .getInt( - TrackerFiltering.CONFIG_PREFIX + "tickCount", - TrackerFiltering.DEFAULT_TICK - ) + (int) (filtersConfig.getAmount() * 100), + filtersConfig.getTickCount() ); int modelSettings; @@ -323,11 +320,15 @@ public class RPCHandler extends ProtocolHandler TrackerFilters type = TrackerFilters.fromId(req.filtering().type()); if (type != null) { this.api.server + .getConfigManager() + .getVrConfig() + .getFilters() .updateTrackersFilters( type, req.filtering().intensity() / 100.0f, req.filtering().ticks() ); + this.api.server.getConfigManager().saveConfig(); } } @@ -413,8 +414,8 @@ public class RPCHandler extends ProtocolHandler } } - cfg.saveToConfig(this.api.server.config); - this.api.server.saveConfig(); + cfg.save(); + this.api.server.getConfigManager().saveConfig(); } } diff --git a/src/main/java/dev/slimevr/vr/processor/skeleton/HumanSkeleton.java b/src/main/java/dev/slimevr/vr/processor/skeleton/HumanSkeleton.java index e2e51070f..7d9fd283d 100644 --- a/src/main/java/dev/slimevr/vr/processor/skeleton/HumanSkeleton.java +++ b/src/main/java/dev/slimevr/vr/processor/skeleton/HumanSkeleton.java @@ -152,7 +152,7 @@ public class HumanSkeleton extends Skeleton implements SkeletonConfigCallback { ) { this(computedTrackers); setTrackersFromServer(server); - skeletonConfig.loadFromConfig(server.config); + skeletonConfig.loadFromConfig(server.getConfigManager()); } public HumanSkeleton( diff --git a/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfig.java b/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfig.java index 13cf4babe..1725d1352 100644 --- a/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfig.java +++ b/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfig.java @@ -1,8 +1,9 @@ package dev.slimevr.vr.processor.skeleton; import com.jme3.math.Vector3f; +import dev.slimevr.Main; +import dev.slimevr.config.ConfigManager; import io.eiren.util.logging.LogManager; -import io.eiren.yaml.YamlFile; import java.util.EnumMap; import java.util.Map; @@ -49,74 +50,6 @@ public class SkeletonConfig { } } - public SkeletonConfig( - Map configOffsets, - Map configToggles, - Map configValues, - boolean autoUpdateOffsets, - SkeletonConfigCallback callback - ) { - this.autoUpdateOffsets = autoUpdateOffsets; - this.callback = callback; - setConfigs(configOffsets, configToggles, configValues); - - callCallbackOnAll(true); - } - - public SkeletonConfig( - Map configOffsets, - Map configToggles, - Map configValues, - boolean autoUpdateOffsets - ) { - this(configOffsets, configToggles, configValues, autoUpdateOffsets, null); - } - - public SkeletonConfig( - SkeletonConfig skeletonConfig, - boolean autoUpdateOffsets, - SkeletonConfigCallback callback - ) { - this.autoUpdateOffsets = autoUpdateOffsets; - this.callback = callback; - setConfigs(skeletonConfig); - - callCallbackOnAll(true); - } - - public SkeletonConfig(SkeletonConfig skeletonConfig, boolean autoUpdateOffsets) { - this(skeletonConfig, autoUpdateOffsets, null); - } - - // #region Cast utilities for config reading - private static Float castFloat(Object o) { - if (o == null) { - return null; - } else if (o instanceof Float) { - return (Float) o; - } else if (o instanceof Double) { - return ((Double) o).floatValue(); - } else if (o instanceof Byte) { - return (float) (Byte) o; - } else if (o instanceof Integer) { - return (float) (Integer) o; - } else if (o instanceof Long) { - return (float) (Long) o; - } else { - return null; - } - } - - private static Boolean castBoolean(Object o) { - if (o == null) { - return null; - } else if (o instanceof Boolean) { - return (Boolean) o; - } else { - return null; - } - } - private void callCallbackOnAll(boolean defaultOnly) { if (callback == null) { return; @@ -188,28 +121,15 @@ public class SkeletonConfig { return setOffset(config, newValue, true); } - public Float setOffset(String config, Float newValue) { - return setOffset(SkeletonConfigOffsets.getByStringValue(config), newValue); - } - public float getOffset(SkeletonConfigOffsets config) { if (config == null) { return 0f; } - // IMPORTANT!! This null check is necessary, getOrDefault seems to - // randomly decide to return null at times, so this is a secondary check - Float val = configOffsets.getOrDefault(config, config.defaultValue); + Float val = configOffsets.get(config); return val != null ? val : config.defaultValue; } - public float getOffset(String config) { - if (config == null) { - return 0f; - } - return getOffset(SkeletonConfigOffsets.getByStringValue(config)); - } - public Boolean setToggle(SkeletonConfigToggles config, Boolean newValue) { Boolean origVal = newValue != null ? configToggles.put(config, newValue) @@ -227,29 +147,15 @@ public class SkeletonConfig { return origVal; } - public Boolean setToggle(String config, Boolean newValue) { - return setToggle(SkeletonConfigToggles.getByStringValue(config), newValue); - } - public boolean getToggle(SkeletonConfigToggles config) { if (config == null) { return false; } - // IMPORTANT!! This null check is necessary, getOrDefault seems to - // randomly decide to return null at times, so this is a secondary check - Boolean val = configToggles.getOrDefault(config, config.defaultValue); + Boolean val = configToggles.get(config); return val != null ? val : config.defaultValue; } - public boolean getToggle(String config) { - if (config == null) { - return false; - } - - return getToggle(SkeletonConfigToggles.getByStringValue(config)); - } - public Float setValue(SkeletonConfigValues config, Float newValue) { Float origVal = newValue != null ? configValues.put(config, newValue) @@ -267,28 +173,15 @@ public class SkeletonConfig { return origVal; } - public Float setValue(String config, Float newValue) { - return setValue(SkeletonConfigValues.getByStringValue(config), newValue); - } - public float getValue(SkeletonConfigValues config) { if (config == null) { return 0f; } - // IMPORTANT!! This null check is necessary, getOrDefault seems to - // randomly decide to return null at times, so this is a secondary check - Float val = configValues.getOrDefault(config, config.defaultValue); + Float val = configValues.get(config); return val != null ? val : config.defaultValue; } - public float getValue(String config) { - if (config == null) { - return 0f; - } - return getValue(SkeletonConfigValues.getByStringValue(config)); - } - protected void setNodeOffset(BoneType nodeOffset, float x, float y, float z) { Vector3f offset = nodeOffsets.get(nodeOffset); @@ -308,19 +201,6 @@ public class SkeletonConfig { } } - protected void setNodeOffset(BoneType nodeOffset, Vector3f offset) { - if (offset == null) { - setNodeOffset(nodeOffset, 0f, 0f, 0f); - return; - } - - setNodeOffset(nodeOffset, offset.x, offset.y, offset.z); - } - - public Vector3f getNodeOffset(BoneType nodeOffset) { - return nodeOffsets.getOrDefault(nodeOffset, Vector3f.ZERO); - } - public void computeNodeOffset(BoneType nodeOffset) { switch (nodeOffset) { case HEAD: @@ -509,36 +389,6 @@ public class SkeletonConfig { } } - public void setStringConfigs( - Map configOffsets, - Map configToggles, - Map configValues - ) { - if (configOffsets != null) { - configOffsets.forEach((key, value) -> { - // Do not recalculate the offsets, these are done in bulk at the - // end - setOffset(SkeletonConfigOffsets.getByStringValue(key), value, false); - }); - } - - if (configToggles != null) { - configToggles.forEach((key, value) -> { - setToggle(SkeletonConfigToggles.getByStringValue(key), value); - }); - } - - if (configValues != null) { - configValues.forEach((key, value) -> { - setValue(SkeletonConfigValues.getByStringValue(key), value); - }); - } - - if (autoUpdateOffsets) { - computeAllNodeOffsets(); - } - } - public void setConfigs(SkeletonConfig skeletonConfig) { setConfigs( skeletonConfig.configOffsets, @@ -548,9 +398,14 @@ public class SkeletonConfig { } // #endregion - public void loadFromConfig(YamlFile config) { + public void loadFromConfig(ConfigManager configManager) { + for (SkeletonConfigOffsets configValue : SkeletonConfigOffsets.values) { - Float val = castFloat(config.getProperty(configValue.configKey)); + Float val = configManager + .getVrConfig() + .getSkeleton() + .getOffsets() + .get(configValue.configKey); if (val != null) { // Do not recalculate the offsets, these are done in bulk at the // end @@ -559,14 +414,22 @@ public class SkeletonConfig { } for (SkeletonConfigToggles configValue : SkeletonConfigToggles.values) { - Boolean val = castBoolean(config.getProperty(configValue.configKey)); + Boolean val = configManager + .getVrConfig() + .getSkeleton() + .getToggles() + .get(configValue.configKey); if (val != null) { setToggle(configValue, val); } } for (SkeletonConfigValues configValue : SkeletonConfigValues.values) { - Float val = castFloat(config.getProperty(configValue.configKey)); + Float val = configManager + .getVrConfig() + .getSkeleton() + .getValues() + .get(configValue.configKey); if (val != null) { setValue(configValue, val); } @@ -578,19 +441,20 @@ public class SkeletonConfig { } } - public void saveToConfig(YamlFile config) { + public void save() { + ConfigManager cm = Main.vrServer.getConfigManager(); // Write all possible values, this keeps configs consistent even if // defaults were changed for (SkeletonConfigOffsets value : SkeletonConfigOffsets.values) { - config.setProperty(value.configKey, getOffset(value)); + cm.getVrConfig().getSkeleton().getOffsets().put(value.configKey, getOffset(value)); } for (SkeletonConfigToggles value : SkeletonConfigToggles.values) { - config.setProperty(value.configKey, getToggle(value)); + cm.getVrConfig().getSkeleton().getToggles().put(value.configKey, getToggle(value)); } for (SkeletonConfigValues value : SkeletonConfigValues.values) { - config.setProperty(value.configKey, getValue(value)); + cm.getVrConfig().getSkeleton().getValues().put(value.configKey, getValue(value)); } } diff --git a/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfigOffsets.java b/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfigOffsets.java index c2dfdbee5..0d290bb6c 100644 --- a/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfigOffsets.java +++ b/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfigOffsets.java @@ -134,7 +134,7 @@ public enum SkeletonConfigOffsets { SHOULDERS_DISTANCE( 16, "Shoulders distance", - "shoulersDistance", + "shouldersDistance", "Shoulders distance", 0.08f, new BoneType[] { BoneType.LEFT_SHOULDER, BoneType.RIGHT_SHOULDER } @@ -142,7 +142,7 @@ public enum SkeletonConfigOffsets { SHOULDERS_WIDTH( 17, "Shoulders width", - "shoulersWidth", + "shouldersWidth", "Shoulders width", 0.36f, new BoneType[] { BoneType.LEFT_SHOULDER, BoneType.RIGHT_SHOULDER } @@ -165,7 +165,6 @@ public enum SkeletonConfigOffsets { ),; public static final SkeletonConfigOffsets[] values = values(); - private static final String CONFIG_PREFIX = "body."; private static final Map byStringVal = new HashMap<>(); private static final Map byIdVal = new HashMap<>(); @@ -193,7 +192,7 @@ public enum SkeletonConfigOffsets { ) { this.id = id; this.stringVal = stringVal; - this.configKey = CONFIG_PREFIX + configKey; + this.configKey = configKey; this.label = label; this.defaultValue = defaultValue; diff --git a/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfigToggles.java b/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfigToggles.java index 4d16c391f..88b94e981 100644 --- a/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfigToggles.java +++ b/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfigToggles.java @@ -12,7 +12,6 @@ public enum SkeletonConfigToggles { FORCE_ARMS_FROM_HMD(4, "Force arms from HMD", "forceArmsFromHMD", false),; public static final SkeletonConfigToggles[] values = values(); - public static final String CONFIG_PREFIX = "skeleton.toggles."; private static final Map byStringVal = new HashMap<>(); private static final Map byIdVal = new HashMap<>(); @@ -32,7 +31,7 @@ public enum SkeletonConfigToggles { SkeletonConfigToggles(int id, String stringVal, String configKey, boolean defaultValue) { this.id = id; this.stringVal = stringVal; - this.configKey = CONFIG_PREFIX + configKey; + this.configKey = configKey; this.defaultValue = defaultValue; } diff --git a/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfigValues.java b/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfigValues.java index e8c534da4..24301c6b4 100644 --- a/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfigValues.java +++ b/src/main/java/dev/slimevr/vr/processor/skeleton/SkeletonConfigValues.java @@ -15,7 +15,6 @@ public enum SkeletonConfigValues { // @formatter:on public static final SkeletonConfigValues[] values = values(); - public static final String CONFIG_PREFIX = "skeleton.values."; private static final Map byStringVal = new HashMap<>(); private static final Map byIdVal = new HashMap<>(); @@ -40,7 +39,7 @@ public enum SkeletonConfigValues { ) { this.id = id; this.stringVal = stringVal; - this.configKey = CONFIG_PREFIX + configKey; + this.configKey = configKey; this.defaultValue = defaultValue; } diff --git a/src/main/java/dev/slimevr/vr/trackers/ComputedTracker.java b/src/main/java/dev/slimevr/vr/trackers/ComputedTracker.java index 01c9f0cad..122d60c44 100644 --- a/src/main/java/dev/slimevr/vr/trackers/ComputedTracker.java +++ b/src/main/java/dev/slimevr/vr/trackers/ComputedTracker.java @@ -2,6 +2,7 @@ package dev.slimevr.vr.trackers; import com.jme3.math.Quaternion; import com.jme3.math.Vector3f; +import dev.slimevr.config.TrackerConfig; import dev.slimevr.vr.Device; @@ -39,18 +40,18 @@ public class ComputedTracker implements Tracker, TrackerWithTPS { } @Override - public void saveConfig(TrackerConfig config) { + public void writeConfig(TrackerConfig config) { config.setDesignation(bodyPosition == null ? null : bodyPosition.designation); } @Override - public void loadConfig(TrackerConfig config) { + public void readConfig(TrackerConfig config) { // Loading a config is an act of user editing, therefore it shouldn't // not be // allowed if editing is not allowed if (userEditable()) { TrackerPosition - .getByDesignation(config.designation) + .getByDesignation(config.getDesignation()) .ifPresent(trackerPosition -> bodyPosition = trackerPosition); } } diff --git a/src/main/java/dev/slimevr/vr/trackers/IMUTracker.java b/src/main/java/dev/slimevr/vr/trackers/IMUTracker.java index c668a02b7..3813f6a2c 100644 --- a/src/main/java/dev/slimevr/vr/trackers/IMUTracker.java +++ b/src/main/java/dev/slimevr/vr/trackers/IMUTracker.java @@ -4,6 +4,8 @@ import com.jme3.math.FastMath; import com.jme3.math.Quaternion; import com.jme3.math.Vector3f; import dev.slimevr.VRServer; +import dev.slimevr.config.FiltersConfig; +import dev.slimevr.config.TrackerConfig; import dev.slimevr.vr.Device; import dev.slimevr.vr.trackers.udp.TrackersUDPServer; import dev.slimevr.vr.trackers.udp.UDPDevice; @@ -69,40 +71,37 @@ public class IMUTracker } @Override - public void saveConfig(TrackerConfig config) { + public void writeConfig(TrackerConfig config) { config.setDesignation(bodyPosition == null ? null : bodyPosition.designation); - config.mountingRotation = mounting != null ? mounting : null; + config.setMountingRotation(mounting != null ? mounting : null); } @Override - public void loadConfig(TrackerConfig config) { + public void readConfig(TrackerConfig config) { // Loading a config is an act of user editing, therefore it shouldn't // not be // allowed if editing is not allowed if (userEditable()) { - setCustomName(config.customName); + setCustomName(config.getCustomName()); - if (config.mountingRotation != null) { - mounting = config.mountingRotation; - rotAdjust.set(config.mountingRotation); + if (config.getMountingRotation() != null) { + mounting = config.getMountingRotation(); + rotAdjust.set(config.getMountingRotation()); } else { rotAdjust.loadIdentity(); } TrackerPosition - .getByDesignation(config.designation) + .getByDesignation(config.getDesignation()) .ifPresent(trackerPosition -> bodyPosition = trackerPosition); + + FiltersConfig filtersConfig = this.vrserver + .getConfigManager() + .getVrConfig() + .getFilters(); setFilter( - vrserver.config.getString(TrackerFiltering.CONFIG_PREFIX + "type"), - vrserver.config - .getFloat( - TrackerFiltering.CONFIG_PREFIX + "amount", - TrackerFiltering.DEFAULT_INTENSITY - ), - vrserver.config - .getInt( - TrackerFiltering.CONFIG_PREFIX + "tickCount", - TrackerFiltering.DEFAULT_TICK - ) + filtersConfig.getType(), + filtersConfig.getAmount(), + filtersConfig.getTickCount() ); } } diff --git a/src/main/java/dev/slimevr/vr/trackers/ReferenceAdjustedTracker.java b/src/main/java/dev/slimevr/vr/trackers/ReferenceAdjustedTracker.java index 6428083f2..23a770bc6 100644 --- a/src/main/java/dev/slimevr/vr/trackers/ReferenceAdjustedTracker.java +++ b/src/main/java/dev/slimevr/vr/trackers/ReferenceAdjustedTracker.java @@ -2,6 +2,7 @@ package dev.slimevr.vr.trackers; import com.jme3.math.Quaternion; import com.jme3.math.Vector3f; +import dev.slimevr.config.TrackerConfig; import dev.slimevr.vr.Device; @@ -23,13 +24,13 @@ public class ReferenceAdjustedTracker implements Tracker { } @Override - public void loadConfig(TrackerConfig config) { - this.tracker.loadConfig(config); + public void readConfig(TrackerConfig config) { + this.tracker.readConfig(config); } @Override - public void saveConfig(TrackerConfig config) { - this.tracker.saveConfig(config); + public void writeConfig(TrackerConfig config) { + this.tracker.writeConfig(config); } /** diff --git a/src/main/java/dev/slimevr/vr/trackers/Tracker.java b/src/main/java/dev/slimevr/vr/trackers/Tracker.java index be012e5ca..85a52ebf5 100644 --- a/src/main/java/dev/slimevr/vr/trackers/Tracker.java +++ b/src/main/java/dev/slimevr/vr/trackers/Tracker.java @@ -2,6 +2,7 @@ package dev.slimevr.vr.trackers; import com.jme3.math.Quaternion; import com.jme3.math.Vector3f; +import dev.slimevr.config.TrackerConfig; import dev.slimevr.vr.Device; import java.util.concurrent.atomic.AtomicInteger; @@ -23,9 +24,9 @@ public interface Tracker { TrackerStatus getStatus(); - void loadConfig(TrackerConfig config); + void readConfig(TrackerConfig config); - void saveConfig(TrackerConfig config); + void writeConfig(TrackerConfig config); float getConfidenceLevel(); diff --git a/src/main/java/dev/slimevr/vr/trackers/TrackerConfig.java b/src/main/java/dev/slimevr/vr/trackers/TrackerConfig.java deleted file mode 100644 index fde1af543..000000000 --- a/src/main/java/dev/slimevr/vr/trackers/TrackerConfig.java +++ /dev/null @@ -1,97 +0,0 @@ -package dev.slimevr.vr.trackers; - -import com.jme3.math.Quaternion; -import io.eiren.yaml.YamlNode; - -import java.util.Objects; - - -public class TrackerConfig { - - public final String trackerName; - public final String customName; - public String designation; - public boolean hide; - public Quaternion adjustment; - public String oldMountingRotation; - public Quaternion mountingRotation; - - public TrackerConfig(Tracker tracker) { - this.trackerName = tracker.getName(); - this.designation = tracker.getBodyPosition() - != null ? tracker.getBodyPosition().designation : null; - this.customName = tracker.getCustomName(); - } - - public TrackerConfig(YamlNode node) { - this.trackerName = node.getString("name"); - this.customName = node.getString("customName"); - this.designation = node.getString("designation"); - this.hide = node.getBoolean("hide", false); - this.oldMountingRotation = node.getString("rotation"); - YamlNode mountingRotationNode = node.getNode("mountingRotation"); - if (mountingRotationNode != null) { - mountingRotation = new Quaternion( - mountingRotationNode.getFloat("x", 0), - mountingRotationNode.getFloat("y", 0), - mountingRotationNode.getFloat("z", 0), - mountingRotationNode.getFloat("w", 1) - ); - } - - if (oldMountingRotation != null) { - TrackerMountingRotation rot = TrackerMountingRotation.fromName(oldMountingRotation); - mountingRotation = Objects - .requireNonNullElse(rot, TrackerMountingRotation.FRONT).quaternion; - } - - YamlNode adjNode = node.getNode("adjustment"); - if (adjNode != null) { - adjustment = new Quaternion( - adjNode.getFloat("x", 0), - adjNode.getFloat("y", 0), - adjNode.getFloat("z", 0), - adjNode.getFloat("w", 0) - ); - } - } - - public void setDesignation(String newDesignation) { - this.designation = newDesignation; - } - - public void saveConfig(YamlNode configNode) { - configNode.setProperty("name", trackerName); - if (customName != null) { - configNode.setProperty("customName", customName); - } - if (designation != null) - configNode.setProperty("designation", designation); - else - configNode.removeProperty("designation"); - if (hide) - configNode.setProperty("hide", hide); - else - configNode.removeProperty("hide"); - if (adjustment != null) { - configNode.setProperty("adj.x", adjustment.getX()); - configNode.setProperty("adj.y", adjustment.getY()); - configNode.setProperty("adj.z", adjustment.getZ()); - configNode.setProperty("adj.w", adjustment.getW()); - } else { - configNode.removeProperty("adj"); - } - if (oldMountingRotation != null) { - configNode.removeProperty("rotation"); - } - - if (mountingRotation != null) { - configNode.setProperty("mountingRotation.x", mountingRotation.getX()); - configNode.setProperty("mountingRotation.y", mountingRotation.getY()); - configNode.setProperty("mountingRotation.z", mountingRotation.getZ()); - configNode.setProperty("mountingRotation.w", mountingRotation.getW()); - } else { - configNode.removeProperty("mountingRotation"); - } - } -} diff --git a/src/main/java/dev/slimevr/vr/trackers/TrackerFiltering.java b/src/main/java/dev/slimevr/vr/trackers/TrackerFiltering.java deleted file mode 100644 index 0b093a5a5..000000000 --- a/src/main/java/dev/slimevr/vr/trackers/TrackerFiltering.java +++ /dev/null @@ -1,12 +0,0 @@ -package dev.slimevr.vr.trackers; - -public class TrackerFiltering { - // TODO move initializing logic from IMUTracker to here so we don't have to - // load settings once per tracker. We can just cache them in this class. - // Also will make adding filtering to other types of trackers than - // IMUTracker much easier if we need to. -Erimel - - public static String CONFIG_PREFIX = "filters."; - public static float DEFAULT_INTENSITY = 0.3f; - public static int DEFAULT_TICK = 1; -}