#include #include #include #include "termbox_render.h" #include "oct_termbox_sprite.h" #include "oct_networking.h" #define TB_IMPL #include "termbox.h" // Errors #define OCT_NO_LUA_FILE 1 #define OCT_LUA_FILE_NOT_FOUND 2 #define OCT_LUA_FILE_MISSING_OCT_INIT 3 #define OCT_HELP 4 #define OCT_VERS 5 #define OCT_INVALID_ARGUMENT 6 #define OCT_LUA_FILENAME_SIZE 128 #define OCT_VERSION "0.0" #define OCT_URL "https://git.thejerks.club/j4nk/open-card-table" int oct_type; 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; void usage(); void version(); int process_args(int argc, char* argv[]); int initialize_everything(char* lua_file); int deinitialize_everything(); int main(int argc, char* argv[]) { int process_args_result = process_args(argc, argv); switch (process_args_result) { case OCT_NO_LUA_FILE: fprintf(stderr, "Error: No lua file given\n\n"); usage(); return EXIT_FAILURE; break; case OCT_LUA_FILE_NOT_FOUND: fprintf(stderr, "Error: Could not open file: %s\n", argv[argc-1]); return EXIT_FAILURE; break; case OCT_HELP: usage(); return EXIT_SUCCESS; break; case OCT_VERS: version(); return EXIT_SUCCESS; break; } 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); 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++; } } 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) { 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; 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 } tb_init(); return 0; } int deinitialize_everything() { tb_shutdown(); lua_close(L); oct_tb_sprite_list_deinitialize(); return 1; } 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); }