Skip to content

Commit

Permalink
Bugfixes and optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
niekschoemaker committed Nov 6, 2021
1 parent a9d56e5 commit f474b5e
Show file tree
Hide file tree
Showing 39 changed files with 1,694 additions and 1,263 deletions.
3 changes: 2 additions & 1 deletion server/ELS-Server/ELS-Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="CitizenFX.Core.Server, Version=0.0.0.0, Culture=neutral, processorArchitecture=AMD64">
<HintPath>..\..\src\packages\CitizenFX.Core.Server.1.0.3489\lib\net45\CitizenFX.Core.Server.dll</HintPath>
<HintPath>..\..\src\packages\CitizenFX.Core.Server.1.0.4394\lib\net45\CitizenFX.Core.Server.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand All @@ -54,6 +54,7 @@
<Compile Include="Mapper.cs" />
<Compile Include="NanoXMLParser.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Timer.cs" />
<Compile Include="Utils.cs" />
<Compile Include="VCF.cs" />
<Compile Include="VcfSync.cs" />
Expand Down
55 changes: 46 additions & 9 deletions server/ELS-Server/ELSServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,29 +123,50 @@ public void UpdateState(int networkID, IDictionary<string, object> data = null)
}
}

[EventHandler(EventNames.VcfSyncServer)]
public void VcfSyncServer([FromSource] Player player)
private readonly Dictionary<string, Timer> rateLimiters = new Dictionary<string, Timer>();

private bool IsRateLimited(Player player)
{
TriggerClientEvent(player, EventNames.VcfSyncClient, VcfSync.VcfData);
TriggerClientEvent(player, "ELS:PatternSync:Client", CustomPatterns.Patterns);
if (!rateLimiters.TryGetValue(player.Handle, out var timer))
{
timer = new Timer(500);
rateLimiters.Add(player.Handle, timer);
}
else
{
if (!timer.Expired)
{
return true;
}
}

timer.Reset();
return false;
}


[EventHandler(EventNames.FullSyncBroadcast)]
public void FullSyncBroadcast([FromSource] Player player, ExpandoObject expandoData, int networkId)
public async void FullSyncBroadcast([FromSource] Player player, ExpandoObject expandoData, int networkId)
{
if (IsRateLimited(player))
{
CitizenFX.Core.Debug.WriteLine($"{player.Name} ({player.Identifiers["steam"]}) exceeded FullSyncBroadcast ratelimiter!");
}

var dataDict = (IDictionary<string, object>)expandoData;
_cachedData[networkId] = dataDict;
CheckModel(networkId);
_lastPlayerData[networkId] = player.Handle;

UpdateState(networkId, dataDict);
await Delay(0);
TriggerClientEvent(EventNames.NewFullSyncData, expandoData, networkId, int.Parse(player.Handle));
}

[EventHandler(EventNames.FullSycnRequestAll)]
public void FullSyncRequestAll([FromSource] Player player)
{
TriggerClientEvent(player, EventNames.NewSpawnWithData, _cachedData);
TriggerLatentClientEvent(player, EventNames.NewSpawnWithData, 12500, _cachedData);
}

[EventHandler(EventNames.FullSyncRequestOne)]
Expand All @@ -160,10 +181,15 @@ public void FullSyncRequestOne([FromSource] Player player, int networkID)
}

