open-card-table/main.c

186 lines
4.1 KiB
C
Raw Normal View History

2023-01-06 19:12:24 -05:00
#include <stdio.h>
#include <lua5.3/lualib.h>
#include <lua5.3/lauxlib.h>
2023-01-06 19:12:24 -05:00
#include "termbox_render.h"
#include "oct_termbox_sprite.h"
#include "oct_networking.h"
2023-01-06 19:12:24 -05:00
#define TB_IMPL
#include "termbox.h"
2023-01-06 19:12:24 -05:00
// Errors
#define OCT_NO_LUA_FILE 1
#define OCT_LUA_FILE_NOT_FOUND 2
#define OCT_LUA_FILE_MISSING_OCT_INIT 3
2023-05-22 09:57:01 -04:00
#define OCT_HELP 4
#define OCT_VERS 5
#define OCT_INVALID_ARGUMENT 6
#define OCT_LUA_FILENAME_SIZE 128
2023-05-22 09:57:01 -04:00
#define OCT_VERSION "0.0"
#define OCT_URL "https://git.thejerks.club/j4nk/open-card-table"
int oct_type;
2023-01-06 19:12:24 -05:00
lua_State *L;
struct {
char port[6]; // max 65535 so 6 bytes needed
// Might come in handy later to keep filename
// e.g. if I ever want to allow downloading a script from peer
char lua_file[OCT_LUA_FILENAME_SIZE];
} args;
2023-05-22 09:57:01 -04:00
void usage();
void version();
int process_args(int argc, char* argv[]);
int initialize_everything(char* lua_file);
2023-01-06 19:12:24 -05:00
int deinitialize_everything();
2023-01-06 19:12:24 -05:00
int main(int argc, char* argv[]) {
int process_args_result = process_args(argc, argv);
switch (process_args_result) {
case OCT_NO_LUA_FILE:
2023-05-22 09:57:01 -04:00
fprintf(stderr, "Error: No lua file given\n\n");
usage();
return EXIT_FAILURE;
break;
case OCT_LUA_FILE_NOT_FOUND:
2023-05-22 09:57:01 -04:00
fprintf(stderr, "Error: Could not open file: %s\n", argv[argc-1]);
return EXIT_FAILURE;
break;
case OCT_INVALID_ARGUMENT:
fprintf(stderr, "Error: unknown argument\n");
return EXIT_FAILURE;
break;
2023-05-22 09:57:01 -04:00
case OCT_HELP:
usage();
return EXIT_SUCCESS;
break;
case OCT_VERS:
version();
return EXIT_SUCCESS;
break;
2023-01-06 19:12:24 -05:00
}
struct tb_event ev;
int finish = 0;
while (!finish) {
tb_clear();
tb_peek_event(&ev, 10);
if (ev.key == TB_KEY_ESC) {
finish = 1;
}
lua_getglobal(L, "oct_loop");
lua_pushinteger(L, ev.key);
lua_pushinteger(L, ev.ch);
lua_call(L, 2, 0);
for (uint32_t i=0; i < oct_tb_sprite_list.new_index; i++) {
oct_render_termbox_sprite(oct_tb_sprite_list.sprite_list[i]);
}
tb_present();
}
deinitialize_everything();
return EXIT_SUCCESS;
}
int process_args(int argc, char* argv[]) {
if (argc == 1) { // Didn't specify a file
return OCT_NO_LUA_FILE;
}
// Set args.port to default
strncpy(args.port, OCT_DEFAULT_PORT, 6);
2023-05-22 09:57:01 -04:00
for (int i = 1; i < argc; i++) {
// argv's are guaranteed to be zero-terminated, so can use
// strcmp instead of strncmp
if (strcmp(argv[i], "-p") == 0) {
if (i != (argc-2)) {
strncpy(args.port, argv[i+1], 6);
i++;
}
}
2023-05-22 09:57:01 -04:00
else if (strcmp(argv[i], "-h") == 0) {
return OCT_HELP;
}
else if (strcmp(argv[i], "-v") == 0) {
return OCT_VERS;
}
else {
if (i == argc-1) {
break;
}
else {
return OCT_INVALID_ARGUMENT;
}
}
}
strncpy(args.lua_file, argv[argc-1], OCT_LUA_FILENAME_SIZE);
// TODO define other cmd line args here
return initialize_everything(argv[argc-1]); // lua file should always be last argument
}
int initialize_everything(char* lua_file) {
// Check if file exists
if (access(lua_file, F_OK) != 0) {
return OCT_LUA_FILE_NOT_FOUND;
}
2023-01-06 19:12:24 -05:00
if (!oct_tb_sprite_list_initialize()) {
return 0;
}
L = luaL_newstate();
if (L == NULL) {
fprintf(stderr, "Can't initialize Lua\n");
return 0;
}
luaL_openlibs(L);
oct_tb_initialize_lua(L);
//if (luaL_dofile(L, lua_file)) return OCT_LUA_FILE_NOT_FOUND;
2023-01-06 19:12:24 -05:00
lua_getglobal(L, "oct_init");
lua_call(L, 0, 1);
int type = lua_tointeger(L, -1);
if (type == OCT_TYPE_SERVER) {
if (!oct_network_server_init(args.port)) {
printf("Could not establish a socket on port %s\n", args.port);
return 0;
}
}
else if (type == OCT_TYPE_CLIENT) {
//oct_network_client_init();
}
else {
// Do nothing, offline
}
2023-01-06 19:12:24 -05:00
tb_init();
return 0;
2023-01-06 19:12:24 -05:00
}
int deinitialize_everything() {
tb_shutdown();
lua_close(L);
oct_tb_sprite_list_deinitialize();
return 1;
}
2023-05-22 09:57:01 -04:00
void usage() {
fprintf(stderr, "Open Card Table, version %s\n", OCT_VERSION);
fprintf(stderr, "%s\n", OCT_URL);
fprintf(stderr, "\n");
fprintf(stderr, "Usage: ./open_card_table [OPTIONS] FILE\n");
fprintf(stderr, "\n");
fprintf(stderr, " -p\tPort on which to listen\n");
fprintf(stderr, " -v\tPrint version and exit\n");
fprintf(stderr, " -h\tPrint this message and exit\n");
fprintf(stderr, "\n");
}
void version() {
fprintf(stderr, "%s\n", OCT_VERSION);
}