Use Task for validation

locales validation is now a MSBuild task instead and will run as part of the build process. The emulator will still need to be ran once to fix locales/langauges after a new addition, but will no longer crash
This commit is contained in:
LotP1
2024-12-17 02:45:36 +01:00
parent f00beaecce
commit 798082c785
7 changed files with 660 additions and 611 deletions

View File

@@ -0,0 +1,67 @@
using System;
using Microsoft.Build.Utilities;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Newtonsoft.Json;
using Microsoft.Build.Framework;
namespace Ryujinx.BuildValidationTasks
{
public class LocaleValidationTask : Task
{
public override bool Execute()
{
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
path = new FileInfo(path).Directory.Parent.Parent.Parent.Parent.GetDirectories("Ryujinx")[0].GetDirectories("Assets")[0].GetFiles("locales.json")[0].FullName;
string data;
using (StreamReader sr = new StreamReader(path))
{
data = sr.ReadToEnd();
}
LocalesJSON json = JsonConvert.DeserializeObject<LocalesJSON>(data);
for (int i = 0; i < json.Locales.Count; i++)
{
LocalesEntry locale = json.Locales[i];
foreach (string language in json.Languages)
{
if (!locale.Translations.ContainsKey(language))
{
locale.Translations.Add(language, "");
Log.LogMessage(MessageImportance.High, $"Added {{{language}}} to Locale {{{locale.ID}}}");
}
}
locale.Translations = locale.Translations.OrderBy(pair => pair.Key).ToDictionary(pair => pair.Key, pair => pair.Value);
json.Locales[i] = locale;
}
string jsonString = JsonConvert.SerializeObject(json, Formatting.Indented);
using (StreamWriter sw = new StreamWriter(path))
{
sw.Write(jsonString);
}
return true;
}
struct LocalesJSON
{
public List<string> Languages { get; set; }
public List<LocalesEntry> Locales { get; set; }
}
struct LocalesEntry
{
public string ID { get; set; }
public Dictionary<string, string> Translations { get; set; }
}
}
}

View File

@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Build.Utilities.Core" />
<PackageReference Include="Newtonsoft.Json" />
</ItemGroup>
</Project>

File diff suppressed because it is too large Load Diff

View File

@@ -152,8 +152,6 @@ namespace Ryujinx.Ava.Common.Locale
var localeStrings = new Dictionary<LocaleKeys, string>();
string fileData = EmbeddedResources.ReadAllText($"Ryujinx/Assets/locales.json");
bool invalidLocalesJson = false; //does the locales.json contain all the languages in all the locales?
if (fileData == null)
{
// We were unable to find file for that language code.
@@ -167,12 +165,7 @@ namespace Ryujinx.Ava.Common.Locale
if (locale.Translations.Count != json.Languages.Count)
{
Logger.Error?.Print(LogClass.UI, $"Locale key {{{locale.ID}}} is missing languages!");
invalidLocalesJson = true;
#if DEBUG
break;
#else
throw new Exception("Missing locale data!");
#endif
}
if (Enum.TryParse<LocaleKeys>(locale.ID, out var localeKey))
@@ -189,45 +182,6 @@ namespace Ryujinx.Ava.Common.Locale
}
}
if (invalidLocalesJson)
{
for (int i = 0; i < json.Locales.Count; i++)
{
LocalesEntry locale = json.Locales[i];
foreach (string language in json.Languages)
{
if (!locale.Translations.ContainsKey(language))
{
locale.Translations.Add(language, "");
Logger.Warning?.Print(LogClass.UI, $"Added {{{language}}} to Locale {{{locale.ID}}}");
}
}
locale.Translations = locale.Translations.OrderBy(pair => pair.Key).ToDictionary(pair => pair.Key, pair => pair.Value);
json.Locales[i] = locale;
}
string location = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.GetDirectories("Assets")[0].GetFiles("locales.json")[0].FullName;
JsonSerializerOptions jsonOptions = new JsonSerializerOptions
{
WriteIndented = true,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
};
LocalesJSONContext context = new LocalesJSONContext(jsonOptions);
string jsonString = JsonHelper.Serialize(json, context.LocalesJSON);
using (StreamWriter sw = new StreamWriter(location))
{
sw.Write(jsonString);
}
throw new Exception("Missing locale data! (missing locale data has been added, please rebuild the project)");
}
return localeStrings;
}
}

View File

@@ -13,6 +13,12 @@
<DefaultItemExcludes>$(DefaultItemExcludes);._*</DefaultItemExcludes>
</PropertyGroup>
<UsingTask TaskName="Ryujinx.BuildValidationTasks.LocaleValidationTask" AssemblyFile="$(ProjectDir)..\Ryujinx.BuildValidationTasks\bin\debug\netstandard2.0\Ryujinx.BuildValidationTasks.dll" />
<Target Name="LocalesJsonValidation" BeforeTargets="BeforeBuild">
<LocaleValidationTask />
</Target>
<Target Name="PostBuild" AfterTargets="PostBuildEvent" Condition="$([MSBuild]::IsOSPlatform('OSX'))">
<Exec Command="codesign --entitlements '$(ProjectDir)..\..\distribution\macos\entitlements.xml' -f -s $(SigningCertificate) '$(TargetDir)$(TargetName)'" />
</Target>
@@ -60,6 +66,7 @@
<ItemGroup>
<ProjectReference Include="..\Ryujinx.Audio.Backends.SDL2\Ryujinx.Audio.Backends.SDL2.csproj" />
<ProjectReference Include="..\Ryujinx.BuildValidationTasks\Ryujinx.BuildValidationTasks.csproj" />
<ProjectReference Include="..\Ryujinx.Graphics.Vulkan\Ryujinx.Graphics.Vulkan.csproj" />
<ProjectReference Include="..\Ryujinx.Input\Ryujinx.Input.csproj" />
<ProjectReference Include="..\Ryujinx.Input.SDL2\Ryujinx.Input.SDL2.csproj" />