Files
unreal-launcher/utils.go
2025-03-31 13:35:32 +02:00

153 lines
3.2 KiB
Go

package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
)
type Project struct {
EnginePath string `json:"enginePath"`
ProjectPath string `json:"projectPath"`
ProjectName string `json:"projectName"`
Version string `json:"engineVersion"`
}
func LoadProjects(projectFilePath string) ([]Project, error) {
file, err := os.ReadFile(projectsFilePath)
if err != nil {
return nil, err
}
var projects []Project
if err := json.Unmarshal(file, &projects); err != nil {
return nil, err
}
return projects, nil
}
func removeDir(path string) {
err := os.RemoveAll(path)
if err != nil {
Log("Can't remove content at " + path)
} else {
Log("Removed " + path)
}
}
func CleanUnrealProject(project Project) {
dirs := []string {
"Binaries",
"DerivedDataCache",
"Intermediate",
"Saved",
"Script",
}
for _, dir := range dirs {
fullPath := filepath.Join(project.ProjectPath, dir)
removeDir(fullPath)
}
pluginsPath := filepath.Join(project.ProjectPath, "Plugins")
entries, err := os.ReadDir(pluginsPath)
if err != nil {
return
}
for _, entry := range entries {
if !entry.IsDir() {
continue
}
pluginDir := filepath.Join(pluginsPath, entry.Name())
removeDir(filepath.Join(pluginDir, "Binaries"))
removeDir(filepath.Join(pluginDir, "Intermediate"))
}
}
func GenerateUnrealSolution(project Project) {
var buildCmd string
switch runtime.GOOS {
case "linux":
buildCmd = project.EnginePath + "/Engine/Build/BatchFiles/Linux/GenerateProjectFiles.sh";
case "darwin":
buildCmd = project.EnginePath + "/Engine/Build/BatchFiles/Mac/GenerateProjectFiles.sh"
default:
fmt.Println("Generate Project is not yet supported on Windows")
return
}
projectCmd := project.ProjectPath + "/" + project.ProjectName + ".uproject"
cmd := exec.Command(buildCmd, projectCmd, "-game")
logToOutput(cmd)
}
func BuildUnrealSolution(project Project) {
switch runtime.GOOS {
case "linux":
cmd := exec.Command("make", "-C", project.ProjectPath, project.ProjectName)
logToOutput(cmd)
default:
fmt.Println("Build Project is not yet supported on Windows")
return
}
}
func RunUnrealProject(project Project) {
var buildCmd string
switch runtime.GOOS {
case "linux":
buildCmd = project.EnginePath + "/Engine/Binaries/Linux/UnrealEditor"
case "darwin":
buildCmd = project.EnginePath + "/Engine/Binaries/Mac/UnrealEditor.app/Contents/MacOS/UnrealEditor"
default:
buildCmd = project.EnginePath + "/Engine/Binaries/Win64/UnrealEditor.exe"
}
projectCmd := project.ProjectPath + "/" + project.ProjectName + ".uproject"
cmd := exec.Command(buildCmd, projectCmd)
logToOutput(cmd)
}
func logToOutput(cmd *exec.Cmd) {
stdout, err := cmd.StdoutPipe()
if err != nil {
fmt.Println("Error during pipe stdout creation")
}
stderr, err := cmd.StderrPipe()
if err != nil {
fmt.Println("Error during pipe stderr creation")
}
if err := cmd.Start(); err != nil {
fmt.Println("Error while loading command")
}
readPipe := func(reader io.ReadCloser) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
Log(line)
}
}
go readPipe(stdout)
go readPipe(stderr)
if err := cmd.Wait(); err != nil {
fmt.Println("Error")
}
}