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 <cstring>
#include <filesystem>
#include "debug.h"
#include "pserror.h"
#include "audiotaunts.h"
#include "cfile.h"
@@ -92,7 +92,7 @@ void taunt_SetDelayTime(float t) { Audio_taunt_delay_time = t; }
// taunt_PlayTauntFile
//
// 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)
return false;
@@ -113,7 +113,7 @@ bool taunt_PlayPlayerTaunt(int pnum, int index) {
}
if ((NetPlayers[pnum].flags & NPF_CONNECTED) && (NetPlayers[pnum].sequence == NETSEQ_PLAYING)) {
char fullpath[_MAX_PATH];
std::filesystem::path fullpath;
char *file;
switch (index) {
case 0:
@@ -130,10 +130,10 @@ bool taunt_PlayPlayerTaunt(int pnum, int index) {
break;
}
ddio_MakePath(fullpath, LocalCustomSoundsDir, file, NULL);
fullpath = LocalCustomSoundsDir / file;
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;
}

View File

@@ -45,6 +45,8 @@
#ifndef __AUDIO_TAUNT_H_
#define __AUDIO_TAUNT_H_
#include <filesystem>
extern bool Audio_taunts_enabled;
// Error codes:
@@ -82,7 +84,7 @@ bool taunt_ImportWave(const char *wave_filename, const char *outputfilename);
// taunt_PlayTauntFile
//
// 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
//

View File

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

View File

@@ -2525,7 +2525,7 @@ bool PltSelectShip(pilot *Pilot) {
FreeVClip(handle);
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) {
// error saving
@@ -2580,7 +2580,7 @@ bool PltSelectShip(pilot *Pilot) {
if (DoPathFileDialog(false, path, TXT_CHOOSE, {"*.wav"}, PFDF_FILEMUSTEXIST)) {
std::filesystem::path dpath;
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
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 LocalArtDir;
extern char LocalCustomGraphicsDir[];
extern char LocalCustomSoundsDir[];
extern std::filesystem::path LocalCustomGraphicsDir;
extern std::filesystem::path LocalCustomSoundsDir;
extern char TempTableFilename[];
extern char TempTableLockFilename[];

View File

@@ -489,8 +489,8 @@ std::filesystem::path LocalMusicDir, NetMusicDir;
std::filesystem::path LocalVoiceDir, NetVoiceDir;
std::filesystem::path NetTableDir, LocalTableDir;
char LocalD3Dir[TABLE_NAME_LEN], NetD3Dir[TABLE_NAME_LEN];
char LocalCustomGraphicsDir[TABLE_NAME_LEN];
char LocalCustomSoundsDir[TABLE_NAME_LEN];
std::filesystem::path LocalCustomGraphicsDir;
std::filesystem::path LocalCustomSoundsDir;
std::filesystem::path LockerFile;
std::filesystem::path VersionFile;
char TableUser[TABLE_NAME_LEN];
@@ -601,17 +601,14 @@ int mng_InitTableFiles() {
Network_up = 0;
}
// Do locals
mng_InitLocalTables();
mng_InitLocalDirectories();
mng_CheckToCreateLocalTables();
if (Network_up == 0) {
mng_InitLocalTables();
mng_InitLocalDirectories();
mng_CheckToCreateLocalTables();
mng_InitTrackLocks();
} else {
// Do locals
mng_InitLocalTables();
mng_InitLocalDirectories();
mng_CheckToCreateLocalTables();
// Do network
mng_InitNetTables();
mng_InitNetDirectories();
@@ -689,8 +686,8 @@ int mng_InitLocalTables() {
LocalManageGraphicsDir = localdir / "data" / "graphics";
LocalModelsDir = localdir / "data" / "models";
LocalSoundsDir = localdir / "data" / "sounds";
ddio_MakePath(LocalCustomSoundsDir, LocalD3Dir, "custom", "sounds", NULL);
ddio_MakePath(LocalCustomGraphicsDir, LocalD3Dir, "custom", "graphics", NULL);
LocalCustomSoundsDir = cf_GetWritableBaseDirectory() / "custom" / "sounds";
LocalCustomGraphicsDir = cf_GetWritableBaseDirectory() / "custom" / "graphics";
LocalRoomsDir = localdir / "data" / "rooms";
LocalBriefingDir = localdir / "data" / "briefings";
ddio_MakePath(LocalScriptDir, LocalD3Dir, "data", "scripts", NULL);
@@ -815,18 +812,19 @@ void mng_CheckToCreateLocalTables() {
}
// Creates directories if needed
void mng_InitLocalDirectories() {
std::filesystem::path dir = LocalD3Dir;
std::filesystem::path dir = cf_GetWritableBaseDirectory();
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" / "sounds", ec);
std::filesystem::create_directories(dir / "custom" / "settings", ec);
std::filesystem::create_directories(dir / "savegame", ec);
cf_SetSearchPath(LocalCustomGraphicsDir);
cf_SetSearchPath(LocalCustomSoundsDir);
if (Network_up) {
std::filesystem::create_directories(dir / "data", ec);
std::filesystem::create_directories(dir / "data" / "tables", ec);
std::filesystem::create_directories(dir / "data" / "graphics", 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];
ASSERT(!m_fp);
@@ -99,7 +99,7 @@ bool OSFArchive::Open(const char *filename, bool write) {
cfseek(m_fp, -OSF_HDR_SIZE, SEEK_END);
cf_ReadBytes((uint8_t *)tag, strlen(OSF_TAG), m_fp);
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);
m_fp = NULL;
return false;
@@ -122,7 +122,7 @@ bool OSFArchive::Open(const char *filename, bool write) {
// read in aux header based off of type.
m_hdr.digi.measure = (uint32_t)cf_ReadInt(m_fp);
} 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);
m_fp = NULL;
return false;
@@ -133,7 +133,7 @@ bool OSFArchive::Open(const char *filename, bool write) {
cfseek(m_fp, -OSF_HDR_TITLE_OFS, SEEK_END);
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);
m_fp = NULL;
return false;

View File

@@ -335,14 +335,14 @@ void AudioStream::SetVolume(float vol) {
}
float AudioStream::GetVolume() { return m_volume; }
// 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
if (m_state != STRM_INVALID) {
AudioStream::Close();
// Int3();
// return false;
}
if (m_archive.Opened() || !filename)
if (m_archive.Opened() || filename.empty())
return false;
if (!m_ll_sndsys)
return false;
@@ -920,7 +920,7 @@ void StreamVolume(float 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)
int StreamPlay(const char *filename, float volume, int flags) {
int StreamPlay(const std::filesystem::path &filename, float volume, int flags) {
int retval = -1;
try {
flags = 0;

View File

@@ -138,12 +138,14 @@
#ifndef __STREAMAUDIO_H_
#define __STREAMAUDIO_H_
#include <filesystem>
#include "adecode.h"
#include "ssl_lib.h"
void *AudioStreamCB(void *user_data, int handle, int *size);
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);
int StreamGetSoundHandle(int handle);
@@ -191,7 +193,7 @@ class OSFArchive {
public:
OSFArchive();
~OSFArchive();
bool Open(const char *filename, bool write = false);
bool Open(const std::filesystem::path &filename, bool write = false);
void Close();
bool Opened() const { return m_fp ? true : false; };
void Rewind();
@@ -300,7 +302,7 @@ public:
~AudioStream();
// simple operations
// 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.
void Close();
// simple requests
@@ -334,7 +336,7 @@ public:
return m_llshandle;
};
// 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 int StreamGetSoundHandle(int handle);
};