Skip to content

Commit

Permalink
Merge pull request #143 from BUTR/dev
Browse files Browse the repository at this point in the history
v2.1.1
  • Loading branch information
Aragas authored Mar 19, 2022
2 parents c0552df + c1eb81b commit 384a7c8
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 29 deletions.
2 changes: 1 addition & 1 deletion build/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<!--Development Variables-->
<PropertyGroup>
<!--Module Version-->
<Version>2.1.0</Version>
<Version>2.1.1</Version>
<!--Harmony Version-->
<HarmonyVersion>2.2.1</HarmonyVersion>
<!--Microsoft Extension Libraries Version-->
Expand Down
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
---------------------------------------------------------------------------------------------------
Version: 2.1.1
Game Versions: e1.7.2
* Fixed unintended crashes when running on pre e1.7.2
* Added message when running on pre e1.7.2
---------------------------------------------------------------------------------------------------
Version: 2.1.0
Game Versions: e1.7.2
* Dependency fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

using TaleWorlds.Core;
using TaleWorlds.InputSystem;
using TaleWorlds.Localization;
using TaleWorlds.MountAndBlade;

namespace Bannerlord.ButterLib.Implementation.HotKeys
Expand All @@ -26,8 +27,8 @@ public HotKeyCategoryContainer(string categoryId, IEnumerable<HotKeyBase> keys)
var variationString = $"{categoryId}_";
foreach (var key in keys)
{
keyName.AddVariationWithId($"{variationString}{key.Id}", TextObjectHelper.Create(key.DisplayName), new List<GameTextManager.ChoiceTag>());
keyDesc.AddVariationWithId($"{variationString}{key.Id}", TextObjectHelper.Create(key.Description), new List<GameTextManager.ChoiceTag>());
keyName.AddVariationWithId($"{variationString}{key.Id}", new TextObject(key.DisplayName), new List<GameTextManager.ChoiceTag>());
keyDesc.AddVariationWithId($"{variationString}{key.Id}", new TextObject(key.Description), new List<GameTextManager.ChoiceTag>());
RegisterGameKey(new GameKey(key.Id, key.Uid, categoryId, key.DefaultKey, key.Category));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
<string id="UZ8zfvudMs" text="{MODULE} is loaded after the {REQUIRED_MODULE}!{NL}Make sure {MODULE} is loaded before it!" />
<string id="FcR4BXnhx8" text="{MODULE} has mutually exclusive mod order directives specified for the {REQUIRED_MODULE}!" />
<string id="eXs6FLm5DP" text="It's strongly recommended to terminate the game now. Do you wish to terminate it?" />
<string id="aGg5Gh64gH" text="This version of ButterLib is intended for e1.7.2 and higher! You are running {GAMEVERSION}!" />
</strings>
</base>
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
<string id="UZ8zfvudMs" text="{MODULE} загружен после {REQUIRED_MODULE}!{NL}{MODULE} должен загружаться до этого модуля!" />
<string id="FcR4BXnhx8" text="В {MODULE} заданы взаимоисключающие указания отностительно {REQUIRED_MODULE}!" />
<string id="eXs6FLm5DP" text="Рекомендуется прервать игру. Вы хотите выйти?" />
<string id="aGg5Gh64gH" text="Эта версия ButterLib предназначена для e1.7.2 и выше! У вас версия игры {GAMEVERSION}!" />
</strings>
</base>
26 changes: 24 additions & 2 deletions src/Bannerlord.ButterLib/ButterLibSubModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

using TaleWorlds.CampaignSystem;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.MountAndBlade;

namespace Bannerlord.ButterLib
Expand All @@ -33,6 +35,8 @@ public sealed partial class ButterLibSubModule : MBSubModuleBase
@"{=BguqytVG3q}Warning from Bannerlord.ButterLib!";
private const string SMessageContinue =
@"{=eXs6FLm5DP}It's strongly recommended to terminate the game now. Do you wish to terminate it?";
private const string SMessageWrongGameVersion =
@"{=aGg5Gh64gH}This version of ButterLib is intended for e1.7.2 and higher! You are running {GAMEVERSION}!";

internal event Action<float>? OnApplicationTickEvent;

Expand All @@ -47,6 +51,7 @@ public ButterLibSubModule()
{
Instance = this;

CheckGameVersion();
CheckLoadOrder();
}

Expand Down Expand Up @@ -168,6 +173,23 @@ public override void OnGameEnd(Game game)
}


