From 8dc566f2dd30ccc32f16e76851eac989ced335bc Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Sat, 9 Nov 2024 16:33:03 +0200
Subject: [PATCH 76/76] Load ui_name from action section

See RM #1092

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 server/ruleset/rscompat.c | 34 +++++++++++++++++++++++++++
 server/ruleset/rscompat.h |  4 ++++
 server/ruleset/ruleload.c | 44 ++++++++++-------------------------
 tools/ruleutil/rulesave.c | 49 +++++++++++++--------------------------
 4 files changed, 66 insertions(+), 65 deletions(-)

diff --git a/server/ruleset/rscompat.c b/server/ruleset/rscompat.c
index 5f90b7e620..adcff0c620 100644
--- a/server/ruleset/rscompat.c
+++ b/server/ruleset/rscompat.c
@@ -735,3 +735,37 @@ void rscompat_civil_war_effects_3_3(struct section_file *game_rs)
     effect_req_append(peffect, e_req);
   }
 }
+
+/**********************************************************************//**
+  Load ui_name of one action
+**************************************************************************/
+bool load_action_ui_name_3_3(struct section_file *file, int act,
+                             const char *entry_name,
+                             struct rscompat_info *compat)
+{
+  const char *text;
+  const char *def = action_ui_name_default(act);
+  struct action *paction = action_by_number(act);
+
+  if (entry_name == nullptr) {
+    text = def;
+  } else {
+    text = secfile_lookup_str_default(file, nullptr,
+                                      "actions.%s", entry_name);
+
+    if (text == nullptr && compat->compat_mode && compat->version < RSFORMAT_3_3) {
+      text = secfile_lookup_str_default(file, nullptr,
+                                        "actions.%s", ui_name_old_name_3_3(entry_name));
+    }
+
+    if (text == nullptr) {
+      text = def;
+    } else {
+      paction->configured = TRUE;
+    }
+  }
+
+  sz_strlcpy(paction->ui_name, text);
+
+  return TRUE;
+}
diff --git a/server/ruleset/rscompat.h b/server/ruleset/rscompat.h
index e18c6b5640..8597ea080e 100644
--- a/server/ruleset/rscompat.h
+++ b/server/ruleset/rscompat.h
@@ -66,6 +66,10 @@ const char *ui_name_old_name_3_3(const char *new_name);
 
 void rscompat_civil_war_effects_3_3(struct section_file *game_rs);
 
+bool load_action_ui_name_3_3(struct section_file *file, int act,
+                             const char *entry_name,
+                             struct rscompat_info *compat);
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
diff --git a/server/ruleset/ruleload.c b/server/ruleset/ruleload.c
index a529346b1b..622d07dec4 100644
--- a/server/ruleset/ruleload.c
+++ b/server/ruleset/ruleload.c
@@ -6253,37 +6253,6 @@ static int secfile_lookup_int_default_min_max(struct section_file *file,
   return ival;
 }
 
-/**********************************************************************//**
-  Load ui_name of one action
-**************************************************************************/
-static bool load_action_ui_name(struct section_file *file, int act,
-                                const char *entry_name,
-                                struct rscompat_info *compat)
-{
-  const char *text;
-  const char *def = action_ui_name_default(act);
-
-  if (entry_name == nullptr) {
-    text = def;
-  } else {
-    text = secfile_lookup_str_default(file, nullptr,
-                                      "actions.%s", entry_name);
-
-    if (text == nullptr && compat->compat_mode && compat->version < RSFORMAT_3_3) {
-      text = secfile_lookup_str_default(file, nullptr,
-                                        "actions.%s", ui_name_old_name_3_3(entry_name));
-    }
-
-    if (text == nullptr) {
-      text = def;
-    }
-  }
-
-  sz_strlcpy(action_by_number(act)->ui_name, text);
-
-  return TRUE;
-}
-
 /**********************************************************************//**
   Load max range of an action
 **************************************************************************/
