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 all 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: *PreloadMixes* for preloading, *PostloadMixes* for postloading.
This section it is just a sorted list of filenames.

Example:

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

The first will be HTK.mix and then HTNK.mix - this list will sorted in ascending order.
43 changes: 34 additions & 9 deletions src/Spawner/CustomMixes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,56 @@
* 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 = { };

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& mixName : config->PreloadMixes)
{
GameDelete(Ra2ModeMIX);
Ra2ModeMIX = nullptr;
CustomMixes.push_back(GameCreate<MixFileClass>(mixName.c_str()));
Debug::Log("%s ", mixName.c_str());
}

// Any 'mode' mixes should be loaded after user custom mixes to prevent overload it.
if (Ra2Mode::IsEnabled())
CustomMixes.push_back(GameCreate<MixFileClass>("ra2mode.mix"));

return 0;
}

DEFINE_HOOK(0x5301AC, InitBootstrapMixfiles_CustomMixes, 0x5)
DEFINE_HOOK(0x53044A, InitBootstrapMixfiles_CustomMixes_Postload, 0x10)
{
ProgEnd_CustomMixes(R);

if (Ra2Mode::IsEnabled())
auto config = Spawner::GetConfig();
for (auto& mixName : config->PostloadMixes)
{
Ra2ModeMIX = GameCreate<MixFileClass>("ra2mode.mix");
CustomMixes.push_back(GameCreate<MixFileClass>(mixName.c_str()));
Debug::Log("%s ", mixName.c_str());
}

return 0;
Expand Down
22 changes: 22 additions & 0 deletions src/Spawner/Spawner.Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@
#include "Spawner.Config.h"

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

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

const int itemsCount = pINI->GetKeyCount(pSection);
for (int i = 0; i < itemsCount; ++i)
{
auto pKey = pINI->GetKeyName(pSection, i);
std::string& str = strings.emplace_back(); str.resize(0x80);
pINI->ReadString(pSection, pKey, NONE_STR, (char*) str.c_str(), str.size()); str.resize(strlen(str.c_str()));

if (str == NONE_STR)
strings.remove(str);
}
}

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

// Custom Mixes
ReadListFromSection(pINI, "PreloadMixes", PreloadMixes);
ReadListFromSection(pINI, "PostloadMixes", PostloadMixes);
}

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

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

class CCINIClass;

constexpr const char* NONE_STR = "<none>";

class SpawnerConfig
{

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

// Custom mixes
// Note: std::list and std::string will be realised followed to RAII concept. It is pretty save instead of const char*.

std::list<std::string> PreloadMixes;
std::list<std::string> PostloadMixes;

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

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

void LoadFromINIFile(CCINIClass* pINI);
Expand Down