Browse Source

Generalized version of resource checking

Lou Yihua 6 years ago
parent
commit
94679794e0

+ 1 - 0
.gitignore

@@ -270,6 +270,7 @@ import-summary.txt
 # Android Studio
 .idea
 *.iml
+.settings/
 
 # CocoaPods
 Pods

+ 7 - 27
android/app/src/main/cpp/android_jni.cpp

@@ -192,36 +192,16 @@ JNIEXPORT jboolean JNICALL Java_com_sdlpal_sdlpal_SettingsActivity_setConfigStri
 
 /*
  * Class:     com_sdlpal_sdlpal_SettingsActivity
- * Method:    checkDataFile
- * Signature: (Ljava/lang/String;)Z
+ * Method:    checkResourceFiles
+ * Signature: (Ljava/lang/String;Ljava/lang/String;)Z
  */
 EXTERN_C_LINKAGE
-JNIEXPORT jboolean JNICALL Java_com_sdlpal_sdlpal_SettingsActivity_checkDataFiles(JNIEnv *env, jclass cls, jstring j_str)
+JNIEXPORT jboolean JNICALL Java_com_sdlpal_sdlpal_SettingsActivity_checkResourceFiles(JNIEnv *env, jclass cls, jstring path_str, jstring msg_str)
 {
-    std::string path = jstring_to_utf8(env, j_str);
-    FILE *fp;
-
-#define checkFile(filename) \
-    fp = fopen((path + "/" + filename).c_str(), "rb"); \
-    if (fp == nullptr) return JNI_FALSE; \
-    fclose(fp);
-
-    checkFile("abc.mkf");
-    checkFile("ball.mkf");
-    checkFile("data.mkf");
-    checkFile("f.mkf");
-    checkFile("fbp.mkf");
-    checkFile("fire.mkf");
-    checkFile("gop.mkf");
-    checkFile("m.msg");
-    checkFile("map.mkf");
-    checkFile("mgo.mkf");
-    checkFile("rgm.mkf");
-    checkFile("rng.mkf");
-    checkFile("sss.mkf");
-    checkFile("word.dat");
-
-    return JNI_TRUE;
+    return PAL_MISSING_REQUIRED(UTIL_CheckResourceFiles(
+        jstring_to_utf8(env, path_str).c_str(),
+        jstring_to_utf8(env, msg_str).c_str()
+    )) ? JNI_FALSE : JNI_TRUE;
 }
 
 

+ 3 - 2
android/app/src/main/java/com/sdlpal/sdlpal/SettingsActivity.java

