louyihua 8 years ago
parent
commit
ff3696d415
5 changed files with 45 additions and 30 deletions
  1. 0 7
      common.h
  2. 6 11
      makemessage.py
  3. 1 2
      sound.c
  4. 37 9
      text.c
  5. 1 1
      ui.c

+ 0 - 7
common.h

@@ -45,13 +45,6 @@ extern "C"
 #include "SDL.h"
 #include "SDL_endian.h"
 
-#ifdef _SDL_stdinc_h
-#define malloc       SDL_malloc
-#define calloc       SDL_calloc
-#define free         SDL_free
-#define realloc      SDL_realloc
-#endif
-
 #if SDL_VERSION_ATLEAST(2,0,0)
 
 #define SDLK_KP1     SDLK_KP_1

+ 6 - 11
makemessage.py

@@ -9,22 +9,17 @@ import struct
 
 def main():
 
-    parser = argparse.ArgumentParser(description='Generate a translatable language file that can be used by SDLPAL.')
-    parser.add_argument('gamepath', help='Game path where SSS.MKF & M.MSG & WORD.DAT are located.')
-    parser.add_argument('outputfile', help='Path of the output message file.')
-    parser.add_argument('encoding', help='Text encoding name (such as "gb2312" or "big5").')
-    parser.add_argument('-w','--width', dest='wordwidth', help='Word width in bytes')
-    parser.add_argument("-c", "--comment", action="store_true", help='Automatically generate comments')
+    parser = argparse.ArgumentParser(description = 'Generate a translatable language file that can be used by SDLPAL.')
+    parser.add_argument('gamepath', help = 'Game path where SSS.MKF & M.MSG & WORD.DAT are located.')
+    parser.add_argument('outputfile', help = 'Path of the output message file.')
+    parser.add_argument('encoding', choices = ['gbk', 'big5'], help = 'Text encoding name, should be either gbk or big5.')
+    parser.add_argument('-w', '--width', dest = 'wordwidth', default = 10, type = int, help = 'Word width in bytes, default is 10')
+    parser.add_argument("-c", "--comment", action = 'store_true', help = 'Automatically generate comments')
     options = parser.parse_args()
 
     if options.gamepath[-1] != '/' and options.gamepath[-1] != '\\':
         options.gamepath += '/'
 
-    if options.wordwidth == None:
-        options.wordwidth = 10
-    else:
-        options.wordwidth = int(options.wordwidth)
-
     script_bytes = []
     index_bytes = []
     msg_bytes = []

+ 1 - 2
sound.c

