Don't use dictionary to store vehicle mods

This commit is contained in:
Sardelka9515
2023-03-20 17:14:52 +08:00
parent 7cd0fd22af
commit 5f1aadbdb5
4 changed files with 19 additions and 13 deletions

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using GTA;
using GTA.Math;
@ -23,7 +24,7 @@ namespace RageCoop.Client
internal VehicleRoofState RoofState { get; set; }
internal VehicleDamageModel DamageModel { get; set; }
internal (byte, byte) Colors { get; set; }
internal Dictionary<int, int> Mods { get; set; }
internal (int, int)[] Mods { get; set; }
internal float EngineHealth { get; set; }
internal VehicleLockStatus LockStatus { get; set; }
internal byte RadioStation = 255;
@ -66,7 +67,7 @@ namespace RageCoop.Client
#region PRIVATE
private (byte, byte) _lastVehicleColors;
private Dictionary<int, int> _lastVehicleMods = new();
private (int, int)[] _lastVehicleMods = Array.Empty<(int, int)>();
private bool _lastHornActive;
private bool _lastTransformed;
internal int _lastLivery = -1;

View File

@ -1,4 +1,5 @@
using System;
using System.Linq;
using GTA;
using GTA.Math;
using GTA.Native;
@ -131,11 +132,11 @@ namespace RageCoop.Client
}
MainVehicle.EngineHealth = EngineHealth;
if (Mods != null && !Mods.Compare(_lastVehicleMods))
if (Mods != null && !Mods.SequenceEqual(_lastVehicleMods))
{
Call(SET_VEHICLE_MOD_KIT, MainVehicle, 0);
foreach (var mod in Mods) MainVehicle.Mods[(VehicleModType)mod.Key].Index = mod.Value;
foreach (var mod in Mods) MainVehicle.Mods[(VehicleModType)mod.Item1].Index = mod.Item2;
_lastVehicleMods = Mods;
}

View File

@ -47,10 +47,14 @@ namespace RageCoop.Client
return flags;
}
public static Dictionary<int, int> GetVehicleMods(this VehicleModCollection mods)
public static (int, int)[] GetVehicleMods(this VehicleModCollection mods)
{
var result = new Dictionary<int, int>();
foreach (var mod in mods.ToArray()) result.Add((int)mod.Type, mod.Index);
var modsArr = mods.ToArray();
var result = new (int, int)[modsArr.Length];
for (int i = 0; i < modsArr.Length; i++)
{
result[i] = ((int)modsArr[i].Type, modsArr[i].Index);
}
return result;
}

View File

@ -62,13 +62,13 @@ namespace RageCoop.Core
// Write vehicle mods
// Write the count of mods
m.Write((short)Mods.Count);
m.Write((short)Mods.Length);
// Loop the dictionary and add the values
foreach (var mod in Mods)
{
// Write the mod value
m.Write(mod.Key);
m.Write(mod.Value);
m.Write(mod.Item1);
m.Write(mod.Item2);
}
if (!DamageModel.Equals(default(VehicleDamageModel)))
@ -140,13 +140,13 @@ namespace RageCoop.Core
// Read vehicle mods
// Create new Dictionary
Mods = new Dictionary<int, int>();
// Read count of mods
var vehModCount = m.ReadInt16();
Mods = new (int, int)[vehModCount];
// Loop
for (var i = 0; i < vehModCount; i++)
// Read the mod value
Mods.Add(m.ReadInt32(), m.ReadInt32());
Mods[i] = (m.ReadInt32(), m.ReadInt32());
if (m.ReadBoolean())
// Read vehicle damage model
@ -183,7 +183,7 @@ namespace RageCoop.Core
public (byte, byte) Colors { get; set; }
public Dictionary<int, int> Mods { get; set; }
public (int, int)[] Mods { get; set; }
public VehicleDamageModel DamageModel { get; set; }