custom palette taxonomy
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"golang.org/x/text/encoding/charmap"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -74,6 +75,16 @@ type tlkEntryData struct {
|
||||
SoundLength float32
|
||||
}
|
||||
|
||||
type standaloneTLKDocument struct {
|
||||
Schema string `yaml:"schema"`
|
||||
BaseStrref int `yaml:"base_strref"`
|
||||
Strings []struct {
|
||||
Key string `yaml:"key"`
|
||||
Text string `yaml:"text"`
|
||||
ID *int `yaml:"id"`
|
||||
} `yaml:"strings"`
|
||||
}
|
||||
|
||||
type tlkStateDocument struct {
|
||||
Version int `json:"version"`
|
||||
Language string `json:"language"`
|
||||
@@ -157,6 +168,42 @@ func newTLKCompiler(sourceDir string, legacy *legacyTLKData) (*tlkCompiler, erro
|
||||
return compiler, nil
|
||||
}
|
||||
|
||||
func loadStandaloneTLKStrings(sourceDir string, compiler *tlkCompiler) error {
|
||||
path := filepath.Join(sourceDir, "tlk", "custom.tlk.yml")
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("read %s: %w", path, err)
|
||||
}
|
||||
var document standaloneTLKDocument
|
||||
if err := yaml.Unmarshal(raw, &document); err != nil {
|
||||
return fmt.Errorf("parse %s: %w", path, err)
|
||||
}
|
||||
if document.Schema != "sow-topdata/tlk/v1" {
|
||||
return fmt.Errorf("%s: unsupported schema %q", path, document.Schema)
|
||||
}
|
||||
if document.BaseStrref != customTLKBase {
|
||||
return fmt.Errorf("%s: base_strref must be %d", path, customTLKBase)
|
||||
}
|
||||
seen := map[string]struct{}{}
|
||||
for index, entry := range document.Strings {
|
||||
entry.Key = strings.TrimSpace(entry.Key)
|
||||
if entry.Key == "" || entry.Text == "" || entry.ID == nil {
|
||||
return fmt.Errorf("%s: strings[%d] requires key, text, and id", path, index)
|
||||
}
|
||||
if _, ok := seen[entry.Key]; ok {
|
||||
return fmt.Errorf("%s: duplicate string key %q", path, entry.Key)
|
||||
}
|
||||
seen[entry.Key] = struct{}{}
|
||||
if err := compiler.registerInlineAtID(entry.Key, *entry.ID, tlkEntryData{Text: entry.Text}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadBaseDialogData(path string) (*legacyTLKData, error) {
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
@@ -329,6 +376,21 @@ func (c *tlkCompiler) registerInline(key string, entry tlkEntryData) (tlkCompile
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *tlkCompiler) registerInlineAtID(key string, id int, entry tlkEntryData) error {
|
||||
if id < 0 {
|
||||
return fmt.Errorf("TLK key %q has negative id %d", key, id)
|
||||
}
|
||||
if mapping, ok := c.state.Entries[key]; ok && mapping.ID != id {
|
||||
return fmt.Errorf("TLK key %q changed id from %d to %d", key, mapping.ID, id)
|
||||
}
|
||||
if owner, ok := c.reservedByID[id]; ok && owner != key {
|
||||
return fmt.Errorf("TLK id %d is already reserved by %q", id, owner)
|
||||
}
|
||||
c.state.Entries[key] = tlkStateMapping{ID: id}
|
||||
c.reservedByID[id] = key
|
||||
return c.markActive(key, entry)
|
||||
}
|
||||
|
||||
func (c *tlkCompiler) customStrrefForKey(key string) int {
|
||||
return customTLKBase + c.state.Entries[key].ID
|
||||
}
|
||||
@@ -338,6 +400,7 @@ func (c *tlkCompiler) markActive(key string, entry tlkEntryData) error {
|
||||
if !ok {
|
||||
mapping = tlkStateMapping{ID: c.allocateID(), Retired: false}
|
||||
c.state.Entries[key] = mapping
|
||||
c.reservedByID[mapping.ID] = key
|
||||
}
|
||||
existing, ok := c.active[key]
|
||||
if ok && existing != entry {
|
||||
@@ -351,9 +414,13 @@ func (c *tlkCompiler) markActive(key string, entry tlkEntryData) error {
|
||||
}
|
||||
|
||||
func (c *tlkCompiler) allocateID() int {
|
||||
id := c.nextID
|
||||
c.nextID++
|
||||
return id
|
||||
for {
|
||||
id := c.nextID
|
||||
c.nextID++
|
||||
if _, reserved := c.reservedByID[id]; !reserved {
|
||||
return id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *tlkCompiler) finish(outputDir, tlkName string) (int, error) {
|
||||
|
||||
Reference in New Issue
Block a user