89 lines
1.8 KiB
Go
89 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
)
|
|
|
|
type RoomFolder struct {
|
|
Path string
|
|
Cfg RoomIni
|
|
IsDefaultCfg bool
|
|
}
|
|
|
|
func LoadRoomFolders(roomspath string) ([]RoomFolder, error) {
|
|
entries, err := os.ReadDir(roomspath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
rooms := make([]RoomFolder, 0)
|
|
|
|
for _, entry := range entries {
|
|
if entry.Name()[0] == '.' || !entry.IsDir() {
|
|
continue
|
|
}
|
|
|
|
entrypath := filepath.Join(roomspath, entry.Name())
|
|
roomfolder, err := loadRoom(entrypath)
|
|
if err != nil {
|
|
log.Printf("ERROR: unable to load directory: %s: %v", entrypath, err)
|
|
continue
|
|
}
|
|
|
|
rooms = append(rooms, roomfolder)
|
|
}
|
|
|
|
return rooms, nil
|
|
}
|
|
|
|
func loadRoom(roompath string) (RoomFolder, error) {
|
|
entries, err := os.ReadDir(roompath)
|
|
if err != nil {
|
|
return RoomFolder{}, err
|
|
}
|
|
|
|
minimalRequiredImages := gameRequiredImages()
|
|
|
|
roominiName := ""
|
|
for _, entry := range entries {
|
|
if entry.Name()[0] == '.' || entry.IsDir() {
|
|
continue
|
|
}
|
|
|
|
name, ext := SplitExt(entry.Name())
|
|
|
|
if name == "ROOM" && (ext == "INI" || ext == "TXT") {
|
|
roominiName = entry.Name()
|
|
}
|
|
|
|
if index := slices.Index(minimalRequiredImages, name); index > -1 && isAcceptedImageExt(ext) {
|
|
minimalRequiredImages = append(minimalRequiredImages[:index], minimalRequiredImages[index+1:]...)
|
|
}
|
|
}
|
|
|
|
if len(minimalRequiredImages) > 0 {
|
|
return RoomFolder{}, fmt.Errorf("unable to load room %s: missing required images: %v", roompath, minimalRequiredImages)
|
|
}
|
|
|
|
cfg := NewRoomIni()
|
|
defaultCfg := true
|
|
if roominiName != "" {
|
|
readcfg, err := ReadIni(filepath.Join(roompath, roominiName))
|
|
if err != nil {
|
|
return RoomFolder{}, err
|
|
}
|
|
cfg = readcfg
|
|
defaultCfg = false
|
|
}
|
|
|
|
return RoomFolder{
|
|
Path: roompath,
|
|
Cfg: cfg,
|
|
IsDefaultCfg: defaultCfg,
|
|
}, nil
|
|
}
|