Skip to content

Commit

Permalink
Merge pull request #14 from KasugaLibGroup/gui
Browse files Browse the repository at this point in the history
fix&feat: gui/menu crash&inspect command
  • Loading branch information
TimeBather authored Nov 23, 2024
2 parents fa4498d + 15fe24c commit 98cb0d7
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 26 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ forge*changelog.txt

node_modules
yarn.lock
package-lock.json
package-lock.json

# Javascript Build
javascript/**/lib
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
import kasuga.lib.KasugaLib;
import kasuga.lib.core.addons.node.NodePackage;
import kasuga.lib.core.base.commands.CommandHandler;
import kasuga.lib.core.client.frontend.gui.GuiInstance;
import kasuga.lib.core.javascript.JavascriptContext;
import kasuga.lib.core.javascript.JavascriptThread;
import kasuga.lib.registrations.common.CommandReg;
import kasuga.lib.registrations.registry.SimpleRegistry;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextColor;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import org.apache.http.impl.client.CloseableHttpClient;
Expand Down Expand Up @@ -83,6 +87,60 @@ public void run() {
});
}
}).submit(REGISTRY);

public static final CommandReg GUI_INSTANCES_LIST = new CommandReg("kasugalib")
.addLiteral("gui", false)
.addLiteral("instances", false)
.addLiteral("list", false)
.onlyIn(Dist.CLIENT)
.setHandler(new CommandHandler(){
@Override
public void run() {
Minecraft.getInstance().player.sendSystemMessage(Component.literal("-------- GUI Instances --------"));
KasugaLib.STACKS.GUI.ifPresent((gui)->{
gui.getAllInstances().forEach((id, instance)->{
Minecraft.getInstance().player.sendSystemMessage(
Component.literal(id.toString().substring(0,8))
.append(" ")
.append(Component.literal(instance.getLocation().toString()))
.append(" ")
.append(Component.literal("[Inspect]")
.withStyle((s)->
s.withBold(true)
.withClickEvent(
new ClickEvent(
ClickEvent.Action.RUN_COMMAND,
"/kasugalib gui instances inspect "+id
)
)
)
).withStyle((s)->s.withColor(TextColor.parseColor("#ffff00")))
);
});
});

}
}).submit(REGISTRY);

public static final CommandReg GUI_INSPECT = new CommandReg("kasugalib")
.addLiteral("gui", false)
.addLiteral("instances", false)
.addLiteral("inspect", false)
.addResourceLocation("id", false)
.onlyIn(Dist.CLIENT)
.setHandler(new CommandHandler(){
@Override
public void run() {
if(KasugaLib.STACKS.GUI.isEmpty()){
return;
}
ResourceLocation id = getParameter("id", ResourceLocation.class);
RenderSystem.recordRenderCall(()->{
Minecraft.getInstance().setScreen(KasugaLib.STACKS.GUI.get().create(id).createScreen());
});
}
}).submit(REGISTRY);

