126 lines
3.6 KiB
Go
126 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"sync"
|
|
|
|
logger "git.site.quack-lab.dev/dave/cylogger"
|
|
)
|
|
|
|
var steamWorkshopContentDir = `C:/Users/Administrator/Seafile/Projects-Go/GoProjects/steamcmd-api/steamcmd/steamapps/workshop/content/445220`
|
|
var avorionRoaming = `C:/Users/Administrator/AppData/Roaming/Avorion`
|
|
|
|
func main() {
|
|
steamWorkshopContentDir, _ = filepath.Abs(steamWorkshopContentDir)
|
|
files, err := os.ReadDir(steamWorkshopContentDir)
|
|
if err != nil {
|
|
logger.Error("Error reading steam workshop content dir: %v", err)
|
|
return
|
|
}
|
|
|
|
wg := sync.WaitGroup{}
|
|
for _, file := range files {
|
|
wg.Add(1)
|
|
go func(file os.DirEntry) {
|
|
defer wg.Done()
|
|
|
|
if !file.IsDir() {
|
|
return
|
|
}
|
|
|
|
src := filepath.Join(steamWorkshopContentDir, file.Name())
|
|
modFile := filepath.Join(steamWorkshopContentDir, file.Name(), "modinfo.lua")
|
|
fileHandle, err := os.Open(modFile)
|
|
if err != nil {
|
|
if !os.IsNotExist(err) {
|
|
logger.Error("Error opening mod file '%s': '%v'", modFile, err)
|
|
return
|
|
} else {
|
|
logger.Info("Mod '%s' is a ship!", src)
|
|
dst := filepath.Join(avorionRoaming, "ships", "workshop", file.Name())
|
|
err2 := Copy(src, dst)
|
|
if err2 != nil {
|
|
logger.Error("Error copying '%s' to '%s': '%v'", src, dst, err2)
|
|
return
|
|
}
|
|
|
|
shipFileSrc := filepath.Join(steamWorkshopContentDir, file.Name(), "design.xml")
|
|
planFileSrc := filepath.Join(steamWorkshopContentDir, file.Name(), "plan.xml")
|
|
turretFileSrc := filepath.Join(steamWorkshopContentDir, file.Name(), "turretdesign.xml")
|
|
|
|
shipFileDst := filepath.Join(avorionRoaming, "ships", "ships", file.Name()+".ship.xml")
|
|
planFileDst := filepath.Join(avorionRoaming, "ships", "plans", file.Name()+".plan.xml")
|
|
turretFileDst := filepath.Join(avorionRoaming, "ships", "turrets", file.Name()+".turret.xml")
|
|
|
|
if FileExists(shipFileSrc) && !FileExists(shipFileDst) {
|
|
logger.Info("Copying '%s' to '%s'", shipFileSrc, shipFileDst)
|
|
err := Copy(shipFileSrc, shipFileDst)
|
|
if err != nil {
|
|
logger.Error("Error copying '%s' to '%s': '%v'", shipFileSrc, shipFileDst, err)
|
|
return
|
|
}
|
|
}
|
|
|
|
if FileExists(planFileSrc) && !FileExists(planFileDst) {
|
|
logger.Info("Copying '%s' to '%s'", planFileSrc, planFileDst)
|
|
err := Copy(planFileSrc, planFileDst)
|
|
if err != nil {
|
|
logger.Error("Error copying '%s' to '%s': '%v'", planFileSrc, planFileDst, err)
|
|
return
|
|
}
|
|
}
|
|
|
|
if FileExists(turretFileSrc) && !FileExists(turretFileDst) {
|
|
logger.Info("Copying '%s' to '%s'", turretFileSrc, turretFileDst)
|
|
err := Copy(turretFileSrc, turretFileDst)
|
|
if err != nil {
|
|
logger.Error("Error copying '%s' to '%s': '%v'", turretFileSrc, turretFileDst, err)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if err == nil {
|
|
logger.Info("'%s' is a mod, not a ship, skipping", src)
|
|
fileHandle.Close()
|
|
return
|
|
}
|
|
}(file)
|
|
}
|
|
wg.Wait()
|
|
}
|
|
|
|
func Copy(src, dst string) error {
|
|
cmd := exec.Command("cp", "-a", src, dst)
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return fmt.Errorf("error copying '%s' to '%s': %v\n%s", src, dst, err, string(out))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func FileExists(path string) bool {
|
|
fileHandle, err := os.Open(path)
|
|
if err == nil {
|
|
fileHandle.Close()
|
|
return true
|
|
}
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
_, err2 := os.Stat(path)
|
|
if err == nil {
|
|
return true
|
|
}
|
|
if os.IsNotExist(err2) {
|
|
return false
|
|
}
|
|
return false
|
|
}
|
|
return false
|
|
}
|