Skip to content
This repository has been archived by the owner on Mar 20, 2024. It is now read-only.

Commit

Permalink
testing a new update system
Browse files Browse the repository at this point in the history
  • Loading branch information
celedev97 committed Nov 16, 2023
1 parent bc1fd00 commit b95663b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 28 deletions.
5 changes: 5 additions & 0 deletions src/main/java/dev/cele/asa_sm/Const.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package dev.cele.asa_sm;

import org.apache.commons.lang.SystemUtils;

import java.io.File;
import java.net.URI;
import java.nio.file.Path;

public final class Const {
Expand All @@ -12,4 +16,5 @@ public final class Const {

public final static Path MOD_CACHE_DIR = DATA_DIR.resolve("mod_cache");

public final static Path TEMP_DIR = SystemUtils.getJavaIoTmpDir().toPath().resolve("asa_sm");
}
36 changes: 17 additions & 19 deletions src/main/java/dev/cele/asa_sm/services/SteamCMDService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.zip.ZipInputStream;

import static dev.cele.asa_sm.Const.ASA_STEAM_GAME_NUMBER;
import static dev.cele.asa_sm.Const.TEMP_DIR;

@Service
@RequiredArgsConstructor
Expand All @@ -25,8 +26,8 @@ public class SteamCMDService {
private final CommandRunnerService commandRunnerService;

private final String WINDOWS_URL = "https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip";
private final String STEAM_CMD_WIN_FOLDER = "steamcmd";
private final String STEAM_CMD_WIN_PATH = STEAM_CMD_WIN_FOLDER + File.separator + "steamcmd.exe";
private final Path STEAM_CMD_WIN_FOLDER = Path.of("steamcmd");
private final File STEAM_CMD_WIN_EXE_FILE = STEAM_CMD_WIN_FOLDER.resolve("steamcmd.exe").toFile();


@PostConstruct
Expand All @@ -37,7 +38,7 @@ public void init() {
var exists = false;

if(SystemUtils.IS_OS_WINDOWS) {
exists = new File(STEAM_CMD_WIN_PATH).exists();
exists = STEAM_CMD_WIN_EXE_FILE.exists();
} else if(SystemUtils.IS_OS_LINUX) {
var result = commandRunnerService.runCommand("which", "steamcmd");
exists = result.getExitCode() == 0 && result.getOutput().contains("steamcmd");
Expand Down Expand Up @@ -67,27 +68,26 @@ public void install() {
@SneakyThrows
private void windowsInstall() {
//get a temp dir
var tempDir = SystemUtils.getJavaIoTmpDir().getAbsolutePath() + File.separator + "asa_sm";
Files.createDirectories(Path.of(tempDir));
Files.createDirectories(TEMP_DIR);

//region download file from WINDOWS_URL to zipLocation
logger.info("Downloading SteamCMD...");
InputStream input = new URL(WINDOWS_URL).openStream();

//download file to temp dir
var zipLocation = tempDir + File.separator + "steamcmd.zip";
Files.copy(input, Path.of(zipLocation), StandardCopyOption.REPLACE_EXISTING);
var zipLocation = Files.createTempFile(TEMP_DIR, "steamcmd", ".zip");
Files.copy(input, zipLocation, StandardCopyOption.REPLACE_EXISTING);
input.close();

logger.info("Download complete. Saved to "+zipLocation);
//endregion

//region unzip file
logger.info("Unzipping "+zipLocation);
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipLocation));
logger.info("Unzipping "+zipLocation+" to "+STEAM_CMD_WIN_FOLDER);
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipLocation.toFile()));
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
var newFile = Path.of(tempDir + File.separator + zipEntry.getName());
var newFile = STEAM_CMD_WIN_FOLDER.resolve(zipEntry.getName());

if (zipEntry.isDirectory()) {
Files.createDirectories(newFile);
Expand All @@ -102,15 +102,13 @@ private void windowsInstall() {
}
//endregion

//region move steamcmd.exe to STEAM_CMD_WIN_PATH
var steamcmdDownloaded = Path.of(tempDir + File.separator + "steamcmd.exe");
var steamcmdTarget = Path.of(STEAM_CMD_WIN_PATH);

Files.createDirectories(steamcmdTarget.getParent());
Files.move(steamcmdDownloaded, steamcmdTarget);

logger.info("Moved steamcmd.exe to "+steamcmdTarget);
//endregion
if(STEAM_CMD_WIN_EXE_FILE.exists()){
logger.info("SteamCMD installed successfully");
}else{
logger.error("Error installing SteamCMD");
throw new RuntimeException("Error installing SteamCMD");
}
}
//endregion

Expand All @@ -134,7 +132,7 @@ public String[] downloadVerifyServerCommand(String guid){
Files.createDirectories(installDir);
var steamCMD = "steamcmd";
if(SystemUtils.IS_OS_WINDOWS) {
steamCMD = STEAM_CMD_WIN_PATH;
steamCMD = STEAM_CMD_WIN_EXE_FILE.getAbsolutePath();
}

// steamcmd +force_install_dir ..\server\guid +login anonymous +app_update 2430930 validate +quit
Expand Down
24 changes: 18 additions & 6 deletions src/main/java/dev/cele/asa_sm/services/UpdateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -127,14 +131,22 @@ private void downloadUpdate(Release latestRelease) {
.findFirst()
.orElseThrow(() -> new RuntimeException("No exe asset found"));

File file = new RestTemplate().execute(asset.getBrowser_download_url(), HttpMethod.GET, null, clientHttpResponse -> {
File ret = File.createTempFile("asa_sm_update", "tmp.exe");
StreamUtils.copy(clientHttpResponse.getBody(), new FileOutputStream(ret));
return ret;
});

log.info("Downloading update...");

InputStream input = new URL(asset.getBrowser_download_url()).openStream();
var tempFile = File.createTempFile("asa_sm_update", ".exe");
Files.copy(input, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
input.close();

//run cmd /c start "" "path/to/file.exe" /SILENT
Runtime.getRuntime().exec("cmd /c start \"\" \"" + file.getAbsolutePath() + "\" /SILENT");
log.info("Starting update process");
log.info("File path: " + tempFile.getAbsolutePath());

// wait 4 seconds, and then run the installer, meanwhile close the current app to avoid file locking
new ProcessBuilder(
"cmd", "/c", "START", "/min", "cmd", "/c", "timeout", "/t", "4", "/nobreak", ">nul", "&&", tempFile.getAbsolutePath(), "/SILENT"
).start();
System.exit(0);
}else{
//open browser
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/dev/cele/asa_sm/ui/frames/MainFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class MainFrame extends JFrame {
public MainFrame() {
setTitle("ASA Server Manager");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(new Dimension(825, 600));
setMinimumSize(new Dimension(825, 600));
setSize(new Dimension(1020, 890));
setLocationRelativeTo(null);
setLayout(new BorderLayout());

Expand Down Expand Up @@ -117,8 +117,6 @@ public MainFrame() {
addProfile();
}
});

pack();
}

private void addProfile() {
Expand Down

0 comments on commit b95663b

Please sign in to comment.