Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions common/src/main/java/com/github/squi2rel/mcft/AutoBlink.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public class AutoBlink {
private static long lastUpdateTime = System.currentTimeMillis();

public static void update() {
if (!config.autoBlink || config.autoSwitchBlink && OSC.lastReceived != lastUpdateTime && System.currentTimeMillis() - OSC.lastReceived < 3000) {
long currentTime = System.currentTimeMillis();
if (!config.autoBlink || config.autoSwitchBlink && currentTime - OSC.lastOscReceived < 3000) {
enabled = false;
return;
}
enabled = true;
long currentTime = System.currentTimeMillis();
float delta = currentTime - lastUpdateTime;
lastUpdateTime = currentTime;
last += delta;
Expand All @@ -50,6 +50,5 @@ public static void update() {
}
}
model.eyeL.percent = model.eyeR.percent = eyeOpenness * config.blinkMaxY;
OSC.lastReceived = lastUpdateTime;
}
}
2 changes: 2 additions & 0 deletions common/src/main/java/com/github/squi2rel/mcft/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ public class Config {
public int httpPort = 8999;
public int oscReceivePort = 9000;
public int oscSendPort = 9001;
public String oscBindHost = "127.0.0.1";
public String oscTargetHost = "127.0.0.1";

public FTModel model = new FTModel();
public float eyeXMul = 0.5f, eyeYMul = 0.3f;
Expand Down
11 changes: 7 additions & 4 deletions common/src/main/java/com/github/squi2rel/mcft/MCFT.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.github.squi2rel.mcft.network.TrackingParamsPayload;
import com.github.squi2rel.mcft.network.TrackingUpdatePayload;
import com.google.gson.Gson;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.server.network.ServerPlayerEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -31,7 +30,7 @@ public static void onInitialize() {

ServerPacketHandler.registerC2S(TrackingParamsPayload.ID, TrackingParamsPayload.CODEC, (payload, p) -> {
FTModel old = models.get(p.getUuid());
if (old == null) LOGGER.info(I18n.translate("mcft.logging.playerconnected", Objects.requireNonNull(p.getDisplayName()).getString()));
if (old == null) LOGGER.info("Player {} is using MCFT", Objects.requireNonNull(p.getDisplayName()).getString());
FTModel model = new FTModel(payload.eyeR(), payload.eyeL(), payload.mouth(), payload.flat());
model.validate(true);
if (old != null) model.enabled = old.enabled;
Expand All @@ -49,7 +48,7 @@ public static void onInitialize() {
model.validate(false);
if (!model.enabled) {
model.enabled = true;
LOGGER.info(I18n.translate("mcft.logging.oscreceived", Objects.requireNonNull(p.getDisplayName()).getString()));
LOGGER.info("Player {} has OSC connected", Objects.requireNonNull(p.getDisplayName()).getString());
TrackingParamsPayload packet = new TrackingParamsPayload(p.getUuid(), model.eyeR, model.eyeL, model.mouth, model.isFlat);
for (ServerPlayerEntity player : Objects.requireNonNull(p.getServer()).getPlayerManager().getPlayerList()) ServerPacketHandler.sendS2C(player, packet);
}
Expand All @@ -59,6 +58,10 @@ public static void onInitialize() {
)) ServerPacketHandler.sendS2C(player, packet);
});

ServerPacketHandler.registerS2C(TrackingParamsPayload.ID, TrackingParamsPayload.CODEC);
ServerPacketHandler.registerS2C(TrackingUpdatePayload.ID, TrackingUpdatePayload.CODEC);
ServerPacketHandler.registerS2C(ConfigPayload.ID, ConfigPayload.CODEC);

Platform.register();
}

Expand Down Expand Up @@ -95,4 +98,4 @@ public static void saveConfig(Object config, Path path) {
throw new RuntimeException(e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static void onInitializeClient() {
public static void update() {
if (MC.player == null || MC.world == null) return;
AutoBlink.update();
OSC.applyPendingParameters();
if (model.active() && System.currentTimeMillis() - lastSync > 1000 / fps) {
FTClient.writeSync(model);
lastSync = System.currentTimeMillis();
Expand All @@ -86,4 +87,4 @@ private static boolean checkVersion(String v) {
if (p1.length < 2 || p2.length < 2) return false;
return p1[0].equals(p2[0]) && p1[1].equals(p2[1]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public static <P extends CustomPayload> void registerC2S(CustomPayload.Id<P> id,
throw new AssertionError();
}

@ExpectPlatform
public static <P extends CustomPayload> void registerS2C(CustomPayload.Id<P> id, PacketCodec<PacketByteBuf, P> codec) {
throw new AssertionError();
}

@ExpectPlatform
public static <P extends CustomPayload> void sendS2C(ServerPlayerEntity player, P packet) {
throw new AssertionError();
Expand Down
69 changes: 56 additions & 13 deletions common/src/main/java/com/github/squi2rel/mcft/services/DNS.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,67 @@
import java.util.concurrent.TimeUnit;

public class DNS {
private static final Object lock = new Object();
private static volatile boolean running;
private static Thread thread;
public static final int port = 5353;
public static void init() {
Thread dns = new Thread(() -> {
while (true) {
try {
TimeUnit.SECONDS.sleep(10);
sendReply();
} catch (InterruptedException ignored) {
} catch (Exception e) {
throw new RuntimeException(e);
}
synchronized (lock) {
if (running) return;
running = true;
Thread candidate = new Thread(DNS::run);
candidate.setName("MCFT DNS");
candidate.setDaemon(true);
thread = candidate;
try {
candidate.start();
} catch (RuntimeException e) {
thread = null;
running = false;
throw e;
}
});
dns.setDaemon(true);
dns.start();
}
MCFT.LOGGER.info("DNS started on port {}", port);
}

public static void shutdown() {
Thread current;
synchronized (lock) {
running = false;
current = thread;
thread = null;
}
if (current != null) current.interrupt();
}

private static void run() {
Thread current = Thread.currentThread();
boolean failureLogged = false;
while (running && thread == current) {
try {
sendReply();
if (failureLogged) MCFT.LOGGER.info("DNS replies resumed");
failureLogged = false;
} catch (Exception e) {
if (!failureLogged) MCFT.LOGGER.error("DNS reply failed", e);
failureLogged = true;
}
if (!running || thread != current) break;
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException ignored) {
current.interrupt();
break;
}
}
synchronized (lock) {
if (thread == current) {
thread = null;
running = false;
}
}
}

private static void sendReply() throws Exception {
Message response = new Message();
Header header = response.getHeader();
Expand All @@ -44,4 +87,4 @@ private static void sendReply() throws Exception {
socket.send(packet);
}
}
}
}
Loading
Loading