Fixed bugs with byte[] sizes

This commit is contained in:
C9Glax 2023-02-08 18:00:19 +01:00
parent 8093ad5dfd
commit 31b2810178

View File

@ -91,11 +91,13 @@ public static class ByteConverter
public static byte[] GetBytes(OsmNode node)
{
int totalBytes = 4 + 8; //size + Coordinates
int totalBytes = sizeof(int) + 8; //size + Coordinates
HashSet<byte[]> edges = new();
foreach (OsmEdge edge in node.edges)
{
edges.Add(GetBytes(edge));
byte[] edgeBytes = GetBytes(edge);
edges.Add(edgeBytes);
totalBytes += edgeBytes.Length;
}
byte[] retBytes = new byte[totalBytes];
@ -158,6 +160,7 @@ public static class ByteConverter
tagsSize += tagBytes.Length;
}
totalBytes += tagsSize;
byte[] retBytes = new byte[totalBytes];
Array.Copy(BitConverter.GetBytes(tagsSize), retBytes, 4);
Array.Copy(GetBytes(edge.neighborCoordinates), 0, retBytes, sizeof(int), 8);
@ -206,32 +209,37 @@ public static class ByteConverter
public static byte[] GetBytes(KeyValuePair<OsmEdge.tagType, object> tag)
{
byte[] objectBytes = Array.Empty<byte>();
if (tag.Value.GetType() == Type.GetType("string"))
byte[] objectBytes;
Type objectType = tag.Value.GetType();
if (objectType == Type.GetType("System.String"))
{
objectBytes = Encoding.ASCII.GetBytes((string)tag.Value);
}
else if (tag.Value.GetType() == Type.GetType("bool"))
else if (objectType == Type.GetType("System.Boolean"))
{
objectBytes = BitConverter.GetBytes((bool)tag.Value);
}
else if (tag.Value.GetType() == Type.GetType("int"))
else if (objectType == Type.GetType("System.Int32"))
{
objectBytes = BitConverter.GetBytes((int)tag.Value);
}
else if (tag.Value.GetType() == Type.GetType("ulong"))
else if (objectType == Type.GetType("System.UInt64"))
{
objectBytes = BitConverter.GetBytes((ulong)tag.Value);
}
else if (tag.Value.GetType() == Type.GetType("byte"))
else if (objectType == Type.GetType("System.Byte") || objectType.Name == "wayType")
{
objectBytes = new[] { (byte)tag.Value };
}
else
{
throw new Exception(string.Format("Tag-Bytes object-Type: {0}", tag.Value.GetType()));
}
byte[] retBytes = new byte[sizeof(int) + 1 + objectBytes.Length];
Array.Copy(BitConverter.GetBytes(objectBytes.Length), 0, retBytes, 0, sizeof(int));
retBytes[sizeof(int)] = (byte)tag.Key;
Array.Copy(objectBytes, 0, retBytes, sizeof(int) + 1, retBytes.Length);
Array.Copy(objectBytes, 0, retBytes, sizeof(int) + 1, objectBytes.Length);
return retBytes;
}