init
This commit is contained in:
131
loadrooms.go
Normal file
131
loadrooms.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var REQUIRED_IMAGES = []string{
|
||||
"ART",
|
||||
"MINI",
|
||||
"CREDIT",
|
||||
"INTERACT",
|
||||
"FLOOR",
|
||||
"LAYER",
|
||||
"ROOF",
|
||||
"WALL",
|
||||
}
|
||||
var ACCEPTED_IMAGE_EXT = []string{
|
||||
".PNG",
|
||||
".JPG",
|
||||
".BMP",
|
||||
}
|
||||
|
||||
var REQUIRED_AUDIO = []string{
|
||||
"FOOT",
|
||||
"MUSIC",
|
||||
}
|
||||
var ACCEPTED_AUDIO_EXT = []string{
|
||||
".WAV",
|
||||
".MP3",
|
||||
".OGG",
|
||||
".MIDI",
|
||||
}
|
||||
|
||||
type RoomFolder struct {
|
||||
path string
|
||||
cfg RoomIni
|
||||
}
|
||||
|
||||
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 {
|
||||
entrypath := filepath.Join(roomspath, entry.Name())
|
||||
if !entry.IsDir() {
|
||||
fmt.Printf("WARN: unexpected non-directory found in rooms directory: %s\n", entrypath)
|
||||
continue
|
||||
}
|
||||
|
||||
roomfolder, err := loadRoom(entrypath)
|
||||
if err != nil {
|
||||
fmt.Printf("ERROR: unable to load directory: %s\n", entrypath)
|
||||
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
|
||||
}
|
||||
|
||||
requiredImages := make([]string, len(REQUIRED_IMAGES))
|
||||
copy(requiredImages, REQUIRED_IMAGES)
|
||||
requiredAudio := make([]string, len(REQUIRED_AUDIO))
|
||||
copy(requiredAudio, REQUIRED_AUDIO)
|
||||
|
||||
roominiName := ""
|
||||
for _, entry := range entries {
|
||||
filename := entry.Name()
|
||||
if strings.ToLower(filename) == "room.ini" {
|
||||
roominiName = filename
|
||||
}
|
||||
|
||||
ext := strings.ToUpper(path.Ext(filename))
|
||||
name := strings.ToUpper(filename[:len(filename)-len(ext)])
|
||||
|
||||
if index := slices.Index(requiredImages, name); index > -1 && slices.Contains(ACCEPTED_IMAGE_EXT, ext) {
|
||||
requiredImages = append(requiredImages[:index], requiredImages[index+1:]...)
|
||||
}
|
||||
|
||||
if index := slices.Index(requiredAudio, name); index > -1 && slices.Contains(ACCEPTED_AUDIO_EXT, ext) {
|
||||
requiredAudio = append(requiredAudio[:index], requiredAudio[index+1:]...)
|
||||
}
|
||||
}
|
||||
|
||||
errormsg := ""
|
||||
if len(requiredImages) > 0 && len(requiredAudio) > 0 {
|
||||
errormsg = fmt.Sprintf("images: %v, audio: %v", requiredImages, requiredAudio)
|
||||
}
|
||||
if len(requiredImages) > 0 {
|
||||
errormsg = fmt.Sprintf("images: %v", requiredImages)
|
||||
}
|
||||
if len(requiredAudio) > 0 {
|
||||
errormsg = fmt.Sprintf("audio: %v", requiredAudio)
|
||||
}
|
||||
|
||||
if errormsg != "" {
|
||||
return RoomFolder{}, fmt.Errorf("unable to load room %s: missing required %s", roompath, errormsg)
|
||||
}
|
||||
|
||||
cfg := NewRoomIni()
|
||||
if roominiName != "" {
|
||||
newcfg, err := ReadIni(filepath.Join(roompath, "room.ini"))
|
||||
if err != nil {
|
||||
return RoomFolder{}, err
|
||||
}
|
||||
cfg = newcfg
|
||||
}
|
||||
|
||||
return RoomFolder{
|
||||
path: roompath,
|
||||
cfg: cfg,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user