Add option to delay entity change publish and remove reflection stuff

Neither of them work actually
Added some delay between tests
This commit is contained in:
Norbi Peti 2022-02-07 00:25:01 +01:00
parent 5fea7dc3b3
commit ddaa933e7d
5 changed files with 55 additions and 12 deletions

View file

@ -82,7 +82,7 @@ namespace TechbloxModdingAPI.Blocks
yield break; yield break;
for (var index = 0; index < blocks.Length; index++) for (var index = 0; index < blocks.Length; index++)
{ {
if (index % 50 == 0) yield return Yield.It; //The material or flipped status can only be changed 130 times per submission if (index % 50 == 0) yield return new WaitForSecondsEnumerator(0.2f).Continue(); //The material or flipped status can only be changed 130 times per submission
var block = blocks[index]; var block = blocks[index];
if (!block.Exists) continue; if (!block.Exists) continue;
foreach (var property in block.GetType().GetProperties()) foreach (var property in block.GetType().GetProperties())

View file

@ -99,16 +99,19 @@ namespace TechbloxModdingAPI.Blocks.Engines
internal object GetBlockInfo(Block block, Type type, string name) internal object GetBlockInfo(Block block, Type type, string name)
{ {
var str = AccessTools.Method(typeof(BlockEngine), "GetBlockInfo", generics: new[] { type }) /*var opt = AccessTools.Method(typeof(BlockEngine), "GetBlockInfoOptional", generics: new[] { type })
.Invoke(this, new object[] { block }); .Invoke(this, new object[] { block }); - TODO: Cannot call method with by-ref return value
return AccessTools.Field(str.GetType(), name).GetValue(str); var str = AccessTools.Method(opt.GetType(), "Get").Invoke(opt, Array.Empty<object>());
return AccessTools.Field(str.GetType(), name).GetValue(str);*/
return AccessTools.Field(type, name).GetValue(Activator.CreateInstance(type));
} }
internal void SetBlockInfo(Block block, Type type, string name, object value) internal void SetBlockInfo(Block block, Type type, string name, object value)
{ {
var str = AccessTools.Method(typeof(BlockEngine), "GetBlockInfo", generics: new[] { type }) /*var opt = AccessTools.Method(typeof(BlockEngine), "GetBlockInfoOptional", generics: new[] { type })
.Invoke(this, new object[] { block }); .Invoke(this, new object[] { block });
AccessTools.Field(str.GetType(), name).SetValue(str, value); var str = AccessTools.Method(opt.GetType(), "Get").Invoke(opt, Array.Empty<object>());
AccessTools.Field(str.GetType(), name).SetValue(str, value);*/
} }
public void UpdateDisplayedBlock(EGID id) public void UpdateDisplayedBlock(EGID id)
@ -120,7 +123,7 @@ namespace TechbloxModdingAPI.Blocks.Engines
var skew = entitiesDB.QueryEntity<SkewComponent>(id); var skew = entitiesDB.QueryEntity<SkewComponent>(id);
entitiesDB.QueryEntity<RenderingDataStruct>(id).matrix = entitiesDB.QueryEntity<RenderingDataStruct>(id).matrix =
math.mul(float4x4.TRS(pos.position, rot.rotation, scale.scale), skew.skewMatrix); math.mul(float4x4.TRS(pos.position, rot.rotation, scale.scale), skew.skewMatrix);
entitiesDB.PublishEntityChange<GFXPrefabEntityStructGPUI>(id); // Signal a prefab change so it updates the render buffers entitiesDB.PublishEntityChangeDelayed<GFXPrefabEntityStructGPUI>(id); // Signal a prefab change so it updates the render buffers
} }
internal void UpdatePrefab(Block block, byte material, bool flipped) internal void UpdatePrefab(Block block, byte material, bool flipped)
@ -141,15 +144,15 @@ namespace TechbloxModdingAPI.Blocks.Engines
entitiesDB.QueryEntityOrDefault<GFXPrefabEntityStructGPUI>(block).prefabID = prefabId; entitiesDB.QueryEntityOrDefault<GFXPrefabEntityStructGPUI>(block).prefabID = prefabId;
if (block.Exists) if (block.Exists)
{ {
entitiesDB.PublishEntityChange<CubeMaterialStruct>(block.Id); entitiesDB.PublishEntityChangeDelayed<CubeMaterialStruct>(block.Id);
entitiesDB.PublishEntityChange<GFXPrefabEntityStructGPUI>(block.Id); entitiesDB.PublishEntityChangeDelayed<GFXPrefabEntityStructGPUI>(block.Id);
ref BuildingActionComponent local = ref BuildingActionComponent local =
ref entitiesDB.QueryEntity<BuildingActionComponent>(BuildingDroneUtility ref entitiesDB.QueryEntity<BuildingActionComponent>(BuildingDroneUtility
.GetLocalBuildingDrone(entitiesDB).ToEGID(entitiesDB)); .GetLocalBuildingDrone(entitiesDB).ToEGID(entitiesDB));
local.buildAction = BuildAction.ChangeMaterial; local.buildAction = BuildAction.ChangeMaterial;
local.targetPosition = block.Position; local.targetPosition = block.Position;
this.entitiesDB.PublishEntityChange<BuildingActionComponent>(local.ID); this.entitiesDB.PublishEntityChangeDelayed<BuildingActionComponent>(local.ID);
} }
//Phyiscs prefab: prefabAssetID, set on block creation from the CubeListData //Phyiscs prefab: prefabAssetID, set on block creation from the CubeListData
} }

