Skip to content

Commit

Permalink
Enable loading language-based files from separate directory
Browse files Browse the repository at this point in the history
  • Loading branch information
bvschaik committed Mar 5, 2020
1 parent cf868a4 commit 6dd7fe8
Show file tree
Hide file tree
Showing 22 changed files with 99 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/building/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ int model_load(void)
return 0;
}
memset(buffer, 0, TMP_BUFFER_SIZE);
int filesize = io_read_file_into_buffer("c3_model.txt", buffer, TMP_BUFFER_SIZE);
int filesize = io_read_file_into_buffer("c3_model.txt", buffer, TMP_BUFFER_SIZE, 0);
if (filesize == 0) {
log_error("no c3_model.txt file", 0, 0);
free(buffer);
Expand Down
2 changes: 1 addition & 1 deletion src/city/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ static int has_video(int text_id)
}
char video_file[FILE_NAME_MAX];
encoding_to_utf8(msg->video.text, video_file, FILE_NAME_MAX, 0);
return file_exists(video_file);
return file_exists(video_file, 1);
}

static void enqueue_message(int sequence)
Expand Down
44 changes: 36 additions & 8 deletions src/core/dir.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "core/dir.h"

#include "core/file.h"
#include "core/locale.h"
#include "core/string.h"
#include "platform/file_manager.h"

Expand Down Expand Up @@ -98,30 +99,42 @@ static void move_left(char *str)
*str = 0;
}

const char *dir_get_case_corrected_file(const char *filepath)
const char *get_case_corrected_file(const char *dir, const char *filepath)
{
static char corrected_filename[2 * FILE_NAME_MAX];

FILE *fp = file_open(filepath, "rb");
int dir_len = 0;
if (dir) {
dir_len = strlen(dir) + 1;
strncpy(corrected_filename, dir, dir_len);
corrected_filename[dir_len - 1] = '/';
} else {
dir = ".";
}

strncpy(&corrected_filename[dir_len], filepath, 2 * FILE_NAME_MAX - dir_len);
corrected_filename[dir_len + 2 * FILE_NAME_MAX - 1] = 0;

FILE *fp = file_open(corrected_filename, "rb");
if (fp) {
file_close(fp);
return filepath;
return corrected_filename;
}

if (!platform_file_manager_should_case_correct_file()) {
return 0;
}

strncpy(corrected_filename, filepath, 2 * FILE_NAME_MAX);
strncpy(&corrected_filename[dir_len], filepath, 2 * FILE_NAME_MAX);
corrected_filename[2 * FILE_NAME_MAX - 1] = 0;

char *slash = strchr(corrected_filename, '/');
char *slash = strchr(&corrected_filename[dir_len], '/');
if (!slash) {
slash = strchr(corrected_filename, '\\');
slash = strchr(&corrected_filename[dir_len], '\\');
}
if (slash) {
*slash = 0;
if (correct_case(".", corrected_filename, TYPE_DIR)) {
if (correct_case(dir, &corrected_filename[dir_len], TYPE_DIR)) {
char *path = slash + 1;
if (*path == '\\') {
// double backslash: move everything to the left
Expand All @@ -133,9 +146,24 @@ const char *dir_get_case_corrected_file(const char *filepath)
}
}
} else {
if (correct_case(".", corrected_filename, TYPE_FILE)) {
if (correct_case(dir, corrected_filename, TYPE_FILE)) {
return corrected_filename;
}
}
return 0;
}

const char *dir_get_case_corrected_file(const char *filepath, int localizable)
{
if (localizable) {
const char *custom_dir = locale_get_directory();
if (custom_dir) {
const char *path = get_case_corrected_file(custom_dir, filepath);
if (path) {
return path;
}
}
}

return get_case_corrected_file(0, filepath);
}
2 changes: 1 addition & 1 deletion src/core/dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ const dir_listing *dir_find_files_with_extension(const char *extension);
* @param filepath File path to match to a case-sensitive file on the filesystem
* @return Case-corrected file, or NULL if the file was not found
*/
const char *dir_get_case_corrected_file(const char *filepath);
const char *dir_get_case_corrected_file(const char *filepath, int localizable);

#endif // CORE_DIR_H
4 changes: 2 additions & 2 deletions src/core/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ void file_remove_extension(uint8_t *filename)
}
}

