Skip to content

Commit

Permalink
fix: automatic update errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Laeng committed Jun 15, 2024
1 parent b09c555 commit ceec008
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 82 deletions.
2 changes: 1 addition & 1 deletion SCTools/SCTool_Redesigned/Properties/app.manifest
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
이전 버전과의 호환성을 위해 애플리케이션에 가상화가 필요한 경우
이 요소를 제거합니다.
-->
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
<applicationRequestMinimum>
<defaultAssemblyRequest permissionSetReference="Custom" />
Expand Down
47 changes: 31 additions & 16 deletions SCTools/SCTool_Redesigned/Resources/update.bat
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
@echo off
setlocal

set workpath=%~dp0
set updatepath=%workpath%updates\
set latestpath=%updatepath%latest\
REM 현재 실행 중인 배치파일의 위치를 변수로 저장
set current_dir=%~dp0
set update_dir=%current_dir%updates

timeout 1
::xcopy "%latestpath%*.*" "%workpath%" /s /k /h /y
if not errorlevel 0 goto update_error
timeout /t 1

del "%updatepath%latest.json"
::del "%updatepath%latest.zip"
::del /q "%latestpath%*"
::for /d %%p in ("%latestpath%*.*") do rmdir /s /q "%%p"
::rmdir /s /q "%latestpath%"
REM 새 버전의 shatagon.exe 파일을 update 폴더에서 복사
if exist "%update_dir%\Shatagon.exe" (
echo Update the new Shatagon.exe file...
move /y "%update_dir%\Shatagon.exe" "%current_dir%Shatagon.exe"
if %errorlevel% equ 0 (
echo The update completed successfully.
set "update_status=0"
) else (
echo The update failed.
set "update_status=1"
)
) else (
echo The update file was not found.
set "update_status=1"
goto :end
)

start "" "%workpath%Shatagon.exe" update_status 0
exit
REM update 폴더 및 그 안의 모든 파일을 삭제
if exist "%update_dir%" (
echo Delete the ''updates' older...
rmdir /s /q "%update_dir%"
)

:update_error
:end
REM shatagon.exe 파일을 update_status 파라미터와 함께 실행
echo Run Shatagon.exe with the value update_status %update_status%...
start "" "%current_dir%shatagon.exe" update_status %update_status%

start "" "%workpath%Shatagon.exe" update_status 1"
exit
endlocal
73 changes: 8 additions & 65 deletions SCTools/SCTool_Redesigned/Update/CustomApplicationUpdater.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Compression;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NLog;
using NSW.StarCitizen.Tools.Lib.Helpers;
using NSW.StarCitizen.Tools.Lib.Update;
Expand All @@ -21,11 +15,8 @@ public class CustomApplicationUpdater : IDisposable
private readonly string _updateScriptContent;
private readonly string _updateScriptPath;
private readonly string _updatesStoragePath;
private readonly string _schedInstallArchivePath;
private readonly string _schedInstallExecutablePath;
private readonly string _schedInstallFilePath;
private readonly string _schedInstallJsonPath;
private readonly string _installUnpackedDir;
private readonly string _installUnpackedExecutablePath;
private readonly string _currentVersion;

public interface IPackageVerifier
Expand Down Expand Up @@ -68,11 +59,8 @@ public CustomApplicationUpdater(IUpdateRepository updateRepository, string execu
_packageVerifier = packageVerifier;
_updateScriptPath = Path.Combine(_executableDir, "update.bat");
_updatesStoragePath = Path.Combine(_executableDir, "updates");
_schedInstallArchivePath = Path.Combine(_updatesStoragePath, "latest.zip");
_schedInstallExecutablePath = Path.Combine(_updatesStoragePath, "Shatagon.exe");
_schedInstallFilePath = Path.Combine(_updatesStoragePath, "shatagon.exe");
_schedInstallJsonPath = Path.Combine(_updatesStoragePath, "latest.json");
_installUnpackedDir = Path.Combine(_updatesStoragePath, "latest");
_installUnpackedExecutablePath = Path.Combine(_updatesStoragePath, "latest", "Shatagon.exe");
_currentVersion = _updateRepository.CurrentVersion;
}

