read/write struct generic

This commit is contained in:
Pavel Merzlyakov 2023-09-01 01:37:06 +03:00
parent ac4c53305e
commit 62a85e0392
2 changed files with 42 additions and 0 deletions

View File

@ -77,6 +77,8 @@ type Struct interface {
Writable Writable
} }
type StructFactory func(classId uint32) (Struct, error)
type Bitmasked interface { type Bitmasked interface {
FieldChanged(index uint64) bool FieldChanged(index uint64) bool
SetFieldChanged(index uint64) SetFieldChanged(index uint64)

40
meta.go Normal file
View File

@ -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()
}