2023-11-13 22:56:20 -08:00
|
|
|
package loading
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2023-12-13 23:48:53 -08:00
|
|
|
"github.com/NickyBoy89/spatial-db/storage"
|
2023-11-13 22:56:20 -08:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
saveOutputDir string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
LoadSaveDirCommand.Flags().StringVar(&saveOutputDir, "output", ".", "Where to place the converted save files")
|
|
|
|
}
|
|
|
|
|
|
|
|
var LoadSaveDirCommand = &cobra.Command{
|
|
|
|
Use: "worldsave <save-directory>",
|
|
|
|
Short: "Loads all the regions in the specified world's save directory",
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
// First, fetch a list of all the files that are in the specified directory
|
|
|
|
regionFiles, err := os.ReadDir(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Loading save directory of %s", args[0])
|
|
|
|
|
2023-12-11 09:56:50 -08:00
|
|
|
u, err := storage.CreateUnityFile(saveOutputDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer u.Close()
|
|
|
|
defer u.WriteMetadataFile(saveOutputDir + ".metadata")
|
|
|
|
|
2023-11-13 22:56:20 -08:00
|
|
|
for regionIndex, regionFile := range regionFiles {
|
|
|
|
if regionFile.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(regionFile.Name(), ".mcr") {
|
|
|
|
log.Warnf("The file %s is in the MCRegion format, skipping", regionFile.Name())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("Converting region file %s, [%d/%d]",
|
|
|
|
regionFile.Name(),
|
|
|
|
regionIndex,
|
|
|
|
len(regionFiles),
|
|
|
|
)
|
|
|
|
|
|
|
|
filePath := filepath.Join(args[0], regionFile.Name())
|
|
|
|
|
|
|
|
// Load each region file
|
|
|
|
chunks, err := LoadRegionFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save each chunk to a separate file
|
|
|
|
for _, chunk := range chunks {
|
2023-12-11 09:56:50 -08:00
|
|
|
if err := u.WriteChunk(chunk); err != nil {
|
2023-11-13 22:56:20 -08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|