Skip to content

Commit

Permalink
Use Tuple syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
teinarss authored and pchote committed Aug 15, 2020
1 parent 8a74f6e commit 19b0287
Show file tree
Hide file tree
Showing 90 changed files with 738 additions and 826 deletions.
58 changes: 25 additions & 33 deletions OpenRA.Game/Graphics/ChromeProvider.cs
Expand Up @@ -54,10 +54,10 @@ public class Collection

public static IReadOnlyDictionary<string, Collection> Collections { get; private set; }
static Dictionary<string, Collection> collections;
static Dictionary<string, Pair<Sheet, int>> cachedSheets;
static Dictionary<string, (Sheet Sheet, int Density)> cachedSheets;
static Dictionary<string, Dictionary<string, Sprite>> cachedSprites;
static Dictionary<string, Sprite[]> cachedPanelSprites;
static Dictionary<Collection, Pair<Sheet, int>> cachedCollectionSheets;
static Dictionary<Collection, (Sheet Sheet, int)> cachedCollectionSheets;

static IReadOnlyFileSystem fileSystem;
static float dpiScale = 1;
Expand All @@ -72,10 +72,10 @@ public static void Initialize(ModData modData)

fileSystem = modData.DefaultFileSystem;
collections = new Dictionary<string, Collection>();
cachedSheets = new Dictionary<string, Pair<Sheet, int>>();
cachedSheets = new Dictionary<string, (Sheet, int)>();
cachedSprites = new Dictionary<string, Dictionary<string, Sprite>>();
cachedPanelSprites = new Dictionary<string, Sprite[]>();
cachedCollectionSheets = new Dictionary<Collection, Pair<Sheet, int>>();
cachedCollectionSheets = new Dictionary<Collection, (Sheet, int)>();

Collections = new ReadOnlyDictionary<string, Collection>(collections);

Expand All @@ -91,7 +91,7 @@ public static void Deinitialize()
{
if (cachedSheets != null)
foreach (var sheet in cachedSheets.Values)
sheet.First.Dispose();
sheet.Sheet.Dispose();

collections = null;
cachedSheets = null;
Expand All @@ -108,12 +108,10 @@ static void LoadCollection(string name, MiniYaml yaml)
collections.Add(name, FieldLoader.Load<Collection>(yaml));
}

