Files
OpenRA/OpenRA.Mods.Common/Traits/Buildings/LineBuild.cs
RoosterDragon 9cd55df584 Ensure editorconfig naming styles align with StyleCop SA13XX style rules.
Aligns the naming conventions defined in editorconfig (dotnet_naming_style, dotnet_naming_symbols, dotnet_naming_rule) which are reported under the IDE1006 rule with the existing StyleCop rules from the SA13XX range.

This ensures the two rulesets agree when rejecting and accepting naming conventions within the IDE, with a few edges cases where only one ruleset can enforce the convention. IDE1006 allows use to specify a naming convention for type parameters, const locals and protected readonly fields which SA13XX cannot enforce. Some StyleCop SA13XX rules such as SA1309 'Field names should not begin with underscore' are not possible to enforce with the naming rules of IDE1006.

Therefore we enable the IDE1006 as a build time warning to enforce conventions and extend them. We disable SA13XX rules that can now be covered by IDE1006 to avoid double-reporting but leave the remaining SA13XX rules that cover additional cases enabled.

We also re-enable the SA1311 rule convention but enforce it via IDE1006, requiring some violations to be fixed or duplication of existing suppressions. Most violations fixes are trivial renames with the following exception. In ActorInitializer.cs, we prefer to make the fields private instead. ValueActorInit provides a publicly accessible property for access and OwnerInit provides a publicly accessible method. Health.cs is adjusted to access the property base instead when overriding. The reflection calls must be adjusted to target the base class specifically, as searching for a private field from the derived class will fail to locate it on the base class.

Unused suppressions were removed.
2022-02-07 19:14:45 +01:00

126 lines
3.6 KiB
C#

#region Copyright & License Information
/*
* Copyright 2007-2021 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;
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public enum LineBuildDirection { Unset, X, Y }
public class LineBuildDirectionInit : ValueActorInit<LineBuildDirection>, ISingleInstanceInit
{
public LineBuildDirectionInit(LineBuildDirection value)
: base(value) { }
}
public class LineBuildParentInit : ValueActorInit<string[]>, ISingleInstanceInit
{
readonly Actor[] parents = null;
public LineBuildParentInit(Actor[] value)
: base(Array.Empty<string>())
{
parents = value;
}
public Actor[] ActorValue(World world)
{
if (parents != null)
return parents;
var sma = world.WorldActor.Trait<SpawnMapActors>();
return Value.Select(n => sma.Actors[n]).ToArray();
}
}
public interface INotifyLineBuildSegmentsChanged
{
void SegmentAdded(Actor self, Actor segment);
void SegmentRemoved(Actor self, Actor segment);
}
[Desc("Place the second actor in line to build more of the same at once (used for walls).")]
public class LineBuildInfo : TraitInfo
{
[Desc("The maximum allowed length of the line.")]
public readonly int Range = 5;
[Desc("LineBuildNode 'Types' to attach to.")]
public readonly HashSet<string> NodeTypes = new HashSet<string> { "wall" };
[ActorReference(typeof(LineBuildInfo))]
[Desc("Actor type for line-built segments (defaults to same actor).")]
public readonly string SegmentType = null;
[Desc("Delete generated segments when destroyed or sold.")]
public readonly bool SegmentsRequireNode = false;
public override object Create(ActorInitializer init) { return new LineBuild(init, this); }
}
public class LineBuild : INotifyKilled, INotifyAddedToWorld, INotifyRemovedFromWorld, INotifyLineBuildSegmentsChanged
{
readonly LineBuildInfo info;
readonly Actor[] parentNodes = Array.Empty<Actor>();
HashSet<Actor> segments;
public LineBuild(ActorInitializer init, LineBuildInfo info)
{
this.info = info;
var lineBuildParentInit = init.GetOrDefault<LineBuildParentInit>();
if (lineBuildParentInit != null)
parentNodes = lineBuildParentInit.ActorValue(init.World);
}
void INotifyLineBuildSegmentsChanged.SegmentAdded(Actor self, Actor segment)
{
if (segments == null)
segments = new HashSet<Actor>();
segments.Add(segment);
}
void INotifyLineBuildSegmentsChanged.SegmentRemoved(Actor self, Actor segment)
{
segments?.Remove(segment);
}
void INotifyAddedToWorld.AddedToWorld(Actor self)
{
foreach (var parent in parentNodes)
if (!parent.Disposed)
foreach (var n in parent.TraitsImplementing<INotifyLineBuildSegmentsChanged>())
n.SegmentAdded(parent, self);
}
void INotifyRemovedFromWorld.RemovedFromWorld(Actor self)
{
foreach (var parent in parentNodes)
if (!parent.Disposed)
foreach (var n in parent.TraitsImplementing<INotifyLineBuildSegmentsChanged>())
n.SegmentRemoved(parent, self);
if (info.SegmentsRequireNode && segments != null)
foreach (var s in segments)
s.Dispose();
}
void INotifyKilled.Killed(Actor self, AttackInfo e)
{
if (info.SegmentsRequireNode && segments != null)
foreach (var s in segments)
s.Kill(e.Attacker);
}
}
}