diff --git a/.gitignore b/.gitignore index 3820021..eb9ff48 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,2 @@ cmake-build* -.cache -build -compile_commands.json -test .idea diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 881aa99..98c11f8 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -6,3 +6,22 @@ test: - cmake .. - make - ./test + +docs: + image: polinux/mkdocs + timeout: 5 minutes + rules: + - if: $CI_COMMIT_BRANCH == 'master' + script: + - apk add openssh-client + - mkdir -p ~/.ssh + - ssh-keyscan -H fputs.com >> ~/.ssh/known_hosts + - echo "$ssh_key" >> ~/.ssh/id_rsa + - chmod -R 700 ~/.ssh + - eval "$(ssh-agent -s)" + - ssh-add ~/.ssh/id_rsa + - mkdocs build + - ssh debian@fputs.com rm -rf /var/www/fputs/docs/spitwad + - ssh debian@fputs.com mkdir -p /var/www/fputs.com/docs/spitwad + - ssh debian@fputs.com chmod 755 /var/www/fputs.com/docs/spitwad + - scp -r ./site/* debian@fputs.com:/var/www/fputs.com/docs/spitwad/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b899ad..3265107 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.25) project(spitwad C) -set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD 99) add_library(spitwad STATIC spitwad.c) if(${CMAKE_PROJECT_NAME} STREQUAL spitwad) diff --git a/docs/index.md b/docs/index.md new file mode 100644 index 0000000..c7a257d --- /dev/null +++ b/docs/index.md @@ -0,0 +1,15 @@ +# spitwad + +`spitwad` is a library for interacting with WAD files, popularized by ID games like DOOM and Quake. The "defining" +feature is for WADs to be transmitted as data over the network and transformed to/from JSON (still working on that part!) + +Originally built as a joke between some friends after we laughed about how it would be easier to use WADs to send data +between applications then JSON, then the idea for a WAD library that could transform WADs to/from JSON was born. + +## Requirements + +Should work on any POSIX platform out of the box + +## Usage + +Docs are a work in progress! \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml new file mode 100644 index 0000000..9127b89 --- /dev/null +++ b/mkdocs.yml @@ -0,0 +1,6 @@ +site_name: spitwad +site_url: https://fputs.com/docs/spitwad +theme: + name: readthedocs +nav: + - 'index.md' \ No newline at end of file diff --git a/spitwad.c b/spitwad.c index 0193223..a9e26a9 100644 --- a/spitwad.c +++ b/spitwad.c @@ -19,7 +19,6 @@ static uint16_t getshort(const unsigned char* data, size_t offset) { static char* getstring(const unsigned char* data, size_t offset) { char* s = malloc(sizeof(char) * 9); strncpy(s, data + offset, 8); - s[8] = '\0'; return s; } @@ -62,7 +61,8 @@ int new_WAD(struct WAD* wad, const unsigned char* data, size_t data_sz) { wad->directory[j].length = getlong(data, i + 4); char *s = getstring(data, i + 8); - strcpy(wad->directory[j].name, s); + strncpy(wad->directory[j].name, s, 8); + wad->directory[j].name[8] = '\0'; free(s); } @@ -107,9 +107,17 @@ void destroy_WAD(struct WAD* wad) { free(wad->data); free(wad->directory); free(wad); - wad = NULL; } + int write_to_file(struct WAD* wad, const char* path) { - + FILE *fp = NULL; + fp = fopen(path, "wb"); + if (fp == NULL) { + fprintf(stderr, "Failed to open %s\n", path); + return 1; + } + fwrite(wad->data, sizeof(unsigned char), wad->data_sz, fp); + fclose(fp); + return 0; } diff --git a/spitwad.h b/spitwad.h index f8c126d..3bd7f73 100644 --- a/spitwad.h +++ b/spitwad.h @@ -1,5 +1,5 @@ -#ifndef SPITWAD_SPITWAD_H -#define SPITWAD_SPITWAD_H +#ifndef SPITWAD_H +#define SPITWAD_H #include #include @@ -29,4 +29,4 @@ int new_WAD_from_file(struct WAD* wad, const char* path); void destroy_WAD(struct WAD* wad); int write_to_file(struct WAD* wad, const char* path); -#endif //SPITWAD_SPITWAD_H +#endif //SPITWAD_H diff --git a/test.c b/test.c index 53accdc..fd787e2 100644 --- a/test.c +++ b/test.c @@ -1,18 +1,45 @@ #include #include #include +#include #include "spitwad.h" -int main() { - struct WAD *wad = malloc(sizeof(struct WAD)); - assert(new_WAD_from_file(wad, "DOOM1.WAD") == 0); - - // Values from manually inspecting DOOM1.WAD with a third-party tool +// Values from manually inspecting DOOM1.WAD with a third-party tool +void assert_doom1_wad(struct WAD *wad) { assert(wad->type == IWAD); assert(wad->dir_sz == 1264); assert(wad->dir_offset == 4175796); assert(wad->directory[0].offset == 12); assert(wad->directory[0].length == 10752); assert(strcmp(wad->directory[0].name, "PLAYPAL") == 0); +} + +void basic_test() { + struct WAD *wad = malloc(sizeof(struct WAD)); + assert(new_WAD_from_file(wad, "DOOM1.WAD") == 0); + assert_doom1_wad(wad); + destroy_WAD(wad); + wad = NULL; +} + +void read_write_test() { + struct WAD *orig = malloc(sizeof(struct WAD)); + assert(new_WAD_from_file(orig, "DOOM1.WAD") == 0); + + assert(write_to_file(orig, "TEST.WAD") == 0); + struct WAD *new = malloc(sizeof(struct WAD)); + assert(new_WAD_from_file(new, "TEST.WAD") == 0); + assert_doom1_wad(new); + + destroy_WAD(orig); + orig = NULL; + destroy_WAD(new); + new = NULL; + remove("TEST.WAD"); +} + +int main() { + basic_test(); + read_write_test(); } \ No newline at end of file