Skip to content

Commit

Permalink
--mods support
Browse files Browse the repository at this point in the history
  • Loading branch information
afwbkbc committed Nov 16, 2024
1 parent 4e55a1c commit 1f8a588
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 8 deletions.
31 changes: 31 additions & 0 deletions src/config/Config.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <iostream>
#include <stdexcept>
#include <sstream>

#include "Config.h"

Expand Down Expand Up @@ -242,6 +243,32 @@ Config::Config( const int argc, const char* argv[] )
m_launch_flags |= LF_QUICKSTART_FACTION;
}
);
m_parser->AddRule(
"mods", "MODS", "Comma-separated list of mods to load", AH( this ) {
std::stringstream ss( value );
while ( ss.good() ) {
std::string mod_name;
getline( ss, mod_name, ',' );
if ( !mod_name.empty() ) {
const auto mod_path = util::FS::GeneratePath(
{
m_data_path,
"mods",
mod_name
}
);
if ( !util::FS::DirectoryExists( mod_path ) ) {
Error( "Mod path does not exist or is not a directory: " + mod_path );
}
m_mod_paths.push_back( mod_path );
}
}
if ( m_mod_paths.empty() ) {
Error( "No mod paths were defined" );
}
m_launch_flags |= LF_MODS;
}
);

#ifdef DEBUG
m_parser->AddRule(
Expand Down Expand Up @@ -405,6 +432,10 @@ const std::string& Config::GetQuickstartFaction() const {
return m_quickstart_faction;
}

const std::vector< std::string >& Config::GetModPaths() const {
return m_mod_paths;
}

#ifdef DEBUG

const bool Config::HasDebugFlag( const debug_flag_t flag ) const {
Expand Down
5 changes: 5 additions & 0 deletions src/config/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ CLASS( Config, common::Module )
LF_QUICKSTART_MAP_LIFEFORMS = 1 << 12,
LF_QUICKSTART_MAP_CLOUDS = 1 << 13,
LF_QUICKSTART_FACTION = 1 << 14,
LF_MODS = 1 << 15,
};

#ifdef DEBUG
Expand Down Expand Up @@ -81,6 +82,8 @@ CLASS( Config, common::Module )
const game::backend::settings::map_config_value_t GetQuickstartMapClouds() const;
const std::string& GetQuickstartFaction() const;

const std::vector< std::string >& GetModPaths() const;

#ifdef DEBUG

const bool HasDebugFlag( const debug_flag_t flag ) const;
Expand Down Expand Up @@ -121,6 +124,8 @@ CLASS( Config, common::Module )
game::backend::settings::map_config_value_t m_quickstart_map_clouds = game::backend::settings::MAP_CONFIG_CLOUDS_AVERAGE;
std::string m_quickstart_faction = "";

std::vector< std::string > m_mod_paths = {};

#ifdef DEBUG

uint16_t m_debug_flags = DF_NONE;
Expand Down
5 changes: 4 additions & 1 deletion src/game/backend/Bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ void Bindings::AddToContext( gse::context::Context* ctx ) {
}

void Bindings::RunMainScript() {
m_gse->GetInclude( m_gse_context, m_si_internal, m_entry_script );
m_gse->RunScript( m_gse_context, m_si_internal, m_entry_script );
for ( const auto& mod_path : g_engine->GetConfig()->GetModPaths() ) {
m_gse->RunScript( m_gse_context, m_si_internal, util::FS::GeneratePath({ mod_path, "main" }));
}
}

void Bindings::RunMain() {
Expand Down
4 changes: 4 additions & 0 deletions src/game/backend/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,10 @@ const size_t Game::GetSlotNum() const {

WRAPIMPL_BEGIN( Game, CLASS_GAME )
WRAPIMPL_PROPS
{
"year",
VALUE( gse::type::Int, 2100/*tmp*/ + m_current_turn.GetId() )
},
{
"random",
m_random->Wrap()
Expand Down
12 changes: 12 additions & 0 deletions src/game/backend/faction/FactionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ WRAPIMPL_BEGIN( FactionManager, CLASS_FM )
return faction->Wrap();
} )
},
{
"remove",
NATIVE_CALL( this ) {
N_EXPECT_ARGS( 1 );
N_GETVALUE( id, 0, String );
if ( !Get( id ) ) {
GSE_ERROR( gse::EC.GAME_ERROR, "Unknown faction: " + id );
}
Remove( id );
return VALUE( gse::type::Undefined );
} )
},
};
WRAPIMPL_END_PTR( FactionManager )

