mirror of
https://github.com/OpenFactorioServerManager/factorio-server-manager.git
synced 2024-12-27 02:43:45 +02:00
added compatibility to factorio >= 1.1.14 savefiles
This commit is contained in:
parent
29fe0650d3
commit
be35959144
@ -311,15 +311,16 @@ func LoadModsFromSaveHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var saveFileStruct struct {
|
||||
Name string `json:"saveFile"`
|
||||
}
|
||||
|
||||
resp, err = ReadFromRequestBody(w, r, &saveFileStruct)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
config := bootstrap.GetConfig()
|
||||
path := filepath.Join(config.FactorioSavesDir, saveFileStruct.Name)
|
||||
f, err := factorio.OpenArchiveFile(path, "level.dat")
|
||||
f, err := factorio.OpenArchiveFile(path, "level.dat", "level-init.dat")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
resp = fmt.Sprintf("cannot open save level file: %v", err)
|
||||
log.Println(resp)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
|
@ -3,9 +3,9 @@ package factorio
|
||||
import (
|
||||
"archive/zip"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
type archiveFile struct {
|
||||
@ -27,7 +27,8 @@ func (af *archiveFile) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func OpenArchiveFile(path string, name string) (r io.ReadCloser, err error) {
|
||||
// openNames contains a list of all. Will stop searching at the first occurance
|
||||
func OpenArchiveFile(path string, openNames ...string) (r io.ReadCloser, err error) {
|
||||
archive, err := zip.OpenReader(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -36,18 +37,21 @@ func OpenArchiveFile(path string, name string) (r io.ReadCloser, err error) {
|
||||
f := &archiveFile{archive: archive}
|
||||
|
||||
for _, file := range archive.File {
|
||||
if file.FileInfo().Name() == name {
|
||||
f.ReadCloser, err = file.Open()
|
||||
if err != nil {
|
||||
archive.Close()
|
||||
return nil, err
|
||||
name := file.FileInfo().Name()
|
||||
for _, openName := range openNames {
|
||||
if name == openName {
|
||||
f.ReadCloser, err = file.Open()
|
||||
if err != nil {
|
||||
archive.Close()
|
||||
return nil, err
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
}
|
||||
|
||||
archive.Close()
|
||||
return nil, errors.New("file not found")
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
|
||||
type SaveHeader struct {
|
||||
@ -100,7 +104,7 @@ func (h *SaveHeader) ReadFrom(r io.Reader) (err error) {
|
||||
}
|
||||
}
|
||||
|
||||
// campaign, example: "transport-belt-madness
|
||||
// campaign, example: "transport-belt-madness"
|
||||
h.Campaign, err = readString(r, Version(h.FactorioVersion), false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("read Campaign: %v", err)
|
||||
|
@ -4,6 +4,55 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 1.1.14 changed the format of the saves, so new test has to be done
|
||||
func Test1_1_14(t *testing.T) {
|
||||
file, err := OpenArchiveFile("../factorio_testfiles/test_1_1_14.zip", "level-init.dat")
|
||||
if err != nil {
|
||||
t.Fatalf("Error opening level.datmetadata: %s", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var header SaveHeader
|
||||
err = header.ReadFrom(file)
|
||||
if err != nil {
|
||||
t.Fatalf("Error reading header: %s", err)
|
||||
}
|
||||
|
||||
testHeader := SaveHeader{
|
||||
FactorioVersion: Version{1, 1, 19, 0},
|
||||
Campaign: "transport-belt-madness",
|
||||
Name: "level-01",
|
||||
BaseMod: "base",
|
||||
Difficulty: 1,
|
||||
Finished: false,
|
||||
PlayerWon: false,
|
||||
NextLevel: "",
|
||||
CanContinue: false,
|
||||
FinishedButContinuing: false,
|
||||
SavingReplay: false,
|
||||
AllowNonAdminDebugOptions: true,
|
||||
LoadedFrom: Version{1, 1, 19},
|
||||
LoadedFromBuild: 57957,
|
||||
AllowedCommands: 1,
|
||||
Mods: []Mod{
|
||||
{
|
||||
Version: Version{1, 1, 19},
|
||||
Name: "base",
|
||||
},
|
||||
{
|
||||
Version: Version{3, 0, 0},
|
||||
Name: "belt-balancer",
|
||||
},
|
||||
{
|
||||
Version: Version{3, 0, 0},
|
||||
Name: "train-station-overview",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
header.Equals(testHeader, t)
|
||||
}
|
||||
|
||||
// 1.1 Binary seems equal to 0.18/1.0 binary, just the default values changed
|
||||
func Test1_1(t *testing.T) {
|
||||
file, err := OpenArchiveFile("../factorio_testfiles/test_1_1.zip", "level.dat")
|
||||
|
BIN
src/factorio_testfiles/test_1_1_14.zip
Normal file
BIN
src/factorio_testfiles/test_1_1_14.zip
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user