Skip to content

Commit 6ac9ade

Browse files
committed
Type safe manual merge
1 parent 30bc2e1 commit 6ac9ade

10 files changed

Lines changed: 121 additions & 101 deletions

File tree

extension/src/openvic-extension/classes/GUILabel.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
#include "GUILabel.hpp"
22

3+
#include <optional>
4+
35
#include <godot_cpp/classes/font_file.hpp>
46
#include <godot_cpp/classes/style_box_texture.hpp>
57
#include <godot_cpp/core/error_macros.hpp>
68
#include <godot_cpp/variant/node_path.hpp>
79
#include <godot_cpp/variant/string_name.hpp>
810
#include <godot_cpp/variant/utility_functions.hpp>
911

12+
#include <openvic-simulation/types/TypedIndices.hpp>
13+
#include <openvic-simulation/types/TextFormat.hpp>
14+
1015
#include "openvic-extension/classes/GUINode.hpp"
1116
#include "openvic-extension/core/Convert.hpp"
1217
#include "openvic-extension/singletons/AssetManager.hpp"
1318
#include "openvic-extension/singletons/GameSingleton.hpp"
1419
#include "openvic-extension/core/Bind.hpp"
1520
#include "openvic-extension/utility/Utilities.hpp"
16-
#include "openvic-simulation/types/TextFormat.hpp"
1721

