Remove our own impl of ReadOnlyList and update usages

This commit is contained in:
teinarss
2021-03-16 20:08:18 +01:00
committed by reaperrr
parent e12ff2c59d
commit 8b0a3ea680
17 changed files with 20 additions and 114 deletions

View File

@@ -115,7 +115,7 @@ namespace OpenRA
if (filterNode != null)
yamlNodes = yamlNodes.Where(k => !filterNode(k));
return new Dictionary<string, T>(yamlNodes.ToDictionaryWithConflictLog(k => k.Key.ToLowerInvariant(), makeObject, "LoadFromManifest<" + name + ">"));
return yamlNodes.ToDictionaryWithConflictLog(k => k.Key.ToLowerInvariant(), makeObject, "LoadFromManifest<" + name + ">");
}
public static Ruleset LoadDefaults(ModData modData)

View File

@@ -21,14 +21,12 @@ namespace OpenRA.Graphics
public int Height { get; private set; }
readonly Dictionary<string, ImmutablePalette> palettes = new Dictionary<string, ImmutablePalette>();
readonly Dictionary<string, MutablePalette> modifiablePalettes = new Dictionary<string, MutablePalette>();
readonly IReadOnlyDictionary<string, MutablePalette> readOnlyModifiablePalettes;
readonly Dictionary<string, int> indices = new Dictionary<string, int>();
byte[] buffer = new byte[0];
public HardwarePalette()
{
Texture = Game.Renderer.Context.CreateTexture();
readOnlyModifiablePalettes = modifiablePalettes;
}
public bool Contains(string name)
@@ -109,7 +107,7 @@ namespace OpenRA.Graphics
public void ApplyModifiers(IEnumerable<IPaletteModifier> paletteMods)
{
foreach (var mod in paletteMods)
mod.AdjustPalette(readOnlyModifiablePalettes);
mod.AdjustPalette(modifiablePalettes);
// Update our texture with the changes.
CopyModifiablePalettesToBuffer();

View File

@@ -216,8 +216,7 @@ namespace OpenRA
if (!yaml.ContainsKey(key))
return new Dictionary<string, string>();
var inner = yaml[key].ToDictionary(my => my.Value);
return inner;
return yaml[key].ToDictionary(my => my.Value);
}
public bool Contains<T>() where T : IGlobalModData

View File

@@ -52,7 +52,7 @@ namespace OpenRA.Network
readonly List<ChatLine> chatCache = new List<ChatLine>();
public readonly ReadOnlyList<ChatLine> ChatCache;
public IReadOnlyList<ChatLine> ChatCache => chatCache;
bool disposed;
bool generateSyncReport = false;
@@ -85,7 +85,6 @@ namespace OpenRA.Network
Password = password;
Connection = conn;
syncReport = new SyncReport(this);
ChatCache = new ReadOnlyList<ChatLine>(chatCache);
AddChatLine += CacheChatLine;
}

View File

@@ -1,81 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
namespace OpenRA
{
/// <summary>
/// A minimal read only list interface for .NET 4
/// </summary>
/// <remarks>
/// .NET 4.5 has an implementation built-in, this code is not meant to
/// duplicate it but provide a compatible interface that can be replaced
/// when we switch to .NET 4.5 or higher.
/// </remarks>
public interface IReadOnlyList<out T> : IEnumerable<T>
{
int Count { get; }
T this[int index] { get; }
}
public static class ReadOnlyList
{
public static IReadOnlyList<T> AsReadOnly<T>(this IList<T> list)
{
return list as IReadOnlyList<T> ?? new ReadOnlyList<T>(list);
}
}
/// <summary>
/// A minimal read only list for .NET 4 implemented as a wrapper
/// around an IList.
/// </summary>
public class ReadOnlyList<T> : IReadOnlyList<T>
{
private readonly IList<T> list;
public ReadOnlyList()
: this(new List<T>())
{
}
public ReadOnlyList(IList<T> list)
{
if (list == null)
throw new ArgumentNullException(nameof(list));
this.list = list;
}
#region IEnumerable implementation
public IEnumerator<T> GetEnumerator()
{
return list.GetEnumerator();
}
#endregion
#region IEnumerable implementation
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return list.GetEnumerator();
}
#endregion
#region IReadOnlyList implementation
public int Count => list.Count;
public T this[int index] => list[index];
#endregion
}
}

View File

@@ -10,6 +10,7 @@
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
@@ -111,7 +112,7 @@ namespace OpenRA.Mods.Cnc.SpriteLoaders
}
}
Frames = frames.AsReadOnly();
Frames = frames;
}
}
}

View File

@@ -134,7 +134,6 @@ namespace OpenRA.Mods.Cnc.SpriteLoaders
stream.Position += 4;
var headers = new ImageHeader[imageCount];
Frames = headers.AsReadOnly();
for (var i = 0; i < headers.Length; i++)
headers[i] = new ImageHeader(stream, this);
@@ -156,6 +155,8 @@ namespace OpenRA.Mods.Cnc.SpriteLoaders
foreach (var h in headers)
Decompress(h);
Frames = headers;
}
void Decompress(ImageHeader h)

