Skip to content

Commit

Permalink
yay more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
guerro323 committed Aug 8, 2021
1 parent a69eda3 commit 1004137
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
11 changes: 9 additions & 2 deletions GameHost/Core/Modules/Feature/Components.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@ public enum ModuleState
Zombie
}

public struct RequestLoadModule
public readonly struct RequestLoadModule
{
public Entity Module;
public readonly string Name;
public readonly Entity Module;

public RequestLoadModule(string name, Entity module)
{
Name = name;
Module = module;
}
}

public struct RequestUnloadModule
Expand Down
33 changes: 22 additions & 11 deletions GameHost/Core/Modules/Feature/GatherModuleSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using DefaultEcs;
using GameHost.Applications;
using GameHost.Core.Ecs;
Expand Down Expand Up @@ -56,12 +57,12 @@ protected override async void OnUpdate()
isTaskRunning = true;
var files = (await moduleStorage.GetFilesAsync("*.dll")).ToList();
// Destroy unloaded modules...
scheduler.Schedule(DestroyUnloadedModules, SchedulingParameters.AsOnce);
// no? scheduler.Schedule(DestroyUnloadedModules, SchedulingParameters.AsOnce);

foreach (var file in files)
{
var assemblyName = file.Name.Replace(".dll", "");
var rm = FindOrCreateEntity(assemblyName, out var wasCreated);
var (rm, _) = await FindOrCreateEntity(assemblyName);
// We have found an already existing module, does not do further operation on it...
if (rm.Has<RegisteredModule>() && rm.Get<RegisteredModule>().State != ModuleState.None)
continue;
Expand All @@ -88,7 +89,7 @@ protected override async void OnUpdate()
scheduler.Schedule(moduleEntity =>
{
World.Mgr.CreateEntity()
.Set(new RequestLoadModule { Module = moduleEntity });
.Set(new RequestLoadModule($"AutoLoad({moduleEntity.Get<RegisteredModule>().Description.NameId})", moduleEntity));
}, moduleEntity, default);
// ReSharper restore VariableHidesOuterVariable
}
Expand All @@ -106,17 +107,27 @@ protected override async void OnUpdate()
/// </summary>
/// <param name="assemblyName"></param>
/// <returns>An existing or new entity.</returns>
private Entity FindOrCreateEntity(string assemblyName, out bool wasCreated)
private async Task<(Entity module, bool wasCreated)> FindOrCreateEntity(string assemblyName)
{
wasCreated = false;
foreach (var entity in moduleSet.GetEntities())
var entityTcs = new TaskCompletionSource<Entity>();
var wasCreatedTcs = new TaskCompletionSource<bool>();
scheduler.Schedule(() =>
{
if (entity.Get<RegisteredModule>().Description.NameId == assemblyName)
return entity;
}
foreach (var entity in moduleSet.GetEntities())
{
if (entity.Get<RegisteredModule>().Description.NameId == assemblyName)
{
wasCreatedTcs.SetResult(false);
entityTcs.SetResult(entity);
return;
}
}

wasCreatedTcs.SetResult(true);
entityTcs.SetResult(World.Mgr.CreateEntity());
}, default);

wasCreated = true;
return World.Mgr.CreateEntity();
return (await entityTcs.Task, await wasCreatedTcs.Task);
}

/// <summary>
Expand Down
5 changes: 4 additions & 1 deletion GameHost/Core/Modules/Feature/ManageModuleLoadSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ protected override void OnUpdate()
foreach (var entity in loadSet.GetEntities())
{
var request = entity.Get<RequestLoadModule>();
if (!request.Module.IsAlive)
throw new InvalidOperationException($"Module Entity was destroyed (Given Name: {request.Name})");

if (request.Module.Get<RegisteredModule>().State != ModuleState.None)
continue; // should we report that?

Expand All @@ -66,7 +69,7 @@ protected override void OnUpdate()
}

unloadSet.DisposeAllEntities();

scheduler.Run();
}
}
Expand Down
2 changes: 1 addition & 1 deletion GameHost/Core/RPC/AvailableRpcCommands/LoadModuleRpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected override ValueTask<NoMembersResponsePacket> GetResponse(LoadModuleRpc
if (m.State == ModuleState.None)
{
World.Mgr.CreateEntity()
.Set(new RequestLoadModule {Module = entity});
.Set(new RequestLoadModule($"RpcLoadModule({request.ModuleId})", entity));

return default;
}
Expand Down

0 comments on commit 1004137

Please sign in to comment.