Skip to content

Commit

Permalink
Fixed some server stuff and added thread tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilligans committed Jan 2, 2024
1 parent 936738b commit bb60ad9
Show file tree
Hide file tree
Showing 31 changed files with 145 additions and 132 deletions.
12 changes: 6 additions & 6 deletions src/main/java/dev/hilligans/ourcraft/ClientMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

public class ClientMain {

//public static GameInstance gameInstance = Ourcraft.GAME_INSTANCE;

public static ArgumentContainer argumentContainer;

public static long start = System.currentTimeMillis();
Expand All @@ -32,6 +30,7 @@ public static void main(String[] args) throws IOException {
gameInstance.handleArgs(args);
gameInstance.side = Side.CLIENT;
gameInstance.loadContent();
gameInstance.THREAD_PROVIDER.map();

Thread serverThread = null;
if(argumentContainer.getBoolean("--integratedServer", false)) {
Expand All @@ -46,17 +45,18 @@ public static void main(String[] args) throws IOException {
}

Client client = new Client(gameInstance, argumentContainer);
// Client client1 = new Client(gameInstance, argumentContainer);
Client client1 = new Client(gameInstance, argumentContainer);
String graphicsEngine = argumentContainer.getString("--graphicsEngine", null);
if(graphicsEngine != null) {
System.out.println(graphicsEngine);
client.setGraphicsEngine(gameInstance.GRAPHICS_ENGINES.get(graphicsEngine));
// client1.setGraphicsEngine(gameInstance.GRAPHICS_ENGINES.get(graphicsEngine));
client1.setGraphicsEngine(gameInstance.GRAPHICS_ENGINES.get(graphicsEngine));
}
//client1.setupClient();
client1.setupClient();
client.startClient();


gameInstance.THREAD_PROVIDER.EXECUTOR.shutdownNow();
Ourcraft.EXECUTOR.shutdownNow();
if(argumentContainer.getBoolean("--integratedServer", false)) {
ServerMain.getServer().stop();
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/dev/hilligans/ourcraft/GameInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public class GameInstance {
public final EventBus EVENT_BUS = new EventBus();
public final ModLoader MOD_LOADER = new ModLoader(this);
public final Logger LOGGER = new Logger("", "");
public final ExecutorService EXECUTOR = Executors.newFixedThreadPool(2,new NamedThreadFactory("random_executor"));
public final ResourceManager RESOURCE_MANAGER = new ResourceManager();
public final ResourceManager RESOURCE_MANAGER = new ResourceManager(this);
public final ModContent OURCRAFT = new ModContent("ourcraft",this).addClassLoader(new URLClassLoader(new URL[]{Ourcraft.class.getProtectionDomain().getCodeSource().getLocation()})).addMainClass(Ourcraft.class);
public final ContentPack CONTENT_PACK = new ContentPack(this);
public final AtomicBoolean REBUILDING = new AtomicBoolean(false);
Expand Down Expand Up @@ -332,6 +331,10 @@ public void registerLayoutEngine(ILayoutEngine<?>... layoutEngines) {
LAYOUT_ENGINES.putAll(layoutEngines);
}

public void register(IRegistryElement registryElement) {
register(registryElement.getIdentifierName(), registryElement);
}

public void register(String name, Object o) {
boolean put = false;
for(Registry<?> registry : REGISTRIES.ELEMENTS) {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/dev/hilligans/ourcraft/Ourcraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import dev.hilligans.ourcraft.resource.loaders.JsonLoader;
import dev.hilligans.ourcraft.resource.ResourceManager;
import dev.hilligans.ourcraft.schematic.LitematicaSchematicLoader;
import dev.hilligans.ourcraft.util.ArgumentContainer;
import dev.hilligans.ourcraft.util.NamedThreadFactory;
import at.favre.lib.crypto.bcrypt.BCrypt;
import dev.hilligans.ourcraft.util.Side;
Expand All @@ -55,8 +56,9 @@
public class Ourcraft {

public static final GameInstance GAME_INSTANCE = new GameInstance();
public static final ExecutorService EXECUTOR = Executors.newFixedThreadPool(2,new NamedThreadFactory("random_executor"));

public static final ArgumentContainer getArgumentContainer() {
return ClientMain.argumentContainer;
}
public static String path = System.getProperty("user.dir");

public static String hashString(String password, String salt) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/dev/hilligans/ourcraft/ServerMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static void main(String[] args) {

public static IServer server(GameInstance gameInstance, ArgumentContainer argumentContainer) {
ServerMain.argumentContainer = argumentContainer;
gameInstance.THREAD_PROVIDER.map();

//ServerWorld world = new ServerWorld(gameInstance);
//world.worldBuilders.add(new OreBuilder("ore", Blocks.GRASS,Blocks.STONE).setFrequency(20));
Expand All @@ -56,6 +57,7 @@ public static IServer server(GameInstance gameInstance, ArgumentContainer argume
server.addWorld(world1);
System.out.println("starting server");
server.startServer("25588");
gameInstance.THREAD_PROVIDER.unmap();
return server;
}

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/dev/hilligans/ourcraft/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public class Client implements IClientPacketHandler {

public int playerId;

public boolean joinServer = true;
public boolean valid = false;
public boolean screenShot = false;
public boolean renderWorld = false;
Expand All @@ -73,7 +72,6 @@ public class Client implements IClientPacketHandler {
public SoundEngine soundEngine;

public ClientPlayerData playerData = new ClientPlayerData();
//public IWorld newClientWorld = new SimpleWorld(0,"");
public IWorld newClientWorld;

public MultiPlayerServer multiPlayerServer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.hilligans.ourcraft.util.Settings;

import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class ClientPlayerData {

Expand All @@ -20,7 +21,7 @@ public class ClientPlayerData {
public boolean spectator = true;

public String authToken = "";
public String userName = "";
public String userName = "" + x.getAndIncrement();
public String email = "";
public String login_token = "";
public boolean valid_account = false;
Expand All @@ -40,4 +41,6 @@ public <T> T get(String key, T clazz) {
public <T> T set(String key, T newValue) {
return (T) arbDataMap.put(key, newValue);
}

public static AtomicInteger x = new AtomicInteger();
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public OpenGLWindow createWindow() {
renderWindow.setRenderPipeline(gameInstance.ARGUMENTS.getString("--renderPipeline", "ourcraft:menu_pipeline"));
windows.add(renderWindow);
renderWindow.setup();
GL.createCapabilities();
//GL.createCapabilities();

glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_BLEND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ public void swapBuffers(GraphicsContext graphicsContext) {
}
client.rendering = false;
client.soundEngine.tick();
if(client.screenShot) {
client.screenShot = false;
ScreenShot.takeScreenShot(this);
}
// if(client.screenShot) {
// client.screenShot = false;
// ScreenShot.takeScreenShot(this);
// }
glfwPollEvents();
tick();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ public void bindTexture(GraphicsContext graphicsContext, long texture) {
if(texture == 0) {
throw new NullPointerException();
}
if(trackingResourceAllocations) {
if(textureAllocationTracker.get((int)texture) == null) {
throw new RuntimeException("Unknown texture with texture id " + texture);
}
}
if(texture != boundTexture) {
GL20.glBindTexture(textureTypes.get((int)texture), (int)texture);
boundTexture = texture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,16 @@ public void draw(RenderWindow window, GraphicsContext graphicsContext, IGraphics
worldStack.push();
worldStack.translate((chunkX) * chunkWidth, (chunkY) * chunkHeight, (chunkZ) * chunkWidth);
if (chunk != null) {
if(chunkX-playerChunkPos.x == 0 && chunkY-playerChunkPos.y == 0 && chunkZ-playerChunkPos.z == 0) {
if (chunkX - playerChunkPos.x == 0 && chunkY - playerChunkPos.y == 0 && chunkZ - playerChunkPos.z == 0) {
worldStack.setColor(0, 255, 0, 255);
} else {
worldStack.setColor(255, 255, 0, 255);
}
impl.uploadMatrix(graphicsContext, worldStack, shaderSource);
impl.drawMesh(graphicsContext, worldStack, mesh, 0, meshBuilder.indices.size());
} else {
worldStack.setColor(255, 0, 0, 255);
}
impl.uploadMatrix(graphicsContext, worldStack, shaderSource);
impl.drawMesh(graphicsContext, worldStack, mesh, 0, meshBuilder.indices.size());
worldStack.pop();
});
impl.destroyMesh(graphicsContext, mesh);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ public class LogicalDevice {
public VulkanQueueFamilyManager queueFamilyManager;
public VkDeviceQueueCreateInfo.Buffer buffer;
public VertexBufferManager bufferManager = new VertexBufferManager(this);
public final ExecutorService cleanup = Executors.newSingleThreadExecutor(new NamedThreadFactory("Vulkan cleanup"));
public final ExecutorService cleanup;
public AtomicBoolean alive = new AtomicBoolean(true);

public LogicalDevice(PhysicalDevice physicalDevice) {
this.vulkanInstance = physicalDevice.vulkanInstance;
this.physicalDevice = physicalDevice;
this.cleanup = Executors.newSingleThreadExecutor(new NamedThreadFactory("Vulkan cleanup", physicalDevice.vulkanInstance.engine.getGameInstance()));
memoryManager = new VulkanMemoryManager(this);
getMemoryAllocations();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public Image(int width, int height) {
}

public Image(int width, int height, int format) {
this(width,height,format,ByteBuffer.allocateDirect(width * height * format));
this(width,height,format, ByteBuffer.allocateDirect(width * height * format));
}

public Image(int width, int height, int format, ByteBuffer byteBuffer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private long getLong(long size, int x, int y) {
public void assemble(GameInstance gameInstance) {
long start = System.currentTimeMillis();

executorService = Executors.newFixedThreadPool(4,new NamedThreadFactory("texture_atlas_builder"));
executorService = Executors.newFixedThreadPool(4, new NamedThreadFactory("texture_atlas_builder", gameInstance));
time = 0;
count = 0;
for(ImageLocation imageLocation : images) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public synchronized int addImage(BufferedImage img, int width) {

TextureAtlas.ImageHolder imageHolder = new TextureAtlas.ImageHolder(width,imageHolders.size(), this);
Triplet<Integer, Integer, Integer> val = imageHolder.getNextID();
gameInstance.THREAD_PROVIDER.submit(() -> imageHolder.addTexture(img, val.typeB, val.typeC));
gameInstance.THREAD_PROVIDER.execute(() -> imageHolder.addTexture(img, val.typeB, val.typeC));
imageHolders.add(imageHolder);
id = val.typeA;
imageMap.put(id,imageHolder);
Expand All @@ -65,13 +65,13 @@ public synchronized int addImage(BufferedImage img, int width) {
}

public dev.hilligans.ourcraft.client.rendering.newrenderer.Image toImage() {
BufferedImage img;
Image img;
if(imageHolders.size() != 0) {
img = imageHolders.get(0).bufferedImage;
img = imageHolders.get(0).image;
for (int x = 1; x < imageHolders.size(); x++) {
img = joinImage(img, imageHolders.get(x).bufferedImage);
img = joinImage(img, imageHolders.get(x).image=null);
}
return new Image(img);
return img;
}
return null;
}
Expand Down Expand Up @@ -129,12 +129,29 @@ public static BufferedImage joinImage(BufferedImage img1, BufferedImage img2) {
return img;
}

public static Image joinImage(Image img1, Image img2) {
int width = img1.getWidth() + img2.getWidth();
int height = Math.max(img1.getHeight(),img2.getHeight());
Image img = new Image(width,height,4);
for(int x = 0; x < img1.getWidth(); x++) {
for(int y = 0; y < img1.getHeight(); y++) {
img.putPixel(x,y,img1.getPixel(x,y));
}
}
for(int x = 0; x < img2.getWidth(); x++) {
for(int y = 0; y < img2.getHeight(); y++) {
img.putPixel(x + img1.getWidth(),y,img2.getPixel(x,y));
}
}
return img;
}

public static class ImageHolder {

public double imageSize;
int count;

public BufferedImage bufferedImage;
public Image image;

int width = 0;
int height = 0;
Expand All @@ -148,10 +165,13 @@ public ImageHolder(int imageSize, int id, TextureAtlas textureAtlas) {
this.imageSize = imageSize;
this.id = id;
this.textureAtlas = textureAtlas;
bufferedImage = new BufferedImage(textureAtlas.maxTextureSize,textureAtlas.maxTextureSize,BufferedImage.TYPE_INT_ARGB);
for(int y = 0; y < bufferedImage.getHeight(); y++) {
for(int x = 0; x < bufferedImage.getWidth(); x++) {
bufferedImage.setRGB(x,y,new Color(255,255,255,127).getRGB());
image = new Image(textureAtlas.maxTextureSize, textureAtlas.maxTextureSize, 4);
//bufferedImage = new BufferedImage(textureAtlas.maxTextureSize,textureAtlas.maxTextureSize,BufferedImage.TYPE_INT_ARGB);
int color = new Color(255,255,255,127).getRGB();
for(int y = 0; y < image.getHeight(); y++) {
for(int x = 0; x < image.getWidth(); x++) {
image.putPixel(x, y, color);
//bufferedImage.setRGB(x,y,);
}
}
count = textureAtlas.maxTextureSize / imageSize;
Expand All @@ -160,7 +180,8 @@ public ImageHolder(int imageSize, int id, TextureAtlas textureAtlas) {
public void addTexture(BufferedImage img, int width, int height) {
for(int y = 0; y < Math.min(img.getHeight(),imageSize); y++) {
for(int x = 0; x < Math.min(img.getWidth(),64); x++) {
bufferedImage.setRGB(x + width * (int)imageSize, y + height * (int)imageSize, img.getRGB(x,y));
//bufferedImage.setRGB(x + width * (int)imageSize, y + height * (int)imageSize, img.getRGB(x,y));
image.putPixel((int) (x + width * imageSize), (int) (y + height * imageSize), img.getRGB(x,y));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void render(RenderWindow window, GraphicsContext graphicsContext, MatrixS
public void joinServer() {
try {
screenBase.getClient().network.joinServer(ip,port, screenBase.getClient());
Thread.sleep(1000);
screenBase.getClient().closeScreen();
screenBase.getClient().serverIP = ip + ":" + port;
screenBase.window.queueRenderPipeline("ourcraft:new_world_pipeline");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,16 @@ public void draw1(RenderWindow window, GraphicsContext graphicsContext, MatrixSt
PrimitiveBuilder primitiveBuilder = primitiveBuilders.get(val);
primitiveBuilder.translate(1.0f,0,1.0f);
IDefaultEngineImpl<?,?> impl = window.getEngineImpl();
impl.uploadMatrix(graphicsContext,matrixStack,shaderSource);
impl.bindPipeline(graphicsContext,shaderSource.program);
impl.bindTexture(graphicsContext,textureAtlas.glTextureId);
impl.drawAndDestroyMesh(graphicsContext,matrixStack,primitiveBuilder.toVertexMesh());
if(textureAtlas.glTextureId != -1) {
impl.uploadMatrix(graphicsContext, matrixStack, shaderSource);
impl.bindPipeline(graphicsContext, shaderSource.program);
impl.bindTexture(graphicsContext, textureAtlas.glTextureId);
impl.drawAndDestroyMesh(graphicsContext, matrixStack, primitiveBuilder.toVertexMesh());
}
});
}

public Int2ObjectOpenHashMap<TextureAtlas> textureAtlases = new Int2ObjectOpenHashMap<>();
public final Int2ObjectOpenHashMap<TextureAtlas> textureAtlases = new Int2ObjectOpenHashMap<>();
public Char2IntOpenHashMap charMap = new Char2IntOpenHashMap();
public Char2IntOpenHashMap idMap = new Char2IntOpenHashMap();
public Int2BooleanArrayMap texturesBuilt = new Int2BooleanArrayMap();
Expand All @@ -172,7 +174,9 @@ public void buildChars() {
int finalX = x;
gameInstance.THREAD_PROVIDER.execute(() -> {
TextureAtlas textureAtlas = buildTextureAtlas(gameInstance, finalX);
textureAtlases.put(finalX,textureAtlas);
synchronized (textureAtlases) {
textureAtlases.put(finalX, textureAtlas);
}
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public void joinServer(String ip, String port, Client client) throws Exception {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class).handler(this);
((NetworkHandler) networkHandler).setData(b.connect(HOST, PORT).sync().channel(), group, ip, port);
} finally {
} catch (Exception e) {
e.printStackTrace();
}
flush();
}
Expand All @@ -53,6 +54,7 @@ public ClientNetwork debug(boolean debug) {

@Override
public void sendPacket(PacketBase packetBase) {
System.out.println("Sending packet:" + packetBase.getClass());
if(networkHandler != null && ((ClientNetworkHandler)networkHandler).enabled) {
packetBase.packetId = sendProtocol.packetMap.get(packetBase.getClass());
sendPacketDirect(packetBase);
Expand Down
Loading

0 comments on commit bb60ad9

Please sign in to comment.