Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core - Fix load/save of deleted entities such as bushes and trees #153

Merged
merged 5 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions addons/core/scripts/Game/ACE_Core/Global/ACE_EntityIdHelper.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,36 @@ class ACE_EntityIdHelper
//------------------------------------------------------------------------------------------------
static array<int> ToInt(EntityID id)
{
return ACE_HexTools.HexStringToInt(ToString(id));
array<int> bits = ACE_HexTools.HexStringToInt(ToString(id));
if (bits.Count() == 1)
bits.InsertAt(0, 0);

return bits;
}

//------------------------------------------------------------------------------------------------
static EntityID FromString(string str)
{
array<int> bits = ACE_HexTools.HexStringToInt(str);
if (bits.Count() < 2)
return EntityID.INVALID;
if (bits.Count() == 1)
bits.InsertAt(0, 0);
else
if (bits.Count() > 2)
return EntityID.INVALID;

return EntityID.FromInt(bits[0], bits[1]);
}

//------------------------------------------------------------------------------------------------
static string ToString(EntityID id)
{
// Drop the last three characters, which are " {}"
return id.ToString().Substring(0, 18);
string str = id.ToString();

// Low id items such as trees/bushes end up with this odd format, fix it
if (str.StartsWith("0x0x"))
return str.Substring(2, str.Length() - 5);
else
return str.Substring(0, str.Length() - 3);
}
}
82 changes: 38 additions & 44 deletions addons/core/scripts/Game/ACE_Core/Global/ACE_HexTools.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,58 @@ class ACE_HexTools
{
protected static int s_iAscii0 = "0".ToAscii();
protected static int s_iAscii9 = "9".ToAscii();
protected static int s_iAsciiA = "A".ToAscii();
protected static int s_iAsciiF = "F".ToAscii();
protected static int s_iAsciiLoA = "a".ToAscii();
protected static int s_iAsciiLoF = "f".ToAscii();

protected static int s_iAsciiA = "a".ToAscii();
protected static int s_iAsciiF = "f".ToAscii();

//------------------------------------------------------------------------------------------------
//! Gets int values of hexadecimal string. Expected to be prefixed with "0x"
//! Ordered by most significant bits
static array<int> HexStringToInt(string hexString)
{
if (hexString.Substring(0, 2) != "0x")
return {};

int numHexChars = hexString.Length() - 2;
if (!float.AlmostEqual(Math.Mod(numHexChars, 8), 0))
hexString.TrimInPlace();
hexString.ToLower();
if (!hexString.StartsWith("0x"))
return {};

hexString = hexString.Substring(2, numHexChars);

int numBit32 = (hexString.Length()) / 8;
array<int> result = {};
result.Reserve(numBit32);
int nibbles = hexString.Length() - 2;
int values = ((nibbles + 7) & ~7) / 8;
result.Reserve(values);

for (int i_bit32 = 0; i_bit32 < numBit32; i_bit32++)
int byte = 0;
int value = 0;
int count = 0;
while(nibbles > 0)
{
int bit32;
int nibble = hexString.Get(nibbles + 1).ToAscii();

if (nibble >= s_iAscii0 && nibble <= s_iAscii9)
nibble -= s_iAscii0;
else
if (nibble >= s_iAsciiA && nibble <= s_iAsciiF)
nibble -= s_iAsciiA - 10;
else
return {}; //invalid character in string

value <<= 4;
value |= nibble;
--nibbles;

for (int i_bit4 = 8 * i_bit32; i_bit4 < 8 * (i_bit32 + 1); i_bit4++)
if (++count == 8 || nibbles == 0)
{
int bit4 = HexCharToInt(hexString, index: i_bit4);
if (bit4 < 0)
return result;
int rev = 0;
for(int i = 0; i < count; ++i)
{
rev = (rev << 4) + (value & 0xf);
value >>= 4;
}

bit32 = 16 * bit32 + bit4;
}

result.Insert(bit32);
result.InsertAt(rev, --values);
value = 0;
count = 0;
}
}

return result;
}

//------------------------------------------------------------------------------------------------
//! Gets int value of a character in a hexadecimal string
//! Returns -1 if parsing failed
//! \param index Index of the character, 0 by default.
static int HexCharToInt(string hexString, int index = 0)
{
int value = hexString.ToAscii(index);
if (value >= s_iAscii0 && value <= s_iAscii9)
value -= s_iAscii0;
else if (value >= s_iAsciiA && value <= s_iAsciiF)
value -= s_iAsciiA - 10;
else if (value >= s_iAsciiLoA && value <= s_iAsciiLoF)
value -= s_iAsciiLoA - 10;
else
return -1;

return value;
}
}