Skip to content

Commit 8f6e701

Browse files
committed
Yeh i hate this shit
1 parent 7253d87 commit 8f6e701

File tree

12 files changed

+175
-119
lines changed

12 files changed

+175
-119
lines changed

src/lemmesay/LemmeSay.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
import arc.Events;
44
import arc.util.CommandHandler;
55
import arc.util.Log;
6-
import lemmesay.net.packet.VCPackets;
6+
import lemmesay.net.packet.VCNet;
7+
import lemmesay.utils.VersionInfo;
78
import mindustry.game.EventType;
89
import mindustry.gen.PingCallPacket;
910
import mindustry.mod.Mod;
1011

1112
public class LemmeSay extends Mod{
1213

13-
public static final int PROTOCOL_VERSION = 1;
14+
public static LemmeSay instance;
15+
16+
1417

1518
public LemmeSay() {
16-
VCPackets.init();
19+
VCNet.init();
1720
}
1821

1922
@Override
@@ -33,13 +36,13 @@ private void registerEvents(){
3336
// On normal client, its does nothing cuz this packet can only send by client
3437
// so u know when a server send this packet, magic will come.
3538
var packet = new PingCallPacket();
36-
packet.time = -291103L; // "Random" negative number to identify our custom ping (it's not actually random)
39+
packet.time = VersionInfo.IDENTIFIER;
3740
player.con.send(packet, true);
3841
});
3942
}
4043

4144
private void loadConfig(){
42-
//TODO: Load configuration settings
45+
4346
}
4447

4548
@Override

