Skip to content

Commit 1b804fa

Browse files
committed
added function for creating mod pack zip files
1 parent 6552488 commit 1b804fa

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ func main() {
9090
}
9191

9292
router := NewRouter()
93-
93+
createModPackDir()
94+
createModPack("test", "advanced-logistics-system_0.2.7.zip", "YARM_0.7.15.zip")
9495
fmt.Printf("Starting server on: %s:%s", config.ServerIP, config.ServerPort)
9596
log.Fatal(http.ListenAndServe(config.ServerIP+":"+config.ServerPort, router))
9697
}

src/mods.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"archive/zip"
45
"encoding/json"
56
"errors"
67
"fmt"
@@ -75,7 +76,7 @@ func rmMod(modName string) error {
7576
}
7677

7778
func createModPackDir() error {
78-
err := os.Mkdir(config.FactorioDir+"/modpacks", 0775)
79+
err := os.Mkdir(filepath.Join(config.FactorioDir, "modpacks"), 0775)
7980
if err != nil {
8081
log.Printf("Could not create modpacks directory: %s", err)
8182
return err
@@ -84,6 +85,53 @@ func createModPackDir() error {
8485
return nil
8586
}
8687

88+
// Create's modpack zip file from provided title, mods parameter is a string of mod filenames
89+
func createModPack(title string, mods ...string) error {
90+
zipfile, err := os.Create(filepath.Join(config.FactorioDir, "modpacks", title+".zip"))
91+
if err != nil {
92+
log.Printf("Error creating zipfile: %s, error: %s", title, err)
93+
}
94+
defer zipfile.Close()
95+
// Create Zip writer
96+
z := zip.NewWriter(zipfile)
97+
defer z.Close()
98+
99+
for _, mod := range mods {
100+
// Process mod file, add to zipfile
101+
f, err := os.Open(filepath.Join(config.FactorioDir, "mods", mod))
102+
if err != nil {
103+
log.Printf("Error creating modpack file %s for archival: ", mod, err)
104+
return err
105+
}
106+
// Read contents of mod to be compressed
107+
modfile, err := ioutil.ReadAll(f)
108+
if err != nil {
109+
log.Printf("Error reading modfile contents: %s", err)
110+
continue
111+
}
112+
// Add file to zip archive
113+
fmt.Println(mod)
114+
zip, err := z.Create(mod)
115+
if err != nil {
116+
log.Printf("Error adding file: %s to zip: %s", f.Name, err)
117+
continue
118+
}
119+
// Write file contents to zip archive
120+
_, err = zip.Write(modfile)
121+
if err != nil {
122+
log.Printf("Error writing to zipfile: %s", err)
123+
continue
124+
}
125+
}
126+
127+
err = z.Close()
128+
if err != nil {
129+
log.Printf("Error trying to zip: %s, error: %s", title, err)
130+
}
131+
132+
return nil
133+
}
134+
87135
// Parses mod-list.json file in factorio/mods
88136
// returns ModList struct
89137
func parseModList() (ModList, error) {

0 commit comments

Comments
 (0)