@@ -180,6 +180,11 @@ func (m Music) Override(other Music) {
180180 }
181181}
182182
183+ // flattens dotted prefixes to underscore form, matching parseMusicSection.
184+ func normalizeMusicPrefix (prefix string ) string {
185+ return strings .ReplaceAll (prefix , "." , "_" )
186+ }
187+
183188// AppendParams parses comma-separated "key=value" pairs (as passed from
184189// Lua addChar/addStage/loadStart) and appends to the proper prefix list.
185190func (m Music ) AppendParams (entries []string ) {
@@ -190,10 +195,27 @@ func (m Music) AppendParams(entries []string) {
190195 value := strings .TrimSpace (c [eqPos + 1 :])
191196 prefix := ""
192197 field := key
193- if dotPos := strings .Index (key , "." ); dotPos != - 1 {
198+
199+ // Allow dots in prefix: split using the last ".<music anchor>" if present,
200+ // otherwise fall back to last dot.
201+ kl := strings .ToLower (key )
202+ anchors := []string {".bgmusic" , ".music" , ".bgm" }
203+ best := - 1
204+ for _ , a := range anchors {
205+ if i := strings .LastIndex (kl , a ); i > best {
206+ best = i
207+ }
208+ }
209+ if best >= 0 {
210+ prefix = strings .TrimSpace (key [:best ])
211+ field = strings .TrimSpace (key [best + 1 :]) // without leading dot
212+ } else if dotPos := strings .LastIndex (key , "." ); dotPos != - 1 {
194213 prefix = key [:dotPos ]
195214 field = key [dotPos + 1 :]
196215 }
216+
217+ // Flatten dotted prefixes to match storage in parseMusicSection.
218+ prefix = normalizeMusicPrefix (prefix )
197219 //fmt.Printf("[music] AppendParams: normalized key='%s' -> prefix='%s', field='%s', value='%s'\n", key, prefix, field, value)
198220
199221 // Ignore non-music fields
@@ -261,10 +283,20 @@ func (m Music) Read(key, def string) (string, int, int, int, int, int, float32,
261283 var loop , volume , loopstart , loopend , startposition , loopcount int = 1 , 100 , 0 , 0 , 0 , - 1
262284 var freqmul float32 = 1.0
263285 //fmt.Printf("[music] Read: key='%s' def='%s'\n", key, def)
286+ // Support dotted prefixes by only stripping a suffix when the key actually targets a music field.
264287 prefix := key
265- if dotPos := strings .Index (key , "." ); dotPos != - 1 {
266- prefix = key [:dotPos ]
288+ kl := strings .ToLower (key )
289+ anchors := []string {".bgmusic" , ".music" , ".bgm" }
290+ best := - 1
291+ for _ , a := range anchors {
292+ if i := strings .LastIndex (kl , a ); i > best {
293+ best = i
294+ }
295+ }
296+ if best >= 0 {
297+ prefix = key [:best ]
267298 }
299+ prefix = normalizeMusicPrefix (prefix )
268300 if len (m [prefix ]) > 0 {
269301 idx := int (RandI (0 , int32 (len (m [prefix ]))- 1 ))
270302 bgm = SearchFile (m [prefix ][idx ].bgmusic , []string {def , "" , "sound/" })
@@ -314,7 +346,8 @@ func (m Music) tryPlay(key, def string) bool {
314346 //if dot := strings.Index(key, "."); dot != -1 {
315347 // key = key[:dot]
316348 //}
317- lst , ok := m [key ]
349+ nkey := normalizeMusicPrefix (key )
350+ lst , ok := m [nkey ]
318351 if ! ok || len (lst ) == 0 {
319352 //fmt.Printf("[music] tryPlay: prefix '%s' not found or empty\n", key)
320353 return false
@@ -331,7 +364,7 @@ func (m Music) tryPlay(key, def string) bool {
331364 //fmt.Printf("[music] tryPlay: prefix '%s' has no defined bgmusic entries\n", key)
332365 return false
333366 }
334- ok = m .Play (key + ".bgmusic" , def )
367+ ok = m .Play (nkey + ".bgmusic" , def )
335368 //fmt.Printf("[music] tryPlay: Play('%s.bgmusic') -> %v\n", key, ok)
336369 return ok
337370}
0 commit comments