From b3261454d954920a1499cb9dcddb370fe056c244 Mon Sep 17 00:00:00 2001 From: snit Date: Fri, 15 Nov 2024 22:08:36 -0600 Subject: [PATCH] basic log helper and also starting mod loading fucking finally god --- CMakeLists.txt | 2 +- mods/core/init.lua | 3 ++ mods/core/mod.json | 6 ++++ server/src/game.c | 67 ++++++++++++++++++++++++++++++++++++++++++ server/src/game.h | 32 ++++++++++++++++++++ server/src/game_data.h | 17 ----------- server/src/main.c | 15 ++++++---- server/src/opts.c | 7 +++-- server/src/opts.h | 2 +- server/src/request.c | 4 +-- server/src/request.h | 4 +-- server/src/socket.c | 6 ++-- server/src/socket.h | 4 +-- 13 files changed, 134 insertions(+), 35 deletions(-) create mode 100644 mods/core/init.lua create mode 100644 mods/core/mod.json create mode 100644 server/src/game.c create mode 100644 server/src/game.h delete mode 100644 server/src/game_data.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 15ff245..e875450 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.10) project(simworld VERSION 0.0.1 LANGUAGES C) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -std=c17 -Wpedantic -pedantic-errors -Wformat=2 -Wshadow -Wwrite-strings -Wstrict-prototypes -g") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -std=gnu17 -Wpedantic -pedantic-errors -Wformat=2 -Wshadow -Wwrite-strings -Wstrict-prototypes -g") set(CMAKE_EXPORT_COMPILE_COMMANDS ON) diff --git a/mods/core/init.lua b/mods/core/init.lua new file mode 100644 index 0000000..114af66 --- /dev/null +++ b/mods/core/init.lua @@ -0,0 +1,3 @@ +-- init.lua + +print("Hello, world!") diff --git a/mods/core/mod.json b/mods/core/mod.json new file mode 100644 index 0000000..37bba9e --- /dev/null +++ b/mods/core/mod.json @@ -0,0 +1,6 @@ +{ + "name": "core", + "version": "0.0.1", + "depends": [], + "optdepends": [] +} diff --git a/server/src/game.c b/server/src/game.c new file mode 100644 index 0000000..01f480f --- /dev/null +++ b/server/src/game.c @@ -0,0 +1,67 @@ +// game.c + +#include +#include +#include +#include +#include +#include + +#include "game.h" + + +void write_log(bool is_daemon, char const *fmt, ...) { + va_list args; + va_start(args, fmt); + + if (is_daemon) vsyslog(LOG_INFO, fmt, args); + else vprintf(fmt, args); + + va_end(args); + +} + + +enum error_t game_load_mods(struct game_t *game) { + assert(game != NULL); + + if (game->options.mods_directory == NULL) return ERR_NOTFOUND; + + DIR *mods_directory = opendir(game->options.mods_directory); + if (mods_directory == NULL) return ERR_NOTFOUND; + // Imagine handling errors properly + + struct dirent *ent = NULL; + while ((ent = readdir(mods_directory)) != NULL) { + if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) + continue; + + size_t abs_path_len = strlen( + game->options.mods_directory) + strlen(ent->d_name + ); + + char* abs_path = calloc(abs_path_len, sizeof(char)); + + strcat(abs_path, game->options.mods_directory); + strcat(abs_path, "/"); + strcat(abs_path, ent->d_name); + + struct stat file_info = { 0 }; + + if (stat(abs_path, &file_info) != 0) { + free(abs_path); + return ERR_NOTFOUND; + } + free(abs_path); + + if (S_ISREG(file_info.st_mode)) + game_log(game, "FOUND REGULAR FILE: "); + else if (S_ISDIR(file_info.st_mode)) + game_log(game, "FOUND DIRECTORY: "); + + game_log(game, "%s\n", ent->d_name); + } + + closedir(mods_directory); + return ERR_OK; +} diff --git a/server/src/game.h b/server/src/game.h new file mode 100644 index 0000000..4584ea3 --- /dev/null +++ b/server/src/game.h @@ -0,0 +1,32 @@ +#ifndef GAME_H +#define GAME_H + +#include + +#include + +#include "opts.h" + +struct game_t { + struct options_t options; + + struct world_t world; + pthread_mutex_t world_lock; +}; + + +struct mod_t { + char const *path; + struct mod_t **depends; + struct mod_t **opt_depends; +}; + + +void write_log(bool, char const *, ...); + +#define game_log(game, fmt, ...) \ + write_log((game)->options.daemonise, fmt, ##__VA_ARGS__) + +enum error_t game_load_mods(struct game_t *); + +#endif diff --git a/server/src/game_data.h b/server/src/game_data.h deleted file mode 100644 index 577bff9..0000000 --- a/server/src/game_data.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef GAME_DATA_H -#define GAME_DATA_H - -#include - -#include - -#include "opts.h" - -struct game_data_t { - struct options_t options; - - struct world_t world; - pthread_mutex_t world_lock; -}; - -#endif diff --git a/server/src/main.c b/server/src/main.c index ed04089..a364d0d 100644 --- a/server/src/main.c +++ b/server/src/main.c @@ -14,7 +14,7 @@ #include "opts.h" #include "socket.h" -#include "game_data.h" +#include "game.h" static void handle_signal(int signal_no) { (void)signal_no; @@ -24,7 +24,7 @@ static void handle_signal(int signal_no) { } -static enum error_t simulation_thread_body(struct game_data_t *game) { +static enum error_t simulation_thread_body(struct game_t *game) { assert(game != NULL); // TODO: An actual game loop lol @@ -45,7 +45,7 @@ error: static enum error_t simulation_thread( pthread_t *pthread, - struct game_data_t *data + struct game_t *data ) { assert(pthread != NULL); assert(data != NULL); @@ -63,7 +63,7 @@ static enum error_t simulation_thread( int main(int argc, char **argv) { // Set up variables enum error_t err = ERR_OK; - struct game_data_t data = { 0 }; + struct game_t data = { 0 }; // Signal handling; TODO: should probably improve this @@ -83,6 +83,11 @@ int main(int argc, char **argv) { err = world_init(&data.world, 10, 10); if (err) goto handle_error; + // Load mods (yes, after world_init() though that sounds really wrong + // and most definitely should be changed) + err = game_load_mods(&data); + if (err) goto handle_error; + pthread_mutex_init(&data.world_lock, NULL); err = world_register_entity(&data.world, "john", 'j'); @@ -132,6 +137,6 @@ handle_error: opts_free(&data.options); handle_error_pre_opts: - printf("SERVER ERROR: %s\n", ERROR_STRS[err]); + write_log(data.options.daemonise, "SERVER ERROR: %s\n", ERROR_STRS[err]); return err; } diff --git a/server/src/opts.c b/server/src/opts.c index 4fdb4c0..18231ae 100644 --- a/server/src/opts.c +++ b/server/src/opts.c @@ -1,8 +1,8 @@ // opts.c -#include #include #include +#include #include #include @@ -12,7 +12,10 @@ void opts_default(struct options_t *options) { options->daemonise = false; - options->mods_directory = NULL; + + // TODO: Decide on a real default mods directory + options->mods_directory = calloc(4096, sizeof(char)); + getcwd(options->mods_directory, 4096); } diff --git a/server/src/opts.h b/server/src/opts.h index c0f90f9..9712b6c 100644 --- a/server/src/opts.h +++ b/server/src/opts.h @@ -6,7 +6,7 @@ #include struct options_t { - char const* mods_directory; + char* mods_directory; bool daemonise; }; diff --git a/server/src/request.c b/server/src/request.c index 8ad4f03..bfe3529 100644 --- a/server/src/request.c +++ b/server/src/request.c @@ -13,7 +13,7 @@ static struct response_t response_failure( static void handle_request_get_world_data( struct response_t *response, struct request_t const *request, - struct game_data_t *ctx + struct game_t *ctx ) { assert(response != NULL); assert(request != NULL); @@ -45,7 +45,7 @@ error: struct response_t handle_request( struct request_t const *request, - struct game_data_t *ctx + struct game_t *ctx ) { assert(request != NULL); assert(ctx != NULL); diff --git a/server/src/request.h b/server/src/request.h index f9ef8c4..98303eb 100644 --- a/server/src/request.h +++ b/server/src/request.h @@ -3,11 +3,11 @@ #include -#include "game_data.h" +#include "game.h" struct response_t handle_request( struct request_t const *, - struct game_data_t * + struct game_t * ); #endif diff --git a/server/src/socket.c b/server/src/socket.c index 37f1072..4293fba 100644 --- a/server/src/socket.c +++ b/server/src/socket.c @@ -48,7 +48,7 @@ void socket_free(int const *sockptr) { // use, so I don't want to include it in socket.h, but it feels off putting it // in socket.c as well just because I'm used to them all being in .h struct socket_data_t { - struct game_data_t *game_data; + struct game_t *game_data; int socket; }; @@ -77,7 +77,7 @@ static enum error_t socket_handle(struct socket_data_t *data) { } -static enum error_t socket_thread_body(struct game_data_t *data) { +static enum error_t socket_thread_body(struct game_t *data) { assert(data != NULL); enum error_t err = ERR_OK; @@ -110,7 +110,7 @@ error: } -enum error_t socket_thread(pthread_t *pthread, struct game_data_t *data) { +enum error_t socket_thread(pthread_t *pthread, struct game_t *data) { assert(pthread != NULL); assert(data != NULL); diff --git a/server/src/socket.h b/server/src/socket.h index 37397fb..9f5eada 100644 --- a/server/src/socket.h +++ b/server/src/socket.h @@ -3,7 +3,7 @@ #include -#include "game_data.h" +#include "game.h" #define SOCKET_PATH "/tmp/swd.sock" // Socket path should be a shared setting between server and client @@ -12,6 +12,6 @@ enum error_t socket_init(int *); void socket_free(int const *); -enum error_t socket_thread(pthread_t *, struct game_data_t *); +enum error_t socket_thread(pthread_t *, struct game_t *); #endif