Skip to content

Commit

Permalink
Adding save file redirection, moving mod dll init to later.
Browse files Browse the repository at this point in the history
  • Loading branch information
MainMemory committed Sep 3, 2015
1 parent f51d27f commit ed1ae0c
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 5 deletions.
75 changes: 72 additions & 3 deletions SADXModLoader/dllmain.cpp
Expand Up @@ -716,6 +716,18 @@ static void ClearTrialSubgameList(unsigned char character)
_TrialSubgames[character] = vector<TrialLevelListEntry>();
}

static const char *mainsavepath = "SAVEDATA";
static const char *GetMainSavePath()
{
return mainsavepath;
}

static const char *chaosavepath = "SAVEDATA";
static const char *GetChaoSavePath()
{
return chaosavepath;
}

static const HelperFunctions helperFunctions =
{
ModLoaderVer,
Expand All @@ -732,7 +744,9 @@ static const HelperFunctions helperFunctions =
RegisterTrialLevel,
ClearTrialLevelList,
RegisterTrialSubgame,
ClearTrialSubgameList
ClearTrialSubgameList,
GetMainSavePath,
GetChaoSavePath
};

static vector<string> &split(const string &s, char delim, vector<string> &elems)
Expand Down Expand Up @@ -1870,6 +1884,10 @@ static void __cdecl InitMods(void)
// in order to handle multiple mods.
unordered_map<string, string> filereplaces;

vector<std::pair<ModInitFunc, string>> initfuncs;

string _mainsavepath, _chaosavepath;

// It's mod loading time!
PrintDebug("Loading mods...\n");
for (int i = 1; i < 999; i++)
Expand Down Expand Up @@ -1993,11 +2011,11 @@ static void __cdecl InitMods(void)
if (info->Init)
{
// TODO: Convert to Unicode later. (Will require an API bump.)
info->Init(mod_dirA.c_str(), helperFunctions);
initfuncs.push_back({ info->Init, mod_dirA });
}
const ModInitFunc init = (const ModInitFunc)GetProcAddress(module, "Init");
if (init)
init(mod_dirA.c_str(), helperFunctions);
initfuncs.push_back({ init, mod_dirA.c_str() });
const PatchList *patches = (const PatchList *)GetProcAddress(module, "Patches");
if (patches)
for (int j = 0; j < patches->Count; j++)
Expand Down Expand Up @@ -2099,6 +2117,12 @@ static void __cdecl InitMods(void)
}
}

if (modinfo->getBool("RedirectMainSave"))
_mainsavepath = mod_dirA + "\\SAVEDATA";

if (modinfo->getBool("RedirectChaoSave"))
_chaosavepath = mod_dirA + "\\SAVEDATA";

// Texture pack stuff
wstring modTextureDir = mod_dir + L"\\textures\\";
if (PathFileExists(modTextureDir.c_str()))
Expand Down Expand Up @@ -2205,6 +2229,51 @@ static void __cdecl InitMods(void)
TrialSubgames[i->first].Count = size;
}

if (!_mainsavepath.empty())
{
char *buf = new char[_mainsavepath.size() + 1];
strncpy(buf, _mainsavepath.c_str(), _mainsavepath.size() + 1);
mainsavepath = buf;
string tmp = "./" + _mainsavepath + "/%s";
buf = new char[tmp.size() + 1];
strncpy(buf, tmp.c_str(), tmp.size() + 1);
WriteData((char **)0x421E4E, buf);
WriteData((char **)0x421E6A, buf);
WriteData((char **)0x421F07, buf);
WriteData((char **)0x42214E, buf);
WriteData((char **)0x5050E5, buf);
WriteData((char **)0x5051ED, buf);
tmp = "./" + _mainsavepath + "/SonicDX%02d.snc";
buf = new char[tmp.size() + 1];
strncpy(buf, tmp.c_str(), tmp.size() + 1);
WriteData((char **)0x42210F, buf);
tmp = "./" + _mainsavepath + "/";
buf = new char[tmp.size() + 1];
strncpy(buf, tmp.c_str(), tmp.size() + 1);
WriteData((char **)0x422020, buf);
tmp = "./" + _mainsavepath + "/SonicDX??.snc";
buf = new char[tmp.size() + 1];
strncpy(buf, tmp.c_str(), tmp.size() + 1);
WriteData((char **)0x5050AB, buf);
}

