Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 41 additions & 30 deletions Assets/PatchKit Patcher/Scripts/Patcher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Diagnostics;
using System.Linq;
Expand Down Expand Up @@ -62,7 +62,7 @@ public static Patcher Instance

private Thread _thread;

private bool _isThreadBeingKilled;
private bool _isForceQuitting;

private App _app;

Expand Down Expand Up @@ -200,12 +200,6 @@ public void CancelUpdateApp()
public void Quit()
{
DebugLogger.Log("Quitting application.");
_canStartThread = false;

if (_wasUpdateSuccessfulOrNotNecessary && !_hasGameBeenStarted)
{
PatcherStatistics.DispatchSendEvent(PatcherStatistics.Event.PatcherSucceededClosed);
}

#if UNITY_EDITOR
if (Application.isEditor)
Expand All @@ -215,7 +209,6 @@ public void Quit()
else
#endif
{
CloseLockFile();
Application.Quit();
}
}
Expand All @@ -229,12 +222,15 @@ private void CloseLockFile()
_lockFileStream.Close();

DebugLogger.Log("Deleting the lock file.");
FileOperations.Delete(_data.Value.LockFilePath, CancellationToken.Empty);
if (File.Exists(_data.Value.LockFilePath))
{
FileOperations.Delete(_data.Value.LockFilePath, CancellationToken.Empty);
}
}
}
catch
catch(Exception e)
{
DebugLogger.LogWarning("Lock file closing error");
DebugLogger.LogWarning("Lock file closing error - " + e);
}
}

Expand Down Expand Up @@ -290,35 +286,44 @@ private void Update()

private void OnApplicationQuit()
{
if (_thread != null && _thread.IsAlive)
{
DebugLogger.Log("Cancelling application quit because patcher thread is alive.");

Application.CancelQuit();

StartCoroutine(KillThread());
}
Application.CancelQuit();
StartCoroutine(ForceQuit());
}

private IEnumerator KillThread()
private IEnumerator ForceQuit()
{
if (_isThreadBeingKilled)
if (_isForceQuitting)
{
yield break;
}

_isThreadBeingKilled = true;
_isForceQuitting = true;

DebugLogger.Log("Killing patcher thread...");
try
{
_canStartThread = false;

yield return StartCoroutine(KillThreadInner());
CloseLockFile();

DebugLogger.Log("Patcher thread has been killed.");
yield return StartCoroutine(KillThread());

_isThreadBeingKilled = false;
if (_wasUpdateSuccessfulOrNotNecessary && !_hasGameBeenStarted)
{
yield return StartCoroutine(PatcherStatistics.SendEvent(PatcherStatistics.Event.PatcherSucceededClosed));
}

if (!Application.isEditor)
{
Process.GetCurrentProcess().Kill();
}
}
finally
{
_isForceQuitting = false;
}
}

private IEnumerator KillThreadInner()
private IEnumerator KillThread()
{
if (_thread == null)
{
Expand Down Expand Up @@ -361,6 +366,8 @@ private IEnumerator KillThreadInner()
yield return null;
}
}

_thread = null;
}

private void StartThread()
Expand All @@ -375,8 +382,6 @@ private void CancelThread()
{
DebugLogger.Log("Cancelling patcher thread...");

PatcherStatistics.DispatchSendEvent(PatcherStatistics.Event.PatcherCanceled);

_threadCancellationTokenSource.Cancel();
}

Expand Down Expand Up @@ -853,6 +858,12 @@ private void ThreadUpdateApp(bool automatically, CancellationToken cancellationT
_wasUpdateSuccessfulOrNotNecessary = true;
}
}
catch (OperationCanceledException)
{
PatcherStatistics.DispatchSendEvent(PatcherStatistics.Event.PatcherCanceled);

throw;
}
finally
{
_state.Value = PatcherState.None;
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Invalid display of progress value when unarchiving
- Wrapping the GZipStream input to avoid errors
- Fixed all warnings that appear when launching on 5.3.4f1
- Freeze or crash after closing the patcher

### Removed
- Torrent downloading
Expand Down