Skip to content

Commit 5e55672

Browse files
committed
Add clang-format to pre-commit
Add SConstruct and SCsub to ruff/ruff-format pre-commit Standardize header consistency: dependency headers should include with `<...>` local headers should include with `"..."` Standardize GDCLASS terminated with a semicolon Replace camelCase variable names with snake_case
1 parent 35eba11 commit 5e55672

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1117
-1169
lines changed

.pre-commit-config.yaml

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ default_language_version:
22
python: python3
33

44
repos:
5-
# Waiting on Hop311 to approve clang-format
6-
# - repo: https://github.com/pre-commit/mirrors-clang-format
7-
# rev: v19.1.3
8-
# hooks:
9-
# - id: clang-format
10-
# files: \.(c|h|cpp|hpp|inc)$
11-
# types_or: [text]
12-
# # exclude: |
13-
# # (?x)^(
14-
# # )
5+
- repo: https://github.com/pre-commit/mirrors-clang-format
6+
rev: v19.1.3
7+
hooks:
8+
- id: clang-format
9+
files: \.(c|h|cpp|hpp|inc)$
10+
types_or: [text]
11+
exclude: |
12+
(?x)^(
13+
extension/src/gen/.*
14+
)
1515
1616
- repo: https://github.com/pocc/pre-commit-hooks
1717
rev: v1.3.5
@@ -25,19 +25,28 @@ repos:
2525
stages: [manual] # Not automatically triggered, invoked via `pre-commit run --hook-stage manual clang-tidy`
2626

2727
- repo: https://github.com/astral-sh/ruff-pre-commit
28-
rev: v0.7.3
28+
rev: v0.11.13
2929
hooks:
3030
- id: ruff
3131
args: [--fix]
32+
files: (\.py|SConstruct|SCsub)$
33+
exclude: |
34+
(?x)^(
35+
extension/doc_tools/make_rst.py|
36+
extension/doc_tools/doc_status.py
37+
)
38+
types_or: [text]
3239
- id: ruff-format
40+
files: (\.py|SConstruct|SCsub)$
3341
exclude: |
3442
(?x)^(
3543
extension/doc_tools/make_rst.py|
3644
extension/doc_tools/doc_status.py
3745
)
46+
types_or: [text]
3847

3948
- repo: https://github.com/pre-commit/mirrors-mypy
40-
rev: v1.13.0
49+
rev: v1.14.1
4150
hooks:
4251
- id: mypy
4352
files: \.py$

SConstruct

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#!/usr/bin/env python
22

33
import os
4-
import platform
5-
import sys
64

75
import SCons
86

@@ -21,7 +19,7 @@ env.FinalizeOptions()
2119
OLD_ARGS = SCons.Script.ARGUMENTS.copy()
2220
SCons.Script.ARGUMENTS["use_static_cpp"] = env["use_static_cpp"]
2321
SCons.Script.ARGUMENTS["disable_exceptions"] = env["disable_exceptions"]
24-
SCons.Script.ARGUMENTS["compiledb_file"] = 'godot-cpp/compile_commands.json'
22+
SCons.Script.ARGUMENTS["compiledb_file"] = "godot-cpp/compile_commands.json"
2523
godot_env = SConscript("godot-cpp/SConstruct")
2624
SCons.Script.ARGUMENTS = OLD_ARGS
2725

@@ -32,6 +30,7 @@ env.Prepend(LIBS=godot_env["LIBS"])
3230

3331
SConscript("extension/deps/SCsub", "env")
3432

