2 #if !defined (SDL_MIX_MAXVOLUME)
3 #define SDL_MIX_MAXVOLUME 128
7 #include <SDL/SDL_thread.h>
8 #include <SDL/SDL_mixer.h>
18 #endif //tells VCC not to generate min/max macros
24 #define sleep( sec ) Sleep( sec*1000 );
36 #include <sys/types.h>
41 #include <sys/types.h>
44 #include <sys/types.h>
51 #ifndef MIX_CHANNEL_POST
52 #define MIX_CHANNEL_POST -2
75 if ( (arg !=
'\r') && (arg !=
'\n') && (arg !=
'\0') )
77 }
while ( (arg !=
'\n') && (arg !=
'\0') );
88 #if defined (_WIN32) && defined (_WINDOWS)
89 FILE *mystdout = stdout;
90 #define STD_ERR mystdout
91 #define STD_OUT mystdout
93 #define STD_ERR stderr
94 #define STD_OUT stdout
103 #include <SDL/SDL_mutex.h>
108 char *songNames[5] = {0, 0, 0, 0, 0};
109 unsigned int counter = 0;
110 SDL_mutex *RestartSong = NULL;
120 std::string
concat(
const std::vector< std::string >& );
140 static std::vector< std::string >paths;
142 char mycurpath[8192];
143 getcwd( mycurpath, 8191 );
144 mycurpath[8191] =
'\0';
145 paths.push_back( mycurpath );
148 struct passwd *pwent;
149 pwent = getpwuid( getuid() );
150 chdir( pwent->pw_dir );
154 }
else if ( !paths.empty() ) {
155 chdir( paths.back().c_str() );
163 std::string::iterator
i = s.begin();
164 unsigned int counter = 0;
165 for (; i != s.end(); ++i, ++counter)
166 if ( (*i >=
'A' && *i <=
'Z')
167 || (*i >=
'a' && *i <=
'z')
168 || (*i >=
'0' && *i <=
'9') || ( (*i) ==
'.' && counter == s.length()-4 ) )
175 for (
unsigned int i = 0;
i < files.size(); ++
i) {
177 as.open( files[
i].c_str(), std::ios::binary );
178 if ( !as.is_open() ) {
180 as.open( files[
i].c_str(), std::ios::binary );
183 if ( !as.is_open() ) {
185 as.open( files[
i].c_str(), std::ios::binary );
188 if ( as.is_open() ) {
196 std::string
concat(
const std::vector< std::string > &files )
207 for (
unsigned int i = 0;
i < files.size(); ++
i)
211 FILE *checker =
fopen( ret.c_str(),
"rb" );
216 std::ofstream o( ret.c_str(), std::ios::binary );
217 if ( !o.is_open() ) {
218 ret =
"music/"+alphan;
219 checker =
fopen( ret.c_str(),
"rb" );
224 std::ofstream o1( ret.c_str(), std::ios::binary );
225 if ( o1.is_open() ) {
230 ret = tempnam(
"c:\tmp", alphan.c_str() );
232 checker =
fopen( ret.c_str(),
"rb" );
237 std::ofstream of( ret.c_str(), std::ios::binary );
239 if ( of.is_open() ) {
251 std::vector< std::string >
split( std::string tmpstr, std::string splitter )
253 std::string::size_type where;
254 std::vector< std::string >ret;
255 while ( ( where = tmpstr.find( splitter ) ) != std::string::npos ) {
256 ret.push_back( tmpstr.substr( 0, where ) );
257 tmpstr = tmpstr.substr( where+1 );
259 if ( tmpstr.length() )
260 ret.push_back( tmpstr );
287 static int numloops( std::string
file )
290 std::string::size_type where;
291 if ( ( where = file.find_last_of(
"." ) ) != std::string::npos ) {
292 file = file.substr( 0, where );
293 if ( ( where = file.find_last_of(
"-_" ) ) != std::string::npos ) {
294 file = file.substr( where+1 );
295 if ( file.length() ) {
299 return atoi( file.c_str() );
309 std::vector< std::string >files =
split( file,
"|" );
310 if (files.size() > 1) {
311 std::string tmp =
concat( files );
317 if ( !file.empty() ) {
318 music = Mix_LoadMUS( file.c_str() );
321 music = Mix_LoadMUS( file.c_str() );
325 music = Mix_LoadMUS( file.c_str() );
333 if ( oldmusic && Mix_PlayingMusic() ) {
336 if (music == NULL)
return NULL;
340 while ( Mix_PlayingMusic() )
343 Mix_FreeMusic( oldmusic );
352 int loops = numloops( file );
355 if (Mix_PlayMusic( music, loops ) == -1) {
359 fprintf(
STD_OUT,
"Playing %s with %d loops\n", file.c_str(), loops );
363 Mix_VolumeMusic( newvolume );
375 SDL_mutexP( RestartSong );
377 char *newname = NULL;
379 newname = songNames[tmp];
384 SDL_mutexV( RestartSong );
386 SDL_mutexV( RestartSong );
398 #if defined (_WIN32) && defined (_WINDOWS)
399 typedef char FileNameCharType[65535];
400 void getPipes( LPSTR cmd,
int(*
const pipes)[2] )
402 ptrdiff_t len = strlen( cmd );
404 for (; i >= 0 && (isspace( cmd[i] ) || cmd[i] >=
'0' && cmd[i] <=
'9'); --i) {}
407 sscanf( cmd+i,
"%d %d", &(*pipes)[0], &(*pipes)[1] );
409 int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine,
int nShowCmd )
411 FileNameCharType argvc;
412 FileNameCharType *argv = &argvc;
413 GetModuleFileName( NULL, argvc, 65534 );
414 mystdout =
fopen(
"soundserver_log.txt",
"w" );
415 int pipes[2] = {-1, -1};
416 getPipes( lpCmdLine, &pipes );
418 setbuf( mystdout, NULL );
422 int main(
int argc,
char **argv )
424 int pipes[2] = {-1, -1};
425 if (argc > 1) sscanf( argv[1],
"%d", &pipes[0] );
426 if (argc > 2) sscanf( argv[2],
"%d", &pipes[1] );
429 char origpath[65535];
430 getcwd( origpath, 65534 );
435 for (i = strlen( argv[0] ); argv[0][i] !=
'\\' && argv[0][i] !=
'/' && i >= 0; i--) {}
440 if (
stat(
"vegastrike.config", &st ) != 0)
443 getcwd( origpath, 65534 );
447 FILE *version =
fopen(
"Version.txt",
"r" );
449 std::string hsd =
"";
451 while ( ( c = fgetc( version ) ) != EOF ) {
452 if ( ( (c) ==
' ' ) || ( (c) ==
'\t' ) || ( (c) ==
'\n' ) || ( (c) ==
'\r' ) || ( (c) ==
'\0' ) )
457 if ( hsd.length() ) {
459 fprintf(
STD_OUT,
"Using %s as the home directory\n", hsd.c_str() );
462 int audio_rate, audio_channels,
465 audio_buffers = 4096;
469 if (SDL_Init( SDL_INIT_AUDIO|SDL_INIT_TIMER ) < 0)
471 signal( SIGSEGV, SIG_DFL );
473 RestartSong = SDL_CreateMutex();
486 fnet = (argc == 3 && pipes[1] == -1);
495 if (Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, audio_buffers ) < 0)
498 Mix_QuerySpec( &audio_rate, &audio_format, &audio_channels );
499 bits = audio_format&0xFF;
501 fprintf(
STD_OUT,
"Opened audio at %d Hz %d bit %s, %d bytes audio buffer\n", audio_rate,
502 bits, audio_channels > 1 ?
"stereo" :
"mono", audio_buffers );
505 if (pipes[1] == -1 || pipes[0] == -1) {
510 mysocket = open( argv[1], O_RDONLY|O_CREAT, 0xffffffff );
519 for (
int i = 0; i < 10 &&
mysocket == -1; i++) {
520 int port = (pipes[0] != -1 ? pipes[0] : 4364);
521 if (port == 0) port = 4364;
522 if (pipes[1] != -1 && pipes[0] != -1) {
534 char ministr[2] = {
'\0',
'\0'};
551 || ( !Mix_PlayingMusic() )
555 std::vector< std::string >
names =
split( str,
"&" );
556 char *tmpstrings[5] = {NULL, NULL, NULL, NULL, NULL};
557 for (
unsigned int t = 0; t < 5 && t+1 < names.size(); ++t)
558 tmpstrings[t] = strdup( names[t+1].c_str() );
559 SDL_mutexP( RestartSong );
560 memcpy( songNames, tmpstrings,
sizeof (
char*)*5 );
561 if (names.size() > 0) str = names[0];
566 SDL_mutexV( RestartSong );
572 char mycurpath[8192];
573 getcwd( mycurpath, 8191 );
574 mycurpath[8191] =
'\0';
575 fprintf(
STD_OUT,
"\n[UNABLE TO PLAY %s IN %s WITH %d FADEIN AND %d FADEOUT]\n",
587 fadein = atoi( str.c_str() );
623 Mix_VolumeMusic( newvolume );
629 fprintf(
STD_OUT,
"\n[STOPPING ALL MUSIC - %d FADEOUT]\n", fadeout );
636 const char *shname =
"UNRECOGNIZED";
654 shname =
"LINEAR EASED";
665 shname =
"EXPONENTIAL";
670 shname =
"EXPONENTIAL EASED";
707 SDL_DestroyMutex( RestartSong );