static Pair<Sheet, int> SheetForCollection(Collection c)
static (Sheet Sheet, int Density) SheetForCollection(Collection c)
{
Pair<Sheet, int> sheetDensity;

// Outer cache avoids recalculating image names
if (!cachedCollectionSheets.TryGetValue(c, out sheetDensity))
if (!cachedCollectionSheets.TryGetValue(c, out (Sheet, int) sheetDensity))
{
var image = c.Image;
var density = 1;
Expand All @@ -137,7 +135,7 @@ static void LoadCollection(string name, MiniYaml yaml)

sheet.GetTexture().ScaleFilter = TextureScaleFilter.Linear;

sheetDensity = Pair.New(sheet, density);
sheetDensity = (sheet, density);
cachedSheets.Add(image, sheetDensity);
}

Expand All @@ -153,13 +151,10 @@ public static Sprite GetImage(string collectionName, string imageName)
return null;

// Cached sprite
Dictionary<string, Sprite> cachedCollection;
Sprite sprite;
if (cachedSprites.TryGetValue(collectionName, out cachedCollection) && cachedCollection.TryGetValue(imageName, out sprite))
if (cachedSprites.TryGetValue(collectionName, out var cachedCollection) && cachedCollection.TryGetValue(imageName, out var sprite))
return sprite;

Collection collection;
if (!collections.TryGetValue(collectionName, out collection))
if (!collections.TryGetValue(collectionName, out var collection))
{
Log.Write("debug", "Could not find collection '{0}'", collectionName);
return null;
Expand All @@ -177,7 +172,7 @@ public static Sprite GetImage(string collectionName, string imageName)
cachedSprites.Add(collectionName, cachedCollection);
}

var image = new Sprite(sheetDensity.First, sheetDensity.Second * mi, TextureChannel.RGBA, 1f / sheetDensity.Second);
var image = new Sprite(sheetDensity.Sheet, sheetDensity.Density * mi, TextureChannel.RGBA, 1f / sheetDensity.Density);
cachedCollection.Add(imageName, image);

return image;
Expand All @@ -189,12 +184,10 @@ public static Sprite[] GetPanelImages(string collectionName)
return null;

// Cached sprite
Sprite[] cachedSprites;
if (cachedPanelSprites.TryGetValue(collectionName, out cachedSprites))
if (cachedPanelSprites.TryGetValue(collectionName, out var cachedSprites))
return cachedSprites;

Collection collection;
if (!collections.TryGetValue(collectionName, out collection))
if (!collections.TryGetValue(collectionName, out var collection))
{
Log.Write("debug", "Could not find collection '{0}'", collectionName);
return null;
Expand All @@ -214,20 +207,20 @@ public static Sprite[] GetPanelImages(string collectionName)
var pr = collection.PanelRegion;
var ps = collection.PanelSides;

var sides = new[]
var sides = new (PanelSides PanelSides, Rectangle Bounds)[]
{
Pair.New(PanelSides.Top | PanelSides.Left, new Rectangle(pr[0], pr[1], pr[2], pr[3])),
Pair.New(PanelSides.Top, new Rectangle(pr[0] + pr[2], pr[1], pr[4], pr[3])),
Pair.New(PanelSides.Top | PanelSides.Right, new Rectangle(pr[0] + pr[2] + pr[4], pr[1], pr[6], pr[3])),
Pair.New(PanelSides.Left, new Rectangle(pr[0], pr[1] + pr[3], pr[2], pr[5])),
Pair.New(PanelSides.Center, new Rectangle(pr[0] + pr[2], pr[1] + pr[3], pr[4], pr[5])),
Pair.New(PanelSides.Right, new Rectangle(pr[0] + pr[2] + pr[4], pr[1] + pr[3], pr[6], pr[5])),
Pair.New(PanelSides.Bottom | PanelSides.Left, new Rectangle(pr[0], pr[1] + pr[3] + pr[5], pr[2], pr[7])),
Pair.New(PanelSides.Bottom, new Rectangle(pr[0] + pr[2], pr[1] + pr[3] + pr[5], pr[4], pr[7])),
Pair.New(PanelSides.Bottom | PanelSides.Right, new Rectangle(pr[0] + pr[2] + pr[4], pr[1] + pr[3] + pr[5], pr[6], pr[7]))
(PanelSides.Top | PanelSides.Left, new Rectangle(pr[0], pr[1], pr[2], pr[3])),
(PanelSides.Top, new Rectangle(pr[0] + pr[2], pr[1], pr[4], pr[3])),
(PanelSides.Top | PanelSides.Right, new Rectangle(pr[0] + pr[2] + pr[4], pr[1], pr[6], pr[3])),
(PanelSides.Left, new Rectangle(pr[0], pr[1] + pr[3], pr[2], pr[5])),
(PanelSides.Center, new Rectangle(pr[0] + pr[2], pr[1] + pr[3], pr[4], pr[5])),
(PanelSides.Right, new Rectangle(pr[0] + pr[2] + pr[4], pr[1] + pr[3], pr[6], pr[5])),
(PanelSides.Bottom | PanelSides.Left, new Rectangle(pr[0], pr[1] + pr[3] + pr[5], pr[2], pr[7])),
(PanelSides.Bottom, new Rectangle(pr[0] + pr[2], pr[1] + pr[3] + pr[5], pr[4], pr[7])),
(PanelSides.Bottom | PanelSides.Right, new Rectangle(pr[0] + pr[2] + pr[4], pr[1] + pr[3] + pr[5], pr[6], pr[7]))
};

sprites = sides.Select(x => ps.HasSide(x.First) ? new Sprite(sheetDensity.First, sheetDensity.Second * x.Second, TextureChannel.RGBA, 1f / sheetDensity.Second) : null)
sprites = sides.Select(x => ps.HasSide(x.PanelSides) ? new Sprite(sheetDensity.Sheet, sheetDensity.Density * x.Bounds, TextureChannel.RGBA, 1f / sheetDensity.Density) : null)
.ToArray();
}
else
Expand Down Expand Up @@ -256,8 +249,7 @@ public static Size GetMinimumPanelSize(string collectionName)
if (string.IsNullOrEmpty(collectionName))
return new Size(0, 0);

Collection collection;
if (!collections.TryGetValue(collectionName, out collection))
if (!collections.TryGetValue(collectionName, out var collection))
{
Log.Write("debug", "Could not find collection '{0}'", collectionName);
return new Size(0, 0);
Expand Down
10 changes: 5 additions & 5 deletions OpenRA.Game/Graphics/ModelRenderer.cs
Expand Up @@ -48,7 +48,7 @@ public sealed class ModelRenderer : IDisposable

readonly Dictionary<Sheet, IFrameBuffer> mappedBuffers = new Dictionary<Sheet, IFrameBuffer>();
readonly Stack<KeyValuePair<Sheet, IFrameBuffer>> unmappedBuffers = new Stack<KeyValuePair<Sheet, IFrameBuffer>>();
readonly List<Pair<Sheet, Action>> doRender = new List<Pair<Sheet, Action>>();
readonly List<(Sheet Sheet, Action Func)> doRender = new List<(Sheet, Action)>();

SheetBuilder sheetBuilderForFrame;
bool isInFrame;
Expand Down Expand Up @@ -180,7 +180,7 @@ public void SetViewportParams(Size screen, int2 scroll)
var correctionTransform = Util.MatrixMultiply(translateMtx, FlipMtx);
var shadowCorrectionTransform = Util.MatrixMultiply(shadowTranslateMtx, ShadowScaleFlipMtx);

doRender.Add(Pair.New<Sheet, Action>(sprite.Sheet, () =>
doRender.Add((sprite.Sheet, () =>
{
foreach (var m in models)
{
Expand Down Expand Up @@ -324,16 +324,16 @@ public void EndFrame()
foreach (var v in doRender)
{
// Change sheet
if (v.First != currentSheet)
if (v.Sheet != currentSheet)
{
if (fbo != null)
DisableFrameBuffer(fbo);

currentSheet = v.First;
currentSheet = v.Sheet;
fbo = EnableFrameBuffer(currentSheet);
}

v.Second();
v.Func();
}

if (fbo != null)
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Game/Graphics/PlayerColorRemap.cs
Expand Up @@ -47,8 +47,8 @@ public PlayerColorRemap(int[] ramp, Color c, float rampFraction)
remapRamp = ramp.Select(r => r - ramp[rampMaxIndex]);
}

remapColors = remapRamp.Select((x, i) => Pair.New(baseIndex + i, Exts.ColorLerp(x / (float)ramp.Length, c1, c2)))
.ToDictionary(u => u.First, u => u.Second);
remapColors = remapRamp.Select((x, i) => (baseIndex + i, Exts.ColorLerp(x / (float)ramp.Length, c1, c2)))
.ToDictionary(u => u.Item1, u => u.Item2);
}

public Color GetRemappedColor(Color original, int index)
Expand Down
30 changes: 15 additions & 15 deletions OpenRA.Game/Graphics/SpriteFont.cs
Expand Up @@ -23,8 +23,8 @@ public sealed class SpriteFont : IDisposable
readonly SheetBuilder builder;
readonly Func<string, float> lineWidth;
readonly IFont font;
readonly Cache<Pair<char, Color>, GlyphInfo> glyphs;
readonly Cache<Tuple<char, Color, int>, Sprite> contrastGlyphs;
readonly Cache<(char C, Color Color), GlyphInfo> glyphs;
readonly Cache<(char, Color, int), Sprite> contrastGlyphs;
readonly Cache<int, float[]> dilationElements;

float deviceScale;
Expand All @@ -40,12 +40,12 @@ public SpriteFont(string name, byte[] data, int size, int ascender, float scale,

font = Game.Renderer.CreateFont(data);

glyphs = new Cache<Pair<char, Color>, GlyphInfo>(CreateGlyph, Pair<char, Color>.EqualityComparer);
contrastGlyphs = new Cache<Tuple<char, Color, int>, Sprite>(CreateContrastGlyph);
glyphs = new Cache<(char, Color), GlyphInfo>(CreateGlyph);
contrastGlyphs = new Cache<(char, Color, int), Sprite>(CreateContrastGlyph);
dilationElements = new Cache<int, float[]>(CreateCircularWeightMap);

// PERF: Cache these delegates for Measure calls.
Func<char, float> characterWidth = character => glyphs[Pair.New(character, Color.White)].Advance;
Func<char, float> characterWidth = character => glyphs[(character, Color.White)].Advance;
lineWidth = line => line.Sum(characterWidth) / deviceScale;

if (size <= 24)
Expand All @@ -65,7 +65,7 @@ void PrecacheColor(Color c, string name)
{
using (new PerfTimer("PrecacheColor {0} {1}px {2}".F(name, size, c)))
for (var n = (char)0x20; n < (char)0x7f; n++)
if (glyphs[Pair.New(n, c)] == null)
if (glyphs[(n, c)] == null)
throw new InvalidOperationException();
}

Expand All @@ -87,12 +87,12 @@ void DrawTextContrast(string text, float2 location, Color contrastColor, int con
continue;
}

var g = glyphs[Pair.New(s, Color.Black)];
var g = glyphs[(s, Color.Black)];

// Convert screen coordinates back to UI coordinates for drawing
if (g.Sprite != null)
{
var contrastSprite = contrastGlyphs[Tuple.Create(s, contrastColor, screenContrast)];
var contrastSprite = contrastGlyphs[(s, contrastColor, screenContrast)];
Game.Renderer.RgbaSpriteRenderer.DrawSprite(contrastSprite,
(screen + g.Offset - contrastVector) / deviceScale,
contrastSprite.Size / deviceScale);
Expand All @@ -118,7 +118,7 @@ public void DrawText(string text, float2 location, Color c)
continue;
}

var g = glyphs[Pair.New(s, c)];
var g = glyphs[(s, c)];

// Convert screen coordinates back to UI coordinates for drawing
if (g.Sprite != null)
Expand Down Expand Up @@ -155,7 +155,7 @@ public void DrawText(string text, float2 location, Color c, float angle)
continue;
}

var g = glyphs[Pair.New(s, c)];
var g = glyphs[(s, c)];
if (g.Sprite != null)
{
var tl = new float2(
Expand Down Expand Up @@ -241,9 +241,9 @@ public int2 Measure(string text)
return new int2((int)Math.Ceiling(lines.Max(lineWidth)), lines.Length * size);
}

GlyphInfo CreateGlyph(Pair<char, Color> c)
GlyphInfo CreateGlyph((char C, Color Color) c)
{
var glyph = font.CreateGlyph(c.First, size, deviceScale);
var glyph = font.CreateGlyph(c.C, size, deviceScale);

if (glyph.Data == null)
{
Expand Down Expand Up @@ -274,7 +274,7 @@ GlyphInfo CreateGlyph(Pair<char, Color> c)
if (p != 0)
{
var q = destStride * (j + s.Bounds.Top) + 4 * (i + s.Bounds.Left);
var pmc = Util.PremultiplyAlpha(Color.FromArgb(p, c.Second));
var pmc = Util.PremultiplyAlpha(Color.FromArgb(p, c.Color));

dest[q] = pmc.B;
dest[q + 1] = pmc.G;
Expand Down Expand Up @@ -347,10 +347,10 @@ float[] CreateCircularWeightMap(int r)
return elem;
}

Sprite CreateContrastGlyph(Tuple<char, Color, int> c)
Sprite CreateContrastGlyph((char, Color, int) c)
{
// Source glyph color doesn't matter, so use black
var glyph = glyphs[Pair.New(c.Item1, Color.Black)];
var glyph = glyphs[(c.Item1, Color.Black)];
var color = c.Item2;
var r = c.Item3;

Expand Down
12 changes: 6 additions & 6 deletions OpenRA.Game/InstalledMods.cs
Expand Up @@ -34,9 +34,9 @@ public InstalledMods(IEnumerable<string> searchPaths, IEnumerable<string> explic
mods = GetInstalledMods(searchPaths, explicitPaths);
}

static IEnumerable<Pair<string, string>> GetCandidateMods(IEnumerable<string> searchPaths)
static IEnumerable<(string Id, string Path)> GetCandidateMods(IEnumerable<string> searchPaths)
{
var mods = new List<Pair<string, string>>();
var mods = new List<(string, string)>();
foreach (var path in searchPaths)
{
try
Expand All @@ -47,7 +47,7 @@ public InstalledMods(IEnumerable<string> searchPaths, IEnumerable<string> explic

var directory = new DirectoryInfo(resolved);
foreach (var subdir in directory.EnumerateDirectories())
mods.Add(Pair.New(subdir.Name, subdir.FullName));
mods.Add((subdir.Name, subdir.FullName));
}
catch (Exception e)
{
Expand Down Expand Up @@ -88,13 +88,13 @@ Manifest LoadMod(string id, string path)
{
var ret = new Dictionary<string, Manifest>();
var candidates = GetCandidateMods(searchPaths)
.Concat(explicitPaths.Select(p => Pair.New(Path.GetFileNameWithoutExtension(p), p)));
.Concat(explicitPaths.Select(p => (Id: Path.GetFileNameWithoutExtension(p), Path: p)));

foreach (var pair in candidates)
{
var mod = LoadMod(pair.First, pair.Second);
var mod = LoadMod(pair.Id, pair.Path);
if (mod != null)
ret[pair.First] = mod;
ret[pair.Id] = mod;
}

return ret;
Expand Down

0 comments on commit 19b0287

Please sign in to comment.