mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-08-01 00:08:05 -04:00
ADDED css min(), max() and clamp() for lengths css-wide keywords inherit, initial, unset, revert (incl. shorthands) added currentColor keyword to css colors can parse oklch(), lab() and hwb() added css text-align: justify css white-space: pre-wrap and break-spaces added css word-break: break-word parses css image-rendering: crisp-edges (point sampling) added css inset shorthand now parses 1–4 values in the css border-width shorthand added css transition-duration, -delay, -property and -timing-function longhands css :has() now supports descendant selectors added css opacity percentages added css overflow: auto (maps to scroll) added css gap: normal added css filter: none css transform: scale() now takes comma-separated args css animations now accept ms units added more css justify-content, align-* and text-align keywords (start, end, …) parses css dvh/svh/lvh/dvw viewport units (treated as vh/vw) parses css object-fit: scale-down (treated as contain) added css margin-block / margin-inline (+ the logical margin sides) added css padding-block / padding-inline (+ the logical padding sides) added css inset-block / inset-inline (+ the logical inset sides) added the css flex-flow shorthand added the css font shorthand added css font-size keywords (xx-small … xxx-large) css letter-spacing and word-spacing now accept normal added css font-smooth: none css aspect-ratio now accepts the auto form css font-family now maps generic families (serif, sans-serif, monospace) added css flex-flow added css font FIXED fixed css !important dropping the whole declaration css line-height without units is now a multiplier fixed css calc() division and full-expression parsing fixed the css flex shorthand for single-number and three-value forms fixed the css transition shorthand when the property is omitted css font-family now uses the first family in a comma stack colors now parse fully in the css background shorthand an unknown css @-rule no longer drops the whole stylesheet css variables no longer match on partial tokens injected css variables are kept across stylesheet hotloads fixed css selector specificity overflowing when comparing rules css background: none now resets the background (image and colour) css filter: none now overrides a filter set by a base class css transform: none now overrides a transform set by a base class IMPROVED css rule matching is ~3–5× faster on large stylesheets (rules indexed by class) ~2× fewer allocations in css transitions (snapshot styles once, not per property) finished css animations no longer re-layout every frame less GC in css sibling/child selector queries (no list copies or wrappers) less GC diffing active css rules (no LINQ or hashsets) faster, lower-GC Yoga layout wrapper css stylesheet hotload now watches newly imported files
202 lines
4.3 KiB
C#
202 lines
4.3 KiB
C#
using System.Text.RegularExpressions;
|
|
|
|
namespace Sandbox;
|
|
|
|
/// <summary>
|
|
/// Watch folders, dispatch events on changed files
|
|
/// </summary>
|
|
[SkipHotload]
|
|
public sealed class FileWatch : IDisposable
|
|
{
|
|
/// <summary>
|
|
/// Bit of a hack until we can do better. Don't trigger any watchers until this time.
|
|
/// </summary>
|
|
internal static float SuppressWatchers { get; set; }
|
|
|
|
private static Logger log = new Logger( "FileWatch" );
|
|
internal static List<BaseFileSystem> WithChanges = new List<BaseFileSystem>();
|
|
|
|
BaseFileSystem system;
|
|
|
|
public bool Enabled { get; set; }
|
|
public List<string> Changes { get; private set; }
|
|
|
|
private Regex regexTest;
|
|
public List<string> watchFiles;
|
|
|
|
|
|
internal FileWatch( BaseFileSystem system, string path )
|
|
{
|
|
this.system = system;
|
|
Enabled = true;
|
|
|
|
var pattern = Regex.Escape( path.ToLower() ).Replace( @"\*", ".*" ).Replace( @"\?", "." );
|
|
regexTest = new Regex( $"^{pattern}$", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled );
|
|
}
|
|
|
|
internal FileWatch( BaseFileSystem system )
|
|
{
|
|
this.system = system;
|
|
Enabled = true;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
OnChanges = default;
|
|
OnChangedFile = default;
|
|
|
|
system?.RemoveWatcher( this );
|
|
system = null;
|
|
}
|
|
|
|
private bool InterestedInFile( string file )
|
|
{
|
|
// watchers
|
|
if ( watchFiles != null && !watchFiles.Any( x => string.Equals( file, x, StringComparison.OrdinalIgnoreCase ) ) )
|
|
return false;
|
|
|
|
// Regex test
|
|
if ( regexTest != null && !regexTest.IsMatch( file ) )
|
|
return false;
|
|
|
|
// Default is interested
|
|
return true;
|
|
}
|
|
|
|
void TriggerCallback( List<string> value )
|
|
{
|
|
if ( !Enabled ) return;
|
|
|
|
if ( Changes == null )
|
|
Changes = new List<string>();
|
|
|
|
Changes.Clear();
|
|
|
|
foreach ( var change in value )
|
|
{
|
|
if ( !InterestedInFile( change ) ) continue;
|
|
|
|
Changes.Add( change );
|
|
}
|
|
|
|
if ( Changes.Count == 0 )
|
|
return;
|
|
|
|
try
|
|
{
|
|
//log.Trace( $"FileWatch.TriggerCallback ({Path})" );
|
|
OnChanges?.Invoke( this );
|
|
|
|
foreach ( var change in Changes )
|
|
{
|
|
OnChangedFile?.Invoke( change );
|
|
}
|
|
}
|
|
catch ( System.Exception e )
|
|
{
|
|
log.Error( e );
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Called once per batch of files changed
|
|
/// </summary>
|
|
public event Action<FileWatch> OnChanges;
|
|
|
|
/// <summary>
|
|
/// Called for each file changed
|
|
/// </summary>
|
|
public event Action<string> OnChangedFile;
|
|
internal static RealTimeSince TimeSinceLastChange;
|
|
|
|
/// <summary>
|
|
/// This is used for unit tests, to assure that a change is detected
|
|
/// </summary>
|
|
internal static async Task<bool> TickUntilFileChanged( string wildcard )
|
|
{
|
|
var sw = Stopwatch.StartNew();
|
|
while ( sw.Elapsed.TotalSeconds < 2 )
|
|
{
|
|
await Task.Delay( 10 );
|
|
|
|
lock ( WithChanges )
|
|
{
|
|
if ( WithChanges.Count > 0 )
|
|
{
|
|
Log.Info( string.Join( "\n", WithChanges.SelectMany( x => x.changedFiles ) ) );
|
|
|
|
if ( WithChanges.Any( x => x.changedFiles.Any( x => x.WildcardMatch( wildcard ) ) ) )
|
|
{
|
|
// do the real tick to send the messages
|
|
TimeSinceLastChange = 1;
|
|
Tick();
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
// TODO - move this into BaseFileSystem
|
|
public static void Tick()
|
|
{
|
|
Dictionary<BaseFileSystem, List<string>> changes = null;
|
|
|
|
if ( TimeSinceLastChange < 0.1f ) return;
|
|
|
|
// Don't lock and loop WithChanges
|
|
// incase a callback triggers a changed file
|
|
// and we end up deadlocked
|
|
lock ( WithChanges )
|
|
{
|
|
//
|
|
// Hack, we sometimes want to suppress this hotload for a number of seconds
|
|
// This blanket suppression is maybe not the best way, could do it via wildcards or something
|
|
//
|
|
if ( SuppressWatchers > RealTime.Now )
|
|
{
|
|
WithChanges.Clear();
|
|
return;
|
|
}
|
|
|
|
|
|
if ( WithChanges.Count == 0 ) return;
|
|
|
|
WithChanges.RemoveAll( x => !x.IsValid || x.changedFiles == null );
|
|
|
|
changes = WithChanges.ToDictionary( x => x, x => x.changedFiles.ToList() );
|
|
|
|
foreach ( var fs in WithChanges )
|
|
{
|
|
fs.changedFiles.Clear();
|
|
}
|
|
|
|
WithChanges.Clear();
|
|
}
|
|
|
|
if ( changes == null )
|
|
return;
|
|
|
|
foreach ( var filesystem in changes )
|
|
{
|
|
foreach ( var watcher in filesystem.Key.watchers.ToArray() )
|
|
{
|
|
watcher.TriggerCallback( filesystem.Value );
|
|
|
|
if ( filesystem.Key.PendingDispose )
|
|
filesystem.Key.Dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
internal void AddFile( string file )
|
|
{
|
|
watchFiles ??= new List<string>();
|
|
if ( !watchFiles.Contains( file ) )
|
|
watchFiles.Add( file );
|
|
}
|
|
}
|