private static void CheckGameVersion()
{
var e172 = new ApplicationVersion(ApplicationVersionType.EarlyAccess, 1, 7, 2, 0, ApplicationVersionGameType.Singleplayer);
if (ApplicationVersionHelper.GameVersion() is { } gameVersion && gameVersion < e172)
{
var sb = new StringBuilder();
sb.AppendLine(new TextObject(SMessageWrongGameVersion, new() { {"GAMEVERSION", ApplicationVersionHelper.ToString(gameVersion)} }).ToString());
sb.AppendLine();
sb.AppendLine(new TextObject(SMessageContinue).ToString());
switch (MessageBox.Show(sb.ToString(), new TextObject(SWarningTitle).ToString(), MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, (MessageBoxOptions) 0x40000))
{
case DialogResult.Yes:
Environment.Exit(1);
break;
}
}
}
private static void CheckLoadOrder()
{
var loadedModules = ModuleInfoHelper.GetLoadedModules().ToList();
Expand All @@ -178,8 +200,8 @@ private static void CheckLoadOrder()
{
sb.AppendLine(report);
sb.AppendLine();
sb.AppendLine(TextObjectHelper.Create(SMessageContinue)?.ToString() ?? "ERROR");
switch (MessageBox.Show(sb.ToString(), TextObjectHelper.Create(SWarningTitle)?.ToString() ?? "ERROR", MessageBoxButtons.YesNo))
sb.AppendLine(new TextObject(SMessageContinue).ToString());
switch (MessageBox.Show(sb.ToString(), new TextObject(SWarningTitle).ToString(), MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, (MessageBoxOptions) 0x40000))
{
case DialogResult.Yes:
Environment.Exit(1);
Expand Down
48 changes: 24 additions & 24 deletions src/Bannerlord.ButterLib/Helpers/Localization/LocalizationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,31 @@ private static TextObject GetEntityTextObject<T>(T entity) where T : class
switch (entity)
{
case Hero hero:
var characterProperties = TextObjectHelper.Create(string.Empty);
characterProperties!.SetTextVariable("NAME", hero.Name);
var characterProperties = new TextObject(string.Empty);
characterProperties.SetTextVariable("NAME", hero.Name);
characterProperties.SetTextVariable("AGE", (int) hero.Age);
characterProperties.SetTextVariable("GENDER", hero.IsFemale ? 1 : 0);
characterProperties.SetTextVariable("LINK", hero.EncyclopediaLinkWithName);
characterProperties.SetTextVariable("FIRSTNAME", hero.FirstName ?? hero.Name);
return characterProperties;
case Settlement settlement:
var settlementProperties = TextObjectHelper.Create(string.Empty);
settlementProperties!.SetTextVariable("NAME", settlement.Name);
var settlementProperties = new TextObject(string.Empty);
settlementProperties.SetTextVariable("NAME", settlement.Name);
settlementProperties.SetTextVariable("IS_TOWN", settlement.IsTown ? 1 : 0);
settlementProperties.SetTextVariable("IS_CASTLE", settlement.IsCastle ? 1 : 0);
settlementProperties.SetTextVariable("IS_VILLAGE", settlement.IsVillage ? 1 : 0);
settlementProperties.SetTextVariable("LINK", settlement.EncyclopediaLinkWithName);
return settlementProperties;
case Clan clan:
var clanProperties = TextObjectHelper.Create(string.Empty);
clanProperties!.SetTextVariable("NAME", clan.Name);
var clanProperties = new TextObject(string.Empty);
clanProperties.SetTextVariable("NAME", clan.Name);
clanProperties.SetTextVariable("MINOR_FACTION", clan.IsMinorFaction ? 1 : 0);
clanProperties.SetTextVariable("UNDER_CONTRACT", clan.IsUnderMercenaryService ? 1 : 0);
clanProperties.SetTextVariable("LINK", clan.EncyclopediaLinkWithName);
return clanProperties;
case Kingdom kingdom:
var kingdomProperties = TextObjectHelper.Create(string.Empty);
kingdomProperties!.SetTextVariable("NAME", kingdom.Name);
var kingdomProperties = new TextObject(string.Empty);
kingdomProperties.SetTextVariable("NAME", kingdom.Name);
kingdomProperties.SetTextVariable("LINK", kingdom.EncyclopediaLinkWithName);
return kingdomProperties;
default:
Expand Down Expand Up @@ -227,12 +227,12 @@ public static PluralForm GetPluralForm(float number)
return number > 1f ? PluralForm.Plural : PluralForm.Singular;
}

private static Dictionary<string, TextObject?> GetPluralFormAttributes(PluralForm pluralForm) =>
private static Dictionary<string, object> GetPluralFormAttributes(PluralForm pluralForm) =>
new()
{
[PLURAL_FORM_TAG] = TextObjectHelper.Create(pluralForm == PluralForm.Plural ? 1 : 0),
[SPECIFIC_PLURAL_FORM_TAG] = TextObjectHelper.Create(pluralForm == PluralForm.SpecificPlural ? 1 : 0),
[SPECIFIC_SINGULAR_FORM_TAG] = TextObjectHelper.Create(pluralForm == PluralForm.SpecificSingular ? 1 : 0)
[PLURAL_FORM_TAG] = new TextObject(pluralForm == PluralForm.Plural ? 1 : 0),
[SPECIFIC_PLURAL_FORM_TAG] = new TextObject(pluralForm == PluralForm.SpecificPlural ? 1 : 0),
[SPECIFIC_SINGULAR_FORM_TAG] = new TextObject(pluralForm == PluralForm.SpecificSingular ? 1 : 0)
};

/// <summary>Sets a numeric variable along with the appropriate <see cref="PluralForm" /> tag in accordance with the grammar rules of the game language.</summary>
Expand All @@ -251,8 +251,8 @@ public static void SetNumericVariable(TextObject? textObject, string tag, int va
}
var attributes = GetPluralFormAttributes(GetPluralForm(variableValue));
var explainedTextObject = string.IsNullOrEmpty(format)
? TextObjectHelper.Create(variableValue.ToString(), attributes)
: TextObjectHelper.Create(variableValue.ToString(format), attributes);
? new TextObject(variableValue.ToString(), attributes)
: new TextObject(variableValue.ToString(format), attributes);
if (textObject is null)
{
MBTextManager.SetTextVariable(tag, explainedTextObject);
Expand All @@ -279,8 +279,8 @@ public static void SetNumericVariable(TextObject? textObject, string tag, float
}
var attributes = GetPluralFormAttributes(GetPluralForm(variableValue));
var explainedTextObject = string.IsNullOrEmpty(format)
? TextObjectHelper.Create(variableValue.ToString("R"), attributes)
: TextObjectHelper.Create(variableValue.ToString(format), attributes);
? new TextObject(variableValue.ToString("R"), attributes)
: new TextObject(variableValue.ToString(format), attributes);
if (textObject is null)
{
MBTextManager.SetTextVariable(tag, explainedTextObject);
Expand All @@ -291,21 +291,21 @@ public static void SetNumericVariable(TextObject? textObject, string tag, float
}
}

private static Dictionary<string, TextObject?> GetListAttributes(IEnumerable<string> valuesList, string separator = ", ", bool useDistinctValues = true)
private static Dictionary<string, TextObject> GetListAttributes(IEnumerable<string> valuesList, string separator = ", ", bool useDistinctValues = true)
{
var localValues = (useDistinctValues ? valuesList.Distinct() : valuesList).ToList();
return localValues.Any()
? (new()
{
[LIST_HAS_MULTIPLE_ITEMS_TAG] = TextObjectHelper.Create((localValues.Count != 1) ? 1 : 0),
[LIST_START_TAG] = TextObjectHelper.Create(string.Join(separator, localValues.Take(localValues.Count - 1))),
[LIST_END_TAG] = TextObjectHelper.Create(localValues.Last())
[LIST_HAS_MULTIPLE_ITEMS_TAG] = new TextObject(localValues.Count != 1 ? 1 : 0),
[LIST_START_TAG] = new TextObject(string.Join(separator, localValues.Take(localValues.Count - 1))),
[LIST_END_TAG] = new TextObject(localValues.Last())
})
: (new()
{
[LIST_HAS_MULTIPLE_ITEMS_TAG] = TextObjectHelper.Create(0),
[LIST_START_TAG] = TextObjectHelper.Create(string.Empty),
[LIST_END_TAG] = TextObjectHelper.Create(string.Empty)
[LIST_HAS_MULTIPLE_ITEMS_TAG] = new TextObject(0),
[LIST_START_TAG] = new TextObject(string.Empty),
[LIST_END_TAG] = new TextObject(string.Empty)
});
}

Expand All @@ -327,7 +327,7 @@ public static void SetNumericVariable(TextObject? textObject, string tag, float
/// <example>
/// <code>
/// var lst = new [] { "First Entry", "Second Entry", "Third Entry", "Second Entry" }.ToList();
/// TextObject? txt = TextObjectHelper.Create("Here is the built-in text: '{TEST_TAG}'. Here is custom text: '{TEST_TAG.START}{?TEST_TAG.IS_PLURAL} and last but not least the {?}{\\?}{TEST_TAG.END}'");
/// var txt = new TextObject("Here is the built-in text: '{TEST_TAG}'. Here is custom text: '{TEST_TAG.START}{?TEST_TAG.IS_PLURAL} and last but not least the {?}{\\?}{TEST_TAG.END}'");
/// LocalizationHelper.SetListVariable(txt, "TEST_TAG", lst);
/// InformationManager.DisplayMessage(new InformationMessage(txt!.ToString(), Color.FromUint(0x00F16D26)));
/// </code>
Expand Down

0 comments on commit 384a7c8

Please sign in to comment.