int file_exists(const char *filename)
int file_exists(const char *filename, int localizable)
{
return NULL != dir_get_case_corrected_file(filename);
return NULL != dir_get_case_corrected_file(filename, localizable);
}

int file_remove(const char *filename)
Expand Down
2 changes: 1 addition & 1 deletion src/core/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void file_remove_extension(uint8_t *filename);
* @param filename Filename to check
* @return boolean true if the file exists, false otherwise
*/
int file_exists(const char *filename);
int file_exists(const char *filename, int localizable);

/**
* Remove a file
Expand Down
20 changes: 10 additions & 10 deletions src/core/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ static void convert_images(image *images, int size, buffer *buf, color_t *dst)

static void load_empire(void)
{
int size = io_read_file_into_buffer(EMPIRE_555, data.tmp_data, EMPIRE_DATA_SIZE);
int size = io_read_file_into_buffer(EMPIRE_555, data.tmp_data, EMPIRE_DATA_SIZE, 1);
if (size != EMPIRE_DATA_SIZE / 2) {
log_error("unable to load empire data", EMPIRE_555, 0);
return;
Expand All @@ -297,7 +297,7 @@ int image_load_climate(int climate_id, int is_editor)
const char *filename_bmp = is_editor ? EDITOR_GRAPHICS_555[climate_id] : MAIN_GRAPHICS_555[climate_id];
const char *filename_idx = is_editor ? EDITOR_GRAPHICS_SG2[climate_id] : MAIN_GRAPHICS_SG2[climate_id];

if (MAIN_INDEX_SIZE != io_read_file_into_buffer(filename_idx, data.tmp_data, MAIN_INDEX_SIZE)) {
if (MAIN_INDEX_SIZE != io_read_file_into_buffer(filename_idx, data.tmp_data, MAIN_INDEX_SIZE, 1)) {
return 0;
}

Expand All @@ -307,7 +307,7 @@ int image_load_climate(int climate_id, int is_editor)
buffer_init(&buf, &data.tmp_data[HEADER_SIZE], ENTRY_SIZE * MAIN_ENTRIES);
read_index(&buf, data.main, MAIN_ENTRIES);

int data_size = io_read_file_into_buffer(filename_bmp, data.tmp_data, SCRATCH_DATA_SIZE);
int data_size = io_read_file_into_buffer(filename_bmp, data.tmp_data, SCRATCH_DATA_SIZE, 1);
if (!data_size) {
return 0;
}
Expand Down Expand Up @@ -349,14 +349,14 @@ static int load_cyrillic_fonts(void)
return 0;
}
if (CYRILLIC_FONT_INDEX_SIZE != io_read_file_part_into_buffer(CYRILLIC_FONTS_SG2, data.tmp_data,
CYRILLIC_FONT_INDEX_SIZE, CYRILLIC_FONT_INDEX_OFFSET)) {
CYRILLIC_FONT_INDEX_SIZE, CYRILLIC_FONT_INDEX_OFFSET, 1)) {
return 0;
}
buffer buf;
buffer_init(&buf, data.tmp_data, CYRILLIC_FONT_INDEX_SIZE);
read_index(&buf, data.font, CYRILLIC_FONT_ENTRIES);

int data_size = io_read_file_into_buffer(CYRILLIC_FONTS_555, data.tmp_data, SCRATCH_DATA_SIZE);
int data_size = io_read_file_into_buffer(CYRILLIC_FONTS_555, data.tmp_data, SCRATCH_DATA_SIZE, 1);
if (!data_size) {
return 0;
}
Expand Down Expand Up @@ -409,7 +409,7 @@ static int load_traditional_chinese_fonts(void)
return 0;
}

int data_size = io_read_file_into_buffer(TRAD_CHINESE_FONTS_555, data.tmp_data, SCRATCH_DATA_SIZE);
int data_size = io_read_file_into_buffer(TRAD_CHINESE_FONTS_555, data.tmp_data, SCRATCH_DATA_SIZE, 1);
if (!data_size) {
return 0;
}
Expand Down Expand Up @@ -446,15 +446,15 @@ int image_load_enemy(int enemy_id)
const char *filename_bmp = ENEMY_GRAPHICS_555[enemy_id];
const char *filename_idx = ENEMY_GRAPHICS_SG2[enemy_id];

if (ENEMY_INDEX_SIZE != io_read_file_part_into_buffer(filename_idx, data.tmp_data, ENEMY_INDEX_SIZE, ENEMY_INDEX_OFFSET)) {
if (ENEMY_INDEX_SIZE != io_read_file_part_into_buffer(filename_idx, data.tmp_data, ENEMY_INDEX_SIZE, ENEMY_INDEX_OFFSET, 0)) {
return 0;
}

buffer buf;
buffer_init(&buf, data.tmp_data, ENEMY_INDEX_SIZE);
read_index(&buf, data.enemy, ENEMY_ENTRIES);

int data_size = io_read_file_into_buffer(filename_bmp, data.tmp_data, SCRATCH_DATA_SIZE);
int data_size = io_read_file_into_buffer(filename_bmp, data.tmp_data, SCRATCH_DATA_SIZE, 1);
if (!data_size) {
return 0;
}
Expand All @@ -471,13 +471,13 @@ static const color_t *load_external_data(int image_id)
file_change_extension(filename, "555");
int size = io_read_file_part_into_buffer(
&filename[4], data.tmp_data,
img->draw.data_length, img->draw.offset - 1
img->draw.data_length, img->draw.offset - 1, 1
);
if (!size) {
// try in 555 dir
size = io_read_file_part_into_buffer(
filename, data.tmp_data,
img->draw.data_length, img->draw.offset - 1
img->draw.data_length, img->draw.offset - 1, 1
);
if (!size) {
log_error("unable to load external image",
Expand Down
10 changes: 5 additions & 5 deletions src/core/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#include "core/dir.h"
#include "core/file.h"

int io_read_file_into_buffer(const char *filepath, void *buffer, int max_size)
int io_read_file_into_buffer(const char *filepath, void *buffer, int max_size, int localizable)
{
const char *cased_file = dir_get_case_corrected_file(filepath);
const char *cased_file = dir_get_case_corrected_file(filepath, localizable);
if (!cased_file) {
return 0;
}
Expand All @@ -26,9 +26,9 @@ int io_read_file_into_buffer(const char *filepath, void *buffer, int max_size)
return bytes_read;
}

int io_read_file_part_into_buffer(const char *filepath, void *buffer, int size, int offset_in_file)
int io_read_file_part_into_buffer(const char *filepath, void *buffer, int size, int offset_in_file, int localizable)
{
const char *cased_file = dir_get_case_corrected_file(filepath);
const char *cased_file = dir_get_case_corrected_file(filepath, localizable);
if (!cased_file) {
return 0;
}
Expand All @@ -47,7 +47,7 @@ int io_read_file_part_into_buffer(const char *filepath, void *buffer, int size,
int io_write_buffer_to_file(const char *filepath, const void *buffer, int size)
{
// Find existing file to overwrite
const char *cased_file = dir_get_case_corrected_file(filepath);
const char *cased_file = dir_get_case_corrected_file(filepath, 0);
if (!cased_file) {
cased_file = filepath;
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @param max_size Max size to read
* @return Number of bytes read
*/
int io_read_file_into_buffer(const char *filepath, void *buffer, int max_size);
int io_read_file_into_buffer(const char *filepath, void *buffer, int max_size, int localizable);

