Skip to content

Commit

Permalink
Move translatable strings into a resource file
Browse files Browse the repository at this point in the history
  • Loading branch information
Citrinate committed Mar 22, 2024
1 parent 4cb3cf1 commit 5803cf8
Show file tree
Hide file tree
Showing 11 changed files with 328 additions and 68 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -488,3 +488,6 @@ $RECYCLE.BIN/

# Windows shortcuts
*.lnk

# https://www.paraesthesia.com/archive/2022/09/30/strongly-typed-resources-with-net-core/
*.Designer.cs
17 changes: 8 additions & 9 deletions FreePackages/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Linq;
using System.Text;
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Localization;
using ArchiSteamFarm.Steam;

namespace FreePackages {
Expand Down Expand Up @@ -66,7 +65,7 @@ internal static class Commands {
}

if (!bot.IsConnectedAndLoggedOn) {
return FormatBotResponse(bot, Strings.BotNotConnected);
return FormatBotResponse(bot, ArchiSteamFarm.Localization.Strings.BotNotConnected);
}

if (!PackageHandler.Handlers.Keys.Contains(bot.BotName)) {
Expand All @@ -84,7 +83,7 @@ internal static class Commands {
HashSet<Bot>? bots = Bot.GetBots(botNames);

if ((bots == null) || (bots.Count == 0)) {
return access >= EAccess.Owner ? FormatStaticResponse(String.Format(Strings.BotNotFound, botNames)) : null;
return access >= EAccess.Owner ? FormatStaticResponse(String.Format(ArchiSteamFarm.Localization.Strings.BotNotFound, botNames)) : null;
}

IEnumerable<string?> results = bots.Select(bot => ResponseClearQueue(bot, ArchiSteamFarm.Steam.Interaction.Commands.GetProxyAccess(bot, access, steamID)));
Expand All @@ -100,7 +99,7 @@ internal static class Commands {
}

if (!bot.IsConnectedAndLoggedOn) {
return FormatBotResponse(bot, Strings.BotNotConnected);
return FormatBotResponse(bot, ArchiSteamFarm.Localization.Strings.BotNotConnected);
}

if (!PackageHandler.Handlers.Keys.Contains(bot.BotName)) {
Expand All @@ -118,7 +117,7 @@ internal static class Commands {
HashSet<Bot>? bots = Bot.GetBots(botNames);

if ((bots == null) || (bots.Count == 0)) {
return access >= EAccess.Owner ? FormatStaticResponse(String.Format(Strings.BotNotFound, botNames)) : null;
return access >= EAccess.Owner ? FormatStaticResponse(String.Format(ArchiSteamFarm.Localization.Strings.BotNotFound, botNames)) : null;
}

IEnumerable<string?> results = bots.Select(bot => ResponseQueueStatus(bot, ArchiSteamFarm.Steam.Interaction.Commands.GetProxyAccess(bot, access, steamID)));
Expand All @@ -134,7 +133,7 @@ internal static class Commands {
}

if (!bot.IsConnectedAndLoggedOn) {
return FormatBotResponse(bot, Strings.BotNotConnected);
return FormatBotResponse(bot, ArchiSteamFarm.Localization.Strings.BotNotConnected);
}

if (!PackageHandler.Handlers.Keys.Contains(bot.BotName)) {
Expand All @@ -154,7 +153,7 @@ internal static class Commands {

if ((index > 0) && (entry.Length > index + 1)) {
if (!uint.TryParse(entry[(index + 1)..], out gameID) || (gameID == 0)) {
response.AppendLine(FormatBotResponse(bot, string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, nameof(gameID))));
response.AppendLine(FormatBotResponse(bot, string.Format(CultureInfo.CurrentCulture, ArchiSteamFarm.Localization.Strings.ErrorIsInvalid, nameof(gameID))));

continue;
}
Expand All @@ -163,7 +162,7 @@ internal static class Commands {
} else if (uint.TryParse(entry, out gameID) && (gameID > 0)) {
type = "SUB";
} else {
response.AppendLine(FormatBotResponse(bot, string.Format(CultureInfo.CurrentCulture, Strings.ErrorIsInvalid, nameof(gameID))));
response.AppendLine(FormatBotResponse(bot, string.Format(CultureInfo.CurrentCulture, ArchiSteamFarm.Localization.Strings.ErrorIsInvalid, nameof(gameID))));

continue;
}
Expand All @@ -190,7 +189,7 @@ internal static class Commands {
HashSet<Bot>? bots = Bot.GetBots(botNames);

if ((bots == null) || (bots.Count == 0)) {
return access >= EAccess.Owner ? FormatStaticResponse(String.Format(Strings.BotNotFound, botNames)) : null;
return access >= EAccess.Owner ? FormatStaticResponse(String.Format(ArchiSteamFarm.Localization.Strings.BotNotFound, botNames)) : null;
}

IEnumerable<string?> results = bots.Select(bot => ResponseQueueLicense(bot, ArchiSteamFarm.Steam.Interaction.Commands.GetProxyAccess(bot, access, steamID), licenses, useFilter));
Expand Down
3 changes: 1 addition & 2 deletions FreePackages/Data/BotCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Helpers;
using ArchiSteamFarm.Helpers.Json;
using ArchiSteamFarm.Localization;
using SteamKit2;

namespace FreePackages {
Expand Down Expand Up @@ -68,7 +67,7 @@ internal BotCache(string filePath) : this() {
string json = await File.ReadAllTextAsync(filePath).ConfigureAwait(false);

if (string.IsNullOrEmpty(json)) {
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorIsEmpty, nameof(json)));
ASF.ArchiLogger.LogGenericError(string.Format(ArchiSteamFarm.Localization.Strings.ErrorIsEmpty, nameof(json)));

return null;
}
Expand Down
5 changes: 3 additions & 2 deletions FreePackages/Data/CardApps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Web.Responses;
using FreePackages.Localization;

namespace FreePackages {
internal static class CardApps {
Expand All @@ -26,7 +27,7 @@ private static async Task DoUpdate() {
ObjectResponse<Badges>? response = await ASF.WebBrowser.UrlGetToJsonObject<Badges>(request).ConfigureAwait(false);

if (response == null) {
ASF.ArchiLogger.LogGenericDebug("Failed to fetch badge data for free packages");
ASF.ArchiLogger.LogGenericDebug(Strings.BadgeDataFetchFailed);
UpdateTimer.Change(TimeSpan.FromMinutes(1), UpdateFrequency);

return;
Expand All @@ -38,7 +39,7 @@ private static async Task DoUpdate() {
AppIDs = response.Content.Data.Keys.Select(uint.Parse).ToHashSet();
} catch (Exception e) {
ASF.ArchiLogger.LogGenericException(e);
ASF.ArchiLogger.LogGenericError("Failed to parse badge data for free packages");
ASF.ArchiLogger.LogGenericError(Strings.BadgeDataParsingFailed);

return;
}
Expand Down
3 changes: 1 addition & 2 deletions FreePackages/Data/GlobalCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Helpers;
using ArchiSteamFarm.Helpers.Json;
using ArchiSteamFarm.Localization;

namespace FreePackages {
internal sealed class GlobalCache : SerializableFile {
Expand Down Expand Up @@ -34,7 +33,7 @@ internal GlobalCache() {
string json = await File.ReadAllTextAsync(SharedFilePath).ConfigureAwait(false);

if (string.IsNullOrEmpty(json)) {
ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorIsEmpty, nameof(json)));
ASF.ArchiLogger.LogGenericError(string.Format(ArchiSteamFarm.Localization.Strings.ErrorIsEmpty, nameof(json)));

return null;
}
Expand Down
13 changes: 13 additions & 0 deletions FreePackages/FreePackages.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<LangVersion>latest</LangVersion>
<TargetFramework>net8.0</TargetFramework>
<Deterministic>true</Deterministic>
<CoreCompileDependsOn>PrepareResources;$(CompileDependsOn)</CoreCompileDependsOn>
</PropertyGroup>

<ItemGroup>
Expand All @@ -21,4 +22,16 @@
<InternalsVisibleTo Include="FreePackages.Tests" />
</ItemGroup>

<!-- https://www.paraesthesia.com/archive/2022/09/30/strongly-typed-resources-with-net-core/ -->
<ItemGroup>
<EmbeddedResource Update="Localization\Strings.resx">
<Generator>MSBuild:Compile</Generator>
<LastGenOutput>Strings.Designer.cs</LastGenOutput>
<StronglyTypedFileName>$(IntermediateOutputPath)\Strings.Designer.cs</StronglyTypedFileName>
<StronglyTypedLanguage>CSharp</StronglyTypedLanguage>
<StronglyTypedNamespace>FreePackages.Localization</StronglyTypedNamespace>
<StronglyTypedClassName>Strings</StronglyTypedClassName>
</EmbeddedResource>
</ItemGroup>

</Project>
11 changes: 6 additions & 5 deletions FreePackages/Handlers/PICSHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Steam;
using FreePackages.Localization;
using SteamKit2;

namespace FreePackages {
Expand Down Expand Up @@ -37,7 +38,7 @@ internal async static Task OnPICSRestart(uint currentChangeNumber) {
}

uint oldChangeNumber = FreePackages.GlobalCache.LastChangeNumber;
ASF.ArchiLogger.LogGenericDebug(String.Format("PICS restarted, skipping from change number {0} to {1}", oldChangeNumber, currentChangeNumber));
ASF.ArchiLogger.LogGenericDebug(String.Format(Strings.PICSRestart, oldChangeNumber, currentChangeNumber));

// ASF restarts PICS if either apps or packages needs a full update. Check the old change number, as one of them might still be good.
SteamApps.PICSChangesCallback? picsChanges = await FetchPICSChanges(oldChangeNumber, sendAppChangeList: false, sendPackageChangeList: true).ConfigureAwait(false);
Expand All @@ -48,12 +49,12 @@ internal async static Task OnPICSRestart(uint currentChangeNumber) {
if (!picsChanges.RequiresFullAppUpdate) {
PackageHandler.AddChanges(picsChanges.AppChanges, new Dictionary<uint, SteamApps.PICSChangesCallback.PICSChangeData>());
} else {
ASF.ArchiLogger.LogGenericDebug("Possibly missed some free apps due to PICS restart");
ASF.ArchiLogger.LogGenericDebug(Strings.MissedApps);

// Search for the oldest change number which is still valid for apps
var appChanges = await FindOldestPICSChanges(oldChangeNumber + 1, picsChanges.CurrentChangeNumber, findApps: true);
if (appChanges != null) {
ASF.ArchiLogger.LogGenericDebug(String.Format("Recovered {0} app changes at change number {1}", appChanges.AppChanges.Count, appChanges.LastChangeNumber + 1));
ASF.ArchiLogger.LogGenericDebug(String.Format(Strings.RecoveredApps, appChanges.AppChanges.Count, appChanges.LastChangeNumber + 1));

PackageHandler.AddChanges(appChanges.AppChanges, new Dictionary<uint, SteamApps.PICSChangesCallback.PICSChangeData>());
}
Expand All @@ -62,12 +63,12 @@ internal async static Task OnPICSRestart(uint currentChangeNumber) {
if (!picsChanges.RequiresFullPackageUpdate) {
PackageHandler.AddChanges(new Dictionary<uint, SteamApps.PICSChangesCallback.PICSChangeData>(), picsChanges.PackageChanges);
} else {
ASF.ArchiLogger.LogGenericDebug("Possibly missed some free packages due to PICS restart");
ASF.ArchiLogger.LogGenericDebug(Strings.MissedPackages);

// Search for the oldest change number which is still valid for packages
var packageChanges = await FindOldestPICSChanges(oldChangeNumber + 1, picsChanges.CurrentChangeNumber, findApps: false);
if (packageChanges != null) {
ASF.ArchiLogger.LogGenericDebug(String.Format("Recovered {0} package changes at change number {1}", packageChanges.PackageChanges.Count, packageChanges.LastChangeNumber + 1));
ASF.ArchiLogger.LogGenericDebug(String.Format(Strings.RecoveredPackages, packageChanges.PackageChanges.Count, packageChanges.LastChangeNumber + 1));

PackageHandler.AddChanges(new Dictionary<uint, SteamApps.PICSChangesCallback.PICSChangeData>(), packageChanges.PackageChanges);
}
Expand Down
24 changes: 12 additions & 12 deletions FreePackages/Handlers/PackageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
using System.Threading;
using System.Threading.Tasks;
using ArchiSteamFarm.Core;
using ArchiSteamFarm.Localization;
using ArchiSteamFarm.Steam;
using FreePackages.Localization;
using SteamKit2;

namespace FreePackages {
Expand Down Expand Up @@ -47,14 +47,14 @@ internal static async Task AddHandler(Bot bot, List<FilterConfig> filterConfigs,
int numBotsThatIncludePlaytests = Handlers.Values.Where(x => x.PackageFilter.FilterConfigs.Any(filterConfig => filterConfig.PlaytestMode != EPlaytestMode.None)).Count();
if (numBotsThatIncludePlaytests > 0) {
filterConfigs.ForEach(filterConfig => filterConfig.PlaytestMode = EPlaytestMode.None);
bot.ArchiLogger.LogGenericInfo("Changed PlaytestMode to 0 (None), only 1 bot is allowed to use this filter");
bot.ArchiLogger.LogGenericInfo(Strings.PlaytestConfigLimitTriggered);
}
}

string cacheFilePath = Bot.GetFilePath(String.Format("{0}_{1}", bot.BotName, nameof(FreePackages)), Bot.EFileType.Database);
BotCache? botCache = await BotCache.CreateOrLoad(cacheFilePath).ConfigureAwait(false);
if (botCache == null) {
bot.ArchiLogger.LogGenericError(String.Format(Strings.ErrorDatabaseInvalid, cacheFilePath));
bot.ArchiLogger.LogGenericError(String.Format(ArchiSteamFarm.Localization.Strings.ErrorDatabaseInvalid, cacheFilePath));
botCache = new(cacheFilePath);
}

Expand Down Expand Up @@ -98,7 +98,7 @@ private async Task FetchUserData() {
UserData? userData = await WebRequest.GetUserData(Bot).ConfigureAwait(false);
if (userData == null) {
UserDataRefreshTimer.Change(TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(15));
Bot.ArchiLogger.LogGenericError(String.Format(Strings.ErrorObjectIsNull, userData));
Bot.ArchiLogger.LogGenericError(String.Format(ArchiSteamFarm.Localization.Strings.ErrorObjectIsNull, userData));

return;
}
Expand Down Expand Up @@ -425,21 +425,21 @@ internal string ClearQueue() {
int numChangedPackages = BotCache.ChangedPackages.Count;

if (numPackages == 0 && numChangedApps == 0 && numChangedPackages == 0) {
return "Queue is empty";
return Strings.QueueEmpty;
}

BotCache.Clear();

HashSet<string> responses = new HashSet<string>();

if (numPackages > 0) {
responses.Add(String.Format("{0} free packages removed.", numPackages));
responses.Add(String.Format(Strings.PackagesRemoved, numPackages));
}
if (numChangedApps > 0) {
responses.Add(String.Format("{0} discovered apps removed.", numChangedApps));
responses.Add(String.Format(Strings.DiscoveredAppsRemoved, numChangedApps));
}
if (numChangedPackages > 0) {
responses.Add(String.Format("{0} discovered packages removed.", numChangedPackages));
responses.Add(String.Format(Strings.DiscoveredPackagesRemoved, numChangedPackages));
}

return String.Join(" ", responses);
Expand All @@ -450,20 +450,20 @@ internal string AddPackage(EPackageType type, uint id, bool useFilter) {
if (type == EPackageType.App) {
BotCache.AddChanges(appIDs: new HashSet<uint> {id});

return String.Format("Added app/{0} to discovered apps queue", id);
return String.Format(Strings.DiscoveredAppsAdded, String.Format("app/{0}", id));
} else {
BotCache.AddChanges(packageIDs: new HashSet<uint> {id});

return String.Format("Added sub/{0} to discovered packages queue", id);
return String.Format(Strings.DiscoveredPackagesAdded, String.Format("sub/{0}", id));
}
}

PackageQueue.AddPackage(new Package(type, id));

if (type == EPackageType.App) {
return String.Format("Added app/{0} to free packages queue", id);
return String.Format(Strings.AppsQueued, String.Format("app/{0}", id));
} else {
return String.Format("Added sub/{0} to free packages queue", id);
return String.Format(Strings.PackagesQueued, String.Format("sub/{0}", id));
}
}

Expand Down
Loading

0 comments on commit 5803cf8

Please sign in to comment.