From 5c27b683f584be495c660b2b88e494cb4a632aed Mon Sep 17 00:00:00 2001 From: LeotomasMC <7706960+LeotomasMC@users.noreply.github.com> Date: Sun, 7 Aug 2022 01:00:45 -0400 Subject: [PATCH] new build system that is actually good and cool + update readme --- .gitignore | 3 +- build.hxml | 6 - build.hxp | 315 ++++++++++++++++++ readme.md | 12 +- src/Connection.hx | 8 +- src/game/GameServer.hx | 5 + src/packet/serverbound/HandshakePacket.hx | 4 - .../serverbound/LoginPluginResponsePacket.hx | 7 + test.sh | 29 +- version.txt | 1 + 10 files changed, 370 insertions(+), 20 deletions(-) delete mode 100644 build.hxml create mode 100644 build.hxp create mode 100644 src/packet/serverbound/LoginPluginResponsePacket.hx create mode 100644 version.txt diff --git a/.gitignore b/.gitignore index cded043..d84ce56 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build/ -test/ \ No newline at end of file +test/ +out/ \ No newline at end of file diff --git a/build.hxml b/build.hxml deleted file mode 100644 index 5d25f29..0000000 --- a/build.hxml +++ /dev/null @@ -1,6 +0,0 @@ --p src --m Main --L uuid -# i might remove `uuid` as a dependancy at some point ---cpp ./build/ ---cmd ./test.sh \ No newline at end of file diff --git a/build.hxp b/build.hxp new file mode 100644 index 0000000..14f606b --- /dev/null +++ b/build.hxp @@ -0,0 +1,315 @@ +import hxp.*; + +class Build extends Script { + public function new() { + super(); + + switch command { + case 'build': + command_build(); + case 'buildall': + command_buildAll(); + case 'test': + command_test(); + case 'run': + command_run(); + default: + command_build(); + } + } + + function getProjectVersion():String { + return System.readText('./version.txt'); + } + + function command_build() { + if (flags.get('all')) { + Log.warn('Consider using the `buildall` command instead of the `-all` target flag to build for all targets.'); + command_buildAll(); + return; + } + + if (flags.get('cpp') || flags.get('c++') || flags.get('cplusplus')) { + build_cpp(); + } + + if (flags.get('cs') || flags.get('csharp')) { + build_csharp(); + } + + if (flags.get('hl') || flags.get('hashlink')) { + build_hashlink(); + } + + if (flags.get('java')) { + build_java(); + } + + if (flags.get('node') || flags.get('js') || flags.get('nodejs')) { + build_nodejs(); + } + + if (flags.get('jvm')) { + build_jvm(); + } + + if (flags.get('neko')) { + build_neko(); + } + + if (flags.get('py') || flags.get('python')) { + build_python(); + } + } + + function command_buildAll() { + build_cpp(); + build_csharp(); + build_hashlink(); + build_java(); + //build_nodejs(); // hxnodejs doesnt support threads + build_jvm(); + build_neko(); + //build_python(); // python compiles... with a syntax error... + } + + function createBaseHXML():HXML { + var hxml = new HXML({cp: ['src'], main: 'Main', libs: ['uuid']}); + if (flags.get('debug')) hxml.debug = true; + return hxml; + } + + function getExecutableName(target:String, arch:HostArchitecture, windows:Bool=false, debug:Bool=false) { + var name = 'Cuttlefish'; + + if (debug) name += '.dev'; + name += '-${getProjectVersion()}'; + + name += switch target { + case 'cpp': '.cpp'; + case 'csharp': '.cs'; + case 'hashlink': '.hl'; + case 'java': '.jar'; + case 'nodejs': 'js'; + case 'jvm': '.jvm.jar'; + case 'neko': '.n'; + case 'python': '.py'; + default: throw 'unknown target'; + } + + if (['cpp', 'csharp'].contains(target)) { + name += switch arch { + case ARMV6: '.armv6'; + case ARMV7: '.armv7'; + case X86: '.x86'; + case X64: '.x64'; + }; + if (windows) name += '.exe'; + } + return name; + } + + function build_cpp() { + var hxml = createBaseHXML(); + hxml.cpp = './build/cpp/'; + hxml.build(); + + var executablePath = './build/cpp/Main'; + if (flags.get('debug')) executablePath += '-debug'; + if (System.hostPlatform == WINDOWS) executablePath += '.exe'; + + var targetName = getExecutableName('cpp', System.hostArchitecture, System.hostPlatform == WINDOWS, flags.get('debug')); + + //System.copyFile('./build/cpp/$executablePath', './out/$targetName'); + System.copyFile(executablePath, './out/$targetName'); + System.runCommand('./out/', 'chmod', ['a+x', targetName]); + } + + function build_csharp() { + var hxml = createBaseHXML(); + hxml.cs = './build/csharp/'; + hxml.build(); + + var executablePath = './build/csharp/bin/Main'; + if (flags.get('debug')) executablePath += '-Debug'; + //if (System.hostPlatform == WINDOWS) executablePath += '.exe'; + executablePath += '.exe'; + + //var targetName = getExecutableName('csharp', System.hostArchitecture, System.hostPlatform == WINDOWS, flags.get('debug')); + var targetName = getExecutableName('csharp', System.hostArchitecture, true, flags.get('debug')); + + System.copyFile(executablePath, './out/$targetName'); + if (hxml.debug) System.copyFile('$executablePath.mdb', './out/$targetName.mdb'); + } + + function build_hashlink() { + var hxml = createBaseHXML(); + hxml.hl = './build/hashlink/Cuttlefish.hl'; + hxml.build(); + + var targetName = getExecutableName('hashlink', System.hostArchitecture, System.hostPlatform == WINDOWS, flags.get('debug')); + + System.copyFile('./build/hashlink/Cuttlefish.hl', './out/$targetName'); + } + + function build_jvm() { + var hxml = createBaseHXML(); + hxml.java = './build/jvm/'; + hxml.define('jvm', true); + hxml.build(); + + var targetName = getExecutableName('jvm', System.hostArchitecture, System.hostPlatform == WINDOWS, flags.get('debug')); + + System.copyFile('./build/jvm/${flags.get('debug') ? 'Main-Debug' : 'Main'}.jar', './out/$targetName'); + } + + function build_nodejs() { + throw 'NodeJS is currently not supported! hxnodejs doesnt support threads!'; + } + + function build_java() { + var hxml = createBaseHXML(); + hxml.java = './build/java/'; + hxml.build(); + + var targetName = getExecutableName('java', System.hostArchitecture, System.hostPlatform == WINDOWS, flags.get('debug')); + + System.copyFile('./build/java/${flags.get('debug') ? 'Main-Debug.jar' : 'Main.jar'}', './out/$targetName'); + } + + function build_neko() { + var hxml = createBaseHXML(); + hxml.neko = './build/neko/Cuttlefish.n'; + hxml.build(); + + var targetName = getExecutableName('neko', System.hostArchitecture, System.hostPlatform == WINDOWS, flags.get('debug')); + + System.copyFile('./build/neko/Cuttlefish.n', './out/$targetName'); + } + + function build_python() { + Log.warn('Python does not currently work, but it does compile!'); + var hxml = createBaseHXML(); + hxml.python = './build/python/Cuttlefish.py'; + hxml.build(); + + var targetName = getExecutableName('python', System.hostArchitecture, System.hostPlatform == WINDOWS, flags.get('debug')); + + System.copyFile('./build/python/Cuttlefish.py', './out/$targetName'); + } + + function command_test() { + switch commandArgs[0] { + case 'cpp': test_cpp(); + case 'cplusplus': test_cpp(); + case 'c++': test_cpp(); + case 'cs': test_csharp(); + case 'csharp': test_csharp(); + case 'c#': test_csharp(); + case 'hl': test_hashlink(); + case 'hashlink': test_hashlink(); + case 'java': test_java(); + case 'nodejs': test_nodejs(); + case 'node': test_nodejs(); + case 'js': test_nodejs(); + case 'jvm': test_jvm(); + case 'neko': test_neko(); + case 'python': test_python(); + case 'py': test_python(); + } + } + + function test_cpp() { + build_cpp(); + run_cpp(); + } + + function test_csharp() { + build_csharp(); + run_csharp(); + } + + function test_hashlink() { + build_hashlink(); + run_hashlink(); + } + + function test_java() { + build_java(); + run_java(); + } + + function test_nodejs() { + build_nodejs(); + run_nodejs(); + } + + function test_jvm() { + build_jvm(); + run_jvm(); + } + + function test_neko() { + build_neko(); + run_neko(); + } + + function test_python() { + build_python(); + run_python(); + } + + function command_run() { + switch commandArgs[0] { + case 'cpp': run_cpp(); + case 'cplusplus': run_cpp(); + case 'c++': run_cpp(); + case 'cs': run_csharp(); + case 'csharp': run_csharp(); + case 'c#': run_csharp(); + case 'hl': run_hashlink(); + case 'hashlink': run_hashlink(); + case 'java': run_java(); + case 'nodejs': run_nodejs(); + case 'node': run_nodejs(); + case 'js': run_nodejs(); + case 'jvm': run_jvm(); + case 'neko': run_neko(); + case 'python': run_python(); + case 'py': run_python(); + } + } + + function run_cpp() { + System.runCommand('./out/', './' + getExecutableName('cpp', System.hostArchitecture, System.hostPlatform == WINDOWS, flags.get('debug'))); + } + + function run_csharp() { + if (System.hostPlatform == WINDOWS) System.runCommand('./out/', getExecutableName('csharp', System.hostArchitecture, true, flags.get('debug'))); + else System.runCommand('./out/', 'wine', [getExecutableName('csharp', System.hostArchitecture, true, flags.get('debug'))]); + } + + function run_hashlink() { + System.runCommand('./out/', 'hl', [getExecutableName('hashlink', System.hostArchitecture, System.hostPlatform == WINDOWS, flags.get('debug'))]); + } + + function run_java() { + System.runCommand('./out/', 'java', ['-jar', getExecutableName('java', System.hostArchitecture, System.hostPlatform == WINDOWS, flags.get('debug'))]); + } + + function run_nodejs() { + // idk lol! + } + + function run_jvm() { + System.runCommand('./out/', 'java', ['-jar', getExecutableName('jvm', System.hostArchitecture, System.hostPlatform == WINDOWS, flags.get('debug'))]); + } + + function run_neko() { + System.runCommand('./out/', 'neko', [getExecutableName('neko', System.hostArchitecture, System.hostPlatform == WINDOWS, flags.get('debug'))]); + } + + function run_python() { + System.runCommand('./out/', 'python3', [getExecutableName('python', System.hostArchitecture, System.hostPlatform == WINDOWS, flags.get('debug'))]); + } +} \ No newline at end of file diff --git a/readme.md b/readme.md index 5353a3d..83d6d1c 100644 --- a/readme.md +++ b/readme.md @@ -9,4 +9,14 @@ A Minecraft server written in Haxe. ## Features Being Worked On - login success packet (to login to the server) - protocol encryption -- some other background stuff \ No newline at end of file +- some other background stuff + +## Compiling +Being written in Haxe, Cuttlefish can be compiled to many different targets. Cuttlefish only supports sys targets that have threading support. This means that the supported targets are currently C++, C#, Hashlink, Java (or JVM bytecode directly), Neko, and Python. NodeJS is not supported because `hxnodejs` does not support threads yet. To test without worrying about downloading anything extra, the following should work: +```git clone https://github.com/LeotomasMC/Cuttlefish.git +cd Cuttlefish +haxelib install uuid +haxelib install hxp +haxelib run hxp --install-hxp-alias +hxp test neko``` +the last command `hxp test neko` will compile Cuttlefish for the Neko target, and then run the compiled output automatically. \ No newline at end of file diff --git a/src/Connection.hx b/src/Connection.hx index fedd388..6cb4d79 100644 --- a/src/Connection.hx +++ b/src/Connection.hx @@ -1,5 +1,6 @@ package; +import haxe.Exception; import haxe.io.Bytes; import game.BlockLocation; import haxe.Int64; @@ -72,11 +73,11 @@ class Connection { while (socketConnected) { try { var packet = readPacket(); - //trace('packet: ${Type.getClassName(Type.getClass(packet))}'); - //packet.handle(this); } catch (e:haxe.io.Eof) { trace('eof, stopping thread'); return; + } catch (e:Exception) { + trace(e.stack.toString()); } } } @@ -105,7 +106,6 @@ class Connection { case Login: var packet = LoginStartPacket.read(this); this._player = new Player(Uuid.v3(packet.name, 'OfflinePlayer'), packet.name); - trace('sending lsp'); new LoginSuccessPacket().send(this, UUID.fromString(player.uuid), player.name); state = Play; packet; @@ -115,9 +115,7 @@ class Connection { case 0x01: switch state { case Status: - trace('status ping'); var packet = PingRequestPacket.read(input); - trace('read packet'); PingResponsePacket.sendFromRequestPacket(this, packet); //socket.close(); //socketConnected = false; diff --git a/src/game/GameServer.hx b/src/game/GameServer.hx index 73dbeaf..0a66a10 100644 --- a/src/game/GameServer.hx +++ b/src/game/GameServer.hx @@ -25,8 +25,13 @@ class GameServer { } public function run() { + #if java + listeningSocket.listen(9999); + #end while (true) { + #if !java listeningSocket.listen(1); + #end var client = listeningSocket.accept(); if (client == null) continue; this.connections.push(new Connection(client)); diff --git a/src/packet/serverbound/HandshakePacket.hx b/src/packet/serverbound/HandshakePacket.hx index ca31a02..6399590 100644 --- a/src/packet/serverbound/HandshakePacket.hx +++ b/src/packet/serverbound/HandshakePacket.hx @@ -22,8 +22,4 @@ class HandshakePacket extends ServerboundPacket { packet.status = stream.readVarInt(); return packet; } - - override public function handle(client:Connection) { - //new StatusResponsePacket().send(client); - } } \ No newline at end of file diff --git a/src/packet/serverbound/LoginPluginResponsePacket.hx b/src/packet/serverbound/LoginPluginResponsePacket.hx new file mode 100644 index 0000000..29d7dc5 --- /dev/null +++ b/src/packet/serverbound/LoginPluginResponsePacket.hx @@ -0,0 +1,7 @@ +package packet.serverbound; + +class LoginPluginResponsePacket extends ServerboundPacket { + public function new() { + super(0x00); + } +} \ No newline at end of file diff --git a/test.sh b/test.sh index 20e3ebc..f0d7257 100755 --- a/test.sh +++ b/test.sh @@ -1,3 +1,26 @@ -cp ./build/Main ./test/Main -cd test -./Main \ No newline at end of file +#!/bin/bash + +while getops u:a:f: flag +do +case "${flag}" +in +hl) runHl; +cpp) runCpp; +esac +done + +toTestDir() { + cd test +} + +runCpp() { + cp ./build/Main ./test/Cuttlefish.x86 + toTestDir; + ./Cuttlefish.x86 +} + +runHl() { + cp ./build/hashlink/build.hl ./test/Cuttlefish.hl + toTestDir; + hl ./Cuttlefish.hl +} diff --git a/version.txt b/version.txt new file mode 100644 index 0000000..7bcd0e3 --- /dev/null +++ b/version.txt @@ -0,0 +1 @@ +0.0.2 \ No newline at end of file