Skip to content

Commit

Permalink
Updated web server to use my own library
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Aug 13, 2024
1 parent 67598e8 commit 5b11da7
Show file tree
Hide file tree
Showing 28 changed files with 279 additions and 709 deletions.
11 changes: 5 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ repositories {
url "https://maven.latvian.dev/releases"
content {
includeGroup "dev.latvian.mods"
includeGroup "dev.latvian.apps"
}
}

Expand Down Expand Up @@ -125,16 +126,14 @@ dependencies {
compileOnly("me.shedaniel:RoughlyEnoughItems-neoforge:$rei_version")
compileOnly("dev.emi:emi-neoforge:$emi_version:api")

/*
jarJar(implementation 'org.java-websocket:Java-WebSocket') {
jarJar(implementation 'dev.latvian.apps:tiny-java-server') {
version {
strictly '[1.5,)'
prefer "$java_websocket_version"
strictly '[1.0,)'
prefer "$tiny_server_version"
}
}

additionalRuntimeClasspath("org.java-websocket:Java-WebSocket:$java_websocket_version")
*/
additionalRuntimeClasspath("dev.latvian.apps:tiny-java-server:$tiny_server_version")
}

processResources {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod_version=2100.7.0
neoforge_version=21.0.163
parchment_version=2024.07.28
rhino_version=2100.2.5-build.46
java_websocket_version=1.5.7
tiny_server_version=1.0.0-build.1

architectury_version=13.0.6
rei_version=16.0.744
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import dev.latvian.apps.tinyserver.ServerRegistry;
import dev.latvian.mods.kubejs.bindings.AABBWrapper;
import dev.latvian.mods.kubejs.bindings.BlockWrapper;
import dev.latvian.mods.kubejs.bindings.ColorWrapper;
Expand Down Expand Up @@ -152,8 +153,7 @@
import dev.latvian.mods.kubejs.util.Tristate;
import dev.latvian.mods.kubejs.util.UtilsJS;
import dev.latvian.mods.kubejs.util.registrypredicate.RegistryPredicate;
import dev.latvian.mods.kubejs.web.KJSHTTPContext;
import dev.latvian.mods.kubejs.web.WebServerRegistry;
import dev.latvian.mods.kubejs.web.KJSHTTPRequest;
import dev.latvian.mods.kubejs.web.local.KubeJSWeb;
import dev.latvian.mods.rhino.type.RecordTypeInfo;
import dev.latvian.mods.rhino.type.TypeInfo;
Expand Down Expand Up @@ -721,7 +721,7 @@ public void registerDataComponentTypeDescriptions(DataComponentTypeInfoRegistry
}

@Override
public void registerLocalWebServer(WebServerRegistry<KJSHTTPContext> registry) {
public void registerLocalWebServer(ServerRegistry<KJSHTTPRequest> registry) {
KubeJSWeb.register(registry);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package dev.latvian.mods.kubejs.client;

import dev.latvian.apps.tinyserver.ServerRegistry;
import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.bindings.event.ClientEvents;
import dev.latvian.mods.kubejs.event.EventGroupRegistry;
import dev.latvian.mods.kubejs.plugin.KubeJSPlugin;
import dev.latvian.mods.kubejs.script.BindingRegistry;
import dev.latvian.mods.kubejs.script.PlatformWrapper;
import dev.latvian.mods.kubejs.util.ScheduledEvents;
import dev.latvian.mods.kubejs.web.KJSHTTPContext;
import dev.latvian.mods.kubejs.web.WebServerRegistry;
import dev.latvian.mods.kubejs.web.KJSHTTPRequest;
import dev.latvian.mods.kubejs.web.local.client.KubeJSClientWeb;
import net.minecraft.client.Minecraft;
import net.neoforged.fml.ModList;
Expand All @@ -34,7 +34,7 @@ public void registerBindings(BindingRegistry bindings) {
}

@Override
public void registerLocalWebServer(WebServerRegistry<KJSHTTPContext> registry) {
public void registerLocalWebServer(ServerRegistry<KJSHTTPRequest> registry) {
KubeJSClientWeb.register(registry);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.latvian.mods.kubejs.plugin;

import com.google.gson.JsonElement;
import dev.latvian.apps.tinyserver.ServerRegistry;
import dev.latvian.mods.kubejs.block.entity.BlockEntityAttachmentType;
import dev.latvian.mods.kubejs.client.LangKubeEvent;
import dev.latvian.mods.kubejs.core.RecipeManagerKJS;
Expand All @@ -24,8 +25,7 @@
import dev.latvian.mods.kubejs.script.TypeWrapperRegistry;
import dev.latvian.mods.kubejs.server.DataExport;
import dev.latvian.mods.kubejs.util.AttachedData;
import dev.latvian.mods.kubejs.web.KJSHTTPContext;
import dev.latvian.mods.kubejs.web.WebServerRegistry;
import dev.latvian.mods.kubejs.web.KJSHTTPRequest;
import dev.latvian.mods.rhino.Context;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
Expand Down Expand Up @@ -95,7 +95,7 @@ default void registerRecipeViewerEntryTypes(Consumer<RecipeViewerEntryType> regi
default void registerDataComponentTypeDescriptions(DataComponentTypeInfoRegistry registry) {
}

default void registerLocalWebServer(WebServerRegistry<KJSHTTPContext> registry) {
default void registerLocalWebServer(ServerRegistry<KJSHTTPRequest> registry) {
}

default void attachServerData(AttachedData<MinecraftServer> event) {
Expand Down
35 changes: 14 additions & 21 deletions src/main/java/dev/latvian/mods/kubejs/script/ConsoleJS.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.latvian.mods.kubejs.script;

import com.google.gson.JsonArray;
import dev.latvian.apps.tinyserver.http.response.HTTPResponse;
import dev.latvian.apps.tinyserver.ws.WSHandler;
import dev.latvian.mods.kubejs.DevProperties;
import dev.latvian.mods.kubejs.bindings.TextIcons;
import dev.latvian.mods.kubejs.error.KubeRuntimeException;
Expand All @@ -11,10 +12,10 @@
import dev.latvian.mods.kubejs.util.TimeJS;
import dev.latvian.mods.kubejs.util.UtilsJS;
import dev.latvian.mods.kubejs.util.WrappedJS;
import dev.latvian.mods.kubejs.web.KJSHTTPContext;
import dev.latvian.mods.kubejs.web.http.HTTPResponse;
import dev.latvian.mods.kubejs.web.http.SimpleHTTPResponse;
import dev.latvian.mods.kubejs.web.ws.WSHandler;
import dev.latvian.mods.kubejs.web.JsonContent;
import dev.latvian.mods.kubejs.web.KJSHTTPRequest;
import dev.latvian.mods.kubejs.web.KJSWSSession;
import dev.latvian.mods.kubejs.web.local.KubeJSWeb;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.ContextFactory;
import dev.latvian.mods.rhino.EcmaError;
Expand Down Expand Up @@ -78,7 +79,7 @@ public static ConsoleJS getCurrent(@Nullable Context cx) {
private final List<String> writeQueue;
private final Calendar calendar;
public WeakReference<ContextFactory> contextFactory;
public WSHandler wsBroadcaster;
public WSHandler<KJSHTTPRequest, KJSWSSession> wsBroadcaster;

public ConsoleJS(ScriptType m, Logger log) {
this.scriptType = m;
Expand Down Expand Up @@ -247,7 +248,7 @@ private ConsoleLine log(LogType type, SourceLine sourceLine, @Nullable Throwable
writeToFile(type, line.timestamp, line.getText());

if (wsBroadcaster != null) {
wsBroadcaster.broadcast(type.id, line::toJson);
KubeJSWeb.broadcastEvent(wsBroadcaster, type.id, line);
}
}

Expand Down Expand Up @@ -605,27 +606,19 @@ public int compareTo(VarFunc o) {
}
}

public HTTPResponse getErrorsResponse(KJSHTTPContext ctx) {
return SimpleHTTPResponse.lazyJson(() -> {
var json = new JsonArray();

public HTTPResponse getErrorsResponse(KJSHTTPRequest ctx) {
return HTTPResponse.ok().content(JsonContent.array(json -> {
for (var error : errors) {
json.add(error.toJson());
}

return json;
});
}));
}

public HTTPResponse getWarningsResponse(KJSHTTPContext ctx) {
return SimpleHTTPResponse.lazyJson(() -> {
var json = new JsonArray();

public HTTPResponse getWarningsResponse(KJSHTTPRequest ctx) {
return HTTPResponse.ok().content(JsonContent.array(json -> {
for (var error : warnings) {
json.add(error.toJson());
}

return json;
});
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

public class ConsoleLine {
public class ConsoleLine implements Supplier<JsonElement> {
public static final ConsoleLine[] EMPTY_ARRAY = new ConsoleLine[0];

public static final StreamCodec<FriendlyByteBuf, ConsoleLine> STREAM_CODEC = new StreamCodec<>() {
Expand Down Expand Up @@ -174,4 +175,9 @@ public JsonObject toJson() {

return json;
}

@Override
public JsonElement get() {
return toJson();
}
}
21 changes: 13 additions & 8 deletions src/main/java/dev/latvian/mods/kubejs/script/ScriptManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,19 @@ private void load() {
scriptType.fileWatcherThread.start();
}

var broadcast = new JsonObject();
broadcast.addProperty("type", scriptType.name);
broadcast.addProperty("total", t);
broadcast.addProperty("successful", i);
broadcast.addProperty("errors", scriptType.console.errors.size());
broadcast.addProperty("warnings", scriptType.console.warnings.size());
broadcast.addProperty("time", ms);
KubeJSWeb.UPDATES.broadcast("scripts_reloaded", broadcast);
int t1 = t;
int i1 = i;

KubeJSWeb.broadcastUpdate("scripts_reloaded", () -> {
var broadcast = new JsonObject();
broadcast.addProperty("type", scriptType.name);
broadcast.addProperty("total", t1);
broadcast.addProperty("successful", i1);
broadcast.addProperty("errors", scriptType.console.errors.size());
broadcast.addProperty("warnings", scriptType.console.warnings.size());
broadcast.addProperty("time", ms);
return broadcast;
});
}

public void loadAdditional() {
Expand Down
93 changes: 0 additions & 93 deletions src/main/java/dev/latvian/mods/kubejs/web/CompiledPath.java

This file was deleted.

Loading

0 comments on commit 5b11da7

Please sign in to comment.