first commit

This commit is contained in:
sam 2024-10-21 13:18:07 +13:00
commit 72703c91c2
5 changed files with 364 additions and 0 deletions

25
.clang-format Normal file
View file

@ -0,0 +1,25 @@
BasedOnStyle: WebKit
IndentWidth: 4
TabWidth: 4
UseTab: Never
AlignConsecutiveDeclarations: false
AlignConsecutiveAssignments: false
AlignTrailingComments: true
ColumnLimit: 105
BreakBeforeBraces: Attach
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortLambdasOnASingleLine: false
PointerAlignment: Left
SpaceBeforeParens: Never
SpacesInParentheses: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: false
SpaceAfterCStyleCast: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeSquareBrackets: false
SpacesBeforeTrailingComments: 2
PenaltyBreakAssignment: 1000
NamespaceIndentation: All

8
.gitignore vendored Normal file
View file

@ -0,0 +1,8 @@
# Xmake cache
.xmake/
build/
# MacOS Cache
.DS_Store

54
.session.vim Normal file
View file

@ -0,0 +1,54 @@
let SessionLoad = 1
let s:so_save = &g:so | let s:siso_save = &g:siso | setg so=0 siso=0 | setl so=-1 siso=-1
let v:this_session=expand("<sfile>:p")
silent only
silent tabonly
cd ~/Documents/Projects/minecraft-server
if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''
let s:wipebuf = bufnr('%')
endif
let s:shortmess_save = &shortmess
if &shortmess =~ 'A'
set shortmess=aoOA
else
set shortmess=aoO
endif
badd +308 ~/.config/nvim/init.lua
argglobal
%argdel
$argadd ~/.config/nvim/init.lua
edit ~/.config/nvim/init.lua
argglobal
setlocal fdm=manual
setlocal fde=0
setlocal fmr={{{,}}}
setlocal fdi=#
setlocal fdl=0
setlocal fml=1
setlocal fdn=20
setlocal fen
silent! normal! zE
let &fdl = &fdl
let s:l = 308 - ((15 * winheight(0) + 15) / 31)
if s:l < 1 | let s:l = 1 | endif
keepjumps exe s:l
normal! zt
keepjumps 308
normal! 027|
tabnext 1
if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0 && getbufvar(s:wipebuf, '&buftype') isnot# 'terminal'
silent exe 'bwipe ' . s:wipebuf
endif
unlet! s:wipebuf
set winheight=1 winwidth=20
let &shortmess = s:shortmess_save
let s:sx = expand("<sfile>:p:r")."x.vim"
if filereadable(s:sx)
exe "source " . fnameescape(s:sx)
endif
let &g:so = s:so_save | let &g:siso = s:siso_save
set hlsearch
nohlsearch
doautoall SessionLoadPost
unlet SessionLoad
" vim: set ft=vim :

202
src/main.c Normal file
View file

