Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

Commit

Permalink
Add weapon re-attach timer schedule, try to fix sync issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
imckl committed May 7, 2019
1 parent 793c800 commit 397b35d
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions WeaponOnBackClient/WeaponOnBack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public WeaponOnBack()

Tick += UpdateWeaponsOnBackAsync;
Tick += LoadWeaponAsset;
Tick += ReAttachWeapons;

API.RegisterCommand("wob", new Action<int, List<object>, string>((source, args, raw) =>
{
Expand Down Expand Up @@ -239,6 +240,11 @@ public static void SetWeapon(Weapon weapon)
DisplayGroupsWeapon[equipGroup] = weapon;
}

public static void SetWeapon(WeaponDisplayGroup displayGroup, Weapon weapon)
{
DisplayGroupsWeapon[displayGroup] = weapon;
}

public static Weapon GetWeapon(WeaponDisplayGroup displayGroup)
{
DisplayGroupsWeapon.TryGetValue(displayGroup, out var weapon);
Expand Down Expand Up @@ -267,6 +273,32 @@ public static void DeleteAllWeaponObjects()
}
}

static async Task ReAttachWeapons()
{
WeaponDisplay.GetAllDisplayedWeaponObjects()
.Where(d => !(d.Value is null) && d.Value.Exists())
.ToList()
.ForEach(d =>
{
var weaponDisplayGroup = d.Key;
var weaponObject = d.Value;
var weapon = WeaponDisplay.GetWeapon(weaponDisplayGroup);

// Try sync
API.NetworkRegisterEntityAsNetworked(API.ObjToNet(weaponObject.Handle));
weaponObject.IsPersistent = true;
var netId = API.NetworkGetNetworkIdFromEntity(weaponObject.Handle);
API.SetNetworkIdCanMigrate(netId, true);
API.SetNetworkIdExistsOnAllMachines(netId, true);
API.NetworkRequestControlOfEntity(weaponObject.Handle);

AttachWeapon(weapon, weaponObject);
Debug.WriteLine($"[{ResourceName}]ReAttachWeapon: weapon: {weapon.Hash}, model: {weaponObject.Model.Hash}, handle: {weaponObject.Handle}");
});

await Delay(1000 * 10);
}

static async Task UpdateWeaponsOnBackAsync()
{
await Delay(100);
Expand Down Expand Up @@ -306,6 +338,7 @@ static async Task UpdateWeaponsOnBackAsync()

AttachWeapon(weaponNeedToCreate, weaponObject);

WeaponDisplay.SetWeapon(WeaponDisplay.GetWeaponDisplayGroup(weaponNeedToCreate), weaponNeedToCreate);
WeaponDisplay.SetWeaponObject(WeaponDisplay.GetWeaponDisplayGroup(weaponNeedToCreate), weaponObject);

_latestWeaponObjectOnBack = weaponObject;
Expand Down

0 comments on commit 397b35d

Please sign in to comment.