Skip to content

Commit

Permalink
compiler fix
Browse files Browse the repository at this point in the history
  • Loading branch information
BBpezsgo committed Jan 11, 2025
1 parent 5183dd7 commit 7599db8
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 59 deletions.
3 changes: 1 addition & 2 deletions Assets/Mono/Client/TerminalManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,13 @@ static void AppendProgressBar(StringBuilder builder, string label, float progres
{
EntityManager entityManager = ConnectionManager.ClientOrDefaultWorld.EntityManager;
Processor processor = entityManager.GetComponentData<Processor>(unitEntity);
if (processor.SourceFile == default)
if (processor.SourceFile == default || !ConnectionManager.ClientOrDefaultWorld.GetExistingSystemManaged<CompilerSystemClient>().CompiledSources.TryGetValue(processor.SourceFile, out CompiledSource? source))
{
_terminal = null;
_terminalBuilder.AppendLine("<color=red>No source</color>");
}
else
{
CompiledSource source = CompilerManager.Instance.CompiledSources[processor.SourceFile];
const string SpinnerChars = "-\\|/";
char spinner = SpinnerChars[(int)(MonoTime.Now * 8f) % SpinnerChars.Length];

Expand Down
1 change: 1 addition & 0 deletions Assets/RPC/CompilationAnalysticsRpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[BurstCompile]
public struct CompilationAnalysticsRpc : IRpcCommand
{
public FileId Source;
public FileId FileName;
public MutableRange<SinglePosition> Position;
public MutableRange<int> AbsolutePosition;
Expand Down
15 changes: 9 additions & 6 deletions Assets/Systems/Client/CompilerSystemClient.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System.Diagnostics.CodeAnalysis;
using Unity.Collections;
using Unity.Entities;
using Unity.NetCode;

[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
partial struct CompilerSystemClient : ISystem
partial class CompilerSystemClient : SystemBase
{
void ISystem.OnUpdate(ref SystemState state)
[NotNull] public readonly SerializableDictionary<FileId, CompiledSource>? CompiledSources = new();

protected override void OnUpdate()
{
EntityCommandBuffer commandBuffer = default;

Expand All @@ -19,7 +22,7 @@ void ISystem.OnUpdate(ref SystemState state)

// Debug.Log($"Received compilation status for {command.ValueRO.FileName}");

CompilerManager.Instance.HandleRpc(command.ValueRO);
CompiledSources[command.ValueRO.FileName] = CompiledSource.FromRpc(command.ValueRO);
}

foreach (var (command, entity) in
Expand All @@ -30,9 +33,9 @@ void ISystem.OnUpdate(ref SystemState state)
if (!commandBuffer.IsCreated) commandBuffer = new(Allocator.Temp);
commandBuffer.DestroyEntity(entity);

if (!CompilerManager.Instance.CompiledSources.TryGetValue(command.ValueRO.FileName, out CompiledSource source))
if (!CompiledSources.TryGetValue(command.ValueRO.Source, out CompiledSource source))
{
// Debug.LogWarning($"Received analytics for unknown compiled source \"{command.ValueRO.FileName}\"");
Debug.LogWarning($"Received analytics for unknown compiled source \"{command.ValueRO.FileName}\"");
continue;
}

Expand All @@ -47,7 +50,7 @@ void ISystem.OnUpdate(ref SystemState state)

if (commandBuffer.IsCreated)
{
commandBuffer.Playback(state.EntityManager);
commandBuffer.Playback(EntityManager);
commandBuffer.Dispose();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#pragma warning disable CS0162 // Unreachable code detected

using System;
using System.Collections.Frozen;
using System.Collections.Generic;
Expand Down Expand Up @@ -39,7 +37,7 @@ public class CompiledSource : IInspect<CompiledSource>

public float Progress;
public bool StatusChanged;
public float LastStatusSync;
public double LastStatusSync;

public bool IsSuccess;

Expand Down Expand Up @@ -90,7 +88,7 @@ public class CompiledSource : IInspect<CompiledSource>
rpc.FileName,
rpc.CompiledVersion,
rpc.LatestVersion,
CompilationStatus.None,
rpc.Status,
default,
rpc.Progress,
rpc.IsSuccess,
Expand All @@ -116,24 +114,9 @@ public CompiledSource OnGUI(Rect rect, CompiledSource value)
public class _Drawer1 : DictionaryDrawer<FileId, CompiledSource> { }
#endif

public class CompilerManager : Singleton<CompilerManager>
public partial class CompilerSystemServer : SystemBase
{
const bool EnableLogging = false;

[SerializeField, NotNull] SerializableDictionary<FileId, CompiledSource>? _compiledSources = default;

public IReadOnlyDictionary<FileId, CompiledSource> CompiledSources => _compiledSources;

public void AddEmpty(FileId file, long latestVersion)
{
_compiledSources.Add(file, CompiledSource.Empty(file, latestVersion));
}

public void HandleRpc(CompilerStatusRpc rpc)
{
if (World.DefaultGameObjectInjectionWorld.IsServer()) return;
_compiledSources[rpc.FileName] = CompiledSource.FromRpc(rpc);
}
static readonly bool EnableLogging = false;

static readonly FrozenDictionary<int, string> ExternalFunctionNames = new Dictionary<int, string>()
{
Expand Down Expand Up @@ -164,16 +147,13 @@ public void HandleRpc(CompilerStatusRpc rpc)
{ 51, "dequeue_command" },
}.ToFrozenDictionary();

void Start()
{
_compiledSources = new();
}
[NotNull] public readonly SerializableDictionary<FileId, CompiledSource>? CompiledSources = new();

void FixedUpdate()
protected override void OnUpdate()
{
EntityCommandBuffer commandBuffer = default;

foreach ((FileId file, CompiledSource source) in _compiledSources)
foreach ((FileId file, CompiledSource source) in CompiledSources)
{
switch (source.Status)
{
Expand All @@ -185,13 +165,13 @@ void FixedUpdate()
source.CompileSecuedued = default;
Task.Factory.StartNew(static (object state)
// TODO: recompiling
=> CompileSourceTask(((FileId, bool))state), (object)(file, false))
=> CompileSourceTask(((FileId, bool, CompiledSource))state), (object)(file, false, CompiledSources[file]))
.ContinueWith(task =>
{
if (task.IsFaulted)
{ Debug.LogException(task.Exception); }
else if (task.IsCanceled)
{ Debug.LogError($"[{nameof(CompilerManager)}]: Compilation task cancelled"); }
{ Debug.LogError($"[{nameof(CompilerSystemServer)}]: Compilation task cancelled"); }
});
if (!commandBuffer.IsCreated) commandBuffer = new(Allocator.Temp);
SendCompilationStatus(source, commandBuffer);
Expand All @@ -207,15 +187,15 @@ void FixedUpdate()
case CompilationStatus.Done:
if (source.CompiledVersion < source.LatestVersion)
{
Debug.Log($"[{nameof(CompilerManager)}]: Source version changed ({source.CompiledVersion} -> {source.LatestVersion}), recompiling \"{source.SourceFile}\"");
Debug.Log($"[{nameof(CompilerSystemServer)}]: Source version changed ({source.CompiledVersion} -> {source.LatestVersion}), recompiling \"{source.SourceFile}\"");
source.Status = CompilationStatus.Secuedued;
source.CompileSecuedued = 1f;
if (!commandBuffer.IsCreated) commandBuffer = new(Allocator.Temp);
SendCompilationStatus(source, commandBuffer);
}
break;
}
if (source.StatusChanged && source.LastStatusSync + 0.5f < Time.time)
if (source.StatusChanged && source.LastStatusSync + 0.5d < SystemAPI.Time.ElapsedTime)
{
if (!commandBuffer.IsCreated) commandBuffer = new(Allocator.Temp);
SendCompilationStatus(source, commandBuffer);
Expand All @@ -229,9 +209,9 @@ void FixedUpdate()
}
}

public static void SendCompilationStatus(CompiledSource source, EntityCommandBuffer commandBuffer)
void SendCompilationStatus(CompiledSource source, EntityCommandBuffer commandBuffer)
{
source.LastStatusSync = Time.time;
source.LastStatusSync = SystemAPI.Time.ElapsedTime;
source.StatusChanged = false;
{
Entity request = commandBuffer.CreateEntity();
Expand All @@ -248,20 +228,21 @@ public static void SendCompilationStatus(CompiledSource source, EntityCommandBuf
{
TargetConnection = source.SourceFile.Source.GetEntity(),
});
if (EnableLogging) Debug.Log($"[{nameof(CompilerManager)}]: Sending compilation status for {source.SourceFile} to {source.SourceFile.Source}");
if (EnableLogging) Debug.Log($"[{nameof(CompilerSystemServer)}]: Sending compilation status for {source.SourceFile} to {source.SourceFile.Source}");
}

foreach (Diagnostic item in source.Diagnostics.Diagnostics.ToArray())
{
if (item.Level == DiagnosticsLevel.Error) Debug.LogWarning($"[{nameof(CompilerManager)}]: {item}\r\n{item.GetArrows()}");
// if (item.Level == DiagnosticsLevel.Warning) Debug.Log($"[{nameof(CompilerManager)}]: {item}\r\n{item.GetArrows()}");
if (item.Level == DiagnosticsLevel.Error) Debug.LogWarning($"[{nameof(CompilerSystemServer)}]: {item}\r\n{item.GetArrows()}");
// if (item.Level == DiagnosticsLevel.Warning) Debug.Log($"[{nameof(CompilerSystemServer)}]: {item}\r\n{item.GetArrows()}");

if (item.File is null) continue;
if (!item.File.TryGetNetcode(out FileId file)) continue;

Entity request = commandBuffer.CreateEntity();
commandBuffer.AddComponent(request, new CompilationAnalysticsRpc()
{
Source = source.SourceFile,
FileName = file,
Position = item.Position.Range.ToMutable(),
AbsolutePosition = item.Position.AbsoluteRange.ToMutable(),
Expand All @@ -276,12 +257,13 @@ public static void SendCompilationStatus(CompiledSource source, EntityCommandBuf

foreach (DiagnosticWithoutContext item in source.Diagnostics.DiagnosticsWithoutContext.ToArray())
{
if (item.Level == DiagnosticsLevel.Error) Debug.LogWarning($"[{nameof(CompilerManager)}]: {item}");
// if (item.Level == DiagnosticsLevel.Warning) Debug.Log($"[{nameof(CompilerManager)}]: {item}");
if (item.Level == DiagnosticsLevel.Error) Debug.LogWarning($"[{nameof(CompilerSystemServer)}]: {item}");
// if (item.Level == DiagnosticsLevel.Warning) Debug.Log($"[{nameof(CompilerSystemServer)}]: {item}");

Entity request = commandBuffer.CreateEntity();
commandBuffer.AddComponent<CompilationAnalysticsRpc>(request, new()
{
Source = source.SourceFile,
Level = item.Level,
Message = item.Message,
});
Expand All @@ -292,17 +274,15 @@ public static void SendCompilationStatus(CompiledSource source, EntityCommandBuf
}
}

public static unsafe void CompileSourceTask((FileId File, bool Force) args)
public static unsafe void CompileSourceTask((FileId File, bool Force, CompiledSource source) args)
{
(FileId file, bool force) = args;
(FileId file, bool force, CompiledSource source) = args;

Uri sourceUri = file.ToUri();
if (EnableLogging) Debug.Log($"[{nameof(CompilerManager)}]: Compilation started for \"{sourceUri}\" ...");
if (EnableLogging) Debug.Log($"[{nameof(CompilerSystemServer)}]: Compilation started for \"{sourceUri}\" ...");

bool sourcesFromOtherConnectionsNeeded = false;

CompiledSource source = Instance.CompiledSources[file];

source.IsSuccess = false;
source.Diagnostics = new DiagnosticsCollection();
source.Status = CompilationStatus.Compiling;
Expand All @@ -314,7 +294,7 @@ public static unsafe void CompileSourceTask((FileId File, bool Force) args)
{
if (!uri.TryGetNetcode(out FileId fileId))
{
Debug.LogError($"[{nameof(CompilerManager)}]: Uri \"{uri}\" aint a netcode uri");
Debug.LogError($"[{nameof(CompilerSystemServer)}]: Uri \"{uri}\" aint a netcode uri");
return null;
}

Expand All @@ -340,7 +320,7 @@ public static unsafe void CompileSourceTask((FileId File, bool Force) args)

if (status == FileStatus.NotFound)
{
Debug.LogError($"[{nameof(CompilerManager)}]: Remote file \"{uri}\" not found");
Debug.LogError($"[{nameof(CompilerSystemServer)}]: Remote file \"{uri}\" not found");
return null;
}

Expand All @@ -352,7 +332,7 @@ public static unsafe void CompileSourceTask((FileId File, bool Force) args)
source.StatusChanged = true;
});
progresses.Add(progress);
if (EnableLogging) Debug.Log($"[{nameof(CompilerManager)}]: Source needs file \"{fileId}\" ...");
if (EnableLogging) Debug.Log($"[{nameof(CompilerSystemServer)}]: Source needs file \"{fileId}\" ...");

{
AwaitableCompletionSource<Stream?> result = new();
Expand All @@ -363,7 +343,7 @@ public static unsafe void CompileSourceTask((FileId File, bool Force) args)
{
MemoryStream data = new(task.GetAwaiter().GetResult().File.Data);
result.SetResult(data);
if (EnableLogging) Debug.Log($"[{nameof(CompilerManager)}]: Source \"{fileId}\" downloaded ...");
if (EnableLogging) Debug.Log($"[{nameof(CompilerSystemServer)}]: Source \"{fileId}\" downloaded ...");
if (source.Status == CompilationStatus.Secuedued &&
source.CompileSecuedued != 1f)
{ source.CompileSecuedued = 1f; }
Expand Down Expand Up @@ -523,4 +503,9 @@ public static unsafe void CompileSourceTask((FileId File, bool Force) args)
source.Progress = float.NaN;
}
}

public void AddEmpty(FileId file, long latestVersion)
{
CompiledSources.Add(file, CompiledSource.Empty(file, latestVersion));
}
}
9 changes: 5 additions & 4 deletions Assets/Systems/Server/ProcessorSourceSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ unsafe partial struct ProcessorSourceSystem : ISystem
void ISystem.OnUpdate(ref SystemState state)
{
EntityCommandBuffer commandBuffer = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(state.WorldUnmanaged);
var compilerSystem = state.World.GetExistingSystemManaged<CompilerSystemServer>();

foreach (var (request, command, entity) in
SystemAPI.Query<RefRO<ReceiveRpcCommandRequest>, RefRO<ProcessorCommandRequestRpc>>()
Expand Down Expand Up @@ -68,7 +69,7 @@ void ISystem.OnUpdate(ref SystemState state)

processor.ValueRW.SourceFile = new FileId(command.ValueRO.Source, ep);

if (CompilerManager.Instance.CompiledSources.TryGetValue(processor.ValueRO.SourceFile, out CompiledSource? source))
if (compilerSystem.CompiledSources.TryGetValue(processor.ValueRO.SourceFile, out CompiledSource? source))
{
if (EnableLogging)
{
Expand All @@ -82,7 +83,7 @@ void ISystem.OnUpdate(ref SystemState state)
else
{
if (EnableLogging) Debug.Log(string.Format("Creating new source file {0}", command.ValueRO.Source));
CompilerManager.Instance.AddEmpty(processor.ValueRO.SourceFile, command.ValueRO.Version);
compilerSystem.AddEmpty(processor.ValueRO.SourceFile, command.ValueRO.Version);
}

break;
Expand All @@ -98,10 +99,10 @@ void ISystem.OnUpdate(ref SystemState state)
continue;
}

if (!CompilerManager.Instance.CompiledSources.TryGetValue(processor.ValueRO.SourceFile, out CompiledSource? source))
if (!compilerSystem.CompiledSources.TryGetValue(processor.ValueRO.SourceFile, out CompiledSource? source))
{
if (EnableLogging) Debug.Log(string.Format("Creating new source file {0} (internal)", processor.ValueRO.SourceFile));
CompilerManager.Instance.AddEmpty(processor.ValueRO.SourceFile, default);
compilerSystem.AddEmpty(processor.ValueRO.SourceFile, default);
instructions.Clear();
continue;
}
Expand Down

0 comments on commit 7599db8

Please sign in to comment.