Skip to content

Commit

Permalink
Merge pull request #1851 from wallisch/CATTY-734
Browse files Browse the repository at this point in the history
CATTY-734 Fix crashes and issues with iOS 17+ SDK
  • Loading branch information
wallisch authored Nov 15, 2024
2 parents 3ce7c37 + b2241fb commit d3f12e3
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/Catty/DataModel/Project/Project.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
+ (BOOL)isLastUsedProject:(NSString* _Nonnull)projectName projectID:(NSString* _Nonnull)projectID;
+ (void)setLastUsedProject:(Project* _Nonnull)project;
+ (NSString* _Nonnull)basePath;
+ (NSArray* _Nonnull)allProjectNames;
+ (NSArray<NSString*>* _Nonnull)allProjectNames;
+ (NSArray* _Nonnull)allProjectLoadingInfos;
+ (NSString* _Nonnull)projectDirectoryNameForProjectName:(NSString* _Nonnull)projectName
projectID:(NSString* _Nullable)projectID;
Expand Down
2 changes: 1 addition & 1 deletion src/Catty/DataModel/Project/Project.m
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ + (NSString*)basePath
}


+ (NSArray*)allProjectNames
+ (NSArray<NSString*>*)allProjectNames
{
NSArray *allProjectLoadingInfos = [[self class] allProjectLoadingInfos];
NSMutableArray *projectNames = [[NSMutableArray alloc] initWithCapacity:[allProjectLoadingInfos count]];
Expand Down
6 changes: 6 additions & 0 deletions src/Catty/Helpers/ImageCache/RuntimeImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ - (void)loadImageFromDiskWithPath:(NSString*)imagePath
{
dispatch_async(self.imageCacheQueue, ^{
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
if (image == nil) {
dispatch_sync(dispatch_get_main_queue(), ^{
completion(nil, imagePath);
});
return;
}

CGSize imageSize = CGSizeMake(size.width, size.height);
if (image.size.height > image.size.width)
Expand Down
2 changes: 0 additions & 2 deletions src/Catty/Helpers/Util/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ if (__functor) __functor(__VA_ARGS__); \

+ (NSString *_Nullable)normalizedDescriptionWithFormat:(NSString *_Nonnull)descriptionFormat formatParameter:(NSUInteger)formatParameter;

+ (NSString* _Nullable)uniqueName:(NSString* _Nullable)nameToCheck existingNames:(NSArray* _Nullable)existingNames;

+ (CGFloat)detectCBLanguageVersionFromXMLWithPath:(NSString* _Nullable)xmlPath;

+ (double)radiansToDegree:(double)rad;
Expand Down
61 changes: 0 additions & 61 deletions src/Catty/Helpers/Util/Util.m
Original file line number Diff line number Diff line change
Expand Up @@ -272,67 +272,6 @@ + (void)askUserForVariableNameAndPerformAction:(SEL)action
showWithController:[Util topmostViewController]];
}

+ (NSString*)uniqueName:(NSString*)nameToCheck existingNames:(NSArray*)existingNames
{
NSMutableString *uniqueName = [nameToCheck mutableCopy];
unichar lastChar = [uniqueName characterAtIndex:([uniqueName length] - 1)];
if (lastChar == 0x20) {
[uniqueName deleteCharactersInRange:NSMakeRange(([uniqueName length] - 1), 1)];
}

NSUInteger counter = 0;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\(\\d\\)"
options:NSRegularExpressionCaseInsensitive
error:NULL];
NSArray *results = [regex matchesInString:uniqueName
options:0
range:NSMakeRange(0, [uniqueName length])];
if ([results count]) {
BOOL duplicate = NO;
for (NSString *existingName in existingNames) {
if ([existingName isEqualToString:uniqueName]) {
duplicate = YES;
break;
}
}
if (! duplicate) {
return [uniqueName copy];
}
NSTextCheckingResult *lastOccurenceResult = [results lastObject];
NSMutableString *lastOccurence = [(NSString*)[uniqueName substringWithRange:lastOccurenceResult.range] mutableCopy];
[uniqueName replaceOccurrencesOfString:lastOccurence
withString:@""
options:NSCaseInsensitiveSearch
range:NSMakeRange(0, [uniqueName length])];
unichar lastChar = [uniqueName characterAtIndex:([uniqueName length] - 1)];
if (lastChar == 0x20) {
[uniqueName deleteCharactersInRange:NSMakeRange(([uniqueName length] - 1), 1)];
}
[lastOccurence replaceOccurrencesOfString:@"("
withString:@""
options:NSCaseInsensitiveSearch
range:NSMakeRange(0, [lastOccurence length])];
[lastOccurence replaceOccurrencesOfString:@")"
withString:@""
options:NSCaseInsensitiveSearch
range:NSMakeRange(0, [lastOccurence length])];
counter = [lastOccurence integerValue];
}
NSString *uniqueFinalName = [uniqueName copy];
BOOL duplicate;
do {
duplicate = NO;
for (NSString *existingName in existingNames) {
if ([existingName isEqualToString:uniqueFinalName]) {
uniqueFinalName = [NSString stringWithFormat:@"%@ (%lu)", uniqueName, (unsigned long)++counter];
duplicate = YES;
break;
}
}
} while (duplicate);
return uniqueFinalName;
}

+ (CGFloat)detectCBLanguageVersionFromXMLWithPath:(NSString*)xmlPath
{
NSError *error;
Expand Down
25 changes: 25 additions & 0 deletions src/Catty/Helpers/Util/Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,29 @@ func synchronized(lock: AnyObject, closure: () -> Void) {
}
return nil
}

static func uniqueName(_ nameToCheck: String, existingNames: [String]) -> String {
var baseName = nameToCheck.trimmingCharacters(in: .whitespaces)

if !existingNames.contains(baseName) {
return baseName
}

var counter = 0
let regex = try? NSRegularExpression(pattern: "\\((\\d+)\\)$", options: [])
if let match = regex?.firstMatch(in: baseName, options: [], range: NSRange(location: 0, length: baseName.count)) {
let numberStr = (baseName as NSString).substring(with: match.range(at: 1))
counter = Int(numberStr) ?? 0
baseName = (baseName as NSString).substring(to: match.range.location)
baseName = baseName.trimmingCharacters(in: .whitespaces)
}

var uniqueName: String
repeat {
counter += 1
uniqueName = "\(baseName) (\(counter))"
} while existingNames.contains(uniqueName)

return uniqueName
}
}
8 changes: 4 additions & 4 deletions src/Catty/IO/ProjectManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,18 @@
Util.alert(text: kLocalizedUnableToImportProject)
return nil
}
projectLoadingInfo.useOriginalName = true

