From 77c2371eff203ad30b31a74332d2af372c98d257 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Fri, 21 Jan 2022 16:36:48 -0600 Subject: [PATCH] Strip old c and c++ stuff --- src/Makefile | 9 -- src/base/Makefile | 10 --- src/base/cyoa.h | 67 --------------- src/base/cyoa.leg | 167 -------------------------------------- src/base/storybook.cpp | 139 ------------------------------- src/base/storybook.h | 45 ---------- src/cli/Makefile | 5 -- src/cli/storybook_cli.cpp | 32 -------- src/godot/.gitignore | 7 -- src/godot/Makefile | 10 --- src/godot/godot-headers | 1 - src/godot/storybook.c | 127 ----------------------------- 12 files changed, 619 deletions(-) delete mode 100644 src/Makefile delete mode 100644 src/base/Makefile delete mode 100644 src/base/cyoa.h delete mode 100644 src/base/cyoa.leg delete mode 100644 src/base/storybook.cpp delete mode 100644 src/base/storybook.h delete mode 100644 src/cli/Makefile delete mode 100644 src/cli/storybook_cli.cpp delete mode 100644 src/godot/.gitignore delete mode 100644 src/godot/Makefile delete mode 160000 src/godot/godot-headers delete mode 100644 src/godot/storybook.c diff --git a/src/Makefile b/src/Makefile deleted file mode 100644 index 11bdfe0..0000000 --- a/src/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -.PHONY: all base cli clean -all: base cli -base: - $(MAKE) -C base -cli: - $(MAKE) -C cli -clean: - $(MAKE) -C base clean - $(MAKE) -C cli clean diff --git a/src/base/Makefile b/src/base/Makefile deleted file mode 100644 index d50ee96..0000000 --- a/src/base/Makefile +++ /dev/null @@ -1,10 +0,0 @@ -CXXFLAGS = -g -OBJS = cyoa.o storybook.o - -all: libstorybook.a -libstorybook.a: $(OBJS) - ar rcs $@ $^ -cyoa.cpp: cyoa.leg - leg -o $@ $^ -clean: - rm -f cyoa.c $(OBJS) diff --git a/src/base/cyoa.h b/src/base/cyoa.h deleted file mode 100644 index 5fecb5c..0000000 --- a/src/base/cyoa.h +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef CYOA_H -#define CYOA_H - -#include -#include - -#define STATCHECK_GT 1 -#define STATCHECK_LT 2 - -namespace storybook { -struct statcheck_t { - char *stat; - int value; - int rel; -}; - -struct statchange_t { - char *stat; - int addend; -}; - -struct Choice { - int option; - char *flavor; - statcheck_t *statcheck; - char *id; - statchange_t *statchange; - - Choice(int); -}; - -typedef std::map ChoiceList; - -enum class FooterType { - End, - Goto, - Choices -}; - -struct Footer { - FooterType type; - union { - ChoiceList *choices; - char *link; - }; - - Footer(); - ~Footer(); -}; - -struct Page { - char *id; - char *body; - Footer footer; - - Page(); - ~Page(); -}; - -std::vector CyoaParse(FILE *); -} - -#endif /* CYOA_H */ - -// Local Variables: -// mode: c++ -// End: diff --git a/src/base/cyoa.leg b/src/base/cyoa.leg deleted file mode 100644 index d05fcea..0000000 --- a/src/base/cyoa.leg +++ /dev/null @@ -1,167 +0,0 @@ -%{ - -#include -#include -#include -#include "cyoa.h" - -using namespace storybook; - -FILE* yyin = stdin; - -#define YY_INPUT(buf, result, max_size) { \ - int yyc = getc(yyin); \ - result = (EOF == yyc) ? 0 : (*(buf) = yyc, 1); \ - } - -std::vector pages; - -Page *emit_ending(); -Page *emit_goto(char*); -Page *emit_choices(ChoiceList*); - -void append_body(Page *page, char *str); - -union value { - Page *page; - char *string; - Choice *choice; - ChoiceList *choices; - int num; - statcheck_t *statcheck; - statchange_t *statchange; -}; - -#define YYSTYPE union value - -%} - - -Story = BlankLine* (p:Page { pages.push_back(p.page); })+ EndOfFile - -Page = h:Header b:Body { $$.page = b.page; $$.page->id = h.string} - BlankLine* - -Header = < i:Identifier > Newline BlankLine* { $$ = i; } - -Identifier = < [A-Z][A-Z0-9_]+ > - { $$.string = strndup(yytext, yyleng); } - -Body = Footer | t:TextLine b:Body { $$ = b; append_body(b.page, t.string);} - -TextLine = < (!Newline .)* Newline > { $$.string = strndup(yytext, yyleng); } - -Footer = Ending { $$.page = emit_ending(); } - | g:Goto { $$.page = emit_goto(g.string); } - | c:ChoiceList { $$.page = emit_choices(c.choices); } - -Goto = 'GOTO' - < i:Identifier > Newline - { $$.string = strndup(yytext, yyleng); } - -Ending = 'THE END' Newline - -ChoiceList = c:Choice l:ChoiceList { $$ = l; (*$$.choices)[c.choice->option] = c.choice; } - | c:Choice { $$.choices = new ChoiceList(); (*$$.choices)[c.choice->option] = c.choice; } - -Choice = b:Bullet - f:Freetext { b.choice->flavor = f.string; } - (ck:StatCheck { b.choice->statcheck = ck.statcheck; })? - '[' i:Identifier ']' { b.choice->id = i.string; } - - (cg:StatChange { b.choice->statchange = cg.statchange; })? - Newline { $$ = b; } - ; - -Bullet = < [0-9]+ > ')' - { $$.choice = new Choice(atoi(yytext)); } - -Freetext = < ([^[<])+ > { $$.string = strndup(yytext, yyleng); } - -StatCheck = '<' n:StatName v:StatVal r:StatRel '>' - - { $$.statcheck = new statcheck_t{n.string, v.num, r.num}; } - -StatChange = '(' ('+' | '-') v:StatVal n:StatName ')' - { $$.statchange = new statchange_t{n.string, v.num}; } - -StatName = < [A-Za-z]+ > - { $$.string = strdup(yytext); } - -StatVal = < [0-9]+ > - { $$.num = atoi(yytext); } - -StatRel = < ('+'|'-')? > { $$.num = yytext[0] == '+' ? STATCHECK_GT : STATCHECK_LT; } - - -EndOfFile = !. - -BlankLine = - Newline - -- = (' ' | '\t')* - -Newline = '\r\n' | '\r' | '\n' - -%% - -Choice::Choice(int option) { - this->option = option; - this->statcheck = NULL; - this->statchange = NULL; -} - -Footer::Footer() { -} - -Footer::~Footer() { -} - -Page::Page() { - id = NULL; - body = NULL; -} - -Page::~Page() { - if (id) { - free(id); - } - if (body) { - free(body); - } -} - -Page *emit_goto(char *id) { - Page *mypage = new Page; - mypage->footer.type = FooterType::Goto; - mypage->footer.link = id; - return mypage; -} - -Page *emit_ending() { - Page *mypage = new Page; - mypage->footer.type = FooterType::End; - return mypage; -} - -Page *emit_choices(ChoiceList *choices) { - Page *mypage = new Page; - mypage->footer.type = FooterType::Choices; - mypage->footer.choices = choices; - return mypage; -} - -void append_body(Page *page, char *str) { - if (page->body) { - int size = strlen(page->body) + strlen(str) + 1; - char *newstring = (char*) calloc(size, sizeof(char)); - strcpy(newstring, str); - strcat(newstring, page->body); - free(page->body); - page->body = newstring; - } else { - page->body = str; - } -} - -std::vector storybook::CyoaParse(FILE *file) { - yyin = file; - yyparse(); - yyin = stdin; - return pages; -} - -/* Local Variables: */ -/* mode: text */ -/* End: */ diff --git a/src/base/storybook.cpp b/src/base/storybook.cpp deleted file mode 100644 index f736b8d..0000000 --- a/src/base/storybook.cpp +++ /dev/null @@ -1,139 +0,0 @@ -#include -#include -#include - -#include "storybook.h" - -using namespace storybook; - -Storybook::Storybook() { - // TODO -} - -Storybook::Storybook(FILE* fin) { - pages = CyoaParse(fin); -} - -void Storybook::UpdateStat(const char *ckey, int rel) { - std::string key(ckey); - auto it = stats.find(key); - if (it == stats.end()) { - // Key not present - stats[key] = rel; - } else { - // Key present, add the rel - stats[key] = it->second + rel; - } -} - -int Storybook::LookupStat(const char *ckey) { - std::string key(ckey); - auto it = stats.find(key); - if (it == stats.end()){ - stats[key] = 0; - return 0; - } else { - return it->second; - } - return 0; -} - -void Storybook::Find(const char *id) { - for (Page* page : pages) { - if (strcmp(page->id, id) == 0) { - current = page; - return; - } - } - // If not found - current = NULL; -} - -void Storybook::Advance() { - // TODO throw an exception if the ending type is choices - switch(current->footer.type) { - case FooterType::End: - IsEnded = true; - current = NULL; - return; - case FooterType::Goto: - Find(current->footer.link); - return; - } -} - -void Storybook::Advance(int choice_option) { - if (!IsChoiceAvailable(choice_option)) { - throw IllegalChoiceException(); - } - - Choice* choice = (*current->footer.choices)[choice_option]; - - if (statchange_t *cg = choice->statchange) - UpdateStat(cg->stat, cg->addend); - - Find(choice->id); -} - -bool Storybook::IsChoiceAvailable(int choice_option) { - Choice* choice = (*current->footer.choices)[choice_option]; - if (!choice) - return false; - - statcheck_t *ck = choice->statcheck; - if (ck) { - int val = LookupStat(ck->stat); - if (ck->rel == STATCHECK_GT && val < ck->value || - ck->rel == STATCHECK_LT && val > ck->value) - return false; - } - - return true; -} - -void Storybook::print_choice(Choice* c) { - if (!IsChoiceAvailable(c->option)) - printf("\033[;31m"); // Red background - - printf("%d) %s", c->option, c->flavor); - if (c->statchange) { - printf("(%s %+d)", - c->statchange->stat, - c->statchange->addend); - } - if (!IsChoiceAvailable(c->option)) - printf("\033[;0m"); - printf("\n"); -} - -void Storybook::Play() { - printf(current->body); - Choice *choice; - - switch (current->footer.type) { - case FooterType::End: - printf("The End."); - Advance(); - break; - case FooterType::Goto: - Advance(); - printf("Press ENTER to continue..."); - std::cin.get(); - break; - case FooterType::Choices: - for (auto c : *current->footer.choices) { - print_choice(c.second); - } - printf("Make a selection: "); - int choice_option; - std::cin >> choice_option; - try { - Advance(choice_option); - } catch (IllegalChoiceException e) { - printf("You can't do that!\n\n"); - } - break; - default: - IsEnded = true; - } -} diff --git a/src/base/storybook.h b/src/base/storybook.h deleted file mode 100644 index 2119665..0000000 --- a/src/base/storybook.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef STORYBOOK_H -#define STORYBOOK_H - -#include -#include -#include -#include -#include "cyoa.h" - -namespace storybook { - -class Storybook { -private: - std::vector pages; - std::unordered_map stats; - Page *current; - - void print_choice(Choice *c); -public: - Storybook(); - Storybook(FILE* fin); - - // Functions for looking up and printing stats - void UpdateStat(const char*, int); - int LookupStat(const char*); - std::vector GetDefinedStats(); - - // Functions to change the game state - void Find(const char*); - void Advance(); // For Goto and End cases - void Advance(int); // For Choices - bool IsChoiceAvailable(int); - bool IsEnded = false; - - // This will probably be moved to the cli driver - void Play(); - - class IllegalChoiceException : std::exception { - virtual const char *what() const noexcept { - return "Illegal Choice."; - } - }; -}; -} -#endif /* STORYBOOK_H */ diff --git a/src/cli/Makefile b/src/cli/Makefile deleted file mode 100644 index 41c5c4b..0000000 --- a/src/cli/Makefile +++ /dev/null @@ -1,5 +0,0 @@ -CXXFLAGS = -g -L../base/ -.PHONY: all clean -all: storybook-cli -storybook-cli: storybook_cli.cpp - $(CXX) $(CXXFLAGS) -o $@ $^ -lstorybook diff --git a/src/cli/storybook_cli.cpp b/src/cli/storybook_cli.cpp deleted file mode 100644 index 77c0769..0000000 --- a/src/cli/storybook_cli.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include - -#include "../base/storybook.h" - -void usage(const char* bin) { - fprintf(stderr, "Usage: %s ", bin); - exit(1); -} - -int main(int argc, const char *argv[]) { - if (argc != 2) { - usage(argv[0]); - } - - FILE *fin = fopen(argv[1], "r"); - if (!fin) { - fprintf(stderr, "Error: could not read %s", argv[1]); - return 2; - } - storybook::Storybook sb(fin); - fclose(fin); - - sb.Find("START"); - sb.Play(); - - while (!sb.IsEnded) { - sb.Play(); - } - - return 0; -} diff --git a/src/godot/.gitignore b/src/godot/.gitignore deleted file mode 100644 index 5e49886..0000000 --- a/src/godot/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -.sconsign.dblite -bin/ -*.os -*.so -*.o -logs/ -storybook.h \ No newline at end of file diff --git a/src/godot/Makefile b/src/godot/Makefile deleted file mode 100644 index 9baaeef..0000000 --- a/src/godot/Makefile +++ /dev/null @@ -1,10 +0,0 @@ -LDFLAGS := -Wl,--gc-sections -lpthread -ldl -rdynamic -shared -CFLAGS := -g -std=c11 -fPIC -Igodot-headers - -.PHONY: all -all: libstorybook.so -libstorybook.so: storybook.o ../rust-base/target/debug/libstorybook.a - $(CC) $(LDFLAGS) $^ -o $@ -storybook.o: storybook.h -storybook.h: - cbindgen -c ../rust-base/cbindgen.toml --crate storybook --lang c -o $@ ../rust-base diff --git a/src/godot/godot-headers b/src/godot/godot-headers deleted file mode 160000 index bd86335..0000000 --- a/src/godot/godot-headers +++ /dev/null @@ -1 +0,0 @@ -Subproject commit bd863357de5b1fa8e04ebffbbb30d28425de9723 diff --git a/src/godot/storybook.c b/src/godot/storybook.c deleted file mode 100644 index ccac300..0000000 --- a/src/godot/storybook.c +++ /dev/null @@ -1,127 +0,0 @@ -#include - -#include - -#include "storybook.h" - -const godot_gdnative_core_api_struct *api = NULL; -const godot_gdnative_ext_nativescript_api_struct *nativescript_api = NULL; - -void *storybook_godot_constructor(godot_object *p_instance, void *p_method_data); -void storybook_godot_destructor(godot_object *p_instance, void *p_method_data, void *p_user_data); -godot_variant storybook_godot_parse(godot_object *p_instance, void *p_method_data, void *p_user_data, int p_num_args, godot_variant **p_args); -godot_variant storybook_godot_get_body(godot_object *p_instance, void *p_method_data, void *p_user_data, int p_num_args, godot_variant **p_args); -godot_variant storybook_godot_advance(godot_object *p_instance, void *p_method_data, void *p_user_data, int p_num_args, godot_variant **p_args); -godot_variant storybook_godot_get_footer(godot_object *p_instance, void *p_method_data, void *p_user_data, int p_num_args, godot_variant **p_args); - -void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *p_options) { - api = p_options->api_struct; - - for (int i = 0; i < api->num_extensions; i++) { - switch(api->extensions[i]->type) { - case GDNATIVE_EXT_NATIVESCRIPT: { - nativescript_api = (godot_gdnative_ext_nativescript_api_struct *)api->extensions[i]; - }; break; - default: break; - } - } -} - -void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *p_options) { - api = NULL; - nativescript_api = NULL; -} - -#define register_method(meth) \ - godot_instance_method meth = { NULL, NULL, NULL}; \ - meth.method = &storybook_godot_ ## meth; \ - nativescript_api->godot_nativescript_register_method(p_handle, "STORYBOOK", #meth, attributes, meth) - -void GDN_EXPORT godot_nativescript_init(void *p_handle) { - godot_instance_create_func create = { NULL, NULL, NULL }; - create.create_func = &storybook_godot_constructor; - - godot_instance_destroy_func destroy = { NULL, NULL, NULL }; - destroy.destroy_func = &storybook_godot_destructor; - - nativescript_api->godot_nativescript_register_class(p_handle, "STORYBOOK", "Reference", - create, destroy); - - godot_method_attributes attributes = { GODOT_METHOD_RPC_MODE_DISABLED }; - - register_method(parse); - register_method(get_body); - register_method(advance); - register_method(get_footer); -} - -typedef struct user_data_struct { - Book *book; -} user_data_struct; - -void *storybook_godot_constructor(godot_object *p_instance, void *p_method_data){ - user_data_struct *user_data = api->godot_alloc(sizeof(user_data_struct)); - return user_data; -} - -void storybook_godot_destructor(godot_object *p_instance, void *p_method_data, void *p_user_data) { - user_data_struct *user_data = (user_data_struct*) p_user_data; - storybook_free_book(user_data->book); - api->godot_free(user_data); -} - -godot_variant storybook_godot_parse(godot_object *p_instance, void *p_method_data, void *p_user_data, int p_num_args, godot_variant **p_args) { - godot_variant ret; - godot_string str = api->godot_variant_as_string(p_args[0]); - godot_char_string utf8 = api->godot_string_utf8(&str); - - user_data_struct *user_data = (user_data_struct*) p_user_data; - user_data->book = storybook_make_book(api->godot_char_string_get_data(&utf8)); - api->godot_char_string_destroy(&utf8); - - api->godot_variant_new_nil(&ret); - return ret; -} - - -godot_variant storybook_godot_get_body(godot_object *p_instance, void *p_method_data, void *p_user_data, int p_num_args, godot_variant **p_args) { - godot_variant ret; - godot_string str; - - user_data_struct *user_data = (user_data_struct*) p_user_data; - char *body = storybook_get_body(user_data->book); - api->godot_string_new(&str); - api->godot_string_parse_utf8(&str, body); - storybook_free_string(body); - - api->godot_variant_new_string(&ret, &str); - api->godot_string_destroy(&str); - - return ret; -} - -godot_variant storybook_godot_advance(godot_object *p_instance, void *p_method_data, void *p_user_data, int p_num_args, godot_variant **p_args) { - godot_variant ret; - - user_data_struct *user_data = (user_data_struct*) p_user_data; - - if (p_num_args == 0) { - storybook_advance_nooption(user_data->book); - } else { - uint64_t option = api->godot_variant_as_uint(p_args[0]); - storybook_advance_option(user_data->book, option); - } - - api->godot_variant_new_nil(&ret); - return ret; -} - -godot_variant storybook_godot_get_footer(godot_object *p_instance, void *p_method_data, void *p_user_data, int p_num_args, godot_variant **p_args) { - godot_variant ret; - - user_data_struct *user_data = (user_data_struct*) p_user_data; - footer ft = storybook_get_footer(user_data->book); - - api->godot_variant_new_int(&ret, ft); - return ret; -}