Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/mlogwatcher/Setting.java
  • Loading branch information
Sharlottes committed Dec 25, 2023
2 parents 6e4e4b9 + b1f1b01 commit 4be1e99
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 38 deletions.
5 changes: 4 additions & 1 deletion assets/bundles/bundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ mlogwatcher.setting.title=MlogWatcher Setting
mlogwatcher.setting.mlogLabel=current mlog target path
mlogwatcher.setting.mlogSelectButton=Change watcher target
mlogwatcher.setting.fileChooserTitle=select target file to watch changes
mlogwatcher.setting.extensionInputLabel=file extension to search:
mlogwatcher.setting.extensionInputLabel=file extension to search:
mlogwatcher.setting.websocketPortLabel=websocket server port:
mlogwatcher.setting.restartServerButton=Restart websocket server
mlogwatcher.info.serverBindError=MlogWatcher: error during the initialization of the websocket server.\nThis likely happened because another process is already using the server's configured port.\n\nConsider ending the process or changing the server's port.\nYou can restart the server in the settings menu.
5 changes: 4 additions & 1 deletion assets/bundles/bundle_ko.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ mlogwatcher.setting.title=MlogWatcher 설정
mlogwatcher.setting.mlogLabel=현재 mlog 대상 경로
mlogwatcher.setting.mlogSelectButton=mlog 대상 경로 바꾸기
mlogwatcher.setting.fileChooserTitle=변경을 지켜볼 대상 파일을 선택하세요
mlogwatcher.setting.extensionInputLabel=검색할 파일 확장자 이름:
mlogwatcher.setting.extensionInputLabel=검색할 파일 확장자 이름:
mlogwatcher.setting.websocketPortLabel=웹소켓 서버 포트:
mlogwatcher.setting.restartServerButton=웹소켓 서버 재시작
mlogwatcher.info.serverBindError=MlogWatcher: 웹소켓 서버 초기화 중 에러가 발생했습니다.\n다른 프로세스가 이미 서버의 구성된 포트를 사용하고 있기 때문에 이런 일이 발생한 것 같습니다.\n\n프로세스를 종료하거나 서버의 포트를 변경해보세요.\n설정 메뉴에서 서버를 다시 시작할 수도 있습니다.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ repositories {
dependencies {
compileOnly "com.github.Anuken.Mindustry:core:$mindustryVersion"
compileOnly "com.github.Anuken.Arc:arc-core:$mindustryVersion"
implementation 'org.java-websocket:Java-WebSocket:1.5.4'

annotationProcessor 'com.github.Anuken:jabel:0.8.0'
}

jar {
duplicatesStrategy DuplicatesStrategy.EXCLUDE
archiveFileName = "${artifactFilename}Desktop.jar"

from{
Expand Down
4 changes: 4 additions & 0 deletions src/mlogwatcher/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ static class Bundles {
public static final String settingMlogSelectButton = "@mlogwatcher.setting.mlogSelectButton";
public static final String settingFileChooserTitle = "@mlogwatcher.setting.fileChooserTitle";
public static final String settingExtensionInputLabel = "@mlogwatcher.setting.extensionInputLabel";
public static final String settingWebsocketPortLabel = "@mlogwatcher.setting.websocketPortLabel";
public static final String settingRestartServerButton = "@mlogwatcher.setting.restartServerButton";
public static final String infoServerBindError = "@mlogwatcher.info.serverBindError";
}

static class Settings {
public static final String mlogPath = "mlogwatcher-mlog-path";
public static final String websocketPort = "mlogwatcher-websocket-port";
}
}
9 changes: 6 additions & 3 deletions src/mlogwatcher/FileWatcher.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mlogwatcher;

import arc.Core;
import arc.func.Cons;
import arc.util.Log;
import arc.util.Nullable;

Expand All @@ -11,12 +10,15 @@
public class FileWatcher {
@Nullable
private static Thread fileWatcherThread;

public static void startWatcherThread() {
if (fileWatcherThread != null) return;
fileWatcherThread = new FileWatcherThread(Core.settings.getString(Constants.Settings.mlogPath));
fileWatcherThread.start();
}

public static void stopWatcherThread() {
if (fileWatcherThread == null) return;
fileWatcherThread.interrupt();
}
}
Expand Down Expand Up @@ -46,9 +48,9 @@ public void run() {
}

boolean valid = watchKey.reset();
if(!valid) throw new Exception("watch mode no longer valid!");
if (!valid) throw new Exception("watch mode no longer valid!");
}
} catch (IOException e) {
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
Log.warn("mlog watcher's target file has been changed");
Expand All @@ -58,3 +60,4 @@ public void run() {
}
}
}