@ -0,0 +1,202 @@
#include <arpa/inet.h>
#include <assert.h>
#include <netinet/in.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <zlib.h>
#define MAX_CONNECTIONS 5
#define STRING_LEN 64
typedef struct {
uint8_t packet_id;
uint8_t protocol_version;
char username[STRING_LEN];
char key[STRING_LEN];
uint8_t unused;
} __attribute__((packed)) player_ident_t;
typedef struct {
uint8_t packet_id;
uint8_t protocol_version;
char name[STRING_LEN];
char motd[STRING_LEN];
uint8_t user_type;
} __attribute__((packed)) server_ident_t;
typedef struct {
uint8_t packet_id;
} __attribute__((packed)) level_initialize_t;
typedef struct {
uint8_t packet_id;
uint16_t length;
uint8_t data[1024];
uint8_t percent_complete;
} __attribute__((packed)) chunk_t;
typedef struct {
uint8_t packet_id;
uint16_t x_size;
uint16_t y_size;
uint16_t z_size;
} __attribute__((packed)) level_finalize_t;
int gzip_compress(
unsigned char* data, size_t data_len, unsigned char** compressed_data, size_t* compressed_len) {
int ret;
z_stream stream;
// Allocate memory for the compressed data, it will be freed later
*compressed_data = (unsigned char*)malloc(
data_len + (data_len / 1000) + 12); // Some extra space for safety
if(!*compressed_data) {
printf("Failed to allocate memory for compressed data.\n");
return -1;
}
// Initialize the zlib stream
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = Z_NULL;
ret = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 | 16, 8,
Z_DEFAULT_STRATEGY); // 15 | 16 for gzip encoding
if(ret != Z_OK) {
printf("Failed to initialize zlib.\n");
return ret;
}
stream.avail_in = data_len; // Input data size
stream.next_in = data; // Input data pointer
stream.avail_out = data_len + (data_len / 1000) + 12; // Output buffer size (safe estimate)
stream.next_out = *compressed_data; // Output buffer
// Compress the data
ret = deflate(&stream, Z_FINISH);
if(ret != Z_STREAM_END) {
printf("Compression failed.\n");
deflateEnd(&stream);
free(*compressed_data);
return ret;
}
*compressed_len = stream.total_out; // Set the actual compressed length
// Clean up zlib
deflateEnd(&stream);
return Z_OK;
}
int main() {
int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
assert(sock >= 0 && "Failed to create socket");
int opt = 1;
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
struct sockaddr_in serv_addr = {
.sin_family = AF_INET,
.sin_addr.s_addr = INADDR_ANY,
.sin_port = htons(25565),
};
int result = bind(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
assert(result == 0 && "Socket bind failed");
int conn;
while(1) {
result = listen(sock, MAX_CONNECTIONS);
assert(result == 0 && "Failed to listen on socket");
struct sockaddr_in client_addr = { 0 };
socklen_t size = sizeof(client_addr);
conn = accept(sock, (struct sockaddr*)&client_addr, &size);
assert(conn >= 0 && "Failed to accept connection");
char client_ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &client_addr.sin_addr, client_ip, sizeof(client_ip));
printf("Connection accepted from %s\n", client_ip);
char buf[1024] = { 0 };
result = recv(conn, buf, 1024, 0);
assert(result >= 0 && "Failed to recieve data");
if(buf[0] == 0x00) {
player_ident_t* player_ident = (player_ident_t*)&buf;
if(player_ident->protocol_version == 0) {
continue;
}
printf("Protocol Version: %x\nUsername: %s\nVerification Key: %s\n",
player_ident->protocol_version, player_ident->username, player_ident->key);
server_ident_t server_ident = {
.packet_id = 0x00,
.protocol_version = 0x07,
.name = "hello",
.motd = "world",
.user_type = 0x64,
};
result = send(conn, &server_ident, sizeof(server_ident_t), 0);
printf("%d bytes sent\n", result);
level_initialize_t level_initialize = {
.packet_id = 0x02,
};
result = send(conn, &level_initialize, sizeof(level_initialize_t), 0);
printf("%d bytes sent\n", result);
uint8_t chunk_data[16 * 16 * 2];
memset(chunk_data, 1, sizeof(chunk_data));
uint8_t* compressed_chunk;
size_t compressed_size;
result = gzip_compress(chunk_data, sizeof(chunk_data), &compressed_chunk, &compressed_size);
printf("Status: %d, compressed: %zu\n", result, compressed_size);
chunk_t chunk = {
.packet_id = 0x03,
.length = htons(compressed_size),
.data = { 0 },
.percent_complete = 100,
};
memcpy(chunk.data, compressed_chunk, compressed_size);
for(int i = 0; i < sizeof(chunk); i++) {
printf("0x%02x ", ((unsigned char*)&chunk)[i]);
}
printf("\n");
result = send(conn, &chunk, sizeof(chunk_t), 0);
printf("%d bytes sent\n", result);
level_finalize_t level_finalize = {
.packet_id = 0x04,
.x_size = htons(16),
.y_size = htons(1),
.z_size = htons(16),
};
result = send(conn, &level_finalize, sizeof(level_finalize_t), 0);
printf("%d bytes sent\n", result);
}
}
close(conn);
close(sock);
return 0;
}

75
xmake.lua Normal file
View file

@ -0,0 +1,75 @@
add_rules("mode.debug", "mode.release")
target("minecraft-server")
set_kind("binary")
add_files("src/*.c")
add_links("z")
--
-- If you want to known more usage about xmake, please see https://xmake.io
--
-- ## FAQ
--
-- You can enter the project directory firstly before building project.
--
-- $ cd projectdir
--
-- 1. How to build project?
--
-- $ xmake
--
-- 2. How to configure project?
--
-- $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release]
--
-- 3. Where is the build output directory?
--
-- The default output directory is `./build` and you can configure the output directory.
--
-- $ xmake f -o outputdir
-- $ xmake
--
-- 4. How to run and debug target after building project?
--
-- $ xmake run [targetname]
-- $ xmake run -d [targetname]
--
-- 5. How to install target to the system directory or other output directory?
--
-- $ xmake install
-- $ xmake install -o installdir
--
-- 6. Add some frequently-used compilation flags in xmake.lua
--
-- @code
-- -- add debug and release modes
-- add_rules("mode.debug", "mode.release")
--
-- -- add macro definition
-- add_defines("NDEBUG", "_GNU_SOURCE=1")
--
-- -- set warning all as error
-- set_warnings("all", "error")
--
-- -- set language: c99, c++11
-- set_languages("c99", "c++11")
--
-- -- set optimization: none, faster, fastest, smallest
-- set_optimize("fastest")
--
-- -- add include search directories
-- add_includedirs("/usr/include", "/usr/local/include")
--
-- -- add link libraries and search directories
-- add_links("tbox")
-- add_linkdirs("/usr/local/lib", "/usr/lib")
--
-- -- add system link libraries
-- add_syslinks("z", "pthread")
--
-- -- add compilation and link flags
-- add_cxflags("-stdnolib", "-fno-strict-aliasing")
-- add_ldflags("-L/usr/local/lib", "-lpthread", {force = true})
--
-- @endcode
--