KYRA: Move Mac file finding code into Util

We use the same method for finding both the unpacked Legend of Kyrandia
executable and the Legend of Kyrandia installer, so there's no need for
duplicate code.
This commit is contained in:
Torbjörn Andersson 2021-11-21 14:57:28 +01:00 committed by Torbjörn Andersson
parent a76ce525d2
commit 3054e32278
4 changed files with 46 additions and 61 deletions

View File

@ -20,6 +20,8 @@
*
*/
#include "common/macresman.h"
#include "common/punycode.h"
#include "kyra/engine/util.h"
namespace Kyra {
@ -115,4 +117,40 @@ Common::String Util::decodeString2(const Common::String &src) {
return tmp;
}
Common::String Util::findMacResourceFile(const char *baseName) {
// The original executable has a TM char as its last character (character
// 0xAA from Mac code page). Depending on the emulator or platform used to
// copy the file it might have been reencoded to something else. So I look
// for multiple versions, also for punycode encoded files and also for the
// case where the user might have just removed the last character by
// renaming the file.
const Common::CodePage tryCodePages[] = {
Common::kMacRoman,
Common::kISO8859_1
};
Common::String tryName(baseName);
Common::String fileName;
for (int i = 0; i < ARRAYSIZE(tryCodePages); ++i) {
for (int ii = 0; ii < 2; ++ii) {
Common::MacResManager resource;
Common::U32String fn(tryName, tryCodePages[i]);
fileName = fn.encode(Common::kUtf8);
if (resource.exists(fileName))
return fileName;
fileName = Common::punycode_encodefilename(fn);
if (resource.exists(fileName))
return fileName;
tryName += "\xAA";
}
}
fileName.clear();
return fileName;
}
} // End of namespace Kyra

View File

@ -40,6 +40,8 @@ public:
static Common::String convertUTF8ToDOS(Common::String &str);
static Common::String convertISOToUTF8(Common::String &str);
static void convertISOToDOS(char &c);
static Common::String findMacResourceFile(const char *baseName);
};
} // End of namespace Kyra

View File

@ -20,12 +20,13 @@
*
*/
#include "kyra/engine/util.h"
#include "kyra/resource/resource.h"
#include "kyra/resource/resource_intern.h"
#include "common/config-manager.h"
#include "common/macresman.h"
#include "common/punycode.h"
//#include "common/macresman.h"
//#include "common/punycode.h"
#include "common/fs.h"
namespace Kyra {
@ -66,33 +67,7 @@ bool Resource::reset() {
error("invalid game path '%s'", dir.getPath().c_str());
if (_vm->game() == GI_KYRA1 && _vm->gameFlags().platform == Common::kPlatformMacintosh && _vm->gameFlags().useInstallerPackage) {
const char *const tryFileNames[] = {
"Install Legend of Kyrandia",
"Install Legend of Kyrandia\xaa"
};
const Common::CodePage tryCodePages[] = {
Common::kMacRoman,
Common::kISO8859_1
};
Common::MacResManager resource;
Common::String kyraInstaller;
for (int i = 0; i < ARRAYSIZE(tryCodePages); ++i) {
for (int ii = 0; ii < ARRAYSIZE(tryFileNames); ++ii) {
Common::U32String fn(tryFileNames[ii], tryCodePages[i]);
kyraInstaller = fn.encode(Common::kUtf8);
if (resource.exists(kyraInstaller))
break;
kyraInstaller = Common::punycode_encodefilename(fn);
if (resource.exists(kyraInstaller))
break;
kyraInstaller.clear();
}
if (!kyraInstaller.empty())
break;
}
Common::String kyraInstaller = Util::findMacResourceFile("Install Legend of Kyrandia");
if (kyraInstaller.empty()) {
error("Could not find Legend of Kyrandia installer file");

View File

@ -20,6 +20,7 @@
*
*/
#include "kyra/engine/util.h"
#include "kyra/resource/resource.h"
#include "kyra/sound/sound_intern.h"
#include "kyra/sound/sound_mac_res.h"
@ -27,7 +28,6 @@
#include "common/config-manager.h"
#include "common/macresman.h"
#include "common/punycode.h"
#include "common/stuffit.h"
#include "audio/mixer.h"
@ -54,37 +54,7 @@ bool SoundMacRes::init() {
return false;
if (!_stuffItArchive) {
// The original executable has a TM char as its last character
// (character 0xaa from Mac code page). Depending on the emulator or
// platform used to copy the file it might have been reencoded to
// something else. So I look for multiple versions, also for punycode
// encoded files and also for the case where the user might have just
// removed the last character by renaming the file.
const Common::CodePage tryCodePages[] = {
Common::kMacRoman,
Common::kISO8859_1
};
const char *const tryExeNames[] = {
"Legend of Kyrandia\xaa",
"Legend of Kyrandia"
};
for (int i = 0; i < ARRAYSIZE(tryCodePages); ++i) {
for (int ii = 0; ii < ARRAYSIZE(tryExeNames); ++ii) {
Common::U32String fn(tryExeNames[ii], tryCodePages[i]);
_kyraMacExe = fn.encode(Common::kUtf8);
if (_macRes->exists(_kyraMacExe))
break;
_kyraMacExe = Common::punycode_encodefilename(fn);
if (_macRes->exists(_kyraMacExe))
break;
_kyraMacExe.clear();
}
if (!_kyraMacExe.empty())
break;
}
_kyraMacExe = Util::findMacResourceFile("Legend of Kyrandia");
if (_kyraMacExe.empty()) {
warning("SoundMacRes::init(): Legend of Kyrandia resource fork not found");