#include #include #include #include #include #include "archive.h" #include "compression.h" std::vector archives; int archive; void stringRead(std::string str, int* pos, char* data, int size) { // Loop over part of the string for(int i = 0; i < size && i < str.size(); i++) { // Set part of the data to part of the string data[i] = str[*pos+i]; } // Add to the position *pos += size; } void stringWrite(std::string &str, const char* data, int size) { // Loop over the data for(int i = 0; i < size; i++) { // Add it to the string str += data[i]; } } int archiveGen(const char* data, int size, bool compression) { // Create the loaded archive data file std::string sdata; // Is there decompression needed if(compression) { // Get the size of the uncompressed string uint64_t csize; // Decompress the string const char* cdata = decompress_string(data, size, &csize); // Load the archives data sdata.append(cdata, csize); } else { // Load the archives data sdata.append(data, size); } // Push back the archive archives.push_back(sdata); // Return the archives position return archives.size()-1; } int archiveLoad(const char* dir, bool compression) { // Open the file std::ifstream file(dir, std::ios::binary); // Is the file good if(file.good()) { // Read the data std::istreambuf_iterator begin(file), end; std::string data(begin, end); // Is there decompression needed if(compression) { // Convert the string to a c string int csize = data.size(); char* cdata = new char[csize]; // Loop over the string for(int i=0;i= archives[a].size()) { // Set the substr pos to the archives size substr_pos = archives[a].size(); } else { // Set the substr pos to the write bytes and size substr_pos = pos.pos + size; } // Get the first part and the last part of the string std::string start = archives[a].substr(0, pos.pos-sizeof(uint32_t)); std::string end = archives[a].substr(substr_pos); // Write the data to the end of the start sring stringWrite(start, (char*)&size, sizeof(size)); stringWrite(start, data, size); // Add the start and end to the archive archives[a] = start + end; } } void archiveRead(int a, ArchivePos pos, char* data) { // Get the data from the position int ipos = pos.pos; stringRead(archives[a], &ipos, data, pos.size); } void archive_init() { // Load the archive archive = archiveLoad("resources.bin", true); // Was there an error if(archive == -1) { // Load a fresh archive archive = archiveGen("", 0, true); } }