Files
unreal-launcher/utils.go
2025-10-03 16:09:13 +02:00

193 lines
4.1 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(projectFilePath)
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 cmd *exec.Cmd
projectFile := project.ProjectPath + "/" + project.ProjectName + ".uproject"
switch runtime.GOOS {
case "linux":
buildCmd := project.EnginePath + "/Engine/Build/BatchFiles/Linux/GenerateProjectFiles.sh"
cmd = exec.Command(buildCmd, projectFile, "-game")
case "darwin":
buildCmd := project.EnginePath + "/Engine/Build/BatchFiles/Mac/GenerateProjectFiles.sh"
cmd = exec.Command(buildCmd, projectFile, "-game")
case "windows":
ubtPath := project.EnginePath + "\\Engine\\Binaries\\DotNET\\UnrealBuildTool\\UnrealBuildTool.dll"
cmd = exec.Command(
"dotnet", ubtPath,
"-projectfiles",
"-project="+projectFile,
"-game",
"-progress",
)
default:
fmt.Println("Generate Project is not supported on this platform:", runtime.GOOS)
return
}
logToOutput(cmd)
}
func BuildUnrealSolution(project Project) {
var cmd *exec.Cmd
projectFile := project.ProjectPath + "/" + project.ProjectName + ".uproject"
switch runtime.GOOS {
case "linux":
cmd = exec.Command("make", "-C", project.ProjectPath, project.ProjectName)
case "windows":
buildBatch := project.EnginePath + "\\Engine\\Build\\BatchFiles\\Build.bat"
target := project.ProjectName + "Editor"
platform := "Win64"
config := "Development"
cmd = exec.Command(
buildBatch,
target,
platform,
config,
"-Project="+projectFile,
"-WaitMutex",
)
default:
fmt.Println("Build Project is not yet supported on this platform:", runtime.GOOS)
return
}
logToOutput(cmd)
}
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, prefix string) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
if prefix != "" {
Log(prefix + line)
} else {
Log(line)
}
}
}
go readPipe(stdout, "")
go readPipe(stderr, "[ERR] ")
// attendre la fin dans une goroutine => pas de blocage de l'UI
go func() {
if err := cmd.Wait(); err != nil {
Log("[FAIL] " + err.Error())
} else {
Log("[DONE] Process finished")
}
}()
}