Skip to content

Commit

Permalink
Handling self patch copy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
colincornaby committed Feb 4, 2024
1 parent 3f92820 commit e61e2d9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Sources/Plasma/Apps/plClient/Mac-Cocoa/PLSPatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ NS_ASSUME_NONNULL_BEGIN
@property(weak) id<PLSPatcherDelegate> delegate;
@property(readonly) BOOL selfPatched;

- (NSURL*)completeSelfPatch;
- (NSURL*)completeSelfPatch:(NSError **)error;
- (void)start;

@end
Expand Down
29 changes: 22 additions & 7 deletions Sources/Plasma/Apps/plClient/Mac-Cocoa/PLSPatcher.mm
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,36 @@ - (void)start
self.patcher->Start();
}

- (NSURL *)completeSelfPatch
- (NSURL *)completeSelfPatch:(NSError **)error;
{
NSString* destinationPath = [NSString stringWithSTString:plManifest::PatcherExecutable().AsString()];
NSURL* destinationURL = [NSURL fileURLWithPath:[NSString stringWithSTString:plManifest::PatcherExecutable().AsString()]];

NSError* errorInScope;

if ([NSFileManager.defaultManager fileExistsAtPath:destinationPath]) {
// need to swap
renamex_np(destinationURL.path.fileSystemRepresentation, self.updatedClientURL.path.fileSystemRepresentation, RENAME_SWAP);

// delete the old version - this is very likely us
// we want to terminate after. Our bundle will no longer be valid.
[NSFileManager.defaultManager removeItemAtURL:self.updatedClientURL error:nil];
int swapError = renamex_np(destinationURL.path.fileSystemRepresentation, self.updatedClientURL.path.fileSystemRepresentation, RENAME_SWAP);
if (swapError == 0) {
// delete the old version - this is very likely us
// we want to terminate after. Our bundle will no longer be valid.
[NSFileManager.defaultManager removeItemAtURL:self.updatedClientURL error:&errorInScope];
} else {
// abort and return an error
errorInScope = [NSError errorWithDomain:NSPOSIXErrorDomain code:swapError userInfo:nil];
}
} else {
// no executable already present! Just move things into place.
[NSFileManager.defaultManager moveItemAtURL:self.updatedClientURL toURL:destinationURL error:nil];
[NSFileManager.defaultManager moveItemAtURL:self.updatedClientURL toURL:destinationURL error:&errorInScope];
}

if (errorInScope) {
// Try to clean up if there was an error
[NSFileManager.defaultManager removeItemAtURL:self.updatedClientURL error:nil];
if (error) {
*error = errorInScope;
}
return nil;
}

return destinationURL;
Expand Down
14 changes: 13 additions & 1 deletion Sources/Plasma/Apps/plClient/Mac-Cocoa/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,19 @@ - (void)patcherCompleted:(PLSPatcher *)patcher didSelfPatch:(BOOL)selfPatched
[NSApp endModalSession:self.currentModalSession];
[self.patcherWindow.window close];
if (selfPatched) {
NSURL* finalURL = [patcher completeSelfPatch];
NSError *error;
NSURL* finalURL = [patcher completeSelfPatch:&error];

if (error) {
// uh oh, we couldn't self patch, present the error and bail
// this should be very rare and could be related to permissions issues
// we expect the game directory to be writable by all
NSAlert *errorAlert = [NSAlert alertWithError:error];
[errorAlert runModal];
[NSApp terminate:self];
// return just in case we ever reach here
return;
}

// Pass the "we've already patched" argument
NSArray* applicationArguments = [[[NSProcessInfo processInfo] arguments] arrayByAddingObject:@"-NoSelfPatch"];
Expand Down

0 comments on commit e61e2d9

Please sign in to comment.