Skip to content

Commit

Permalink
EnumTags: Support non-monotonic enums (e.g. EventCommands)
Browse files Browse the repository at this point in the history
Add Unit Test
  • Loading branch information
Ghabry committed Nov 22, 2023
1 parent 28fd197 commit b8e4a30
Show file tree
Hide file tree
Showing 33 changed files with 1,224 additions and 515 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ test_runner_SOURCES = \
tests/dbbitarray.cpp \
tests/dbstring.cpp \
tests/doctest.h \
tests/enum_tags.cpp \
tests/flag_set.cpp \
tests/test_main.cpp \
tests/time_stamp.cpp \
Expand Down
9 changes: 0 additions & 9 deletions generator/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,6 @@ def type_is_array_of_struct(ty):
m = re.match(r'(Vector|Array|DBArray)<(.*)>', ty)
return m and type_is_struct(m.group(2))

def is_monotonic_from_0(enum):
expected = 0
for (val, idx) in enum:
if int(idx) != expected:
return False
expected += 1
return True

def openToRender(path):
subdir = os.path.dirname(path)
if not os.path.exists(subdir):
Expand Down Expand Up @@ -507,7 +499,6 @@ def main(argv):
env.filters["num_flags"] = num_flags
env.filters["flag_size"] = flag_size
env.filters["flag_set"] = flag_set
env.tests['monotonic_from_0'] = is_monotonic_from_0
env.tests['is_db_string'] = type_is_db_string
env.tests['is_array'] = type_is_array
env.tests['is_array_of_struct'] = type_is_array_of_struct
Expand Down
12 changes: 7 additions & 5 deletions generator/templates/rpg_header.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ namespace rpg {
{%- endif %} = {{ field.1 }}{%- if not loop.last %},{% endif %}
{%- endfor %}
};
{%- if enum is monotonic_from_0 %}
static constexpr auto k{{ name }}Tags = lcf::makeEnumTags<{{name}}>(
static constexpr auto k{{ name }}Tags = lcf::EnumTags{
{%- for field in enum %}
"{{ field.0 }}"{%- if not loop.last %},{% endif %}
{%- if name != "Code" %}
{{ name }}_{{ field.0 }}, "{{ field.0 }}"{%- if not loop.last %},{% endif %}
{%- else %}
{{ name }}::{{ field.0 }}, "{{ field.0 }}"{%- if not loop.last %},{% endif %}
{%- endif %}
{%- endfor %}
);
{%- endif %}
};
{%- endfor %}
{% endif %}
{%- for func in functions[struct_name] %}
Expand Down
18 changes: 9 additions & 9 deletions src/generated/lcf/rpg/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ namespace rpg {
Scope_target = 0,
Scope_screen = 1
};
static constexpr auto kScopeTags = lcf::makeEnumTags<Scope>(
"target",
"screen"
);
static constexpr auto kScopeTags = lcf::EnumTags{
Scope_target, "target",
Scope_screen, "screen"
};
enum Position {
Position_up = 0,
Position_middle = 1,
Position_down = 2
};
static constexpr auto kPositionTags = lcf::makeEnumTags<Position>(
"up",
"middle",
"down"
);
static constexpr auto kPositionTags = lcf::EnumTags{
Position_up, "up",
Position_middle, "middle",
Position_down, "down"
};

int ID = 0;
DBString name;
Expand Down
20 changes: 10 additions & 10 deletions src/generated/lcf/rpg/animationtiming.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ namespace rpg {
FlashScope_target = 1,
FlashScope_screen = 2
};
static constexpr auto kFlashScopeTags = lcf::makeEnumTags<FlashScope>(
"nothing",
"target",
"screen"
);
static constexpr auto kFlashScopeTags = lcf::EnumTags{
FlashScope_nothing, "nothing",
FlashScope_target, "target",
FlashScope_screen, "screen"
};
enum ScreenShake {
ScreenShake_nothing = 0,
ScreenShake_target = 1,
ScreenShake_screen = 2
};
static constexpr auto kScreenShakeTags = lcf::makeEnumTags<ScreenShake>(
"nothing",
"target",
"screen"
);
static constexpr auto kScreenShakeTags = lcf::EnumTags{
ScreenShake_nothing, "nothing",
ScreenShake_target, "target",
ScreenShake_screen, "screen"
};

