Fix cooldown ticker ending at the wrong time

This commit is contained in:
NGnius (Graham) 2021-03-08 11:24:33 -05:00
parent 84dec875b9
commit 00589d1c28

View file

@ -86,7 +86,7 @@ namespace CLre.Fixes
//API.Utility.Logging.MetaLog($"Cooling down? {isInCooldown} for {cooldownLeft}s");
// build functions for querying all Game.Handhelds.WeaponCooldownEntityView objects
Type protoRaw =
typeof(Reflection.QueryEntityViews<IEntityViewStruct>).GetGenericTypeDefinition()
typeof(Reflection.QueryEntityViews<>)
.MakeGenericType(
AccessTools.TypeByName("Game.Handhelds.WeaponCooldownEntityView"));
Delegate queryAllWCEV =
@ -100,10 +100,16 @@ namespace CLre.Fixes
object collectionStruct =
queryAllWCEV.DynamicInvoke((int) baseGroup);
int count = Traverse.Create(collectionStruct).Field<int>("_count").Value;
PropertyInfo indexer = typeof(Svelto.DataStructures.ReadOnlyCollectionStruct<IEntityStruct>).GetGenericTypeDefinition()
PropertyInfo indexer = typeof(Svelto.DataStructures.ReadOnlyCollectionStruct<>)
.MakeGenericType(AccessTools.TypeByName("Game.Handhelds.WeaponCooldownEntityView"))
.GetIndexer();
object[] indexParams = {0};
if (!isRunningTick)
{
isRunningTick = true;
cooldownTickEverything(characterId, cwcevExists, queryCWCEV, queryWCEV, queryAllWCEV, indexer, baseGroup).Run();
}
/*object[] indexParams = {0};
for (int index = 0; index < count; index++)
{
#if DEBUG
@ -119,12 +125,7 @@ namespace CLre.Fixes
Traverse.Create(wcev)
.Field("weaponCooldownComponent")
.Property<bool>("isInCooldown").Value = isInCooldown;
}
if (!isRunningTick)
{
cooldownTickEverything(characterId, entitiesDB, cwcevExists, queryCWCEV, queryWCEV, queryAllWCEV, indexer, baseGroup).Run();
}
}*/
}
[HarmonyTargetMethod]
@ -134,14 +135,10 @@ namespace CLre.Fixes
new[] {typeof(int), typeof(bool)});
}
private static IEnumerator cooldownTickEverything(int characterId, IEntitiesDB entitiesDb, Reflection.ExistsV2 cwcevExists, Reflection.QueryEntityViewV2<object> cwcevQuery, Reflection.QueryEntityViewV2<object> wcevQuery, Delegate wcevQueryAll, PropertyInfo indexer, ExclusiveGroup baseGroup)
private static IEnumerator cooldownTickEverything(int characterId, Reflection.ExistsV2 cwcevExists, Reflection.QueryEntityViewV2<object> cwcevQuery, Reflection.QueryEntityViewV2<object> wcevQuery, Delegate wcevQueryAll, PropertyInfo indexer, ExclusiveGroup baseGroup)
{
isRunningTick = true;
while (cwcevExists(characterId, (int) DEPRECATED_SveltoExtensions.DEPRECATED_GROUP))
{
#if DEBUG
API.Utility.Logging.MetaLog("Doing cooldown tick for all weapons");
#endif
// get equipped handheld info
object cwcevOriginal = cwcevQuery(characterId,
(ExclusiveGroup.ExclusiveGroupStruct) DEPRECATED_SveltoExtensions.DEPRECATED_GROUP);
@ -157,9 +154,9 @@ namespace CLre.Fixes
Traverse.Create(wcevOriginal).Field("weaponCooldownComponent").Property<float>("cooldownLeft").Value;
bool isInCooldown =
Traverse.Create(wcevOriginal).Field("weaponCooldownComponent").Property<bool>("isInCooldown").Value;
if (!isInCooldown) break;
object[] indexParams = {0};
// iterate over other handhelds and sync their cooldowns to the held item
API.Utility.Logging.MetaDebugLog($"Syncing {count} weapon cooldowns with the held item {toolId}");
for (int index = 0; index < count; index++)
{
indexParams[0] = index;
@ -173,10 +170,12 @@ namespace CLre.Fixes
.Field("weaponCooldownComponent")
.Property<bool>("isInCooldown").Value = isInCooldown;
}
// stop running this task after one final sync to set every handheld to non-cooldown state
if (!isInCooldown && cooldownLeft <= 0) break;
yield return null;
}
// cleanup
API.Utility.Logging.MetaDebugLog("Custom cooldown ticks complete");
isRunningTick = false;
yield return null;
}