Add readonly to structs

This commit is contained in:
teinarss
2021-01-29 16:56:11 +01:00
committed by abcdefg30
parent 65c796dec7
commit 6b74093c04
83 changed files with 146 additions and 125 deletions

View File

@@ -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;

View File

@@ -16,7 +16,7 @@ using OpenRA.Scripting;
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

View File

@@ -17,7 +17,7 @@ using OpenRA.Scripting;
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;

View File

@@ -52,7 +52,7 @@ namespace OpenRA.Graphics
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);
@@ -68,7 +68,7 @@ namespace OpenRA.Graphics
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();
@@ -87,7 +87,7 @@ namespace OpenRA.Graphics
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);

View File

@@ -29,7 +29,7 @@ namespace OpenRA.Graphics
Rectangle AggregateBounds { get; }
}
public struct ModelRenderData
public readonly struct ModelRenderData
{
public readonly int Start;
public readonly int Count;

View File

@@ -14,7 +14,7 @@ using OpenRA.Primitives;
namespace OpenRA.Graphics
{
public struct ModelAnimation
public readonly struct ModelAnimation
{
public readonly IModel Model;
public readonly Func<WVec> OffsetFunc;

View File

@@ -145,7 +145,7 @@ namespace OpenRA
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; }

View File

@@ -21,7 +21,7 @@ namespace OpenRA.Graphics
bool IsDecoration { get; }
IRenderable WithZOffset(int newOffset);
IRenderable OffsetBy(WVec offset);
IRenderable OffsetBy(in WVec offset);
IRenderable AsDecoration();
IFinalizedRenderable PrepareRender(WorldRenderer wr);

View File

@@ -55,7 +55,7 @@ namespace OpenRA.Graphics
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)

View File

@@ -35,7 +35,14 @@ namespace OpenRA.Graphics
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; }

View File

@@ -44,7 +44,7 @@ namespace OpenRA.Graphics
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; }

View File

@@ -14,7 +14,7 @@ using System.Runtime.InteropServices;
namespace OpenRA.Graphics
{
[StructLayout(LayoutKind.Sequential)]
public struct Vertex
public readonly struct Vertex
{
// 3d position
public readonly float X, Y, Z;

View File

@@ -373,7 +373,7 @@ namespace OpenRA.Graphics
}
// 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,
@@ -382,13 +382,13 @@ namespace OpenRA.Graphics
}
// 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);

View File

@@ -13,7 +13,7 @@ using System;
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()

View File