[EventHandler(EventNames.LightSyncBroadcast)]
public void LightSyncBroadcast([FromSource] Player player, IDictionary<string, object> newData, int networkID)
public async void LightSyncBroadcast([FromSource] Player player, IDictionary<string, object> newData, int networkID)
{
try
{
if (IsRateLimited(player))
{
CitizenFX.Core.Debug.WriteLine($"{player.Name} ({player.Identifiers["steam"]}) exceeded LightSyncBroadcast ratelimiter!");
}

if (!_cachedData.TryGetValue(networkID, out var cachedData))
{
cachedData = new Dictionary<string, object>();
Expand All @@ -188,6 +214,7 @@ public void LightSyncBroadcast([FromSource] Player player, IDictionary<string, o
_lastPlayerData[networkID] = player.Handle;

UpdateState(networkID, cachedData);
await Delay(0);
TriggerClientEvent(EventNames.NewLightSyncData, newData, networkID, int.Parse(player.Handle));
}
catch (Exception e)
Expand All @@ -203,7 +230,12 @@ public void CheckModel(int networkId)
var cachedData = _cachedData[networkId];
if (!cachedData.ContainsKey(DataNames.Model))
{
var vehicle = new Vehicle(API.NetworkGetEntityFromNetworkId(networkId));
var handle = API.NetworkGetEntityFromNetworkId(networkId);
if (handle == 0)
{
return;
}
var vehicle = new Vehicle(handle);
cachedData[DataNames.Model] = vehicle.Model;
}
}
Expand All @@ -223,10 +255,14 @@ public void TestSirenCommand(Player player, string[] args)


[EventHandler(EventNames.SirenSyncBroadcast)]
public void SirenSyncBroadCast([FromSource] Player player, IDictionary<string, object> newData, int networkID)
public async void SirenSyncBroadCast([FromSource] Player player, IDictionary<string, object> newData, int networkID)
{
try
{
if (IsRateLimited(player))
{
CitizenFX.Core.Debug.WriteLine($"{player.Name} ({player.Identifiers["steam"]}) exceeded SirenSyncBroadcast ratelimiter!");
}
// Make sure the cachedData exists and otherwise create it
if (!_cachedData.TryGetValue(networkID, out var cachedData))
{
Expand All @@ -248,6 +284,7 @@ public void SirenSyncBroadCast([FromSource] Player player, IDictionary<string, o
_lastPlayerData[networkID] = player.Handle;

UpdateState(networkID, cachedData);
await Delay(0);
TriggerClientEvent(EventNames.NewSirenSyncData, newData, networkID, int.Parse(player.Handle));
}
catch (Exception e)
Expand Down
58 changes: 58 additions & 0 deletions server/ELS-Server/Timer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ELS_Server
{
public class Timer
{
static long TicksPerMillisecond = Stopwatch.Frequency / 1000;
long _limit = 0;

int _timeLimit = 0;

public int Limit
{
set
{
_limit = Stopwatch.GetTimestamp() + value * TicksPerMillisecond;
_timeLimit = value;
}
get
{
return _timeLimit;
}
}
public bool Expired
{
get
{

if (Stopwatch.GetTimestamp() > _limit)
{
return true;
}
else
{
return false;
}

}
}

public Timer() { }

public Timer(int limit)
{
Limit = limit;
}

public void Reset()
{
Limit = _timeLimit;
}
}
}
2 changes: 1 addition & 1 deletion server/ELS-Server/VcfSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class VcfSync
public static List<string> ElsResources = new List<string>();
public static List<Tuple<string, string, string>> VcfData = new List<Tuple<string, string, string>>();

public async Task CheckVCF(Player player)
public void CheckVCF(Player player)
{
foreach (string name in ElsResources)
{
Expand Down
2 changes: 1 addition & 1 deletion server/ELS-Server/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="CitizenFX.Core.Server" version="1.0.3489" targetFramework="net452" />
<package id="CitizenFX.Core.Server" version="1.0.4394" targetFramework="net452" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net461" />
<package id="SharpRaven" version="2.2.0" targetFramework="net461" />
</packages>
2 changes: 1 addition & 1 deletion src/Board/ArrowBoard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace ELS.Board
{
internal class ArrowBoard : IFullSyncComponent
internal class ArrowBoard
{
ILight lights;
configuration.MISC _misc;
Expand Down
61 changes: 41 additions & 20 deletions src/ELS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ You should have received a copy of the GNU Lesser General Public License
using ELS.NUI;
using ELSShared;
using Newtonsoft.Json;
using Shared;
using System;
using System.Collections.Generic;
using System.Dynamic;
Expand All @@ -37,7 +38,7 @@ namespace ELS
{
public class ELS : BaseScript
{
public static int GameTime { get; private set; }
public static int GameTime { get; private set; } = Game.GameTime;
internal static int? CurrentSeat { get; private set; }
internal static Vehicle CurrentVehicle { get; private set; }
private readonly FileLoader _FileLoader;
Expand All @@ -50,6 +51,11 @@ public class ELS : BaseScript

public ELS()
{
if (API.GetConvar("dev_server", "false") == "true" || API.GetConvar("test_server", "false") == "true")
{
Utils.IsDeveloper = true;
Utils.ReleaseWriteLine("Debug logging enabled!");
}
bool Loaded = false;
_controlConfiguration = new ElsConfiguration();
_FileLoader = new FileLoader(this);
Expand All @@ -73,7 +79,10 @@ public ELS()
await Delay(250);
SetupConnections();
await Delay(250);
TriggerServerEvent(EventNames.VcfSyncServer, Game.Player.ServerId);
VcfSync.CheckResources();
VCF.ParseVcfs(VcfSync.VcfData);
CustomPatterns.CheckCustomPatterns();
VCF.ParsePatterns(CustomPatterns.Patterns);
await Delay(250);
Tick -= Class1_Tick;
Tick += Class1_Tick;
Expand Down Expand Up @@ -108,10 +117,6 @@ public ELS()
}
}
});
if (API.GetConvar("dev_server", "false") == "true")
{
Utils.IsDeveloper = true;
}

}
int lastVehicle = -1;
Expand All @@ -129,15 +134,6 @@ private void SetupConnections()
}
});

EventHandlers[EventNames.VcfSyncClient] += new Action<List<object>>((vcfs) =>
{
VCF.ParseVcfs(vcfs);
});

EventHandlers.Add("ELS:PatternSync:Client", new Action<List<dynamic>>((patterns) =>
{
VCF.ParsePatterns(patterns);
}));
EventHandlers["ELS:FullSync:NewSpawnWithData"] += new Action<IDictionary<string, object>>((a) =>
{
VehicleManager.Instance.SyncAllVehiclesOnFirstSpawn(a);
Expand Down Expand Up @@ -397,7 +393,7 @@ public void GetState(int source, List<object> args, string raw)
#endif

[Command("vcfsync")]
public void VcfSync()
public void VcfSyncCommand()
{
if (GameTime - lastCall < 60000)
{
Expand Down Expand Up @@ -500,6 +496,33 @@ internal static Vector3 Position {
}
set => position = value;
}

private static InputMode? _currentInputMode = null;
public static InputMode CurrentInputMode
{
get
{
if (_currentInputMode == null)
{
_currentInputMode = API.IsInputDisabled(2) ? InputMode.MouseAndKeyboard : InputMode.GamePad;
}
return _currentInputMode.Value;
}
}

private void ClearGetterCaches()
{
GameTime = Game.GameTime;
Player = null;
Ped = null;
position = null;
_currentInputMode = null;
}

public delegate void OnTickEventHandler(object sender);

public static event OnTickEventHandler OnTick;

private async Task Class1_Tick()
{
try
Expand All @@ -515,10 +538,8 @@ private async Task Class1_Tick()
{
return;
}
GameTime = Game.GameTime;
Player = null;
Ped = null;
position = null;
OnTick?.Invoke(this);
ClearGetterCaches();
VehicleManager.Instance.RunTick();
if (CurrentVehicle?.IsEls() ?? false)
{
Expand Down
4 changes: 3 additions & 1 deletion src/ELS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="CitizenFX.Core.Client, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\CitizenFX.Core.Client.1.0.3489\lib\net45\CitizenFX.Core.Client.dll</HintPath>
<HintPath>packages\CitizenFX.Core.Client.1.0.4394\lib\net45\CitizenFX.Core.Client.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
Expand All @@ -99,11 +99,13 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Board\ArrowBoard.cs" />
<Compile Include="configuration\CustomPatterns.cs" />
<Compile Include="configuration\ElsConfiguration.cs" />
<Compile Include="configuration\Global.cs" />
<Compile Include="configuration\NanoXMLParser.cs" />
<Compile Include="configuration\SettingsType.cs" />
<Compile Include="configuration\UserSettings.cs" />
<Compile Include="configuration\VcfSync.cs" />
<Compile Include="ELS.cs" />
<Compile Include="Extra\Extra.FullSync.cs" />
<Compile Include="Gadgets\Ladder.cs" />
Expand Down
Loading

1 comment on commit f474b5e

@ToasterF16
Copy link

@ToasterF16 ToasterF16 commented on f474b5e Jan 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the first ELS that has the "whoop whoop" sync bug fixed, that you get when 2 vehicles from outside of each others hearing range drive towards each other. Our server was really hoping for a project like this.

However, it does have 2 issues that currently make us refrain from using it right now:

If you could use the files for those things from the latest ELS+ dev branch / version published on the Friends in Code discord to ensure compatibility with .xml files made for current els-plus with server sided sirens that would be greatly appreciated!

Please sign in to comment.