View file

@ -8,7 +8,7 @@
<PackageProjectUrl>https://git.exmods.org/modtainers/GamecraftModdingAPI</PackageProjectUrl> <PackageProjectUrl>https://git.exmods.org/modtainers/GamecraftModdingAPI</PackageProjectUrl>
<NeutralLanguage>en-CA</NeutralLanguage> <NeutralLanguage>en-CA</NeutralLanguage>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>8</LangVersion> <LangVersion>9</LangVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
</PropertyGroup> </PropertyGroup>

View file

@ -205,7 +205,7 @@ namespace TechbloxModdingAPI.Tests
} while (cont); } while (cont);
} }
yield return Yield.It; yield return new WaitForSecondsEnumerator(1f).Continue();
} }
} }

View file

@ -1,4 +1,9 @@
using System;
using System.Collections.Generic;
using Svelto.ECS; using Svelto.ECS;
using Svelto.Tasks;
using Svelto.Tasks.Lean;
using TechbloxModdingAPI.Tasks;
namespace TechbloxModdingAPI.Utility namespace TechbloxModdingAPI.Utility
{ {
@ -58,5 +63,40 @@ namespace TechbloxModdingAPI.Utility
if (init.Has<T>()) return ref init.Get<T>();*/ if (init.Has<T>()) return ref init.Get<T>();*/
return ref opt.Get(); //Default value return ref opt.Get(); //Default value
} }
private static readonly Dictionary<Type, (int PublishedCount, HashSet<EGID> Changes)> ChangesToPublish = new();
/// <summary>
/// Publishes an entity change, ignoring duplicate publishes and delaying changes as necessary.
/// It will only publish in the next frame.
/// </summary>
/// <param name="entitiesDB">The entities DB to publish to</param>
/// <param name="obj">The ECS object that got changed</param>
/// <param name="limit">Limits how many changes to publish - should be no more than the consumers' capacity that process this component</param>
/// <typeparam name="T">The component that changed</typeparam>
public static void PublishEntityChangeDelayed<T>(this EntitiesDB entitiesDB, EGID id, int limit = 80)
where T : unmanaged, IEntityComponent
{ //TODO: Doesn't seem to help
if(!ChangesToPublish.ContainsKey(typeof(T)))
ChangesToPublish.Add(typeof(T), (0, new HashSet<EGID>()));
var changes = ChangesToPublish[typeof(T)].Changes;
if (changes.Contains(id)) return;
changes.Add(id);
PublishChanges<T>(entitiesDB, id, limit).RunOn(Scheduler.leanRunner);
}
private static IEnumerator<TaskContract> PublishChanges<T>(EntitiesDB entitiesDB, EGID id, int limit)
where T : unmanaged, IEntityComponent
{
yield return Yield.It;
while (ChangesToPublish[typeof(T)].PublishedCount >= limit)
yield return Yield.It;
entitiesDB.PublishEntityChange<T>(id);
var (count, changes) = ChangesToPublish[typeof(T)];
changes.Remove(id);
ChangesToPublish[typeof(T)] = (count + 1, changes);
yield return Yield.It;
ChangesToPublish[typeof(T)] = (0, changes);
}
} }
} }