int ID = 0;
int32_t frame = 0;
Expand Down
8 changes: 4 additions & 4 deletions src/generated/lcf/rpg/attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ namespace rpg {
Type_physical = 0,
Type_magical = 1
};
static constexpr auto kTypeTags = lcf::makeEnumTags<Type>(
"physical",
"magical"
);
static constexpr auto kTypeTags = lcf::EnumTags{
Type_physical, "physical",
Type_magical, "magical"
};

int ID = 0;
DBString name;
Expand Down
18 changes: 9 additions & 9 deletions src/generated/lcf/rpg/battlecommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ namespace rpg {
Type_escape = 5,
Type_special = 6
};
static constexpr auto kTypeTags = lcf::makeEnumTags<Type>(
"attack",
"skill",
"subskill",
"defense",
"item",
"escape",
"special"
);
static constexpr auto kTypeTags = lcf::EnumTags{
Type_attack, "attack",
Type_skill, "skill",
Type_subskill, "subskill",
Type_defense, "defense",
Type_item, "item",
Type_escape, "escape",
Type_special, "special"
};

int ID = 0;
DBString name;
Expand Down
56 changes: 28 additions & 28 deletions src/generated/lcf/rpg/battlecommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,58 +32,58 @@ namespace rpg {
Placement_manual = 0,
Placement_automatic = 1
};
static constexpr auto kPlacementTags = lcf::makeEnumTags<Placement>(
"manual",
"automatic"
);
static constexpr auto kPlacementTags = lcf::EnumTags{
Placement_manual, "manual",
Placement_automatic, "automatic"
};
enum RowShown {
RowShown_front = 0,
RowShown_back = 1
};
static constexpr auto kRowShownTags = lcf::makeEnumTags<RowShown>(
"front",
"back"
);
static constexpr auto kRowShownTags = lcf::EnumTags{
RowShown_front, "front",
RowShown_back, "back"
};
enum BattleType {
BattleType_traditional = 0,
BattleType_alternative = 1,
BattleType_gauge = 2
};
static constexpr auto kBattleTypeTags = lcf::makeEnumTags<BattleType>(
"traditional",
"alternative",
"gauge"
);
static constexpr auto kBattleTypeTags = lcf::EnumTags{
BattleType_traditional, "traditional",
BattleType_alternative, "alternative",
BattleType_gauge, "gauge"
};
enum WindowSize {
WindowSize_large = 0,
WindowSize_small = 1
};
static constexpr auto kWindowSizeTags = lcf::makeEnumTags<WindowSize>(
"large",
"small"
);
static constexpr auto kWindowSizeTags = lcf::EnumTags{
WindowSize_large, "large",
WindowSize_small, "small"
};
enum Transparency {
Transparency_opaque = 0,
Transparency_transparent = 1
};
static constexpr auto kTransparencyTags = lcf::makeEnumTags<Transparency>(
"opaque",
"transparent"
);
static constexpr auto kTransparencyTags = lcf::EnumTags{
Transparency_opaque, "opaque",
Transparency_transparent, "transparent"
};
enum Facing {
Facing_retain = 0,
Facing_up = 1,
Facing_right = 2,
Facing_down = 3,
Facing_left = 4
};
static constexpr auto kFacingTags = lcf::makeEnumTags<Facing>(
"retain",
"up",
"right",
"down",
"left"
);
static constexpr auto kFacingTags = lcf::EnumTags{
Facing_retain, "retain",
Facing_up, "up",
Facing_right, "right",
Facing_down, "down",
Facing_left, "left"
};

