From 62a85e0392e48491d6a16bedca58ae71651ed116 Mon Sep 17 00:00:00 2001 From: Pavel Merzlyakov Date: Fri, 1 Sep 2023 01:37:06 +0300 Subject: [PATCH] read/write struct generic --- interface.go | 2 ++ meta.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 meta.go diff --git a/interface.go b/interface.go index 952e712..42c4ba9 100644 --- a/interface.go +++ b/interface.go @@ -77,6 +77,8 @@ type Struct interface { Writable } +type StructFactory func(classId uint32) (Struct, error) + type Bitmasked interface { FieldChanged(index uint64) bool SetFieldChanged(index uint64) diff --git a/meta.go b/meta.go new file mode 100644 index 0000000..8f84c57 --- /dev/null +++ b/meta.go @@ -0,0 +1,40 @@ +package meta + +func ReadStructGeneric(r Reader, createStruct StructFactory, field string) (Struct, error) { + if err := r.BeginContainer(field); err != nil { + return nil, err + } + + var classId uint32 + if err := r.ReadUint32(&classId, ""); err != nil { + return nil, err + } + + s, err := createStruct(classId) + if err != nil { + return nil, err + } + + if err := s.Read(r); err != nil { + return nil, err + } + + if err := r.EndContainer(); err != nil { + return nil, err + } + + return s, nil +} + +func WriteStructGeneric(w Writer, s Struct, field string) error { + if err := w.BeginCollection(2, field); err != nil { + return err + } + if err := w.WriteUint32(s.ClassId(), ""); err != nil { + return err + } + if err := s.Write(w); err != nil { + return err + } + return w.EndCollection() +}