33+
3534
def version_git_info_builder(target, source, env):
3635
with open(str(target[0]), "wt", encoding="utf-8", newline="\n") as file:
3736
file.write("/* THIS FILE IS GENERATED. EDITS WILL BE LOST. */\n\n")
@@ -51,7 +50,12 @@ namespace OpenVic {{
5150
""".format(**source[0].read())
5251
)
5352

54-
result = env.Command("extension/src/gen/git_info.gen.hpp", env.Value(env.get_git_info()), env.Action(version_git_info_builder, "$GENCOMSTR"))
53+
54+
result = env.Command(
55+
"extension/src/gen/git_info.gen.hpp",
56+
env.Value(env.get_git_info()),
57+
env.Action(version_git_info_builder, "$GENCOMSTR"),
58+
)
5559
env.NoCache(result)
5660
Default(result)
5761

@@ -77,13 +81,15 @@ if env["target"] in ["editor", "template_debug"]:
7781
# Remove unassociated intermediate binary files if allowed, usually the result of a renamed or deleted source file
7882
if env["intermediate_delete"]:
7983
from glob import glob
80-
def remove_extension(file : str):
81-
if file.find(".") == -1: return file
82-
return file[:file.rindex(".")]
84+
85+
def remove_extension(file: str):
86+
if file.find(".") == -1:
87+
return file
88+
return file[: file.rindex(".")]
8389

8490
found_one = False
8591
for path in paths:
86-
for obj_file in [file[:-len(".os")] for file in glob(path + "*.os", recursive=True)]:
92+
for obj_file in [file[: -len(".os")] for file in glob(path + "*.os", recursive=True)]:
8793
found = False
8894
for source_file in sources:
8995
if remove_extension(str(source_file)) == obj_file:
@@ -93,12 +99,13 @@ if env["intermediate_delete"]:
9399
if not found_one:
94100
found_one = True
95101
print("Unassociated intermediate files found...")
96-
print("Removing "+obj_file+".os")
97-
os.remove(obj_file+".os")
102+
print("Removing " + obj_file + ".os")
103+
os.remove(obj_file + ".os")
98104

99105
if env["platform"] == "macos":
100106
library = env.SharedLibrary(
101-
BINDIR + "/openvic/libopenvic.{}.{}.framework/libopenvic.{}.{}".format(
107+
BINDIR
108+
+ "/openvic/libopenvic.{}.{}.framework/libopenvic.{}.{}".format(
102109
godot_env["platform"], godot_env["target"], godot_env["platform"], godot_env["target"]
103110
),
104111
source=sources,

extension/deps/SCsub

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22

33
Import("env")
44

5+
56
def build_openvic_simulation(env):
67
ovsim_env = SConscript("openvic-simulation/SConstruct")
78
env.Append(LIBPATH=ovsim_env.openvic_simulation["LIBPATH"])
89
env.Prepend(LIBS=ovsim_env.openvic_simulation["LIBS"])
910
env.Append(CPPPATH=ovsim_env.openvic_simulation["INCPATH"])
1011
env.openvic_simulation = ovsim_env.openvic_simulation
1112

13+
1214
def build_gli(env):
1315
gli_includes = ["gli", "gli/external"]
1416
env.gli_loader = {}
1517
env.gli_loader["INCPATH"] = [env.Dir(p) for p in gli_includes]
1618
env.Append(CPPPATH=env.gli_loader["INCPATH"])
1719

20+
1821
build_openvic_simulation(env)
1922
build_gli(env)

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

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ void GFXCorneredTileSupportingTexture::draw_rect_cornered(RID const& to_canvas_i
2626
if (rendering_server != nullptr) {
2727
const Size2 size = get_size();
2828
rendering_server->canvas_item_add_nine_patch(
29-
to_canvas_item, rect, { {}, size }, get_rid(),
30-
cornered_tile_border_size, size - cornered_tile_border_size
29+
to_canvas_item, rect, { {}, size }, get_rid(), cornered_tile_border_size, size - cornered_tile_border_size
3130
);
3231
}
3332
} else {
@@ -69,8 +68,7 @@ Ref<GFXButtonStateTexture> GFXButtonStateTexture::make_gfx_button_state_texture(
6968

7069
void GFXButtonStateTexture::set_button_state(ButtonState new_button_state) {
7170
ERR_FAIL_COND(
72-
new_button_state != HOVER && new_button_state != PRESSED &&
73-
new_button_state != DISABLED && new_button_state != SELECTED
71+
new_button_state != HOVER && new_button_state != PRESSED && new_button_state != DISABLED && new_button_state != SELECTED
7472
);
7573
button_state = new_button_state;
7674
}
@@ -82,9 +80,9 @@ Error GFXButtonStateTexture::generate_state_image(
8280
const Rect2i source_image_rect { {}, source_image->get_size() };
8381
ERR_FAIL_COND_V(!region.has_area() || !source_image_rect.encloses(region), FAILED);
8482
/* Whether we've already set the ImageTexture to an image of the right dimensions and format,
85-
* and so can update it without creating and setting a new image, or not. */
86-
bool can_update = state_image.is_valid() && state_image->get_size() == region.get_size()
87-
&& state_image->get_format() == source_image->get_format();
83+
* and so can update it without creating and setting a new image, or not. */
84+
bool can_update = state_image.is_valid() && state_image->get_size() == region.get_size() &&
85+
state_image->get_format() == source_image->get_format();
8886
if (!can_update) {
8987
state_image = Image::create(region.size.width, region.size.height, false, source_image->get_format());
9088
ERR_FAIL_NULL_V(state_image, FAILED);
@@ -114,11 +112,14 @@ Error GFXButtonStateTexture::generate_state_image(
114112
return { std::max(colour.r - 0.3f, 0.0f), std::max(colour.g - 0.3f, 0.0f), std::max(colour.b - 0.3f, 0.0f), colour.a };
115113
};
116114

117-
const auto colour_func =
118-
button_state == HOVER ? hover_colour :
119-
button_state == PRESSED ? pressed_colour :
120-
button_state == DISABLED ? disabled_colour :
121-
selected_colour;
115+
const auto colour_func = [&]() -> Color (*const)(const Color&) {
116+
switch (button_state) {
117+
case HOVER: return hover_colour;
118+
case PRESSED: return pressed_colour;
119+
case DISABLED: return disabled_colour;
120+
default: return selected_colour;
121+
}
122+
}();
122123

123124
for (Vector2i point { 0, 0 }; point.y < state_image->get_height(); ++point.y) {
124125
for (point.x = 0; point.x < state_image->get_width(); ++point.x) {
@@ -141,16 +142,11 @@ StringName const& GFXButtonStateTexture::button_state_to_name(ButtonState button
141142
static const StringName name_selected = "selected";
142143
static const StringName name_error = "INVALID BUTTON STATE";
143144
switch (button_state) {
144-
case HOVER:
145-
return name_hover;
146-
case PRESSED:
147-
return name_pressed;
148-
case DISABLED:
149-
return name_disabled;
150-
case SELECTED:
151-
return name_selected;
152-
default:
153-
return name_error;
145+
case HOVER: return name_hover;
146+
case PRESSED: return name_pressed;
147+
case DISABLED: return name_disabled;
148+
case SELECTED: return name_selected;
149+
default: return name_error;
154150
}
155151
}
156152

@@ -181,7 +177,7 @@ void GFXButtonStateHavingTexture::_clear_button_states() {
181177

182178
GFXButtonStateHavingTexture::GFXButtonStateHavingTexture() : button_state_textures {} {}
183179

184-
Ref<GFXButtonStateTexture> GFXButtonStateHavingTexture::get_button_state_texture(
180+
Ref<GFXButtonStateTexture> GFXButtonStateHavingTexture::get_button_state_texture( //
185181
GFXButtonStateTexture::ButtonState button_state
186182
) {
187183
const size_t button_state_index = button_state;

extension/src/openvic-extension/classes/GFXButtonStateTexture.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace OpenVic {
99
class GFXCorneredTileSupportingTexture : public godot::AtlasTexture {
10-
GDCLASS(GFXCorneredTileSupportingTexture, godot::AtlasTexture)
10+
GDCLASS(GFXCorneredTileSupportingTexture, godot::AtlasTexture);
1111

1212
protected:
1313
godot::Vector2i PROPERTY_ACCESS(cornered_tile_border_size, protected);
@@ -25,10 +25,10 @@ namespace OpenVic {
2525
};
2626

2727
class GFXButtonStateTexture : public GFXCorneredTileSupportingTexture {
28-
GDCLASS(GFXButtonStateTexture, GFXCorneredTileSupportingTexture)
28+
GDCLASS(GFXButtonStateTexture, GFXCorneredTileSupportingTexture);
2929

3030
public:
31-
enum ButtonState {
31+
enum ButtonState { //
3232
HOVER,
3333
PRESSED,
3434
DISABLED,
@@ -68,7 +68,7 @@ namespace OpenVic {
6868
};
6969

7070
class GFXButtonStateHavingTexture : public GFXCorneredTileSupportingTexture {
71-
GDCLASS(GFXButtonStateHavingTexture, GFXCorneredTileSupportingTexture)
71+
GDCLASS(GFXButtonStateHavingTexture, GFXCorneredTileSupportingTexture);
7272

7373
std::array<godot::Ref<GFXButtonStateTexture>, GFXButtonStateTexture::BUTTON_STATE_COUNT> button_state_textures;
7474

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ Error GFXMaskedFlagTexture::_generate_combined_image() {
1313
ERR_FAIL_NULL_V(overlay_image, FAILED);
1414
/* Whether we've already set the ImageTexture to an image of the right dimensions and format,
1515
* and so can update it without creating and setting a new image, or not. */
16-
bool can_update = button_image.is_valid() && button_image->get_size() == overlay_image->get_size()
17-
&& button_image->get_format() == overlay_image->get_format();
16+
bool can_update = button_image.is_valid() && button_image->get_size() == overlay_image->get_size() &&
17+
button_image->get_format() == overlay_image->get_format();
1818
if (!can_update) {
19-
button_image = Image::create(
20-
overlay_image->get_width(), overlay_image->get_height(), false, overlay_image->get_format()
21-
);
19+
button_image =
20+
Image::create(overlay_image->get_width(), overlay_image->get_height(), false, overlay_image->get_format());
2221
ERR_FAIL_NULL_V(button_image, FAILED);
2322
}
2423

@@ -33,10 +32,9 @@ Error GFXMaskedFlagTexture::_generate_combined_image() {
3332
if (mask_image.is_valid() && flag_image.is_valid() && flag_image_rect.has_area()) {
3433
const Vector2i centre_translation = (mask_image->get_size() - button_image->get_size()) / 2;
3534

36-
for (
37-
Vector2i combined_image_point { 0, 0 };
38-
combined_image_point.y < button_image->get_height();
39-
++combined_image_point.y
35+
for (Vector2i combined_image_point { 0, 0 }; //
36+
combined_image_point.y < button_image->get_height(); //
37+
++combined_image_point.y //
4038
) {
4139

4240
for (combined_image_point.x = 0; combined_image_point.x < button_image->get_width(); ++combined_image_point.x) {
@@ -46,10 +44,8 @@ Error GFXMaskedFlagTexture::_generate_combined_image() {
4644
// Translate to mask_image coordinates, keeping the centres of each image aligned.
4745
const Vector2i mask_image_point = combined_image_point + centre_translation;
4846

49-
if (
50-
0 <= mask_image_point.x && mask_image_point.x < mask_image->get_width() &&
51-
0 <= mask_image_point.y && mask_image_point.y < mask_image->get_height()
52-
) {
47+
if (0 <= mask_image_point.x && mask_image_point.x < mask_image->get_width() && 0 <= mask_image_point.y &&
48+
mask_image_point.y < mask_image->get_height()) {
5349
const Color mask_image_colour = mask_image->get_pixelv(mask_image_point);
5450

5551
// Rescale from mask_image to flag_image coordinates.
@@ -152,7 +148,8 @@ Error GFXMaskedFlagTexture::set_gfx_masked_flag_name(String const& gfx_masked_fl
152148

153149
GFX::MaskedFlag const* new_masked_flag = sprite->cast_to<GFX::MaskedFlag>();
154150
ERR_FAIL_NULL_V_MSG(
155-
new_masked_flag, FAILED, vformat(
151+
new_masked_flag, FAILED,
152+
vformat(
156153
"Invalid type for GFX sprite %s: %s (expected %s)", gfx_masked_flag_name,
157154
Utilities::std_to_godot_string(sprite->get_type()),
158155
Utilities::std_to_godot_string(GFX::MaskedFlag::get_type_static())

extension/src/openvic-extension/classes/GFXMaskedFlagTexture.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace OpenVic {
99
struct CountryInstance;
1010

1111
class GFXMaskedFlagTexture : public GFXButtonStateHavingTexture {
12-
GDCLASS(GFXMaskedFlagTexture, GFXButtonStateHavingTexture)
12+
GDCLASS(GFXMaskedFlagTexture, GFXButtonStateHavingTexture);
1313

1414
GFX::MaskedFlag const* PROPERTY(gfx_masked_flag, nullptr);
1515
CountryDefinition const* PROPERTY(flag_country, nullptr);
@@ -44,13 +44,13 @@ namespace OpenVic {
4444
godot::String get_gfx_masked_flag_name() const;
4545

4646
/* Set flag_country and flag_type and update the combined image to use that flag, or no flag if it doesn't exist. */
47-
godot::Error set_flag_country_and_type(
47+
godot::Error set_flag_country_and_type( //
4848
CountryDefinition const* new_flag_country, godot::StringName const& new_flag_type
4949
);
5050

5151
/* Look up the country with the specified identifier, then call set_flag_country_and_type with the country and
5252
* specified flag_type as arguments. */
53-
godot::Error set_flag_country_name_and_type(
53+
godot::Error set_flag_country_name_and_type( //
5454
godot::String const& new_flag_country_name, godot::StringName const& new_flag_type
5555
);
5656

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ Error GFXPieChartTexture::_generate_pie_chart_image() {
6868

6969
/* Whether we've already set the ImageTexture to an image of the right dimensions,
7070
* and so can update it without creating and setting a new image, or not. */
71-
const bool can_update = pie_chart_image.is_valid() && pie_chart_image->get_width() == pie_chart_diameter
72-
&& pie_chart_image->get_height() == pie_chart_diameter;
71+
const bool can_update = pie_chart_image.is_valid() && pie_chart_image->get_width() == pie_chart_diameter &&
72+
pie_chart_image->get_height() == pie_chart_diameter;
7373

7474
if (!can_update) {
7575
pie_chart_image = Image::create(pie_chart_diameter, pie_chart_diameter, false, Image::FORMAT_RGBA8);
@@ -99,7 +99,6 @@ Error GFXPieChartTexture::_generate_pie_chart_image() {
9999
} else {
100100

101101
pie_chart_image->fill(background_colour);
102-
103102
}
104103

105104
if (can_update) {
@@ -123,7 +122,7 @@ Error GFXPieChartTexture::set_slices_array(godot_pie_chart_data_t const& new_sli
123122
);
124123
const slice_t slice {
125124
slice_dict[_slice_identifier_key()], slice_dict[_slice_tooltip_key()], slice_dict[_slice_colour_key()],
126-
slice_dict[_slice_weight_key()]
125+
slice_dict[_slice_weight_key()] //
127126
};
128127
if (slice.weight > 0.0f) {
129128
total_weight += slice.weight;
@@ -182,10 +181,10 @@ Error GFXPieChartTexture::set_gfx_pie_chart_name(String const& gfx_pie_chart_nam
182181
ERR_FAIL_NULL_V(sprite, FAILED);
183182
GFX::PieChart const* new_pie_chart = sprite->cast_to<GFX::PieChart>();
184183
ERR_FAIL_NULL_V_MSG(
185-
new_pie_chart, FAILED, vformat(
184+
new_pie_chart, FAILED,
185+
vformat(
186186
"Invalid type for GFX sprite %s: %s (expected %s)", gfx_pie_chart_name,
187-
Utilities::std_to_godot_string(sprite->get_type()),
188-
Utilities::std_to_godot_string(GFX::PieChart::get_type_static())
187+
Utilities::std_to_godot_string(sprite->get_type()), Utilities::std_to_godot_string(GFX::PieChart::get_type_static())
189188
)
190189
);
191190
return set_gfx_pie_chart(new_pie_chart);

0 commit comments

Comments
 (0)