@@ -7686,7 +7655,10 @@ static bool load_ruleset_actions(struct section_file *file,
             entry_name = action_ui_name_ruleset_var_name(act_id);
           }
 
-          load_action_ui_name(file, act_id, entry_name, compat);
+          /* FIXME: Allow old style ui_name definitions only in compat mode. */
+          if (TRUE /* compat->compat_mode && compat->version < RSFORMAT_3_3 */) {
+            load_action_ui_name_3_3(file, act_id, entry_name, compat);
+          }
         }
 
         if (!ok) {
@@ -7855,6 +7827,7 @@ static bool load_ruleset_actions(struct section_file *file,
         const char *sec_name = section_name(psection);
         const char *action_text;
         struct action *paction;
+        const char *ui_name;
 
         action_text = secfile_lookup_str(file, "%s.action", sec_name);
 
@@ -7884,6 +7857,13 @@ static bool load_ruleset_actions(struct section_file *file,
         }
 
         paction->configured = TRUE;
+
+        ui_name = secfile_lookup_str_default(file, nullptr,
+                                             "%s.ui_name", sec_name);
+        if (ui_name == nullptr) {
+          ui_name = action_ui_name_default(action_id(paction));
+        }
+        sz_strlcpy(paction->ui_name, ui_name);
       } section_list_iterate_end;
     }
   }
diff --git a/tools/ruleutil/rulesave.c b/tools/ruleutil/rulesave.c
index 79000de3ba..51d893951a 100644
--- a/tools/ruleutil/rulesave.c
+++ b/tools/ruleutil/rulesave.c
@@ -866,35 +866,6 @@ static bool save_effects_ruleset(const char *filename, const char *name)
   return save_ruleset_file(sfile, filename);
 }
 
-/**********************************************************************//**
-  Save ui_name of one action.
-**************************************************************************/
-static bool save_action_ui_name(struct section_file *sfile,
-                                int act, const char *entry_name)
-{
-  struct action *paction = action_by_number(act);
-  const char *ui_name;
-
-  if (action_is_internal(paction)) {
-    return TRUE;
-  }
-
-  ui_name = paction->ui_name;
-
-  if (ui_name == nullptr) {
-    fc_assert(ui_name != nullptr);
-
-    return FALSE;
-  }
-
-  if (strcmp(ui_name, action_ui_name_default(act))) {
-    secfile_insert_str(sfile, ui_name,
-                       "actions.%s", entry_name);
-  }
-
-  return TRUE;
-}
-
 /**********************************************************************//**
   Save max range of an action.
 **************************************************************************/
@@ -1201,10 +1172,6 @@ static bool save_actions_ruleset(const char *filename, const char *name)
   action_iterate(act_id) {
     struct action *act = action_by_number(act_id);
 
-    if (!action_id_is_internal(act_id)) {
-      save_action_ui_name(sfile,
-                          act_id, action_ui_name_ruleset_var_name(act_id));
-    }
     save_action_kind(sfile, act_id);
     save_action_range(sfile, act_id);
     save_action_actor_consuming_always(sfile, act_id);
@@ -1238,11 +1205,27 @@ static bool save_actions_ruleset(const char *filename, const char *name)
 
     if (paction->configured) {
       char path[512];
+      const char *ui_name;
 
       fc_snprintf(path, sizeof(path), "action_%d", i);
 
       secfile_insert_str(sfile, action_rule_name(paction),
                          "%s.action", path);
+
+      if (!action_id_is_internal(i)) {
+        ui_name = paction->ui_name;
+
+        if (ui_name == nullptr) {
+          fc_assert(ui_name != nullptr);
+
+          return FALSE;
+        }
+
+        if (strcmp(ui_name, action_ui_name_default(i))) {
+          secfile_insert_str(sfile, ui_name,
+                             "%s.ui_name", path);
+        }
+      }
     }
   }
 
-- 
2.45.2

