Working on the packeteditor

This commit is contained in:
Marcel M. Otte 2023-04-11 16:05:24 +02:00
parent 262bef4e3b
commit 2cacca7a97
12 changed files with 311 additions and 6 deletions

View File

@ -11,9 +11,9 @@ import (
type NewObjectDialog struct {
representation *fyne.Container
// views
selection *fyne.Container // 4buttons grid
protocolChooser *container.AppTabs
packetChooser *fyne.Container
selection *fyne.Container // 4buttons grid
//protocolChooser *container.AppTabs
//packetChooser *fyne.Container
}
var pnod *NewObjectDialog

View File

@ -1,7 +1,47 @@
package ui
import "fyne.io/fyne/v2"
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"gitea.mmo.to/ProtocolPacketForger/ppf/packet"
"gitea.mmo.to/ProtocolPacketForger/ppf/packetctl"
)
type PacketEditor struct {
Representation *fyne.Container
FieldContainer *fyne.Container
Metadata *PacketMetadata
Fields []*PacketFieldEditor
ShowShortHints bool
Reference *packet.PacketStructure
}
func NewPacketEditor(ref *packet.PacketStructure) *PacketEditor {
metadata := NewMetadataPacket()
fields := container.NewGridWrap(fyne.NewSize(300, 200))
container := container.NewBorder(nil, nil, metadata.Representation, nil, container.NewVScroll(fields))
packetEditor := &PacketEditor{container, fields, metadata, []*PacketFieldEditor{}, false, ref}
packetEditor.Redraw()
return packetEditor
}
func (ed *PacketEditor) Redraw() {
json := packetctl.ToJson(ed.Reference)
fmt.Printf("%s\n", json)
ed.FieldContainer.RemoveAll()
// existing fields
k := 0
for i, v := range ed.Reference.Layers {
for j, vv := range v.Values {
ed.FieldContainer.Add(CreatePacketFieldEditor(ed, k, i, j, &vv).Representation)
k++
}
}
// metadata
//ed.Metadata.SetPacket(ed.Reference)
}

View File

@ -0,0 +1,53 @@
package ui
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"gitea.mmo.to/ProtocolPacketForger/ppf/packet"
"gitea.mmo.to/ProtocolPacketForger/ppf/packetctl"
)
type PacketFieldEditor struct {
Representation *fyne.Container
Value *widget.Entry
Reference *packet.FieldValue
GlobalIndex int
Index int
}
func CreatePacketFieldEditor(
ed *PacketEditor,
gblidx int,
lyridx int,
index int,
ref *packet.FieldValue,
) *PacketFieldEditor {
pfe := &PacketFieldEditor{}
pfe.Reference = ref
pfe.Index = index
pfe.GlobalIndex = gblidx
setfunc := func(s string) {
//todo
packetctl.UpdateField(&ed.Reference.Layers[lyridx], index, packetctl.NewFieldValue(s))
}
elements := []fyne.CanvasObject{}
elements = append(elements, widget.NewLabel(ref.GetProtocolField().Name))
if ed.ShowShortHints {
elements = append(elements, widget.NewLabel(ref.GetProtocolField().Regex)) // todo: implement real shorthints if feasible
}
// depending on the input stuff, entry for now
pfe.Value = widget.NewEntry()
pfe.Value.OnChanged = setfunc
elements = append(elements, pfe.Value)
pfe.Representation = container.NewHBox(elements...)
return pfe
}

View File

@ -0,0 +1,35 @@
package ui
import (
"fyne.io/fyne/v2/container"
"gitea.mmo.to/ProtocolPacketForger/ppf/packet"
"gitea.mmo.to/ProtocolPacketForger/ppf/packetctl"
)
// implements TabProvider, FileHandler
type PacketFileHandler struct {
PacketEditor *PacketEditor
name string
path string
changed bool
tab *container.TabItem
Reference *packet.PacketStructure
}
func NewPacketFileHandler() *PacketFileHandler {
pfh := PacketFileHandler{}
pfh.name = "*new"
pfh.changed = true
pfh.Reference = packetctl.NewPacketStructure()
pfh.PacketEditor = NewPacketEditor(pfh.Reference)
return &pfh
}
func (pfh *PacketFileHandler) SetTab(tab *container.TabItem) {
pfh.tab = tab
}
func (pfh *PacketFileHandler) Tab() *container.TabItem {
return pfh.tab
}

View File

