Skip to content

Commit

Permalink
Add readonly to structs
Browse files Browse the repository at this point in the history
  • Loading branch information
teinarss authored and abcdefg30 committed Mar 14, 2021
1 parent 65c796d commit 6b74093
Show file tree
Hide file tree
Showing 83 changed files with 146 additions and 125 deletions.
2 changes: 1 addition & 1 deletion OpenRA.Game/Actor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace OpenRA
{
public sealed class Actor : IScriptBindable, IScriptNotifyBind, ILuaTableBinding, ILuaEqualityBinding, ILuaToStringBinding, IEquatable<Actor>, IDisposable
{
internal struct SyncHash
internal readonly struct SyncHash
{
public readonly ISync Trait;
readonly Func<object, int> hashFunction;
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/CPos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace OpenRA
{
public struct CPos : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaEqualityBinding, ILuaTableBinding, IEquatable<CPos>
public readonly struct CPos : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaEqualityBinding, ILuaTableBinding, IEquatable<CPos>
{
// Coordinates are packed in a 32 bit signed int
// X and Y are 12 bits (signed): -2048...2047
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/CVec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace OpenRA
{
public struct CVec : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaUnaryMinusBinding, ILuaEqualityBinding, ILuaTableBinding, IEquatable<CVec>
public readonly struct CVec : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaUnaryMinusBinding, ILuaEqualityBinding, ILuaTableBinding, IEquatable<CVec>
{
public readonly int X, Y;

Expand Down
6 changes: 3 additions & 3 deletions OpenRA.Game/Graphics/Animation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Animation(World world, string name, Func<WAngle> facingFunc, Func<bool> p
public int CurrentFrame => backwards ? CurrentSequence.Length - frame - 1 : frame;
public Sprite Image => CurrentSequence.GetSprite(CurrentFrame, facingFunc());

public IRenderable[] Render(WPos pos, WVec offset, int zOffset, PaletteReference palette)
public IRenderable[] Render(WPos pos, in WVec offset, int zOffset, PaletteReference palette)
{
var tintModifiers = CurrentSequence.IgnoreWorldTint ? TintModifiers.IgnoreWorldTint : TintModifiers.None;
var alpha = CurrentSequence.GetAlpha(CurrentFrame);
Expand All @@ -68,7 +68,7 @@ public IRenderable[] Render(WPos pos, WVec offset, int zOffset, PaletteReference
return new IRenderable[] { imageRenderable };
}

public IRenderable[] RenderUI(WorldRenderer wr, int2 pos, WVec offset, int zOffset, PaletteReference palette, float scale = 1f)
public IRenderable[] RenderUI(WorldRenderer wr, int2 pos, in WVec offset, int zOffset, PaletteReference palette, float scale = 1f)
{
scale *= CurrentSequence.Scale;
var screenOffset = (scale * wr.ScreenVectorComponents(offset)).XY.ToInt2();
Expand All @@ -87,7 +87,7 @@ public IRenderable[] RenderUI(WorldRenderer wr, int2 pos, WVec offset, int zOffs
return new IRenderable[] { imageRenderable };
}

public Rectangle ScreenBounds(WorldRenderer wr, WPos pos, WVec offset)
public Rectangle ScreenBounds(WorldRenderer wr, WPos pos, in WVec offset)
{
var scale = CurrentSequence.Scale;
var xy = wr.ScreenPxPosition(pos) + wr.ScreenPxOffset(offset);
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Graphics/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface IModel
Rectangle AggregateBounds { get; }
}

public struct ModelRenderData
public readonly struct ModelRenderData
{
public readonly int Start;
public readonly int Count;
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Graphics/ModelAnimation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace OpenRA.Graphics
{
public struct ModelAnimation
public readonly struct ModelAnimation
{
public readonly IModel Model;
public readonly Func<WVec> OffsetFunc;
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Graphics/PlatformInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public enum PrimitiveType
TriangleList,
}

public struct Range<T>
public readonly struct Range<T>
{
public readonly T Start, End;
public Range(T start, T end) { Start = start; End = end; }
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Graphics/Renderable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface IRenderable
bool IsDecoration { get; }

IRenderable WithZOffset(int newOffset);
IRenderable OffsetBy(WVec offset);
IRenderable OffsetBy(in WVec offset);
IRenderable AsDecoration();

IFinalizedRenderable PrepareRender(WorldRenderer wr);
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Graphics/SpriteRenderable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public SpriteRenderable(Sprite sprite, WPos pos, WVec offset, int zOffset, Palet

public IPalettedRenderable WithPalette(PaletteReference newPalette) { return new SpriteRenderable(sprite, pos, offset, zOffset, newPalette, scale, alpha, tint, tintModifiers, isDecoration); }
public IRenderable WithZOffset(int newOffset) { return new SpriteRenderable(sprite, pos, offset, newOffset, palette, scale, alpha, tint, tintModifiers, isDecoration); }
public IRenderable OffsetBy(WVec vec) { return new SpriteRenderable(sprite, pos + vec, offset, zOffset, palette, scale, alpha, tint, tintModifiers, isDecoration); }
public IRenderable OffsetBy(in WVec vec) { return new SpriteRenderable(sprite, pos + vec, offset, zOffset, palette, scale, alpha, tint, tintModifiers, isDecoration); }
public IRenderable AsDecoration() { return new SpriteRenderable(sprite, pos, offset, zOffset, palette, scale, alpha, tint, tintModifiers, true); }

public IModifyableRenderable WithAlpha(float newAlpha)
Expand Down
9 changes: 8 additions & 1 deletion OpenRA.Game/Graphics/TargetLineRenderable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ public TargetLineRenderable(IEnumerable<WPos> waypoints, Color color, int width
public bool IsDecoration => true;

public IRenderable WithZOffset(int newOffset) { return new TargetLineRenderable(waypoints, color); }
public IRenderable OffsetBy(WVec vec) { return new TargetLineRenderable(waypoints.Select(w => w + vec), color); }

public IRenderable OffsetBy(in WVec vec)
{
// Lambdas can't use 'in' variables, so capture a copy for later
var offset = vec;
return new TargetLineRenderable(waypoints.Select(w => w + offset), color);
}

public IRenderable AsDecoration() { return this; }

public IFinalizedRenderable PrepareRender(WorldRenderer wr) { return this; }
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Graphics/UISpriteRenderable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public UISpriteRenderable(Sprite sprite, WPos effectiveWorldPos, int2 screenPos,

public IPalettedRenderable WithPalette(PaletteReference newPalette) { return new UISpriteRenderable(sprite, effectiveWorldPos, screenPos, zOffset, newPalette, scale, alpha); }
public IRenderable WithZOffset(int newOffset) { return this; }
public IRenderable OffsetBy(WVec vec) { return this; }
public IRenderable OffsetBy(in WVec vec) { return this; }
public IRenderable AsDecoration() { return this; }

public IFinalizedRenderable PrepareRender(WorldRenderer wr) { return this; }
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Graphics/Vertex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace OpenRA.Graphics
{
[StructLayout(LayoutKind.Sequential)]
public struct Vertex
public readonly struct Vertex
{
// 3d position
public readonly float X, Y, Z;
Expand Down
6 changes: 3 additions & 3 deletions OpenRA.Game/Graphics/WorldRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public float3 Screen3DPxPosition(WPos pos)
}

// For scaling vectors to pixel sizes in the model renderer
public float3 ScreenVectorComponents(WVec vec)
public float3 ScreenVectorComponents(in WVec vec)
{
return new float3(
(float)TileSize.Width * vec.X / TileScale,
Expand All @@ -382,13 +382,13 @@ public float3 ScreenVectorComponents(WVec vec)
}

// For scaling vectors to pixel sizes in the model renderer
public float[] ScreenVector(WVec vec)
public float[] ScreenVector(in WVec vec)
{
var xyz = ScreenVectorComponents(vec);
return new[] { xyz.X, xyz.Y, xyz.Z, 1f };
}

public int2 ScreenPxOffset(WVec vec)
public int2 ScreenPxOffset(in WVec vec)
{
// Round to nearest pixel
var xyz = ScreenVectorComponents(vec);
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Input/Hotkey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace OpenRA
{
public struct Hotkey : IEquatable<Hotkey>
public readonly struct Hotkey : IEquatable<Hotkey>
{
public static Hotkey Invalid = new Hotkey(Keycode.UNKNOWN, Modifiers.None);
public bool IsValid()
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Game/MPos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace OpenRA
{
public struct MPos : IEquatable<MPos>
public readonly struct MPos : IEquatable<MPos>
{
public readonly int U, V;

Expand Down Expand Up @@ -66,7 +66,7 @@ public CPos ToCPos(MapGridType gridType)
/// <summary>
/// Projected map position
/// </summary>
public struct PPos : IEquatable<PPos>
public readonly struct PPos : IEquatable<PPos>
{
public readonly int U, V;

Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Game/Map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace OpenRA
{
struct BinaryDataHeader
readonly struct BinaryDataHeader
{
public readonly byte Format;
public readonly uint TilesOffset;
Expand Down Expand Up @@ -1225,7 +1225,7 @@ public CPos ChooseRandomEdgeCell(MersenneTwister rand)
return AllEdgeCells.Random(rand);
}

public WDist DistanceToEdge(WPos pos, WVec dir)
public WDist DistanceToEdge(WPos pos, in WVec dir)
{
var projectedPos = pos - new WVec(0, pos.Z, pos.Z);
var x = dir.X == 0 ? int.MaxValue : ((dir.X < 0 ? ProjectedTopLeft.X : ProjectedBottomRight.X) - projectedPos.X) / dir.X;
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Map/MapCoordsRegion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace OpenRA
{
public struct MapCoordsRegion : IEnumerable<MPos>
public readonly struct MapCoordsRegion : IEnumerable<MPos>
{
public struct MapCoordsEnumerator : IEnumerator<MPos>
{
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Map/MapGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public enum MapGridType { Rectangular, RectangularIsometric }
public enum RampSplit { Flat, X, Y }
public enum RampCornerHeight { Low = 0, Half = 1, Full = 2 }

public struct CellRamp
public readonly struct CellRamp
{
public readonly int CenterHeightOffset;
public readonly WVec[] Corners;
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Game/Map/TileReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace OpenRA
{
public struct TerrainTile
public readonly struct TerrainTile
{
public readonly ushort Type;
public readonly byte Index;
Expand All @@ -27,7 +27,7 @@ public TerrainTile(ushort type, byte index)
public override string ToString() { return Type + "," + Index; }
}

public struct ResourceTile
public readonly struct ResourceTile
{
public readonly byte Type;
public readonly byte Index;
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Primitives/ActionQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ int Index(DelayedAction action)
}
}

struct DelayedAction : IComparable<DelayedAction>
readonly struct DelayedAction : IComparable<DelayedAction>
{
public readonly long Time;
public readonly Action Action;
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Primitives/BitSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static bool BitsContainString(BitSetIndex bits, string value)
}
}

public struct BitSet<T> : IEnumerable<string>, IEquatable<BitSet<T>> where T : class
public readonly struct BitSet<T> : IEnumerable<string>, IEquatable<BitSet<T>> where T : class
{
readonly BitSetIndex bits;

Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Primitives/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace OpenRA.Primitives
{
public struct Color : IEquatable<Color>, IScriptBindable
public readonly struct Color : IEquatable<Color>, IScriptBindable
{
readonly long argb;

Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Primitives/Int32Matrix4x4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace OpenRA
{
public struct Int32Matrix4x4 : IEquatable<Int32Matrix4x4>
public readonly struct Int32Matrix4x4 : IEquatable<Int32Matrix4x4>
{
public readonly int M11, M12, M13, M14, M21, M22, M23, M24, M31, M32, M33, M34, M41, M42, M43, M44;

Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Primitives/LongBitSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static void Reset()
}

// Opitmized BitSet to be used only when guaranteed to be no more than 64 values.
public struct LongBitSet<T> : IEnumerable<string>, IEquatable<LongBitSet<T>> where T : class
public readonly struct LongBitSet<T> : IEnumerable<string>, IEquatable<LongBitSet<T>> where T : class
{
readonly long bits;

Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Primitives/PlayerDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace OpenRA.Primitives
/// <summary>
/// Provides a mapping of players to values, as well as fast lookup by player index.
/// </summary>
public struct PlayerDictionary<T> : IReadOnlyList<T>, IReadOnlyDictionary<Player, T> where T : class
public readonly struct PlayerDictionary<T> : IReadOnlyList<T>, IReadOnlyDictionary<Player, T> where T : class
{
readonly T[] valueByPlayerIndex;
readonly Dictionary<Player, T> valueByPlayer;
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Game/Primitives/Polygon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

namespace OpenRA.Primitives
{
public struct Polygon
public readonly struct Polygon
{
public static readonly Polygon Empty = new Polygon(Rectangle.Empty);

public readonly Rectangle BoundingRect;
public readonly int2[] Vertices;
bool isRectangle;
readonly bool isRectangle;

public Polygon(Rectangle bounds)
{
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Primitives/Size.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace OpenRA.Primitives
{
public struct Size : IEquatable<Size>
public readonly struct Size : IEquatable<Size>
{
public readonly int Width;
public readonly int Height;
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Primitives/float2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace OpenRA
{
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "Mimic a built-in type alias.")]
[StructLayout(LayoutKind.Sequential)]
public struct float2 : IEquatable<float2>
public readonly struct float2 : IEquatable<float2>
{
public readonly float X, Y;

Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Primitives/int2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace OpenRA
{
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "Mimic a built-in type alias.")]
public struct int2 : IEquatable<int2>
public readonly struct int2 : IEquatable<int2>
{
public readonly int X, Y;
public int2(int x, int y) { X = x; Y = y; }
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/SelectableExts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static FrozenActor WithHighestSelectionPriority(this IEnumerable<FrozenAc
return actors.MaxByOrDefault(a => CalculateActorSelectionPriority(a.Info, a.MouseBounds, selectionPixel, modifiers));
}

static long CalculateActorSelectionPriority(ActorInfo info, Polygon bounds, int2 selectionPixel, Modifiers modifiers)
static long CalculateActorSelectionPriority(ActorInfo info, in Polygon bounds, int2 selectionPixel, Modifiers modifiers)
{
if (bounds.IsEmpty)
return info.SelectionPriority(modifiers);
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Support/PerfSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace OpenRA.Support
{
public struct PerfSample : IDisposable
public readonly struct PerfSample : IDisposable
{
readonly string item;
readonly long ticks;
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Support/VariableExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ enum Precedence
Invalid = ~0
}

struct TokenTypeInfo
readonly struct TokenTypeInfo
{
public readonly string Symbol;
public readonly Precedence Precedence;
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/TraitDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public IEnumerable<Actor> Actors(Func<T, bool> predicate)
}
}

struct AllEnumerable : IEnumerable<TraitPair<T>>
readonly struct AllEnumerable : IEnumerable<TraitPair<T>>
{
readonly TraitContainer<T> container;
public AllEnumerable(TraitContainer<T> container) { this.container = container; }
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/Traits/World/ScreenMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace OpenRA.Traits
{
public struct ActorBoundsPair
public readonly struct ActorBoundsPair
{
public readonly Actor Actor;
public readonly Polygon Bounds;
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/WAngle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace OpenRA
/// <summary>
/// 1D angle - 1024 units = 360 degrees.
/// </summary>
public struct WAngle : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaEqualityBinding, IEquatable<WAngle>
public readonly struct WAngle : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaEqualityBinding, IEquatable<WAngle>
{
public readonly int Angle;
public int AngleSquared => (int)Angle * Angle;
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Game/WDist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace OpenRA
/// <summary>
/// 1d world distance - 1024 units = 1 cell.
/// </summary>
public struct WDist : IComparable, IComparable<WDist>, IEquatable<WDist>, IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaEqualityBinding, ILuaTableBinding
public readonly struct WDist : IComparable, IComparable<WDist>, IEquatable<WDist>, IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaEqualityBinding, ILuaTableBinding
{
public readonly int Length;
public long LengthSquared => (long)Length * Length;
Expand Down
Loading

0 comments on commit 6b74093

Please sign in to comment.