Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/custom mix loading #7

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
### Custom mixes

Mixes can be preloaded and postloaded. Preload mixes will overwrite all content of original mix files and postloaded mix files.
Postloaded mixes will be overwritten by any other mix files content (include original files).

> Overwite priority it's loading order:\
Preload mixes -> Original mixes -> Postload mixes

Configuration file (spawn.ini) allow to configure mix loading via two sections: *mixes_preload* for preloading, *mixes_postload* for postloading.
This section it is just a sorted list of filenames.

Example:

> [mixes_preload] \
1=HTNK.mix \
0=HTK.mix

The first will be HTK.mix and then HTNK.mix - this list will sorted in ascending order.
44 changes: 35 additions & 9 deletions src/Spawner/CustomMixes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,57 @@
* You should have received a copy of the GNU General Public License
* along with this program.If not, see <http://www.gnu.org/licenses/>.
*/
#include "Spawner.h"
#include "Ra2Mode.h"

#include <Utilities/Debug.h>
#include <Utilities/Macro.h>

#include <MixFileClass.h>

MixFileClass *Ra2ModeMIX = nullptr;
#include <list>

static std::list<MixFileClass*> CustomMixes = { };
Multfinite marked this conversation as resolved.
Show resolved Hide resolved

inline void FreeMixes(std::list<MixFileClass*>& mixes)
{
for (auto pMix : CustomMixes)
GameDelete(pMix);
CustomMixes.clear();
}
Multfinite marked this conversation as resolved.
Show resolved Hide resolved

DEFINE_HOOK(0x6BE9BD, ProgEnd_CustomMixes, 0x6)
{
if (Ra2ModeMIX)
FreeMixes(CustomMixes);
Multfinite marked this conversation as resolved.
Show resolved Hide resolved
return 0;
}

DEFINE_HOOK(0x5301AC, InitBootstrapMixfiles_CustomMixes_Preload, 0x5)
{
FreeMixes(CustomMixes);
Multfinite marked this conversation as resolved.
Show resolved Hide resolved

auto config = Spawner::GetConfig();
for (auto& pair : config->PreloadMixes)
Multfinite marked this conversation as resolved.
Show resolved Hide resolved
{
GameDelete(Ra2ModeMIX);
Ra2ModeMIX = nullptr;
CustomMixes.push_back(GameCreate<MixFileClass>(pair.second.c_str()));
Debug::Log(" %s ", pair.second.c_str());
Multfinite marked this conversation as resolved.
Show resolved Hide resolved
}

// Any 'mode' mixes should be loaded after user custom mixes to prevent overload it.
if (Ra2Mode::IsEnabled())
CustomMixes.push_back(GameCreate<MixFileClass>(Ra2Mode::MixFileName));
Multfinite marked this conversation as resolved.
Show resolved Hide resolved

return 0;
}

DEFINE_HOOK(0x5301AC, InitBootstrapMixfiles_CustomMixes, 0x5)
DEFINE_HOOK_AGAIN(0x5302E4, InitBootstrapMixfiles_CustomMixes_Postload, 0x9)
DEFINE_HOOK(0x53044A, InitBootstrapMixfiles_CustomMixes_Postload, 0x9)
Multfinite marked this conversation as resolved.
Show resolved Hide resolved
{
ProgEnd_CustomMixes(R);

if (Ra2Mode::IsEnabled())
auto config = Spawner::GetConfig();
for (auto& pair : config->PostloadMixes)
Multfinite marked this conversation as resolved.
Show resolved Hide resolved
{
Ra2ModeMIX = GameCreate<MixFileClass>("ra2mode.mix");
CustomMixes.push_back(GameCreate<MixFileClass>(pair.second.c_str()));
Debug::Log(" %s ", pair.second.c_str());
Multfinite marked this conversation as resolved.
Show resolved Hide resolved
}

return 0;
Expand Down
2 changes: 2 additions & 0 deletions src/Spawner/Ra2Mode.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Ra2Mode
{
static bool Enabled;
public:
static constexpr const char* MixFileName = "ra2mode.mix";

static bool IsEnabled()
Multfinite marked this conversation as resolved.
Show resolved Hide resolved
{
return Ra2Mode::Enabled;
Expand Down
37 changes: 37 additions & 0 deletions src/Spawner/Spawner.Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,31 @@
#include "Spawner.Config.h"

#include <CCINIClass.h>
#include <MixFileClass.h>

constexpr const char* NONE_STR = "<none>";
Multfinite marked this conversation as resolved.
Show resolved Hide resolved

inline void ReadFiles(CCINIClass* pINI, const char* pSection, std::list<std::pair<int, std::string>>& files)
{
if (!pINI->GetSection(pSection))
return;

const int itemsCount = pINI->GetKeyCount(pSection);
for (int i = 0; i < itemsCount; ++i)
{
auto pKey = pINI->GetKeyName(pSection, i);
char* pEnd = nullptr;
auto index = std::strtol(pKey, &pEnd, 10);
if (pEnd == pKey || *pEnd != 0)
continue;

char fileName[0x80];
pINI->ReadString(pSection, pKey, "", fileName);

if (fileName[0] != 0 || _strcmpi(fileName, NONE_STR) != 0)
files.emplace_back(index, fileName);
}
}
Multfinite marked this conversation as resolved.
Show resolved Hide resolved

void SpawnerConfig::LoadFromINIFile(CCINIClass* pINI)
{
Expand Down Expand Up @@ -108,6 +133,18 @@ void SpawnerConfig::LoadFromINIFile(CCINIClass* pINI)
// TODO:
// QuickMatch = pINI->ReadBool(pSettingsSection, "QuickMatch", QuickMatch);
// RunAutoSS = pINI->ReadBool(pSettingsSection, "RunAutoSS", RunAutoSS);

// Custom Mixes
ReadFiles(pINI, "mixes_preload", PreloadMixes);
ReadFiles(pINI, "mixes_postload", PostloadMixes);
Multfinite marked this conversation as resolved.
Show resolved Hide resolved

auto predicate = [](std::pair<int, std::string> const& a, std::pair<int, std::string> const& b) -> bool
{
return a.first < b.first;
};

PreloadMixes.sort(predicate);
PostloadMixes.sort(predicate);
Multfinite marked this conversation as resolved.
Show resolved Hide resolved
}

constexpr char* PlayerSectionArray[8] = {
Expand Down
9 changes: 9 additions & 0 deletions src/Spawner/Spawner.Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#pragma once
#include <Main.h>
#include <list>

class CCINIClass;

Expand Down Expand Up @@ -137,6 +138,10 @@ class SpawnerConfig
// bool QuickMatch;
// bool RunAutoSS;

// Custom mixes
std::list<std::pair<int, std::string>> PreloadMixes;
std::list<std::pair<int, std::string>> PostloadMixes;
Multfinite marked this conversation as resolved.
Show resolved Hide resolved

SpawnerConfig() // default values
// Game Mode Options
: MPModeIndex { 1 }
Expand Down Expand Up @@ -224,6 +229,10 @@ class SpawnerConfig
// TODO:
// , QuickMatch { false }
// , RunAutoSS { false }

// Custom Mixes
, PreloadMixes()
, PostloadMixes()
{ }

void LoadFromINIFile(CCINIClass* pINI);
Expand Down