Skip to content

Commit

Permalink
Resource manager rework
Browse files Browse the repository at this point in the history
- Use user specific writable application folder for preferences, logs
and saved game files.
- Configure game data directory via settings.ini.
- Removed support for selective installation types via command line
arguments.
- Removed CD-ROM support.
- Removed feature to only render flic animation still frames when
reading flic files from CD-ROM.
- Use basic dialog window to report startup issues to users instead of
console output.
- Removed console output window on Windows.
- Use SDL_Log to log runtime errors and redirect its output to
stdout.txt. Use AiLog and NetLog for debug messages.
  • Loading branch information
klei1984 committed Mar 4, 2024
1 parent 65acf6d commit c2551e1
Show file tree
Hide file tree
Showing 30 changed files with 1,431 additions and 1,406 deletions.
2,259 changes: 1,135 additions & 1,124 deletions assets/lang_english.ini

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions assets/lang_french.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1122,3 +1122,14 @@ eb57=%s à [%i,%i] réagissant à ennemie %s à [%i,%i].
7101=%s %i sera disponible dans %i tour(s).
c366=Fini
36f9=Annuler
2690=Failed to obtain application base directory.
416b=Failed to obtain application user directory.
0f72=Failed to obtain application data directory.
438a=Failed to initialize SDL library.
d0a2=Not enough memory available to run M.A.X.\nAmount Needed: %i MiB, Amount found: %i MiB.
efb0=The Drive has less than %.2f MiB available space.\nYou may have trouble saving games...
cf05=M.A.X. Error
b7f4=M.A.X. Warning
3889=Cannot create saved game backup: '%s'.
37d9=Failed to initialize audio library.
6a9c=Failed to initialize font library.
11 changes: 11 additions & 0 deletions assets/lang_german.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1122,3 +1122,14 @@ eb57=%s bei [%i,%i] reagiert auf feindliche %s bei [%i,%i].
7101=%s %i wird nach %i Runden verfügbar sein.
c366=Fertig
36f9=Abbruch
2690=Failed to obtain application base directory.
416b=Failed to obtain application user directory.
0f72=Failed to obtain application data directory.
438a=Failed to initialize SDL library.
d0a2=Not enough memory available to run M.A.X.\nAmount Needed: %i MiB, Amount found: %i MiB.
efb0=The Drive has less than %.2f MiB available space.\nYou may have trouble saving games...
cf05=M.A.X. Error
b7f4=M.A.X. Warning
3889=Cannot create saved game backup: '%s'.
37d9=Failed to initialize audio library.
6a9c=Failed to initialize font library.
11 changes: 11 additions & 0 deletions assets/lang_italian.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1122,3 +1122,14 @@ eb57=%s in %i,%i reagisce a %s nemica in
7101=%s %i sarà disponibile tra %i turno/i.
c366=Fatto
36f9=Annulla
2690=Failed to obtain application base directory.
416b=Failed to obtain application user directory.
0f72=Failed to obtain application data directory.
438a=Failed to initialize SDL library.
d0a2=Not enough memory available to run M.A.X.\nAmount Needed: %i MiB, Amount found: %i MiB.
efb0=The Drive has less than %.2f MiB available space.\nYou may have trouble saving games...
cf05=M.A.X. Error
b7f4=M.A.X. Warning
3889=Cannot create saved game backup: '%s'.
37d9=Failed to initialize audio library.
6a9c=Failed to initialize font library.
11 changes: 11 additions & 0 deletions assets/lang_spanish.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1122,3 +1122,14 @@ eb57=%s en el punto [%i,%i] respondiente a %s enemigo en [%i,%i].
7101=%s %i estare disponible en %i turnos.
c366=Fin
36f9=Cancelar
2690=Failed to obtain application base directory.
416b=Failed to obtain application user directory.
0f72=Failed to obtain application data directory.
438a=Failed to initialize SDL library.
d0a2=Not enough memory available to run M.A.X.\nAmount Needed: %i MiB, Amount found: %i MiB.
efb0=The Drive has less than %.2f MiB available space.\nYou may have trouble saving games...
cf05=M.A.X. Error
b7f4=M.A.X. Warning
3889=Cannot create saved game backup: '%s'.
37d9=Failed to initialize audio library.
6a9c=Failed to initialize font library.
7 changes: 4 additions & 3 deletions assets/settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ last_campaign=1
movie_play=0
alt_movie_res=0
language=english
game_data=.

[OPTIONS]
world=0
Expand Down Expand Up @@ -56,9 +57,9 @@ blue_team_clan=0
gray_team_clan=0

