This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
WorkerSystem.cs
127 lines (107 loc) · 4.08 KB
/
WorkerSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Collections.Generic;
using Improbable.Gdk.Core.NetworkStats;
using Improbable.Worker.CInterop;
using Unity.Entities;
using Unity.Profiling;
using UnityEngine;
using Entity = Unity.Entities.Entity;
namespace Improbable.Gdk.Core
{
/// <summary>
/// A SpatialOS worker instance.
/// </summary>
[DisableAutoCreation]
public class WorkerSystem : ComponentSystem
{
/// <summary>
/// An ECS entity that represents the Worker.
/// </summary>
public Entity WorkerEntity;
public readonly ILogDispatcher LogDispatcher;
public readonly string WorkerType;
public readonly string WorkerId;
public readonly Vector3 Origin;
/// <summary>
/// Denotes whether the underlying worker is connected or not.
/// </summary>
public bool IsConnected => Worker.IsConnected;
internal readonly View View;
internal readonly Dictionary<EntityId, Entity> EntityIdToEntity = new Dictionary<EntityId, Entity>();
internal WorkerInWorld Worker;
internal ViewDiff Diff => Worker.ViewDiff;
internal MessagesToSend MessagesToSend => Worker.MessagesToSend;
private ProfilerMarker tickMarker = new ProfilerMarker("WorkerSystem.Tick");
public WorkerSystem(WorkerInWorld worker)
{
Worker = worker;
LogDispatcher = worker.LogDispatcher;
WorkerType = worker.WorkerType;
WorkerId = worker.WorkerId;
Origin = worker.Origin;
View = worker.View;
}
/// <summary>
/// Attempts to find an ECS entity associated with a SpatialOS entity ID.
/// </summary>
/// <param name="entityId">The SpatialOS entity ID.</param>
/// <param name="entity">
/// When this method returns, contains the ECS entity associated with the SpatialOS entity ID if one was
/// found, else the default value for <see cref="Entity" />.
/// </param>
/// <returns>
/// True, if an ECS entity associated with the SpatialOS entity ID was found, false otherwise.
/// </returns>
public bool TryGetEntity(EntityId entityId, out Entity entity)
{
return EntityIdToEntity.TryGetValue(entityId, out entity);
}
public Entity GetEntity(EntityId entityId)
{
if (!EntityIdToEntity.TryGetValue(entityId, out var entity))
{
throw new ArgumentException($"Unknown EntityId {entityId}", nameof(entityId));
}
return entity;
}
/// <summary>
/// Checks whether a SpatialOS entity is checked out on this worker.
/// </summary>
/// <param name="entityId">The SpatialOS entity ID to check for.</param>
/// <returns>True, if the SpatialOS entity is checked out on this worker, false otherwise.</returns>
public bool HasEntity(EntityId entityId)
{
return EntityIdToEntity.ContainsKey(entityId);
}
public void SendLogMessage(string message, string loggerName, LogLevel logLevel, EntityId? entityId)
{
Worker.MessagesToSend.AddLogMessage(new LogMessageToSend(message, loggerName, logLevel, entityId?.Id));
}
public void SendMetrics(Metrics metrics)
{
Worker.MessagesToSend.AddMetrics(metrics);
}
internal void Tick()
{
using (tickMarker.Auto())
{
Worker.Tick();
}
}
internal void SendMessages(NetFrameStats frameStats)
{
Worker.EnsureMessagesFlushed(frameStats);
}
protected override void OnCreate()
{
base.OnCreate();
var entityManager = World.EntityManager;
WorkerEntity = entityManager.CreateEntity(typeof(OnConnected), typeof(WorkerEntityTag));
EntityIdToEntity.Add(new EntityId(0), WorkerEntity);
Enabled = false;
}
protected override void OnUpdate()
{
}
}
}