Skip to content
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

CheckTranslationReference lint pass learns additional checks #21389

Merged
merged 4 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 21 additions & 16 deletions OpenRA.Game/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,22 +368,7 @@ static void Initialize(Arguments args)
Settings.Game.Platform = p;
try
{
var rendererPath = Path.Combine(Platform.BinDir, "OpenRA.Platforms." + p + ".dll");

#if NET5_0_OR_GREATER
var loader = new AssemblyLoader(rendererPath);
var platformType = loader.LoadDefaultAssembly().GetTypes().SingleOrDefault(t => typeof(IPlatform).IsAssignableFrom(t));

#else
// NOTE: This is currently the only use of System.Reflection in this file, so would give an unused using error if we import it above
var assembly = System.Reflection.Assembly.LoadFile(rendererPath);
var platformType = assembly.GetTypes().SingleOrDefault(t => typeof(IPlatform).IsAssignableFrom(t));
#endif

if (platformType == null)
throw new InvalidOperationException("Platform dll must include exactly one IPlatform implementation.");

var platform = (IPlatform)platformType.GetConstructor(Type.EmptyTypes).Invoke(null);
var platform = CreatePlatform(p);
Renderer = new Renderer(platform, Settings.Graphics);
Sound = new Sound(platform, Settings.Sound);

Expand Down Expand Up @@ -440,6 +425,26 @@ static void Initialize(Arguments args)
InitializeMod(modID, args);
}

public static IPlatform CreatePlatform(string platformName)
{
var rendererPath = Path.Combine(Platform.BinDir, "OpenRA.Platforms." + platformName + ".dll");

#if NET5_0_OR_GREATER
var loader = new AssemblyLoader(rendererPath);
var platformType = loader.LoadDefaultAssembly().GetTypes().SingleOrDefault(t => typeof(IPlatform).IsAssignableFrom(t));

#else
// NOTE: This is currently the only use of System.Reflection in this file, so would give an unused using error if we import it above
var assembly = System.Reflection.Assembly.LoadFile(rendererPath);
var platformType = assembly.GetTypes().SingleOrDefault(t => typeof(IPlatform).IsAssignableFrom(t));
#endif

if (platformType == null)
throw new InvalidOperationException("Platform dll must include exactly one IPlatform implementation.");

return (IPlatform)platformType.GetConstructor(Type.EmptyTypes).Invoke(null);
}

public static void InitializeMod(string mod, Arguments args)
{
// Clear static state if we have switched mods
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Game/Graphics/SpriteFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public sealed class SpriteFont : IDisposable

float deviceScale;

public SpriteFont(string name, byte[] data, int size, int ascender, float scale, SheetBuilder builder)
public SpriteFont(IPlatform platform, string name, byte[] data, int size, int ascender, float scale, SheetBuilder builder)
{
if (builder.Type != SheetType.BGRA)
throw new ArgumentException("The sheet builder must create BGRA sheets.", nameof(builder));
Expand All @@ -36,7 +36,7 @@ public SpriteFont(string name, byte[] data, int size, int ascender, float scale,
this.size = size;
this.builder = builder;

font = Game.Renderer.CreateFont(data);
font = platform.CreateFont(data);
glyphs = new Cache<char, GlyphInfo>(CreateGlyph);
contrastGlyphs = new Cache<(char, int), Sprite>(CreateContrastGlyph);
dilationElements = new Cache<int, float[]>(CreateCircularWeightMap);
Expand Down
10 changes: 3 additions & 7 deletions OpenRA.Game/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ public void InitializeFonts(ModData modData)
fontSheetBuilder?.Dispose();
fontSheetBuilder = new SheetBuilder(SheetType.BGRA, 512);
Fonts = modData.Manifest.Get<Fonts>().FontList.ToDictionary(x => x.Key,
x => new SpriteFont(x.Value.Font, modData.DefaultFileSystem.Open(x.Value.Font).ReadAllBytes(),
x.Value.Size, x.Value.Ascender, Window.EffectiveWindowScale, fontSheetBuilder));
x => new SpriteFont(
platform, x.Value.Font, modData.DefaultFileSystem.Open(x.Value.Font).ReadAllBytes(),
x.Value.Size, x.Value.Ascender, Window.EffectiveWindowScale, fontSheetBuilder));
}

Window.OnWindowScaleChanged += (oldNative, oldEffective, newNative, newEffective) =>
Expand Down Expand Up @@ -563,11 +564,6 @@ public bool SetClipboardText(string text)

public string GLVersion => Context.GLVersion;

public IFont CreateFont(byte[] data)
{
return platform.CreateFont(data);
}

public int DisplayCount => Window.DisplayCount;

public int CurrentDisplay => Window.CurrentDisplay;
Expand Down
6 changes: 3 additions & 3 deletions OpenRA.Game/Translation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class Translation
readonly FluentBundle bundle;

public Translation(string language, string[] translations, IReadOnlyFileSystem fileSystem)
: this(language, translations, fileSystem, error => Log.Write("debug", error.ToString())) { }
: this(language, translations, fileSystem, error => Log.Write("debug", error.Message)) { }

public Translation(string language, string[] translations, IReadOnlyFileSystem fileSystem, Action<ParseError> onError)
{
Expand Down Expand Up @@ -92,12 +92,12 @@ void ParseTranslations(string language, string[] translations, IReadOnlyFileSyst
{
// Always load english strings to provide a fallback for missing translations.
// It is important to load the english files first so the chosen language's files can override them.
var paths = translations.Where(t => t.EndsWith("en.ftl", StringComparison.Ordinal)).ToHashSet();
var paths = translations.Where(t => t.EndsWith("en.ftl", StringComparison.Ordinal)).ToList();
foreach (var t in translations)
if (t.EndsWith($"{language}.ftl", StringComparison.Ordinal))
paths.Add(t);

foreach (var path in paths)
foreach (var path in paths.Distinct())
{
var stream = fileSystem.Open(path);
using (var reader = new StreamReader(stream))
Expand Down