67 changes: 67 additions & 0 deletions src/mlogwatcher/MlogServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package mlogwatcher;

import arc.Core;
import arc.util.Log;
import arc.util.Nullable;
import mindustry.Vars;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

import java.net.BindException;
import java.net.InetSocketAddress;

public class MlogServer extends WebSocketServer {
@Nullable
private static MlogServer server;

MlogServer(int port) {
super(new InetSocketAddress(port));
}

public static void startServer() {
if (server != null) return;
server = new MlogServer(Core.settings.getInt(Constants.Settings.websocketPort));
server.start();
}

public static void stopServer() {
if (server == null) return;

try {
server.stop();
} catch (InterruptedException ignored) {

}
server = null;
}

@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {

}

@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {

}

@Override
public void onMessage(WebSocket conn, String message) {
ProcessorUpdater.InsertLogic(message);
}

@Override
public void onError(WebSocket conn, Exception ex) {
Log.err("[MlogWatcher] socket error", ex);

if (ex instanceof BindException) {
Vars.ui.showInfo(Constants.Bundles.infoServerBindError);
}
}

@Override
public void onStart() {
Log.info("[MlogWatcher] server running on port @", getPort());
}
}
12 changes: 9 additions & 3 deletions src/mlogwatcher/MlogWatcher.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package mlogwatcher;

import arc.Events;
import mindustry.mod.Mod;
import mindustry.game.EventType;
import mindustry.mod.Mod;

public class MlogWatcher extends Mod {
public MlogWatcher(){
public MlogWatcher() {
Events.on(EventType.ClientLoadEvent.class, e -> {
Setting.init();
ProcessorUpdater.init();
FileWatcher.startWatcherThread();
MlogServer.startServer();
});

Events.on(EventType.DisposeEvent.class, e -> {
FileWatcher.stopWatcherThread();
MlogServer.stopServer();
});
}
}
}
22 changes: 14 additions & 8 deletions src/mlogwatcher/ProcessorUpdater.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package mlogwatcher;

import arc.*;
import arc.util.*;
import arc.Core;
import arc.Events;
import arc.files.Fi;
import arc.graphics.g2d.*;

import arc.graphics.g2d.Draw;
import arc.graphics.g2d.Lines;
import arc.util.Log;
import arc.util.Nullable;
import mindustry.content.Fx;
import mindustry.graphics.Pal;
import mindustry.game.EventType;
import mindustry.graphics.Pal;
import mindustry.world.blocks.logic.LogicBlock;

public class ProcessorUpdater {
Expand All @@ -24,7 +26,7 @@ public static void init() {
});

