Skip to content

Commit

Permalink
Proper working sync system when someone spawns
Browse files Browse the repository at this point in the history
Still needs extensive testing.
  • Loading branch information
ejb1123 committed Dec 4, 2017
1 parent 101ca87 commit 21e8d02
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 37 deletions.
1 change: 1 addition & 0 deletions server/ELS-Server/Class1.cs
Expand Up @@ -17,6 +17,7 @@ public Class1()
EventHandlers["ELS:FullSync:Broadcast"] += new Action<System.Dynamic.ExpandoObject>((dataDic) =>
{
var dd = (IDictionary<string,object> )dataDic;
Debug.WriteLine($"NetworkID {dd["NetworkID"]}");
_cachedData[(int)dd["NetworkID"]] = dd;
TriggerClientEvent("ELS:NewFullSyncData", dataDic);
});
Expand Down
7 changes: 4 additions & 3 deletions src/Debug.cs
Expand Up @@ -17,17 +17,18 @@ internal static async Task Spawn()
await polModel.Request(-1);
Vehicle veh = await World.CreateVehicle(polModel, Game.PlayerPed.Position);
polModel.MarkAsNoLongerNeeded();
veh.RegisterAsNetworked();
//veh.RegisterAsNetworked();
Screen.ShowNotification($"network status {Function.Call<bool>(Hash.NETWORK_GET_ENTITY_IS_NETWORKED, veh)}");
veh.SetExistOnAllMachines(true);
//await CitizenFX.Core.BaseScript.Delay(10000);
await CitizenFX.Core.BaseScript.Delay(10000);
CitizenFX.Core.Debug.WriteLine($"vehtonet{API.VehToNet(veh.Handle)} getnetworkidfromentity{API.NetworkGetNetworkIdFromEntity(veh.Handle)}");

if (veh == null)
{
CitizenFX.Core.Debug.WriteLine("failure to spawn");
return;
}
Game.Player.Character.SetIntoVehicle(veh, VehicleSeat.Any);
// Game.Player.Character.SetIntoVehicle(veh, VehicleSeat.Any);
}

}
Expand Down
38 changes: 17 additions & 21 deletions src/ELSVehicle.cs
Expand Up @@ -8,39 +8,35 @@ public class ELSVehicle : PoolObject, FullSync.IFullSyncComponent
{
private Siren.Siren _siren;
private Vehicle _vehicle;
public ELSVehicle(int handle,IDictionary<string,object> data) : base(handle)
public ELSVehicle(int handle, IDictionary<string, object> data) : base(handle)
{
_vehicle = new Vehicle(handle);
if(data.ContainsKey("Siren"))
ModelLoaded();
if (data.ContainsKey("Siren"))
{
_siren = new Siren.Siren(_vehicle, (IDictionary<string, object>)data["Siren"]);

}
else
{

//_vehicle.SetExistOnAllMachines(true);
CitizenFX.Core.Debug.WriteLine(CitizenFX.Core.Native.API.IsEntityAMissionEntity(_vehicle.Handle).ToString());
#if DEBUG

CitizenFX.Core.Debug.WriteLine($"registering netid:{_vehicle.GetNetworkId()}\n" +
$"Does entity belong to this script:{CitizenFX.Core.Native.API.DoesEntityBelongToThisScript(_vehicle.Handle,true)}\n" +
$"local handle{CitizenFX.Core.Native.API.NetworkGetEntityIsLocal(_vehicle.Handle)} veh handle{_vehicle.Handle}");

#endif
CitizenFX.Core.Native.API.NetworkRequestControlOfEntity(_vehicle.Handle);
CitizenFX.Core.Native.API.NetworkSetEntityVisibleToNetwork(_vehicle.Handle, false);
_vehicle.RegisterAsNetworked();
_vehicle.SetExistOnAllMachines(true);
BaseScript.Delay(5000);

#if DEBUG

CitizenFX.Core.Debug.WriteLine($"registering netid:{_vehicle.GetNetworkId()}\n" +
$"Does entity belong to this script:{CitizenFX.Core.Native.API.DoesEntityBelongToThisScript(_vehicle.Handle, true)}");
$"Does entity belong to this script:{CitizenFX.Core.Native.API.DoesEntityBelongToThisScript(_vehicle.Handle, false)}");

#endif
_siren = new Siren.Siren(_vehicle);
}
CitizenFX.Core.Debug.WriteLine($"created vehicle");
}
private async void ModelLoaded()
{
while (_vehicle.DisplayName == "CARNOTFOUND")
{
await CitizenFX.Core.BaseScript.Delay(0);
}
}
internal void CleanUP()
{
Expand All @@ -61,7 +57,7 @@ internal Vector3 GetBonePosistion()
}
public override bool Exists()
{
return CitizenFX.Core.Native.Function.Call<bool>(CitizenFX.Core.Native.Hash.DOES_ENTITY_EXIST,_vehicle);
return CitizenFX.Core.Native.Function.Call<bool>(CitizenFX.Core.Native.Hash.DOES_ENTITY_EXIST, _vehicle);
}