guard let projectObject = Project(loadingInfo: projectLoadingInfo),
let newProjectName = Util.uniqueName(projectObject.header.programName, existingNames: Project.allProjectNames()) else {
projectLoadingInfo.useOriginalName = true
guard let projectObject = Project(loadingInfo: projectLoadingInfo) else {
Project.removeProjectFromDisk(withProjectName: tempProjectName, projectID: kNoProjectIDYetPlaceholder)
Util.alert(text: kLocalizedUnableToImportProject)
return nil
}
let newProjectName = Util.uniqueName(projectObject.header.programName, existingNames: Project.allProjectNames())

projectLoadingInfo.useOriginalName = false

let project = Project(loadingInfo: projectLoadingInfo)

project?.rename(toProjectName: newProjectName, andShowSaveNotification: false)

return project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,9 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*
self.selectedLookIndex = indexPath.row;
NSString *lookImagePath = [[self.object.lookList objectAtIndex:self.selectedLookIndex] pathForScene: self.object.scene];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:lookImagePath];
if (image == nil) {
return;
}
vc.editingImage = image;
vc.editingPath = lookImagePath;
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
Expand Down
2 changes: 1 addition & 1 deletion src/CattyTests/FileManager/ProjectManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ final class ProjectManagerTests: XCTestCase {

XCTAssertEqual(sumProjectNamesBefore + 1, sumProjectNamesAfter)

XCTAssertTrue((Project.allProjectNames() as! [String]).contains("Tic-Tac-Toe Master"))
XCTAssertTrue((Project.allProjectNames()).contains("Tic-Tac-Toe Master"))
}

func testAddProjectFromFileWithInvalidUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ final class StagePresenterSideMenuViewTests: XCTestCase {

var view: StagePresenterSideMenuView!
var project: Project!
var frame = CGRect(x: 0, y: 0, width: 1, height: 1)
var delegateMock: StagePresenterSideMenuDelegate!

override func setUp() {
Expand All @@ -45,21 +46,21 @@ final class StagePresenterSideMenuViewTests: XCTestCase {
func testPortrait() {
project.header.landscapeMode = false

view = StagePresenterSideMenuView(frame: .zero, delegate: delegateMock)
view = StagePresenterSideMenuView(frame: frame, delegate: delegateMock)
XCTAssertFalse(view.landscape)
}

func testLandscape() {
project.header.landscapeMode = true

view = StagePresenterSideMenuView(frame: .zero, delegate: delegateMock)
view = StagePresenterSideMenuView(frame: frame, delegate: delegateMock)
XCTAssertTrue(view.landscape)
}

func testAspectRatioMinimize() {
project.header.screenMode = kCatrobatHeaderScreenModeMaximize

let view = StagePresenterSideMenuView(frame: .zero, delegate: delegateMock)
let view = StagePresenterSideMenuView(frame: frame, delegate: delegateMock)

XCTAssertNotNil(view.aspectRatioButton)
XCTAssertNotNil(view.aspectRatioLabel)
Expand All @@ -71,7 +72,7 @@ final class StagePresenterSideMenuViewTests: XCTestCase {
func testAspectRatioMaximize() {
project.header.screenMode = kCatrobatHeaderScreenModeStretch

let view = StagePresenterSideMenuView(frame: .zero, delegate: delegateMock)
let view = StagePresenterSideMenuView(frame: frame, delegate: delegateMock)

XCTAssertNotNil(view.aspectRatioButton)
XCTAssertNotNil(view.aspectRatioLabel)
Expand All @@ -85,7 +86,7 @@ final class StagePresenterSideMenuViewTests: XCTestCase {
project.header.screenWidth = NSNumber(value: Util.screenWidth(true))
project.header.screenHeight = NSNumber(value: Util.screenHeight(true))

let view = StagePresenterSideMenuView(frame: .zero, delegate: delegateMock)
let view = StagePresenterSideMenuView(frame: frame, delegate: delegateMock)

XCTAssertTrue(view.aspectRatioButton!.isHidden)
XCTAssertTrue(view.aspectRatioLabel!.isHidden)
Expand All @@ -96,7 +97,7 @@ final class StagePresenterSideMenuViewTests: XCTestCase {
project.header.screenHeight = NSNumber(value: Util.screenHeight(true))
project.header.landscapeMode = true

let view = StagePresenterSideMenuView(frame: .zero, delegate: delegateMock)
let view = StagePresenterSideMenuView(frame: frame, delegate: delegateMock)

XCTAssertFalse(view.aspectRatioButton!.isHidden)
XCTAssertTrue(view.aspectRatioLabel == nil)
Expand All @@ -107,14 +108,14 @@ final class StagePresenterSideMenuViewTests: XCTestCase {
project.header.screenHeight = NSNumber(value: Util.screenWidth(true))
project.header.landscapeMode = true

let view = StagePresenterSideMenuView(frame: .zero, delegate: delegateMock)
let view = StagePresenterSideMenuView(frame: frame, delegate: delegateMock)

XCTAssertTrue(view.aspectRatioButton!.isHidden)
XCTAssertTrue(view.aspectRatioLabel == nil)
}

func testRestart() {
let view = StagePresenterSideMenuView(frame: .zero, delegate: delegateMock)
let view = StagePresenterSideMenuView(frame: frame, delegate: delegateMock)
XCTAssertFalse(view.landscape)

project.header.landscapeMode = true
Expand All @@ -127,7 +128,7 @@ final class StagePresenterSideMenuViewTests: XCTestCase {
project.header.screenWidth = NSNumber(value: Util.screenWidth(true))
project.header.screenHeight = 10

let view = StagePresenterSideMenuView(frame: .zero, delegate: delegateMock)
let view = StagePresenterSideMenuView(frame: frame, delegate: delegateMock)
XCTAssertFalse(view.aspectRatioButton!.isHidden)
XCTAssertFalse(view.aspectRatioLabel!.isHidden)

Expand Down

0 comments on commit d3f12e3

Please sign in to comment.