Skip to content

Commit

Permalink
Fix loading of sub-hit-locations
Browse files Browse the repository at this point in the history
  • Loading branch information
richardwilkes committed Oct 28, 2023
1 parent f990995 commit 700a6a7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
29 changes: 15 additions & 14 deletions model/gurps/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,21 @@ 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
}

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.
Expand All @@ -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.
Expand Down Expand Up @@ -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,
})
}

Expand Down
6 changes: 2 additions & 4 deletions ux/hit_location_settings_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 700a6a7

Please sign in to comment.