Compare commits

...

25 Commits

Author SHA1 Message Date
metalgearsloth
139b6f796c Version: 248.0.2 2025-03-12 20:26:50 +11:00
metalgearsloth
2ee7c35fd3 Don't throw on invalid MapUids for overlays (#5740) 2025-03-12 20:16:13 +11:00
metalgearsloth
56eae5ad08 Reduce EntityManager.IsDefault allocations (#5741)
* Reduce EntityManager.IsDefault allocations

We don't actually need the MappingDataNode so we can avoid allocating the entire class per entity.

* Adjust this order
2025-03-12 19:42:08 +11:00
DrSmugleaf
0662cae224 Version: 248.0.1 2025-03-11 19:38:08 -07:00
metalgearsloth
0e2b00edd0 Fix NaN gain audio (#5737) 2025-03-10 20:16:05 +11:00
Richard Van Tassel
d48f7ecb5b bumps ImageSharp version (#5733) 2025-03-08 15:19:23 +01:00
metalgearsloth
e064b7a4f9 Version: 248.0.0 2025-03-08 15:34:53 +11:00
metalgearsloth
654480862e Use Prototype for ITileDef (#5731)
Forgot to push this.
2025-03-08 15:20:25 +11:00
metalgearsloth
353c044b52 Hot reload resources (#5443)
* Fix ResPath CanonPath

Apparently this is supposed to standardise to / but this isn't always the case. Alternatively we could just assert for performance reasons I'm good with either. The comment as written says this should happen.

* Fixes

* change

* assert

* Fix bad respath input

* Buffer

* Merge conflicts

* review

* Fix
2025-03-08 15:16:31 +11:00
Whatstone
3dda8d9e93 Try/catch blocks around BUI open/dispose calls (#5730) 2025-03-08 15:16:11 +11:00
metalgearsloth
41ea10083d Fix ResPath CanonPath (#5452)
* Fix ResPath CanonPath

Apparently this is supposed to standardise to / but this isn't always the case. Alternatively we could just assert for performance reasons I'm good with either. The comment as written says this should happen.

* assert

* Fix bad respath input

* review
2025-03-08 15:02:46 +11:00
eoineoineoin
6290bb7af1 Adds method to Controls.ItemList which updates the item list without erasing contents (#5425)
* Add algorithm from ss14#30292 to ItemList, for use in other UIs

* Add overload for common case of comparing items by their text label
2025-03-08 14:43:43 +11:00
metalgearsloth
348ab70a8d Update B2DynamicTree (#5332)
* Update B2DynamicTree

* API updates

* weh

* forcing it

* Fix all of the bugs

* Rebuild

* A crumb of danger

* Fix merge conflicts
2025-03-08 14:15:47 +11:00
IProduceWidgets
47e11e988c Fix map netId completions (#5495)
* make map netId completions function.

* Update Robust.Shared/Console/CompletionHelper.cs

---------

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2025-03-08 13:57:13 +11:00
Leon Friedrich
c459b55052 Add TryLoadGrid override (#5701)
* Add TryLoadGrid override

* fix cmd-addmap-help
2025-03-08 13:50:14 +11:00
Tayrtahn
f10e96a6d1 Clean up warnings in Stack_Test (#5705) 2025-03-08 13:49:52 +11:00
metalgearsloth
e8bac558c6 Use Entity<T> for TileChangedEvent (#5698)
Content is manually resolving this in a few spots so we can avoid that overhead.
2025-03-08 13:48:32 +11:00
metalgearsloth
ffa3bb7202 Fix savedpos caching on UI shutdown (#5729)
Don't need this as it gets handled already.
2025-03-08 13:28:29 +11:00
Dylan Craine
9bcdc95651 Fix deceptive "successfully saved" messages for mappers (#5714)
* Actually check if map save succeeded before displaying success message

It would be great to offer more clarity to the mapper about *why* the
save didn't succeed, but at least they won't be deceived into thinking
their work has been saved when it hasn't.

Portuguese localization text is via DuckDuckGo Translate, so I hope it's
reasonable.

* Actually check save success for saving grids

These messages need localization, too, but that seems out of scope for
my PR.

* Improve map save error message

Now it tells the mapper to go look at the server log.
Still translated via DuckDuckGo Translate.

* Normalize indentation and style
2025-03-08 13:27:59 +11:00
TemporalOroboros
543088ea1f Remove unused private members/local vars (#5722) 2025-03-05 15:50:54 +01:00
deltanedas
56daa63783 make map loading logs better (#5723)
Co-authored-by: deltanedas <@deltanedas:kde.org>
2025-03-05 15:49:20 +01:00
metalgearsloth
9fe9730d4a Add public API for physicshull (#5719) 2025-03-03 22:07:38 +11:00
SlamBamActionman
a1a7ea92d9 Add Regex.Count and StringBuilder.set_Chars to sandbox whitelist (#5717) 2025-03-01 22:42:19 +01:00
poklj
0dec6a425f Modify TryCopyComponents metadata TryGetComponent (#5710) 2025-02-27 11:25:28 +11:00
metalgearsloth
56ced913b7 Audio fixes (#5707) 2025-02-26 22:08:17 +11:00
72 changed files with 2434 additions and 611 deletions

View File

@@ -55,7 +55,7 @@
<PackageVersion Include="Serilog" Version="4.2.0" />
<PackageVersion Include="Serilog.Sinks.Loki" Version="4.0.0-beta3" />
<PackageVersion Include="SharpZstd.Interop" Version="1.5.2-beta2" />
<PackageVersion Include="SixLabors.ImageSharp" Version="3.1.6" />
<PackageVersion Include="SixLabors.ImageSharp" Version="3.1.7" />
<PackageVersion Include="SpaceWizards.HttpListener" Version="0.1.1" />
<PackageVersion Include="SpaceWizards.NFluidsynth" Version="0.1.1" />
<PackageVersion Include="SpaceWizards.SharpFont" Version="1.0.2" />

View File

@@ -1,4 +1,4 @@
<Project>
<!-- This file automatically reset by Tools/version.py -->
<!-- This file automatically reset by Tools/version.py -->

View File

@@ -54,6 +54,63 @@ END TEMPLATE-->
*None yet*
## 248.0.2
### Bugfixes
* Don't throw in overlay rendering if MapUid not found.
### Internal
* Reduce EntityManager.IsDefault allocations.
## 248.0.1
### Bugfixes
* Bump ImageSharp version.
* Fix instances of NaN gain for audio where a negative-infinity value is being used for volume.
## 248.0.0
### Breaking changes
* Use `Entity<MapGridComponent>` for TileChangedEvent instead of EntityUid.
* Audio files are no longer tempo perfect when being played if the offset is small. At some point in the future an AudioParams bool is likely to be added to enforce this.
* MoveProxy method args got changed in the B2DynamicTree update.
* ResPath will now assert in debug if you pass in an invalid path containing the non-standardized directory separator.
### New features
* Added a new `MapLoaderSystem.TryLoadGrid()` override that loads a grid onto a newly created map.
* Added a CVar for the endbuffer for audio. If an audio file will play below this length (for PVS reasons) it will be ignored.
* Added Regex.Count + StringBuilder.Chars setter to the sandbox.
* Added a public API for PhysicsHull.
* Made MapLoader log more helpful.
* Add TryLoadGrid override that also creates a map at the same time.
* Updated B2Dynamictree to the latest Box2D V3 version.
* Added SetItems to ItemList control to set items without removing the existing ones.
* Shaders, textures, and audio will now hot reload automatically to varying degrees. Also added IReloadManager to handle watching for file-system changes and relaying events.
* Wrap BUI disposes in a try-catch in case of exceptions.
### Bugfixes
* Fix some instances of invalid PlaybackPositions being set.
* Play audio from the start of a file if it's only just come into PVS range / had its state handled.
* Fix TryCopyComponents.
* Use shell.WriteError if TryLoad fails for mapping commands.
* Fix UI control position saving causing exceptions where the entity is cleaned-up alongside a state change.
* Fix Map NetId completions.
* Fix some ResPath calls using the wrong paths.
### Internal
* Remove some unused local variables and the associated warnings.
## 247.2.0
### New features

View File

@@ -156,6 +156,7 @@ cmd-savemap-not-exist = Target map does not exist.
cmd-savemap-init-warning = Attempted to save a post-init map without forcing the save.
cmd-savemap-attempt = Attempting to save map {$mapId} to {$path}.
cmd-savemap-success = Map successfully saved.
cmd-savemap-error = Could not save map! See server log for details.
cmd-hint-savemap-id = <MapID>
cmd-hint-savemap-path = <Path>
cmd-hint-savemap-force = [bool]
@@ -293,7 +294,7 @@ cmd-lsgrid-desc = Lists grids.
cmd-lsgrid-help = lsgrid
cmd-addmap-desc = Adds a new empty map to the round. If the mapID already exists, this command does nothing.
cmd-addmap-help = addmap <mapID> [initialize]
cmd-addmap-help = addmap <mapID> [pre-init]
cmd-rmmap-desc = Removes a map from the world. You cannot remove nullspace.
cmd-rmmap-help = rmmap <mapId>

View File

@@ -136,6 +136,7 @@ cmd-savemap-not-exist = O mapa de destino não existe.
cmd-savemap-init-warning = Tentativa de salvar um mapa pós-inicialização sem forçar o salvamento.
cmd-savemap-attempt = Tentando salvar o mapa {$mapId} em {$path}.
cmd-savemap-success = Mapa salvo com sucesso.
cmd-savemap-error = Não foi possível salvar o mapa! Consulte o log do servidor para obter detalhes.
cmd-hint-savemap-id = <MapID>
cmd-hint-savemap-path = <Path>
cmd-hint-savemap-force = [bool]

View File

@@ -26,10 +26,10 @@ public class PhysicsTumblerBenchmark
var entManager = _sim.Resolve<IEntityManager>();
var physics = entManager.System<SharedPhysicsSystem>();
var fixtures = entManager.System<FixtureSystem>();
entManager.System<SharedMapSystem>().CreateMap(out var mapId);
var mapUid = entManager.System<SharedMapSystem>().CreateMap(out var mapId);
SetupTumbler(entManager, mapId);
for (var i = 0; i < 800; i++)
for (var i = 0; i < 300; i++)
{
entManager.TickUpdate(0.016f, false);
var boxUid = entManager.SpawnEntity(null, new MapCoordinates(0f, 10f, mapId));
@@ -42,6 +42,9 @@ public class PhysicsTumblerBenchmark
physics.WakeBody(boxUid, body: box);
physics.SetSleepingAllowed(boxUid, box, false);
}
if (entManager.TryGetComponent(mapUid, out BroadphaseComponent? mapBroadphase))
entManager.System<SharedBroadphaseSystem>().RebuildBottomUp(mapBroadphase);
}
[Benchmark]
@@ -49,7 +52,7 @@ public class PhysicsTumblerBenchmark
{
var entManager = _sim.Resolve<IEntityManager>();
for (var i = 0; i < 5000; i++)
for (var i = 0; i < 1000; i++)
{
entManager.TickUpdate(0.016f, false);
}

View File

@@ -84,6 +84,19 @@ internal partial class AudioManager
AL.Listener(ALListenerfv.Orientation, ref at, ref up);
}
void IAudioInternal.Remove(AudioStream stream)
{
if (stream.ClydeHandle == null)
return;
if (!_audioSampleBuffers.Remove(stream.BufferId))
{
return;
}
AL.DeleteBuffer(stream.BufferId);
}
/// <inheritdoc/>
public AudioStream LoadAudioOggVorbis(Stream stream, string? name = null)
{
@@ -120,9 +133,9 @@ internal partial class AudioManager
_checkAlError();
var handle = new ClydeHandle(_audioSampleBuffers.Count);
_audioSampleBuffers.Add(new LoadedAudioSample(buffer));
_audioSampleBuffers.Add(buffer, new LoadedAudioSample(buffer));
var length = TimeSpan.FromSeconds(vorbis.TotalSamples / (double) vorbis.SampleRate);
return new AudioStream(handle, length, (int) vorbis.Channels, name, vorbis.Title, vorbis.Artist);
return new AudioStream(this, buffer, handle, length, (int) vorbis.Channels, name, vorbis.Title, vorbis.Artist);
}
/// <inheritdoc/>
@@ -179,9 +192,9 @@ internal partial class AudioManager
_checkAlError();
var handle = new ClydeHandle(_audioSampleBuffers.Count);
_audioSampleBuffers.Add(new LoadedAudioSample(buffer));
_audioSampleBuffers.Add(buffer, new LoadedAudioSample(buffer));
var length = TimeSpan.FromSeconds(wav.Data.Length / (double) wav.BlockAlign / wav.SampleRate);
return new AudioStream(handle, length, wav.NumChannels, name);
return new AudioStream(this, buffer, handle, length, wav.NumChannels, name);
}
/// <inheritdoc/>
@@ -210,8 +223,8 @@ internal partial class AudioManager
var handle = new ClydeHandle(_audioSampleBuffers.Count);
var length = TimeSpan.FromSeconds((double) samples.Length / channels / sampleRate);
_audioSampleBuffers.Add(new LoadedAudioSample(buffer));
return new AudioStream(handle, length, channels, name);
_audioSampleBuffers.Add(buffer, new LoadedAudioSample(buffer));
return new AudioStream(this, buffer, handle, length, channels, name);
}
public void SetMasterGain(float newGain)
@@ -293,7 +306,7 @@ internal partial class AudioManager
// ReSharper disable once PossibleInvalidOperationException
// TODO: This really shouldn't be indexing based on the ClydeHandle...
AL.Source(source, ALSourcei.Buffer, _audioSampleBuffers[(int) stream.ClydeHandle!.Value].BufferHandle);
AL.Source(source, ALSourcei.Buffer, _audioSampleBuffers[stream.BufferId].BufferHandle);
var audioSource = new AudioSource(this, source, stream);
_audioSources.Add(source, new WeakReference<BaseAudioSource>(audioSource));
@@ -370,5 +383,12 @@ internal partial class AudioManager
}
_bufferedAudioSources.Clear();
foreach (var buffer in _audioSampleBuffers.Values)
{
DeleteAudioBufferOnMainThread(buffer.BufferHandle);
}
_audioSampleBuffers.Clear();
}
}

View File

@@ -5,6 +5,7 @@ using System.Threading;
using OpenTK.Audio.OpenAL;
using OpenTK.Audio.OpenAL.Extensions.Creative.EFX;
using Robust.Client.Audio.Sources;
using Robust.Client.ResourceManagement;
using Robust.Shared;
using Robust.Shared.Audio;
using Robust.Shared.Configuration;
@@ -17,13 +18,15 @@ internal sealed partial class AudioManager : IAudioInternal
{
[Shared.IoC.Dependency] private readonly IConfigurationManager _cfg = default!;
[Shared.IoC.Dependency] private readonly ILogManager _logMan = default!;
[Shared.IoC.Dependency] private readonly IReloadManager _reload = default!;
[Shared.IoC.Dependency] private readonly IResourceCache _cache = default!;
private Thread? _gameThread;
private ALDevice _openALDevice;
private ALContext _openALContext;
private readonly List<LoadedAudioSample> _audioSampleBuffers = new();
private readonly Dictionary<int, LoadedAudioSample> _audioSampleBuffers = new();
private readonly Dictionary<int, WeakReference<BaseAudioSource>> _audioSources =
new();
@@ -116,6 +119,22 @@ internal sealed partial class AudioManager : IAudioInternal
IsEfxSupported = HasAlDeviceExtension("ALC_EXT_EFX");
_cfg.OnValueChanged(CVars.AudioMasterVolume, SetMasterGain, true);
_reload.Register("/Audio", "*.ogg");
_reload.Register("/Audio", "*.wav");
_reload.OnChanged += OnReload;
}
private void OnReload(ResPath args)
{
if (args.Extension != "ogg" &&
args.Extension != "wav")
{
return;
}
_cache.ReloadResource<AudioResource>(args);
}
internal bool IsMainThread()
@@ -140,6 +159,11 @@ internal sealed partial class AudioManager : IAudioInternal
}
}
internal void LogError(string message)
{
OpenALSawmill.Error(message);
}
/// <summary>
/// Like _checkAlError but allows custom data to be passed in as relevant.
/// </summary>

View File

@@ -6,8 +6,15 @@ namespace Robust.Client.Audio;
/// <summary>
/// Has the metadata for a particular audio stream as well as the relevant internal handle to it.
/// </summary>
public sealed class AudioStream
public sealed class AudioStream : IDisposable
{
private IAudioInternal _audio;
/// <summary>
/// Buffer ID for this audio in AL.
/// </summary>
internal int BufferId { get; }
public TimeSpan Length { get; }
internal IClydeHandle? ClydeHandle { get; }
public string? Name { get; }
@@ -15,8 +22,10 @@ public sealed class AudioStream
public string? Artist { get; }
public int ChannelCount { get; }
internal AudioStream(IClydeHandle? handle, TimeSpan length, int channelCount, string? name = null, string? title = null, string? artist = null)
internal AudioStream(IAudioInternal internalAudio, int bufferId, IClydeHandle? handle, TimeSpan length, int channelCount, string? name = null, string? title = null, string? artist = null)
{
_audio = internalAudio;
BufferId = bufferId;
ClydeHandle = handle;
Length = length;
ChannelCount = channelCount;
@@ -24,4 +33,9 @@ public sealed class AudioStream
Title = title;
Artist = artist;
}
public void Dispose()
{
_audio.Remove(this);
}
}

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using OpenTK.Audio.OpenAL;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.Player;
@@ -56,6 +57,8 @@ public sealed partial class AudioSystem : SharedAudioSystem
private EntityQuery<PhysicsComponent> _physicsQuery;
private float _maxRayLength;
private float _zOffset;
private float _audioEndBuffer;
public override float ZOffset
{
@@ -79,8 +82,6 @@ public sealed partial class AudioSystem : SharedAudioSystem
}
}
private float _zOffset;
/// <inheritdoc />
public override void Initialize()
{
@@ -108,20 +109,31 @@ public sealed partial class AudioSystem : SharedAudioSystem
SubscribeNetworkEvent<PlayAudioEntityMessage>(OnEntityAudio);
SubscribeNetworkEvent<PlayAudioPositionalMessage>(OnEntityCoordinates);
Subs.CVar(CfgManager, CVars.AudioEndBuffer, OnAudioBuffer, true);
Subs.CVar(CfgManager, CVars.AudioAttenuation, OnAudioAttenuation, true);
Subs.CVar(CfgManager, CVars.AudioRaycastLength, OnRaycastLengthChanged, true);
Subs.CVar(CfgManager, CVars.AudioTickRate, OnAudioTickRate, true);
InitializeLimit();
}
private void OnAudioBuffer(float value)
{
_audioEndBuffer = value;
}
private void OnAudioTickRate(int obj)
{
_audioFrameTime = 1f / obj;
_audioFrameTimeRemaining = MathF.Min(_audioFrameTimeRemaining, _audioFrameTime);
}
private void OnAudioState(EntityUid uid, AudioComponent component, ref AfterAutoHandleStateEvent args)
private void OnAudioState(Entity<AudioComponent> entity, ref AfterAutoHandleStateEvent args)
{
var component = entity.Comp;
if (component.LifeStage < ComponentLifeStage.Initialized)
return;
ApplyAudioParams(component.Params, component);
component.Source.Global = component.Global;
@@ -145,21 +157,29 @@ public sealed partial class AudioSystem : SharedAudioSystem
case AudioState.Stopped:
component.StopPlaying();
component.PlaybackPosition = 0f;
break;
return;
}
// If playback position changed then update it.
if (!string.IsNullOrEmpty(component.FileName))
{
var position = (float) ((component.PauseTime ?? Timing.CurTime) - component.AudioStart).TotalSeconds;
var currentPosition = component.Source.PlaybackPosition;
var diff = Math.Abs(position - currentPosition);
var position = (float) ((entity.Comp.PauseTime ?? Timing.CurTime) - entity.Comp.AudioStart).TotalSeconds;
var currentPosition = entity.Comp.Source.PlaybackPosition;
var diff = Math.Abs(position - currentPosition);
if (diff > 0.1f)
// Don't try to set the audio too far ahead.
if (!string.IsNullOrEmpty(entity.Comp.FileName))
{
if (position > GetAudioLengthImpl(entity.Comp.FileName).TotalSeconds - _audioEndBuffer)
{
component.PlaybackPosition = position;
entity.Comp.StopPlaying();
return;
}
}
// If the difference is minor then we'll just keep playing it.
if (diff > 0.1f)
{
entity.Comp.PlaybackPosition = position;
}
}
/// <summary>
@@ -207,6 +227,10 @@ public sealed partial class AudioSystem : SharedAudioSystem
private void SetupSource(Entity<AudioComponent> entity, AudioResource audioResource, TimeSpan? length = null)
{
var component = entity.Comp;
length ??= GetAudioLength(component.FileName);
// If audio came into range then start playback at the correct position.
var offset = ((entity.Comp.PauseTime ?? Timing.CurTime) - component.AudioStart).TotalSeconds;
if (TryAudioLimit(component.FileName))
{
@@ -230,10 +254,17 @@ public sealed partial class AudioSystem : SharedAudioSystem
// Don't play until first frame so occlusion etc. are correct.
component.Gain = 0f;
length ??= GetAudioLength(component.FileName);
// If audio came into range then start playback at the correct position.
var offset = (Timing.CurTime - component.AudioStart).TotalSeconds % length.Value.TotalSeconds;
// If the offset < buffer than just play it from the start.
if (offset < AudioDespawnBuffer)
{
offset = 0;
}
// Not enough audio to play
else if (offset > length.Value.TotalSeconds - _audioEndBuffer)
{
component.StopPlaying();
return;
}
if (offset > 0)
{

View File

@@ -13,6 +13,8 @@ namespace Robust.Client.Audio;
/// </summary>
internal sealed class HeadlessAudioManager : IAudioInternal
{
private int _audioBuffer;
/// <inheritdoc />
public void InitializePostWindowing()
{
@@ -65,6 +67,11 @@ internal sealed class HeadlessAudioManager : IAudioInternal
{
}
/// <inheritdoc />
public void Remove(AudioStream stream)
{
}
/// <inheritdoc />
public void StopAllAudio()
{
@@ -101,11 +108,11 @@ internal sealed class HeadlessAudioManager : IAudioInternal
public AudioStream LoadAudioRaw(ReadOnlySpan<short> samples, int channels, int sampleRate, string? name = null)
{
var length = TimeSpan.FromSeconds((double) samples.Length / channels / sampleRate);
return new AudioStream(null, length, channels, name);
return new AudioStream(this, _audioBuffer++, null, length, channels, name);
}
private static AudioStream AudioStreamFromMetadata(AudioMetadata metadata, string? name)
private AudioStream AudioStreamFromMetadata(AudioMetadata metadata, string? name)
{
return new AudioStream(null, metadata.Length, metadata.ChannelCount, name, metadata.Title, metadata.Artist);
return new AudioStream(this, _audioBuffer++, null, metadata.Length, metadata.ChannelCount, name, metadata.Title, metadata.Artist);
}
}

View File

@@ -44,6 +44,8 @@ internal interface IAudioInternal : IAudioManager
void SetAttenuation(Attenuation attenuation);
void Remove(AudioStream stream);
/// <summary>
/// Stops all audio from playing.
/// </summary>

View File

@@ -1,6 +1,5 @@
using System;
using System.IO;
using Robust.Client.Audio.Sources;
using Robust.Shared.Audio.Sources;
namespace Robust.Client.Audio;
@@ -11,7 +10,7 @@ namespace Robust.Client.Audio;
public interface IAudioManager
{
IAudioSource? CreateAudioSource(AudioStream stream);
AudioStream LoadAudioOggVorbis(Stream stream, string? name = null);
AudioStream LoadAudioWav(Stream stream, string? name = null);

View File

@@ -13,7 +13,7 @@ internal sealed class AudioSource : BaseAudioSource
/// <summary>
/// Underlying stream to the audio.
/// </summary>
private readonly AudioStream _sourceStream;
internal readonly AudioStream SourceStream;
#if DEBUG
private bool _didPositionWarning;
@@ -21,7 +21,7 @@ internal sealed class AudioSource : BaseAudioSource
public AudioSource(AudioManager master, int sourceHandle, AudioStream sourceStream) : base(master, sourceHandle)
{
_sourceStream = sourceStream;
SourceStream = sourceStream;
}
/// <inheritdoc />
@@ -47,13 +47,13 @@ internal sealed class AudioSource : BaseAudioSource
#if DEBUG
// OpenAL doesn't seem to want to play stereo positionally.
// Log a warning if people try to.
if (_sourceStream.ChannelCount > 1 && !_didPositionWarning)
if (SourceStream.ChannelCount > 1 && !_didPositionWarning)
{
_didPositionWarning = true;
Master.OpenALSawmill.Warning("Attempting to set position on audio source with multiple audio channels! Stream: '{0}'. Make sure the audio is MONO, not stereo.",
_sourceStream.Name);
SourceStream.Name);
// warning isn't enough, people just ignore it :(
DebugTools.Assert(false, $"Attempting to set position on audio source with multiple audio channels! Stream: '{_sourceStream.Name}'. Make sure the audio is MONO, not stereo.");
DebugTools.Assert(false, $"Attempting to set position on audio source with multiple audio channels! Stream: '{SourceStream.Name}'. Make sure the audio is MONO, not stereo.");
}
#endif

View File

@@ -208,6 +208,12 @@ public abstract class BaseAudioSource : IAudioSource
}
set
{
if (float.IsNaN(value))
{
Master.LogError($"Tried to set NaN gain, setting audio source to 0f: {Environment.StackTrace}");
value = 0f;
}
_checkDisposed();
var priorOcclusion = 1f;
if (!IsEfxSupported)

View File

@@ -46,6 +46,7 @@ using Robust.Shared.Replays;
using Robust.Shared.Serialization;
using Robust.Shared.Timing;
using Robust.Shared.Upload;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Robust.Client
@@ -102,6 +103,7 @@ namespace Robust.Client
deps.Register<ProfViewManager>();
deps.Register<IGamePrototypeLoadManager, GamePrototypeLoadManager>();
deps.Register<NetworkResourceManager>();
deps.Register<IReloadManager, ReloadManager>();
switch (mode)
{

View File

@@ -93,6 +93,7 @@ namespace Robust.Client
[Dependency] private readonly IReplayPlaybackManager _replayPlayback = default!;
[Dependency] private readonly IReplayRecordingManagerInternal _replayRecording = default!;
[Dependency] private readonly IReflectionManager _reflectionManager = default!;
[Dependency] private readonly IReloadManager _reload = default!;
private IWebViewManagerHook? _webViewHook;
@@ -185,6 +186,7 @@ namespace Robust.Client
// before prototype load.
ProgramShared.FinishCheckBadFileExtensions(checkBadExtensions);
_reload.Initialize();
_reflectionManager.Initialize();
_prototypeManager.Initialize();
_prototypeManager.LoadDefaultPrototypes();

View File

@@ -25,12 +25,6 @@ public sealed class UserInterfaceSystem : SharedUserInterfaceSystem
ProtoManager.PrototypesReloaded -= OnProtoReload;
}
protected override void OnUserInterfaceShutdown(Entity<UserInterfaceComponent> ent, ref ComponentShutdown args)
{
base.OnUserInterfaceShutdown(ent, ref args);
_savedPositions.Remove(ent.Owner);
}
/// <inheritdoc />
public override void OpenUi(Entity<UserInterfaceComponent?> entity, Enum key, bool predicted = false)
{

View File

@@ -126,7 +126,7 @@ namespace Robust.Client.Graphics.Clyde
DebugTools.Assert(space != OverlaySpace.ScreenSpaceBelowWorld && space != OverlaySpace.ScreenSpace);
var mapId = vp.Eye!.Position.MapId;
var args = new OverlayDrawArgs(space, null, vp, _renderHandle, new UIBox2i((0, 0), vp.Size), _mapManager.GetMapEntityIdOrThrow(mapId), mapId, worldBox, worldBounds);
var args = new OverlayDrawArgs(space, null, vp, _renderHandle, new UIBox2i((0, 0), vp.Size), _mapSystem.GetMapOrInvalid(mapId), mapId, worldBox, worldBounds);
if (!overlay.BeforeDraw(args))
return;
@@ -178,7 +178,7 @@ namespace Robust.Client.Graphics.Clyde
var worldAABB = worldBounds.CalcBoundingBox();
var mapId = vp.Eye!.Position.MapId;
var args = new OverlayDrawArgs(space, vpControl, vp, handle, bounds, _mapManager.GetMapEntityIdOrThrow(mapId), mapId, worldAABB, worldBounds);
var args = new OverlayDrawArgs(space, vpControl, vp, handle, bounds, _mapSystem.GetMapOrInvalid(mapId), mapId, worldAABB, worldBounds);
foreach (var overlay in list)
{

View File

@@ -10,6 +10,7 @@ using Robust.Client.Input;
using Robust.Client.Map;
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface;
using Robust.Client.Utility;
using Robust.Shared;
using Robust.Shared.Configuration;
using Robust.Shared.ContentPack;
@@ -23,6 +24,7 @@ using Robust.Shared.Maths;
using Robust.Shared.Profiling;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
using TextureWrapMode = Robust.Shared.Graphics.TextureWrapMode;
namespace Robust.Client.Graphics.Clyde
@@ -48,6 +50,8 @@ namespace Robust.Client.Graphics.Clyde
[Dependency] private readonly ILocalizationManager _loc = default!;
[Dependency] private readonly IInputManager _inputManager = default!;
[Dependency] private readonly ClientEntityManager _entityManager = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly IReloadManager _reloads = default!;
private GLUniformBuffer<ProjViewMatrices> ProjViewUBO = default!;
private GLUniformBuffer<UniformConstants> UniformConstantsUBO = default!;
@@ -99,6 +103,16 @@ namespace Robust.Client.Graphics.Clyde
_sawmillOgl = _logManager.GetSawmill("clyde.ogl");
_sawmillWin = _logManager.GetSawmill("clyde.win");
_reloads.Register("/Shaders", "*.swsl");
_reloads.Register("/Textures/Shaders", "*.swsl");
_reloads.Register("/Textures", "*.jpg");
_reloads.Register("/Textures", "*.jpeg");
_reloads.Register("/Textures", "*.png");
_reloads.Register("/Textures", "*.webp");
_reloads.OnChanged += OnChange;
_proto.PrototypesReloaded += OnProtoReload;
_cfg.OnValueChanged(CVars.DisplayOGLCheckErrors, b => _checkGLErrors = b, true);
_cfg.OnValueChanged(CVars.DisplayVSync, VSyncChanged, true);
_cfg.OnValueChanged(CVars.DisplayWindowMode, WindowModeChanged, true);
@@ -122,6 +136,38 @@ namespace Robust.Client.Graphics.Clyde
return InitWindowing();
}
private void OnProtoReload(PrototypesReloadedEventArgs obj)
{
if (!obj.WasModified<ShaderPrototype>())
return;
foreach (var shader in obj.ByType[typeof(ShaderPrototype)].Modified.Keys)
{
_resourceCache.ReloadResource<ShaderSourceResource>(shader);
}
}
private void OnChange(ResPath obj)
{
if ((obj.TryRelativeTo(new ResPath("/Shaders"), out _) || obj.TryRelativeTo(new ResPath("/Textures/Shaders"), out _)) && obj.Extension == "swsl")
{
_resourceCache.ReloadResource<ShaderSourceResource>(obj);
}
if (obj.TryRelativeTo(new ResPath("/Textures"), out _) && !obj.TryRelativeTo(new ResPath("/Textures/Tiles"), out _))
{
if (obj.Extension == "jpg" || obj.Extension == "jpeg" || obj.Extension == "webp")
{
_resourceCache.ReloadResource<TextureResource>(obj);
}
if (obj.Extension == "png")
{
_resourceCache.ReloadResource<TextureResource>(obj);
}
}
}
public bool InitializePostWindowing()
{
_gameThread = Thread.CurrentThread;

View File

@@ -1,20 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.Map;
using Robust.Client.ResourceManagement;
using Robust.Client.Utility;
using Robust.Shared.Console;
using Robust.Shared.ContentPack;
using Robust.Shared.GameObjects;
using Robust.Shared.Graphics;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Toolshed;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
@@ -23,6 +18,8 @@ namespace Robust.Client.Map
{
internal sealed class ClydeTileDefinitionManager : TileDefinitionManager, IClydeTileDefinitionManager, IPostInjectInit
{
[Dependency] private readonly IPrototypeManager _protoManager = default!;
[Dependency] private readonly IReloadManager _reload = default!;
[Dependency] private readonly IResourceManager _manager = default!;
[Dependency] private readonly ILogManager _logManager = default!;
@@ -57,6 +54,30 @@ namespace Robust.Client.Map
{
base.Initialize();
_protoManager.PrototypesReloaded += OnProtoReload;
_reload.Register("/Textures/Tiles", "*.png");
_reload.OnChanged += OnReload;
_genTextureAtlas();
}
private void OnProtoReload(PrototypesReloadedEventArgs obj)
{
if (!obj.WasModified<ITileDefinition>())
return;
_genTextureAtlas();
}
private void OnReload(ResPath obj)
{
if (obj.Extension != "png")
return;
if (!obj.TryRelativeTo(new ResPath("/Textures/Tiles"), out _))
return;
_genTextureAtlas();
}

View File

@@ -1,35 +1,23 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using Robust.Client.Graphics;
using Robust.Client.Timing;
using Robust.Shared;
using Robust.Shared.Configuration;
using Robust.Client.Utility;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Network;
using Robust.Shared.Network.Messages;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
using Timer = Robust.Shared.Timing.Timer;
namespace Robust.Client.Prototypes
{
public sealed class ClientPrototypeManager : PrototypeManager
{
[Dependency] private readonly IClyde _clyde = default!;
[Dependency] private readonly INetManager _netManager = default!;
[Dependency] private readonly IClientGameTiming _timing = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IGameControllerInternal _controller = default!;
private readonly List<FileSystemWatcher> _watchers = new();
private readonly TimeSpan _reloadDelay = TimeSpan.FromMilliseconds(10);
private CancellationTokenSource _reloadToken = new();
private readonly HashSet<ResPath> _reloadQueue = new();
[Dependency] private readonly IReloadManager _reload = default!;
public override void Initialize()
{
@@ -37,9 +25,8 @@ namespace Robust.Client.Prototypes
_netManager.RegisterNetMessage<MsgReloadPrototypes>(accept: NetMessageAccept.Server);
_clyde.OnWindowFocused += WindowFocusedChanged;
WatchResources();
_reload.Register("/Prototypes", "*.yml");
_reload.OnChanged += ReloadPrototypeQueue;
}
public override void LoadDefaultPrototypes(Dictionary<Type, HashSet<string>>? changed = null)
@@ -49,99 +36,27 @@ namespace Robust.Client.Prototypes
ResolveResults();
}
private void WindowFocusedChanged(WindowFocusedEventArgs args)
private void ReloadPrototypeQueue(ResPath file)
{
#if TOOLS
if (args.Focused && _reloadQueue.Count > 0)
{
Timer.Spawn(_reloadDelay, ReloadPrototypeQueue, _reloadToken.Token);
}
else
{
_reloadToken.Cancel();
_reloadToken = new CancellationTokenSource();
}
#endif
}
if (file.Extension != "yml")
return;
private void ReloadPrototypeQueue()
{
#if TOOLS
var sw = Stopwatch.StartNew();
var msg = new MsgReloadPrototypes();
msg.Paths = _reloadQueue.ToArray();
var msg = new MsgReloadPrototypes
{
Paths = [file]
};
_netManager.ClientSendMessage(msg);
// Reloading prototypes modifies entities. This currently causes some state management debug asserts to
// fail. To avoid this, we set `IGameTiming.ApplyingState` to true, even though this isn't really applying a
// server state.
using var _ = _timing.StartStateApplicationArea();
ReloadPrototypes(_reloadQueue);
_reloadQueue.Clear();
ReloadPrototypes([file]);
Logger.Info($"Reloaded prototypes in {sw.ElapsedMilliseconds} ms");
#endif
}
private void WatchResources()
{
if (!_cfg.GetCVar(CVars.ResPrototypeReloadWatch))
return;
#if TOOLS
foreach (var path in Resources.GetContentRoots().Select(r => r.ToString())
.Where(r => Directory.Exists(r + "/Prototypes")).Select(p => p + "/Prototypes"))
{
var watcher = new FileSystemWatcher(path, "*.yml")
{
IncludeSubdirectories = true,
NotifyFilter = NotifyFilters.LastWrite
};
watcher.Changed += (_, args) =>
{
switch (args.ChangeType)
{
case WatcherChangeTypes.Renamed:
case WatcherChangeTypes.Deleted:
return;
case WatcherChangeTypes.Created:
// case WatcherChangeTypes.Deleted:
case WatcherChangeTypes.Changed:
case WatcherChangeTypes.All:
break;
default:
throw new ArgumentOutOfRangeException();
}
TaskManager.RunOnMainThread(() =>
{
var file = new ResPath(args.FullPath);
foreach (var root in Resources.GetContentRoots())
{
if (!file.TryRelativeTo(root, out var relative))
{
continue;
}
_reloadQueue.Add(relative.Value);
}
});
};
try
{
watcher.EnableRaisingEvents = true;
_watchers.Add(watcher);
}
catch (IOException ex)
{
Logger.Error($"Watching resources in path {path} threw an exception:\n{ex}");
}
}
#endif
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Threading;
using Robust.Client.Audio;
using Robust.Shared.Audio;
using Robust.Shared.ContentPack;
@@ -26,22 +27,26 @@ public sealed class AudioResource : BaseResource
throw new FileNotFoundException("Content file does not exist for audio sample.");
}
using (var fileStream = cache.ContentFileRead(path))
using var fileStream = cache.ContentFileRead(path);
var audioManager = dependencies.Resolve<IAudioInternal>();
if (path.Extension == "ogg")
{
var audioManager = dependencies.Resolve<IAudioInternal>();
if (path.Extension == "ogg")
{
AudioStream = audioManager.LoadAudioOggVorbis(fileStream, path.ToString());
}
else if (path.Extension == "wav")
{
AudioStream = audioManager.LoadAudioWav(fileStream, path.ToString());
}
else
{
throw new NotSupportedException("Unable to load audio files outside of ogg Vorbis or PCM wav");
}
AudioStream = audioManager.LoadAudioOggVorbis(fileStream, path.ToString());
}
else if (path.Extension == "wav")
{
AudioStream = audioManager.LoadAudioWav(fileStream, path.ToString());
}
else
{
throw new NotSupportedException("Unable to load audio files outside of ogg Vorbis or PCM wav");
}
}
public override void Reload(IDependencyCollection dependencies, ResPath path, CancellationToken ct = default)
{
dependencies.Resolve<IAudioInternal>().Remove(AudioStream);
Load(dependencies, path);
}
public AudioResource(AudioStream stream) : base()

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
@@ -176,6 +176,71 @@ namespace Robust.Client.UserInterface.Controls
_scrollBar.MoveToEnd();
}
/// <summary>
/// Replace the current list of items with the items in newItems.
/// newItems should be in the order which they should appear in the list,
/// and items are considered equal if the Item text is equal in each item.
///
/// Provided the existing items have not been re-ordered relative to each
/// other, any items which already exist in the list are not destroyed,
/// which maintains consistency of scrollbars, selected items, etc.
/// </summary>
/// <param name="newItems">The list of items to update this list to</param>
public void SetItems(List<Item> newItems)
{
SetItems(newItems, (a,b) => string.Compare(a.Text, b.Text));
}
/// <inheritdoc />
/// <summary>
/// This variant allows for a custom equality operator to compare items, when
/// comparing the Item text is not desired.
/// </summary>
/// <param name="itemCmp">Comparison function to compare existing to new items.</param>
public void SetItems(List<Item> newItems, Comparison<Item> itemCmp)
{
// Walk through the existing items in this list and in newItems
// in parallel to synchronize our items with those in newItems.
int i = this.Count - 1;
int j = newItems.Count - 1;
while(i >= 0 && j >= 0)
{
var cmpResult = itemCmp(this[i], newItems[j]);
if (cmpResult == 0)
{
// This item exists in both our list and `newItems`. Nothing to do.
i--;
j--;
}
else if (cmpResult > 0)
{
// Item exists in our list, but not in `newItems`. Remove it.
RemoveAt(i);
i--;
}
else if (cmpResult < 0)
{
// A new entry which doesn't exist in our list. Insert it.
Insert(i + 1, newItems[j]);
j--;
}
}
// Any remaining items in our list don't exist in `newItems` so remove them
while (i >= 0)
{
RemoveAt(i);
i--;
}
// And finally, any remaining items in `newItems` don't exist in our list. Create them.
while (j >= 0)
{
Insert(0, newItems[j]);
j--;
}
}
// Without this attribute, this would compile into a property called "Item", causing problems with the Item class.
[System.Runtime.CompilerServices.IndexerName("IndexItem")]
public Item this[int index]

View File

@@ -21,7 +21,6 @@ namespace Robust.Client.UserInterface.Controls
[Virtual]
public class LineEdit : Control
{
[Dependency] private readonly IClyde _clyde = default!;
[Dependency] private readonly IConfigurationManager _cfgManager = default!;
[Dependency] private readonly IGameTiming _timing = default!;

View File

@@ -38,7 +38,6 @@ namespace Robust.Client.UserInterface.Controls;
public sealed class TextEdit : Control
{
[Dependency] private readonly IClipboardManager _clipboard = null!;
[Dependency] private readonly IClyde _clyde = null!;
// @formatter:off
public const string StylePropertyCursorColor = "cursor-color";

View File

@@ -0,0 +1,141 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Robust.Client.Graphics;
using Robust.Shared;
using Robust.Shared.Asynchronous;
using Robust.Shared.Configuration;
using Robust.Shared.ContentPack;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Utility;
using Timer = Robust.Shared.Timing.Timer;
namespace Robust.Client.Utility;
internal sealed class ReloadManager : IReloadManager
{
[Dependency] private readonly IClyde _clyde = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly ILogManager _logMan = default!;
[Dependency] private readonly IResourceManager _res = default!;
[Dependency] private readonly ITaskManager _tasks = default!;
private readonly TimeSpan _reloadDelay = TimeSpan.FromMilliseconds(10);
private CancellationTokenSource _reloadToken = new();
private readonly HashSet<ResPath> _reloadQueue = new();
private List<FileSystemWatcher> _watchers = new();
public event Action<ResPath>? OnChanged;
private ISawmill _sawmill = default!;
public void Initialize()
{
_sawmill = _logMan.GetSawmill("reload");
_clyde.OnWindowFocused += WindowFocusedChanged;
}
private void WindowFocusedChanged(WindowFocusedEventArgs args)
{
#if TOOLS
if (args.Focused && _reloadQueue.Count > 0)
{
Timer.Spawn(_reloadDelay, ReloadFiles, _reloadToken.Token);
}
else
{
_reloadToken.Cancel();
_reloadToken = new CancellationTokenSource();
}
#endif
}
private void ReloadFiles()
{
foreach (var file in _reloadQueue)
{
var rootedFile = file.ToRootedPath();
if (!_res.ContentFileExists(rootedFile))
continue;
_sawmill.Info($"Reloading {rootedFile}");
OnChanged?.Invoke(rootedFile);
}
_reloadQueue.Clear();
}
public void Register(string directory, string filter)
{
if (!_cfg.GetCVar(CVars.ResPrototypeReloadWatch))
return;
#if TOOLS
foreach (var root in _res.GetContentRoots())
{
var path = root + directory;
if (!Directory.Exists(path))
{
continue;
}
var watcher = new FileSystemWatcher(path, filter)
{
IncludeSubdirectories = true,
NotifyFilter = NotifyFilters.LastWrite
};
_watchers.Add(watcher);
watcher.Changed += OnWatch;
try
{
watcher.EnableRaisingEvents = true;
}
catch (IOException ex)
{
Logger.Error($"Watching resources in path {path} threw an exception:\n{ex}");
}
}
void OnWatch(object sender, FileSystemEventArgs args)
{
switch (args.ChangeType)
{
case WatcherChangeTypes.Renamed:
case WatcherChangeTypes.Deleted:
return;
case WatcherChangeTypes.Created:
// case WatcherChangeTypes.Deleted:
case WatcherChangeTypes.Changed:
case WatcherChangeTypes.All:
break;
default:
throw new ArgumentOutOfRangeException();
}
_tasks.RunOnMainThread(() =>
{
var fullPath = args.FullPath.Replace(Path.DirectorySeparatorChar, '/');
var file = new ResPath(fullPath);
foreach (var rootIter in _res.GetContentRoots())
{
if (!file.TryRelativeTo(rootIter, out var relative))
{
continue;
}
_reloadQueue.Add(relative.Value);
}
});
}
#endif
}
}

View File

@@ -43,8 +43,15 @@ namespace Robust.Server.Console.Commands
return;
}
_ent.System<MapLoaderSystem>().TrySaveGrid(uid, new ResPath(args[1]));
shell.WriteLine("Save successful. Look in the user data directory.");
bool saveSuccess = _ent.System<MapLoaderSystem>().TrySaveGrid(uid, new ResPath(args[1]));
if(saveSuccess)
{
shell.WriteLine("Save successful. Look in the user data directory.");
}
else
{
shell.WriteError("Save unsuccessful!");
}
}
public override CompletionResult GetCompletion(IConsoleShell shell, string[] args)
@@ -207,8 +214,15 @@ namespace Robust.Server.Console.Commands
}
shell.WriteLine(Loc.GetString("cmd-savemap-attempt", ("mapId", mapId), ("path", args[1])));
_system.GetEntitySystem<MapLoaderSystem>().TrySaveMap(mapId, new ResPath(args[1]));
shell.WriteLine(Loc.GetString("cmd-savemap-success"));
bool saveSuccess = _system.GetEntitySystem<MapLoaderSystem>().TrySaveMap(mapId, new ResPath(args[1]));
if(saveSuccess)
{
shell.WriteLine(Loc.GetString("cmd-savemap-success"));
}
else
{
shell.WriteError(Loc.GetString("cmd-savemap-error"));
}
}
}

View File

@@ -29,12 +29,12 @@ namespace Robust.Server
/// <summary>
/// Directory to load all assemblies from.
/// </summary>
public ResPath AssemblyDirectory { get; init; } = new(@"/Assemblies");
public ResPath AssemblyDirectory { get; init; } = new(@"/Assemblies/");
/// <summary>
/// Directory to load all prototypes from.
/// </summary>
public ResPath PrototypeDirectory { get; init; } = new(@"/Prototypes");
public ResPath PrototypeDirectory { get; init; } = new(@"/Prototypes/");
/// <summary>
/// Whether to disable mounting the "Resources/" folder on FULL_RELEASE.

View File

@@ -212,6 +212,48 @@ namespace Robust.Shared.Maths
return surfaceIntersect / (Area(this) + Area(other) - surfaceIntersect);
}
public readonly bool IsValid()
{
var d = Vector2.Subtract(TopRight, BottomLeft);
bool valid = d.X >= 0.0f && d.Y >= 0.0f;
valid = valid && BottomLeft.IsValid() && TopRight.IsValid();
return valid;
}
/// <summary>
/// Enlarges this box to contain another box.
/// </summary>
public bool EnlargeAabb(Box2 other)
{
var changed = false;
if (other.Left < Left)
{
Left = other.Left;
changed = true;
}
if (other.Bottom < Bottom)
{
Bottom = other.Bottom;
changed = true;
}
if (Right < other.Right)
{
Right = other.Right;
changed = true;
}
if (other.Top < Top)
{
Top = other.Top;
changed = true;
}
return changed;
}
/// <summary>
/// Returns the smallest rectangle that contains both of the rectangles.
/// </summary>
@@ -401,6 +443,15 @@ namespace Robust.Shared.Maths
public static float Perimeter(in Box2 box)
=> (box.Width + box.Height) * 2;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Pure]
public static Box2 Union(Box2 a, Box2 b)
{
return new Box2(
Vector2.Min(a.BottomLeft, b.BottomLeft),
Vector2.Max(a.TopRight, b.TopRight));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Pure]
public static Box2 Union(in Vector2 a, in Vector2 b)

View File

@@ -15,6 +15,7 @@ public static class Vector2Helpers
/// </summary>
public static readonly Vector2 Half = new(0.5f, 0.5f);
[Pure]
public static bool IsValid(this Vector2 v)
{
if (float.IsNaN(v.X) || float.IsNaN(v.Y))
@@ -30,6 +31,13 @@ public static class Vector2Helpers
return true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[Pure]
public static Vector2 MulAdd(Vector2 a, float s, Vector2 b)
{
return new Vector2(a.X + s * b.X, a.Y + s * b.Y);
}
public static Vector2 GetLengthAndNormalize(this Vector2 v, ref float length)
{
length = v.Length();

View File

@@ -5,6 +5,9 @@ using Robust.Shared.Audio.Systems;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Utility;
namespace Robust.Shared.Audio
{
@@ -29,11 +32,25 @@ namespace Robust.Shared.Audio
[DataDefinition]
public partial struct AudioParams
{
private float _volume = Default.Volume;
/// <summary>
/// Base volume to play the audio at, in dB.
/// </summary>
[DataField]
public float Volume { get; set; } = Default.Volume;
public float Volume
{
get => _volume;
set
{
if (float.IsNaN(value))
{
value = float.NegativeInfinity;
}
_volume = value;
}
}
/// <summary>
/// Scale for the audio pitch.

View File

@@ -37,7 +37,7 @@ public abstract partial class SharedAudioSystem : EntitySystem
[Dependency] protected readonly MetaDataSystem MetadataSys = default!;
[Dependency] protected readonly SharedTransformSystem XformSystem = default!;
private const float AudioDespawnBuffer = 1f;
public const float AudioDespawnBuffer = 1f;
/// <summary>
/// Default max range at which the sound can be heard.
@@ -412,6 +412,13 @@ public abstract partial class SharedAudioSystem : EntitySystem
if (component.Params.Volume.Equals(value))
return;
// Not a log error for now because if something has a negative infinity volume (i.e. 0 gain) then subtracting from it can
// easily cause this and making callers deal with it everywhere is quite annoying.
if (float.IsNaN(value))
{
value = float.NegativeInfinity;
}
component.Params.Volume = value;
component.Volume = value;
DirtyField(entity.Value, component, nameof(AudioComponent.Params));

View File

@@ -1233,6 +1233,12 @@ namespace Robust.Shared
public static readonly CVarDef<float> AudioRaycastLength =
CVarDef.Create("audio.raycast_length", SharedAudioSystem.DefaultSoundRange, CVar.ARCHIVE | CVar.CLIENTONLY);
/// <summary>
/// Maximum offset for audio to be played at from its full duration. If it's past this then the audio won't be played.
/// </summary>
public static readonly CVarDef<float> AudioEndBuffer =
CVarDef.Create("audio.end_buffer", 0.01f, CVar.REPLICATED);
/// <summary>
/// Tickrate for audio calculations.
/// OpenAL recommends 30TPS. This is to avoid running raycasts every frame especially for high-refresh rate monitors.

View File

@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using JetBrains.Annotations;
@@ -186,7 +186,9 @@ public static class CompletionHelper
public static IEnumerable<CompletionOption> MapUids(IEntityManager? entManager = null)
{
return Components<MapComponent>(string.Empty, entManager);
IoCManager.Resolve(ref entManager);
return Components<MapComponent>(string.Empty, entManager, limit: 128);
}
/// <summary>
@@ -194,7 +196,7 @@ public static class CompletionHelper
/// </summary>
public static IEnumerable<CompletionOption> NetEntities(string text, IEntityManager? entManager = null, int limit = 20)
{
if (!NetEntity.TryParse(text, out _))
if (text != string.Empty && !NetEntity.TryParse(text, out _))
yield break;
IoCManager.Resolve(ref entManager);
@@ -214,7 +216,7 @@ public static class CompletionHelper
public static IEnumerable<CompletionOption> Components<T>(string text, IEntityManager? entManager = null, int limit = 20) where T : IComponent
{
if (!NetEntity.TryParse(text, out _))
if (text != string.Empty && !NetEntity.TryParse(text, out _))
yield break;
IoCManager.Resolve(ref entManager);

View File

@@ -60,7 +60,9 @@ namespace Robust.Shared.ContentPack
internal string GetPath(ResPath relPath)
{
return Path.GetFullPath(Path.Combine(_directory.FullName, relPath.ToRelativeSystemPath()));
return Path.GetFullPath(Path.Combine(_directory.FullName, relPath.ToRelativeSystemPath()))
// Sanitise platform-specific path and standardize it for engine use.
.Replace(Path.DirectorySeparatorChar, '/');
}
/// <inheritdoc />

View File

@@ -77,7 +77,7 @@ namespace Robust.Shared.ContentPack
var paths = new List<ResPath>();
foreach (var filePath in _res.ContentFindRelativeFiles(mountPath)
.Where(p => !p.ToString().Contains('/') && p.Filename.StartsWith(filterPrefix) &&
.Where(p => p.Filename.StartsWith(filterPrefix) &&
p.Extension == "dll"))
{
var fullPath = mountPath / filePath;

View File

@@ -379,7 +379,9 @@ namespace Robust.Shared.ContentPack
{
if (root is DirLoader loader)
{
yield return new ResPath(loader.GetPath(new ResPath(@"/")));
var rootDir = loader.GetPath(new ResPath(@"/"));
yield return new ResPath(rootDir);
}
}
}

View File

@@ -736,6 +736,15 @@ Types:
- "void .ctor(string)"
- "void .ctor(string, System.Text.RegularExpressions.RegexOptions)"
- "void .ctor(string, System.Text.RegularExpressions.RegexOptions, System.TimeSpan)"
- "int Count(string)"
- "int Count(System.ReadOnlySpan`1<char>)"
- "int Count(System.ReadOnlySpan`1<char>, int)"
- "int Count(string, string)"
- "int Count(string, string, System.Text.RegularExpressions.RegexOptions)"
- "int Count(string, string, System.Text.RegularExpressions.RegexOptions, System.TimeSpan)"
- "int Count(System.ReadOnlySpan`1<char>, string)"
- "int Count(System.ReadOnlySpan`1<char>, string, System.Text.RegularExpressions.RegexOptions)"
- "int Count(System.ReadOnlySpan`1<char>, string, System.Text.RegularExpressions.RegexOptions, System.TimeSpan)"
RegexMatchTimeoutException: { All: True }
RegexOptions: { } # Enum
RegexParseError: { }
@@ -789,6 +798,7 @@ Types:
- "bool Equals(System.ReadOnlySpan`1<char>)"
- "bool Equals(System.Text.StringBuilder)"
- "char get_Chars(int)"
- "void set_Chars(int, char)"
- "int EnsureCapacity(int)"
- "int get_Capacity()"
- "int get_Length()"

View File

@@ -95,7 +95,7 @@ public sealed partial class MapLoaderSystem
}
catch (Exception e)
{
Log.Error($"Caught exception while creating entities: {e}");
Log.Error($"Caught exception while creating entities for map {file}: {e}");
Delete(deserializer.Result);
throw;
}
@@ -103,7 +103,7 @@ public sealed partial class MapLoaderSystem
if (opts.ExpectedCategory is { } exp && exp != deserializer.Result.Category)
{
// Did someone try to load a map file as a grid or vice versa?
Log.Error($"File does not contain the expected data. Expected {exp} but got {deserializer.Result.Category}");
Log.Error($"Map {file} does not contain the expected data. Expected {exp} but got {deserializer.Result.Category}");
Delete(deserializer.Result);
return false;
}
@@ -203,6 +203,35 @@ public sealed partial class MapLoaderSystem
return false;
}
/// <summary>
/// Tries to load a grid entity from a file and parent it to a newly created map.
/// If the file does not contain exactly one grid, this will return false and delete loaded entities.
/// </summary>
public bool TryLoadGrid(
ResPath path,
[NotNullWhen(true)] out Entity<MapComponent>? map,
[NotNullWhen(true)] out Entity<MapGridComponent>? grid,
DeserializationOptions? options = null,
Vector2 offset = default,
Angle rot = default)
{
var opts = options ?? DeserializationOptions.Default;
var mapUid = _mapSystem.CreateMap(out var mapId, runMapInit: opts.InitializeMaps);
if (opts.PauseMaps)
_mapSystem.SetPaused(mapUid, true);
if (!TryLoadGrid(mapId, path, out grid, options, offset, rot))
{
Del(mapUid);
map = null;
return false;
}
map = new(mapUid, Comp<MapComponent>(mapUid));
return true;
}
private void ApplyTransform(EntityDeserializer deserializer, MapLoadOptions opts)
{
if (opts.Rotation == Angle.Zero && opts.Offset == Vector2.Zero)

View File

@@ -1101,7 +1101,7 @@ namespace Robust.Shared.GameObjects
MetaDataComponent? meta = null,
params Type[] sourceComponents)
{
if (!MetaQuery.TryGetComponent(source, out meta))
if (!MetaQuery.TryGetComponent(target, out meta))
return false;
var allCopied = true;

View File

@@ -189,10 +189,12 @@ namespace Robust.Shared.GameObjects
return false;
var compType = component.GetType();
var compName = _componentFactory.GetComponentName(compType);
if (compName == _xformName || compName == _metaReg.Name)
if (compType == typeof(TransformComponent) || compType == typeof(MetaDataComponent))
continue;
var compName = _componentFactory.GetComponentName(compType);
// If the component isn't on the prototype then it's custom.
if (!protoData.TryGetValue(compName, out var protoMapping))
return false;
@@ -208,9 +210,7 @@ namespace Robust.Shared.GameObjects
return false;
}
var diff = compMapping.Except(protoMapping);
if (diff != null && diff.Children.Count != 0)
if (compMapping.AnyExcept(protoMapping))
return false;
}

View File

@@ -490,7 +490,7 @@ public sealed partial class EntityLookupSystem : EntitySystem
{
var bounds = fixture.Shape.ComputeAABB(broadphaseTransform, i);
var proxy = fixture.Proxies[i];
tree.MoveProxy(proxy.ProxyId, bounds, Vector2.Zero);
tree.MoveProxy(proxy.ProxyId, bounds);
proxy.AABB = bounds;
moveBuffer[proxy] = fixture.Shape.ComputeAABB(mapTransform, i);
}

View File

@@ -162,7 +162,7 @@ public abstract partial class SharedMapSystem
if (TryComp<GridTreeComponent>(xform.MapUid, out var gridTree))
{
gridTree.Tree.MoveProxy(component.MapProxy, in aabb, Vector2.Zero);
gridTree.Tree.MoveProxy(component.MapProxy, in aabb);
}
if (TryComp<MovedGridsComponent>(xform.MapUid, out var movedGrids))
@@ -187,7 +187,7 @@ public abstract partial class SharedMapSystem
if (TryComp<GridTreeComponent>(xform.MapUid, out var gridTree))
{
gridTree.Tree.MoveProxy(component.MapProxy, in aabb, Vector2.Zero);
gridTree.Tree.MoveProxy(component.MapProxy, in aabb);
}
if (TryComp<MovedGridsComponent>(xform.MapUid, out var movedGrids))
@@ -309,6 +309,7 @@ public abstract partial class SharedMapSystem
ChunkDatum data)
{
var counter = 0;
var gridEnt = new Entity<MapGridComponent>(uid, component);
if (data.IsDeleted())
{
@@ -326,10 +327,12 @@ public abstract partial class SharedMapSystem
var gridIndices = deletedChunk.ChunkTileToGridTile((x, y));
var newTileRef = new TileRef(uid, gridIndices, Tile.Empty);
_mapInternal.RaiseOnTileChanged(newTileRef, oldTile, index);
_mapInternal.RaiseOnTileChanged(gridEnt, newTileRef, oldTile, index);
}
}
deletedChunk.CachedBounds = Box2i.Empty;
deletedChunk.SuppressCollisionRegeneration = false;
return;
}
@@ -350,10 +353,12 @@ public abstract partial class SharedMapSystem
var gridIndices = chunk.ChunkTileToGridTile((x, y));
var newTileRef = new TileRef(uid, gridIndices, tile);
_mapInternal.RaiseOnTileChanged(newTileRef, oldTile, index);
_mapInternal.RaiseOnTileChanged(gridEnt, newTileRef, oldTile, index);
}
}
DebugTools.Assert(chunk.Fixtures.SetEquals(data.Fixtures));
// These should never refer to the same object
DebugTools.AssertNotEqual(chunk.Fixtures, data.Fixtures);
@@ -508,7 +513,7 @@ public abstract partial class SharedMapSystem
if (TryComp<GridTreeComponent>(xform.MapUid, out var gridTree))
{
var proxy = gridTree.Tree.CreateProxy(in aabb, (uid, _fixturesQuery.Comp(uid), component));
var proxy = gridTree.Tree.CreateProxy(in aabb, uint.MaxValue, (uid, _fixturesQuery.Comp(uid), component));
DebugTools.Assert(component.MapProxy == DynamicTree.Proxy.Free);
component.MapProxy = proxy;
}
@@ -562,7 +567,7 @@ public abstract partial class SharedMapSystem
if (TryComp<GridTreeComponent>(xform.MapUid, out var gridTree))
{
var proxy = gridTree.Tree.CreateProxy(in aabb, (uid, _fixturesQuery.Comp(uid), grid));
var proxy = gridTree.Tree.CreateProxy(in aabb, uint.MaxValue, (uid, _fixturesQuery.Comp(uid), grid));
DebugTools.Assert(grid.MapProxy == DynamicTree.Proxy.Free);
grid.MapProxy = proxy;
}
@@ -1614,7 +1619,7 @@ public abstract partial class SharedMapSystem
if (!MapManager.SuppressOnTileChanged)
{
var newTileRef = new TileRef(uid, gridTile, newTile);
_mapInternal.RaiseOnTileChanged(newTileRef, oldTile, mapChunk.Indices);
_mapInternal.RaiseOnTileChanged((uid, grid), newTileRef, oldTile, mapChunk.Indices);
}
if (shapeChanged && !mapChunk.SuppressCollisionRegeneration)

View File

@@ -22,7 +22,6 @@ namespace Robust.Shared.GameObjects
[Dependency] private readonly FixtureSystem _fixtures = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly IComponentFactory _factory = default!;
[Dependency] private readonly MetaDataSystem _meta = default!;
private EntityQuery<FixturesComponent> _fixturesQuery;
@@ -159,9 +158,9 @@ namespace Robust.Shared.GameObjects
/// <summary>
/// Creates a new instance of this class.
/// </summary>
public TileChangedEvent(EntityUid uid, TileRef newTile, Tile oldTile, Vector2i chunkIndex)
public TileChangedEvent(Entity<MapGridComponent> entity, TileRef newTile, Tile oldTile, Vector2i chunkIndex)
{
Entity = uid;
Entity = entity;
NewTile = newTile;
OldTile = oldTile;
ChunkIndex = chunkIndex;
@@ -173,9 +172,9 @@ namespace Robust.Shared.GameObjects
public bool EmptyChanged => OldTile.IsEmpty != NewTile.Tile.IsEmpty;
/// <summary>
/// EntityUid of the grid with the tile-change. TileRef stores the GridId.
/// Entity of the grid with the tile-change. TileRef stores the GridId.
/// </summary>
public readonly EntityUid Entity;
public readonly Entity<MapGridComponent> Entity;
/// <summary>
/// New tile that replaced the old one.

View File

@@ -279,7 +279,7 @@ public abstract class SharedUserInterfaceSystem : EntitySystem
}
}
protected virtual void OnUserInterfaceShutdown(Entity<UserInterfaceComponent> ent, ref ComponentShutdown args)
protected void OnUserInterfaceShutdown(Entity<UserInterfaceComponent> ent, ref ComponentShutdown args)
{
var ents = new ValueList<EntityUid>();
foreach (var (key, acts) in ent.Comp.Actors)
@@ -1064,11 +1064,11 @@ public abstract class SharedUserInterfaceSystem : EntitySystem
{
if (open)
{
bui.Open();
#if EXCEPTION_TOLERANCE
try
{
#endif
bui.Open();
if (UIQuery.TryComp(bui.Owner, out var uiComp))
{
@@ -1096,8 +1096,24 @@ public abstract class SharedUserInterfaceSystem : EntitySystem
uiComp.ClientOpenInterfaces.Remove(bui.UiKey);
}
SavePosition(bui);
#if EXCEPTION_TOLERANCE
try
{
#endif
if (!TerminatingOrDeleted(bui.Owner))
{
SavePosition(bui);
}
bui.Dispose();
#if EXCEPTION_TOLERANCE
}
catch (Exception e)
{
Log.Error(
$"Caught exception while attempting to dispose of a BUI {bui.UiKey} with type {bui.GetType()} on entity {ToPrettyString(bui.Owner)}. Exception: {e}");
}
#endif
}
}

View File

@@ -1,3 +1,5 @@
using Robust.Shared.GameObjects;
using Robust.Shared.Map.Components;
using Robust.Shared.Maths;
namespace Robust.Shared.Map
@@ -10,6 +12,6 @@ namespace Robust.Shared.Map
/// </summary>
/// <param name="tileRef">A reference to the new tile.</param>
/// <param name="oldTile">The old tile that got replaced.</param>
void RaiseOnTileChanged(TileRef tileRef, Tile oldTile, Vector2i chunk);
void RaiseOnTileChanged(Entity<MapGridComponent> entity, TileRef tileRef, Tile oldTile, Vector2i chunk);
}
}

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Robust.Shared.Map
@@ -7,7 +8,7 @@ namespace Robust.Shared.Map
/// <summary>
/// The definition (template) for a grid tile.
/// </summary>
public interface ITileDefinition
public interface ITileDefinition : IPrototype
{
/// <summary>
/// The numeric tile ID used to refer to this tile inside the map datastructure.

View File

@@ -96,14 +96,13 @@ internal partial class MapManager
/// </summary>
/// <param name="tileRef">A reference to the new tile.</param>
/// <param name="oldTile">The old tile that got replaced.</param>
public void RaiseOnTileChanged(TileRef tileRef, Tile oldTile, Vector2i chunk)
void IMapManagerInternal.RaiseOnTileChanged(Entity<MapGridComponent> entity, TileRef tileRef, Tile oldTile, Vector2i chunk)
{
if (SuppressOnTileChanged)
return;
var euid = tileRef.GridUid;
var ev = new TileChangedEvent(euid, tileRef, oldTile, chunk);
EntityManager.EventBus.RaiseLocalEvent(euid, ref ev, true);
var ev = new TileChangedEvent(entity, tileRef, oldTile, chunk);
EntityManager.EventBus.RaiseLocalEvent(entity.Owner, ref ev, true);
}
protected Entity<MapGridComponent> CreateGrid(EntityUid map, ushort chunkSize, EntityUid forcedGridEuid)

View File

@@ -10,7 +10,6 @@ namespace Robust.Shared.Map
[Virtual]
internal class TileDefinitionManager : ITileDefinitionManager
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
protected readonly List<ITileDefinition> TileDefs;
private readonly Dictionary<string, ITileDefinition> _tileNames;
private readonly Dictionary<string, List<string>> _awaitingAliases;

File diff suppressed because it is too large Load Diff

View File

@@ -28,18 +28,18 @@ public sealed class DynamicTreeBroadPhase : IBroadPhase
public Box2 GetFatAabb(DynamicTree.Proxy proxy)
{
return _tree.GetFatAabb(proxy);
return _tree.GetUserData(proxy)!.AABB;
}
public DynamicTree.Proxy AddProxy(ref FixtureProxy proxy)
{
var proxyId = _tree.CreateProxy(proxy.AABB, proxy);
var proxyId = _tree.CreateProxy(proxy.AABB, uint.MaxValue, proxy);
return proxyId;
}
public bool MoveProxy(DynamicTree.Proxy proxy, in Box2 aabb, Vector2 displacement)
public void MoveProxy(DynamicTree.Proxy proxy, in Box2 aabb)
{
return _tree.MoveProxy(proxy, in aabb, displacement);
_tree.MoveProxy(proxy, in aabb);
}
public void RemoveProxy(DynamicTree.Proxy proxy)
@@ -132,6 +132,16 @@ public sealed class DynamicTreeBroadPhase : IBroadPhase
state = tuple.state;
}
public void Rebuild(bool fullBuild)
{
_tree.Rebuild(fullBuild);
}
public void RebuildBottomUp()
{
_tree.RebuildBottomUp();
}
private static bool AabbQueryStateCallback<TState>(ref (TState state, B2DynamicTree<FixtureProxy> tree, DynamicTree<FixtureProxy>.QueryCallbackDelegate<TState> callback, Box2 aabb, bool approx, DynamicTree<FixtureProxy>.ExtractAabbDelegate extract) tuple, DynamicTree.Proxy proxy)
{
var item = tuple.tree.GetUserData(proxy)!;

View File

@@ -1,6 +1,5 @@
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using Robust.Shared.Maths;
using Robust.Shared.Utility;
@@ -9,21 +8,23 @@ namespace Robust.Shared.Physics;
/// <summary>
/// Convex hull used for poly collision.
/// </summary>
internal ref struct PhysicsHull()
internal ref struct InternalPhysicsHull
{
public Span<Vector2> Points;
public int Count;
public PhysicsHull(Span<Vector2> vertices, int count) : this()
internal InternalPhysicsHull(Span<Vector2> vertices, int count) : this()
{
Count = count;
Points = vertices[..count];
}
private static PhysicsHull RecurseHull(Vector2 p1, Vector2 p2, Span<Vector2> ps, int count)
private static InternalPhysicsHull RecurseHull(Vector2 p1, Vector2 p2, Span<Vector2> ps, int count)
{
PhysicsHull hull = new();
hull.Count = 0;
InternalPhysicsHull hull = new()
{
Count = 0
};
if (count == 0)
{
@@ -69,10 +70,10 @@ internal ref struct PhysicsHull()
var bestPoint = ps[bestIndex];
// compute hull to the right of p1-bestPoint
PhysicsHull hull1 = RecurseHull(p1, bestPoint, rightPoints, rightCount);
InternalPhysicsHull hull1 = RecurseHull(p1, bestPoint, rightPoints, rightCount);
// compute hull to the right of bestPoint-p2
PhysicsHull hull2 = RecurseHull(bestPoint, p2, rightPoints, rightCount);
InternalPhysicsHull hull2 = RecurseHull(bestPoint, p2, rightPoints, rightCount);
// stich together hulls
for (var i = 0; i < hull1.Count; ++i)
@@ -96,9 +97,9 @@ internal ref struct PhysicsHull()
// - merges vertices based on b2_linearSlop
// - removes collinear points using b2_linearSlop
// - returns an empty hull if it fails
public static PhysicsHull ComputeHull(ReadOnlySpan<Vector2> points, int count)
public static InternalPhysicsHull ComputeHull(ReadOnlySpan<Vector2> points, int count)
{
PhysicsHull hull = new();
InternalPhysicsHull hull = new();
if (count is < 3 or > PhysicsConstants.MaxPolygonVertices)
{
@@ -287,7 +288,7 @@ internal ref struct PhysicsHull()
return hull;
}
public static bool ValidateHull(PhysicsHull hull)
public static bool ValidateHull(InternalPhysicsHull hull)
{
if (hull.Count < 3 || PhysicsConstants.MaxPolygonVertices < hull.Count)
{

View File

@@ -76,7 +76,7 @@ namespace Robust.Shared.Physics.Collision.Shapes
{
DebugTools.Assert(count is >= 3 and <= PhysicsConstants.MaxPolygonVertices);
var hull = PhysicsHull.ComputeHull(vertices, count);
var hull = InternalPhysicsHull.ComputeHull(vertices, count);
if (hull.Count < 3)
{
@@ -87,7 +87,7 @@ namespace Robust.Shared.Physics.Collision.Shapes
return true;
}
internal void Set(PhysicsHull hull)
internal void Set(InternalPhysicsHull hull)
{
DebugTools.Assert(hull.Count >= 3);
var vertexCount = hull.Count;
@@ -119,14 +119,14 @@ namespace Robust.Shared.Physics.Collision.Shapes
if (count is < 3 or > PhysicsConstants.MaxPolygonVertices)
return false;
var hull = new PhysicsHull();
var hull = new InternalPhysicsHull();
for (var i = 0; i < count; i++)
{
hull.Points[i] = Vertices[i];
}
hull.Count = count;
return PhysicsHull.ValidateHull(hull);
return InternalPhysicsHull.ValidateHull(hull);
}
private static Vector2 ComputeCentroid(Vector2[] vs, int count)
@@ -199,7 +199,7 @@ namespace Robust.Shared.Physics.Collision.Shapes
verts[2] = bounds.TopRight;
verts[3] = bounds.TopLeft;
var hull = new PhysicsHull(verts, 4);
var hull = new InternalPhysicsHull(verts, 4);
Set(hull);
}

View File

@@ -139,7 +139,7 @@ namespace Robust.Shared.Physics
return true;
}
proxy = _b2Tree.CreateProxy(aabb.Value, item);
proxy = _b2Tree.CreateProxy(aabb.Value, uint.MaxValue, item);
_nodeLookup[item] = proxy;
return true;
@@ -199,11 +199,12 @@ namespace Robust.Shared.Physics
if (proxy == DynamicTree.Proxy.Free)
{
proxy = _b2Tree.CreateProxy(newBox.Value, item);
proxy = _b2Tree.CreateProxy(newBox.Value, uint.MaxValue, item);
return true;
}
return _b2Tree.MoveProxy(proxy, newBox.Value, Vector2.Zero);
_b2Tree.MoveProxy(proxy, newBox.Value);
return true;
}
public void QueryAabb(QueryCallbackDelegate callback, Box2 aabb, bool approx = false)
@@ -334,7 +335,7 @@ namespace Robust.Shared.Physics
ref var proxy = ref CollectionsMarshal.GetValueRefOrAddDefault(_nodeLookup, item, out var exists);
if (!exists)
{
proxy = aabb.Value.HasNan() ? DynamicTree.Proxy.Free : _b2Tree.CreateProxy(aabb.Value, item);
proxy = aabb.Value.HasNan() ? DynamicTree.Proxy.Free : _b2Tree.CreateProxy(aabb.Value, uint.MaxValue, item);
return;
}
@@ -349,9 +350,9 @@ namespace Robust.Shared.Physics
}
if (proxy == DynamicTree.Proxy.Free)
proxy = _b2Tree.CreateProxy(aabb.Value, item);
proxy = _b2Tree.CreateProxy(aabb.Value, uint.MaxValue, item);
else
_b2Tree.MoveProxy(proxy, aabb.Value, Vector2.Zero);
_b2Tree.MoveProxy(proxy, aabb.Value);
}
[Conditional("DEBUG_DYNAMIC_TREE")]

View File

@@ -16,7 +16,7 @@ public interface IBroadPhase
DynamicTree.Proxy AddProxy(ref FixtureProxy proxy);
bool MoveProxy(DynamicTree.Proxy proxyId, in Box2 aabb, Vector2 displacement);
void MoveProxy(DynamicTree.Proxy proxyId, in Box2 aabb);
FixtureProxy? GetProxy(DynamicTree.Proxy proxy);
@@ -54,6 +54,11 @@ public interface IBroadPhase
DynamicTree<FixtureProxy>.RayQueryCallbackDelegate<TState> callback,
in Ray ray,
bool approx = false);
void Rebuild(bool fullBuild);
void RebuildBottomUp();
}
public interface IBroadPhase<T> : ICollection<T> where T : notnull {

View File

@@ -4,6 +4,12 @@ namespace Robust.Shared.Physics
{
public static class PhysicsConstants
{
public const int LengthUnitsPerMetre = 1;
// Used to detect bad values. Positions greater than about 16km will have precision
// problems, so 100km as a limit should be fine in all cases.
public const float Huge = (100000.0f * LengthUnitsPerMetre);
/// <summary>
/// The radius of the polygon/edge shape skin. This should not be modified. Making
/// this smaller means polygons will have an insufficient buffer for continuous collision.
@@ -23,7 +29,7 @@ namespace Robust.Shared.Physics
/// Minimum buffer distance for angles.
/// </summary>
public const float AngularSlop = 2.0f / 180.0f * MathF.PI;
public const byte MaxPolygonVertices = 8;
public const float DefaultContactFriction = 0.4f;

View File

@@ -0,0 +1,13 @@
using System;
using System.Numerics;
namespace Robust.Shared.Physics;
public struct PhysicsHull
{
public static Span<Vector2> ComputePoints(ReadOnlySpan<Vector2> points, int count)
{
var hull = InternalPhysicsHull.ComputeHull(points, count);
return hull.Points;
}
}

View File

@@ -99,7 +99,7 @@ internal record struct Polygon : IPhysShape
public Polygon(Vector2[] vertices)
{
Unsafe.SkipInit(out this);
var hull = PhysicsHull.ComputeHull(vertices, vertices.Length);
var hull = InternalPhysicsHull.ComputeHull(vertices, vertices.Length);
if (hull.Count < 3)
{
@@ -121,7 +121,7 @@ internal record struct Polygon : IPhysShape
return new Polygon(polyShape);
}
private void Set(PhysicsHull hull)
private void Set(InternalPhysicsHull hull)
{
DebugTools.Assert(hull.Count >= 3);
var vertexCount = hull.Count;

View File

@@ -77,6 +77,22 @@ namespace Robust.Shared.Physics.Systems
_broadphaseExpand = value;
}
public void Rebuild(BroadphaseComponent component, bool fullBuild)
{
component.StaticTree.Rebuild(fullBuild);
component.DynamicTree.Rebuild(fullBuild);
component.SundriesTree._b2Tree.Rebuild(fullBuild);
component.StaticSundriesTree._b2Tree.Rebuild(fullBuild);
}
public void RebuildBottomUp(BroadphaseComponent component)
{
component.StaticTree.RebuildBottomUp();
component.DynamicTree.RebuildBottomUp();
component.SundriesTree._b2Tree.RebuildBottomUp();
component.StaticSundriesTree._b2Tree.RebuildBottomUp();
}
#region Find Contacts
/// <summary>

View File

@@ -618,6 +618,24 @@ public abstract partial class SharedPhysicsSystem
ArrayPool<Vector2>.Shared.Return(worldPoints);
}
private record struct UpdateTreesJob : IRobustJob
{
public IEntityManager EntManager;
public void Execute()
{
var query = EntManager.AllEntityQueryEnumerator<BroadphaseComponent>();
while (query.MoveNext(out var broadphase))
{
broadphase.DynamicTree.Rebuild(false);
broadphase.StaticTree.Rebuild(false);
broadphase.SundriesTree._b2Tree.Rebuild(false);
broadphase.StaticSundriesTree._b2Tree.Rebuild(false);
}
}
}
private void BuildManifolds(Contact[] contacts, int count, ContactStatus[] status, Vector2[] worldPoints)
{
if (count == 0)

View File

@@ -35,7 +35,6 @@ namespace Robust.Shared.Physics.Systems
public bool TryCollideRect(Box2 collider, MapId mapId, bool approximate = true)
{
var state = (collider, mapId, found: false);
var broadphases = new ValueList<Entity<BroadphaseComponent>>();
_broadphase.GetBroadphases(mapId,
collider,

View File

@@ -8,6 +8,7 @@ using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Robust.Shared.Asynchronous;
using Robust.Shared.Collections;
using Robust.Shared.ContentPack;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
@@ -352,6 +353,29 @@ namespace Robust.Shared.Serialization.Markdown.Mapping
return mappingNode._children.Count == 0 ? null : mappingNode;
}
/// <summary>
/// Returns true if there are any nodes on this node that aren't in the other node.
/// </summary>
[Pure]
public bool AnyExcept(MappingDataNode node)
{
foreach (var (key, val) in _list)
{
var other = node._list.FirstOrNull(p => p.Key.Equals(key));
if (other == null)
{
return true;
}
// We only keep the entry if the values are not equal
if (!val.Equals(other.Value.Value))
return true;
}
return false;
}
public override bool Equals(object? obj)
{
if (obj is not MappingDataNode other)

View File

@@ -26,6 +26,8 @@ namespace Robust.Shared.Utility
_count = 0;
}
internal ref T this[int index] => ref _stack[index];
public void Push(in T element)
{
if (_count == _capacity)

View File

@@ -0,0 +1,22 @@
using System;
namespace Robust.Shared.Utility;
/// <summary>
/// Handles hot-reloading modified resource files such as prototypes or shaders.
/// </summary>
internal interface IReloadManager
{
/// <summary>
/// File that has been modified.
/// </summary>
public event Action<ResPath>? OnChanged;
/// <summary>
/// Registers the specified directory and specified file extension to subscribe to.
/// </summary>
internal void Register(string directory, string filter);
void Initialize();
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
using System.Text.Json.Serialization;
using JetBrains.Annotations;
@@ -64,6 +65,8 @@ public readonly struct ResPath : IEquatable<ResPath>
public ResPath(string canonPath)
{
// Paths should never have non-standardised directory separators passed in, the caller should have already sanitised it.
DebugTools.Assert(!canonPath.Contains('\\'));
CanonPath = canonPath;
}

View File

@@ -4,6 +4,7 @@ using NUnit.Framework;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
@@ -44,10 +45,13 @@ public sealed partial class EntitySaveTestComponent : Component
/// <summary>
/// Dummy tile definition for serializing grids.
/// </summary>
[Prototype("testTileDef")]
public sealed class TileDef(string id) : ITileDefinition
{
public ushort TileId { get; set; }
public string Name => id;
[IdDataField]
public string ID => id;
public ResPath? Sprite => null;
public Dictionary<Direction, ResPath> EdgeSprites => new();

View File

@@ -112,6 +112,46 @@ public sealed partial class EntityManagerCopyTests
Assert.That(!ReferenceEquals(comp2, targetComp2));
}
[Test]
public void CopyComponentMultipleViaTry()
{
var instant = RobustServerSimulation.NewSimulation();
instant.RegisterComponents(fac =>
{
fac.RegisterClass<AComponent>();
fac.RegisterClass<BComponent>();
});
var sim = instant.InitializeInstance();
var entManager = sim.Resolve<IEntityManager>();
var mapSystem = entManager.System<SharedMapSystem>();
mapSystem.CreateMap(out var mapId);
var original = entManager.Spawn(null, new MapCoordinates(Vector2.Zero, mapId));
var comp = entManager.AddComponent<AComponent>(original);
var comp2 = entManager.AddComponent<BComponent>(original);
Assert.That(comp.Value, Is.EqualTo(false));
comp.Value = true;
var target = entManager.Spawn(null, new MapCoordinates(Vector2.Zero, mapId));
Assert.That(!entManager.HasComponent<AComponent>(target));
entManager.TryCopyComponents(original, target, null, comp.GetType(), comp2.GetType());
var targetComp = entManager.GetComponent<AComponent>(target);
var targetComp2 = entManager.GetComponent<BComponent>(target);
Assert.That(targetComp!.Owner == target);
Assert.That(targetComp.Value, Is.EqualTo(comp.Value));
Assert.That(targetComp2!.Owner == target);
Assert.That(targetComp2.Value, Is.EqualTo(comp2.Value));
Assert.That(!ReferenceEquals(comp, targetComp));
Assert.That(!ReferenceEquals(comp2, targetComp2));
}
[DataDefinition]
private sealed partial class AComponent : Component
{

View File

@@ -40,7 +40,7 @@ namespace Robust.UnitTesting.Shared.Physics
for (var i = 0; i < aabbs1.Length; ++i)
{
dt.CreateProxy(aabbs1[i], i);
dt.CreateProxy(aabbs1[i], uint.MaxValue, i);
}
var point = new Vector2(0, 0);

View File

@@ -35,7 +35,7 @@ internal sealed class PhysicsHull_Test
[Test, TestCaseSource(nameof(CollinearHulls))]
public void CollinearTest(Vector2[] vertices, int count)
{
var hull = PhysicsHull.ComputeHull(vertices.AsSpan(), vertices.Length);
var hull = InternalPhysicsHull.ComputeHull(vertices.AsSpan(), vertices.Length);
Assert.That(hull.Count, Is.EqualTo(count));
}
@@ -89,7 +89,7 @@ internal sealed class PhysicsHull_Test
[Test, TestCaseSource(nameof(ValidateHulls))]
public void ValidationTest(Vector2[] vertices, bool result)
{
var hull = new PhysicsHull(vertices.AsSpan(), vertices.Length);
Assert.That(PhysicsHull.ValidateHull(hull), Is.EqualTo(result));
var hull = new InternalPhysicsHull(vertices.AsSpan(), vertices.Length);
Assert.That(InternalPhysicsHull.ValidateHull(hull), Is.EqualTo(result));
}
}

View File

@@ -57,11 +57,12 @@ public sealed class PhysicsTestBedTest : RobustIntegrationTest
var fixtureSystem = entitySystemManager.GetEntitySystem<FixtureSystem>();
var physSystem = entitySystemManager.GetEntitySystem<SharedPhysicsSystem>();
var gravSystem = entitySystemManager.GetEntitySystem<Gravity2DController>();
var transformSystem = entitySystemManager.GetEntitySystem<SharedTransformSystem>();
MapId mapId;
const int columnCount = 1;
const int rowCount = 15;
PhysicsComponent[] bodies = new PhysicsComponent[columnCount * rowCount];
Entity<PhysicsComponent>[] bodies = new Entity<PhysicsComponent>[columnCount * rowCount];
Vector2 firstPos = Vector2.Zero;
await server.WaitPost(() =>
@@ -110,12 +111,12 @@ public sealed class PhysicsTestBedTest : RobustIntegrationTest
fixtureSystem.CreateFixture(boxUid, "fix1", new Fixture(poly, 1, 1, true), manager: manager, body: box);
physSystem.WakeBody(boxUid, manager: manager, body: box);
bodies[j * rowCount + i] = box;
bodies[j * rowCount + i] = (boxUid, box);
}
}
var bodyOne = bodies[0].Owner;
firstPos = entityManager.GetComponent<TransformComponent>(bodyOne).WorldPosition;
firstPos = transformSystem.GetWorldPosition(bodyOne);
});
await server.WaitRunTicks(1);
@@ -124,7 +125,7 @@ public sealed class PhysicsTestBedTest : RobustIntegrationTest
await server.WaitAssertion(() =>
{
var tempQualifier = bodies[0].Owner;
Assert.That(firstPos, Is.Not.EqualTo(entityManager.GetComponent<TransformComponent>(tempQualifier).WorldPosition));
Assert.That(firstPos, Is.Not.EqualTo(transformSystem.GetWorldPosition(tempQualifier)));
});
// Assert
@@ -139,12 +140,12 @@ public sealed class PhysicsTestBedTest : RobustIntegrationTest
for (var i = 0; i < bodies.Length; i++)
{
var body = bodies[j * columnCount + i];
var worldPos = entityManager.GetComponent<TransformComponent>(body.Owner).WorldPosition;
var worldPos = transformSystem.GetWorldPosition(body);
// TODO: Multi-column support but I cbf right now
// Can't be more exact as some level of sinking is allowed.
Assert.That(worldPos.EqualsApprox(new Vector2(0.0f, i + 0.5f), 0.2f), $"Expected y-value of {i + 0.5f} but found {worldPos.Y}");
Assert.That(!body.Awake, $"Body {i} wasn't asleep");
Assert.That(!body.Comp.Awake, $"Body {i} wasn't asleep");
}
}
});
@@ -162,11 +163,12 @@ public sealed class PhysicsTestBedTest : RobustIntegrationTest
var fixtureSystem = entitySystemManager.GetEntitySystem<FixtureSystem>();
var physSystem = entitySystemManager.GetEntitySystem<SharedPhysicsSystem>();
var gravSystem = entitySystemManager.GetEntitySystem<Gravity2DController>();
var transformSystem = entitySystemManager.GetEntitySystem<SharedTransformSystem>();
MapId mapId;
var columnCount = 1;
var rowCount = 15;
PhysicsComponent[] bodies = new PhysicsComponent[columnCount * rowCount];
Entity<PhysicsComponent>[] bodies = new Entity<PhysicsComponent>[columnCount * rowCount];
Vector2 firstPos = Vector2.Zero;
await server.WaitPost(() =>
@@ -210,12 +212,12 @@ public sealed class PhysicsTestBedTest : RobustIntegrationTest
fixtureSystem.CreateFixture(circleUid, "fix1", new Fixture(shape, 1, 1, true), manager: manager, body: circle);
physSystem.WakeBody(circleUid, manager: manager, body: circle);
bodies[j * rowCount + i] = circle;
bodies[j * rowCount + i] = (circleUid, circle);
}
}
EntityUid tempQualifier3 = bodies[0].Owner;
firstPos = entityManager.GetComponent<TransformComponent>(tempQualifier3).WorldPosition;
firstPos = transformSystem.GetWorldPosition(tempQualifier3);
});
await server.WaitRunTicks(1);
@@ -224,7 +226,7 @@ public sealed class PhysicsTestBedTest : RobustIntegrationTest
await server.WaitAssertion(() =>
{
EntityUid tempQualifier = bodies[0].Owner;
Assert.That(firstPos, Is.Not.EqualTo(entityManager.GetComponent<TransformComponent>(tempQualifier).WorldPosition));
Assert.That(firstPos, Is.Not.EqualTo(transformSystem.GetWorldPosition(tempQualifier)));
});
// Assert
@@ -239,14 +241,14 @@ public sealed class PhysicsTestBedTest : RobustIntegrationTest
for (var i = 0; i < bodies.Length; i++)
{
var body = bodies[j * columnCount + i];
var worldPos = entityManager.GetComponent<TransformComponent>(body.Owner).WorldPosition;
var worldPos = transformSystem.GetWorldPosition(body);
var expectedY = 0.5f + i;
// TODO: Multi-column support but I cbf right now
// Can't be more exact as some level of sinking is allowed.
Assert.That(worldPos.EqualsApproxPercent(new Vector2(0.0f, expectedY), 0.1f), $"Expected y-value of {expectedY} but found {worldPos.Y}");
Assert.That(!body.Awake);
Assert.That(!body.Comp.Awake);
}
}
});