1package netwalk23import (4 "encoding/json"5 "fmt"67 "github.com/FelineStateMachine/puzzletea/game"8)910type Save struct {11 Size int `json:"size"`12 Masks string `json:"masks"`13 Rotations string `json:"rotations"`14 InitialRotations string `json:"initial_rotations"`15 Kinds string `json:"kinds"`16 Locks string `json:"locks"`17 ModeTitle string `json:"mode_title"`18}1920func (m Model) GetSave() ([]byte, error) {21 save := Save{22 Size: m.puzzle.Size,23 Masks: encodeMaskRows(m.puzzle.Tiles),24 Rotations: encodeRotationRows(m.puzzle.Tiles, false),25 InitialRotations: encodeRotationRows(m.puzzle.Tiles, true),26 Kinds: encodeKindRows(m.puzzle.Tiles),27 Locks: encodeLockRows(m.puzzle.Tiles),28 ModeTitle: m.modeTitle,29 }30 data, err := json.Marshal(save)31 if err != nil {32 return nil, fmt.Errorf("marshal netwalk save: %w", err)33 }34 return data, nil35}3637func ImportModel(data []byte) (*Model, error) {38 var save Save39 if err := json.Unmarshal(data, &save); err != nil {40 return nil, fmt.Errorf("unmarshal netwalk save: %w", err)41 }4243 if save.Locks == "" {44 save.Locks = encodeRows(save.Size, func(_, _ int) byte { return '.' })45 }46 puzzle, err := decodePuzzle(save.Size, save.Masks, save.Rotations, save.InitialRotations, save.Kinds, save.Locks)47 if err != nil {48 return nil, err49 }5051 cursor := puzzle.firstActive()52 m := &Model{53 puzzle: puzzle,54 cursor: game.Cursor{X: cursor.X, Y: cursor.Y},55 keys: DefaultKeyMap,56 modeTitle: save.ModeTitle,57 }58 m.recompute()59 return m, nil60}