1822
using namespace OpenVic;
1923
using namespace godot;
@@ -690,7 +694,7 @@ void GUILabel::separate_currency_segments(
690694
GUILabel::flag_segment_t GUILabel::make_flag_segment(String const& identifier) {
691695
GameSingleton& game_singleton = *GameSingleton::get_singleton();
692696

693-
int32_t country_index = -1;
697+
std::optional<country_index_t> country_index = std::nullopt;
694698
StringName flag_type;
695699

696700
InstanceManager* instance_manager = game_singleton.get_instance_manager();
@@ -721,14 +725,14 @@ GUILabel::flag_segment_t GUILabel::make_flag_segment(String const& identifier) {
721725
}
722726

723727
// If no country with the given identifier can be found, fallback to country index 0 (usually REB) and empty flag type
724-
if (country_index < 0) {
728+
if (!country_index.has_value()) {
725729
UtilityFunctions::push_warning(
726730
"Failed to find country with identifier \"", identifier, "\" for GUILabel flag segment, falling back to index 0"
727731
);
728-
country_index = 0;
732+
country_index = country_index_t(0);
729733
}
730734

731-
const Rect2 flag_image_rect = game_singleton.get_flag_sheet_rect(country_index, flag_type);
735+
const Rect2 flag_image_rect = game_singleton.get_flag_sheet_rect(country_index.value(), flag_type);
732736
ERR_FAIL_COND_V(!flag_image_rect.has_area(), {});
733737

734738
flag_segment_t flag_segment;

extension/src/openvic-extension/components/budget/StrataTaxBudget.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "StrataTaxBudget.hpp"
22

33
#include <godot_cpp/variant/string.hpp>
4+
#include <type_safe/strong_typedef.hpp>
45

56
#include <openvic-simulation/country/CountryInstance.hpp>
67
#include "openvic-simulation/modifier/ModifierEffectCache.hpp"
@@ -23,8 +24,8 @@ StrataTaxBudget::StrataTaxBudget(
2324
parent,
2425
generate_slider_tooltip_localisation_key(new_strata),
2526
BALANCE,
26-
Utilities::format("./country_budget/tax_%d_slider", static_cast<uint64_t>(new_strata.index)),
27-
Utilities::format("./country_budget/tax_%d_inc", static_cast<uint64_t>(new_strata.index))
27+
Utilities::format("./country_budget/tax_%d_slider", static_cast<uint64_t>(type_safe::get(new_strata.index))),
28+
Utilities::format("./country_budget/tax_%d_inc", static_cast<uint64_t>(type_safe::get(new_strata.index)))
2829
),
2930
BudgetIncomeComponent(generate_summary_localisation_key(new_strata), 1),
3031
strata{new_strata},
@@ -37,7 +38,7 @@ StrataTaxBudget::StrataTaxBudget(
3738

3839
GUILabel::set_text_and_tooltip(
3940
parent,
40-
Utilities::format("./country_budget/tax_%d_desc", static_cast<uint64_t>(new_strata.index)),
41+
Utilities::format("./country_budget/tax_%d_desc", static_cast<uint64_t>(type_safe::get(new_strata.index))),
4142
generate_slider_tooltip_localisation_key(new_strata),
4243
Utilities::format(
4344
"TAX_%s_DESC",

extension/src/openvic-extension/singletons/GameSingleton.cpp

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
#include "GameSingleton.hpp"
22

3+
#include <cstdint>
34
#include <functional>
5+
#include <type_safe/strong_typedef.hpp>
46

57
#include <godot_cpp/classes/time.hpp>
68
#include <godot_cpp/core/error_macros.hpp>
79
#include <godot_cpp/variant/packed_string_array.hpp>
810
#include <godot_cpp/variant/utility_functions.hpp>
911

1012
#include <openvic-simulation/dataloader/ModManager.hpp>
13+
#include <openvic-simulation/DefinitionManager.hpp>
14+
#include <openvic-simulation/map/Crime.hpp>
15+
#include <openvic-simulation/types/TypedIndices.hpp>
1116
#include <openvic-simulation/utility/Containers.hpp>
1217
#include <openvic-simulation/utility/Logger.hpp>
1318

@@ -203,19 +208,21 @@ TypedArray<Dictionary> GameSingleton::get_bookmark_info() const {
203208
}
204209

205210
Error GameSingleton::setup_game(int32_t bookmark_index) {
206-
Bookmark const* bookmark =
207-
game_manager.get_definition_manager().get_history_manager().get_bookmark_manager().get_bookmark_by_index(bookmark_index
208-
);
211+
DefinitionManager const& definition_manager = game_manager.get_definition_manager();
212+
Bookmark const* bookmark = definition_manager.get_history_manager()
213+
.get_bookmark_manager()
214+
.get_bookmark_by_index(bookmark_index_t(bookmark_index));
209215
ERR_FAIL_NULL_V_MSG(bookmark, FAILED, Utilities::format("Failed to get bookmark with index: %d", bookmark_index));
210-
bool ret = game_manager.setup_instance(bookmark);
216+
bool ret = game_manager.setup_instance(*bookmark);
211217

212218
// TODO - remove this temporary crime assignment
213219
InstanceManager* instance_manager = get_instance_manager();
214220
ERR_FAIL_NULL_V_MSG(instance_manager, FAILED, "Failed to setup instance manager!");
221+
222+
CrimeManager const& crime_manager = definition_manager.get_crime_manager();
215223
for (ProvinceInstance& province : instance_manager->get_map_instance().get_province_instances()) {
216-
province.set_crime(get_definition_manager().get_crime_manager().get_crime_modifier_by_index(
217-
province.index % get_definition_manager().get_crime_manager().get_crime_modifier_count()
218-
));
224+
const crime_index_t crime_index = crime_index_t(type_safe::get(province.index) % crime_manager.get_crime_modifier_count());
225+
province.set_crime(crime_manager.get_crime_modifier_by_index(crime_index));
219226
}
220227

221228
ret &= MenuSingleton::get_singleton()->_population_menu_update_provinces() == OK;
@@ -314,17 +321,18 @@ Ref<ImageTexture> GameSingleton::get_flag_sheet_texture() const {
314321
return flag_sheet_texture;
315322
}
316323

317-
int32_t GameSingleton::get_flag_sheet_index(int32_t country_index, StringName const& flag_type) const {
324+
int32_t GameSingleton::get_flag_sheet_index(const country_index_t country_index, StringName const& flag_type) const {
325+
const uint64_t index = static_cast<uint64_t>(type_safe::get(country_index));
318326
ERR_FAIL_COND_V_MSG(
319-
country_index < 0 ||
320-
country_index >= get_definition_manager().get_country_definition_manager().get_country_definition_count(),
321-
-1, Utilities::format("Invalid country index: %d", country_index)
327+
index < 0 ||
328+
index >= get_definition_manager().get_country_definition_manager().get_country_definition_count(),
329+
-1, Utilities::format("Invalid country index: %d", index)
322330
);
323331

324332
const typename decltype(flag_type_index_map)::const_iterator it = flag_type_index_map.find(flag_type);
325333
ERR_FAIL_COND_V_MSG(it == flag_type_index_map.end(), -1, Utilities::format("Invalid flag type %s", flag_type));
326334

327-
return flag_type_index_map.size() * country_index + it->second;
335+
return flag_type_index_map.size() * index + it->second;
328336
}
329337

330338
Rect2i GameSingleton::get_flag_sheet_rect(int32_t flag_index) const {
@@ -335,7 +343,7 @@ Rect2i GameSingleton::get_flag_sheet_rect(int32_t flag_index) const {
335343
return { Vector2i { flag_index % flag_sheet_dims.x, flag_index / flag_sheet_dims.x } * flag_dims, flag_dims };
336344
}
337345

338-
Rect2i GameSingleton::get_flag_sheet_rect(int32_t country_index, StringName const& flag_type) const {
346+
Rect2i GameSingleton::get_flag_sheet_rect(const country_index_t country_index, StringName const& flag_type) const {
339347
return get_flag_sheet_rect(get_flag_sheet_index(country_index, flag_type));
340348
}
341349

@@ -408,9 +416,7 @@ TypedArray<Dictionary> GameSingleton::get_province_names() const {
408416
TypedArray<Dictionary> ret;
409417
ERR_FAIL_COND_V(ret.resize(map_definition.get_province_definition_count()) != OK, {});
410418

411-
for (int32_t index = 0; index < map_definition.get_province_definition_count(); ++index) {
412-
ProvinceDefinition const& province = map_definition.get_province_definitions()[index];
413-
419+
for (ProvinceDefinition const& province : map_definition.get_province_definitions()) {
414420
Dictionary province_dict;
415421

416422
province_dict[identifier_key] = convert_to<String>(province.get_identifier());
@@ -426,7 +432,7 @@ TypedArray<Dictionary> GameSingleton::get_province_names() const {
426432
province_dict[scale_key] = scale;
427433
}
428434

429-
ret[index] = std::move(province_dict);
435+
ret[type_safe::get(province.index)] = std::move(province_dict);
430436
}
431437

432438
return ret;
@@ -437,31 +443,37 @@ int32_t GameSingleton::get_mapmode_count() const {
437443
}
438444

439445
String GameSingleton::get_mapmode_identifier(int32_t index) const {
440-
Mapmode const* identifier_mapmode = get_definition_manager().get_mapmode_manager().get_mapmode_by_index(index);
446+
Mapmode const* identifier_mapmode = get_definition_manager()
447+
.get_mapmode_manager()
448+
.get_mapmode_by_index(map_mode_index_t(index));
441449
if (identifier_mapmode != nullptr) {
442450
return convert_to<String>(identifier_mapmode->get_identifier());
443451
}
444452
return String {};
445453
}
446454

447455
String GameSingleton::get_mapmode_localisation_key(int32_t index) const {
448-
Mapmode const* localisation_key_mapmode = get_definition_manager().get_mapmode_manager().get_mapmode_by_index(index);
456+
Mapmode const* localisation_key_mapmode = get_definition_manager()
457+
.get_mapmode_manager()
458+
.get_mapmode_by_index(map_mode_index_t(index));
449459
if (localisation_key_mapmode != nullptr) {
450460
return convert_to<String>(localisation_key_mapmode->get_localisation_key());
451461
}
452462
return String {};
453463
}
454464

455465
int32_t GameSingleton::get_current_mapmode_index() const {
456-
return mapmode->index;
466+
return type_safe::get(mapmode->index);
457467
}
458468

459469
Error GameSingleton::set_mapmode(int32_t index) {
460-
Mapmode const* new_mapmode = get_definition_manager().get_mapmode_manager().get_mapmode_by_index(index);
470+
Mapmode const* new_mapmode = get_definition_manager()
471+
.get_mapmode_manager()
472+
.get_mapmode_by_index(map_mode_index_t(index));
461473
ERR_FAIL_NULL_V_MSG(new_mapmode, FAILED, Utilities::format("Failed to find mapmode with index: %d", index));
462474
mapmode = new_mapmode;
463475
const Error err = _update_colour_image();
464-
emit_signal(_signal_mapmode_changed(), mapmode->index);
476+
emit_signal(_signal_mapmode_changed(), static_cast<uint64_t>(type_safe::get(mapmode->index)));
465477
return err;
466478
}
467479

extension/src/openvic-extension/singletons/GameSingleton.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <openvic-simulation/GameManager.hpp>
88
#include <openvic-simulation/dataloader/Dataloader.hpp>
9+
#include <openvic-simulation/types/TypedIndices.hpp>
910

1011
namespace OpenVic {
1112

@@ -106,9 +107,9 @@ namespace OpenVic {
106107

107108
/* The index of the flag in the flag sheet corresponding to the requested country / flag_type
108109
* combination, or -1 if no such flag can be found. */
109-
int32_t get_flag_sheet_index(int32_t country_index, godot::StringName const& flag_type) const;
110+
int32_t get_flag_sheet_index(const country_index_t country_index, godot::StringName const& flag_type) const;
110111
godot::Rect2i get_flag_sheet_rect(int32_t flag_index) const;
111-
godot::Rect2i get_flag_sheet_rect(int32_t country_index, godot::StringName const& flag_type) const;
112+
godot::Rect2i get_flag_sheet_rect(const country_index_t country_index, godot::StringName const& flag_type) const;
112113

113114
/* Number of (vertical, horizontal) subdivisions the province shape image
114115
* was split into when making the province_shape_texture to ensure no

extension/src/openvic-extension/singletons/MapItemSingleton.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "MapItemSingleton.hpp"
22

3+
#include <type_safe/strong_typedef.hpp>
4+
35
#include <openvic-simulation/utility/Containers.hpp>
46

57
#include "godot_cpp/core/error_macros.hpp"
@@ -226,7 +228,9 @@ PackedByteArray MapItemSingleton::get_rgo_icons() const {
226228
}
227229

228230
GoodDefinition const* rgo_good = prov_inst.get_rgo_good();
229-
icons[index++] = rgo_good != nullptr ? rgo_good->index + 1 : 0; // 0 if no rgo good in the province
231+
icons[index++] = rgo_good != nullptr
232+
? type_safe::get(rgo_good->index) + 1
233+
: 0; // 0 if no rgo good in the province
230234
}
231235

232236
return icons;

extension/src/openvic-extension/singletons/MenuSingleton.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "MenuSingleton.hpp"
22

33
#include <span>
4+
#include <type_safe/strong_typedef.hpp>
45

56
#include <godot_cpp/variant/utility_functions.hpp>
67

@@ -539,7 +540,7 @@ Dictionary MenuSingleton::get_province_info_from_number(int32_t province_number)
539540
ProductionType const& production_type = *rgo.get_production_type_nullable();
540541
GoodDefinition const& rgo_good = *province->get_rgo_good();
541542

542-
ret[province_info_rgo_icon_key] = static_cast<int32_t>(rgo_good.index);
543+
ret[province_info_rgo_icon_key] = static_cast<uint64_t>(type_safe::get(rgo_good.index));
543544

544545
ret[province_info_rgo_output_quantity_yesterday_key] = static_cast<real_t>(rgo.get_output_quantity_yesterday());
545546
ret[province_info_rgo_revenue_yesterday_key] = static_cast<real_t>(rgo.get_revenue_yesterday());

extension/src/openvic-extension/singletons/MilitaryMenu.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <godot_cpp/variant/utility_functions.hpp>
44

55
#include <openvic-simulation/military/UnitInstanceGroup.hpp>
6+
#include <openvic-simulation/types/TypedIndices.hpp>
67
#include <openvic-simulation/types/UnitBranchType.hpp>
78

89
#include "openvic-extension/classes/GUINode.hpp"
@@ -308,16 +309,16 @@ Dictionary MenuSingleton::make_in_progress_unit_dict() const {
308309
const fixed_point_t progress = fixed_point_t::_0_50;
309310
const ordered_map<GoodDefinition const*, std::pair<fixed_point_t, fixed_point_t>> required_goods {
310311
{
311-
good_definition_manager.get_good_definition_by_index(0),
312+
good_definition_manager.get_good_definition_by_index(good_index_t(0)),
312313
{ fixed_point_t::parse(1234) / 100, fixed_point_t::parse(1900) / 100 }
313314
}, {
314-
good_definition_manager.get_good_definition_by_index(1),
315+
good_definition_manager.get_good_definition_by_index(good_index_t(1)),
315316
{ fixed_point_t::parse(888) / 100, fixed_point_t::parse(1444) / 100 }
316317
}, {
317-
good_definition_manager.get_good_definition_by_index(2),
318+
good_definition_manager.get_good_definition_by_index(good_index_t(2)),
318319
{ fixed_point_t::parse(1622) / 100, fixed_point_t::parse(1622) / 100 }
319320
}, {
320-
good_definition_manager.get_good_definition_by_index(3),
321+
good_definition_manager.get_good_definition_by_index(good_index_t(3)),
321322
{ fixed_point_t::parse(211) / 100, fixed_point_t::parse(805) / 100 }
322323
}
323324
};

0 commit comments

Comments
 (0)