diff options
author | Ben Longbons <b.r.longbons@gmail.com> | 2013-02-18 19:20:35 -0800 |
---|---|---|
committer | Ben Longbons <b.r.longbons@gmail.com> | 2013-02-20 18:27:30 -0800 |
commit | 25823b36905a84d92f9299ba7f9f0c713141c8fb (patch) | |
tree | a335120de1c4618c9b41391e70bc621a4218010c /src/map/grfio.cpp | |
parent | a5d231b6a60a4ab868918850be24640e88843825 (diff) | |
download | tmwa-25823b36905a84d92f9299ba7f9f0c713141c8fb.tar.gz tmwa-25823b36905a84d92f9299ba7f9f0c713141c8fb.tar.bz2 tmwa-25823b36905a84d92f9299ba7f9f0c713141c8fb.tar.xz tmwa-25823b36905a84d92f9299ba7f9f0c713141c8fb.zip |
Strictify map cells
Also fix a small but major bug in map_randfreecell.
Diffstat (limited to 'src/map/grfio.cpp')
-rw-r--r-- | src/map/grfio.cpp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/map/grfio.cpp b/src/map/grfio.cpp new file mode 100644 index 0000000..a190d1b --- /dev/null +++ b/src/map/grfio.cpp @@ -0,0 +1,72 @@ +// Reads .gat files by name-mapping .wlk files +#include "grfio.hpp" + +#include <sys/stat.h> + +#include <fcntl.h> +#include <unistd.h> + +#include <cassert> +#include <cstdio> +#include <cstring> + +#include <fstream> +#include <map> + +#include "../common/cxxstdio.hpp" +#include "../common/extract.hpp" + +#include "../poison.hpp" + +static +std::map<std::string, std::string> load_resnametable() +{ + std::ifstream in("data/resnametable.txt"); + if (!in.is_open()) + { + fprintf(stderr, "Missing data/resnametable.txt"); + abort(); + } + std::map<std::string, std::string> out; + + std::string line; + while (std::getline(in, line)) + { + std::string key, value; + if (!extract(line, + record<'#'>(&key, &value))) + continue; + out[key] = value; + } + return out; +} + +/// Change *.gat to *.wlk +static +std::string grfio_resnametable(const_string rname) +{ + static + std::map<std::string, std::string> resnametable = load_resnametable(); + + return resnametable.at(std::string(rname.begin(), rname.end())); +} + +std::vector<uint8_t> grfio_reads(const_string rname) +{ + std::string lfname = "data/" + grfio_resnametable(rname); + + int fd = open(lfname.c_str(), O_RDONLY); + if (fd == -1) + { + FPRINTF(stderr, "Resource %s (file %s) not found\n", + std::string(rname.begin(), rname.end()), lfname); + return {}; + } + off_t len = lseek(fd, 0, SEEK_END); + assert (len != -1); + std::vector<uint8_t> buffer(len); + int err = pread(fd, buffer.data(), len, 0); + assert (err == len); + close(fd); + return buffer; +} |