Events.run(EventType.Trigger.draw, () -> {
if(lastTappedLogicBuild == null) return;
if (lastTappedLogicBuild == null) return;
Draw.reset();
Lines.stroke(1f);
Draw.color(Pal.accent);
Expand All @@ -34,13 +36,17 @@ public static void init() {
}

public static void InsertLogic() {
String mlogPath = Core.settings.getString(Constants.Settings.mlogPath);
String asmCode = Fi.get(mlogPath).readString().replace("\r\n", "\n");
InsertLogic(asmCode);
}

public static void InsertLogic(String asmCode) {
if (lastTappedLogicBuild == null) {
Log.warn("cannot find any selected logic block!");
return;
}

String mlogPath = Core.settings.getString(Constants.Settings.mlogPath);
String asmCode = Fi.get(mlogPath).readString().replace("\r\n", "\n");
lastTappedLogicBuild.configure(LogicBlock.compress(asmCode, lastTappedLogicBuild.relativeConnections()));
Fx.spawn.at(lastTappedLogicBuild.x, lastTappedLogicBuild.y);
}
Expand Down
70 changes: 48 additions & 22 deletions src/mlogwatcher/Setting.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,61 @@
import arc.Core;
import arc.scene.ui.Label;
import arc.scene.ui.TextField;

import mindustry.Vars;

public class Setting {
public static void init(){
Label label = new Label(Core.settings.getString(Constants.Settings.mlogPath, "[lightgray]@none[]"));
label.setFontScale(0.75f);

TextField field = new TextField();
field.setText("mlog");
field.setFilter((f, c) -> c != ' ' && c != '.');
field.setMessageText("no ext");

Vars.ui.settings.addCategory(Constants.Bundles.settingTitle, (settingTable) -> {
settingTable.center();
settingTable.add(Constants.Bundles.settingMlogPathLabel).row();
settingTable.add(label).row();
settingTable.button(Constants.Bundles.settingMlogSelectButton, () ->
Vars.platform.showFileChooser(true, Constants.Bundles.settingFileChooserTitle, field.getText(), fi -> {
label.setText(fi.absolutePath());
public static void init() {
Core.settings.defaults(
Constants.Settings.mlogPath, "[lightgray]@none[]",
Constants.Settings.websocketPort, 9992
);

Vars.ui.settings.addCategory("Mlog Watcher", t -> {
Label label = new Label(() -> Core.settings.getString(Constants.Settings.mlogPath));
label.setFontScale(0.75f);

TextField extensionTextField = new TextField();
extensionTextField.setText("mlog");
extensionTextField.setFilter((f, c) -> c != ' ' && c != '.');
extensionTextField.setMessageText("no ext");

t.add(Constants.Bundles.settingMlogPathLabel).left().colspan(2).row();
t.add(label).left().colspan(2).row();
t.button(Constants.Bundles.settingMlogSelectButton, () -> {
Vars.platform.showFileChooser(true, Constants.Bundles.settingFileChooserTitle, extensionTextField.getText(), fi -> {
Core.settings.put(Constants.Settings.mlogPath, fi.absolutePath());
FileWatcher.stopWatcherThread();
FileWatcher.startWatcherThread();
})
).width(280f).height(60f).pad(16f).row();
settingTable.table(fieldTable -> {
fieldTable.add(Constants.Bundles.settingExtensionInputLabel);
fieldTable.add(field);
});
}).height(60f).pad(16f).colspan(2).fill().row();
t.add(Constants.Bundles.settingExtensionInputLabel).left();
t.add(extensionTextField);
t.row();

TextField portTextField = new TextField();
portTextField.update(() -> portTextField.setText(String.valueOf(Core.settings.getInt(Constants.Settings.websocketPort))));
portTextField.changed(() -> {
try {
int port = Integer.parseInt(portTextField.getText());
Core.settings.put(Constants.Settings.websocketPort, port);
} catch (NumberFormatException ignored) {

}
});

t.add(Constants.Bundles.settingWebsocketPortLabel).left();
t.add(portTextField);
t.row();

t.button(Constants.Bundles.settingRestartServerButton, () -> {
MlogServer.stopServer();
MlogServer.startServer();
}).height(60f).pad(16f).colspan(2).fill().row();

t.button("@settings.reset", () -> {
Core.settings.remove(Constants.Settings.mlogPath);
Core.settings.remove(Constants.Settings.websocketPort);
}).margin(14).width(240f).pad(6).colspan(2).row();
});
}
}

0 comments on commit 4be1e99

Please sign in to comment.