basic log helper and also starting mod loading fucking finally god

This commit is contained in:
snit 2024-11-15 22:08:36 -06:00
parent 4f93992fba
commit b3261454d9
13 changed files with 134 additions and 35 deletions

View File

@ -3,7 +3,7 @@
cmake_minimum_required(VERSION 3.10) cmake_minimum_required(VERSION 3.10)
project(simworld VERSION 0.0.1 LANGUAGES C) 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) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

3
mods/core/init.lua Normal file
View File

@ -0,0 +1,3 @@
-- init.lua
print("Hello, world!")

6
mods/core/mod.json Normal file
View File

@ -0,0 +1,6 @@
{
"name": "core",
"version": "0.0.1",
"depends": [],
"optdepends": []
}

67
server/src/game.c Normal file
View File

@ -0,0 +1,67 @@
// game.c
#include <assert.h>
#include <stdarg.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syslog.h>
#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;
}

32
server/src/game.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef GAME_H
#define GAME_H
#include <pthread.h>
#include <world.h>
#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

View File

@ -1,17 +0,0 @@
#ifndef GAME_DATA_H
#define GAME_DATA_H
#include <pthread.h>
#include <world.h>
#include "opts.h"
struct game_data_t {
struct options_t options;
struct world_t world;
pthread_mutex_t world_lock;
};
#endif

View File

@ -14,7 +14,7 @@
#include "opts.h" #include "opts.h"
#include "socket.h" #include "socket.h"
#include "game_data.h" #include "game.h"
static void handle_signal(int signal_no) { static void handle_signal(int signal_no) {
(void)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); assert(game != NULL);
// TODO: An actual game loop lol // TODO: An actual game loop lol
@ -45,7 +45,7 @@ error:
static enum error_t simulation_thread( static enum error_t simulation_thread(
pthread_t *pthread, pthread_t *pthread,
struct game_data_t *data struct game_t *data
) { ) {
assert(pthread != NULL); assert(pthread != NULL);
assert(data != NULL); assert(data != NULL);
@ -63,7 +63,7 @@ static enum error_t simulation_thread(
int main(int argc, char **argv) { int main(int argc, char **argv) {
// Set up variables // Set up variables
enum error_t err = ERR_OK; enum error_t err = ERR_OK;
struct game_data_t data = { 0 }; struct game_t data = { 0 };
// Signal handling; TODO: should probably improve this // Signal handling; TODO: should probably improve this
@ -83,6 +83,11 @@ int main(int argc, char **argv) {
err = world_init(&data.world, 10, 10); err = world_init(&data.world, 10, 10);
if (err) goto handle_error; 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); pthread_mutex_init(&data.world_lock, NULL);
err = world_register_entity(&data.world, "john", 'j'); err = world_register_entity(&data.world, "john", 'j');
@ -132,6 +137,6 @@ handle_error:
opts_free(&data.options); opts_free(&data.options);
handle_error_pre_opts: 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; return err;
} }

View File

@ -1,8 +1,8 @@
// opts.c // opts.c
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <unistd.h>
#include <sys/syslog.h> #include <sys/syslog.h>
#include <getopt.h> #include <getopt.h>
@ -12,7 +12,10 @@
void opts_default(struct options_t *options) { void opts_default(struct options_t *options) {
options->daemonise = false; 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);
} }

View File

@ -6,7 +6,7 @@
#include <error.h> #include <error.h>
struct options_t { struct options_t {
char const* mods_directory; char* mods_directory;
bool daemonise; bool daemonise;
}; };

View File

@ -13,7 +13,7 @@ static struct response_t response_failure(
static void handle_request_get_world_data( static void handle_request_get_world_data(
struct response_t *response, struct response_t *response,
struct request_t const *request, struct request_t const *request,
struct game_data_t *ctx struct game_t *ctx
) { ) {
assert(response != NULL); assert(response != NULL);
assert(request != NULL); assert(request != NULL);
@ -45,7 +45,7 @@ error:
struct response_t handle_request( struct response_t handle_request(
struct request_t const *request, struct request_t const *request,
struct game_data_t *ctx struct game_t *ctx
) { ) {
assert(request != NULL); assert(request != NULL);
assert(ctx != NULL); assert(ctx != NULL);

View File

@ -3,11 +3,11 @@
#include <request.h> #include <request.h>
#include "game_data.h" #include "game.h"
struct response_t handle_request( struct response_t handle_request(
struct request_t const *, struct request_t const *,
struct game_data_t * struct game_t *
); );
#endif #endif

View File

@ -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 // 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 // in socket.c as well just because I'm used to them all being in .h
struct socket_data_t { struct socket_data_t {
struct game_data_t *game_data; struct game_t *game_data;
int socket; 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); assert(data != NULL);
enum error_t err = ERR_OK; 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(pthread != NULL);
assert(data != NULL); assert(data != NULL);

View File

@ -3,7 +3,7 @@
#include <error.h> #include <error.h>
#include "game_data.h" #include "game.h"
#define SOCKET_PATH "/tmp/swd.sock" #define SOCKET_PATH "/tmp/swd.sock"
// Socket path should be a shared setting between server and client // Socket path should be a shared setting between server and client
@ -12,6 +12,6 @@
enum error_t socket_init(int *); enum error_t socket_init(int *);
void socket_free(int const *); 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 #endif