/**
* Reads part of the file into the buffer
Expand All @@ -22,7 +22,7 @@ int io_read_file_into_buffer(const char *filepath, void *buffer, int max_size);
* @param size Number of bytes to read
* @param offset_in_file Offset into the file to start reading
*/
int io_read_file_part_into_buffer(const char *filepath, void *buffer, int size, int offset_in_file);
int io_read_file_part_into_buffer(const char *filepath, void *buffer, int size, int offset_in_file, int localizable);

/**
* Writes the entire buffer to the file
Expand Down
4 changes: 2 additions & 2 deletions src/core/lang.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static void parse_text(buffer *buf)
static int load_text(const char *filename, uint8_t *buf_data)
{
buffer buf;
int filesize = io_read_file_into_buffer(filename, buf_data, BUFFER_SIZE);
int filesize = io_read_file_into_buffer(filename, buf_data, BUFFER_SIZE, 1);
if (filesize < MIN_TEXT_SIZE || filesize > MAX_TEXT_SIZE) {
return 0;
}
Expand Down Expand Up @@ -97,7 +97,7 @@ static void parse_message(buffer *buf)
static int load_message(const char *filename, uint8_t *data_buffer)
{
buffer buf;
int filesize = io_read_file_into_buffer(filename, data_buffer, BUFFER_SIZE);
int filesize = io_read_file_into_buffer(filename, data_buffer, BUFFER_SIZE, 1);
if (filesize < MIN_MESSAGE_SIZE || filesize > MAX_MESSAGE_SIZE) {
return 0;
}
Expand Down
11 changes: 11 additions & 0 deletions src/core/locale.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ static const uint8_t NEW_GAME_TRADITIONAL_CHINESE[] = { 0x83, 0x80, 0x20, 0x84,

static struct {
language_type last_determined_language;
const char *locale_directory;
} data;

static language_type determine_language(void)
Expand Down Expand Up @@ -73,6 +74,16 @@ language_type locale_determine_language(void)
return data.last_determined_language;
}

const char *locale_get_directory(void)
{
return data.locale_directory;
}

void locale_set_directory(const char *dir)
{
data.locale_directory = dir;
}

int locale_year_before_ad(void)
{
// In all languages it's "200 AD" except for English
Expand Down
12 changes: 12 additions & 0 deletions src/core/locale.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ typedef enum {
*/
language_type locale_determine_language(void);