public override void Delete()
Expand All @@ -81,14 +77,14 @@ internal void UpdateRemoteSiren(string command, bool state)
{
_siren.SirenControlsRemote(command, state);
}
internal long GetNetworkId()
internal int GetNetworkId()
{
return this._vehicle.GetNetworkId();
return _vehicle.GetNetworkId();
}

public void SetData(IDictionary<string, object> data)
{
_siren.SetData((IDictionary<string,object>)data["siren"]);
_siren.SetData((IDictionary<string, object>)data["siren"]);
}

public Dictionary<string, object> GetData()
Expand Down
27 changes: 17 additions & 10 deletions src/Manager/VehicleManager.cs
Expand Up @@ -29,7 +29,9 @@ internal override void RunTick()
)
{
//Screen.ShowNotification("adding vehicle");
AddIfNotPresint(Game.PlayerPed.CurrentVehicle, vehicle: out _currentVehicle);
API.SetEntityAsMissionEntity(Game.PlayerPed.CurrentVehicle.Handle, false, false);
Game.PlayerPed.CurrentVehicle.SetExistOnAllMachines(true);
AddIfNotPresint(API.VehToNet(Game.PlayerPed.CurrentVehicle.Handle), vehicle: out _currentVehicle);
_currentVehicle?.RunTick();
if (Game.IsControlJustPressed(0, Control.Cover))
{
Expand Down Expand Up @@ -57,20 +59,26 @@ internal override void RunTick()
//TODO Chnage how I check for the panic alarm
}

private bool AddIfNotPresint(Vehicle o, [Optional] IDictionary<string, object> data, [Optional]out ELSVehicle vehicle)
private bool AddIfNotPresint(int NETID, [Optional] IDictionary<string, object> data, [Optional]out ELSVehicle vehicle)
{
if (NETID == 0)
{
CitizenFX.Core.Debug.WriteLine("ERROR NetwordID equals 0\n");
vehicle = null;
return false;
}

if (!Entities.Exists(poolObject => poolObject.Handle == o.Handle))
else if (!Entities.Exists(poolObject => ((ELSVehicle) poolObject).GetNetworkId() == NETID))
{
if (data == null) data = new Dictionary<string, object>();
var veh = new ELSVehicle(o.Handle, data);
var veh = new ELSVehicle(API.NetToVeh(NETID), data);
Entities.Add(veh);
vehicle = veh;
return false;
}
else
{
vehicle = Entities.Find(poolObject => poolObject.Handle == o.Handle) as ELSVehicle;
vehicle = Entities.Find(poolObject => ((ELSVehicle)poolObject).GetNetworkId() == NETID) as ELSVehicle;
return true;
}
}
Expand All @@ -97,14 +105,13 @@ private bool AddIfNotPresint(Vehicle o, [Optional] IDictionary<string, object> d
/// <param name="dataDic">data</param>
async internal void SetVehicleSyncData(IDictionary<string, object> dataDic)
{
CitizenFX.Core.Debug.WriteLine("cerate evhc");
var veh = new Vehicle(API.NetworkGetEntityFromNetworkId((int)dataDic["NetworkID"]));
var bo = AddIfNotPresint(veh
CitizenFX.Core.Debug.WriteLine($"creating vehicle with NETID of {(int)dataDic["NetworkID"]} LOCALID of {CitizenFX.Core.Native.API.NetToVeh((int)dataDic["NetworkID"])}");
var bo = AddIfNotPresint((int)dataDic["NetworkID"]
, dataDic,out ELSVehicle veh1);
veh1.SetData(dataDic);
}

internal static void SyncRequestReply(long NetworkId)
internal static void SyncRequestReply(int NetworkId)
{
FullSync.FullSyncManager.SendDataBroadcast(
((ELSVehicle)Entities.Find(o => ((ELSVehicle)o).GetNetworkId() == NetworkId)).GetData()
Expand All @@ -119,7 +126,7 @@ internal void SyncAllVehiclesOnFirstSpawn(System.Dynamic.ExpandoObject data)
int netID = int.Parse(struct1.Key);
var vehData = (IDictionary<string,object>)struct1.Value;
CitizenFX.Core.Debug.WriteLine($"{vehData["NetworkID"]}, {API.NetworkDoesEntityExistWithNetworkId(netID)}, {API.NetworkDoesNetworkIdExist(netID)}, {API.DoesEntityExist(API.NetworkGetEntityFromNetworkId(netID))}");
AddIfNotPresint(new Vehicle(API.NetworkGetEntityFromNetworkId( (int)vehData["NetworkID"])),
AddIfNotPresint((int)vehData["NetworkID"],
vehData,
out ELSVehicle veh
);
Expand Down
1 change: 1 addition & 0 deletions src/RemoteEventManager.cs
Expand Up @@ -51,6 +51,7 @@ internal enum MessageTypes

internal static void SendEvent(Commands type, Vehicle vehicle, bool state, int playerID)
{
CitizenFX.Core.Debug.WriteLine($"sendding data for netID {vehicle.GetNetworkId()}");
Manager.VehicleManager.SyncRequestReply(vehicle.GetNetworkId());

//var netId = Function.Call<int>(Hash.VEH_TO_NET, vehicle.Handle);
Expand Down
2 changes: 1 addition & 1 deletion src/Siren/Siren/Controls/Siren.Controls.KB.cs
Expand Up @@ -107,7 +107,7 @@ void DualSirenControlsKB()
DualSirenLogic(true, true);
#endif
System.Collections.Generic.Dictionary<String, object> dic = new System.Collections.Generic.Dictionary<string, object>();
Manager.VehicleManager.SyncRequestReply(this._vehicle.GetNetworkId());
Manager.VehicleManager.SyncRequestReply(_vehicle.GetNetworkId());
//RemoteEventManager.SendEvent(RemoteEventManager.Commands.DualSiren, _vehicle, true, Game.Player.ServerId);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/extentions.cs
Expand Up @@ -28,9 +28,9 @@ async public static Task<bool> RequestCollision(this Vehicle vehicle)
// if (!poolObject.Exists()) poolObject.CleanUp();
//}

public static Int64 GetNetworkId(this Entity entity)
public static int GetNetworkId(this Entity entity)
{
return Function.Call<Int64>(Hash.VEH_TO_NET, entity.Handle);
return Function.Call<int>(Hash.VEH_TO_NET, entity.Handle);
}

public static void RegisterAsNetworked(this Entity entity)
Expand Down

0 comments on commit 21e8d02

Please sign in to comment.