feat(i18n): localized quest text and per-lang quest cache

Phase B of #188. Quest JSON title/description/text_main/text_sub_a/
text_sub_b/success_cond/fail_cond/contractor now accept either a plain
string (existing behaviour) or a language-keyed object like
  "title": { "jp": "...", "en": "...", "fr": "..." }
CompileQuestJSON takes the compiling session's language and resolves
each field through a fallback chain (requested -> plain -> jp -> en ->
any non-empty), so existing single-language quest JSONs keep working
byte-for-byte unchanged.

The quest cache is re-keyed on (questID, language) so compiled binaries
for different languages never leak between sessions on a multi-language
server. loadQuestBinary and loadQuestFile now pass s.Lang() into both
the compiler and the cache.

ParseQuestBinary emits plain-string LocalizedStrings, so the
binary -> JSON -> binary round-trip still produces identical output.

The new LocalizedString type lives in its own file and is reusable by
phase C (scenarios, mail templates, shop text). Shift-JIS encoding
still applies to the wire format, so localized values must use
characters representable in Shift-JIS — ASCII, kana, CJK — which is
documented on the type.
This commit is contained in:
Houmgaor
2026-04-06 20:00:43 +02:00
parent 5b38bfde3f
commit f7ea275540
9 changed files with 506 additions and 75 deletions

View File

@@ -8,7 +8,7 @@ import (
func TestQuestCache_GetMiss(t *testing.T) {
c := NewQuestCache(60)
_, ok := c.Get(999)
_, ok := c.Get(999, "jp")
if ok {
t.Error("expected cache miss for unknown quest ID")
}
@@ -17,9 +17,9 @@ func TestQuestCache_GetMiss(t *testing.T) {
func TestQuestCache_PutGet(t *testing.T) {
c := NewQuestCache(60)
data := []byte{0xDE, 0xAD}
c.Put(1, data)
c.Put(1, "jp", data)
got, ok := c.Get(1)
got, ok := c.Get(1, "jp")
if !ok {
t.Fatal("expected cache hit")
}
@@ -30,9 +30,9 @@ func TestQuestCache_PutGet(t *testing.T) {
func TestQuestCache_Expiry(t *testing.T) {
c := NewQuestCache(0) // TTL=0 disables caching
c.Put(1, []byte{0x01})
c.Put(1, "jp", []byte{0x01})
_, ok := c.Get(1)
_, ok := c.Get(1, "jp")
if ok {
t.Error("expected cache miss when TTL is 0")
}
@@ -40,25 +40,44 @@ func TestQuestCache_Expiry(t *testing.T) {
func TestQuestCache_ExpiryElapsed(t *testing.T) {
c := &QuestCache{
data: make(map[int][]byte),
expiry: make(map[int]time.Time),
data: make(map[questCacheKey][]byte),
expiry: make(map[questCacheKey]time.Time),
ttl: 50 * time.Millisecond,
}
c.Put(1, []byte{0x01})
c.Put(1, "jp", []byte{0x01})
// Should hit immediately
if _, ok := c.Get(1); !ok {
if _, ok := c.Get(1, "jp"); !ok {
t.Fatal("expected cache hit before expiry")
}
time.Sleep(60 * time.Millisecond)
// Should miss after expiry
if _, ok := c.Get(1); ok {
if _, ok := c.Get(1, "jp"); ok {
t.Error("expected cache miss after expiry")
}
}
// TestQuestCache_LangIsolation verifies that entries for different languages
// of the same quest ID are stored independently (phase B of #188).
func TestQuestCache_LangIsolation(t *testing.T) {
c := NewQuestCache(60)
c.Put(1, "jp", []byte{0x01})
c.Put(1, "en", []byte{0x02})
if got, ok := c.Get(1, "jp"); !ok || got[0] != 0x01 {
t.Errorf("jp variant: got %v ok=%v, want [0x01] true", got, ok)
}
if got, ok := c.Get(1, "en"); !ok || got[0] != 0x02 {
t.Errorf("en variant: got %v ok=%v, want [0x02] true", got, ok)
}
// Unset language variant should miss.
if _, ok := c.Get(1, "fr"); ok {
t.Error("fr variant should miss when not populated")
}
}
func TestQuestCache_ConcurrentAccess(t *testing.T) {
c := NewQuestCache(60)
var wg sync.WaitGroup
@@ -67,11 +86,11 @@ func TestQuestCache_ConcurrentAccess(t *testing.T) {
id := i
go func() {
defer wg.Done()
c.Put(id, []byte{byte(id)})
c.Put(id, "jp", []byte{byte(id)})
}()
go func() {
defer wg.Done()
c.Get(id)
c.Get(id, "jp")
}()
}
wg.Wait()