From 700a6a7facaff631b2d8443f1a6016cc5ce707a2 Mon Sep 17 00:00:00 2001 From: Richard Wilkes Date: Sat, 28 Oct 2023 16:41:26 -0700 Subject: [PATCH] Fix loading of sub-hit-locations --- model/gurps/body.go | 29 +++++++++++++++-------------- ux/hit_location_settings_panel.go | 6 ++---- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/model/gurps/body.go b/model/gurps/body.go index d740b90b9..e3ae8daad 100644 --- a/model/gurps/body.go +++ b/model/gurps/body.go @@ -40,12 +40,12 @@ type BodyData struct { Name string `json:"name,omitempty"` Roll *dice.Dice `json:"roll"` Locations []*HitLocation `json:"locations,omitempty"` - KeyPrefix string `json:"-"` } // Body holds a set of hit locations. type Body struct { BodyData + KeyPrefix string owningLocation *HitLocation locationLookup map[string]*HitLocation } @@ -53,7 +53,8 @@ type Body struct { type standaloneBodyData struct { Type string `json:"type"` Version int `json:"version"` - *Body + BodyData + OldHitLocations *Body `json:"hit_locations,omitempty"` } // BodyFor returns the Body for the given Entity, or the global settings if the Entity is nil. @@ -70,27 +71,27 @@ func FactoryBody() *Body { // NewBodyFromFile loads a Body from a file. func NewBodyFromFile(fileSystem fs.FS, filePath string) (*Body, error) { - var data struct { - standaloneBodyData - OldHitLocations *Body `json:"hit_locations"` - } + var data standaloneBodyData if err := jio.LoadFromFS(context.Background(), fileSystem, filePath, &data); err != nil { return nil, errs.NewWithCause(invalidFileDataMsg(), err) } + var body Body + body.BodyData = data.BodyData if data.Type != bodyTypeListTypeKey { - if data.OldHitLocations != nil { - data.Body = data.OldHitLocations - } else { + if data.OldHitLocations == nil { return nil, errs.New(unexpectedFileDataMsg()) } + body = *data.OldHitLocations + } else { + body.Update(nil) } if err := CheckVersion(data.Version); err != nil { return nil, err } if data.Version < noNeedForRewrapVersion { - data.Body.Rewrap() + body.Rewrap() } - return data.Body, nil + return &body, nil } // MarshalJSON implements json.Marshaler. @@ -135,9 +136,9 @@ func (b *Body) Clone(entity *Entity, owningLocation *HitLocation) *Body { // Save writes the Body to the file as JSON. func (b *Body) Save(filePath string) error { return jio.SaveToFile(context.Background(), filePath, &standaloneBodyData{ - Type: bodyTypeListTypeKey, - Version: CurrentDataVersion, - Body: b, + Type: bodyTypeListTypeKey, + Version: CurrentDataVersion, + BodyData: b.BodyData, }) } diff --git a/ux/hit_location_settings_panel.go b/ux/hit_location_settings_panel.go index ca3d4bd24..e0a351811 100644 --- a/ux/hit_location_settings_panel.go +++ b/ux/hit_location_settings_panel.go @@ -96,10 +96,8 @@ func (p *hitLocationSettingsPanel) createButtons() *unison.Panel { func (p *hitLocationSettingsPanel) addSubTable() { undo := p.dockable.prepareUndo(i18n.Text("Add Sub-Table")) p.loc.SubTable = &gurps.Body{ - BodyData: gurps.BodyData{ - Roll: dice.New("1d"), - KeyPrefix: p.dockable.targetMgr.NextPrefix(), - }, + BodyData: gurps.BodyData{Roll: dice.New("1d")}, + KeyPrefix: p.dockable.targetMgr.NextPrefix(), } p.loc.SubTable.SetOwningLocation(p.loc) p.loc.SubTable.Update(p.dockable.Entity())