Permalink
Browse files

SLUDGE: Objectify language manager

  • Loading branch information...
yinsimei committed Jul 18, 2017
1 parent 70c965a commit 798ec60f54064a6a7f5750ab792d06666f9ce8ea
@@ -2438,7 +2438,7 @@ builtIn(getCharacterScale) {
builtIn(getLanguageID) {
UNUSEDALL
setVariable(fun->reg, SVT_INT, gameSettings.languageID);
setVariable(fun->reg, SVT_INT, g_sludge->getLanguageID());
return BR_CONTINUE;
}
View
@@ -169,16 +169,13 @@ void finishAccess() {
int32 startIndex;
void setFileIndices(Common::File *fp, uint numLanguages, uint skipBefore) {
if (fp) {
// Keep hold of the file handle, and let things get at it
bigDataFile = fp;
startIndex = fp->pos();
} else {
// No file pointer - this means that we reuse the bigDataFile
fp = bigDataFile;
fp->seek(startIndex, SEEK_SET);
}
void setBigDataFile(Common::File *fp) {
bigDataFile = fp;
startIndex = fp->pos();
}
void setFileIndices(uint numLanguages, uint skipBefore) {
bigDataFile->seek(startIndex, SEEK_SET);
sliceBusy = false;
if (skipBefore > numLanguages) {
@@ -189,29 +186,29 @@ void setFileIndices(Common::File *fp, uint numLanguages, uint skipBefore) {
// STRINGS
int skipAfter = numLanguages - skipBefore;
while (skipBefore) {
fp->seek(fp->readUint32LE(), SEEK_SET);
bigDataFile->seek(bigDataFile->readUint32LE(), SEEK_SET);
skipBefore--;
}
startOfTextIndex = fp->pos() + 4;
startOfTextIndex = bigDataFile->pos() + 4;
debug(kSludgeDebugDataLoad, "startOfTextIndex: %i", startOfTextIndex);
fp->seek(fp->readUint32LE(), SEEK_SET);
bigDataFile->seek(bigDataFile->readUint32LE(), SEEK_SET);
while (skipAfter) {
fp->seek(fp->readUint32LE(), SEEK_SET);
bigDataFile->seek(bigDataFile->readUint32LE(), SEEK_SET);
skipAfter--;
}
startOfSubIndex = fp->pos() + 4;
fp->seek(fp->readUint32LE(), SEEK_CUR);
startOfSubIndex = bigDataFile->pos() + 4;
bigDataFile->seek(bigDataFile->readUint32LE(), SEEK_CUR);
debug(kSludgeDebugDataLoad, "startOfSubIndex: %i", startOfSubIndex);
startOfObjectIndex = fp->pos() + 4;
fp->seek(fp->readUint32LE(), SEEK_CUR);
startOfObjectIndex = bigDataFile->pos() + 4;
bigDataFile->seek(bigDataFile->readUint32LE(), SEEK_CUR);
debug(kSludgeDebugDataLoad, "startOfObjectIndex: %i", startOfObjectIndex);
// Remember that the data section starts here
startOfDataIndex = fp->pos();
startOfDataIndex = bigDataFile->pos();
debug(kSludgeDebugDataLoad, "startOfDataIndex: %i", startOfDataIndex);
}
View
@@ -28,7 +28,8 @@ namespace Sludge {
extern Common::File *bigDataFile;
void setFileIndices(Common::File *fp, uint, uint);
void setBigDataFile(Common::File *readStream);
void setFileIndices(uint, uint);
uint openFileFromNum(int num);
bool openSubSlice(int num);
@@ -23,6 +23,7 @@
#include "common/debug.h"
#include "sludge/allfiles.h"
#include "sludge/fileset.h"
#include "sludge/newfatal.h"
#include "sludge/moreio.h"
#include "sludge/language.h"
@@ -31,40 +32,80 @@
namespace Sludge {
uint *languageTable;
Common::String *languageName;
settingsStruct gameSettings;
LanguageManager::~LanguageManager() {
if (_languageTable) {
delete []_languageTable;
_languageTable = NULL;
}
if (_languageNames) {
delete []_languageNames;
_languageNames = NULL;
}
}
void makeLanguageTable(Common::File *table) {
languageTable = new uint[gameSettings.numLanguages + 1];
if (!checkNew(languageTable))
void LanguageManager::init(Common::File *fp) {
// get number of languages
_numLanguages =
(gameVersion >= VERSION(1, 3)) ? (fp->readByte()) : 0;
debug(kSludgeDebugDataLoad, "numLanguages : %c", _numLanguages);
// make language table
_languageTable = new uint[_numLanguages + 1];
if (!checkNew(_languageTable))
return;
languageName = new Common::String[gameSettings.numLanguages + 1];
if (!checkNew(languageName))
_languageNames = new Common::String[_numLanguages + 1];
if (!checkNew(_languageNames))
return;
for (uint i = 0; i <= gameSettings.numLanguages; i++) {
languageTable[i] = i ? table->readUint16BE() : 0;
debug(kSludgeDebugDataLoad, "languageTable %i: %i", i, languageTable[i]);
languageName[i].clear();
for (uint i = 0; i <= _numLanguages; i++) {
_languageTable[i] = i ? fp->readUint16BE() : 0;
debug(kSludgeDebugDataLoad, "languageTable %i: %i", i, _languageTable[i]);
_languageNames[i].clear();
if (gameVersion >= VERSION(2, 0)) {
if (gameSettings.numLanguages) {
languageName[i] = readString(table);
debug(kSludgeDebugDataLoad, "languageName %i: %s\n", i, languageName[i].c_str());
if (_numLanguages) {
_languageNames[i] = readString(fp);
debug(kSludgeDebugDataLoad, "languageName %i: %s\n", i, _languageNames[i].c_str());
}
}
}
}
int getLanguageForFileB() {
int indexNum = -1;
void LanguageManager::setLanguageID(uint id) {
_languageID = id;
// get index of language
setLanguageIndex(getLanguageForFileB());
}
for (uint i = 0; i <= gameSettings.numLanguages; i++) {
if (languageTable[i] == gameSettings.languageID)
int LanguageManager::getLanguageForFileB() {
int indexNum = -1;
for (uint i = 0; i <= _numLanguages; i++) {
if (_languageTable[i] == _languageID)
indexNum = i;
}
return indexNum;
}
void LanguageManager::saveLanguageSetting(Common::WriteStream *writeStream) {
writeStream->writeByte(_numLanguages);
}
void LanguageManager::loadLanguageSetting(Common::SeekableReadStream *readStream) {
uint languageIdx = readStream->readByte();
setLanguageIndex(languageIdx);
}
void LanguageManager::setLanguageIndex(int idx) {
if (idx < 0)
fatal("Can't find the translation data specified!");
if (idx != _languageIdx) {
// Load the saved language!
_languageIdx = idx;
// Now set file indices properly to the chosen language.
setFileIndices(_numLanguages, _languageIdx);
}
}
} // End of namespace Sludge
View
@@ -28,17 +28,32 @@
namespace Sludge {
struct settingsStruct {
uint languageID;
uint numLanguages;
class LanguageManager {
public:
LanguageManager() :
_languageID(0),
_languageIdx(-1),
_numLanguages(0),
_languageTable(0),
_languageNames(0) {}
~LanguageManager();
void init(Common::File *table);
void setLanguageID(uint id);
void saveLanguageSetting(Common::WriteStream *writeStream);
void loadLanguageSetting(Common::SeekableReadStream *readStream);
private:
uint _languageID; // id of selected language
int _languageIdx; // index of selected language in table
uint _numLanguages; // number of existing languages in game
uint *_languageTable; // indexes of existing languages
Common::String *_languageNames; // language names
int getLanguageForFileB();
void setLanguageIndex(int idx);
};
extern settingsStruct gameSettings;
int getLanguageForFileB();
void makeLanguageTable(Common::File *table);
} // End of namespace Sludge
#endif
@@ -29,6 +29,7 @@
#include "sludge/variable.h"
#include "sludge/language.h"
#include "sludge/moreio.h"
#include "sludge/sludge.h"
#include "sludge/sludger.h"
#include "sludge/people.h"
#include "sludge/talk.h"
@@ -40,7 +41,6 @@
#include "sludge/cursors.h"
#include "sludge/statusba.h"
#include "sludge/sound.h"
#include "sludge/fileset.h"
#include "sludge/loadsave.h"
#include "sludge/bg_effects.h"
#include "sludge/thumbnail.h"
@@ -80,7 +80,6 @@ extern byte currentBurnR, currentBurnG, currentBurnB;
extern uint currentBlankColour; // in backdrop.cpp
extern parallaxLayer *parallaxStuff; // "
extern int lightMapMode; // "
extern int languageNum;
//----------------------------------------------------------------------
// Globals (so we know what's saved already and what's a reference
@@ -464,7 +463,7 @@ bool saveGame(const Common::String &fname) {
saveParallaxRecursive(parallaxStuff, fp);
fp->writeByte(0);
fp->writeByte(languageNum); // Selected language
g_sludge->_languageMan->saveLanguageSetting(fp);
saveSnapshot(fp);
@@ -663,12 +662,7 @@ bool loadGame(const Common::String &fname) {
return false;
}
int selectedLanguage = fp->readByte();
if (selectedLanguage != languageNum) {
// Load the saved language!
languageNum = selectedLanguage;
setFileIndices(NULL, gameSettings.numLanguages, languageNum);
}
g_sludge->_languageMan->loadLanguageSetting(fp);
}
nosnapshot();
@@ -61,6 +61,9 @@ SludgeEngine::SludgeEngine(OSystem *syst, const SludgeGameDescription *gameDesc)
fatalMessage = "";
fatalInfo = "Initialisation error! Something went wrong before we even got started!";
// Init managers
_languageMan = new LanguageManager();
}
SludgeEngine::~SludgeEngine() {
@@ -81,6 +84,10 @@ SludgeEngine::~SludgeEngine() {
_origFormat = nullptr;
delete _pixelFormat;
_pixelFormat = nullptr;
// Dispose managers
delete _languageMan;
_languageMan = nullptr;
}
Common::Error SludgeEngine::run() {
View
@@ -29,6 +29,7 @@
#include "gui/debugger.h"
#include "sludge/console.h"
#include "sludge/language.h"
#include "sludge/timing.h"
namespace Sludge {
@@ -57,7 +58,6 @@ class SludgeEngine: public Engine {
// global String variables
Common::String launchMe;
Common::String loadNow;
Common::String gameName;
Common::String gamePath;
Common::String bundleFolder;
Common::String fatalMessage;
@@ -66,6 +66,9 @@ class SludgeEngine: public Engine {
// timer
Timer _timer;
// managers
LanguageManager *_languageMan;
SludgeEngine(OSystem *syst, const SludgeGameDescription *gameDesc);
virtual ~SludgeEngine();
View
@@ -65,7 +65,6 @@ Common::String *allUserFunc = NULL;
int numResourceNames = 0;
Common::String *allResourceNames = NULL;
int selectedLanguage = 0;
int languageNum = -1;
int gameVersion;
FILETIME fileTime;
@@ -230,10 +229,7 @@ bool initSludge(const Common::String &filename) {
Common::String dataFol = (gameVersion >= VERSION(1, 3)) ? readString(fp) : "";
debug(kSludgeDebugDataLoad, "dataFol : %s", dataFol.c_str());
gameSettings.numLanguages =
(gameVersion >= VERSION(1, 3)) ? (fp->readByte()) : 0;
debug(kSludgeDebugDataLoad, "numLanguages : %c", gameSettings.numLanguages);
makeLanguageTable(fp);
g_sludge->_languageMan->init(fp);
if (gameVersion >= VERSION(1, 6)) {
fp->readByte();
@@ -282,21 +278,9 @@ bool initSludge(const Common::String &filename) {
for (a = 0; a < numGlobals; a++)
initVarNew(globalVars[a]);
// Get the original (untranslated) name of the game and convert it to Unicode.
// We use this to find saved preferences and saved games.
setFileIndices(fp, gameSettings.numLanguages, 0);
Common::String gameNameOrig = getNumberedString(1);
Common::String gameName = encodeFilename(gameNameOrig);
// Get language selected by user
gameSettings.languageID = g_sludge->getLanguageID();
// Now set file indices properly to the chosen language.
languageNum = getLanguageForFileB();
if (languageNum < 0)
return fatal("Can't find the translation data specified!");
setFileIndices(NULL, gameSettings.numLanguages, languageNum);
setBigDataFile(fp);
g_sludge->_languageMan->setLanguageID(g_sludge->getLanguageID());
if (!dataFol.empty()) {
Common::String dataFolder = encodeFilename(dataFol);

0 comments on commit 798ec60

Please sign in to comment.