/**
* Gets the locale directory, if set
* @return Directory name or null
*/
const char *locale_get_directory(void);

/**
* Sets the locale directory
* @param dir Directory, or null
*/
void locale_set_directory(const char *dir);

/**
* Check whether to write the year before the 'AD' part or the other way around
* @return Boolean true if years should be written as "200 AD", false for "AD 200"
Expand Down
2 changes: 1 addition & 1 deletion src/editor/editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static int is_active;
int editor_is_present(void)
{
for (int i = 0; i < MAX_EDITOR_FILES; i++) {
if (!file_exists(EDITOR_FILES[i])) {
if (!file_exists(EDITOR_FILES[i], 1)) {
return 0;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/empire/empire.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void empire_load(int is_custom_scenario, int empire_id)
const char *filename = is_custom_scenario ? "c32.emp" : "c3.emp";

// read header with scroll positions
if (!io_read_file_part_into_buffer(filename, raw_data, 4, 32 * empire_id)) {
if (!io_read_file_part_into_buffer(filename, raw_data, 4, 32 * empire_id, 0)) {
memset(raw_data, 0, 4);
}
buffer buf;
Expand All @@ -46,7 +46,7 @@ void empire_load(int is_custom_scenario, int empire_id)

// read data section with objects
int offset = EMPIRE_HEADER_SIZE + EMPIRE_DATA_SIZE * empire_id;
if (io_read_file_part_into_buffer(filename, raw_data, EMPIRE_DATA_SIZE, offset) != EMPIRE_DATA_SIZE) {
if (io_read_file_part_into_buffer(filename, raw_data, EMPIRE_DATA_SIZE, offset, 0) != EMPIRE_DATA_SIZE) {
// load empty empire when loading fails
log_error("Unable to load empire data from file", filename, 0);
memset(raw_data, 0, EMPIRE_DATA_SIZE);
Expand Down
6 changes: 3 additions & 3 deletions src/game/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ static void initialize_scenario_data(const uint8_t *scenario_name)

static int load_custom_scenario(const uint8_t *scenario_name, const char *scenario_file)
{
if (!file_exists(scenario_file)) {
if (!file_exists(scenario_file, 0)) {
return 0;
}

Expand Down Expand Up @@ -247,7 +247,7 @@ static int get_campaign_mission_offset(int mission_id)
uint8_t offset_data[4];
buffer buf;
buffer_init(&buf, offset_data, 4);
if (!io_read_file_part_into_buffer(MISSION_PACK_FILE, offset_data, 4, 4 * mission_id)) {
if (!io_read_file_part_into_buffer(MISSION_PACK_FILE, offset_data, 4, 4 * mission_id, 0)) {
return 0;
}
return buffer_read_i32(&buf);
Expand Down Expand Up @@ -374,7 +374,7 @@ void game_file_write_mission_saved_game(void)
} else if (rank > 11) {
rank = 11;
}
if (city_mission_should_save_start() && !file_exists(MISSION_SAVED_GAMES[rank])) {
if (city_mission_should_save_start() && !file_exists(MISSION_SAVED_GAMES[rank], 0)) {
game_file_io_write_saved_game(MISSION_SAVED_GAMES[rank]);
}
}
Loading

0 comments on commit 6dd7fe8

Please sign in to comment.