Dynamically check generator registration on any use.

This helps find when there's string mismatches and/or forgotten
registrations.
This commit is contained in:
2024-04-26 22:13:55 -04:00
parent 4f1b12d4c5
commit ef18781091
3 changed files with 52 additions and 2 deletions
+5
View File
@@ -608,6 +608,11 @@ STAR_DESC starmap_array[] =
{{MAX_X_UNIVERSE << 1, MAX_Y_UNIVERSE << 1}, 0, 0, 0, 0}, {{MAX_X_UNIVERSE << 1, MAX_Y_UNIVERSE << 1}, 0, 0, 0, 0},
}; };
STAR_DESC *
GetAllStars( void )
{
return starmap_array;
}
COUNT COUNT
GetStarCount( void ) GetStarCount( void )
+44 -1
View File
@@ -28,6 +28,8 @@
#include "uqm/planets/generate/gendefault.h" #include "uqm/planets/generate/gendefault.h"
#include "uqm/gendef.h" #include "uqm/gendef.h"
#include "uqm/planets/planets.h"
#include "uqm/starmap.h"
/* /*
* XXX: Note that the way that defaults get called via C++ is a bit * XXX: Note that the way that defaults get called via C++ is a bit
@@ -63,6 +65,17 @@
namespace Planets ::detail:: Generate_m namespace Planets ::detail:: Generate_m
{ {
using namespace std::literals::string_literals;
namespace
{
namespace C
{
const bool debug= true;
const bool debugGenerationByValidatingAllStars= false or C::debug;
}
}
Generator::~Generator()= default; Generator::~Generator()= default;
@@ -203,25 +216,55 @@ namespace Planets ::detail:: Generate_m
void void
Generator::add( const std::string &name, std::unique_ptr< Generator > gen ) Generator::add( const std::string &name, std::unique_ptr< Generator > gen )
{ {
if( C::debug )
{
std::cerr << "Attempting to register `" << name << "` generator." << std::endl;
}
if( registry().count( name ) ) if( registry().count( name ) )
{ {
throw std::runtime_error( "Double registration of `" + name + "`" ); throw std::runtime_error( "Double registration of `" + name + "`" );
} }
registry()[ name ]= std::move( gen ); registry()[ name ]= std::move( gen );
} }
namespace
{
void
validateAllStars()
{
for( int i= 0; i < GetStarCount(); ++i )
{
const auto &star= GetAllStars()[ i ];
if( star.supplement == nullptr ) continue;
std::cerr << "Checking if required generator `" << star.supplement << "` is "
<< "present...";
if( not registry().count( star.supplement ) )
{
std::cerr << " NO!!!!" << std::endl;
throw std::runtime_error{ "Solar system generator `"s + star.supplement
+ "` was needed but not found" };
}
std::cerr << " yes" << std::endl;
}
}
}
} }
extern "C" extern "C"
{ {
using Planets::SolarSystem; using Planets::SolarSystem;
using Planets::detail::Generate_m::registry; using namespace Planets::detail::Generate_m;
extern GenerateFunctions *getGenerateCxxFunctions( void ); extern GenerateFunctions *getGenerateCxxFunctions( void );
void void
SetupGeneratorForSolarSystem( STAR_DESC *const desc, SolarSystem *const solarSys ) SetupGeneratorForSolarSystem( STAR_DESC *const desc, SolarSystem *const solarSys )
{ {
if( C::debugGenerationByValidatingAllStars ) validateAllStars();
// Must either have a legacy constructor or a named lookup, // Must either have a legacy constructor or a named lookup,
// but not both. // but not both.
+2
View File
@@ -115,6 +115,8 @@ typedef struct solarsys_state SOLARSYS_STATE;
extern "C" { extern "C" {
#endif #endif
extern STAR_DESC *GetAllStars( void );
struct planet_desc struct planet_desc
{ {
DWORD rand_seed; DWORD rand_seed;