int32_t placement = 0;
bool death_handler_unused = 0;
Expand Down
33 changes: 19 additions & 14 deletions src/generated/lcf/rpg/battleranimation.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ namespace rpg {
Speed_medium = 14,
Speed_fast = 8
};
static constexpr auto kSpeedTags = lcf::EnumTags{
Speed_slow, "slow",
Speed_medium, "medium",
Speed_fast, "fast"
};
enum Pose {
Pose_Idle = 0,
Pose_AttackRight = 1,
Expand All @@ -48,20 +53,20 @@ namespace rpg {
Pose_Victory = 10,
Pose_Item = 11
};
static constexpr auto kPoseTags = lcf::makeEnumTags<Pose>(
"Idle",
"AttackRight",
"AttackLeft",
"Skill",
"Dead",
"Damage",
"Dazed",
"Defend",
"WalkLeft",
"WalkRight",
"Victory",
"Item"
);
static constexpr auto kPoseTags = lcf::EnumTags{
Pose_Idle, "Idle",
Pose_AttackRight, "AttackRight",
Pose_AttackLeft, "AttackLeft",
Pose_Skill, "Skill",
Pose_Dead, "Dead",
Pose_Damage, "Damage",
Pose_Dazed, "Dazed",
Pose_Defend, "Defend",
Pose_WalkLeft, "WalkLeft",
Pose_WalkRight, "WalkRight",
Pose_Victory, "Victory",
Pose_Item, "Item"
};

int ID = 0;
DBString name;
Expand Down
38 changes: 19 additions & 19 deletions src/generated/lcf/rpg/battleranimationitemskill.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,39 @@ namespace rpg {
Speed_medium = 1,
Speed_slow = 2
};
static constexpr auto kSpeedTags = lcf::makeEnumTags<Speed>(
"fast",
"medium",
"slow"
);
static constexpr auto kSpeedTags = lcf::EnumTags{
Speed_fast, "fast",
Speed_medium, "medium",
Speed_slow, "slow"
};
enum AnimType {
AnimType_weapon = 0,
AnimType_battle = 1
};
static constexpr auto kAnimTypeTags = lcf::makeEnumTags<AnimType>(
"weapon",
"battle"
);
static constexpr auto kAnimTypeTags = lcf::EnumTags{
AnimType_weapon, "weapon",
AnimType_battle, "battle"
};
enum Movement {
Movement_none = 0,
Movement_step = 1,
Movement_jump = 2,
Movement_move = 3
};
static constexpr auto kMovementTags = lcf::makeEnumTags<Movement>(
"none",
"step",
"jump",
"move"
);
static constexpr auto kMovementTags = lcf::EnumTags{
Movement_none, "none",
Movement_step, "step",
Movement_jump, "jump",
Movement_move, "move"
};
enum Afterimage {
Afterimage_none = 0,
Afterimage_add = 1
};
static constexpr auto kAfterimageTags = lcf::makeEnumTags<Afterimage>(
"none",
"add"
);
static constexpr auto kAfterimageTags = lcf::EnumTags{
Afterimage_none, "none",
Afterimage_add, "add"
};

int ID = 0;
int32_t unknown02 = 0;
Expand Down
8 changes: 4 additions & 4 deletions src/generated/lcf/rpg/battleranimationpose.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ namespace rpg {
AnimType_character = 0,
AnimType_battle = 1
};
static constexpr auto kAnimTypeTags = lcf::makeEnumTags<AnimType>(
"character",
"battle"
);
static constexpr auto kAnimTypeTags = lcf::EnumTags{
AnimType_character, "character",
AnimType_battle, "battle"
};

int ID = 0;
DBString name;
Expand Down
8 changes: 4 additions & 4 deletions src/generated/lcf/rpg/chipset.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ namespace rpg {
AnimType_reciprocating = 0,
AnimType_cyclic = 1
};
static constexpr auto kAnimTypeTags = lcf::makeEnumTags<AnimType>(
"reciprocating",
"cyclic"
);
static constexpr auto kAnimTypeTags = lcf::EnumTags{
AnimType_reciprocating, "reciprocating",
AnimType_cyclic, "cyclic"
};

int ID = 0;
DBString name;
Expand Down
5 changes: 5 additions & 0 deletions src/generated/lcf/rpg/commonevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ namespace rpg {
Trigger_parallel = 4,
Trigger_call = 5
};
static constexpr auto kTriggerTags = lcf::EnumTags{
Trigger_automatic, "automatic",
Trigger_parallel, "parallel",
Trigger_call, "call"
};

int ID = 0;
DBString name;
Expand Down
Loading

0 comments on commit b8e4a30

Please sign in to comment.