Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a crash in Unreal Engine games when system language is Chinese #182

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 24 additions & 0 deletions PlayTools/Controls/PTFakeTouch/NSObject+Swizzle.m
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ - (void) hook_setCurrentSubscription:(VSSubscription *)currentSubscription {
// do nothing
}

- (NSString *)hook_stringByReplacingOccurrencesOfRegularExpressionPattern:(NSString *)pattern
withTemplate:(NSString *)template
options:(NSRegularExpressionOptions)options
range:(NSRange)range {
// If the string is empty, return immediately to prevent a range out-of-bounds error.
if ([(NSString*)self isEqualToString:@""]) {
return @"";
}
return [self hook_stringByReplacingOccurrencesOfRegularExpressionPattern:pattern
withTemplate:template
options:options
range:range];
}

// Hook for UIUserInterfaceIdiom

// - (long long) hook_userInterfaceIdiom {
Expand Down Expand Up @@ -249,6 +263,16 @@ + (void)load {
// [objc_getClass("UITraitCollection") swizzleInstanceMethod:@selector(userInterfaceIdiom) withMethod:@selector(hook_userInterfaceIdiom)];

[objc_getClass("VSSubscriptionRegistrationCenter") swizzleInstanceMethod:@selector(setCurrentSubscription:) withMethod:@selector(hook_setCurrentSubscription:)];

if (PlayInfo.isUnrealEngine) {
// Fix NSRegularExpression crash when system language is set to Chinese
CFStringEncoding encoding = CFStringGetSystemEncoding();
if (encoding == kCFStringEncodingMacChineseSimp || encoding == kCFStringEncodingMacChineseTrad) {
SEL origSelector = NSSelectorFromString(@"_stringByReplacingOccurrencesOfRegularExpressionPattern:withTemplate:options:range:");
SEL newSelector = @selector(hook_stringByReplacingOccurrencesOfRegularExpressionPattern:withTemplate:options:range:);
[objc_getClass("NSString") swizzleInstanceMethod:origSelector withMethod:newSelector];
}
}
}

@end
12 changes: 2 additions & 10 deletions PlayTools/PlayLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,8 @@ static void __attribute__((constructor)) initialize(void) {
[PlayCover launch];

if (ue_status == 0) {
NSURL* appFolder = [[NSBundle mainBundle] bundleURL];
NSArray* ueFiles = @[
[appFolder URLByAppendingPathComponent:@"ue4commandline.txt"],
[appFolder URLByAppendingPathComponent:@"uecommandline.txt"],
];

for (NSURL* ueFile in ueFiles) {
if (!access([[ueFile path] cStringUsingEncoding:NSUTF8StringEncoding], F_OK)) {
ue_status = 2;
}
if (PlayInfo.isUnrealEngine) {
ue_status = 2;
}
}

Expand Down
15 changes: 14 additions & 1 deletion PlayTools/Utils/PlayInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@

import Foundation

class PlayInfo {
class PlayInfo: NSObject {
static var isLauncherInstalled: Bool {
return AKInterface.shared!
.urlForApplicationWithBundleIdentifier("io.playcover.PlayCover") != nil
}

@objc static var isUnrealEngine: Bool {
let appFolder = Bundle.main.bundleURL
let ueFiles: [URL] = [
appFolder.appendingPathComponent("ue4commandline.txt"),
appFolder.appendingPathComponent("uecommandline.txt")
]

for ueFile in ueFiles where FileManager.default.fileExists(atPath: ueFile.path) {
return true
}
return false
}
}

extension ProcessInfo {
Expand Down