@ -9,7 +9,7 @@ import (
"gitea.mmo.to/ProtocolPacketForger/ppf/protocolctl"
)
// implements FileHandler
// implements FileHandler, TabProvider
type ProtocolFileHandler struct {
ProtocolEditor *ProtocolEditor
name string

View File

@ -1 +1,18 @@
package packet
import "gitea.mmo.to/ProtocolPacketForger/ppf/protocol"
// implements protocol.ProtocolFieldReferencer
type FieldValue struct {
fieldReference *protocol.Field
Value string
}
func (fv *FieldValue) GetProtocolField() *protocol.Field {
return fv.fieldReference
}
func (fv *FieldValue) SetProtocolField(pf *protocol.Field) {
fv.fieldReference = pf
}

View File

@ -1 +1,25 @@
package packet
import "gitea.mmo.to/ProtocolPacketForger/ppf/protocol"
// implements protocol.ProtocolReferencer
type PacketLayer struct {
// needs a reference to a protocol
protocolReference *protocol.ProtocolStructure
ProtocolName string
Values []FieldValue
}
type PacketStructure struct {
MetaData PacketMetadata
Layers []PacketLayer
}
func (pl *PacketLayer) GetProtocol() *protocol.ProtocolStructure {
return pl.protocolReference
}
func (pl *PacketLayer) Setprotocol(ps *protocol.ProtocolStructure) {
pl.protocolReference = ps
}

4
packet/packetmetadata.go Normal file
View File

@ -0,0 +1,4 @@
package packet
type PacketMetadata struct {
}

View File

@ -1 +1,123 @@
package packetctl
import (
"encoding/json"
"io/fs"
"os"
"gitea.mmo.to/ProtocolPacketForger/ppf/packet"
)
func NewPacketStructure() *packet.PacketStructure {
p := packet.PacketStructure{}
return &p
}
func UpdateMetaData(
pack *packet.PacketStructure,
) {
// still empty
}
func NewPacketLayer() *packet.PacketLayer {
p := packet.PacketLayer{}
return &p
}
func NewEmptyFieldValue() *packet.FieldValue {
f := packet.FieldValue{}
return &f
}
func NewFieldValue(
value string,
) *packet.FieldValue {
f := packet.FieldValue{
Value: value,
}
return &f
}
func AppendField(pack *packet.PacketLayer, field *packet.FieldValue) int {
i := len(pack.Values)
pack.Values = append(pack.Values, *field)
return i + 1
}
func AddField(pack *packet.PacketLayer, index int, field *packet.FieldValue) {
if len(pack.Values) == index {
AppendField(pack, field)
return
}
ret := make([]packet.FieldValue, 0)
ret = append(ret, pack.Values[:index]...)
ret = append(ret, *field)
ret = append(ret, pack.Values[index:]...)
pack.Values = ret
}
func AppendLayer(pack *packet.PacketStructure, layer *packet.PacketLayer) {
pack.Layers = append(pack.Layers, *layer)
}
func Addlayer(pack *packet.PacketStructure, index int, layer *packet.PacketLayer) {
if len(pack.Layers) == index {
AppendLayer(pack, layer)
return
}
ret := make([]packet.PacketLayer, 0)
ret = append(ret, pack.Layers[:index]...)
ret = append(ret, *layer)
ret = append(ret, pack.Layers[index:]...)
pack.Layers = ret
}
func UpdateField(pack *packet.PacketLayer, e int, field *packet.FieldValue) {
pack.Values[e] = *field
}
func UpdateLayer(pack *packet.PacketStructure, e int, layer *packet.PacketLayer) {
pack.Layers[e] = *layer
}
func RemoveField(pack *packet.PacketLayer, e int) {
l := len(pack.Values) - 1
ret := make([]packet.FieldValue, l)
ret = append(ret, pack.Values[:e]...)
ret = append(ret, pack.Values[e+1:]...)
pack.Values = ret
}
func RemoveLayer(pack *packet.PacketStructure, e int) {
l := len(pack.Layers) - 1
ret := make([]packet.PacketLayer, l)
ret = append(ret, pack.Layers[:e]...)
ret = append(ret, pack.Layers[e+1:]...)
pack.Layers = ret
}
func Load(pack *packet.PacketStructure, path string) error {
data, err := os.ReadFile(path)
if err != nil {
return err
}
err = json.Unmarshal(data, pack)
return err
}
func ToJson(pack *packet.PacketStructure) string {
data, err := json.MarshalIndent(*pack, "", " ")
if err != nil {
return ""
}
return string(data)
}
func Save(pack *packet.PacketStructure, path string) error {
data, err := json.MarshalIndent(*pack, "", " ")
if err != nil {
return err
}
err = os.WriteFile(path, data, fs.ModeAppend)
return err
}

View File

@ -12,6 +12,11 @@ type Field struct {
Payload bool // Is this field the payload or next protocol level?
}
type ProtocolFieldReferencer interface {
GetProtocolField() *Field
SetProtocolField(f *Field)
}
func (f *Field) ToJson() string {
b, err := json.Marshal(*f)
if err != nil {

View File

@ -6,3 +6,8 @@ type ProtocolStructure struct {
DefaultValues []DefaultValue
JavaScript string
}
type ProtocolReferencer interface {
GetProtocol() *ProtocolStructure
SetProtocol(prot *ProtocolStructure)
}

View File

@ -59,7 +59,7 @@ func NewField(
func AppendField(prot *protocol.ProtocolStructure, field *protocol.Field) int {
i := len(prot.Structure)
prot.Structure = append(prot.Structure, field)
return i
return i + 1
}
func AddField(prot *protocol.ProtocolStructure, index int, field *protocol.Field) {
if len(prot.Structure) == index {