Skip to content

Commit

Permalink
use cloth config api
Browse files Browse the repository at this point in the history
  • Loading branch information
ImUrX committed Sep 16, 2021
1 parent 8d9530e commit 306157d
Show file tree
Hide file tree
Showing 16 changed files with 92 additions and 60 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ repositories {
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
// See https://docs.gradle.org/current/userguide/declaring_repositories.html
// for more information about repositories.
maven { url "https://maven.shedaniel.me/" }
}

dependencies {
Expand All @@ -27,6 +28,9 @@ dependencies {
// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

modApi("me.shedaniel.cloth:cloth-config-fabric:${project.cloth_config_version}") {
exclude(group: "net.fabricmc.fabric-api")
}
// PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
// You may need to force-disable transitiveness on them.
}
Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ org.gradle.jvmargs=-Xmx1G
# Mod Properties
mod_version = 1.0.0
maven_group = io.github.imurx
archives_base_name = audioswitcher
archives_base_name = littletweaks

# Dependencies
fabric_version=0.40.1+1.17
cloth_config_version=5.0.38
10 changes: 10 additions & 0 deletions src/main/java/io/github/imurx/littletweaks/LittleConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.imurx.littletweaks;

import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.annotation.Config;

@Config(name = "littletweaks")
public class LittleConfig implements ConfigData {
public String preferredDevice = "";
public boolean useDefaultDevice = true;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.github.imurx.audioswitcher;
package io.github.imurx.littletweaks;

import io.github.imurx.audioswitcher.events.SoundSystemCallback;
import io.github.imurx.audioswitcher.mixin.SoundEngineAccessor;
import io.github.imurx.audioswitcher.mixin.SoundManagerAccessor;
import io.github.imurx.audioswitcher.mixin.SoundSystemAccessor;
import io.github.imurx.littletweaks.events.SoundSystemCallback;
import io.github.imurx.littletweaks.mixin.SoundEngineAccessor;
import io.github.imurx.littletweaks.mixin.SoundManagerAccessor;
import io.github.imurx.littletweaks.mixin.SoundSystemAccessor;
import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.autoconfig.serializer.Toml4jConfigSerializer;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.MinecraftClient;
Expand All @@ -13,17 +15,21 @@

import java.util.List;

public class AudioSwitcher implements ClientModInitializer {
public class LittleTweaks implements ClientModInitializer {
private int tickCounter = -1;
static private Thread thread;
static private Thread audioThread;
static public List<String> devices;
static public String defaultDevice = "";
static public String currentDevice = "";
static public String preferredDevice = "";
static public boolean useDefault = true;

@Override
public void onInitializeClient() {
AutoConfig.register(LittleConfig.class, Toml4jConfigSerializer::new);
this.audioSwitcher();

}

private void audioSwitcher() {
updateDevices();
ClientTickEvents.END_CLIENT_TICK.register((client) -> {
if(tickCounter < 0 || ++tickCounter < 40) return;
Expand All @@ -32,39 +38,51 @@ public void onInitializeClient() {
int connect = ALC11.alcGetInteger(accessor.getDevicePointer(), EXTDisconnect.ALC_CONNECTED);
updateDevices();

if(thread != null) {
if(audioThread != null) {
try {
thread.join();
audioThread.join();
} catch(InterruptedException ex) {
ex.printStackTrace();
}
thread = null;
audioThread = null;
return;
}

if(connect == ALC11.ALC_FALSE) {
thread = new Thread(() -> restartSoundSystem(defaultDevice));
} else if(!currentDevice.equals(preferredDevice) && devices.contains(preferredDevice)) {
thread = new Thread(() -> restartSoundSystem(preferredDevice));
audioThread = new Thread(() -> restartSoundSystem(defaultDevice));
} else if(!currentDevice.equals(getConfig().preferredDevice) && devices.contains(getConfig().preferredDevice)) {
audioThread = new Thread(() -> restartSoundSystem(getConfig().preferredDevice));
} else {
return;
}

thread.setName("AudioSwitcher");
thread.start();
audioThread.setName("LittleTweaks/AudioSwitcher");
audioThread.start();
});
SoundSystemCallback.STARTED_SYSTEM.register((_x) -> tickCounter = 0);
SoundSystemCallback.STOPPING_SYSTEM.register((_x) -> tickCounter = -1);
}

private void screenshotClipboard() {

}

static public void updateDevices() {
devices = ALUtil.getStringList(0, ALC11.ALC_ALL_DEVICES_SPECIFIER);
defaultDevice = ALC11.alcGetString(0, ALC11.ALC_DEFAULT_ALL_DEVICES_SPECIFIER);
}

static public void restartSoundSystem(String device) {
SoundSystem soundSystem = ((SoundManagerAccessor) MinecraftClient.getInstance().getSoundManager()).getSoundSystem();
soundSystem.stop();
currentDevice = device;
((SoundSystemAccessor) soundSystem).callStart();
}

static public LittleConfig getConfig() {
return AutoConfig.getConfigHolder(LittleConfig.class).getConfig();
}
static public void saveConfig() {
AutoConfig.getConfigHolder(LittleConfig.class).save();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.imurx.audioswitcher;
package io.github.imurx.littletweaks;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.widget.ButtonWidget;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.imurx.audioswitcher.events;
package io.github.imurx.littletweaks.events;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.imurx.audioswitcher.mixin;
package io.github.imurx.littletweaks.mixin;

import net.minecraft.client.sound.AlUtil;
import org.spongepowered.asm.mixin.Mixin;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.imurx.audioswitcher.mixin;
package io.github.imurx.littletweaks.mixin;

import net.minecraft.client.sound.SoundEngine;
import org.spongepowered.asm.mixin.Mixin;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package io.github.imurx.audioswitcher.mixin;
package io.github.imurx.littletweaks.mixin;

import io.github.imurx.audioswitcher.AudioSwitcher;
import net.minecraft.client.sound.AlUtil;
import io.github.imurx.littletweaks.LittleTweaks;
import net.minecraft.client.sound.SoundEngine;
import org.lwjgl.openal.ALC10;
import org.lwjgl.openal.ALC11;
import org.lwjgl.openal.ALCCapabilities;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.nio.ByteBuffer;

@Mixin(SoundEngine.class)
public class SoundEngineMixin {
Expand All @@ -41,8 +37,8 @@ public void getCapabilities(CallbackInfo ci, ALCCapabilities capabilities) {
cancellable = true
)
private static void openDevice(CallbackInfoReturnable<Long> cir) {
if(!AudioSwitcher.useDefault) {
long l = ALC10.alcOpenDevice(AudioSwitcher.currentDevice);
if(!LittleTweaks.getConfig().useDefaultDevice) {
long l = ALC10.alcOpenDevice(LittleTweaks.currentDevice);
if (l != 0L && !AlUtilAccessor.callCheckAlcErrors(l, "Open device")) {
cir.setReturnValue(l);
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.imurx.audioswitcher.mixin;
package io.github.imurx.littletweaks.mixin;

import net.minecraft.client.sound.SoundManager;
import net.minecraft.client.sound.SoundSystem;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package io.github.imurx.audioswitcher.mixin;
package io.github.imurx.littletweaks.mixin;

import io.github.imurx.audioswitcher.AudioSwitcher;
import io.github.imurx.audioswitcher.RightClickableWidget;
import io.github.imurx.littletweaks.LittleConfig;
import io.github.imurx.littletweaks.LittleTweaks;
import io.github.imurx.littletweaks.RightClickableWidget;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.screen.Screen;
Expand Down Expand Up @@ -40,24 +41,24 @@ private Element addSubtitleWidget(SoundOptionsScreen soundOptionsScreen, Element
if(!(widget instanceof ClickableWidget subtitleWidget)) {
throw new IllegalStateException("The subtitle button isn't a ClickableWidget!");
}
AudioSwitcher.updateDevices();
if(!AudioSwitcher.useDefault) {
selectedIndex = AudioSwitcher.devices.indexOf(AudioSwitcher.currentDevice) + 1;
LittleTweaks.updateDevices();
if(!LittleTweaks.getConfig().useDefaultDevice) {
selectedIndex = LittleTweaks.devices.indexOf(LittleTweaks.currentDevice) + 1;
} else {
selectedIndex = 0;
}
subtitleWidget.x = this.width / 2 + 5;
subtitleWidget.y -= 24;
this.addDrawableChild(subtitleWidget);
String option = AudioSwitcher.useDefault ? "Device: System Default" : "Device: " + AudioSwitcher.currentDevice.replaceAll("OpenAL Soft on ", "");
String option = LittleTweaks.getConfig().useDefaultDevice ? "Device: System Default" : "Device: " + LittleTweaks.currentDevice.replaceAll("OpenAL Soft on ", "");
ButtonWidget sourcesWidget = new RightClickableWidget(subtitleWidget.x - 160, subtitleWidget.y + 24, subtitleWidget.getWidth() * 2 + 10, subtitleWidget.getHeight(), Text.of(option), (button) -> {
if(++selectedIndex > AudioSwitcher.devices.size()) {
if(++selectedIndex > LittleTweaks.devices.size()) {
selectedIndex = 0;
}
updateDevice(button);
}, (button -> {
if(--selectedIndex < 0) {
selectedIndex = AudioSwitcher.devices.size();
selectedIndex = LittleTweaks.devices.size();
}
updateDevice(button);
}));
Expand All @@ -69,16 +70,18 @@ private Element addSubtitleWidget(SoundOptionsScreen soundOptionsScreen, Element
private void updateDevice(ClickableWidget button) {
SoundSystem soundSystem = ((SoundManagerAccessor) MinecraftClient.getInstance().getSoundManager()).getSoundSystem();
if(selectedIndex == 0) {
AudioSwitcher.useDefault = true;
LittleTweaks.getConfig().useDefaultDevice = true;
button.setMessage(Text.of("Device: System Default"));
soundSystem.reloadSounds();
LittleTweaks.saveConfig();
return;
} else if(AudioSwitcher.useDefault) {
AudioSwitcher.useDefault = false;
} else if(LittleTweaks.getConfig().useDefaultDevice) {
LittleTweaks.getConfig().useDefaultDevice = false;
}
AudioSwitcher.currentDevice = AudioSwitcher.devices.get(selectedIndex - 1);
AudioSwitcher.preferredDevice = AudioSwitcher.currentDevice;
button.setMessage(Text.of("Device: " + AudioSwitcher.currentDevice.replaceAll("OpenAL Soft on ", "")));
AudioSwitcher.restartSoundSystem(AudioSwitcher.currentDevice);
LittleTweaks.currentDevice = LittleTweaks.devices.get(selectedIndex - 1);
LittleTweaks.getConfig().preferredDevice = LittleTweaks.currentDevice;
button.setMessage(Text.of("Device: " + LittleTweaks.currentDevice.replaceAll("OpenAL Soft on ", "")));
LittleTweaks.restartSoundSystem(LittleTweaks.currentDevice);
LittleTweaks.saveConfig();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.imurx.audioswitcher.mixin;
package io.github.imurx.littletweaks.mixin;

import net.minecraft.client.sound.SoundEngine;
import net.minecraft.client.sound.SoundSystem;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.imurx.audioswitcher.mixin;
package io.github.imurx.littletweaks.mixin;

import io.github.imurx.audioswitcher.events.SoundSystemCallback;
import io.github.imurx.littletweaks.events.SoundSystemCallback;
import net.minecraft.client.sound.SoundSystem;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand Down
18 changes: 9 additions & 9 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
{
"schemaVersion": 1,
"id": "audioswitcher",
"id": "littletweaks",
"version": "${version}",

"name": "AudioSwitcher",
"description": "Let's you switch audio device + detects change of default one",
"name": "LittleTweaks",
"description": "Small client tweaks",
"authors": [
"ImUrX"
],
"contact": {
"homepage": "https://github.com/ImUrX/AudioSwitcher-fabric",
"sources": "https://github.com/ImUrX/AudioSwitcher-fabric",
"issues": "https://github.com/ImUrX/AudioSwitcher-fabric/issues"
"homepage": "https://github.com/ImUrX/littletweaks",
"sources": "https://github.com/ImUrX/littletweaks",
"issues": "https://github.com/ImUrX/littletweaks"
},

"license": "MIT",
"icon": "assets/audioswitcher/icon.png",
"icon": "assets/littletweaks/icon.png",

"environment": "client",
"entrypoints": {
"client": [
"io.github.imurx.audioswitcher.AudioSwitcher"
"io.github.imurx.littletweaks.LittleTweaks"
]
},
"mixins": [
"audioswitcher.mixins.json"
"littletweaks.mixins.json"
],

"depends": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"required": true,
"minVersion": "0.8",
"package": "io.github.imurx.audioswitcher.mixin",
"package": "io.github.imurx.littletweaks.mixin",
"compatibilityLevel": "JAVA_16",
"mixins": [],
"client": [
Expand Down

0 comments on commit 306157d

Please sign in to comment.