@@ -32,7 +32,7 @@ public class SettingsActivity extends AppCompatActivity {
     public static native boolean setConfigBoolean(String item, boolean value);
     public static native boolean setConfigInt(String item, int value);
     public static native boolean setConfigString(String item, String value);
-    public static native boolean checkDataFiles(String path);
+    public static native boolean checkResourceFiles(String path, String msgfile);
     public static native String getGitRevision();
 
     private static final String KeepAspectRatio = "KeepAspectRatio";
@@ -131,7 +131,8 @@ public class SettingsActivity extends AppCompatActivity {
             @Override
             public void onClick(View v) {
                 String gamePath = ((EditText)findViewById(R.id.edFolder)).getText().toString();
-                if (!checkDataFiles(gamePath)) {
+                String msgFile = ((EditText)findViewById(R.id.edMsgFile)).getText().toString();
+                if (!checkResourceFiles(gamePath, msgFile)) {
                     AlertDialog.Builder builder = new AlertDialog.Builder(mInstance);
                     builder.setMessage(getString(R.string.msg_data_not_found_header) + "\n" +
                             gamePath + "\n\n" +

+ 30 - 0
palcommon.h

@@ -123,6 +123,36 @@ typedef enum tagCODEPAGE {
 	CP_UTF_8 = CP_MAX + 1
 } CODEPAGE;
 
+typedef enum tagPALFILE {
+	PALFILE_ABC = 0x00000001,
+	PALFILE_BALL = 0x00000002,
+	PALFILE_DATA = 0x00000004,
+	PALFILE_F = 0x00000008,
+	PALFILE_FBP = 0x00000010,
+	PALFILE_FIRE = 0x00000020,
+	PALFILE_GOP = 0x00000040,
+	PALFILE_MAP = 0x00000080,
+	PALFILE_MGO = 0x00000100,
+	PALFILE_PAT = 0x00000200,
+	PALFILE_RGM = 0x00000400,
+	PALFILE_RNG = 0x00000800,
+	PALFILE_SSS = 0x00001000,
+	PALFILE_MSG = 0x00002000,
+	PALFILE_M = 0x00004000,
+	PALFILE_WORD = 0x00008000,
+	PALFILE_REQUIRED_MASK = 0x0000ffff,
+	PALFILE_VOC = 0x00010000,
+	PALFILE_SOUNDS = 0x00020000,
+	PALFILE_SOUND_MASK = 0x00030000,
+	PALFILE_MIDI = 0x00040000,
+	PALFILE_MUS = 0x00080000,
+	PALFILE_MUSIC_MASK = 0x000c0000,
+} PALFILE;
+
+#define PAL_MISSING_REQUIRED(x) (((x) & PALFILE_REQUIRED_MASK) != 0)
+#define PAL_MISSING_SOUND(x) (((x) & PALFILE_SOUND_MASK) == PALFILE_SOUND_MASK)
+#define PAL_MISSING_MUSIC(x) (((x) & PALFILE_MUSIC_MASK) == PALFILE_MUSIC_MASK)
+
 PAL_C_LINKAGE_BEGIN
 
 INT

+ 63 - 5
util.c

@@ -33,6 +33,7 @@
 #endif
 
 static char internal_buffer[PAL_MAX_GLOBAL_BUFFERS + 1][PAL_GLOBAL_BUFFER_SIZE];
+#define INTERNAL_BUFFER_SIZE_ARGS internal_buffer[PAL_MAX_GLOBAL_BUFFERS], PAL_GLOBAL_BUFFER_SIZE
 
 void UTIL_MsgBox(char *string)
 {
@@ -524,7 +525,7 @@ UTIL_OpenFileAtPathForMode(
 	//
 	// Construct full path according to lpszPath and lpszFileName
 	//
-	const char *path = UTIL_GetFullPathName(internal_buffer[PAL_MAX_GLOBAL_BUFFERS], PAL_GLOBAL_BUFFER_SIZE, lpszPath, lpszFileName);
+	const char *path = UTIL_GetFullPathName(INTERNAL_BUFFER_SIZE_ARGS, lpszPath, lpszFileName);
 
 	//
 	// If no matching path, check the open mode
@@ -535,7 +536,7 @@ UTIL_OpenFileAtPathForMode(
 	}
 	else if (szMode[0] != 'r')
 	{
-		return fopen(UTIL_CombinePath(internal_buffer[PAL_MAX_GLOBAL_BUFFERS], PAL_GLOBAL_BUFFER_SIZE, 2, lpszPath, lpszFileName), szMode);
+		return fopen(UTIL_CombinePath(INTERNAL_BUFFER_SIZE_ARGS, 2, lpszPath, lpszFileName), szMode);
 	}
 	else
 	{
@@ -585,7 +586,7 @@ UTIL_GetFullPathName(
 	char *_base = strdup(basepath), *_sub = strdup(subpath);
 	const char *result = NULL;
 
-	if (access(UTIL_CombinePath(internal_buffer[PAL_MAX_GLOBAL_BUFFERS], PAL_GLOBAL_BUFFER_SIZE, 2, _base, _sub), 0) == 0)
+	if (access(UTIL_CombinePath(INTERNAL_BUFFER_SIZE_ARGS, 2, _base, _sub), 0) == 0)
 	{
 		result = internal_buffer[PAL_MAX_GLOBAL_BUFFERS];
 	}
@@ -610,9 +611,9 @@ UTIL_GetFullPathName(
 			{
 				if (!result && strcasecmp(list[n]->d_name, start) == 0)
 				{
-					result = UTIL_CombinePath(internal_buffer[PAL_MAX_GLOBAL_BUFFERS], PAL_GLOBAL_BUFFER_SIZE, 2, _base, list[n]->d_name);
+					result = UTIL_CombinePath(INTERNAL_BUFFER_SIZE_ARGS, 2, _base, list[n]->d_name);
 					if (end)
-						result = UTIL_GetFullPathName(internal_buffer[PAL_MAX_GLOBAL_BUFFERS], PAL_GLOBAL_BUFFER_SIZE, result, end + 1);
+						result = UTIL_GetFullPathName(INTERNAL_BUFFER_SIZE_ARGS, result, end + 1);
 					else if (access(result, 0) != 0)
 						result = NULL;
 				}
@@ -703,6 +704,63 @@ UTIL_GlobalBuffer(
 }
 
 
+PALFILE
+UTIL_CheckResourceFiles(
+	const char *path,
+	const char *msgfile
+)
+{
+	const char *common_files[] = {
+		"abc.mkf", "ball.mkf", "data.mkf", "f.mkf",
+		"fbp.mkf", "fire.mkf", "gop.mkf",  "map.mkf",
+		"mgo.mkf", "pat.mkf",  "rgm.mkf",  "rng.mkf",
+		"sss.mkf"
+	};
+	const char *msg_files[][2] = {
+		{ msgfile, "m.msg"    },
+		{ msgfile, "word.dat" }
+	};
+	const char *sound_files[2] = { "voc.mkf", "sounds.mkf" };
+	const char *music_files[2] = { "midi.mkf", "mus.mkf" };
+	int msgidx = !(msgfile && *msgfile);
+	PALFILE retval = (PALFILE)0;
+
+	for (int i = 0; i < sizeof(common_files) / sizeof(common_files[0]); i++)
+	{
+		if (!UTIL_GetFullPathName(INTERNAL_BUFFER_SIZE_ARGS, path, common_files[i]))
+		{
+			retval |= (PALFILE)(1 << i);
+		}
+	}
+
+	for (int i = 0; i < sizeof(msg_files[0]) / sizeof(msg_files[0][0]); i++)
+	{
+		if (!UTIL_GetFullPathName(INTERNAL_BUFFER_SIZE_ARGS, path, msg_files[i][msgidx]))
+		{
+			retval |= (PALFILE)(1 << ((i + 1) * msgidx + 13));
+		}
+	}
+
+	for (int i = 0; i < sizeof(sound_files) / sizeof(sound_files[0]); i++)
+	{
+		if (!UTIL_GetFullPathName(INTERNAL_BUFFER_SIZE_ARGS, path, sound_files[i]))
+		{
+			retval |= (PALFILE)(1 << (i + 16));
+		}
+	}
+
+	for (int i = 0; i < sizeof(music_files) / sizeof(music_files[0]); i++)
+	{
+		if (!UTIL_GetFullPathName(INTERNAL_BUFFER_SIZE_ARGS, path, music_files[i]))
+		{
+			retval |= (PALFILE)(1 << (i + 18));
+		}
+	}
+
+	return retval;
+}
+
+
 #if !defined(PAL_HAS_PLATFORM_SPECIFIC_UTILS)
 
 BOOL

+ 7 - 0
util.h

@@ -24,6 +24,7 @@
 #define UTIL_H
 
 #include "common.h"
+#include "palcommon.h"
 
 PAL_C_LINKAGE_BEGIN
 
@@ -201,6 +202,12 @@ UTIL_GetFullPathName(
 	const char *subpath
 );
 
+PALFILE
+UTIL_CheckResourceFiles(
+	const char *path,
+	const char *msgfile
+);
+
 /*
  * Platform-specific utilities
  */