Skip to content

Commit

Permalink
5.4-M2 & WebUI 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Dituon committed Mar 10, 2023
1 parent ddce095 commit bcdc539
Show file tree
Hide file tree
Showing 16 changed files with 240 additions and 44 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ apply plugin: "java"


group = 'xmmt.dituon'
version = '5.4-M1'
version = '5.4-M2'

repositories {
maven { url 'https://maven.aliyun.com/repository/public' }
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/moe/dituon/petpet/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public static void main(String[] args) {
return;
}
List<String> param = Arrays.asList(args);
if (param.contains("-preview")) {
WebServer server = new WebServer();
server.service.usePreview = true;
}
if (param.contains("-gocq")) {
try {
GoCQPetpet goCQPetpet = GoCQPetpet.getInstance();
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/moe/dituon/petpet/mirai/MiraiPetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import moe.dituon.petpet.plugin.Cooler;
import moe.dituon.petpet.plugin.DataUpdater;
import moe.dituon.petpet.plugin.PluginPetService;
import moe.dituon.petpet.server.WebServer;
import moe.dituon.petpet.share.BaseConfigFactory;
import moe.dituon.petpet.share.GifAvatarExtraDataProvider;
import moe.dituon.petpet.share.TextExtraData;
Expand Down Expand Up @@ -74,15 +75,17 @@ public void readConfigByPluginAutoSave() {
break;
}

if (config.getStartWebServer()) new WebServer();

super.readPluginServiceConfig(config.toPluginServiceConfig());
}

@Override
public void readData(File dir){
if (dir.listFiles() == null) {
System.out.println(autoUpdate ?
"o((>ω< ))o 你这头懒猪, 没有下载petData!\n\\^o^/ 还好我冰雪聪明, 帮你自动更新了⭐" :
"(゚Д゚*)ノ 没有petData! 你自己手动更新吧x\n(☆-v-) 笨蛋! 让你不开自动更新⭐");
"o((>ω< ))o 你这头懒猪, 没有下载petpet模板!\n\\^o^/ 还好我冰雪聪明, 帮你自动更新了⭐" :
"(゚Д゚*)ノ 没有petpet模板! 你自己手动更新吧x\n(☆-v-) 笨蛋! 让你不开自动更新⭐");
return;
}
super.readData(dir);
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/moe/dituon/petpet/mirai/MiraiPluginConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,12 @@ object MiraiPluginConfig : AutoSavePluginConfig("PetPet")
@ValueDescription("触发图片生成后的群聊冷却时长")
val groupCoolDown: Long by value(Cooler.DEFAULT_GROUP_COOLDOWN)

@ValueDescription("触发冷却后的回复消息, '[nudge]'为戳一戳")
@ValueDescription("触发冷却后的回复消息, '[nudge]'为戳一戳, 可使用消息注入")
val inCoolDownMessage: String by value(Cooler.DEFAULT_MESSAGE)

@ValueDescription("启动WebServer (详见文档)")
val startWebServer: Boolean by value(false)

fun toPluginServiceConfig() = PluginServiceConfig(
command = command,
disabled = disabled,
Expand Down
22 changes: 9 additions & 13 deletions src/main/java/moe/dituon/petpet/server/PetDataDTO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ data class PetDataDTO(
) {
companion object {
@JvmStatic
fun stringify(dataMap: Map<String, KeyData>): String{
fun stringify(dataMap: Map<String, KeyData>): String {
val dataList: ArrayList<PetDataObject> = ArrayList()
dataMap.forEach { (key, data) ->
dataList.add(PetDataObject(
key,
data.avatar.stream().map { a -> a.type }.toList(),
data.alias ?: emptyList(),
PreviewType.GIF
))
if (data.hidden == false) dataList.add(
PetDataObject(
key,
data.avatar.stream().map { a -> a.type }.toList(),
data.alias ?: emptyList()
)
)
}
return Json.encodeToString(
PetDataDTO(VERSION, dataList)
Expand All @@ -36,12 +37,7 @@ data class PetDataDTO(
data class PetDataObject(
val key: String,
val types: List<AvatarType>,
val alias: List<String>,
val previewType: PreviewType
val alias: List<String>
)

enum class PreviewType {
PNG, GIF
}


6 changes: 3 additions & 3 deletions src/main/java/moe/dituon/petpet/server/PetHttpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ private void handleResponse(HttpExchange httpExchange, int rCode) {
}
}

private void handleResponse(HttpExchange httpExchange, InputStream input, String type) throws IOException {
static public void handleResponse(HttpExchange httpExchange, InputStream input, String mime) throws IOException {
input.reset();
httpExchange.getResponseHeaders().add("Content-Type", "image/" + type);
httpExchange.getResponseHeaders().add("Content-Type", mime);
httpExchange.sendResponseHeaders(200, input.available());
OutputStream out = httpExchange.getResponseBody();
input.transferTo(out);
Expand All @@ -100,7 +100,7 @@ private void handleResponseIndex(HttpExchange httpExchange) throws IOException {
}

private void finish(HttpExchange httpExchange, RequestParser parser) throws IOException {
handleResponse(httpExchange, parser.getImagePair().getFirst(), parser.getImagePair().getSecond());
handleResponse(httpExchange, parser.getImagePair().getFirst(), ("image/" + parser.getImagePair().getSecond()).intern());
parser.close();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public class PreviewHttpHandler implements HttpHandler {
if (!configFile.exists()) {
Files.write(configFile.toPath(), new PreviewConfigDTO().stringify().getBytes());
}
File destDir = new File(PREVIEW_ROOT);
if(!(destDir.exists()&& destDir.isDirectory())) {
if (!destDir.mkdirs()) System.out.println("can not mkdir " + PREVIEW_ROOT);
}
PreviewConfigDTO config = PreviewConfigDTO.decodeFromString(service.getFileStr(configFile));

// mimeMap = new HashMap<>(service.getDataMap().size());
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/moe/dituon/petpet/server/ServerPetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
public class ServerPetService extends BasePetService {
public static final int DEFAULT_PORT = 2333;
public static final String DEFAULT_DATA_PATH = "data/xmmt.dituon.petpet/";
public static final String CONFIG_NAME = "config.json";
public static final String CONFIG_NAME = "server-config.json";
public static final int DEFAULT_SERVER_THREAD_POOL_SIZE = 10;
public int port = DEFAULT_PORT;
public String path = DEFAULT_DATA_PATH;
public int webServerThreadPoolSize = DEFAULT_SERVER_THREAD_POOL_SIZE;
public boolean usePreview = true;
public boolean usePreview = false;
private String indexJson;

public void readConfig(ServerServiceConfig config) {
Expand All @@ -28,10 +28,12 @@ public void readConfig(ServerServiceConfig config) {
}

public void readConfig() {
File configFile = new File(CONFIG_NAME);
File configFile = new File("./" + CONFIG_NAME);
try {
if (!configFile.exists()) { //save default config
Files.write(configFile.toPath(), new ServerServiceConfig().stringify().getBytes());
var defaultConfig = new ServerServiceConfig();
defaultConfig.setPreview(usePreview);
Files.write(configFile.toPath(), defaultConfig.stringify().getBytes());
}

ServerServiceConfig config = ServerServiceConfig.parse(getFileStr(configFile));
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/moe/dituon/petpet/server/ServerServiceConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ data class ServerServiceConfig(
val port: Int = ServerPetService.DEFAULT_PORT,
val dataPath: String = ServerPetService.DEFAULT_DATA_PATH,
val webServerThreadPoolSize: Int = ServerPetService.DEFAULT_SERVER_THREAD_POOL_SIZE,
val preview: Boolean = false,
var preview: Boolean = false,

val antialias: Boolean = true,
val resampling: Boolean = true,
Expand Down Expand Up @@ -58,8 +58,8 @@ data class ServerServiceConfig(
data class PreviewConfigDTO(
val form: TargetDTO = TargetDTO("form", "https://q1.qlogo.cn/g?b=qq&nk=2544193782&s=640"),
val to: TargetDTO = TargetDTO("to", "https://q1.qlogo.cn/g?b=qq&nk=2544193782&s=640"),
val group: TargetDTO = TargetDTO("group", "file:./example-data/input/avatar1.png"),
val bot: TargetDTO = TargetDTO("bot", "file:./example-data/input/avatar2.png"),
val group: TargetDTO = TargetDTO("group", "https://q1.qlogo.cn/g?b=qq&nk=2544193782&s=640"),
val bot: TargetDTO = TargetDTO("bot", "https://q1.qlogo.cn/g?b=qq&nk=2544193782&s=640"),
val randomAvatarList: List<String> = emptyList(),
val textList: List<String> = listOf("petpet!")
) {
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/moe/dituon/petpet/server/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.sun.net.httpserver.HttpServer;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

Expand All @@ -26,12 +28,25 @@ private void init() {

try {
HttpServer httpServer = HttpServer.create(new InetSocketAddress(service.port), 0);
System.out.println("PetpetWebServer started in port " + service.port);

httpServer.createContext("/petpet", new PetHttpHandler(service));
httpServer.createContext("/preview", new PreviewHttpHandler(service));

try(InputStream stream = this.getClass().getResourceAsStream("/index.html")) {
if (stream == null) throw new Exception();
InputStream html = new ByteArrayInputStream(stream.readAllBytes());
httpServer.createContext("/", exchange -> PetHttpHandler.handleResponse(exchange, html, "text/html"));

System.out.println("WebUI-URL: http://127.0.0.1:" + service.port + '/');
if (!service.usePreview) System.out.println("Warn: WebUI preview is disabled");
} catch (Exception e){
System.out.println("Petpet WebUI is disabled");
}

httpServer.setExecutor(Executors.newFixedThreadPool(service.webServerThreadPoolSize));
httpServer.start();

System.out.println("PetpetWebServer started in port " + service.port);
apiUrl = ("http://127.0.0.1:" + service.port + "/petpet").intern();
System.out.println("API-URL: " + apiUrl);
} catch (IOException ex) {
Expand Down
10 changes: 0 additions & 10 deletions src/main/resources/config.json

This file was deleted.

Loading

0 comments on commit bcdc539

Please sign in to comment.