Skip to content

Update mod order logic [beta] #1605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions Core/RuleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ public static class RuleService
{
public static List<Rules> ruleList = new List<Rules>();

public static Dictionary<string, int> category2orderMap = new Dictionary<string, int>();

public static readonly Dictionary<string, string> category2ruleType = new Dictionary<string, string>{
{"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
Expand Down Expand Up @@ -69,6 +82,8 @@ public static IEnumerable<Mod> OrderMods(IEnumerable<Mod> mods)
if (ruleList.Count == 0)
ruleList = GetRules();

category2orderMap = getCategory2OrderMap(ruleList);

foreach (var rule in ruleList)
{
foreach (var orderedMod in rule.Mod)
Expand All @@ -88,6 +103,32 @@ public static IEnumerable<Mod> OrderMods(IEnumerable<Mod> 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);
Expand Down Expand Up @@ -152,5 +193,35 @@ void removeModFromOtherList(Mod mod, int order)
rule.ModsOrdered.Remove(mod);
}
}

private static Dictionary<string, int> getCategory2OrderMap(List<Rules> rules)
{
var rule2order = new Dictionary<string, int>();
foreach (var rule in rules)
{
string[] categories = rule.Name.Split(',');
foreach (string category in categories)
{
rule2order[category.Trim()] = rule.Order;
}
}

var category2order = new Dictionary<string, int>();

foreach (KeyValuePair<string, string> 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;
}
}
}