123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704 |
- #include "util.h"
- #include "input.h"
- #include "global.h"
- #include "palcfg.h"
- #include <errno.h>
- #include "midi.h"
- #if SDL_VERSION_ATLEAST(2, 0, 0)
- #include "SDL_video.h"
- #include "SDL_messagebox.h"
- #endif
- void UTIL_MsgBox(char *string){
- extern SDL_Window *gpWindow;
- char buffer[300];
- SDL_MessageBoxButtonData buttons[] = { { 0, 0, "OK" } };
- SDL_MessageBoxData mbd = { SDL_MESSAGEBOX_WARNING, gpWindow, "Alert",buffer, 1, buttons, NULL };
- int btnid;
- sprintf(buffer, "%s\n", string);
- SDL_ShowMessageBox(&mbd, &btnid);
- }
- long
- flength(
- FILE *fp
- )
- {
- long old_pos = ftell(fp), length;
- if (old_pos == -1) return -1;
- if (fseek(fp, 0, SEEK_END) == -1) return -1;
- length = ftell(fp); fseek(fp, old_pos, SEEK_SET);
- return length;
- }
- void
- trim(
- char *str
- )
- {
- int pos = 0;
- char *dest = str;
-
-
-
- while (str[pos] <= ' ' && str[pos] > 0)
- pos++;
- while (str[pos])
- {
- *(dest++) = str[pos];
- pos++;
- }
- *(dest--) = '\0';
-
-
-
- while (dest >= str && *dest <= ' ' && *dest > 0)
- *(dest--) = '\0';
- }
- char *
- va(
- const char *format,
- ...
- )
- {
- static char string[1024];
- va_list argptr;
- va_start(argptr, format);
- vsnprintf(string, sizeof(string), format, argptr);
- va_end(argptr);
- return string;
- }
- static int glSeed = 0;
- static void
- lsrand(
- unsigned int iInitialSeed
- )
- {
-
-
-
- glSeed = 1664525L * iInitialSeed + 1013904223L;
- }
- static int
- lrand(
- void
- )
- {
- if (glSeed == 0)
- lsrand((unsigned int)time(NULL));
- glSeed = 1664525L * glSeed + 1013904223L;
- return ((glSeed >> 1) + 1073741824L);
- }
- int
- RandomLong(
- int from,
- int to
- )
- {
- if (to <= from)
- return from;
- return from + lrand() / (INT_MAX / (to - from + 1));
- }
- float
- RandomFloat(
- float from,
- float to
- )
- {
- if (to <= from)
- return from;
- return from + (float)lrand() / (INT_MAX / (to - from));
- }
- void
- UTIL_Delay(
- unsigned int ms
- )
- {
- unsigned int t = SDL_GetTicks() + ms;
- while (PAL_PollEvent(NULL));
- while (!SDL_TICKS_PASSED(SDL_GetTicks(), t))
- {
- SDL_Delay(1);
- while (PAL_PollEvent(NULL));
- }
- }
- void
- TerminateOnError(
- const char *fmt,
- ...
- )
- {
- va_list argptr;
- char string[256];
- extern VOID PAL_Shutdown(int);
-
- va_start(argptr, fmt);
- vsnprintf(string, sizeof(string), fmt, argptr);
- va_end(argptr);
- fprintf(stderr, "\nFATAL ERROR: %s\n", string);
- #if SDL_VERSION_ATLEAST(2, 0, 0)
- {
- extern SDL_Window *gpWindow;
- char buffer[300];
- SDL_MessageBoxButtonData buttons[2] = { { 0, 0, "Yes" },{ 0, 1, "No" } };
- SDL_MessageBoxData mbd = { SDL_MESSAGEBOX_ERROR, gpWindow, "FATAL ERROR", buffer, 2, buttons, NULL };
- int btnid;
- sprintf(buffer, "%sLaunch setting dialog on next start?\n", string);
- if (SDL_ShowMessageBox(&mbd, &btnid) == 0 && btnid == 0)
- {
- gConfig.fLaunchSetting = TRUE;
- PAL_SaveConfig();
- }
- PAL_Shutdown(255);
- }
- #else
- PAL_FATAL_OUTPUT(string);
- #endif
- #ifdef _DEBUG
- assert(!"TerminateOnError()");
- #endif
- PAL_Shutdown(255);
- }
- void *
- UTIL_malloc(
- size_t buffer_size
- )
- {
-
- void *buffer;
-
- if (buffer_size == 0)
- TerminateOnError("UTIL_malloc() called with invalid buffer size: %d\n", buffer_size);
- buffer = malloc(buffer_size);
-
- if (buffer == NULL)
- TerminateOnError("UTIL_malloc() failure for %d bytes (out of memory?)\n", buffer_size);
- return buffer;
- }
- void *
- UTIL_calloc(
- size_t n,
- size_t size
- )
- {
-
- void *buffer;
-
- if (n == 0 || size == 0)
- TerminateOnError ("UTIL_calloc() called with invalid parameters\n");
- buffer = calloc(n, size);
-
- if (buffer == NULL)
- TerminateOnError("UTIL_calloc() failure for %d bytes (out of memory?)\n", size * n);
- return buffer;
- }
- FILE *
- UTIL_OpenRequiredFile(
- LPCSTR lpszFileName
- )
- {
- return UTIL_OpenRequiredFileForMode(lpszFileName, "rb");
- }
- FILE *
- UTIL_OpenRequiredFileForMode(
- LPCSTR lpszFileName,
- LPCSTR szMode
- )
- {
- FILE *fp = UTIL_OpenFileForMode(lpszFileName, szMode);
- if (fp == NULL)
- {
- TerminateOnError("File open error(%d): %s!\n", errno, lpszFileName);
- }
- return fp;
- }
- FILE *
- UTIL_OpenFile(
- LPCSTR lpszFileName
- )
- {
- return UTIL_OpenFileForMode(lpszFileName, "rb");
- }
- FILE *
- UTIL_OpenFileForMode(
- LPCSTR lpszFileName,
- LPCSTR szMode
- )
- {
- FILE *fp;
- if (UTIL_IsAbsolutePath(lpszFileName))
- fp = fopen(lpszFileName, szMode);
- else
- fp = fopen(va("%s%s", gConfig.pszGamePath, lpszFileName), szMode);
- #if !defined(PAL_FILESYSTEM_IGNORE_CASE) || !PAL_FILESYSTEM_IGNORE_CASE
- if (fp == NULL)
- {
-
-
-
- struct dirent **list;
- int n = scandir(gConfig.pszGamePath, &list, 0, alphasort);
- while (n-- > 0)
- {
- if (!fp && strcasecmp(list[n]->d_name, lpszFileName) == 0)
- fp = fopen(va("%s%s", gConfig.pszGamePath, list[n]->d_name), szMode);
- free(list[n]);
- }
- free(list);
- }
- #endif
- return fp;
- }
- VOID
- UTIL_CloseFile(
- FILE *fp
- )
- {
- if (fp != NULL)
- {
- fclose(fp);
- }
- }
- #if !defined(PAL_HAS_PLATFORM_SPECIFIC_UTILS)
- BOOL
- UTIL_GetScreenSize(
- DWORD *pdwScreenWidth,
- DWORD *pdwScreenHeight
- )
- {
- return FALSE;
- }
- BOOL
- UTIL_IsAbsolutePath(
- LPCSTR lpszFileName
- )
- {
- return FALSE;
- }
- INT
- UTIL_Platform_Init(
- int argc,
- char* argv[]
- )
- {
- gConfig.fLaunchSetting = FALSE;
- return 0;
- }
- VOID
- UTIL_Platform_Quit(
- VOID
- )
- {
- }
- #endif
- #ifndef PAL_LOG_BUFFER_SIZE
- # define PAL_LOG_BUFFER_SIZE 4096
- #endif
- #define PAL_LOG_BUFFER_EXTRA_SIZE 32
- static LOGCALLBACK _log_callbacks[PAL_LOG_MAX_OUTPUTS];
- static LOGLEVEL _log_callback_levels[PAL_LOG_MAX_OUTPUTS];
- static char _log_buffer[PAL_LOG_BUFFER_SIZE + PAL_LOG_BUFFER_EXTRA_SIZE];
- static const char * const _loglevel_str[] = {
- "[VERBOSE]",
- " [DEBUG]",
- " [INFO]",
- "[WARNING]",
- " [ERROR]",
- " [FATAL]",
- };
- int
- UTIL_LogAddOutputCallback(
- LOGCALLBACK callback,
- LOGLEVEL loglevel
- )
- {
- if (!callback) return -1;
-
- for (int i = 0; i < PAL_LOG_MAX_OUTPUTS; i++)
- {
- if (!_log_callbacks[i])
- {
- _log_callbacks[i] = callback;
- }
- if (_log_callbacks[i] == callback)
- {
- _log_callback_levels[i] = loglevel;
- return i;
- }
- }
- return -1;
- }
- void
- UTIL_LogRemoveOutputCallback(
- int id
- )
- {
- if (id < 0 || id >= PAL_LOG_MAX_OUTPUTS) return;
- while (id < PAL_LOG_MAX_OUTPUTS - 1)
- {
- _log_callbacks[id] = _log_callbacks[id + 1];
- _log_callback_levels[id] = _log_callback_levels[id + 1];
- id++;
- }
- _log_callbacks[id] = NULL;
- _log_callback_levels[id] = LOGLEVEL_MIN;
- }
- void
- UTIL_LogOutput(
- LOGLEVEL level,
- const char *fmt,
- ...
- )
- {
- va_list va;
- time_t tv = time(NULL);
- struct tm *tmval = localtime(&tv);
- int id;
- if (level < gConfig.iLogLevel || !_log_callbacks[0]) return;
- if (level > LOGLEVEL_MAX) level = LOGLEVEL_MAX;
- snprintf(_log_buffer, PAL_LOG_BUFFER_EXTRA_SIZE,
- "%04d-%02d-%02d %02d:%02d:%02d %s: ",
- tmval->tm_year + 1900, tmval->tm_mon, tmval->tm_mday,
- tmval->tm_hour, tmval->tm_min, tmval->tm_sec,
- _loglevel_str[level]);
- va_start(va, fmt);
- vsnprintf(_log_buffer + PAL_LOG_BUFFER_EXTRA_SIZE - 1, PAL_LOG_BUFFER_SIZE + 1, fmt, va);
- va_end(va);
- for(id = 0; id < PAL_LOG_MAX_OUTPUTS && _log_callbacks[id]; id++)
- {
- if (level >= _log_callback_levels[id])
- {
- _log_callbacks[id](level, _log_buffer, _log_buffer + PAL_LOG_BUFFER_EXTRA_SIZE - 1);
- }
- }
- }
- void
- UTIL_LogSetLevel(
- LOGLEVEL minlevel
- )
- {
- if (minlevel < LOGLEVEL_MIN)
- gConfig.iLogLevel = LOGLEVEL_MIN;
- else if (minlevel > LOGLEVEL_MAX)
- gConfig.iLogLevel = LOGLEVEL_MAX;
- else
- gConfig.iLogLevel = minlevel;
- }
- void
- UTIL_LogToFile(
- LOGLEVEL _,
- const char *string,
- const char *__
- )
- {
- FILE *fp = fopen(gConfig.pszLogFile, "a");
- if (fp)
- {
- fputs(string, fp);
- fclose(fp);
- }
- }
|