Hello, Per, RodZilla, et al! Well, after several days of tinkering, here's the results of my efforts: the soundtrack code converted to PHYSFS_* calls! Some notes:
1) in src/main.c:initialize_PhysicsFS, calls were being made to PHYSFS_getBaseDir(), On my box (at least) this was consistently returning /usr/local/bin, and according to the docs, that's what it should have been returning, but the code looks and acts like it wants the base dir of the files: /usr/local/share/warzone. I've subbed in DEFAULT_DATA_DIR where I thought it was appropriate. If I was wrong in these assumptions, let me know. 2) The patch modifies the Makefile.am in the lib/sound directory, as I create two new files: physfs_support.[ch]. One of the main reasons for this is that both the MP3 and the Vorbis routines need a FILE * handle to work with the soundfile, so I wrote a little routine that checks the PHYSFS path for a file, and if it exists, then figures out the real path, and does a standard fopen(). However, when I tried to re-run automake, it complained mightily, so I ended up patching the Makefile manually to include the new files. Maybe they should be separate files, maybe the routines should just be >>'d onto the end of one of the other files: time will tell. 3) I've tested this on my Ubuntu boxes, and it works like a champ. On my Debian Sarge box, the PHYSFS_exists() call looking for "music.wpl" fails! I'm interested in seeing if this works on your boxes. Maybe that mission 3 thing is an issue with a flakey libphysfs on Sarge! I'll slap together a sid box in the next day or so and try it there. Cheers! Scott -- (o_ Scott L. Balneaves | "You are the Universe's only chance to appreciate //\ Systems Department | its own beauty." V_/_ Legal Aid Manitoba | -- MSG, POE news forums
--- warzone.clean/lib/sound/openal_track.c 2006-01-29 21:22:49.000000000
-0600
+++ warzone/lib/sound/openal_track.c 2006-02-04 22:53:21.000000000 -0600
@@ -6,6 +6,7 @@
// this has to be first
#include "frame.h"
+#include "physfs_support.h"
#include <AL/al.h>
#include <AL/alc.h>
@@ -350,26 +351,15 @@
//
BOOL sound_ReadTrackFromFile( TRACK *psTrack, signed char szFileName[] )
{
- FILE* f = fopen(szFileName, "r");
- static char* buffer = NULL;
- static unsigned int buffer_size = 0;
- unsigned int size;
-
- if (f == NULL) return FALSE;
+ char *buffer;
+ long int size;
- fseek(f, 0, SEEK_END);
- size = ftell(f);
- fseek(f, 0, SEEK_SET);
-
- if (size > buffer_size) {
- if (buffer != NULL) free(buffer);
- buffer_size = size*2;
- buffer = (char*)malloc(buffer_size);
- }
+ size = physfs_loadBufferFromFile(&buffer, szFileName);
- fread(buffer, 1, size, f);
-
- return sound_ReadTrackFromBuffer(psTrack, buffer, size);
+ if (size > 0)
+ return sound_ReadTrackFromBuffer(psTrack, buffer, size);
+ else
+ return FALSE;
}
//*
--- warzone.clean/lib/sound/playlist.c 2006-01-29 21:22:49.000000000 -0600
+++ warzone/lib/sound/playlist.c 2006-02-04 22:52:38.000000000 -0600
@@ -1,9 +1,9 @@
#include <stdio.h>
-#include "frame.h"
-#define BUFFER_SIZE 2048
-static char buffer[BUFFER_SIZE];
+#include "frame.h"
+#include "physfs_support.h"
+#include <physfs.h>
#define NB_TRACKS 3
@@ -31,61 +31,84 @@
}
}
-char PlayList_Read(const char* path) {
- FILE* f;
- char* path_to_music = NULL;
-
- sprintf(buffer, "%s/music.wpl", path);
+char PlayList_Read() {
+ int size;
+ char* music_wpl = NULL;
+ char* next_line;
+ char* line;
+
+ size = physfs_loadBufferFromFile(&music_wpl, "music.wpl");
+
+ if (size < 0)
+ return 1;
+
+ // We want to realloc the size to be one larger, to include
+ // a terminating null.
+
+ music_wpl = realloc(music_wpl, size + 1);
- f = fopen(buffer, "r");
-
- if (f == NULL) {
+ if (!music_wpl)
return 1;
- }
- while (!feof(f)) {
+ music_wpl[size] = '\0'; // Null at the end
+
+ line = next_line = music_wpl;
+
+ while (*line != '\0') {
char* filename;
- fgets(buffer, BUFFER_SIZE, f);
-
- if (strncmp(buffer, "[game]", 6) == 0) {
+ /*
+ * Make sure next line pointer is either pointing at the next
+ * line, or at the null at the end.
+ */
+
+ for (; *next_line != '\0'; next_line++)
+ if (*next_line == '\n')
+ break;
+ next_line++;
+
+ if (*line == '\n') { // skip blank lines
+ line = next_line;
+ continue;
+ }
+
+ if (strncmp(line, "[game]", 6) == 0) {
current_track = 1;
- free(path_to_music);
- path_to_music = NULL;
CURRENT_TRACK.shuffle = FALSE;
- } else if (strncmp(buffer, "[menu]", 6) == 0) {
+ } else if (strncmp(line, "[menu]", 6) == 0) {
current_track = 2;
- free(path_to_music);
- path_to_music = NULL;
CURRENT_TRACK.shuffle = FALSE;
- } else if (strncmp(buffer, "path=", 5) == 0) {
- free(path_to_music);
- path_to_music = strtok(buffer+5, "\n");
+ } else if (strncmp(line, "path=", 5) == 0) {
+ char* path_to_music;
+
+ path_to_music = strtok(line+5, "\n");
if (strcmp(path_to_music, ".") == 0) {
- path_to_music = strdup(path);
+ // Don't do anything, it's already in the
+ // search path
+ } else if (*path_to_music == '/') {
+ // absolute path, add to PHYSFS search path
+ PHYSFS_addToSearchPath(path_to_music, 1);
} else {
- path_to_music = strdup(path_to_music);
+ char relative_path[MAX_PATH];
+ // relative path, add to PHYSFS search path
+ strcpy(relative_path, DEFAULT_DATA_DIR);
+ strcat(relative_path, "/");
+ strcat(relative_path, path_to_music);
+ PHYSFS_addToSearchPath(relative_path, 1);
}
- //printf(" path = %s\n", path_to_music);
- } else if (strncmp(buffer, "shuffle=", 8) == 0) {
- if (strcmp(strtok(buffer+8, "\n"), "yes") == 0) {
+ printf(" path = %s\n", path_to_music);
+ } else if (strncmp(line, "shuffle=", 8) == 0) {
+ if (strcmp(strtok(line+8, "\n"), "yes") == 0) {
CURRENT_TRACK.shuffle = TRUE;
}
- //printf(" shuffle = yes\n");
- } else if ( buffer[0] != '\0'
- && (filename = strtok(buffer, "\n")) != NULL
+ printf(" shuffle = yes\n");
+ } else if ( *line != '\0'
+ && (filename = strtok(line, "\n")) != NULL
&& strlen(filename) != 0) {
char* filepath;
- if (path_to_music == NULL) {
- filepath = malloc(strlen(filename)+1);
- sprintf(filepath, "%s", filename);
- } else {
- filepath = malloc( strlen(filename)
- + strlen(path_to_music)+2);
- sprintf(filepath, "%s/%s", path_to_music,
filename);
- }
- //printf(" adding song %s\n", filepath);
+ filepath = strdup(filename);
+ printf(" adding song %s\n", filepath);
if (CURRENT_TRACK.nb_songs == CURRENT_TRACK.list_size) {
CURRENT_TRACK.list_size <<= 1;
@@ -95,10 +118,11 @@
CURRENT_TRACK.songs[CURRENT_TRACK.nb_songs++] =
filepath;
}
- }
- free(path_to_music);
+ line = next_line;
+ }
+ free(music_wpl);
return 0;
}
--- warzone.clean/lib/sound/cdaudio.c 2006-01-29 21:22:49.000000000 -0600
+++ warzone/lib/sound/cdaudio.c 2006-02-04 22:44:58.000000000 -0600
@@ -6,6 +6,7 @@
#else
+#include "physfs_support.h"
#include <AL/al.h>
#ifndef WZ_NOMP3
@@ -81,7 +82,7 @@
void PlayList_Init();
-char PlayList_Read(const char* path);
+char PlayList_Read();
void PlayList_SetTrack(unsigned int t);
char* PlayList_CurrentSong();
char* PlayList_NextSong();
@@ -128,9 +129,7 @@
alSourcei (music_source, AL_SOURCE_RELATIVE, AL_TRUE);
PlayList_Init();
- if ( ( user_musicdir == NULL
- || PlayList_Read(user_musicdir))
- && PlayList_Read("music")) {
+ if (PlayList_Read()) {
return FALSE;
}
@@ -268,7 +267,7 @@
#ifndef WZ_NOMP3
if (strncasecmp(filename+strlen(filename)-4, ".mp3", 4) == 0)
{
- music_file = fopen(filename, "rb");
+ music_file = physfs_getFileOpen(filename, "rb");
if (music_file == NULL) {
return FALSE;
@@ -310,7 +309,7 @@
#ifndef WZ_NOOGG
if (strncasecmp(filename+strlen(filename)-4, ".ogg", 4) == 0)
{
- music_file = fopen(filename, "rb");
+ music_file = physfs_getFileOpen(filename, "rb");
if (music_file == NULL) {
return FALSE;
@@ -377,6 +376,7 @@
#endif
#ifndef WZ_NOOGG
+
if (music_file_format == WZ_OGG) {
result = ov_read(&ogg_stream, music_data+size,
BUFFER_SIZE-size, 0, 2, 1, §ion);
--- warzone.clean/lib/sound/physfs_support.c 1969-12-31 18:00:00.000000000
-0600
+++ warzone/lib/sound/physfs_support.c 2006-02-04 22:51:58.000000000 -0600
@@ -0,0 +1,68 @@
+//*
+//
+// PhysicsFS support routines.
+//*
+//
+
+// this has to be first
+#include "frame.h"
+
+#include <physfs.h>
+
+//*
+//
=======================================================================================================================
+//
=======================================================================================================================
+//
+long int physfs_loadBufferFromFile( char **buffer, signed char szFileName[] )
+{
+ PHYSFS_file *f;
+ PHYSFS_sint64 size;
+
+ if (!PHYSFS_exists(szFileName))
+ return -1;
+
+ f = PHYSFS_openRead(szFileName);
+
+ size = PHYSFS_fileLength(f);
+
+ *buffer = (char*)malloc(size);
+
+ if (!(*buffer)) {
+ PHYSFS_close(f);
+ return -1;
+ }
+
+ PHYSFS_read(f, *buffer, 1, size);
+
+ PHYSFS_close(f); // No close here before
+
+ return size;
+}
+
+/*
+ * Ugh. This is a gross, gross hack, but workable. Some routines actually
+ * need to deal with a file as a FILE *, but PhysicsFS basically makes this
+ * somewhat impossible. What would be a nice addition to PhysicsFS would be
+ * a routine that returns a FILE * based on a physicsfs filesystem search, but
+ * that's what this routine does in the short term.
+ */
+
+FILE *physfs_getFileOpen(char* physfs_file, char *mode)
+{
+ FILE *f;
+ char fullpath[MAX_PATH];
+ char *real_dir;
+
+ if (!PHYSFS_exists(physfs_file))
+ return NULL; /* Not in the search path */
+
+ real_dir = strdup(PHYSFS_getRealDir(physfs_file));
+
+ sprintf(fullpath, "%s/%s", real_dir, physfs_file);
+
+ free(real_dir);
+
+ f = fopen(fullpath, mode);
+
+ return f;
+}
--- warzone.clean/lib/sound/physfs_support.h 1969-12-31 18:00:00.000000000
-0600
+++ warzone/lib/sound/physfs_support.h 2006-02-04 22:52:14.000000000 -0600
@@ -0,0 +1,8 @@
+//*
+//
+// PhysicsFS support routines.
+//*
+//
+
+long int physfs_loadBufferFromFile( char **buffer, signed char szFileName[] );
+FILE *physfs_getFileOpen(char* physfs_file, char *mode);
--- warzone.clean/lib/sound/Makefile.am 2006-01-29 21:22:49.000000000 -0600
+++ warzone/lib/sound/Makefile.am 2006-02-04 13:57:48.000000000 -0600
@@ -6,6 +6,7 @@
mixer_stub.c \
openal_track.c \
playlist.c \
+ physfs_support.c \
track.c \
aud.h \
audio.h \
--- warzone.clean/src/main.c 2006-01-29 21:23:15.000000000 -0600
+++ warzone/src/main.c 2006-02-04 20:16:06.000000000 -0600
@@ -163,6 +163,7 @@
PHYSFS_Version linked;
char **i;
char overridepath[MAX_PATH], writepath[MAX_PATH], mappath[MAX_PATH];
+ char musicpath[MAX_PATH], usermusicpath[MAX_PATH];
#ifdef WIN32
const char *writedir = "warzone-2.0";
#else
@@ -192,13 +193,18 @@
}
PHYSFS_addToSearchPath(writepath, 0); /* add to search path */
+
find_data_dir();
debug(LOG_WZ, "Data dir set to \"%s\".", datadir);
- snprintf(overridepath, sizeof(overridepath), "%smods",
- PHYSFS_getBaseDir());
- strcpy(mappath, PHYSFS_getBaseDir());
- strcat(mappath, "maps");
+ strcpy(overridepath, DEFAULT_DATA_DIR);
+ strcat(overridepath, "/mods");
+ strcpy(mappath, DEFAULT_DATA_DIR);
+ strcat(mappath, "/maps");
+ strcpy(musicpath, DEFAULT_DATA_DIR);
+ strcat(musicpath, "/music");
+ strcpy(usermusicpath, writepath);
+ strcat(usermusicpath, "/music");
/* The 1 below means append to search path, while 0 means prepend. */
if (!PHYSFS_addToSearchPath(overridepath, 0)) {
@@ -209,6 +215,14 @@
debug(LOG_WZ, "Error adding map path %s: %s", mappath,
PHYSFS_getLastError());
}
+ if (!PHYSFS_addToSearchPath(usermusicpath, 1)) {
+ debug(LOG_WZ, "Error adding music path %s: %s", usermusicpath,
+ PHYSFS_getLastError());
+ }
+ if (!PHYSFS_addToSearchPath(musicpath, 1)) {
+ debug(LOG_WZ, "Error adding music path %s: %s", musicpath,
+ PHYSFS_getLastError());
+ }
/** Debugging and sanity checks **/
signature.asc
Description: Digital signature