public static void invoke(){
REGISTRY.submit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public T addSource(Object source){

public LayoutNode removeSource(Object source){
LayoutNode node = this.sources.remove(source);
if(node == null){
if(Envs.isDevEnvironment()){
KasugaLib.MAIN_LOGGER.error("Source "+source.toString()+ "has not found in layout context, it may caused by multithread problems. If this still happends, please check.");
}
}
for (LayoutContext<?,N> child : children) {
node.removeChild(child.removeSource(source));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ public void unload(){
return;
this.context.getRootNode().clear();
this.closed = true;
context.appendTask(()->sideEffectContext.close());
SideEffectContext _closingContext = this.sideEffectContext;
context.appendTask(()->{
_closingContext.close();
});
context.setNotReady();
this.registryContext = null;
this.sideEffectContext = null;
this.registryContext = null;
}

public void enable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class GuiContext extends DomContext<GuiDomNode,GuiDomRoot> implements Tic

public GuiContext(GuiInstance guiInstance, DOMPriorityRegistry registry, ResourceLocation location) {
super(registry, location);
layoutEngine = LayoutEngines.YOGA;
layoutEngine = LayoutEngines.YOGA;
this.guiInstance = guiInstance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@ public GuiInstance create(UUID id, ResourceLocation location){
public Optional<GuiInstance> getInstanceById(UUID id) {
return Optional.ofNullable(localInstances.get(id));
}

public HashMap<UUID, GuiInstance> getAllInstances() {
return localInstances;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,20 @@ public Optional<GuiContext> getContext() {

public void updateSourceInfo(Object source, SourceInfo sourceInfo){
this.sourceInfos.put(source, sourceInfo);
beforeRender();
getContext().ifPresent((c)->{
c.setSourceInfo(source, sourceInfo);
});
afterRender();
}

public void removeSourceInfo(Object source){
this.sourceInfos.remove(source);
beforeRender();
getContext().ifPresent((c)->{
c.removeSourceInfo(source);
});
afterRender();
}

public void beforeRender(){
Expand All @@ -139,9 +143,6 @@ public void afterRender(){
return;
this.context.renderLock.unlock();
this.context.isRendering = false;
this.context.getRenderer().getContext().ifPresent((c)->{
c.dispatchBeforeRenderTick();
});
}

public Object getModule(String contextModuleName) {
Expand All @@ -151,4 +152,8 @@ public Object getModule(String contextModuleName) {
public void putContextObject(String contextModuleName, Object object){
contextObject.put(contextModuleName, object);
}

public ResourceLocation getLocation() {
return location;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public static String extract() throws URISyntaxException, IOException {
return librariesFolder.getAbsolutePath();
}
URI path = YogaFileExtractor.class.getProtectionDomain().getCodeSource().getLocation().toURI();
if (!path.getPath().contains(".jar")) return null;
if (!path.getPath().contains(".jar")) {
return YogaFileLocator.getDevelopmentLibrariesDirectory();
}
String jarPath = path.getPath().substring(0, path.getPath().indexOf(".jar") + 4);
JarFile jarFile = new JarFile(jarPath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static String getYogaAssemblyDirectory(){
throw new IllegalStateException("Illegal environment");
}

public static String getDevYogaAssemblyDirectory(){
return "../src/generated/resources/libraries/lwjgl-yoga-3.3.1/";
public static String getDevelopmentLibrariesDirectory(){
return "../src/generated/resources/libraries/";
}
}
14 changes: 0 additions & 14 deletions src/main/java/kasuga/lib/core/javascript/JavascriptContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,7 @@ public void close(){

Set<Tickable> tickables = new HashSet<>();

Queue<CompletableFuture> futures = new ArrayDeque<>();

public void tick(){
if(!this.futures.isEmpty()){
beforeRenderTick();
CompletableFuture future;
while((future = this.futures.poll()) != null) future.complete(null);
}
tickables.forEach(Tickable::tick);
}

Expand Down Expand Up @@ -85,13 +78,6 @@ public void beforeRenderTick(){
}
}

public CompletableFuture dispatchBeforeRenderTick(){
CompletableFuture future = new CompletableFuture();
this.futures.add(future);
return future;
}


public void enqueueAfterRenderTask(Runnable runnable) {
this.afterRenderTickTasks.add(runnable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public JavetJavascriptModule compileModuleFromSource(NodePackage packageTarget,
return new JavetJavascriptModule(
module,
packageTarget,
dirName,
fileName,
dirName,
this.context
);
}catch (JavetException e){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ private void unlisten() {

@Override
public void disable(LocatedMenuManager manager) {
this.broadcastDisable();
super.disable(manager);
this.unlisten();
this.broadcastDisable();
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/kasuga/lib/core/menu/locator/MenuLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public void disable(LocatedMenuManager manager){


public void sendUpTo(Connection connection){
if(!connection.isConnected())
return;
AllPackets.CHANNEL_REG.sendTo(
new ServerLocatorChangePacket(this, manager.asServer()),
connection,
Expand All @@ -50,6 +52,8 @@ public void sendUpTo(Connection connection){


public void sendDownTo(Connection connection){
if(!connection.isConnected())
return;
AllPackets.CHANNEL_REG.sendTo(
new ServerLocatorChangePacket(this, manager.asServer()),
connection,
Expand Down

0 comments on commit 98cb0d7

Please sign in to comment.