if (!_chaosavepath.empty())
{
char *buf = new char[_chaosavepath.size() + 1];
strncpy(buf, _chaosavepath.c_str(), _chaosavepath.size() + 1);
chaosavepath = buf;
string tmp = "./" + _chaosavepath + "/SONICADVENTURE_DX_CHAOGARDEN.snc";
buf = new char[tmp.size() + 1];
strncpy(buf, tmp.c_str(), tmp.size() + 1);
WriteData((char **)0x7163EF, buf);
WriteData((char **)0x71AA6F, buf);
WriteData((char **)0x71ACDB, buf);
WriteData((char **)0x71ADC5, buf);
}

for (unsigned int i = 0; i < initfuncs.size(); i++)
initfuncs[i].first(initfuncs[i].second.c_str(), helperFunctions);

PrintDebug("Finished loading mods\n");

// Check for codes.
Expand Down
42 changes: 40 additions & 2 deletions SADXModLoader/include/SADXModLoader.h
Expand Up @@ -46,7 +46,7 @@ static inline void ResizeTextureList(NJS_TEXLIST *texlist, NJS_TEXNAME(&textures
// ModInfo

// SADX Mod Loader API version.
static const int ModLoaderVer = 3;
static const int ModLoaderVer = 4;

struct PatchInfo
{
Expand Down Expand Up @@ -107,7 +107,45 @@ struct HelperFunctions_v3
void (__cdecl *ClearTrialSubgameList)(unsigned char character);
};

typedef HelperFunctions_v3 HelperFunctions;
struct HelperFunctions_v4
{
// The version of the structure.
int Version;
// Registers a start position for a character.
void(__cdecl *RegisterStartPosition)(unsigned char character, const StartPosition &position);
// Clears the list of registered start positions for a character.
void(__cdecl *ClearStartPositionList)(unsigned char character);
// Registers a field start position for a character.
void(__cdecl *RegisterFieldStartPosition)(unsigned char character, const FieldStartPosition &position);
// Clears the list of registered field start positions for a character.
void(__cdecl *ClearFieldStartPositionList)(unsigned char character);
// Registers a path list.
void(__cdecl *RegisterPathList)(const PathDataPtr &paths);
// Clears the list of registered path lists.
void(__cdecl *ClearPathListList)();
// Registers a PVM file for a character.
void(__cdecl *RegisterCharacterPVM)(unsigned char character, const PVMEntry &pvm);
// Clears the list of registered PVM files for a character.
void(__cdecl *ClearCharacterPVMList)(unsigned char character);
// Registers a PVM file for a common object.
void(__cdecl *RegisterCommonObjectPVM)(const PVMEntry &pvm);
// Clears the list of registered PVM files for common objects.
void(__cdecl *ClearCommonObjectPVMList)();
// Registers a trial level entry for a character.
void(__cdecl *RegisterTrialLevel)(unsigned char character, const TrialLevelListEntry &level);
// Clears the list of registered trial level entries for a character.
void(__cdecl *ClearTrialLevelList)(unsigned char character);
// Registers a trial subgame entry for a character.
void(__cdecl *RegisterTrialSubgame)(unsigned char character, const TrialLevelListEntry &level);
// Clears the list of registered trial subgame entries for a character.
void(__cdecl *ClearTrialSubgameList)(unsigned char character);
// Returns the path where main game save files are stored.
const char *(__cdecl *GetMainSavePath)();
// Returns the path where Chao save files are stored.
const char *(__cdecl *GetChaoSavePath)();
};

typedef HelperFunctions_v4 HelperFunctions;

typedef void (__cdecl *ModInitFunc)(const char *path, const HelperFunctions &helperFunctions);

Expand Down

0 comments on commit ed1ae0c

Please sign in to comment.