SLUDGE: Remove global variable saveEncoding and refactor CustomSaveData features

This commit is contained in:
Simei Yin 2018-04-29 09:42:26 +02:00
parent 5d1ed6d68f
commit 8b91fe536c
5 changed files with 98 additions and 96 deletions

View file

@ -66,7 +66,6 @@ extern Common::String *allUserFunc;
extern Common::String *allBIFNames; extern Common::String *allBIFNames;
extern byte fadeMode; extern byte fadeMode;
extern uint16 saveEncoding;
int paramNum[] = { -1, 0, 1, 1, -1, -1, 1, 3, 4, 1, 0, 0, 8, -1, // SAY->MOVEMOUSE int paramNum[] = { -1, 0, 1, 1, -1, -1, 1, 3, 4, 1, 0, 0, 8, -1, // SAY->MOVEMOUSE
-1, 0, 0, -1, -1, 1, 1, 1, 1, 4, 1, 1, 2, 1,// FOCUS->REMOVEREGION -1, 0, 0, -1, -1, 1, 1, 1, 1, 4, 1, 1, 2, 1,// FOCUS->REMOVEREGION
@ -2246,7 +2245,7 @@ builtIn(saveCustomData) {
fatal("First parameter isn't a stack"); fatal("First parameter isn't a stack");
return BR_ERROR; return BR_ERROR;
} }
if (!stackToFile(fileName, fun->stack->thisVar)) if (!CustomSaveHelper::stackToFile(fileName, fun->stack->thisVar))
return BR_ERROR; return BR_ERROR;
trimStack(fun->stack); trimStack(fun->stack);
return BR_CONTINUE; return BR_CONTINUE;
@ -2271,7 +2270,7 @@ builtIn(loadCustomData) {
fun->reg.varData.theStack->first = NULL; fun->reg.varData.theStack->first = NULL;
fun->reg.varData.theStack->last = NULL; fun->reg.varData.theStack->last = NULL;
fun->reg.varData.theStack->timesUsed = 1; fun->reg.varData.theStack->timesUsed = 1;
if (!fileToStack(newText, fun->reg.varData.theStack)) if (!CustomSaveHelper::fileToStack(newText, fun->reg.varData.theStack))
return BR_ERROR; return BR_ERROR;
return BR_CONTINUE; return BR_CONTINUE;
} }
@ -2281,7 +2280,7 @@ builtIn(setCustomEncoding) {
int n; int n;
if (!getValueType(n, SVT_INT, fun->stack->thisVar)) if (!getValueType(n, SVT_INT, fun->stack->thisVar))
return BR_ERROR; return BR_ERROR;
saveEncoding = n; CustomSaveHelper::_saveEncoding = n;
trimStack(fun->stack); trimStack(fun->stack);
setVariable(fun->reg, SVT_INT, 1); setVariable(fun->reg, SVT_INT, 1);
return BR_CONTINUE; return BR_CONTINUE;

View file

@ -37,6 +37,7 @@
#include "sludge/objtypes.h" #include "sludge/objtypes.h"
#include "sludge/people.h" #include "sludge/people.h"
#include "sludge/region.h" #include "sludge/region.h"
#include "sludge/savedata.h"
#include "sludge/sludge.h" #include "sludge/sludge.h"
#include "sludge/sludger.h" #include "sludge/sludger.h"
#include "sludge/sound.h" #include "sludge/sound.h"
@ -62,7 +63,6 @@ extern Floor *currentFloor; // In floor.cpp
extern FILETIME fileTime; // In sludger.cpp extern FILETIME fileTime; // In sludger.cpp
extern byte fadeMode; // In transition.cpp extern byte fadeMode; // In transition.cpp
extern bool allowAnyFilename; extern bool allowAnyFilename;
extern uint16 saveEncoding; // in savedata.cpp
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// Globals (so we know what's saved already and what's a reference // Globals (so we know what's saved already and what's a reference
@ -409,7 +409,7 @@ bool saveGame(const Common::String &fname) {
saveStatusBars(fp); saveStatusBars(fp);
g_sludge->_soundMan->saveSounds(fp); g_sludge->_soundMan->saveSounds(fp);
fp->writeUint16BE(saveEncoding); fp->writeUint16BE(CustomSaveHelper::_saveEncoding);
blur_saveSettings(fp); blur_saveSettings(fp);
@ -544,7 +544,7 @@ bool loadGame(const Common::String &fname) {
loadStatusBars(fp); loadStatusBars(fp);
g_sludge->_soundMan->loadSounds(fp); g_sludge->_soundMan->loadSounds(fp);
saveEncoding = fp->readUint16BE(); CustomSaveHelper::_saveEncoding = fp->readUint16BE();
if (ssgVersion >= VERSION(1, 6)) { if (ssgVersion >= VERSION(1, 6)) {
if (ssgVersion < VERSION(2, 0)) { if (ssgVersion < VERSION(2, 0)) {

View file

@ -20,44 +20,45 @@
* *
*/ */
#include "common/file.h" #include "common/savefile.h"
#include "sludge/allfiles.h" #include "sludge/allfiles.h"
#include "sludge/moreio.h" #include "sludge/moreio.h"
#include "sludge/newfatal.h" #include "sludge/newfatal.h"
#include "sludge/savedata.h"
#include "sludge/variable.h" #include "sludge/variable.h"
#define LOAD_ERROR "Can't load custom data...\n\n" #define LOAD_ERROR "Can't load custom data...\n\n"
namespace Sludge { namespace Sludge {
const char UTF8_CHECKER[] = {'U', 'N', '\xef', '\xbf', '\xbd', 'L', 'O', '\xef', '\xbf', '\xbd', 'C', 'K', 'E', 'D', '\0'}; const char CustomSaveHelper::UTF8_CHECKER[] = {'U', 'N', '\xef', '\xbf', '\xbd', 'L', 'O', '\xef', '\xbf', '\xbd', 'C', 'K', 'E', 'D', '\0'};
uint16 saveEncoding = false; uint16 CustomSaveHelper::_saveEncoding = false;
char encode1 = 0; char CustomSaveHelper::_encode1 = 0;
char encode2 = 0; char CustomSaveHelper::_encode2 = 0;
void writeStringEncoded(const Common::String &s, Common::WriteStream *stream) { void CustomSaveHelper::writeStringEncoded(const Common::String checker, Common::WriteStream *stream) {
int len = s.size(); int len = checker.size();
stream->writeUint16BE(len); stream->writeUint16BE(len);
for (int a = 0; a < len; a++) { for (int a = 0; a < len; a++) {
stream->writeByte(s[a] ^ encode1); stream->writeByte(checker[a] ^ _encode1);
encode1 += encode2; _encode1 += _encode2;
} }
} }
Common::String readStringEncoded(Common::File *fp) { Common::String CustomSaveHelper::readStringEncoded(Common::SeekableReadStream *fp) {
int len = fp->readUint16BE(); int len = fp->readUint16BE();
Common::String res = ""; Common::String res = "";
for (int a = 0; a < len; a++) { for (int a = 0; a < len; a++) {
res += (char)(fp->readByte() ^ encode1); res += (char)(fp->readByte() ^ _encode1);
encode1 += encode2; _encode1 += _encode2;
} }
return res; return res;
} }
char *readTextPlain(Common::File *fp) { char *CustomSaveHelper::readTextPlain(Common::SeekableReadStream *fp) {
int32 startPos; int32 startPos;
uint32 stringSize = 0; uint32 stringSize = 0;
@ -94,83 +95,63 @@ char *readTextPlain(Common::File *fp) {
return reply; return reply;
} }
bool fileToStack(const Common::String &filename, StackHandler *sH) { bool CustomSaveHelper::fileToStack(const Common::String &filename, StackHandler *sH) {
Variable stringVar; Variable stringVar;
stringVar.varType = SVT_NULL; stringVar.varType = SVT_NULL;
Common::String checker = saveEncoding ? "[Custom data (encoded)]\r\n" : "[Custom data (ASCII)]\n"; Common::String checker = _saveEncoding ? "[Custom data (encoded)]\r\n" : "[Custom data (ASCII)]\n";
Common::File fd; Common::InSaveFile *fp = g_system->getSavefileManager()->openForLoading(filename);
if (!fd.open(filename)) { if (fp == NULL) {
#if 0
char currentDir[1000];
if (!getcwd(currentDir, 998)) {
debugOut("Can't get current directory.\n");
}
if (chdir(gamePath)) {
debugOut("Error: Failed changing to directory %s\n", gamePath);
}
if (chdir(currentDir)) {
debugOut("Error: Failed changing to directory %s\n", currentDir);
}
if (!fd.open(filename)) {
return fatal("No such file", filename);
}
#endif
return fatal("No such file", filename); //TODO: false value return fatal("No such file", filename); //TODO: false value
} }
encode1 = (byte)saveEncoding & 255; _encode1 = (byte)_saveEncoding & 255;
encode2 = (byte)(saveEncoding >> 8); _encode2 = (byte)(_saveEncoding >> 8);
for (uint i = 0; i < checker.size(); ++i) { for (uint i = 0; i < checker.size(); ++i) {
if (fd.readByte() != checker[i]) { if (fp->readByte() != checker[i]) {
fd.close(); delete fp;
return fatal(LOAD_ERROR "This isn't a SLUDGE custom data file:", filename); return fatal(LOAD_ERROR "This isn't a SLUDGE custom data file:", filename);
} }
} }
if (saveEncoding) { if (_saveEncoding) {
checker = readStringEncoded(&fd); checker = readStringEncoded(fp);
if (checker == UTF8_CHECKER) { if (checker == UTF8_CHECKER) {
fd.close(); delete fp;
return fatal( return fatal(LOAD_ERROR "The current file encoding setting does not match the encoding setting used when this file was created:", filename);
LOAD_ERROR "The current file encoding setting does not match the encoding setting used when this file was created:", filename);
} }
} }
for (;;) { for (;;) {
if (saveEncoding) { if (_saveEncoding) {
char i = fd.readByte() ^ encode1; char i = fp->readByte() ^ _encode1;
if (fd.eos()) if (fp->eos())
break; break;
switch (i) { switch (i) {
case 0: { case 0: {
Common::String g = readStringEncoded(&fd); Common::String g = readStringEncoded(fp);
makeTextVar(stringVar, g); makeTextVar(stringVar, g);
} }
break; break;
case 1: case 1:
setVariable(stringVar, SVT_INT, fd.readUint32LE()); setVariable(stringVar, SVT_INT, fp->readUint32LE());
break; break;
case 2: case 2:
setVariable(stringVar, SVT_INT, fd.readByte()); setVariable(stringVar, SVT_INT, fp->readByte());
break; break;
default: default:
fatal(LOAD_ERROR "Corrupt custom data file:", filename); fatal(LOAD_ERROR "Corrupt custom data file:", filename);
fd.close(); delete fp;
return false; return false;
} }
} else { } else {
char *line = readTextPlain(&fd); char *line = readTextPlain(fp);
if (!line) if (!line)
break; break;
makeTextVar(stringVar, line); makeTextVar(stringVar, line);
@ -188,63 +169,66 @@ bool fileToStack(const Common::String &filename, StackHandler *sH) {
sH->last = sH->last->next; sH->last = sH->last->next;
} }
} }
fd.close();
delete fp;
return true; return true;
} }
bool stackToFile(const Common::String &filename, const Variable &from) { bool CustomSaveHelper::stackToFile(const Common::String &filename, const Variable &from) {
#if 0 Common::OutSaveFile *fp = g_system->getSavefileManager()->openForSaving(filename);
FILE *fp = fopen(filename, saveEncoding ? "wb" : "wt"); if (fp == NULL) {
if (!fp) return fatal("Can't create file", filename); return fatal("Can't create file", filename);
}
VariableStack *hereWeAre = from.varData.theStack -> first; VariableStack *hereWeAre = from.varData.theStack -> first;
encode1 = (byte)saveEncoding & 255; _encode1 = (byte)_saveEncoding & 255;
encode2 = (byte)(saveEncoding >> 8); _encode2 = (byte)(_saveEncoding >> 8);
if (saveEncoding) { if (_saveEncoding) {
fprintf(fp, "[Custom data (encoded)]\r\n"); fp->writeString("[Custom data (encoded)]\r\n");
writeStringEncoded(UTF8_CHECKER, fp); writeStringEncoded(UTF8_CHECKER, fp);
} else { } else {
fprintf(fp, "[Custom data (ASCII)]\n"); fp->writeString("[Custom data (ASCII)]\n");
} }
while (hereWeAre) { while (hereWeAre) {
if (saveEncoding) { if (_saveEncoding) {
switch (hereWeAre -> thisVar.varType) { switch (hereWeAre -> thisVar.varType) {
case SVT_STRING: case SVT_STRING:
fputc(encode1, fp); fp->writeByte(_encode1);
writeStringEncoded(hereWeAre -> thisVar.varData.theString, fp); writeStringEncoded(hereWeAre -> thisVar.varData.theString, fp);
break; break;
case SVT_INT: case SVT_INT:
// Small enough to be stored as a char // Small enough to be stored as a char
if (hereWeAre -> thisVar.varData.intValue >= 0 && hereWeAre -> thisVar.varData.intValue < 256) { if (hereWeAre -> thisVar.varData.intValue >= 0 && hereWeAre -> thisVar.varData.intValue < 256) {
fputc(2 ^ encode1, fp); fp->writeByte(2 ^ _encode1);
fputc(hereWeAre -> thisVar.varData.intValue, fp); fp->writeByte(hereWeAre -> thisVar.varData.intValue);
} else { } else {
fputc(1 ^ encode1, fp); fp->writeByte(1 ^ _encode1);
fp->writeUint32LE(hereWeAre -> thisVar.varData.intValue); fp->writeUint32LE(hereWeAre -> thisVar.varData.intValue);
} }
break; break;
default: default:
fatal("Can't create an encoded custom data file containing anything other than numbers and strings", filename); fatal("Can't create an encoded custom data file containing anything other than numbers and strings", filename);
fclose(fp); delete fp;
return false; return false;
} }
} else { } else {
char *makeSureItsText = getTextFromAnyVar(hereWeAre -> thisVar); Common::String makeSureItsText = getTextFromAnyVar(hereWeAre -> thisVar);
if (makeSureItsText == NULL) break; if (makeSureItsText.empty())
fprintf(fp, "%s\n", makeSureItsText); break;
delete makeSureItsText; fp->writeString((makeSureItsText + "\n").c_str());
} }
hereWeAre = hereWeAre -> next; hereWeAre = hereWeAre -> next;
} }
fclose(fp);
#endif delete fp;
return true; return true;
} }

View file

@ -24,8 +24,26 @@
namespace Sludge { namespace Sludge {
bool fileToStack(const Common::String &filename, StackHandler *sH); struct StackHandler;
bool stackToFile(const Common::String &filename, const Variable &from); struct Variable;
class CustomSaveHelper {
public:
static bool fileToStack(const Common::String &filename, StackHandler *sH);
static bool stackToFile(const Common::String &filename, const Variable &from);
static uint16 _saveEncoding;
private:
static const char UTF8_CHECKER[];
static char _encode1;
static char _encode2;
static void writeStringEncoded(const Common::String checker, Common::WriteStream *stream);
static Common::String readStringEncoded(Common::SeekableReadStream *fp);
static char *readTextPlain(Common::SeekableReadStream *fp);
};
} // End of namespace Sludge } // End of namespace Sludge

View file

@ -41,6 +41,7 @@
#include "sludge/objtypes.h" #include "sludge/objtypes.h"
#include "sludge/people.h" #include "sludge/people.h"
#include "sludge/region.h" #include "sludge/region.h"
#include "sludge/savedata.h"
#include "sludge/statusba.h" #include "sludge/statusba.h"
#include "sludge/sprites.h" #include "sludge/sprites.h"
#include "sludge/sprbanks.h" #include "sludge/sprbanks.h"
@ -83,7 +84,6 @@ extern int lastFramesPerSecond;
extern bool allowAnyFilename; extern bool allowAnyFilename;
extern byte fadeMode; extern byte fadeMode;
extern uint16 saveEncoding;
const char *sludgeText[] = { "?????", "RETURN", "BRANCH", "BR_ZERO", const char *sludgeText[] = { "?????", "RETURN", "BRANCH", "BR_ZERO",
"SET_GLOBAL", "SET_LOCAL", "LOAD_GLOBAL", "LOAD_LOCAL", "PLUS", "MINUS", "SET_GLOBAL", "SET_LOCAL", "LOAD_GLOBAL", "LOAD_LOCAL", "PLUS", "MINUS",
@ -162,6 +162,8 @@ void initSludge() {
g_sludge->_soundMan->initSoundStuff(); g_sludge->_soundMan->initSoundStuff();
} }
CustomSaveHelper::_saveEncoding = false;
// global variables // global variables
numGlobals = 0; numGlobals = 0;
launchResult = nullptr; launchResult = nullptr;
@ -173,7 +175,6 @@ void initSludge() {
allUserFunc = allBIFNames = nullptr; allUserFunc = allBIFNames = nullptr;
brightnessLevel = 255; brightnessLevel = 255;
fadeMode = 2; fadeMode = 2;
saveEncoding = false;
} }
void killSludge() { void killSludge() {