Expand Down
2 changes: 1 addition & 1 deletion src/gse/GSE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void GSE::Run() {
Log( "GSE finished" );
}

const Value GSE::GetInclude( context::Context* ctx, const si_t& si, const std::string& path ) {
const Value GSE::RunScript( context::Context* ctx, const si_t& si, const std::string& path ) {
const auto& it = m_include_cache.find( path );
if ( it != m_include_cache.end() ) {
return it->second.result;
Expand Down
2 changes: 1 addition & 1 deletion src/gse/GSE.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ CLASS( GSE, common::Class )
void AddModule( const std::string& path, type::Callable* module );

void Run();
const Value GetInclude( context::Context* ctx, const si_t& si, const std::string& path );
const Value RunScript( context::Context* ctx, const si_t& si, const std::string& path );

void SetGlobal( const std::string& identifier, Value variable );
const Value& GetGlobal( const std::string& identifier );
Expand Down
2 changes: 1 addition & 1 deletion src/gse/builtins/Include.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void Include::AddToContext( context::Context* ctx ) {
N_EXPECT_ARGS( 1 );
N_GETVALUE( path, 0, String );
const auto full_path = ctx->GetScriptInfo().directory + GSE::PATH_SEPARATOR + path;
return ctx->GetGSE()->GetInclude( ctx, call_si, full_path );
return ctx->GetGSE()->RunScript( ctx, call_si, full_path );
} ) );

}
Expand Down
30 changes: 29 additions & 1 deletion src/gse/type/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ static const std::unordered_map< Object::object_class_t, std::string > s_object_
Object::CLASS_EXCEPTION,
"#exception"
},
{
Object::CLASS_RANDOM,
"#random"
},
{
Object::CLASS_COLOR,
"#color"
Expand All @@ -39,6 +43,14 @@ static const std::unordered_map< Object::object_class_t, std::string > s_object_
Object::CLASS_GAME,
"#game"
},
{
Object::CLASS_RM,
"#rm"
},
{
Object::CLASS_TM,
"#tm"
},
{
Object::CLASS_TILE,
"#tile"
Expand All @@ -53,7 +65,11 @@ static const std::unordered_map< Object::object_class_t, std::string > s_object_
},
{
Object::CLASS_FM,
"#factions"
"#fm"
},
{
Object::CLASS_UM,
"#um"
},
{
Object::CLASS_UNITDEF,
Expand All @@ -63,10 +79,22 @@ static const std::unordered_map< Object::object_class_t, std::string > s_object_
Object::CLASS_UNIT,
"#unit"
},
{
Object::CLASS_BM,
"#bm"
},
{
Object::CLASS_BASE,
"#base"
},
{
Object::CLASS_BASE_POP,
"#basepop"
},
{
Object::CLASS_AM,
"#am"
}
};
const std::string& Object::GetClassString( const object_class_t object_class ) {
const auto& it = s_object_class_str.find( object_class );
Expand Down
1 change: 0 additions & 1 deletion src/gse/type/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class Object : public Type {
CLASS_SYSTEM,
CLASS_STATE,
CLASS_GAME,
CLASS_MAP,
CLASS_RM,
CLASS_TM,
CLASS_TILE,
Expand Down
16 changes: 14 additions & 2 deletions src/resource/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

#include "util/FS.h"

#include "engine/Engine.h"
#include "config/Config.h"

namespace resource {

ResourceManager::ResourceManager()
Expand Down Expand Up @@ -369,8 +372,17 @@ const std::string& ResourceManager::GetCustomPath( const std::string& path ) {
key.resize( path.length() );
std::transform( path.begin(), path.end(), key.begin(), ::tolower );

// look in datadir
auto resolved_file = util::FS::GetExistingCaseSensitivePath( m_data_path, path );
std::string resolved_file = "";

// look in mod dirs
for ( const auto& mod_path : g_engine->GetConfig()->GetModPaths() ) {
resolved_file = util::FS::GetExistingCaseSensitivePath( mod_path, path );
}

if ( resolved_file.empty() ) {
// look in datadir
resolved_file = util::FS::GetExistingCaseSensitivePath( m_data_path, path );
}

if ( resolved_file.empty() ) {

Expand Down

0 comments on commit 1f8a588

Please sign in to comment.