added compatibility to factorio >= 1.1.14 savefiles

This commit is contained in:
knoxfighter 2021-02-02 01:40:09 +01:00
parent 29fe0650d3
commit be35959144
4 changed files with 66 additions and 12 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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")

Binary file not shown.