palcfg.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include "global.h"
  2. #include "resampler.h"
  3. #include "palcfg.h"
  4. #include <stdint.h>
  5. static const ConfigItem gConfigItems[PALCFG_ALL_MAX] = {
  6. { PALCFG_DOS, PALCFG_BOOLEAN, "DOS", 3, TRUE, FALSE, TRUE }, // Default for DOS
  7. { PALCFG_FULLSCREEN, PALCFG_BOOLEAN, "FULLSCREEN", 10, FALSE, FALSE, TRUE },
  8. { PALCFG_KEEPASPECTRATIO, PALCFG_BOOLEAN, "KEEPASPECTRATIO", 15, TRUE, FALSE, TRUE },
  9. { PALCFG_LAUNCHSETTING, PALCFG_BOOLEAN, "LAUNCHSETTING", 13, TRUE, FALSE, TRUE },
  10. { PALCFG_STEREO, PALCFG_BOOLEAN, "STEREO", 6, TRUE, FALSE, TRUE }, // Default for stereo audio
  11. { PALCFG_USEEMBEDDEDFONTS, PALCFG_BOOLEAN, "USEEMBEDDEDFONTS", 16, TRUE, FALSE, TRUE }, // Default for using embedded fonts in DOS version
  12. { PALCFG_USESURROUNDOPL, PALCFG_BOOLEAN, "USESURROUNDOPL", 14, TRUE, FALSE, TRUE }, // Default for using surround opl
  13. { PALCFG_USETOUCHOVERLAY, PALCFG_BOOLEAN, "USETOUCHOVERLAY", 15, TRUE, FALSE, TRUE },
  14. { PALCFG_SURROUNDOPLOFFSET, PALCFG_INTEGER, "SURROUNDOPLOFFSET", 17, 384, INT32_MIN, INT32_MAX },
  15. { PALCFG_AUDIOBUFFERSIZE, PALCFG_UNSIGNED, "AUDIOBUFFERSIZE", 15, PAL_AUDIO_DEFAULT_BUFFER_SIZE, 2, 32768 },
  16. { PALCFG_CODEPAGE, PALCFG_UNSIGNED, "CODEPAGE", 8, CP_BIG5, CP_BIG5, CP_MAX - 1 }, // Default for BIG5
  17. { PALCFG_OPLSAMPLERATE, PALCFG_UNSIGNED, "OPLSAMPLERATE", 13, 49716, 0, UINT32_MAX },
  18. { PALCFG_RESAMPLEQUALITY, PALCFG_UNSIGNED, "RESAMPLEQUALITY", 15, RESAMPLER_QUALITY_MAX, RESAMPLER_QUALITY_MIN, RESAMPLER_QUALITY_MAX }, // Default for best quality
  19. { PALCFG_SAMPLERATE, PALCFG_UNSIGNED, "SAMPLERATE", 10, 44100, 0, PAL_MAX_SAMPLERATE },
  20. { PALCFG_VOLUME, PALCFG_UNSIGNED, "VOLUME", 6, 100, 0, 100 }, // Default for maximum volume
  21. { PALCFG_WINDOWHEIGHT, PALCFG_UNSIGNED, "WINDOWHEIGHT", 12, PAL_DEFAULT_WINDOW_HEIGHT, 0, UINT32_MAX },
  22. { PALCFG_WINDOWWIDTH, PALCFG_UNSIGNED, "WINDOWWIDTH", 11, PAL_DEFAULT_WINDOW_WIDTH, 0, UINT32_MAX },
  23. { PALCFG_CD, PALCFG_STRING, "CD", 2, "OGG", NULL, NULL },
  24. { PALCFG_GAMEPATH, PALCFG_STRING, "GAMEPATH", 8, NULL, NULL, NULL },
  25. { PALCFG_MESSAGEFILE, PALCFG_STRING, "MESSAGEFILENAME", 15, NULL, NULL, NULL },
  26. { PALCFG_MUSIC, PALCFG_STRING, "MUSIC", 5, "RIX", NULL, NULL },
  27. { PALCFG_OPL, PALCFG_STRING, "OPL", 3, "DOSBOX", NULL, NULL },
  28. { PALCFG_RIXEXTRAINIT, PALCFG_STRING, "RIXEXTRAINIT", 12, NULL, NULL, NULL },
  29. };
  30. BOOL
  31. PAL_ParseConfigLine(
  32. const char * line,
  33. const ConfigItem ** ppItem,
  34. ConfigValue * pValue
  35. )
  36. {
  37. //
  38. // Skip leading spaces
  39. //
  40. while (*line && isspace(*line)) line++;
  41. //
  42. // Skip comments
  43. //
  44. if (*line && *line != '#')
  45. {
  46. const char *ptr;
  47. if (ptr = strchr(line, '='))
  48. {
  49. const char *end = ptr++;
  50. //
  51. // Skip tailing spaces
  52. //
  53. while (end > line && isspace(end[-1])) end--;
  54. int len = end - line;
  55. for (int i = 0; i < sizeof(gConfigItems) / sizeof(ConfigItem); i++)
  56. {
  57. if (gConfigItems[i].NameLength == len &&
  58. SDL_strncasecmp(line, gConfigItems[i].Name, len) == 0)
  59. {
  60. if (ppItem) *ppItem = &gConfigItems[i];
  61. if (pValue)
  62. {
  63. if (gConfigItems[i].Type != PALCFG_STRING)
  64. {
  65. switch (gConfigItems[i].Type)
  66. {
  67. case PALCFG_UNSIGNED:
  68. sscanf(ptr, "%u", &pValue->uValue);
  69. if (pValue->uValue < gConfigItems[i].MinValue.uValue)
  70. pValue->uValue = gConfigItems[i].MinValue.uValue;
  71. else if (pValue->uValue > gConfigItems[i].MaxValue.uValue)
  72. pValue->uValue = gConfigItems[i].MaxValue.uValue;
  73. break;
  74. case PALCFG_INTEGER:
  75. sscanf(ptr, "%d", &pValue->iValue);
  76. if (pValue->iValue < gConfigItems[i].MinValue.iValue)
  77. pValue->iValue = gConfigItems[i].MinValue.iValue;
  78. else if (pValue->iValue > gConfigItems[i].MaxValue.iValue)
  79. pValue->iValue = gConfigItems[i].MaxValue.iValue;
  80. break;
  81. case PALCFG_BOOLEAN:
  82. sscanf(ptr, "%d", &pValue->bValue);
  83. pValue->bValue = pValue->bValue ? TRUE : FALSE;
  84. break;
  85. }
  86. }
  87. else
  88. {
  89. //
  90. // Skip leading spaces
  91. //
  92. while (*ptr && isspace(*ptr)) ptr++;
  93. pValue->sValue = ptr;
  94. }
  95. return TRUE;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. return FALSE;
  102. }
  103. ConfigValue
  104. PAL_DefaultConfig(
  105. PALCFG_ITEM item
  106. )
  107. {
  108. return gConfigItems[item].DefaultValue;
  109. }
  110. const char *
  111. PAL_ConfigName(
  112. PALCFG_ITEM item
  113. )
  114. {
  115. return gConfigItems[item].Name;
  116. }
  117. BOOL
  118. PAL_LimitConfig(
  119. PALCFG_ITEM item,
  120. ConfigValue * pValue
  121. )
  122. {
  123. if (!pValue) return FALSE;
  124. switch (gConfigItems[item].Type)
  125. {
  126. case PALCFG_UNSIGNED:
  127. if (pValue->uValue < gConfigItems[item].MinValue.uValue)
  128. {
  129. pValue->uValue = gConfigItems[item].MinValue.uValue;
  130. return TRUE;
  131. }
  132. else if (pValue->uValue > gConfigItems[item].MaxValue.uValue)
  133. {
  134. pValue->uValue = gConfigItems[item].MaxValue.uValue;
  135. return TRUE;
  136. }
  137. else
  138. return FALSE;
  139. case PALCFG_INTEGER:
  140. if (pValue->iValue < gConfigItems[item].MinValue.iValue)
  141. {
  142. pValue->iValue = gConfigItems[item].MinValue.iValue;
  143. return TRUE;
  144. }
  145. else if (pValue->iValue > gConfigItems[item].MaxValue.iValue)
  146. {
  147. pValue->iValue = gConfigItems[item].MaxValue.iValue;
  148. return TRUE;
  149. }
  150. else
  151. return FALSE;
  152. case PALCFG_BOOLEAN:
  153. if (pValue->bValue != TRUE && pValue->bValue != FALSE)
  154. {
  155. pValue->bValue = pValue->bValue ? TRUE : FALSE;
  156. return TRUE;
  157. }
  158. else
  159. return FALSE;
  160. default:
  161. return FALSE;
  162. }
  163. }
  164. static char *move_to_next_line(char *ptr)
  165. {
  166. while (*ptr && (*ptr != '\r' && *ptr != '\n')) ptr++;
  167. while (*ptr && (*ptr == '\r' || *ptr == '\n')) ptr++;
  168. return (*ptr == '\0') ? NULL : ptr;
  169. }