View File

@@ -42,8 +42,7 @@ namespace OpenRA.Mods.Cnc.Traits
public IReadOnlyDictionary<CPos, SubCell> OccupiedCells(ActorInfo info, CPos location, SubCell subCell = SubCell.Any)
{
var occupied = new Dictionary<CPos, SubCell>() { { location, SubCell.FullCell } };
return occupied;
return new Dictionary<CPos, SubCell>() { { location, SubCell.FullCell } };
}
bool IOccupySpaceInfo.SharesCell => false;

View File

@@ -9,6 +9,7 @@
*/
#endregion
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenRA.Graphics;
@@ -74,7 +75,7 @@ namespace OpenRA.Mods.Common.SpriteLoaders
public DdsSprite(Stream stream)
{
Frames = new ISpriteFrame[] { new DdsFrame(stream) }.AsReadOnly();
Frames = new ISpriteFrame[] { new DdsFrame(stream) };
}
}
}

View File

@@ -9,6 +9,7 @@
*/
#endregion
using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenRA.Graphics;
@@ -108,7 +109,7 @@ namespace OpenRA.Mods.Common.SpriteLoaders
public TgaSprite(Stream stream)
{
Frames = new ISpriteFrame[] { new TgaFrame(stream) }.AsReadOnly();
Frames = new ISpriteFrame[] { new TgaFrame(stream) };
}
}
}

View File

@@ -239,10 +239,8 @@ namespace OpenRA.Mods.Common.Traits
public IReadOnlyDictionary<CPos, SubCell> OccupiedCells(ActorInfo info, CPos topLeft, SubCell subCell = SubCell.Any)
{
var occupied = OccupiedTiles(topLeft)
return OccupiedTiles(topLeft)
.ToDictionary(c => c, c => SubCell.FullCell);
return occupied;
}
bool IOccupySpaceInfo.SharesCell => false;

View File

@@ -34,8 +34,7 @@ namespace OpenRA.Mods.Common.Traits
public IReadOnlyDictionary<CPos, SubCell> OccupiedCells(ActorInfo info, CPos location, SubCell subCell = SubCell.Any)
{
var occupied = new Dictionary<CPos, SubCell>() { { location, SubCell.FullCell } };
return occupied;
return new Dictionary<CPos, SubCell>() { { location, SubCell.FullCell } };
}
bool IOccupySpaceInfo.SharesCell => false;

View File

@@ -37,8 +37,7 @@ namespace OpenRA.Mods.Common.Traits
public IReadOnlyDictionary<CPos, SubCell> OccupiedCells(ActorInfo info, CPos location, SubCell subCell = SubCell.Any)
{
var occupied = new Dictionary<CPos, SubCell>() { { location, SubCell.FullCell } };
return occupied;
return new Dictionary<CPos, SubCell>() { { location, SubCell.FullCell } };
}
bool IOccupySpaceInfo.SharesCell => false;

View File

@@ -21,10 +21,8 @@ namespace OpenRA.Mods.Common.Traits
public IReadOnlyDictionary<CPos, SubCell> OccupiedCells(ActorInfo info, CPos location, SubCell subCell = SubCell.Any)
{
var occupied = OccupiesSpace ? new Dictionary<CPos, SubCell>() { { location, SubCell.FullCell } } :
return OccupiesSpace ? new Dictionary<CPos, SubCell>() { { location, SubCell.FullCell } } :
new Dictionary<CPos, SubCell>();
return occupied;
}
bool IOccupySpaceInfo.SharesCell => false;

View File

@@ -67,7 +67,7 @@ namespace OpenRA.Mods.Common.Traits
public readonly MissionObjectivesInfo Info;
readonly List<MissionObjective> objectives = new List<MissionObjective>();
readonly Player player;
public ReadOnlyList<MissionObjective> Objectives;
public IReadOnlyList<MissionObjective> Objectives => objectives;
Player[] enemies;
Player[] allies;
@@ -92,7 +92,6 @@ namespace OpenRA.Mods.Common.Traits
{
Info = info;
this.player = player;
Objectives = new ReadOnlyList<MissionObjective>(objectives);
}
void IWorldLoaded.WorldLoaded(World w, WorldRenderer wr)

View File

@@ -77,12 +77,7 @@ namespace OpenRA.Mods.Common.Traits
var radarColorInfo = Info.TraitInfoOrDefault<RadarColorFromTerrainInfo>();
RadarColor = radarColorInfo == null ? owner.Color : radarColorInfo.GetColorFromTerrain(world);
if (ios != null)
Footprint = ios.OccupiedCells(Info, location, subCell);
else
{
Footprint = new Dictionary<CPos, SubCell>() { { location, SubCell.FullCell } };
}
Footprint = ios?.OccupiedCells(Info, location, subCell) ?? new Dictionary<CPos, SubCell>() { { location, SubCell.FullCell } };
tooltip = Info.TraitInfos<EditorOnlyTooltipInfo>().FirstOrDefault(info => info.EnabledByDefault) as TooltipInfoBase
?? Info.TraitInfos<TooltipInfo>().FirstOrDefault(info => info.EnabledByDefault);