@@ -14,7 +14,7 @@ using OpenRA.Primitives;
namespace OpenRA
{
public struct MPos : IEquatable<MPos>
public readonly struct MPos : IEquatable<MPos>
{
public readonly int U, V;
@@ -66,7 +66,7 @@ namespace OpenRA
/// <summary>
/// Projected map position
/// </summary>
public struct PPos : IEquatable<PPos>
public readonly struct PPos : IEquatable<PPos>
{
public readonly int U, V;

View File

@@ -24,7 +24,7 @@ using OpenRA.Traits;
namespace OpenRA
{
struct BinaryDataHeader
readonly struct BinaryDataHeader
{
public readonly byte Format;
public readonly uint TilesOffset;
@@ -1225,7 +1225,7 @@ namespace OpenRA
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;

View File

@@ -14,7 +14,7 @@ using System.Collections.Generic;
namespace OpenRA
{
public struct MapCoordsRegion : IEnumerable<MPos>
public readonly struct MapCoordsRegion : IEnumerable<MPos>
{
public struct MapCoordsEnumerator : IEnumerator<MPos>
{

View File

@@ -22,7 +22,7 @@ namespace OpenRA
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;

View File

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

View File

@@ -65,7 +65,7 @@ namespace OpenRA.Primitives
}
}
struct DelayedAction : IComparable<DelayedAction>
readonly struct DelayedAction : IComparable<DelayedAction>
{
public readonly long Time;
public readonly Action Action;

View File

@@ -75,7 +75,7 @@ namespace OpenRA.Primitives
}
}
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;

View File

@@ -15,7 +15,7 @@ using OpenRA.Scripting;
namespace OpenRA.Primitives
{
public struct Color : IEquatable<Color>, IScriptBindable
public readonly struct Color : IEquatable<Color>, IScriptBindable
{
readonly long argb;

View File

@@ -13,7 +13,7 @@ using System;
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;

View File

@@ -92,7 +92,7 @@ namespace OpenRA.Primitives
}
// 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;

View File

@@ -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;

View File

@@ -14,13 +14,13 @@ using System.Linq;
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)
{

View File

@@ -13,7 +13,7 @@ using System;
namespace OpenRA.Primitives
{
public struct Size : IEquatable<Size>
public readonly struct Size : IEquatable<Size>
{
public readonly int Width;
public readonly int Height;

View File

@@ -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;

View File

@@ -16,7 +16,7 @@ using OpenRA.Primitives;
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; }

View File

@@ -73,7 +73,7 @@ namespace OpenRA.Traits
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);

View File

@@ -14,7 +14,7 @@ using System.Diagnostics;
namespace OpenRA.Support
{
public struct PerfSample : IDisposable
public readonly struct PerfSample : IDisposable
{
readonly string item;
readonly long ticks;

View File

@@ -157,7 +157,7 @@ namespace OpenRA.Support
Invalid = ~0
}
struct TokenTypeInfo
readonly struct TokenTypeInfo
{
public readonly string Symbol;
public readonly Precedence Precedence;

View File

@@ -249,7 +249,7 @@ namespace OpenRA
}
}
struct AllEnumerable : IEnumerable<TraitPair<T>>
readonly struct AllEnumerable : IEnumerable<TraitPair<T>>
{
readonly TraitContainer<T> container;
public AllEnumerable(TraitContainer<T> container) { this.container = container; }

View File

@@ -18,7 +18,7 @@ using OpenRA.Primitives;
namespace OpenRA.Traits
{
public struct ActorBoundsPair
public readonly struct ActorBoundsPair
{
public readonly Actor Actor;
public readonly Polygon Bounds;

View File

@@ -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;

View File

@@ -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;

View File

@@ -18,7 +18,7 @@ using OpenRA.Scripting;
namespace OpenRA
{
public struct WPos : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaEqualityBinding, ILuaTableBinding, IEquatable<WPos>
public readonly struct WPos : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaEqualityBinding, ILuaTableBinding, IEquatable<WPos>
{
public readonly int X, Y, Z;
@@ -27,24 +27,24 @@ namespace OpenRA
public static readonly WPos Zero = new WPos(0, 0, 0);
public static explicit operator WVec(WPos a) { return new WVec(a.X, a.Y, a.Z); }
public static explicit operator WVec(in WPos a) { return new WVec(a.X, a.Y, a.Z); }
public static WPos operator +(WPos a, WVec b) { return new WPos(a.X + b.X, a.Y + b.Y, a.Z + b.Z); }
public static WPos operator -(WPos a, WVec b) { return new WPos(a.X - b.X, a.Y - b.Y, a.Z - b.Z); }
public static WVec operator -(WPos a, WPos b) { return new WVec(a.X - b.X, a.Y - b.Y, a.Z - b.Z); }
public static WPos operator +(in WPos a, in WVec b) { return new WPos(a.X + b.X, a.Y + b.Y, a.Z + b.Z); }
public static WPos operator -(in WPos a, in WVec b) { return new WPos(a.X - b.X, a.Y - b.Y, a.Z - b.Z); }
public static WVec operator -(in WPos a, in WPos b) { return new WVec(a.X - b.X, a.Y - b.Y, a.Z - b.Z); }
public static bool operator ==(WPos me, WPos other) { return me.X == other.X && me.Y == other.Y && me.Z == other.Z; }
public static bool operator !=(WPos me, WPos other) { return !(me == other); }
public static bool operator ==(in WPos me, in WPos other) { return me.X == other.X && me.Y == other.Y && me.Z == other.Z; }
public static bool operator !=(in WPos me, in WPos other) { return !(me == other); }
/// <summary>
/// Returns the linear interpolation between points 'a' and 'b'
/// </summary>
public static WPos Lerp(WPos a, WPos b, int mul, int div) { return a + (b - a) * mul / div; }
public static WPos Lerp(in WPos a, in WPos b, int mul, int div) { return a + (b - a) * mul / div; }
/// <summary>
/// Returns the linear interpolation between points 'a' and 'b'
/// </summary>
public static WPos Lerp(WPos a, WPos b, long mul, long div)
public static WPos Lerp(in WPos a, in WPos b, long mul, long div)
{
// The intermediate variables may need more precision than
// an int can provide, so we can't use WPos.
@@ -55,7 +55,7 @@ namespace OpenRA
return new WPos(x, y, z);
}
public static WPos LerpQuadratic(WPos a, WPos b, WAngle pitch, int mul, int div)
public static WPos LerpQuadratic(in WPos a, in WPos b, WAngle pitch, int mul, int div)
{
// Start with a linear lerp between the points
var ret = Lerp(a, b, mul, div);

View File

@@ -17,7 +17,7 @@ using OpenRA.Support;
namespace OpenRA
{
public struct WVec : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaUnaryMinusBinding, ILuaEqualityBinding, ILuaTableBinding, IEquatable<WVec>
public readonly struct WVec : IScriptBindable, ILuaAdditionBinding, ILuaSubtractionBinding, ILuaUnaryMinusBinding, ILuaEqualityBinding, ILuaTableBinding, IEquatable<WVec>
{
public readonly int X, Y, Z;
@@ -26,17 +26,17 @@ namespace OpenRA
public static readonly WVec Zero = new WVec(0, 0, 0);
public static WVec operator +(WVec a, WVec b) { return new WVec(a.X + b.X, a.Y + b.Y, a.Z + b.Z); }
public static WVec operator -(WVec a, WVec b) { return new WVec(a.X - b.X, a.Y - b.Y, a.Z - b.Z); }
public static WVec operator -(WVec a) { return new WVec(-a.X, -a.Y, -a.Z); }
public static WVec operator /(WVec a, int b) { return new WVec(a.X / b, a.Y / b, a.Z / b); }
public static WVec operator *(int a, WVec b) { return new WVec(a * b.X, a * b.Y, a * b.Z); }
public static WVec operator *(WVec a, int b) { return b * a; }
public static WVec operator +(in WVec a, in WVec b) { return new WVec(a.X + b.X, a.Y + b.Y, a.Z + b.Z); }
public static WVec operator -(in WVec a, in WVec b) { return new WVec(a.X - b.X, a.Y - b.Y, a.Z - b.Z); }
public static WVec operator -(in WVec a) { return new WVec(-a.X, -a.Y, -a.Z); }
public static WVec operator /(in WVec a, int b) { return new WVec(a.X / b, a.Y / b, a.Z / b); }
public static WVec operator *(int a, in WVec b) { return new WVec(a * b.X, a * b.Y, a * b.Z); }
public static WVec operator *(in WVec a, int b) { return b * a; }
public static bool operator ==(WVec me, WVec other) { return me.X == other.X && me.Y == other.Y && me.Z == other.Z; }
public static bool operator !=(WVec me, WVec other) { return !(me == other); }
public static bool operator ==(in WVec me, in WVec other) { return me.X == other.X && me.Y == other.Y && me.Z == other.Z; }
public static bool operator !=(in WVec me, in WVec other) { return !(me == other); }
public static int Dot(WVec a, WVec b) { return a.X * b.X + a.Y * b.Y + a.Z * b.Z; }
public static int Dot(in WVec a, in WVec b) { return a.X * b.X + a.Y * b.Y + a.Z * b.Z; }
public long LengthSquared => (long)X * X + (long)Y * Y + (long)Z * Z;
public int Length => (int)Exts.ISqrt(LengthSquared);
public long HorizontalLengthSquared => (long)X * X + (long)Y * Y;
@@ -73,9 +73,9 @@ namespace OpenRA
}
}
public static WVec Lerp(WVec a, WVec b, int mul, int div) { return a + (b - a) * mul / div; }
public static WVec Lerp(in WVec a, in WVec b, int mul, int div) { return a + (b - a) * mul / div; }
public static WVec LerpQuadratic(WVec a, WVec b, WAngle pitch, int mul, int div)
public static WVec LerpQuadratic(in WVec a, in WVec b, WAngle pitch, int mul, int div)
{
// Start with a linear lerp between the points
var ret = Lerp(a, b, mul, div);

View File

@@ -576,7 +576,7 @@ namespace OpenRA
}
}
public struct TraitPair<T> : IEquatable<TraitPair<T>>
public readonly struct TraitPair<T> : IEquatable<TraitPair<T>>
{
public readonly Actor Actor;
public readonly T Trait;