@@ -910,9 +910,8 @@ SOUND_PlayChannel(
 	   resampler_set_quality(gSndPlayer.resampler, SOUND_IsIntegerConversion(wavespec.freq) ? RESAMPLER_QUALITY_MIN : gpGlobals->iResampleQuality);
 	   resampler_set_rate(gSndPlayer.resampler, (double)wavespec.freq / (double)gSndPlayer.spec.freq);
 	   len = (int)ceil(wavespec.size * (double)gSndPlayer.spec.freq / (double)wavespec.freq) * (SDL_AUDIO_BITSIZE(AUDIO_S16) / SDL_AUDIO_BITSIZE(wavespec.format));
-	   if (len >= wavespec.channels * 2)
+	   if (len >= wavespec.channels * 2 && (bufdec = malloc(len)))
 	   {
-		   bufdec = malloc(len);
 		   if (wavespec.format == AUDIO_S16)
 			   SOUND_ResampleS16(bufsrc, &wavespec, bufdec, len, gSndPlayer.resampler);
 		   else

+ 37 - 9
text.c

@@ -226,12 +226,12 @@ PAL_ReadMessageFile(
 						//
 						if (head)
 						{
-							item->next = (struct _msg_list_entry *)malloc(sizeof(struct _msg_list_entry));
+							item->next = (struct _msg_list_entry *)UTIL_malloc(sizeof(struct _msg_list_entry));
 							item = item->next;
 						}
 						else
 						{
-							head = (struct _msg_list_entry *)malloc(sizeof(struct _msg_list_entry));
+							head = (struct _msg_list_entry *)UTIL_malloc(sizeof(struct _msg_list_entry));
 							item = head;
 						}
 						item->value = NULL; item->index = sid;
@@ -273,11 +273,11 @@ PAL_ReadMessageFile(
 				{
 					if (cur_val)
 					{
-						cur_val->next = (struct _msg_entry *)malloc(sizeof(struct _msg_entry));
+						cur_val->next = (struct _msg_entry *)UTIL_malloc(sizeof(struct _msg_entry));
 						cur_val = cur_val->next;
 					}
 					else
-						cur_val = (struct _msg_entry *)malloc(sizeof(struct _msg_entry));
+						cur_val = (struct _msg_entry *)UTIL_malloc(sizeof(struct _msg_entry));
 					if (strncmp(buffer, "[CLEAR MESSAGE]", 15) == 0)
 					{
 						cur_val->value = NULL;
@@ -285,7 +285,7 @@ PAL_ReadMessageFile(
 					else
 					{
 						int len = PAL_MultiByteToWideCharCP(CP_UTF_8, buffer, -1, NULL, 0);
-						cur_val->value = (wchar_t *)malloc(len * sizeof(wchar_t));
+						cur_val->value = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
 						PAL_MultiByteToWideCharCP(CP_UTF_8, buffer, -1, cur_val->value, len);
 						msg_cnt++;
 					}
@@ -309,8 +309,8 @@ PAL_ReadMessageFile(
 					if (i > 0 && i <= PAL_ADDITIONAL_WORD_LAST)
 					{
 						int len = PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, NULL, 0);
-						struct _word_list_entry *val = (struct _word_list_entry *)malloc(sizeof(struct _word_list_entry));
-						val->value = (wchar_t *)malloc(len * sizeof(wchar_t));
+						struct _word_list_entry *val = (struct _word_list_entry *)UTIL_malloc(sizeof(struct _word_list_entry));
+						val->value = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
 						PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, val->value, len);
 						val->index = i; val->next = NULL;
 						witem->next = val; witem = witem->next;
@@ -365,14 +365,14 @@ PAL_ReadMessageFile(
 								}
 								*dst = 0;
 								len = PAL_MultiByteToWideCharCP(CP_UTF_8, tmp, -1, NULL, 0);
-								g_rcCredits[i] = (wchar_t *)malloc(len * sizeof(wchar_t));
+								g_rcCredits[i] = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
 								PAL_MultiByteToWideCharCP(CP_UTF_8, tmp, -1, g_rcCredits[i], len);
 							}
 						}
 						else
 						{
 							len = PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, NULL, 0);
-							g_rcCredits[i] = (wchar_t *)malloc(len * sizeof(wchar_t));
+							g_rcCredits[i] = (wchar_t *)UTIL_malloc(len * sizeof(wchar_t));
 							PAL_MultiByteToWideCharCP(CP_UTF_8, v, -1, g_rcCredits[i], len);
 						}
 						if (g_rcCredits[i])
@@ -574,7 +574,22 @@ PAL_InitText(
 		   wlen += PAL_MultiByteToWideChar((LPCSTR)temp + base, gpGlobals->dwWordLength, NULL, 0) + 1;
 	   }
 	   g_TextLib.lpWordBuf = (LPWSTR*)malloc(g_TextLib.nWords * sizeof(LPWSTR));
+	   if (g_TextLib.lpWordBuf == NULL)
+	   {
+		   free(temp);
+		   fclose(fpWord);
+		   fclose(fpMsg);
+		   return -1;
+	   }
 	   tmp = (LPWSTR)malloc(wlen * sizeof(WCHAR));
+	   if (tmp == NULL)
+	   {
+		   free(g_TextLib.lpWordBuf);
+		   free(temp);
+		   fclose(fpWord);
+		   fclose(fpMsg);
+		   return -1;
+	   }
 	   for (i = 0, wpos = 0; i < g_TextLib.nWords; i++)
 	   {
 		   int l;
@@ -630,7 +645,20 @@ PAL_InitText(
 		   wlen += PAL_MultiByteToWideChar((LPCSTR)temp + SDL_SwapLE32(offsets[i]), SDL_SwapLE32(offsets[i + 1]) - SDL_SwapLE32(offsets[i]), NULL, 0) + 1;
 	   }
 	   g_TextLib.lpMsgBuf = (LPWSTR*)malloc(g_TextLib.nMsgs * sizeof(LPWSTR));
+	   if (g_TextLib.lpMsgBuf == NULL)
+	   {
+		   free(g_TextLib.lpWordBuf);
+		   free(offsets);
+		   return -1;
+	   }
 	   tmp = (LPWSTR)malloc(wlen * sizeof(WCHAR));
+	   if (tmp == NULL)
+	   {
+		   free(g_TextLib.lpMsgBuf);
+		   free(g_TextLib.lpWordBuf);
+		   free(offsets);
+		   return -1;
+	   }
 	   for (i = 0, wpos = 0; i < g_TextLib.nMsgs; i++)
 	   {
 		   int l;

+ 1 - 1
ui.c

@@ -840,7 +840,7 @@ PAL_LoadObjectDesc(
 
       sscanf(buf, "%x", &i);
       pNew->wObjectID = i;
-	  pNew->lpDesc = (LPWSTR)malloc(wlen * sizeof(WCHAR));
+	  pNew->lpDesc = (LPWSTR)UTIL_malloc(wlen * sizeof(WCHAR));
 	  PAL_MultiByteToWideChar(p, -1, pNew->lpDesc, wlen);
 
       pNew->next = lpDesc;