Expand Down Expand Up @@ -105,7 +93,7 @@ public async Task<string> DownloadVersionAsync(UpdateInfo version, CancellationT
public InstallUpdateStatus InstallScheduledUpdate()
{
_logger.Info("Install scheduled update");
if (ExtractReadyInstallUpdate() && ExtractUpdateScript())
if (ExtractUpdateScript())
{
using var updateProcess = new Process();
updateProcess.StartInfo.UseShellExecute = false;
Expand All @@ -128,7 +116,7 @@ public InstallUpdateStatus InstallScheduledUpdate()
return InstallUpdateStatus.ExtractFilesError;
}

public UpdateInfo? GetScheduledUpdateInfo() => File.Exists(_schedInstallArchivePath) ? JsonHelper.ReadFile<GitHubUpdateInfo>(_schedInstallJsonPath) : null;
public UpdateInfo? GetScheduledUpdateInfo() => File.Exists(_schedInstallFilePath) ? JsonHelper.ReadFile<GitHubUpdateInfo>(_schedInstallJsonPath) : null;

public bool IsAlreadyInstalledVersion(UpdateInfo updateInfo) =>
string.Compare(updateInfo.GetVersion(), _currentVersion, StringComparison.OrdinalIgnoreCase) == 0;
Expand All @@ -137,7 +125,7 @@ public bool IsAlreadyInstalledVersion(UpdateInfo updateInfo) =>

public bool ScheduleInstallUpdate(UpdateInfo updateInfo, string filePath)
{
_logger.Info($"Shedule install update with version: {updateInfo.GetVersion()}");
_logger.Info($"Schedule install update with version: {updateInfo.GetVersion()}");
if (File.Exists(filePath))
{
_updateRepository.SetCurrentVersion(_currentVersion);
Expand All @@ -147,11 +135,6 @@ public bool ScheduleInstallUpdate(UpdateInfo updateInfo, string filePath)
{
Directory.CreateDirectory(_updatesStoragePath);
}
if (File.Exists(_schedInstallExecutablePath))
{
File.Delete(_schedInstallExecutablePath);
}
File.Move(filePath, _schedInstallExecutablePath);
if (JsonHelper.WriteFile(_schedInstallJsonPath, updateInfo))
{
_updateRepository.SetCurrentVersion(updateInfo.GetVersion());
Expand All @@ -176,8 +159,8 @@ public bool CancelScheduleInstallUpdate()
_updateRepository.SetCurrentVersion(_currentVersion);
if (File.Exists(_schedInstallJsonPath))
FileUtils.DeleteFileNoThrow(_schedInstallJsonPath);
return File.Exists(_schedInstallExecutablePath) &&
FileUtils.DeleteFileNoThrow(_schedInstallExecutablePath);
return File.Exists(_schedInstallFilePath) &&
FileUtils.DeleteFileNoThrow(_schedInstallFilePath);
}

public void RemoveUpdateScript()
Expand All @@ -201,45 +184,5 @@ private bool ExtractUpdateScript()
}
return true;
}

private bool ExtractReadyInstallUpdate()
{
var installUnpackedDir = new DirectoryInfo(_installUnpackedDir);
//var extractTempDir = new DirectoryInfo(Path.Combine(_updatesStoragePath, "temp_" + Path.GetRandomFileName()));
try
{
if (installUnpackedDir.Exists && !FileUtils.DeleteDirectoryNoThrow(installUnpackedDir, true))
{
_logger.Error($"Already exist extract directory can't be removed: {_installUnpackedDir}");
return false;
}

//updates/latest.zip
//updates/temp_random
//updates/lastest
//

//using var archive = ZipFile.OpenRead(_schedInstallArchivePath);

//extractTempDir.Create();
//archive.ExtractToDirectory(extractTempDir.FullName);
//if (!_packageVerifier.VerifyPackage(extractTempDir.FullName))
// throw new NotSupportedException("Not supported upgrade package");
//Directory.Move(extractTempDir.FullName, _installUnpackedDir);

Directory.CreateDirectory(_installUnpackedDir);
File.Copy(_schedInstallExecutablePath, _installUnpackedExecutablePath);
}
catch (Exception e)
{
_logger.Error(e, $"Failed extract update package to: {_installUnpackedDir}");
//if (extractTempDir.Exists)
// FileUtils.DeleteDirectoryNoThrow(extractTempDir, true);
if (installUnpackedDir.Exists)
FileUtils.DeleteDirectoryNoThrow(installUnpackedDir, true);
return false;
}
return true;
}
}
}
}

0 comments on commit ceec008

Please sign in to comment.