Skip to content

Commit 1133f7d

Browse files
committed
fix for production
1 parent 8746dce commit 1133f7d

7 files changed

Lines changed: 248 additions & 6 deletions

File tree

build.gradle

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,139 @@ tasks.register('stripPreviewMinor', StripPreviewMinorTask) {
251251

252252
tasks.named('classes') {
253253
finalizedBy tasks.named('stripPreviewMinor')
254+
}
255+
256+
257+
import groovy.json.JsonBuilder
258+
import groovy.json.JsonSlurper
259+
260+
import java.nio.file.FileSystems
261+
import java.nio.file.Files
262+
import java.nio.file.Path
263+
264+
abstract class RemoveWebRTCLibJarjarMetadata extends DefaultTask {
265+
266+
@InputFile
267+
abstract RegularFileProperty getTargetJar()
268+
269+
@TaskAction
270+
void processMetadata() {
271+
def jarFile = getTargetJar().get().asFile
272+
if (!jarFile.exists()) {
273+
return
274+
}
275+
276+
URI uri = URI.create("jar:" + jarFile.toPath().toUri().toString())
277+
278+
try {
279+
FileSystems.newFileSystem(uri, Collections.emptyMap()).withCloseable { fs ->
280+
Path metadataPath = fs.getPath("/META-INF/jarjar/metadata.json")
281+
282+
if (Files.exists(metadataPath)) {
283+
def reader = Files.newBufferedReader(metadataPath)
284+
def json = new JsonSlurper().parse(reader)
285+
reader.close()
286+
287+
if (json.jars instanceof List) {
288+
boolean removed = json.jars.removeIf { jarObj ->
289+
String pathStr = jarObj.path?.toString()
290+
return pathStr != null && pathStr.matches(".*webrtc-java-[0-9.]+-.*\\.jar")
291+
}
292+
293+
if (removed) {
294+
def writer = Files.newBufferedWriter(metadataPath)
295+
writer.write(new JsonBuilder(json).toPrettyString())
296+
writer.close()
297+
}
298+
}
299+
}
300+
}
301+
} catch (Exception e) {
302+
project.logger.error("Failed to process jarjar metadata in: " + jarFile.name, e)
303+
}
304+
}
305+
}
306+
307+
tasks.register("removeWebRTCLibJarjarMetadata", RemoveWebRTCLibJarjarMetadata) {
308+
targetJar.set(tasks.named("jar").flatMap { it.archiveFile })
309+
}
310+
311+
tasks.named("build") {
312+
finalizedBy tasks.named("removeWebRTCLibJarjarMetadata")
313+
}
314+
315+
import java.nio.file.StandardCopyOption
316+
import java.util.jar.Manifest
317+
318+
abstract class MakeLibMixinableTask extends DefaultTask {
319+
320+
@InputFile
321+
abstract RegularFileProperty getTargetJar()
322+
323+
@TaskAction
324+
void processJar() {
325+
def mainJarFile = getTargetJar().get().asFile
326+
if (!mainJarFile.exists()) {
327+
return
328+
}
329+
330+
URI mainUri = URI.create("jar:" + mainJarFile.toPath().toUri().toString())
331+
332+
try {
333+
FileSystems.newFileSystem(mainUri, Collections.emptyMap()).withCloseable { mainFs ->
334+
Path jarJarDir = mainFs.getPath("/META-INF/jarjar")
335+
if (!Files.exists(jarJarDir)) {
336+
return
337+
}
338+
339+
Files.list(jarJarDir).forEach { innerJarPath ->
340+
String fileName = innerJarPath.getFileName().toString()
341+
342+
if (fileName.matches("webrtc-java-[0-9.]+\\.jar")) {
343+
Path tempJar = Files.createTempFile("temp-webrtc", ".jar")
344+
Files.copy(innerJarPath, tempJar, StandardCopyOption.REPLACE_EXISTING)
345+
346+
boolean needsUpdate = false
347+
URI tempUri = URI.create("jar:" + tempJar.toUri().toString())
348+
349+
FileSystems.newFileSystem(tempUri, Collections.emptyMap()).withCloseable { tempFs ->
350+
Path manifestPath = tempFs.getPath("/META-INF/MANIFEST.MF")
351+
352+
if (Files.exists(manifestPath)) {
353+
Manifest manifest
354+
Files.newInputStream(manifestPath).withCloseable { is ->
355+
manifest = new Manifest(is)
356+
}
357+
358+
String fmlModType = manifest.getMainAttributes().getValue("FMLModType")
359+
if (fmlModType != "GAMELIBRARY") {
360+
manifest.getMainAttributes().putValue("FMLModType", "GAMELIBRARY")
361+
Files.newOutputStream(manifestPath).withCloseable { os ->
362+
manifest.write(os)
363+
}
364+
needsUpdate = true
365+
}
366+
}
367+
}
368+
369+
if (needsUpdate) {
370+
Files.copy(tempJar, innerJarPath, StandardCopyOption.REPLACE_EXISTING)
371+
}
372+
Files.deleteIfExists(tempJar)
373+
}
374+
}
375+
}
376+
} catch (Exception e) {
377+
project.logger.error("Failed to inject manifest entry in: " + mainJarFile.name, e)
378+
}
379+
}
380+
}
381+
382+
tasks.register("makeLibMixinableTask", MakeLibMixinableTask) {
383+
targetJar.set(tasks.named("jar").flatMap { it.archiveFile })
384+
mustRunAfter tasks.named("removeWebRTCLibJarjarMetadata")
385+
}
386+
387+
tasks.named("build") {
388+
finalizedBy tasks.named("makeLibMixinableTask")
254389
}

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-9.2.1-bin.zip
3+
distributionUrl=https\://mirrors.aliyun.com/gradle/distributions/v9.2.1/gradle-9.2.1-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

src/main/java/cn/ussshenzhou/channel/audio/server/RelayHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package cn.ussshenzhou.channel.audio.server;
22

33
import cn.ussshenzhou.channel.network.TalkPacket2C;
4+
import cn.ussshenzhou.t88.T88;
45
import cn.ussshenzhou.t88.network.NetworkHelper;
56
import com.google.common.util.concurrent.ThreadFactoryBuilder;
67
import net.minecraft.server.level.ServerPlayer;
8+
import net.neoforged.neoforge.gametest.GameTestHooks;
79

810
import java.util.concurrent.ExecutorService;
911
import java.util.concurrent.Executors;
@@ -26,7 +28,7 @@ public static void process(ServerPlayer from, byte[] opusAudio, int sampleRate)
2628

2729
public static void normalTalking(ServerPlayer from, byte[] opusAudio, int sampleRate) {
2830
from.level().players().stream().filter(to ->
29-
to.getId() != from.getId() &&
31+
(GameTestHooks.isGametestEnabled() || to.getId() != from.getId()) &&
3032
to.position().distanceTo(from.position()) < 64 &&
3133
(!from.isSpectator() || to.isSpectator())
3234
)

src/main/java/cn/ussshenzhou/channel/config/ChannelClientConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public class ChannelClientConfig implements TConfig {
2222
public boolean listen = false;
2323
public Trigger trigger = Trigger.THRESHOLD;
2424
public Vad voiceDetectThreshold = Vad.LOW;
25-
public float triggerThresholdDBFS = -40;
26-
public NC noiseCanceling = NC.HIGH;
25+
public float triggerThresholdDBFS = -48;
26+
public NC noiseCanceling = NC.MID;
2727
public float aiNoiseCancelingRatio = 0.5f;
2828
public boolean highPassFilter = true;
2929
public boolean echoCanceling = false;

src/main/java/cn/ussshenzhou/channel/gui/OutputConfigPanel.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import net.neoforged.bus.api.SubscribeEvent;
1515
import net.neoforged.fml.common.EventBusSubscriber;
1616
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
17+
import net.neoforged.neoforge.gametest.GameTestHooks;
1718
import org.joml.Vector2i;
1819

1920
import java.util.*;
@@ -61,7 +62,9 @@ public static class PlayerVolumePanel extends TPanel {
6162
private static boolean dirty = true;
6263

6364
public PlayerVolumePanel() {
64-
//update(Minecraft.getInstance().player.getUUID(), 0);
65+
if (GameTestHooks.isGametestEnabled()) {
66+
update(Minecraft.getInstance().player.getUUID(), 0);
67+
}
6568
dirty = true;
6669
}
6770

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,73 @@
11
package cn.ussshenzhou.channel.mixin;
22

3+
import cn.ussshenzhou.channel.audio.client.send.WebRTCHelper;
4+
import cn.ussshenzhou.channel.util.PlatformUtils;
5+
import com.mojang.logging.LogUtils;
36
import dev.onvoid.webrtc.internal.NativeLoader;
7+
import net.minecraft.SharedConstants;
48
import org.spongepowered.asm.mixin.Mixin;
9+
import org.spongepowered.asm.mixin.Unique;
510
import org.spongepowered.asm.mixin.injection.At;
611
import org.spongepowered.asm.mixin.injection.Inject;
712
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
813

14+
import java.io.IOException;
15+
import java.nio.file.Files;
16+
import java.nio.file.StandardCopyOption;
17+
import java.util.zip.ZipEntry;
18+
import java.util.zip.ZipInputStream;
19+
920
/**
1021
* @author USS_Shenzhou
1122
*/
12-
@Mixin(value = NativeLoader.class, remap = false)
23+
@Mixin(NativeLoader.class)
1324
public class NativeLoaderMixin {
1425

1526
@Inject(method = "loadLibrary", at = @At(value = "HEAD"), cancellable = true)
1627
private static void channelCancelLoad(String libName, CallbackInfo ci) {
28+
if (SharedConstants.IS_RUNNING_WITH_JDWP) {
29+
LogUtils.getLogger().warn("We are in a dev env now. Native things may work differently.");
30+
return;
31+
}
32+
loadNativeInternal(PlatformUtils.getOS(), PlatformUtils.getLibSuffix());
1733
ci.cancel();
1834
}
35+
36+
@Unique
37+
private static void loadNativeInternal(String platform, String fileSuffix) {
38+
var jarJarPath = "/META-INF/jarjar/webrtc-java-0.14.0-" + platform + ".jar";
39+
var libName = "webrtc-java-" + platform + fileSuffix;
40+
LogUtils.getLogger().info("Loading " + libName + " from " + jarJarPath);
41+
try (var resourceStream = WebRTCHelper.class.getResourceAsStream(jarJarPath)) {
42+
if (resourceStream == null) {
43+
throw new RuntimeException("Mod Jar is not complete.");
44+
}
45+
try (var zipStream = new ZipInputStream(resourceStream)) {
46+
ZipEntry entry;
47+
boolean found = false;
48+
while ((entry = zipStream.getNextEntry()) != null) {
49+
found = findAndLoad(fileSuffix, entry, libName, zipStream, found);
50+
zipStream.closeEntry();
51+
}
52+
if (!found) {
53+
throw new RuntimeException("Failed to extract and load WebRTC. Something went wrong.");
54+
}
55+
}
56+
} catch (Exception e) {
57+
throw new RuntimeException("Failed to load WebRTC lib.", e);
58+
}
59+
LogUtils.getLogger().info("Successfully loaded WebRTC lib.");
60+
}
61+
62+
@Unique
63+
private static boolean findAndLoad(String fileSuffix, ZipEntry entry, String libName, ZipInputStream zipStream, boolean found) throws IOException {
64+
if (entry.getName().equals(libName)) {
65+
var tempLib = Files.createTempFile("Channel_WebRTC_", fileSuffix);
66+
tempLib.toFile().deleteOnExit();
67+
Files.copy(zipStream, tempLib, StandardCopyOption.REPLACE_EXISTING);
68+
System.load(tempLib.toAbsolutePath().toString());
69+
found = true;
70+
}
71+
return found;
72+
}
1973
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package cn.ussshenzhou.channel.util;
2+
3+
/**
4+
* @author USS_Shenzhou
5+
*/
6+
public class PlatformUtils {
7+
8+
public static String getOS() {
9+
String osName = System.getProperty("os.name").toLowerCase(java.util.Locale.ROOT);
10+
String osArch = System.getProperty("os.arch").toLowerCase(java.util.Locale.ROOT);
11+
12+
String os;
13+
if (osName.contains("win")) {
14+
os = "windows";
15+
} else if (osName.contains("mac")) {
16+
os = "macos";
17+
} else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) {
18+
os = "linux";
19+
} else {
20+
throw new UnsupportedOperationException("Unsupported operating system: " + osName);
21+
}
22+
23+
String arch;
24+
if ("amd64".equals(osArch) || "x86_64".equals(osArch)) {
25+
arch = "x86_64";
26+
} else if ("aarch64".equals(osArch)) {
27+
arch = "aarch64";
28+
} else {
29+
throw new UnsupportedOperationException("Unsupported architecture: " + osArch);
30+
}
31+
32+
return os + "-" + arch;
33+
}
34+
35+
public static String getLibSuffix() {
36+
String osName = System.getProperty("os.name").toLowerCase(java.util.Locale.ROOT);
37+
38+
if (osName.contains("win")) {
39+
return ".dll";
40+
} else if (osName.contains("mac")) {
41+
return ".dylib";
42+
} else if (osName.contains("nix") || osName.contains("nux") || osName.contains("aix")) {
43+
return ".so";
44+
} else {
45+
throw new UnsupportedOperationException("Unsupported operating system: " + osName);
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)