Convert LocalCustomGraphicsDir and LocalCustomSoundsDir to use std::fs::path

Write custom graphics and sounds to writable base directory.
This commit is contained in:
Azamat H. Hackimov
2024-10-08 17:42:30 +03:00
committed by Louis Gombert
parent a3a66339a9
commit 405f6dfec6
9 changed files with 43 additions and 41 deletions

View File

@@ -18,8 +18,8 @@
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <filesystem>
#include "debug.h"
#include "pserror.h" #include "pserror.h"
#include "audiotaunts.h" #include "audiotaunts.h"
#include "cfile.h" #include "cfile.h"
@@ -92,7 +92,7 @@ void taunt_SetDelayTime(float t) { Audio_taunt_delay_time = t; }
// taunt_PlayTauntFile // taunt_PlayTauntFile
// //
// Given a path to an .osf file, it will play it // Given a path to an .osf file, it will play it
bool taunt_PlayTauntFile(const char *filename) { bool taunt_PlayTauntFile(const std::filesystem::path &filename) {
if (!Audio_taunts_enabled) if (!Audio_taunts_enabled)
return false; return false;
@@ -113,7 +113,7 @@ bool taunt_PlayPlayerTaunt(int pnum, int index) {
} }
if ((NetPlayers[pnum].flags & NPF_CONNECTED) && (NetPlayers[pnum].sequence == NETSEQ_PLAYING)) { if ((NetPlayers[pnum].flags & NPF_CONNECTED) && (NetPlayers[pnum].sequence == NETSEQ_PLAYING)) {
char fullpath[_MAX_PATH]; std::filesystem::path fullpath;
char *file; char *file;
switch (index) { switch (index) {
case 0: case 0:
@@ -130,10 +130,10 @@ bool taunt_PlayPlayerTaunt(int pnum, int index) {
break; break;
} }
ddio_MakePath(fullpath, LocalCustomSoundsDir, file, NULL); fullpath = LocalCustomSoundsDir / file;
if (!cfexist(fullpath)) { if (!cfexist(fullpath)) {
LOG_WARNING.printf("TAUNT: file %s doesn't exist (pnum=%d)", fullpath, pnum); LOG_WARNING.printf("TAUNT: file %s doesn't exist (pnum=%d)", fullpath.u8string().c_str(), pnum);
return false; return false;
} }

View File

@@ -45,6 +45,8 @@
#ifndef __AUDIO_TAUNT_H_ #ifndef __AUDIO_TAUNT_H_
#define __AUDIO_TAUNT_H_ #define __AUDIO_TAUNT_H_
#include <filesystem>
extern bool Audio_taunts_enabled; extern bool Audio_taunts_enabled;
// Error codes: // Error codes:
@@ -82,7 +84,7 @@ bool taunt_ImportWave(const char *wave_filename, const char *outputfilename);
// taunt_PlayTauntFile // taunt_PlayTauntFile
// //
// Given a path to an .osf file, it will play it // Given a path to an .osf file, it will play it
bool taunt_PlayTauntFile(const char *filename); bool taunt_PlayTauntFile(const std::filesystem::path &filename);
// taunt_PlayPlayerTaunt // taunt_PlayPlayerTaunt
// //

View File

@@ -8201,20 +8201,20 @@ void MultiSendRequestPlayTaunt(int index) {
return; return;
// make sure an audio file exists there // make sure an audio file exists there
char audio_file[_MAX_PATH]; std::filesystem::path audio_file;
switch (index) { switch (index) {
case 0: case 0:
ddio_MakePath(audio_file, LocalCustomSoundsDir, NetPlayers[Player_num].voice_taunt1, NULL); audio_file = LocalCustomSoundsDir / NetPlayers[Player_num].voice_taunt1;
break; break;
case 1: case 1:
ddio_MakePath(audio_file, LocalCustomSoundsDir, NetPlayers[Player_num].voice_taunt2, NULL); audio_file = LocalCustomSoundsDir / NetPlayers[Player_num].voice_taunt2;
break; break;
case 2: case 2:
ddio_MakePath(audio_file, LocalCustomSoundsDir, NetPlayers[Player_num].voice_taunt3, NULL); audio_file = LocalCustomSoundsDir / NetPlayers[Player_num].voice_taunt3;
break; break;
case 3: case 3:
ddio_MakePath(audio_file, LocalCustomSoundsDir, NetPlayers[Player_num].voice_taunt4, NULL); audio_file = LocalCustomSoundsDir / NetPlayers[Player_num].voice_taunt4;
break; break;
} }

View File

@@ -2525,7 +2525,7 @@ bool PltSelectShip(pilot *Pilot) {
FreeVClip(handle); FreeVClip(handle);
break; break;
} }
std::filesystem::path newf = std::filesystem::path(LocalCustomGraphicsDir) / tempf.filename().replace_extension(".oaf"); std::filesystem::path newf = LocalCustomGraphicsDir / tempf.filename().replace_extension(".oaf");
if (SaveVClip(newf, handle) == 0) { if (SaveVClip(newf, handle) == 0) {
// error saving // error saving
@@ -2580,7 +2580,7 @@ bool PltSelectShip(pilot *Pilot) {
if (DoPathFileDialog(false, path, TXT_CHOOSE, {"*.wav"}, PFDF_FILEMUSTEXIST)) { if (DoPathFileDialog(false, path, TXT_CHOOSE, {"*.wav"}, PFDF_FILEMUSTEXIST)) {
std::filesystem::path dpath; std::filesystem::path dpath;
std::filesystem::path filename = path.filename().replace_extension(".osf"); std::filesystem::path filename = path.filename().replace_extension(".osf");
std::filesystem::path tempfile = std::filesystem::path(LocalCustomSoundsDir) / filename; std::filesystem::path tempfile = LocalCustomSoundsDir / filename;
// import the sound // import the sound
LOG_INFO.printf("Importing: '%s'->'%s'", path.u8string().c_str(), tempfile.u8string().c_str()); LOG_INFO.printf("Importing: '%s'->'%s'", path.u8string().c_str(), tempfile.u8string().c_str());

View File

@@ -128,8 +128,8 @@ extern char LocalScriptDir[];
extern std::filesystem::path NetArtDir; extern std::filesystem::path NetArtDir;
extern std::filesystem::path LocalArtDir; extern std::filesystem::path LocalArtDir;
extern char LocalCustomGraphicsDir[]; extern std::filesystem::path LocalCustomGraphicsDir;
extern char LocalCustomSoundsDir[]; extern std::filesystem::path LocalCustomSoundsDir;
extern char TempTableFilename[]; extern char TempTableFilename[];
extern char TempTableLockFilename[]; extern char TempTableLockFilename[];

View File

@@ -489,8 +489,8 @@ std::filesystem::path LocalMusicDir, NetMusicDir;
std::filesystem::path LocalVoiceDir, NetVoiceDir; std::filesystem::path LocalVoiceDir, NetVoiceDir;
std::filesystem::path NetTableDir, LocalTableDir; std::filesystem::path NetTableDir, LocalTableDir;
char LocalD3Dir[TABLE_NAME_LEN], NetD3Dir[TABLE_NAME_LEN]; char LocalD3Dir[TABLE_NAME_LEN], NetD3Dir[TABLE_NAME_LEN];
char LocalCustomGraphicsDir[TABLE_NAME_LEN]; std::filesystem::path LocalCustomGraphicsDir;
char LocalCustomSoundsDir[TABLE_NAME_LEN]; std::filesystem::path LocalCustomSoundsDir;
std::filesystem::path LockerFile; std::filesystem::path LockerFile;
std::filesystem::path VersionFile; std::filesystem::path VersionFile;
char TableUser[TABLE_NAME_LEN]; char TableUser[TABLE_NAME_LEN];
@@ -601,17 +601,14 @@ int mng_InitTableFiles() {
Network_up = 0; Network_up = 0;
} }
// Do locals
mng_InitLocalTables();
mng_InitLocalDirectories();
mng_CheckToCreateLocalTables();
if (Network_up == 0) { if (Network_up == 0) {
mng_InitLocalTables();
mng_InitLocalDirectories();
mng_CheckToCreateLocalTables();
mng_InitTrackLocks(); mng_InitTrackLocks();
} else { } else {
// Do locals
mng_InitLocalTables();
mng_InitLocalDirectories();
mng_CheckToCreateLocalTables();
// Do network // Do network
mng_InitNetTables(); mng_InitNetTables();
mng_InitNetDirectories(); mng_InitNetDirectories();
@@ -689,8 +686,8 @@ int mng_InitLocalTables() {
LocalManageGraphicsDir = localdir / "data" / "graphics"; LocalManageGraphicsDir = localdir / "data" / "graphics";
LocalModelsDir = localdir / "data" / "models"; LocalModelsDir = localdir / "data" / "models";
LocalSoundsDir = localdir / "data" / "sounds"; LocalSoundsDir = localdir / "data" / "sounds";
ddio_MakePath(LocalCustomSoundsDir, LocalD3Dir, "custom", "sounds", NULL); LocalCustomSoundsDir = cf_GetWritableBaseDirectory() / "custom" / "sounds";
ddio_MakePath(LocalCustomGraphicsDir, LocalD3Dir, "custom", "graphics", NULL); LocalCustomGraphicsDir = cf_GetWritableBaseDirectory() / "custom" / "graphics";
LocalRoomsDir = localdir / "data" / "rooms"; LocalRoomsDir = localdir / "data" / "rooms";
LocalBriefingDir = localdir / "data" / "briefings"; LocalBriefingDir = localdir / "data" / "briefings";
ddio_MakePath(LocalScriptDir, LocalD3Dir, "data", "scripts", NULL); ddio_MakePath(LocalScriptDir, LocalD3Dir, "data", "scripts", NULL);
@@ -815,18 +812,19 @@ void mng_CheckToCreateLocalTables() {
} }
// Creates directories if needed // Creates directories if needed
void mng_InitLocalDirectories() { void mng_InitLocalDirectories() {
std::filesystem::path dir = LocalD3Dir; std::filesystem::path dir = cf_GetWritableBaseDirectory();
std::error_code ec; std::error_code ec;
std::filesystem::create_directories(dir / "custom", ec); std::filesystem::create_directories(dir / "demo", ec);
std::filesystem::create_directories(dir / "custom" / "graphics", ec); std::filesystem::create_directories(dir / "custom" / "graphics", ec);
std::filesystem::create_directories(dir / "custom" / "sounds", ec); std::filesystem::create_directories(dir / "custom" / "sounds", ec);
std::filesystem::create_directories(dir / "custom" / "settings", ec); std::filesystem::create_directories(dir / "custom" / "settings", ec);
std::filesystem::create_directories(dir / "savegame", ec);
cf_SetSearchPath(LocalCustomGraphicsDir); cf_SetSearchPath(LocalCustomGraphicsDir);
cf_SetSearchPath(LocalCustomSoundsDir); cf_SetSearchPath(LocalCustomSoundsDir);
if (Network_up) { if (Network_up) {
std::filesystem::create_directories(dir / "data", ec);
std::filesystem::create_directories(dir / "data" / "tables", ec); std::filesystem::create_directories(dir / "data" / "tables", ec);
std::filesystem::create_directories(dir / "data" / "graphics", ec); std::filesystem::create_directories(dir / "data" / "graphics", ec);
std::filesystem::create_directories(dir / "data" / "sounds", ec); std::filesystem::create_directories(dir / "data" / "sounds", ec);

View File

@@ -75,7 +75,7 @@ OSFArchive::~OSFArchive() {
} }
} }
bool OSFArchive::Open(const char *filename, bool write) { bool OSFArchive::Open(const std::filesystem::path &filename, bool write) {
char tag[8]; char tag[8];
ASSERT(!m_fp); ASSERT(!m_fp);
@@ -99,7 +99,7 @@ bool OSFArchive::Open(const char *filename, bool write) {
cfseek(m_fp, -OSF_HDR_SIZE, SEEK_END); cfseek(m_fp, -OSF_HDR_SIZE, SEEK_END);
cf_ReadBytes((uint8_t *)tag, strlen(OSF_TAG), m_fp); cf_ReadBytes((uint8_t *)tag, strlen(OSF_TAG), m_fp);
if (strcmp(OSF_TAG, tag) != 0) { if (strcmp(OSF_TAG, tag) != 0) {
LOG_WARNING.printf("Illegal OSF file format for %s.", filename); LOG_WARNING.printf("Illegal OSF file format for %s.", filename.u8string().c_str());
cfclose(m_fp); cfclose(m_fp);
m_fp = NULL; m_fp = NULL;
return false; return false;
@@ -122,7 +122,7 @@ bool OSFArchive::Open(const char *filename, bool write) {
// read in aux header based off of type. // read in aux header based off of type.
m_hdr.digi.measure = (uint32_t)cf_ReadInt(m_fp); m_hdr.digi.measure = (uint32_t)cf_ReadInt(m_fp);
} else { } else {
LOG_WARNING.printf("Unsupported OSF file type in %s!", filename); LOG_WARNING.printf("Unsupported OSF file type in %s!", filename.u8string().c_str());
cfclose(m_fp); cfclose(m_fp);
m_fp = NULL; m_fp = NULL;
return false; return false;
@@ -133,7 +133,7 @@ bool OSFArchive::Open(const char *filename, bool write) {
cfseek(m_fp, -OSF_HDR_TITLE_OFS, SEEK_END); cfseek(m_fp, -OSF_HDR_TITLE_OFS, SEEK_END);
if (!cf_ReadBytes((uint8_t *)m_name, OSF_HDR_TITLE_LEN, m_fp)) { if (!cf_ReadBytes((uint8_t *)m_name, OSF_HDR_TITLE_LEN, m_fp)) {
LOG_WARNING.printf("Stream title not found for %s.", filename); LOG_WARNING.printf("Stream title not found for %s.", filename.u8string().c_str());
cfclose(m_fp); cfclose(m_fp);
m_fp = NULL; m_fp = NULL;
return false; return false;

View File

@@ -335,14 +335,14 @@ void AudioStream::SetVolume(float vol) {
} }
float AudioStream::GetVolume() { return m_volume; } float AudioStream::GetVolume() { return m_volume; }
// flags specify what type of stream it is. // flags specify what type of stream it is.
bool AudioStream::Open(const char *filename, int open_flags) { bool AudioStream::Open(const std::filesystem::path &filename, int open_flags) {
// don't open a stream that's already open, or bogus filename // don't open a stream that's already open, or bogus filename
if (m_state != STRM_INVALID) { if (m_state != STRM_INVALID) {
AudioStream::Close(); AudioStream::Close();
// Int3(); // Int3();
// return false; // return false;
} }
if (m_archive.Opened() || !filename) if (m_archive.Opened() || filename.empty())
return false; return false;
if (!m_ll_sndsys) if (!m_ll_sndsys)
return false; return false;
@@ -920,7 +920,7 @@ void StreamVolume(float master_volume) {
User_audio_stream.SetVolume(Stream_volume * master_volume); User_audio_stream.SetVolume(Stream_volume * master_volume);
} }
// these functions are the 'simplified' stream interface from Jeff (most of this code is from Jeff) // these functions are the 'simplified' stream interface from Jeff (most of this code is from Jeff)
int StreamPlay(const char *filename, float volume, int flags) { int StreamPlay(const std::filesystem::path &filename, float volume, int flags) {
int retval = -1; int retval = -1;
try { try {
flags = 0; flags = 0;

View File

@@ -138,12 +138,14 @@
#ifndef __STREAMAUDIO_H_ #ifndef __STREAMAUDIO_H_
#define __STREAMAUDIO_H_ #define __STREAMAUDIO_H_
#include <filesystem>
#include "adecode.h" #include "adecode.h"
#include "ssl_lib.h" #include "ssl_lib.h"
void *AudioStreamCB(void *user_data, int handle, int *size); void *AudioStreamCB(void *user_data, int handle, int *size);
int ADecodeFileRead(void *data, void *buf, uint32_t qty); int ADecodeFileRead(void *data, void *buf, uint32_t qty);
int StreamPlay(const char *filename, float volume, int flags); int StreamPlay(const std::filesystem::path &filename, float volume, int flags);
void StreamStop(int handle); void StreamStop(int handle);
int StreamGetSoundHandle(int handle); int StreamGetSoundHandle(int handle);
@@ -191,7 +193,7 @@ class OSFArchive {
public: public:
OSFArchive(); OSFArchive();
~OSFArchive(); ~OSFArchive();
bool Open(const char *filename, bool write = false); bool Open(const std::filesystem::path &filename, bool write = false);
void Close(); void Close();
bool Opened() const { return m_fp ? true : false; }; bool Opened() const { return m_fp ? true : false; };
void Rewind(); void Rewind();
@@ -300,7 +302,7 @@ public:
~AudioStream(); ~AudioStream();
// simple operations // simple operations
// flags specify what type of stream you want. // flags specify what type of stream you want.
bool Open(const char *filename, int open_flags = 0); bool Open(const std::filesystem::path &filename, int open_flags = 0);
// specifies next stream to be opened when current one ends. // specifies next stream to be opened when current one ends.
void Close(); void Close();
// simple requests // simple requests
@@ -334,7 +336,7 @@ public:
return m_llshandle; return m_llshandle;
}; };
// these functions are the 'simplified' stream interface from Jeff (most of this code is from Jeff) // these functions are the 'simplified' stream interface from Jeff (most of this code is from Jeff)
friend int StreamPlay(const char *filename, float volume, int flags); friend int StreamPlay(const std::filesystem::path &filename, float volume, int flags);
friend void StreamStop(int handle); friend void StreamStop(int handle);
friend int StreamGetSoundHandle(int handle); friend int StreamGetSoundHandle(int handle);
}; };