From 0e0e20a4fae6994c0e94b6be20dd080df69d5b39 Mon Sep 17 00:00:00 2001 From: RedContritio Date: Wed, 28 Feb 2024 17:18:22 +0800 Subject: [PATCH] update order logic --- Core/RuleService.cs | 71 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/Core/RuleService.cs b/Core/RuleService.cs index 123f6a6..7768d00 100644 --- a/Core/RuleService.cs +++ b/Core/RuleService.cs @@ -12,6 +12,19 @@ public static class RuleService { public static List ruleList = new List(); + public static Dictionary category2orderMap = new Dictionary(); + + public static readonly Dictionary category2ruleType = new Dictionary{ + {"GUI", "Ui"}, + {"Graphical", "Graphics"}, + {"Races", "RaceEdits"}, + {"Factions", "FactionEdits"}, + {"Buildings", "Buildings"}, + {"Clothing/Armour", "Armor/Weapons"}, + {"Items/Weapons", "Armor/Weapons"}, + {"Gameplay", "Overhauls & Big additions/world changes"}, + }; + public static string GetLatestVersion() { try @@ -69,6 +82,8 @@ public static IEnumerable OrderMods(IEnumerable mods) if (ruleList.Count == 0) ruleList = GetRules(); + category2orderMap = getCategory2OrderMap(ruleList); + foreach (var rule in ruleList) { foreach (var orderedMod in rule.Mod) @@ -88,6 +103,32 @@ public static IEnumerable OrderMods(IEnumerable mods) } } + // Fine, lots of mods is not set in rule set, there will force order them sequencely. + // A better order rule for unspecified mods process by their categories + int PRESET_BIG_ORDER_NUMBER = 999999; + + foreach (var item in mods.Where(c => !c.OrderedAutomatically)) + { + int expected_order = PRESET_BIG_ORDER_NUMBER; + foreach (var c in item.Categories) + { + if (category2orderMap.ContainsKey(c)) + { + expected_order = Math.Min(category2orderMap[c], expected_order); + } + } + + if (expected_order != PRESET_BIG_ORDER_NUMBER) + { + ruleList.FirstOrDefault(c => c.Order == expected_order).ModsOrdered.Add(item); + item.OrderedAutomatically = true; + } + else + { + Logging.Write(Constants.Logfile, String.Format("mod {0} with categories {1} has no target order.", item.DisplayName, item.DisplayCategories)); + } + } + foreach (var item in mods.Where(c => !c.OrderedAutomatically)) { ruleList.FirstOrDefault(c => c.Order == 10).ModsOrdered.Add(item); @@ -152,5 +193,35 @@ void removeModFromOtherList(Mod mod, int order) rule.ModsOrdered.Remove(mod); } } + + private static Dictionary getCategory2OrderMap(List rules) + { + var rule2order = new Dictionary(); + foreach (var rule in rules) + { + string[] categories = rule.Name.Split(','); + foreach (string category in categories) + { + rule2order[category.Trim()] = rule.Order; + } + } + + var category2order = new Dictionary(); + + foreach (KeyValuePair kv in category2ruleType) + { + if (!rule2order.ContainsKey(kv.Value)) + { + Logging.Write(Constants.Errorfile, String.Format("rule type {0} not found.", kv.Value)); + continue; + } + + category2order.Add(kv.Key, rule2order[kv.Value]); + } + + //category2order.Add("Translation", 20); + + return category2order; + } } } \ No newline at end of file