Skip to content

Commit 90daa7f

Browse files
committed
fix .old rename
1 parent 6c2bd2d commit 90daa7f

File tree

2 files changed

+77
-8
lines changed

2 files changed

+77
-8
lines changed

Framework/Intersect.Framework.Core/AssetManagement/Updater.cs

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -827,31 +827,54 @@ private bool StreamDownloads(UpdateManifest updateManifest, CancellationToken ca
827827
return false;
828828
}
829829

830+
var isSelf = IsSelf(currentFile.Path);
831+
if (isSelf)
832+
{
833+
ShouldRestart = true;
834+
835+
try
836+
{
837+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
838+
{
839+
temporaryFileInfo.UnixFileMode |= UnixFileMode.UserExecute;
840+
}
841+
}
842+
catch (Exception exception)
843+
{
844+
ApplicationContext.CurrentContext.Logger.LogWarning(exception, $"Failed to make {temporaryFileInfo.Name} executable");
845+
}
846+
}
847+
830848
try
831849
{
832850
if (targetFileInfo.Exists)
833-
{
834-
targetFileInfo.Delete();
851+
{
852+
ApplicationContext.CurrentContext.Logger.LogTrace(
853+
"Attempting to delete: {TargetFilePath}",
854+
relativePathToTargetFile
855+
);
856+
targetFileInfo.Delete();
835857
}
836858
}
837859
catch (Exception exception)
838860
{
839861
ApplicationContext.CurrentContext.Logger.LogWarning(
840862
exception,
841-
"Failed to delete {RelativeTargetPath}",
863+
"Failed to delete, falling back to rename {RelativeTargetPath}",
842864
relativePathToTargetFile
843865
);
844866

845867
FileInfo oldTargetFileInfo = new($"{targetFileInfo.FullName}.old");
846868
try
847869
{
848-
targetFileInfo.MoveTo(oldTargetFileInfo.FullName, true);
870+
FileInfo temporaryRenameInfo = new FileInfo(targetFileInfo.FullName);
871+
temporaryRenameInfo.MoveTo(oldTargetFileInfo.FullName, true);
849872
}
850873
catch (Exception moveToException)
851874
{
852875
ApplicationContext.CurrentContext.Logger.LogWarning(
853876
moveToException,
854-
"Failed to move {RelativeSourcePath} to {RelativeTargetPath}.old",
877+
"Failed to rename {RelativeSourcePath} to {RelativeTargetPath}.old",
855878
relativePathToTargetFile,
856879
relativePathToTargetFile
857880
);
@@ -874,6 +897,13 @@ private bool StreamDownloads(UpdateManifest updateManifest, CancellationToken ca
874897
}
875898
catch (Exception exception)
876899
{
900+
ApplicationContext.CurrentContext.Logger.LogTrace(
901+
exception,
902+
"Failed to move {SourcePath} to {TargetPath}",
903+
temporaryFileInfo.FullName,
904+
targetFileInfo.FullName
905+
);
906+
877907
ApplicationContext.CurrentContext.Logger.LogError(
878908
exception,
879909
"Failed to move {RelativeSourcePath} to {RelativeTargetPath}",
@@ -1189,7 +1219,8 @@ private bool DownloadUpdates(int workerThreadIndex, CancellationToken cancellati
11891219
return false;
11901220
}
11911221

1192-
if (IsSelf(currentFile.Path))
1222+
var isSelf = IsSelf(currentFile.Path);
1223+
if (isSelf)
11931224
{
11941225
ShouldRestart = true;
11951226

@@ -1210,12 +1241,36 @@ private bool DownloadUpdates(int workerThreadIndex, CancellationToken cancellati
12101241
{
12111242
if (targetFileInfo.Exists)
12121243
{
1244+
ApplicationContext.CurrentContext.Logger.LogTrace(
1245+
"Attempting to delete: {TargetFilePath}",
1246+
relativePathToTargetFile
1247+
);
12131248
targetFileInfo.Delete();
12141249
}
12151250
}
12161251
catch (Exception exception)
12171252
{
1218-
ApplicationContext.CurrentContext.Logger.LogWarning(exception, $"Failed to delete {relativePathToTargetFile}");
1253+
ApplicationContext.CurrentContext.Logger.LogWarning(
1254+
exception,
1255+
"Failed to delete, falling back to rename {RelativeTargetPath}",
1256+
relativePathToTargetFile
1257+
);
1258+
1259+
FileInfo oldTargetFileInfo = new($"{targetFileInfo.FullName}.old");
1260+
try
1261+
{
1262+
FileInfo temporaryRenameInfo = new FileInfo(targetFileInfo.FullName);
1263+
temporaryRenameInfo.MoveTo(oldTargetFileInfo.FullName, true);
1264+
}
1265+
catch (Exception moveToException)
1266+
{
1267+
ApplicationContext.CurrentContext.Logger.LogWarning(
1268+
moveToException,
1269+
"Failed to rename {RelativeSourcePath} to {RelativeTargetPath}.old",
1270+
relativePathToTargetFile,
1271+
relativePathToTargetFile
1272+
);
1273+
}
12191274
}
12201275

12211276
try
@@ -1228,13 +1283,20 @@ private bool DownloadUpdates(int workerThreadIndex, CancellationToken cancellati
12281283
}
12291284

12301285
#if DIAGNOSTIC
1231-
ApplicationContext.CurrentContext.Logger.LogTraceApplicationContext.CurrentContext.Logger.LogTrace($"Completed for {currentFile.Path}");
1286+
ApplicationContext.CurrentContext.Logger.LogTrace($"Completed for {currentFile.Path}");
12321287
#endif
12331288
currentResult.State = DownloadState.Completed;
12341289
_downloadedBytes += currentFile.Size;
12351290
}
12361291
catch (Exception exception)
12371292
{
1293+
ApplicationContext.CurrentContext.Logger.LogTrace(
1294+
exception,
1295+
"Failed to move {SourcePath} to {TargetPath}",
1296+
temporaryFileInfo.FullName,
1297+
targetFileInfo.FullName
1298+
);
1299+
12381300
ApplicationContext.CurrentContext.Logger.LogError(
12391301
exception,
12401302
$"Failed to move {relativePathToTemporaryFile} to {relativePathToTargetFile}"

Intersect.Client.Core/MonoGame/IntersectGame.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ private void IntersectInit()
205205
}
206206

207207
private TimeSpan _elapsedSincePlatformStatisticsRefresh;
208+
private bool _restartFailed;
208209

209210
/// <summary>
210211
/// Allows the game to run logic such as updating the world,
@@ -270,9 +271,15 @@ protected override void Update(GameTime gameTime)
270271
}
271272
case UpdateStatus.Restart:
272273
{
274+
if (_restartFailed)
275+
{
276+
break;
277+
}
278+
273279
if (!ProcessHelper.TryRelaunch())
274280
{
275281
ApplicationContext.CurrentContext.Logger.LogWarning("Failed to restart automatically");
282+
_restartFailed = true;
276283
}
277284

278285
break;

0 commit comments

Comments
 (0)