src/lemmesay/net/packet/MicPacket.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
public class MicPacket extends Packet {
1212
private byte[] DATA;
13+
public int playerId;
1314
public byte[] audioData;
1415

1516
public MicPacket() { this.DATA = NODATA; }
@@ -26,12 +27,14 @@ public void read(Reads read, int length) {
2627

2728
@Override
2829
public void write(Writes write) {
30+
write.i(playerId);
2931
TypeIO.writeBytes(write, this.audioData);
3032
}
3133

3234
@Override
3335
public void handled() {
3436
BAIS.setBytes(this.DATA);
37+
this.playerId = READ.i();
3538
this.audioData = TypeIO.readBytes(READ);
3639
}
3740

src/lemmesay/net/packet/VCNet.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,34 @@
22

33
import arc.util.Log;
44
import lemmesay.LemmeSay;
5+
import lemmesay.utils.VersionInfo;
56
import mindustry.Vars;
67
import mindustry.gen.PingCallPacket;
78
import mindustry.gen.PingResponseCallPacket;
89
import mindustry.net.Net;
910

10-
public class VCPackets {
11+
public class VCNet {
1112

12-
public static void init(){
13+
public static void registerPackets(){
1314
Net.registerPacket(VoiceRequestPacket::new);
1415
Net.registerPacket(VoiceResponsePacket::new);
1516
Net.registerPacket(MicPacket::new);
17+
}
1618

19+
public static void handlePackets(){
1720
Vars.net.handleClient(PingCallPacket.class, ping -> {
1821
Log.info("Received PingCallPacket with time: " + ping.time);
1922
// Normal server cant send this packet so i use this trick to detect modded client on modded server.
20-
var packet = new PingResponseCallPacket();
21-
if (ping.time == -291103L){
22-
packet.time = ping.time;
23+
if (ping.time == VersionInfo.IDENTIFIER){
24+
var packet = new VoiceRequestPacket();
25+
packet.protocolVersion = VersionInfo.getProtocolVersion();
2326
Vars.net.send(packet, true);
2427
}
2528
});
29+
}
2630

27-
Vars.net.handleServer(PingResponseCallPacket.class, (con, ping) -> {
28-
Log.info("Received PingResponseCallPacket with time: " + ping.time);
29-
// Normal client cant send this packet so i use this trick to detect modded server on modded client.
30-
var packet = new VoiceRequestPacket();
31-
packet.protocolVersion = LemmeSay.PROTOCOL_VERSION;
32-
if (con.isConnected()){
33-
con.send(packet, true);
34-
}
35-
});
31+
public static void init(){
32+
registerPackets();
33+
handlePackets();
3634
}
3735
}

src/lemmesay/net/packet/VoiceRequestPacket.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
import arc.util.io.Reads;
77
import arc.util.io.Writes;
88
import lemmesay.LemmeSay;
9+
import lemmesay.net.session.Session;
10+
import lemmesay.net.session.SessionManager;
11+
import lemmesay.utils.VersionInfo;
912
import lemmesay.voice.VoiceManager;
1013
import mindustry.Vars;
14+
import mindustry.net.NetConnection;
1115
import mindustry.net.Packet;
1216

1317
public class VoiceRequestPacket extends Packet {
@@ -38,18 +42,18 @@ public void handled() {
3842
}
3943

4044
@Override
41-
public void handleClient() {
42-
Log.info("Received VoiceRequestPacket from server");
43-
VoiceResponsePacket packet = new VoiceResponsePacket();
44-
if (this.protocolVersion != LemmeSay.PROTOCOL_VERSION){
45-
packet.responseCode = this.protocolVersion < LemmeSay.PROTOCOL_VERSION ?
45+
public void handleServer(NetConnection con) {
46+
Log.debug("Received VoiceRequestPacket from client with protocol version: " + this.protocolVersion);
47+
var responsePacket = new VoiceResponsePacket();
48+
if (this.protocolVersion != VersionInfo.getProtocolVersion()){
49+
responsePacket.responseCode = this.protocolVersion < VersionInfo.getProtocolVersion() ?
4650
VoiceResponsePacket.CLIENT_OUTDATED : VoiceResponsePacket.SERVER_OUTDATED;
47-
Vars.net.send(packet, true);
51+
con.send(responsePacket, true);
4852
return;
4953
}
50-
packet.responseCode = VoiceResponsePacket.ACCEPTED;
51-
Vars.net.send(packet, true);
52-
53-
Threads.daemon(new VoiceManager());
54+
responsePacket.responseCode = VoiceResponsePacket.ACCEPTED;
55+
con.send(responsePacket, true);
56+
SessionManager.addSession(new Session(con));
57+
Log.info("Accepted voice communication from " + con.player.name());
5458
}
5559
}

src/lemmesay/net/packet/VoiceResponsePacket.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package lemmesay.net.packet;
22

33
import arc.util.Log;
4+
import arc.util.Threads;
45
import arc.util.io.Reads;
56
import arc.util.io.Writes;
67
import lemmesay.net.session.Session;
78
import lemmesay.net.session.SessionManager;
9+
import lemmesay.voice.VoiceManager;
810
import mindustry.net.NetConnection;
911
import mindustry.net.Packet;
1012

@@ -41,11 +43,16 @@ public void handled() {
4143
}
4244

4345
@Override
44-
public void handleServer(NetConnection con) {
45-
Log.info("Received VoiceResponsePacket from client with response code: " + this.responseCode);
46-
if (this.responseCode == ACCEPTED) {
47-
Log.info("Client accepted voice communication.");
48-
SessionManager.addSession(new Session(con));
46+
public void handleClient() {
47+
switch (this.responseCode) {
48+
case ACCEPTED -> Log.info("Voice communication accepted by server.");
49+
case CLIENT_OUTDATED -> Log.warn("Voice communication rejected: Client is outdated.");
50+
case SERVER_OUTDATED -> Log.warn("Voice communication rejected: Server is outdated.");
51+
case UNKNOWN -> Log.warn("Voice communication rejected: Unknown error.");
52+
default -> Log.warn("Voice communication rejected: Unrecognized response code " + this.responseCode);
53+
}
54+
if (this.responseCode == ACCEPTED){
55+
VoiceManager.startVoiceThread();
4956
}
5057
}
5158
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
package lemmesay.utils;
22

33
public class VersionInfo {
4+
5+
public static final String VERSION = "1.0.0";
6+
public static final String BUILD = "7";
7+
public static final int PROTOCOL = 2;
8+
public static final int DEV_PROTOCOL = 0x40000001;
9+
public static final boolean DEV_BUILD = true;
10+
public static final long IDENTIFIER = 0x291103L; // DO NOT EDIT.
11+
12+
public static String getFullVersion() {
13+
return VERSION + (DEV_BUILD ? "-dev" : "") + " (build " + BUILD + ")";
14+
}
15+
16+
public static int getProtocolVersion() {
17+
return DEV_BUILD ? DEV_PROTOCOL : PROTOCOL;
18+
}
419
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package lemmesay.voice;
2+
3+
import arc.util.Log;
4+
import lemmesay.voice.microphone.Microphone;
5+
import lemmesay.voice.process.denoiser.Denoiser;
6+
import lemmesay.voice.process.opus.OpusDecoder;
7+
import lemmesay.voice.process.opus.OpusEncoder;
8+
import lemmesay.voice.speaker.Speaker;
9+
10+
public class VoiceConfig {
11+
12+
public static Microphone mic;
13+
public static Speaker speaker;
14+
// Audio processing
15+
public static Denoiser denoiser;
16+
public static OpusEncoder encoder;
17+
public static OpusDecoder decoder;
18+
// Toggles;
19+
public static boolean enableVoice = false;
20+
public static boolean enableDenoiser = true;
21+
22+
public static void closeProcessors(){
23+
try{
24+
if(denoiser != null && !denoiser.isClosed()){
25+
denoiser.close();
26+
}
27+
if(encoder != null && !encoder.isClosed()){
28+
encoder.close();
29+
}
30+
if(decoder != null && !decoder.isClosed()){
31+
decoder.close();
32+
}
33+
} catch (Exception e){
34+
Log.err(e);
35+
}
36+
}
37+
38+
public static void closeSpeaker(){
39+
try{
40+
if(speaker != null){
41+
speaker.close();
42+
}
43+
} catch (Exception e){
44+
Log.err(e);
45+
}
46+
}
47+
48+
public static void closeMicrophone(){
49+
try{
50+
if(mic != null){
51+
mic.close();
52+
}
53+
} catch (Exception e){
54+
Log.err(e);
55+
}
56+
}
57+
}

src/lemmesay/voice/VoiceManager.java

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,50 @@
11
package lemmesay.voice;
22

33
import arc.util.Log;
4+
import arc.util.Threads;
45
import lemmesay.voice.microphone.JavaxMicrophone;
56
import mindustry.Vars;
67

7-
public class VoiceManager implements Runnable {
8+
public class VoiceManager{
89

910
// WARNING: LAZY Implement.
1011
private static final int SAMPLE_RATE = 48000;
1112
private static final int CHANNELS = 1;
1213
private static final int BUFFER_SIZE = 960;
1314

14-
@Override
15-
public void run() {
16-
JavaxMicrophone mic = new JavaxMicrophone(SAMPLE_RATE, BUFFER_SIZE, "");
17-
mic.open();
18-
mic.start();
19-
Log.info("Microphone capture started for voice chat.");
20-
while (true) {
21-
if (!Vars.net.client()) {
22-
Log.info("Voice chat disconnected, stopping microphone capture.");
23-
mic.close();
24-
VoiceProcessing.getInstance().close();
25-
Log.info("Voice processing resources released.");
26-
break;
27-
}
28-
try {
29-
short[] audioData = mic.read();
30-
VoiceProcessing.getInstance().handleAudio(audioData);
31-
Thread.sleep(20);
32-
} catch (IllegalStateException e) {
33-
Log.warn("Failed to read from microphone: " + e.getMessage());
34-
} catch (InterruptedException e) {
35-
Thread.currentThread().interrupt();
36-
mic.close();
37-
Log.info("Voice chat thread interrupted, stopping microphone capture.");
38-
break;
15+
public static void startVoiceThread() {
16+
Log.info("Starting voice communication thread.");
17+
Threads.daemon(VoiceThread::new);
18+
Vars.player.usid();
19+
}
20+
21+
public static class VoiceThread implements Runnable {
22+
@Override
23+
public void run() {
24+
JavaxMicrophone mic = new JavaxMicrophone(SAMPLE_RATE, BUFFER_SIZE, "");
25+
mic.open();
26+
mic.start();
27+
Log.info("Microphone capture started for voice chat.");
28+
while (true) {
29+
if (!Vars.net.client()) {
30+
Log.info("Voice chat disconnected, stopping microphone capture.");
31+
mic.close();
32+
VoiceProcessing.getInstance().close();
33+
Log.info("Voice processing resources released.");
34+
break;
35+
}
36+
try {
37+
short[] audioData = mic.read();
38+
VoiceProcessing.getInstance().handleAudio(audioData);
39+
Thread.sleep(20);
40+
} catch (IllegalStateException e) {
41+
Log.warn("Failed to read from microphone: " + e.getMessage());
42+
} catch (InterruptedException e) {
43+
Thread.currentThread().interrupt();
44+
mic.close();
45+
Log.info("Voice chat thread interrupted, stopping microphone capture.");
46+
break;
47+
}
3948
}
4049
}
4150
}

0 commit comments

Comments
 (0)