improved file read/copy logic
This commit is contained in:
95
loadrooms.go
95
loadrooms.go
@@ -2,43 +2,16 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"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
|
||||
Path string
|
||||
Cfg RoomIni
|
||||
IsDefaultCfg bool
|
||||
}
|
||||
|
||||
func LoadRoomFolders(roomspath string) ([]RoomFolder, error) {
|
||||
@@ -50,15 +23,14 @@ func LoadRoomFolders(roomspath string) ([]RoomFolder, error) {
|
||||
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)
|
||||
if entry.Name()[0] == '.' || !entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
entrypath := filepath.Join(roomspath, entry.Name())
|
||||
roomfolder, err := loadRoom(entrypath)
|
||||
if err != nil {
|
||||
fmt.Printf("ERROR: unable to load directory: %s\n", entrypath)
|
||||
log.Printf("ERROR: unable to load directory: %s: %v", entrypath, err)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -74,58 +46,43 @@ func loadRoom(roompath string) (RoomFolder, error) {
|
||||
return RoomFolder{}, err
|
||||
}
|
||||
|
||||
requiredImages := make([]string, len(REQUIRED_IMAGES))
|
||||
copy(requiredImages, REQUIRED_IMAGES)
|
||||
requiredAudio := make([]string, len(REQUIRED_AUDIO))
|
||||
copy(requiredAudio, REQUIRED_AUDIO)
|
||||
minimalRequiredImages := gameRequiredImages()
|
||||
|
||||
roominiName := ""
|
||||
for _, entry := range entries {
|
||||
filename := entry.Name()
|
||||
if strings.ToLower(filename) == "room.ini" {
|
||||
roominiName = filename
|
||||
if entry.Name()[0] == '.' || entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
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:]...)
|
||||
name, ext := SplitExt(entry.Name())
|
||||
|
||||
if name == "ROOM" && (ext == "INI" || ext == "TXT") {
|
||||
roominiName = entry.Name()
|
||||
}
|
||||
|
||||
if index := slices.Index(requiredAudio, name); index > -1 && slices.Contains(ACCEPTED_AUDIO_EXT, ext) {
|
||||
requiredAudio = append(requiredAudio[:index], requiredAudio[index+1:]...)
|
||||
|
||||
if index := slices.Index(minimalRequiredImages, name); index > -1 && isAcceptedImageExt(ext) {
|
||||
minimalRequiredImages = append(minimalRequiredImages[:index], minimalRequiredImages[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)
|
||||
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 != "" {
|
||||
newcfg, err := ReadIni(filepath.Join(roompath, "room.ini"))
|
||||
readcfg, err := ReadIni(filepath.Join(roompath, roominiName))
|
||||
if err != nil {
|
||||
return RoomFolder{}, err
|
||||
}
|
||||
cfg = newcfg
|
||||
cfg = readcfg
|
||||
defaultCfg = false
|
||||
}
|
||||
|
||||
return RoomFolder{
|
||||
path: roompath,
|
||||
cfg: cfg,
|
||||
Path: roompath,
|
||||
Cfg: cfg,
|
||||
IsDefaultCfg: defaultCfg,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user