Skip to content

Commit

Permalink
Merge pull request #29 from toppers/varray
Browse files Browse the repository at this point in the history
可変長配列対応!
  • Loading branch information
tmori authored Jun 14, 2024
2 parents b1df09b + 95ab511 commit 9f32ba7
Show file tree
Hide file tree
Showing 12 changed files with 336 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@

namespace Hakoniwa.PluggableAsset.Communication.Pdu.Raw
{
public struct HakoPduMetaDataType
{
public uint magicno;
public uint version;
public uint base_off;
public uint heap_off;
public uint total_size;
}

public class PduBinOffsetElmInfo
{
public bool is_array;
public bool is_varray;
public bool is_primitive;
public string field_name;
public string type_name;
Expand Down Expand Up @@ -102,6 +112,7 @@ static public void Parse(string filepath)
string[] attr = line.Split(':');
PduBinOffsetElmInfo elm = new PduBinOffsetElmInfo();
elm.is_array = attr[0].Equals("array");
elm.is_varray = attr[0].Equals("varray");
elm.is_primitive = attr[1].Equals("primitive");
elm.field_name = attr[2];
if (attr[3].Contains("/") || IsPrimitive(attr[3]))
Expand All @@ -113,14 +124,24 @@ static public void Parse(string filepath)
elm.type_name = package_name + "/" + attr[3];
}
elm.offset = int.Parse(attr[4]);
off_info.size = elm.offset + int.Parse(attr[5]);
if (attr.Length >= 7)
if (elm.is_array || elm.is_varray)
{
elm.array_size = int.Parse(attr[6]);
elm.elm_size = int.Parse(attr[5]) / elm.array_size;
if (elm.is_array)
{
off_info.size = elm.offset + int.Parse(attr[5]);
elm.array_size = int.Parse(attr[6]);
elm.elm_size = int.Parse(attr[5]) / elm.array_size;
}
else
{
off_info.size = elm.offset + int.Parse(attr[6]);
elm.array_size = -1;
elm.elm_size = int.Parse(attr[5]);
}
}
else
{
off_info.size = elm.offset + int.Parse(attr[5]);
elm.array_size = 1;
elm.elm_size = int.Parse(attr[5]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using Hakoniwa.Core.Utils.Logger;
using Hakoniwa.PluggableAsset.Assets.Robot.Parts;

namespace Hakoniwa.PluggableAsset.Communication.Pdu.Raw
{
class RawPduReaderConverter : IPduReaderConverter
{
HakoPduMetaDataType meta;
public IPduCommData ConvertToIoData(IPduReader src)
{
if (!src.IsValidData())
Expand All @@ -24,10 +26,16 @@ public void ConvertToPduData(IPduCommData src, IPduReader dst)
{
throw new InvalidOperationException("Error: Can not found offset: type=" + type_name);
}
ConvertFromStruct(off_info, 0, buffer, dst.GetWriteOps());
meta = new HakoPduMetaDataType();
meta.magicno = BitConverter.ToUInt32(buffer, 0);
meta.version = BitConverter.ToUInt32(buffer, 4);
meta.base_off = BitConverter.ToUInt32(buffer, 8);
meta.heap_off = BitConverter.ToUInt32(buffer, 12);
meta.total_size = BitConverter.ToUInt32(buffer, 16);
ConvertFromStruct(meta, off_info, ConstantValues.PduMetaDataSize, buffer, dst.GetWriteOps());
}

private static void ConvertFromStruct(PduBinOffsetInfo off_info, int base_off, byte[] src_buffer, IPduWriteOperation dst)
private static void ConvertFromStruct(HakoPduMetaDataType meta, PduBinOffsetInfo off_info, int base_off, byte[] src_buffer, IPduWriteOperation dst)
{
//SimpleLogger.Get().Log(Level.INFO, "TO PDU:Start Convert: package=" + off_info.package_name + " type=" + off_info.type_name);
foreach (var elm in off_info.elms)
Expand All @@ -37,43 +45,55 @@ private static void ConvertFromStruct(PduBinOffsetInfo off_info, int base_off, b
//primitive
if (elm.is_array)
{
ConvertFromPrimtiveArray(elm, base_off, src_buffer, dst);
ConvertFromPrimtiveArray(elm, base_off, elm.offset, elm.array_size, src_buffer, dst);
}
else if (elm.is_varray)
{
int array_size = BitConverter.ToInt32(src_buffer, base_off + elm.offset);
int offset_from_heap = BitConverter.ToInt32(src_buffer, base_off + elm.offset + 4);
ConvertFromPrimtiveArray(elm, (int)meta.heap_off, offset_from_heap, array_size, src_buffer, dst);
}
else
{
ConvertFromPrimtive(elm, base_off, src_buffer, dst);
ConvertFromPrimtive(elm, base_off, elm.offset, src_buffer, dst);
}
}
else
{
//struct
if (elm.is_array)
{
ConvertFromStructArray(elm, base_off + elm.offset, src_buffer, dst);
ConvertFromStructArray(meta, elm, base_off, elm.offset, elm.array_size, src_buffer, dst);
}
else if (elm.is_varray)
{
int array_size = BitConverter.ToInt32(src_buffer, base_off + elm.offset);
int offset_from_heap = BitConverter.ToInt32(src_buffer, base_off + elm.offset + 4);
ConvertFromStructArray(meta, elm, (int)meta.heap_off, offset_from_heap, array_size, src_buffer, dst);
}
else
{
PduBinOffsetInfo struct_off_info = PduOffset.Get(elm.type_name);
ConvertFromStruct(struct_off_info, base_off + elm.offset, src_buffer, dst.Ref(elm.field_name).GetPduWriteOps());
ConvertFromStruct(meta, struct_off_info, base_off + elm.offset, src_buffer, dst.Ref(elm.field_name).GetPduWriteOps());
}
}
}
}


private static void ConvertFromStructArray(PduBinOffsetElmInfo elm, int base_off, byte[] src_buffer, IPduWriteOperation dst)
private static void ConvertFromStructArray(HakoPduMetaDataType meta, PduBinOffsetElmInfo elm, int base_off, int elm_off, int array_size, byte[] src_buffer, IPduWriteOperation dst)
{
PduBinOffsetInfo struct_off_info = PduOffset.Get(elm.type_name);
for (int i = 0; i < elm.array_size; i++)
for (int i = 0; i < array_size; i++)
{
Pdu dst_data = dst.Refs(elm.field_name)[i];
ConvertFromStruct(struct_off_info, base_off + (i * elm.elm_size), src_buffer, dst_data.GetPduWriteOps());
ConvertFromStruct(meta, struct_off_info, (base_off + elm_off) + (i * elm.elm_size), src_buffer, dst_data.GetPduWriteOps());
}
}

private static void ConvertFromPrimtive(PduBinOffsetElmInfo elm, int base_off, byte[] src_buffer, IPduWriteOperation dst)
private static void ConvertFromPrimtive(PduBinOffsetElmInfo elm, int base_off, int elm_off, byte[] src_buffer, IPduWriteOperation dst)
{
var off = base_off + elm.offset;
var off = base_off + elm_off;
switch (elm.type_name)
{
case "int8":
Expand Down Expand Up @@ -120,10 +140,10 @@ private static void ConvertFromPrimtive(PduBinOffsetElmInfo elm, int base_off, b
throw new InvalidCastException("Error: Can not found ptype: " + elm.type_name);
}
}
private static void ConvertFromPrimtiveArray(PduBinOffsetElmInfo elm, int base_off, byte[] src_buffer, IPduWriteOperation dst)
private static void ConvertFromPrimtiveArray(PduBinOffsetElmInfo elm, int base_off, int elm_off, int array_size, byte[] src_buffer, IPduWriteOperation dst)
{
int roff = base_off + elm.offset;
for (int i = 0; i < elm.array_size; i++)
int roff = base_off + elm_off;
for (int i = 0; i < array_size; i++)
{
//SimpleLogger.Get().Log(Level.INFO, "field=" + elm.field_name);
//SimpleLogger.Get().Log(Level.INFO, "type=" + elm.type_name);
Expand Down
Loading

0 comments on commit 9f32ba7

Please sign in to comment.