acabat amb resource.pack
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include "core/jail/jfile.hpp"
|
||||
#include "core/resources/resource_helper.hpp"
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wunused-but-set-variable"
|
||||
@@ -44,10 +44,10 @@ JD8_Surface JD8_NewSurface() {
|
||||
}
|
||||
|
||||
JD8_Surface JD8_LoadSurface(const char* file) {
|
||||
auto buffer = file_readfile(file);
|
||||
auto buffer = ResourceHelper::loadFile(file);
|
||||
|
||||
unsigned short w, h;
|
||||
Uint8* pixels = LoadGif(reinterpret_cast<unsigned char*>(buffer.data()), &w, &h);
|
||||
Uint8* pixels = LoadGif(buffer.data(), &w, &h);
|
||||
|
||||
if (pixels == NULL) {
|
||||
printf("Unable to load bitmap: %s\n", SDL_GetError());
|
||||
@@ -62,8 +62,8 @@ JD8_Surface JD8_LoadSurface(const char* file) {
|
||||
}
|
||||
|
||||
JD8_Palette JD8_LoadPalette(const char* file) {
|
||||
auto buffer = file_readfile(file);
|
||||
return (JD8_Palette)LoadPalette(reinterpret_cast<unsigned char*>(buffer.data()));
|
||||
auto buffer = ResourceHelper::loadFile(file);
|
||||
return (JD8_Palette)LoadPalette(buffer.data());
|
||||
}
|
||||
|
||||
void JD8_SetScreenPalette(JD8_Palette palette) {
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
#include "core/jail/jfile.hpp"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -19,58 +14,14 @@
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr const char* DEFAULT_FILENAME = "data.jf2";
|
||||
constexpr const char* DEFAULT_FOLDER = "data/";
|
||||
|
||||
struct file_entry {
|
||||
std::string path;
|
||||
uint32_t size;
|
||||
uint32_t offset;
|
||||
};
|
||||
|
||||
struct keyvalue {
|
||||
std::string key;
|
||||
std::string value;
|
||||
};
|
||||
|
||||
std::vector<file_entry> toc;
|
||||
std::vector<keyvalue> config;
|
||||
|
||||
std::string resource_filename;
|
||||
std::string resource_folder;
|
||||
std::string config_folder;
|
||||
int file_source = SOURCE_FILE;
|
||||
|
||||
bool dictionary_loaded() {
|
||||
if (resource_filename.empty()) resource_filename = DEFAULT_FILENAME;
|
||||
|
||||
std::ifstream fi(resource_filename, std::ios::binary);
|
||||
if (!fi.is_open()) return false;
|
||||
|
||||
char header[4];
|
||||
fi.read(header, 4);
|
||||
uint32_t num_files, toc_offset;
|
||||
fi.read(reinterpret_cast<char*>(&num_files), 4);
|
||||
fi.read(reinterpret_cast<char*>(&toc_offset), 4);
|
||||
fi.seekg(toc_offset);
|
||||
|
||||
for (uint32_t i = 0; i < num_files; ++i) {
|
||||
uint32_t file_offset, file_size;
|
||||
fi.read(reinterpret_cast<char*>(&file_offset), 4);
|
||||
fi.read(reinterpret_cast<char*>(&file_size), 4);
|
||||
uint8_t path_size;
|
||||
fi.read(reinterpret_cast<char*>(&path_size), 1);
|
||||
char file_name[256];
|
||||
fi.read(file_name, path_size);
|
||||
file_name[path_size] = 0;
|
||||
toc.push_back({std::string(file_name), file_size, file_offset});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string filename_with_folder(const char* filename) {
|
||||
return resource_folder + filename;
|
||||
}
|
||||
|
||||
void load_config_values() {
|
||||
config.clear();
|
||||
@@ -97,10 +48,6 @@ void save_config_values() {
|
||||
|
||||
} // namespace
|
||||
|
||||
void file_setresourcefilename(const char* str) {
|
||||
resource_filename = str;
|
||||
}
|
||||
|
||||
void file_setresourcefolder(const char* str) {
|
||||
resource_folder = str;
|
||||
}
|
||||
@@ -109,58 +56,6 @@ const char* file_getresourcefolder() {
|
||||
return resource_folder.c_str();
|
||||
}
|
||||
|
||||
void file_setsource(const int src) {
|
||||
file_source = src % 2;
|
||||
if (src == SOURCE_FOLDER && resource_folder.empty()) file_setresourcefolder(DEFAULT_FOLDER);
|
||||
}
|
||||
|
||||
FILE* file_getfilepointer(const char* resourcename, int& filesize, const bool binary) {
|
||||
if (file_source == SOURCE_FILE && toc.empty()) {
|
||||
if (!dictionary_loaded()) file_setsource(SOURCE_FOLDER);
|
||||
}
|
||||
|
||||
FILE* f = nullptr;
|
||||
|
||||
if (file_source == SOURCE_FILE) {
|
||||
const std::string name(resourcename);
|
||||
size_t count = 0;
|
||||
for (; count < toc.size(); ++count) {
|
||||
if (toc[count].path == name) break;
|
||||
}
|
||||
if (count == toc.size()) {
|
||||
perror("El recurs no s'ha trobat en l'arxiu de recursos");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
filesize = static_cast<int>(toc[count].size);
|
||||
|
||||
f = fopen(resource_filename.c_str(), binary ? "rb" : "r");
|
||||
if (!f) {
|
||||
perror("No s'ha pogut obrir l'arxiu de recursos");
|
||||
exit(1);
|
||||
}
|
||||
fseek(f, toc[count].offset, SEEK_SET);
|
||||
} else {
|
||||
const std::string full = filename_with_folder(resourcename);
|
||||
f = fopen(full.c_str(), binary ? "rb" : "r");
|
||||
if (!f) return nullptr;
|
||||
fseek(f, 0, SEEK_END);
|
||||
filesize = static_cast<int>(ftell(f));
|
||||
fseek(f, 0, SEEK_SET);
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
std::vector<char> file_readfile(const char* resourcename) {
|
||||
int filesize = 0;
|
||||
FILE* f = file_getfilepointer(resourcename, filesize, true);
|
||||
if (!f) return {};
|
||||
std::vector<char> buffer(filesize);
|
||||
fread(buffer.data(), filesize, 1, f);
|
||||
fclose(f);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
// Crea la carpeta del sistema on guardar les dades.
|
||||
// Accepta rutes amb subdirectoris (ex: "jailgames/aee") i crea tota la jerarquia.
|
||||
void file_setconfigfolder(const char* foldername) {
|
||||
|
||||
@@ -1,26 +1,10 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
#define SOURCE_FILE 0
|
||||
#define SOURCE_FOLDER 1
|
||||
|
||||
void file_setconfigfolder(const char* foldername);
|
||||
const char* file_getconfigfolder();
|
||||
|
||||
void file_setresourcefilename(const char* str);
|
||||
void file_setresourcefolder(const char* str);
|
||||
const char* file_getresourcefolder();
|
||||
void file_setsource(const int src);
|
||||
|
||||
FILE* file_getfilepointer(const char* resourcename, int& filesize, const bool binary = false);
|
||||
|
||||
// Llig tot el contingut d'un recurs (fitxer solt o entrada del .jrf).
|
||||
// Retorna un vector buit si el recurs no existeix. El vector es destrueix
|
||||
// automàticament en eixir d'àmbit — no fa falta cap free() manual. Mida =
|
||||
// bytes llegits (el buffer no està null-terminated).
|
||||
std::vector<char> file_readfile(const char* resourcename);
|
||||
|
||||
const char* file_getconfigvalue(const char* key);
|
||||
void file_setconfigvalue(const char* key, const char* value);
|
||||
|
||||
Reference in New Issue
Block a user