[AUDIO_SETTINGS]
Audio_Device_Name=None
Audio_Device_ID=-1
Channels_Reversed=0
audio_device_name=None
audio_device_id=-1
channels_reversed=0

[GRAPHICS_SETTINGS]
display_index=0
Expand Down
5 changes: 4 additions & 1 deletion src/ailog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "ailog.hpp"

#include "gnw.h"
#include "resource_manager.hpp"
#include "smartstring.hpp"

#define AILOG_FILE_LIMIT UINT16_MAX
Expand Down Expand Up @@ -98,7 +99,9 @@ void AiLog_Open() {
AiLog_Close();
}

AiLog::AiLog_File.open("ai_log.txt");
auto filepath = (ResourceManager_FilePathGamePref / "ai_log.txt").lexically_normal();

AiLog::AiLog_File.open(filepath.string().c_str());
AiLog::AiLog_EntryCount = 0;
}

Expand Down
11 changes: 2 additions & 9 deletions src/flicsmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,6 @@ char flicsmgr_read(Flic *flc) {
free(flc->frames[0].buffer);
flc->frames[0].buffer = nullptr;

if (ResourceManager_IsMaxDdInUse && strcmp(ResourceManager_FilePathFlc, ResourceManager_FilePathGameInstall)) {
flc->frame_count = 1;
}

if (flc->frame_count > 1 && flc->animate) {
flc->frame_pos = 1;
fseek(flc->fp, flc->file_pos, SEEK_SET);
Expand Down Expand Up @@ -399,20 +395,17 @@ char flicsmgr_read(Flic *flc) {
}

char flicsmgr_load(char *flic_file, Flic *flc) {
char filename[PATH_MAX];

if (!flic_file) {
return 0;
}

ResourceManager_ToUpperCase(flic_file);

strcpy(filename, ResourceManager_FilePathFlc);
strcat(filename, flic_file);
auto filepath = ResourceManager_FilePathFlic / flic_file;

delete[] flic_file;

flc->fp = fopen(filename, "rb");
flc->fp = fopen(filepath.lexically_normal().string().c_str(), "rb");

if (!flc->fp) {
return 0;
Expand Down
10 changes: 6 additions & 4 deletions src/game_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3985,15 +3985,17 @@ void GameManager_MenuClickChatGoalButton() {

} else if (GameManager_GameFileNumber) {
int32_t game_file_type;
char file_path[PATH_MAX];
SmartString filename;
std::filesystem::path filepath;
FILE* fp;

game_file_type = ini_get_setting(INI_GAME_FILE_TYPE);

sprintf(file_path, "%sdescr%i.%s", ResourceManager_FilePathText, GameManager_GameFileNumber,
SaveLoadMenu_SaveFileTypes[game_file_type]);
filename.Sprintf(20, "descr%i.%s", GameManager_GameFileNumber, SaveLoadMenu_SaveFileTypes[game_file_type])
.Toupper();
filepath = (ResourceManager_FilePathText / filename.GetCStr()).lexically_normal();

fp = fopen(file_path, "rt");
fp = fopen(filepath.string().c_str(), "rt");

if (fp) {
int32_t text_size;
Expand Down
12 changes: 5 additions & 7 deletions src/gamesetupmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ void GameSetupMenu::DrawMissionList() {

void GameSetupMenu::LoadMissionDescription() {
SmartString string;
SmartString filename;
std::filesystem::path filepath;
FILE* fp;
int32_t width;
int32_t height;
Expand All @@ -348,14 +350,10 @@ void GameSetupMenu::LoadMissionDescription() {
}
}

std::string file_name =
"descr" + std::to_string(game_file_number) + "." + SaveLoadMenu_SaveFileTypes[game_file_type];
filename.Sprintf(20, "descr%i.%s", game_file_number, SaveLoadMenu_SaveFileTypes[game_file_type]).Toupper();
filepath = (ResourceManager_FilePathText / filename.GetCStr()).lexically_normal();

ResourceManager_ToUpperCase(file_name);

auto filepath = std::filesystem::path(ResourceManager_FilePathText) / file_name;

fp = fopen(file_name.c_str(), "rt");
fp = fopen(filepath.string().c_str(), "rt");

if (fp) {
char character;
Expand Down
18 changes: 10 additions & 8 deletions src/ini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static uint32_t hex_to_dec_lut2[] = {0x00, 0x01, 0x10, 0x100, 0x1000, 0x10000, 0
static int32_t inifile_hex_to_dec_digit(char c);

void inifile_load_from_resource(Ini_descriptor *const pini, ResourceID resource_id) {
pini->ini_file_path[0] = '\0';
pini->ini_file_path.clear();
pini->file_size = ResourceManager_GetResourceSize(resource_id);
pini->buffer_size = pini->file_size;
pini->buffer = reinterpret_cast<char *>(ResourceManager_ReadResource(resource_id));
Expand All @@ -51,8 +51,8 @@ int32_t inifile_init_ini_object_from_ini_file(Ini_descriptor *const pini, const
int32_t result;
FILE *fp;

strcpy(pini->ini_file_path, inifile_path);
fp = fopen(inifile_path, "rb");
pini->ini_file_path = std::filesystem::path(inifile_path).lexically_normal();
fp = fopen(pini->ini_file_path.string().c_str(), "rb");

if (fp == nullptr) {
return 0;
Expand Down Expand Up @@ -95,7 +95,7 @@ int32_t inifile_save_to_file(Ini_descriptor *const pini) {

if (pini->buffer) {
if (pini->flags & 0x80u) {
fp = fopen(pini->ini_file_path, "wb");
fp = fopen(pini->ini_file_path.string().c_str(), "wb");

if (fp == nullptr) {
return 0;
Expand Down Expand Up @@ -352,7 +352,7 @@ int32_t inifile_ini_process_string_value(Ini_descriptor *const pini, char *const
}

int32_t inifile_ini_get_string(Ini_descriptor *const pini, char *const buffer, const uint32_t buffer_size,
const int32_t mode, bool skip_leading_white_space) {
const int32_t mode, bool skip_leading_white_space) {
char *address;
char *end_address;
uint32_t offset;
Expand Down Expand Up @@ -536,7 +536,8 @@ int32_t inifile_get_boolean_value(Ini_descriptor *const pini, const char *const
return 0;
}

int32_t inifile_ini_get_numeric_value(Ini_descriptor *const pini, const char *const ini_param_name, int32_t *const value) {
int32_t inifile_ini_get_numeric_value(Ini_descriptor *const pini, const char *const ini_param_name,
int32_t *const value) {
int32_t result;

if (inifile_ini_seek_param(pini, ini_param_name)) {
Expand All @@ -549,7 +550,7 @@ int32_t inifile_ini_get_numeric_value(Ini_descriptor *const pini, const char *co
}

int32_t inifile_ini_get_string_value(Ini_descriptor *const pini, const char *const ini_param_name, char *const buffer,
const int32_t buffer_size) {
const int32_t buffer_size) {
int32_t result;

if (inifile_ini_seek_param(pini, ini_param_name)) {
Expand Down Expand Up @@ -732,7 +733,8 @@ int32_t inifile_add_string_param(Ini_descriptor *const pini, char *ini_param_nam
return result;
}

int32_t inifile_add_numeric_param(Ini_descriptor *const pini, char *ini_param_name, int32_t value, int32_t size, int32_t radix) {
int32_t inifile_add_numeric_param(Ini_descriptor *const pini, char *ini_param_name, int32_t value, int32_t size,
int32_t radix) {
char buffer[30];
char *src;
int32_t length;
Expand Down
10 changes: 6 additions & 4 deletions src/ini.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#ifndef INI_HPP
#define INI_HPP

#include <climits>
#include <filesystem>

#include "enums.hpp"

Expand Down Expand Up @@ -99,6 +99,7 @@ enum IniParameter {
INI_MOVIE_PLAY,
INI_ALT_MOVIE_RES,
INI_LANGUAGE,
INI_GAME_DATA,

/// OPTIONS SECTION
INI_OPTIONS,
Expand Down Expand Up @@ -169,7 +170,7 @@ struct Ini_descriptor {
uint8_t unknown_1;
uint8_t flags;
uint8_t unknown_2[2];
char ini_file_path[PATH_MAX];
std::filesystem::path ini_file_path;
char *buffer;
uint32_t file_size;
uint32_t buffer_size;
Expand All @@ -186,11 +187,12 @@ int32_t inifile_init_ini_object_from_ini_file(Ini_descriptor *const pini, const
uint32_t inifile_hex_to_dec(const char *const hex);
int32_t inifile_ini_seek_section(Ini_descriptor *const pini, const char *const ini_section_name);
int32_t inifile_ini_seek_param(Ini_descriptor *const pini, const char *const ini_param_name);
int32_t inifile_ini_get_numeric_value(Ini_descriptor *const pini, const char *const ini_param_name, int32_t *const value);
int32_t inifile_ini_get_numeric_value(Ini_descriptor *const pini, const char *const ini_param_name,
int32_t *const value);
int32_t inifile_ini_set_numeric_value(Ini_descriptor *const pini, const int32_t value);
int32_t inifile_ini_set_string_value(Ini_descriptor *const pini, const char *value);
int32_t inifile_ini_get_string(Ini_descriptor *const pini, char *const buffer, const uint32_t buffer_size,
const int32_t mode, bool skip_leading_white_space = true);
const int32_t mode, bool skip_leading_white_space = true);
int32_t inifile_save_to_file(Ini_descriptor *const pini);
void inifile_load_from_resource(Ini_descriptor *const pini, ResourceID resource_id);
int32_t inifile_ini_process_string_value(Ini_descriptor *const pini, char *const buffer, const uint32_t buffer_size);
Expand Down
18 changes: 9 additions & 9 deletions src/inifile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ static const IniKey ini_keys_table[] = {
{"movie_play", "0", INI_NUMERIC},
{"alt_movie_res", "0", INI_NUMERIC},
{"language", "english", INI_STRING},
{"game_data", "", INI_STRING},

/// Keep original layout for v1.04 save game compatibility
{"OPTIONS", nullptr, INI_SECTION},
Expand Down Expand Up @@ -158,9 +159,9 @@ static const IniKey ini_keys_table[] = {
{"gray_team_clan", "0", INI_NUMERIC},

{"AUDIO_SETTINGS", nullptr, INI_SECTION},
{"Audio_Device_Name", "None", INI_STRING},
{"Audio_Device_ID", "-1", INI_NUMERIC},
{"Channels_Reversed", "0", INI_NUMERIC},
{"audio_device_name", "None", INI_STRING},
{"audio_device_id", "-1", INI_NUMERIC},
{"channels_reversed", "0", INI_NUMERIC},

{"GRAPHICS_SETTINGS", nullptr, INI_SECTION},
{"display_index", "0", INI_NUMERIC},
Expand Down Expand Up @@ -212,17 +213,16 @@ void IniSettings::Init() {
int32_t v1;
int32_t value;
char format_string[50];
char filename[PATH_MAX];
FILE *fp;
int32_t index;

strcpy(filename, ResourceManager_FilePathGameInstall);
strcat(filename, "settings.ini");
fp = fopen(filename, "rt");
auto filepath{ResourceManager_FilePathGamePref / "settings.ini"};

fp = fopen(filepath.string().c_str(), "rt");

if (!fp) {
SDL_Log(_(f3e3));
fp = fopen(filename, "wt");
fp = fopen(filepath.string().c_str(), "wt");

if (!fp) {
SDL_Log(_(db0c));
Expand Down Expand Up @@ -252,7 +252,7 @@ void IniSettings::Init() {

fclose(fp);

if (!inifile_init_ini_object_from_ini_file(&ini, filename)) {
if (!inifile_init_ini_object_from_ini_file(&ini, filepath.string().c_str())) {
ResourceManager_ExitGame(EXIT_CODE_CANNOT_FIND_MAX_INI);
}

Expand Down
4 changes: 3 additions & 1 deletion src/lang_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,6 @@
{"bb70", INI_STRING}, {"2c2a", INI_STRING}, {"1141", INI_STRING}, {"1c5c", INI_STRING}, {"f876", INI_STRING}, \
{"a939", INI_STRING}, {"8c00", INI_STRING}, {"ea13", INI_STRING}, {"836d", INI_STRING}, {"c15c", INI_STRING}, \
{"60f3", INI_STRING}, {"1b93", INI_STRING}, {"5a01", INI_STRING}, {"eb57", INI_STRING}, {"360a", INI_STRING}, \
{"16f4", INI_STRING}, {"7101", INI_STRING}, {"c366", INI_STRING}, {"36f9", INI_STRING},
{"16f4", INI_STRING}, {"7101", INI_STRING}, {"c366", INI_STRING}, {"36f9", INI_STRING}, {"2690", INI_STRING}, \
{"416b", INI_STRING}, {"0f72", INI_STRING}, {"438a", INI_STRING}, {"d0a2", INI_STRING}, {"efb0", INI_STRING}, \
{"cf05", INI_STRING}, {"b7f4", INI_STRING}, {"3889", INI_STRING}, {"37d9", INI_STRING}, {"6a9c", INI_STRING},
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "sound_manager.hpp"

int main(int argc, char* argv[]) {
ResourceManager_InitResources(argc, argv);
ResourceManager_InitResources();

if (Movie_PlayIntro()) {
menu_draw_logo(ILOGO, 3000);
Expand Down
Loading

0 comments on commit c2551e1

Please sign in to comment.