Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wixoaGit committed Jan 5, 2025
1 parent 4b548c5 commit 26be28e
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 89 deletions.
2 changes: 1 addition & 1 deletion OpenDreamClient/Rendering/ClientAppearanceSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public DreamIcon GetTurfIcon(uint turfId) {
}

public void OnNewAppearance(MsgNewAppearance e) {
uint appearanceId = e.Appearance.MustGetID();
uint appearanceId = e.Appearance.MustGetId();
_appearances[appearanceId] = e.Appearance;
_appearances[appearanceId].ResolveOverlays(this);

Expand Down
30 changes: 13 additions & 17 deletions OpenDreamClient/Rendering/DreamIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ private void UpdateAnimation() {
float timeFactor = Math.Clamp((float)(DateTime.Now - animation.Start).Ticks / animation.Duration.Ticks, 0.0f, 1.0f);
float factor = 0;
if((animation.Easing & AnimationEasing.EaseIn) != 0)
timeFactor = timeFactor/2.0f;
timeFactor /= 2.0f;
if((animation.Easing & AnimationEasing.EaseOut) != 0)
timeFactor = 0.5f+timeFactor/2.0f;

Expand Down Expand Up @@ -287,6 +287,7 @@ private void UpdateAnimation() {
bounce -= 2.625f;
factor = MathF.Pow(bounce, 2) + 0.984375f;
}

break;
case AnimationEasing.Elastic: //http://www.java2s.com/example/csharp/system/easing-equation-function-for-an-elastic-exponentially-decaying-sine-w.html with d=1, s=pi/2, c=2, b = -1
factor = MathF.Pow(2, -10 * timeFactor) * MathF.Sin((timeFactor - MathF.PI/2.0f) * (2.0f*MathF.PI/0.3f)) + 1.0f;
Expand Down Expand Up @@ -314,27 +315,22 @@ private void UpdateAnimation() {
suffix
*/

if (endAppearance.Direction != _appearance.Direction) {
if (endAppearance.Direction != _appearance.Direction)
_animatedAppearance.Direction = endAppearance.Direction;
}
if (endAppearance.Icon != _appearance.Icon) {
if (endAppearance.Icon != _appearance.Icon)
_animatedAppearance.Icon = endAppearance.Icon;
}
if (endAppearance.IconState != _appearance.IconState) {
if (endAppearance.IconState != _appearance.IconState)
_animatedAppearance.IconState = endAppearance.IconState;
}
if (endAppearance.Invisibility != _appearance.Invisibility) {
if (endAppearance.Invisibility != _appearance.Invisibility)
_animatedAppearance.Invisibility = endAppearance.Invisibility;
}

/* TODO maptext
if (endAppearance.MapText != _appearance.MapText) {
if (endAppearance.MapText != _appearance.MapText)
appearance.MapText = endAppearance.MapText;
}
*/
/* TODO suffix
if (endAppearance.Suffix != _appearance.Suffix) {
if (endAppearance.Suffix != _appearance.Suffix)
appearance.Suffix = endAppearance.Suffix;
}
*/

//smooth animation properties
Expand Down Expand Up @@ -367,7 +363,7 @@ private void UpdateAnimation() {
ColorMatrix.Interpolate(in _appearance.ColorMatrix, in endAppearance.ColorMatrix, factor, out _animatedAppearance.ColorMatrix);
}

if (endAppearance.GlideSize != _appearance.GlideSize) {
if (!endAppearance.GlideSize.Equals(_appearance.GlideSize)) {
_animatedAppearance.GlideSize = ((1-factor) * _appearance.GlideSize) + (factor * endAppearance.GlideSize);
}

Expand All @@ -377,7 +373,7 @@ private void UpdateAnimation() {
}
*/

if (endAppearance.Layer != _appearance.Layer) {
if (!endAppearance.Layer.Equals(_appearance.Layer)) {
_animatedAppearance.Layer = ((1-factor) * _appearance.Layer) + (factor * endAppearance.Layer);
}

Expand Down Expand Up @@ -481,15 +477,15 @@ private void UpdateIcon() {

Overlays.Clear();
foreach (var overlayAppearance in Appearance.Overlays) {
DreamIcon overlay = new DreamIcon(renderTargetPool, gameTiming, clyde, appearanceSystem, overlayAppearance.MustGetID(), _direction);
DreamIcon overlay = new DreamIcon(renderTargetPool, gameTiming, clyde, appearanceSystem, overlayAppearance.MustGetId(), _direction);
overlay.SizeChanged += CheckSizeChange;

Overlays.Add(overlay);
}

Underlays.Clear();
foreach (var underlayAppearance in Appearance.Underlays) {
DreamIcon underlay = new DreamIcon(renderTargetPool, gameTiming, clyde, appearanceSystem, underlayAppearance.MustGetID(), _direction);
DreamIcon underlay = new DreamIcon(renderTargetPool, gameTiming, clyde, appearanceSystem, underlayAppearance.MustGetId(), _direction);
underlay.SizeChanged += CheckSizeChange;

Underlays.Add(underlay);
Expand Down
17 changes: 9 additions & 8 deletions OpenDreamRuntime/AtomManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ public sealed class AtomManager {
private readonly Dictionary<EntityUid, DreamObjectMovable> _entityToAtom = new();
private readonly Dictionary<DreamObjectDefinition, MutableAppearance> _definitionAppearanceCache = new();

private ServerAppearanceSystem? AppearanceSystem{
get {
if(_appearanceSystem is null)
_entitySystemManager.TryGetEntitySystem(out _appearanceSystem);
return _appearanceSystem;
}
}
private ServerAppearanceSystem? AppearanceSystem {
get {
if(_appearanceSystem is null)
_entitySystemManager.TryGetEntitySystem(out _appearanceSystem);
return _appearanceSystem;
}
}

private ServerVerbSystem VerbSystem => _verbSystem ??= _entitySystemManager.GetEntitySystem<ServerVerbSystem>();
private ServerAppearanceSystem? _appearanceSystem;
private ServerVerbSystem? _verbSystem;
Expand Down Expand Up @@ -596,7 +597,7 @@ public void AnimateAppearance(DreamObject atom, TimeSpan duration, AnimationEasi
} else if (atom is DreamObjectTurf turf) {
//TODO: turf appearances are just set to the end appearance, they do not get properly animated
_dreamMapManager.SetTurfAppearance(turf, appearance);
turfId = turf.Appearance.MustGetID();
turfId = turf.Appearance.MustGetId();
} else if (atom is DreamObjectArea area) {
//fuck knows, this will trigger a bunch of turf updates to? idek
}
Expand Down
2 changes: 1 addition & 1 deletion OpenDreamRuntime/DreamManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public string CreateRef(DreamValue value) {
} else if (value.TryGetValueAsAppearance(out var appearance)) {
refType = RefType.DreamAppearance;
_appearanceSystem ??= _entitySystemManager.GetEntitySystem<ServerAppearanceSystem>();
idx = (int)_appearanceSystem.AddAppearance(appearance).MustGetID();
idx = (int)_appearanceSystem.AddAppearance(appearance).MustGetId();
} else if (value.TryGetValueAsDreamResource(out var refRsc)) {
refType = RefType.DreamResource;
idx = refRsc.Id;
Expand Down
8 changes: 4 additions & 4 deletions OpenDreamRuntime/DreamMapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ public void SetTurf(DreamObjectTurf turf, DreamObjectDefinition type, DreamProcA
public void SetTurfAppearance(DreamObjectTurf turf, MutableAppearance appearance) {
if(turf.Cell.Area.Appearance != _appearanceSystem.DefaultAppearance)
if(!appearance.Overlays.Contains(turf.Cell.Area.Appearance)) {
if(!_turfAreaLookup.TryGetValue((appearance, turf.Cell.Area.Appearance.MustGetID()), out var newAppearance)) {
if(!_turfAreaLookup.TryGetValue((appearance, turf.Cell.Area.Appearance.MustGetId()), out var newAppearance)) {
newAppearance = MutableAppearance.GetCopy(appearance);
newAppearance.Overlays.Add(turf.Cell.Area.Appearance);
_turfAreaLookup.Add((appearance, turf.Cell.Area.Appearance.MustGetID()), newAppearance);
_turfAreaLookup.Add((appearance, turf.Cell.Area.Appearance.MustGetId()), newAppearance);
}

appearance = newAppearance;
Expand All @@ -193,7 +193,7 @@ public void SetTurfAppearance(DreamObjectTurf turf, MutableAppearance appearance
var immutableAppearance = _appearanceSystem.AddAppearance(appearance);

Check warning

Code scanning / InspectCode

Possible unintended reference comparison. To get a value comparison, use 'Equals' method. Warning

Possible unintended reference comparison. To get a value comparison, use 'Equals' method.
var level = _levels[turf.Z - 1];
uint turfId = immutableAppearance.MustGetID();
uint turfId = immutableAppearance.MustGetId();
level.QueuedTileUpdates[(turf.X, turf.Y)] = new Tile((int)turfId);
turf.Appearance = immutableAppearance;
}
Expand Down Expand Up @@ -224,7 +224,7 @@ public void SetAreaAppearance(DreamObjectArea area, MutableAppearance appearance
}

var level = _levels[turf.Z - 1];
uint turfId = newAppearance.MustGetID();
uint turfId = newAppearance.MustGetId();
level.QueuedTileUpdates[(turf.X, turf.Y)] = new Tile((int)turfId);
}
}
Expand Down
6 changes: 2 additions & 4 deletions OpenDreamRuntime/Rendering/DMISpriteSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
namespace OpenDreamRuntime.Rendering;

public sealed class DMISpriteSystem : EntitySystem {
private ServerAppearanceSystem? _appearance;
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
[Dependency] private readonly ServerAppearanceSystem _appearance = default!;

public override void Initialize() {
SubscribeLocalEvent<DMISpriteComponent, ComponentGetState>(GetComponentState);
_entitySystemManager.TryGetEntitySystem(out _appearance);
}

private void GetComponentState(EntityUid uid, DMISpriteComponent component, ref ComponentGetState args) {
uint? appearanceId = (component.Appearance != null)
? _appearance?.AddAppearance(component.Appearance).MustGetID()
? _appearance.AddAppearance(component.Appearance).MustGetId()
: null;

args.State = new SharedDMISpriteComponent.DMISpriteComponentState(appearanceId, component.ScreenLocation);
Expand Down
34 changes: 18 additions & 16 deletions OpenDreamRuntime/Rendering/ServerAppearanceSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,32 @@
namespace OpenDreamRuntime.Rendering;

public sealed class ServerAppearanceSystem : SharedAppearanceSystem {
public readonly ImmutableAppearance DefaultAppearance;

/// <summary>
/// Each appearance gets a unique ID when marked as registered. Here we store these as a key -> weakref in a weaktable, which does not count
/// as a hard ref but allows quick lookup. Each object which holds an appearance MUST hold that ImmutableAppearance until it is no longer
/// needed or it will be GC'd. Overlays & underlays are stored as hard refs on the ImmutableAppearance so you only need to hold the main appearance.
/// </summary>
private readonly HashSet<ProxyWeakRef> _appearanceLookup = new();
private readonly Dictionary<uint, ProxyWeakRef> _idToAppearance = new();
private uint _counter;
public readonly ImmutableAppearance DefaultAppearance;
[Dependency] private readonly IServerNetManager _networkManager = default!;

/// <summary>
/// This system is used by the PVS thread, we need to be thread-safe
/// </summary>
private readonly object _lock = new();

private readonly Dictionary<uint, ProxyWeakRef> _idToAppearance = new();
private uint _counter;

[Dependency] private readonly IServerNetManager _networkManager = default!;

Check notice

Code scanning / InspectCode

Prefer using concrete value over 'default' or 'new()' Note

Use 'null' instead of 'default'
[Dependency] private readonly IPlayerManager _playerManager = default!;

public ServerAppearanceSystem() {
DefaultAppearance = new ImmutableAppearance(MutableAppearance.Default, this);
DefaultAppearance.MarkRegistered(_counter++); //first appearance registered gets id 0, this is the blank default appearance
ProxyWeakRef proxyWeakRef = new(DefaultAppearance);
_appearanceLookup.Add(proxyWeakRef);
_idToAppearance.Add(DefaultAppearance.MustGetID(), proxyWeakRef);
_idToAppearance.Add(DefaultAppearance.MustGetId(), proxyWeakRef);
//leaving this in as a sanity check for mutable and immutable appearance hashcodes covering all the same vars
//if this debug assert fails, you've probably changed appearance var and not updated its counterpart
Debug.Assert(DefaultAppearance.GetHashCode() == MutableAppearance.Default.GetHashCode());
Expand All @@ -59,7 +61,7 @@ private void OnPlayerStatusChanged(object? sender, SessionStatusEventArgs e) {

foreach(ProxyWeakRef proxyWeakRef in _appearanceLookup){
if(proxyWeakRef.TryGetTarget(out var immutable))
sendData.Add(immutable.MustGetID(), immutable);
sendData.Add(immutable.MustGetId(), immutable);
}

Logger.GetSawmill("appearance").Debug($"Sending {sendData.Count} appearances to new player {e.Session.Name}");
Expand All @@ -72,7 +74,7 @@ private void RegisterAppearance(ImmutableAppearance immutableAppearance) {
immutableAppearance.MarkRegistered(_counter++); //lets this appearance know it needs to do GC finaliser & get an ID
ProxyWeakRef proxyWeakRef = new(immutableAppearance);
_appearanceLookup.Add(proxyWeakRef);
_idToAppearance.Add(immutableAppearance.MustGetID(), proxyWeakRef);
_idToAppearance.Add(immutableAppearance.MustGetId(), proxyWeakRef);
_networkManager.ServerSendToAll(new MsgNewAppearance(immutableAppearance));
}

Expand Down Expand Up @@ -104,8 +106,8 @@ public override void RemoveAppearance(ImmutableAppearance appearance) {
if(weakRef.TryGetTarget(out var target) && !ReferenceEquals(target,appearance))
return;
_appearanceLookup.Remove(proxyWeakRef);
_idToAppearance.Remove(appearance.MustGetID());
RaiseNetworkEvent(new RemoveAppearanceEvent(appearance.MustGetID()));
_idToAppearance.Remove(appearance.MustGetId());
RaiseNetworkEvent(new RemoveAppearanceEvent(appearance.MustGetId()));
}
}
}
Expand All @@ -126,23 +128,23 @@ public bool TryGetAppearanceById(uint appearanceId, [NotNullWhen(true)] out Immu
}

public void Animate(NetEntity entity, MutableAppearance targetAppearance, TimeSpan duration, AnimationEasing easing, int loop, AnimationFlags flags, int delay, bool chainAnim, uint? turfId) {
uint appearanceId = AddAppearance(targetAppearance).MustGetID();
uint appearanceId = AddAppearance(targetAppearance).MustGetId();

RaiseNetworkEvent(new AnimationEvent(entity, appearanceId, duration, easing, loop, flags, delay, chainAnim, turfId));
}
}

//this class lets us hold a weakref and also do quick lookups in hash tables
internal sealed class ProxyWeakRef: IEquatable<ProxyWeakRef>{
public WeakReference<ImmutableAppearance> WeakRef;
private readonly uint? _registeredId;
private readonly int _hashCode;
public bool IsAlive => WeakRef.TryGetTarget(out var _);
public bool TryGetTarget([NotNullWhen(true)] out ImmutableAppearance? target) => WeakRef.TryGetTarget(out target);
public bool TryGetTarget([NotNullWhen(true)] out ImmutableAppearance? target) => _weakRef.TryGetTarget(out target);

private readonly WeakReference<ImmutableAppearance> _weakRef;

public ProxyWeakRef(ImmutableAppearance appearance) {
appearance.TryGetID(out _registeredId);
WeakRef = new(appearance);
appearance.TryGetId(out _registeredId);
_weakRef = new(appearance);
_hashCode = appearance.GetHashCode();
}

Expand All @@ -157,7 +159,7 @@ public bool Equals(ProxyWeakRef? proxy) {
return false;
if(_registeredId is not null && _registeredId == proxy._registeredId)
return true;
if(WeakRef.TryGetTarget(out ImmutableAppearance? thisRef) && proxy.WeakRef.TryGetTarget(out ImmutableAppearance? thatRef))
if(_weakRef.TryGetTarget(out ImmutableAppearance? thisRef) && proxy._weakRef.TryGetTarget(out ImmutableAppearance? thatRef))
return thisRef.Equals(thatRef);
return false;
}
Expand Down
1 change: 0 additions & 1 deletion OpenDreamRuntime/ServerContentIoC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public static void Register(bool unitTests = false) {
IoCManager.Register<WalkManager, WalkManager>();
IoCManager.Register<IDreamDebugManager, DreamDebugManager>();
IoCManager.Register<ServerInfoManager>();
IoCManager.Register<DMISpriteSystem>();

#if DEBUG
IoCManager.Register<LocalHostConGroup>();
Expand Down
Loading

0 comments on commit 26be28e

Please sign in to comment.