Skip to content

Commit

Permalink
Merge pull request #4299 from osmandapp/fixesCloudBundledAssets4.9
Browse files Browse the repository at this point in the history
Fixes cloud bundled assets4.9
  • Loading branch information
vshcherb authored Jan 15, 2025
2 parents 0462328 + 2d6c7dc commit 363fd0b
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 172 deletions.
8 changes: 8 additions & 0 deletions OsmAnd.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,8 @@
78AC698C23AB7BE1006C3DDE /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 78AC698823AB7BDF006C3DDE /* [email protected] */; };
78AC698E23AB7BE1006C3DDE /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 78AC698923AB7BE0006C3DDE /* [email protected] */; };
81AE516126298A3600BDF947 /* libGeographicLib_STATIC.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 81AE516026298A3600BDF947 /* libGeographicLib_STATIC.a */; };
81E0C9432D37DE5B00BC7D85 /* bundled_assets.json in Resources */ = {isa = PBXBuildFile; fileRef = 81E0C9422D37DE5A00BC7D85 /* bundled_assets.json */; };
81E0C9462D37DFCD00BC7D85 /* OABundledAsetts.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81E0C9452D37DFC700BC7D85 /* OABundledAsetts.swift */; };
840C9FB61CF353C500F2F991 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 84BA1DCF1CF338E400BF3D96 /* [email protected] */; };
840C9FB81CF353C800F2F991 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 84BA1DD01CF338E400BF3D96 /* [email protected] */; };
8410D068207F48200049DEB2 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 8410D062207F481E0049DEB2 /* [email protected] */; };
Expand Down Expand Up @@ -4248,6 +4250,8 @@
78AC698823AB7BDF006C3DDE /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "[email protected]"; path = "Resources/Icons/[email protected]"; sourceTree = "<group>"; };
78AC698923AB7BE0006C3DDE /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "[email protected]"; path = "Resources/Icons/[email protected]"; sourceTree = "<group>"; };
81AE516026298A3600BDF947 /* libGeographicLib_STATIC.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libGeographicLib_STATIC.a; sourceTree = BUILT_PRODUCTS_DIR; };
81E0C9422D37DE5A00BC7D85 /* bundled_assets.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; name = bundled_assets.json; path = ../resources/bundled_assets.json; sourceTree = "<group>"; };
81E0C9452D37DFC700BC7D85 /* OABundledAsetts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OABundledAsetts.swift; sourceTree = "<group>"; };
8401FCF61EE46B7F00238915 /* ca */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ca; path = ca.lproj/Localizable.strings; sourceTree = "<group>"; };
8401FCF71EE46C2600238915 /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Localizable.strings"; sourceTree = "<group>"; };
8401FCF81EE46C3100238915 /* zh-Hant */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hant"; path = "zh-Hant.lproj/Localizable.strings"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -9913,6 +9917,7 @@
C51BE6FC2BBC3E7C00B63A1A /* PrivacyInfo.xcprivacy */,
C5735AF42AFE9CB500638715 /* InfoPlist.strings */,
BB451DF318D8F7D7005FBE09 /* Localizable.strings */,
81E0C9422D37DE5A00BC7D85 /* bundled_assets.json */,
);
name = Resources;
sourceTree = "<group>";
Expand Down Expand Up @@ -12874,6 +12879,7 @@
DA5A802F26C563A500F274C7 /* Data */ = {
isa = PBXGroup;
children = (
81E0C9452D37DFC700BC7D85 /* OABundledAsetts.swift */,
DA5A803226C563A500F274C7 /* Coordinates */,
DA5A803726C563A500F274C7 /* OAAppData.h */,
DA5A803E26C563A500F274C7 /* OAAppData.m */,
Expand Down Expand Up @@ -14115,6 +14121,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
81E0C9432D37DE5B00BC7D85 /* bundled_assets.json in Resources */,
32309A4426989180005751F5 /* [email protected] in Resources */,
32B50ADF26BBE3C7002E6C92 /* [email protected] in Resources */,
84F3A9631B4ECF2700D16EBC /* [email protected] in Resources */,
Expand Down Expand Up @@ -17155,6 +17162,7 @@
46548DAA2BF7794F00B0D3C0 /* DefaultMapButtonsViewController.swift in Sources */,
DA5A847B26C563A900F274C7 /* OATurnDrawable.mm in Sources */,
FAF6C89F2AD014C900A3ED94 /* ThemeManager.swift in Sources */,
81E0C9462D37DFCD00BC7D85 /* OABundledAsetts.swift in Sources */,
DAD5B4CF288E80EC009CF611 /* OAOperationLog.m in Sources */,
46C8412D2C32F23A00E284B0 /* OAShowHideAirPressureAction.m in Sources */,
DA5A853826C563A900F274C7 /* OAFavoritesHelper.mm in Sources */,
Expand Down
124 changes: 6 additions & 118 deletions Sources/AppHost/OsmAndAppImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Copyright (c) 2014 OsmAnd. All rights reserved.
//

#import "OsmAnd_Maps-Swift.h"
#import "OsmAndAppImpl.h"
#import <UIKit/UIKit.h>
#import "OsmAndApp.h"
Expand Down Expand Up @@ -297,27 +298,6 @@ - (void) runOpeningHoursParserTests
OpeningHoursParser::runTestAmPmArabic();
}

- (void) migrateResourcesToDocumentsIfNeeded
{
BOOL movedRes = [self moveContentsOfDirectory:[_dataPath stringByAppendingPathComponent:RESOURCES_DIR]
toDest:[_documentsPath stringByAppendingPathComponent:RESOURCES_DIR]
removeOriginalFile:YES];
BOOL movedSqlite = [self moveContentsOfDirectory:[_dataPath stringByAppendingPathComponent:MAP_CREATOR_DIR]
toDest:[_documentsPath stringByAppendingPathComponent:MAP_CREATOR_DIR]
removeOriginalFile:YES];
if (movedRes)
[self migrateMapNames:[_documentsPath stringByAppendingPathComponent:RESOURCES_DIR]];
if (movedRes || movedSqlite)
_resourcesManager->rescanUnmanagedStoragePaths(true);

[self moveContentsOfDirectory:[[NSBundle mainBundle] pathForResource:CLR_PALETTE_DIR ofType:nil]
toDest:_colorsPalettePath
removeOriginalFile:NO];
[self moveContentsOfDirectory:[[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:MODEL_3D_DIR]
toDest:[_documentsPath stringByAppendingPathComponent:MODEL_3D_DIR]
removeOriginalFile:NO];
}

- (BOOL) initializeCore
{
@synchronized (self)
Expand Down Expand Up @@ -417,6 +397,7 @@ - (BOOL) initializeImpl
_data = [OAAppData defaults];
[defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:_data]
forKey:kAppData];

[defaults setBool:NO forKey:@"hide_all_gpx"];
[defaults setBool:NO forKey:@"reset_settings"];
[defaults setBool:NO forKey:@"reset_routing"];
Expand Down Expand Up @@ -585,7 +566,10 @@ - (BOOL) initializeImpl
if (_terminating)
return NO;

[self migrateResourcesToDocumentsIfNeeded];
BOOL rescan = [BundledAssets.shared migrateResourcesToDocumentsIfNeededWithDataPath:_dataPath documentsPath:_documentsPath
versionChanged: currentVersion != prevVersion || _firstLaunch];
if (rescan)
_resourcesManager->rescanUnmanagedStoragePaths(true);

// Copy regions.ocbf to Documents/Resources if needed
NSString *ocbfPathBundle = [[NSBundle mainBundle] pathForResource:@"regions" ofType:@"ocbf"];
Expand Down Expand Up @@ -745,102 +729,6 @@ - (BOOL) initializeImpl
return YES;
}

- (BOOL) moveContentsOfDirectory:(NSString *)src
toDest:(NSString *)dest
removeOriginalFile:(BOOL)remove
{
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:src])
return NO;
if (![fm fileExistsAtPath:dest])
[fm createDirectoryAtPath:dest withIntermediateDirectories:YES attributes:nil error:nil];

NSArray *files = [fm contentsOfDirectoryAtPath:src error:nil];
BOOL tryAgain = NO;
for (NSString *file in files)
{
if ([fm fileExistsAtPath:[dest stringByAppendingPathComponent:file]])
continue;
NSError *err = nil;
if (remove)
{
[fm moveItemAtPath:[src stringByAppendingPathComponent:file]
toPath:[dest stringByAppendingPathComponent:file]
error:&err];
}
else
{
[fm copyItemAtPath:[src stringByAppendingPathComponent:file]
toPath:[dest stringByAppendingPathComponent:file]
error:&err];
}
if (err)
tryAgain = YES;
}
if (remove && !tryAgain)
[fm removeItemAtPath:src error:nil];
return YES;
}

- (void) migrateMapNames:(NSString *)path
{
NSFileManager *fm = [NSFileManager defaultManager];
BOOL isDirectory = NO;
[fm fileExistsAtPath:path isDirectory:&isDirectory];
if (!isDirectory)
return;

NSArray *files = [fm contentsOfDirectoryAtPath:path error:nil];

for (NSString *file in files)
{
NSString *oldPath = [path stringByAppendingPathComponent:file];
BOOL isDir = NO;
[fm fileExistsAtPath:oldPath isDirectory:&isDir];
if (isDir)
[self migrateMapNames:oldPath];
else
{
NSString *newPath = [path stringByAppendingPathComponent:[self generateCorrectFileName:file]];
if (![newPath isEqualToString:oldPath])
{
[fm moveItemAtPath:oldPath
toPath:newPath
error:nil];
}
}
}
}

- (NSString *) generateCorrectFileName:(NSString *)path
{
NSString *fileName = path.lastPathComponent;
if ([fileName hasSuffix:@".map.obf"])
{
fileName = [OAUtilities capitalizeFirstLetter:[fileName stringByReplacingOccurrencesOfString:@".map.obf" withString:@".obf"]];
}
else if ([fileName.pathExtension isEqualToString:@"obf"])
{
fileName = [OAUtilities capitalizeFirstLetter:fileName];
}
else if ([fileName.pathExtension isEqualToString:@"sqlitedb"])
{
if ([fileName hasSuffix:@".hillshade.sqlitedb"])
{
fileName = [fileName stringByReplacingOccurrencesOfString:@".hillshade.sqlitedb" withString:@".sqlitedb"];
fileName = [fileName stringByReplacingOccurrencesOfString:@"_" withString:@" "];
fileName = [NSString stringWithFormat:@"Hillshade %@", [OAUtilities capitalizeFirstLetter:fileName]];
}
else if ([fileName hasSuffix:@".slope.sqlitedb"])
{
fileName = [fileName stringByReplacingOccurrencesOfString:@".slope.sqlitedb" withString:@".sqlitedb"];
fileName = [fileName stringByReplacingOccurrencesOfString:@"_" withString:@" "];
fileName = [NSString stringWithFormat:@"Slope %@", [OAUtilities capitalizeFirstLetter:fileName]];
}
}
return [path.stringByDeletingLastPathComponent stringByAppendingPathComponent:fileName];
}

- (NSString *) generateIndexesUrl
{
NSMutableString *res = [NSMutableString stringWithFormat:@"https://download.osmand.net/get_indexes?gzip&osmandver=%@", OAAppVersion.getVersionForUrl];
Expand Down
66 changes: 33 additions & 33 deletions Sources/Backup/BackupUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,44 @@ final class BackupUtils: NSObject {
infoFiles: Bool) -> [OARemoteFile: OASettingsItem] {
var res = [OARemoteFile: OASettingsItem]()
var files = remoteFiles
var settingsItemMap = [String: OASettingsItem]()
var subtypeFolders = [OAFileSettingsItem]()
let DELIMETER = "___"
for item in items {
var processedFiles = [OARemoteFile]()
for file in files {
var name = file.name as NSString
if infoFiles && name.pathExtension == OABackupHelper.info_EXT() {
name = name.deletingPathExtension as NSString
let itemFileName = getItemFileName(item)
settingsItemMap[(OASettingsItemType.typeName(item.type) ?? "") + DELIMETER + itemFileName] = item
if let fileItem = item as? OAFileSettingsItem {
let subtypeFolder = OAFileSettingsItemFileSubtype.getFolderName(fileItem.subtype)
var isDir: ObjCBool = false
FileManager.default.fileExists(atPath: fileItem.filePath, isDirectory: &isDir)
if !subtypeFolder.isEmpty && isDir.boolValue {
subtypeFolders.append(fileItem)
}

if applyItem(item, type: file.type, name: name) {
if file.item == nil {
file.item = item
}
}
for file in files {
var name = file.name as NSString
if infoFiles && name.pathExtension == OABackupHelper.info_EXT() {
name = name.deletingPathExtension as NSString
}

if let item = settingsItemMap[file.type + DELIMETER + (name as String)] {
res[file] = item
} else {
for fileItem in subtypeFolders {
let itemFileName = getItemFileName(fileItem)
var found = false
if !itemFileName.hasSuffix("/") {
found = name.hasPrefix(itemFileName + "/")
} else {
found = name.hasPrefix(itemFileName)
}
if found {
res[file] = fileItem
break
}
res[file] = item
processedFiles.append(file)
}
}
files.removeAll { processedFiles.contains($0) }
}
return res
}
Expand All @@ -92,27 +113,6 @@ final class BackupUtils: NSObject {
OACommonBoolean.withKey("\(versionHistoryPrefix)\(type.name)", defValue: true).makeGlobal().makeShared()
}

static func applyItem(_ item: OASettingsItem, type: String, name: NSString) -> Bool {
let itemFileName = getItemFileName(item)
let itemTypeName = OASettingsItemType.typeName(item.type)
if itemTypeName == type {
if name.isEqual(to: itemFileName) {
return true
} else if let fileItem = item as? OAFileSettingsItem {
let subtypeFolder = OAFileSettingsItemFileSubtype.getFolderName(fileItem.subtype)
if name.hasPrefix(subtypeFolder) || subtypeFolder.isEmpty {
var isDir: ObjCBool = false
FileManager.default.fileExists(atPath: fileItem.filePath, isDirectory: &isDir)
if isDir.boolValue, !itemFileName.hasSuffix("/") {
return name.hasPrefix("\(itemFileName)/")
} else {
return name.hasPrefix(itemFileName)
}
}
}
}
return false
}

static func getItemFileName(_ item: OASettingsItem) -> String {
var fileName: String
Expand Down
19 changes: 18 additions & 1 deletion Sources/Backup/LocalBackup/OASettingsHelper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// Copyright © 2020 OsmAnd. All rights reserved.
//

#import "OsmAnd_Maps-Swift.h"
#import "OAIndexConstants.h"
#import "OASettingsHelper.h"
#import "OASettingsItem.h"
#import "OASettingsImporter.h"
Expand Down Expand Up @@ -314,8 +316,23 @@ - (void) exportSettings:(NSString *)fileDir fileName:(NSString *)fileName settin
NSMutableArray<NSString *> *items = [NSMutableArray array];
for (NSString *file in files)
{
if ([file.pathExtension isEqualToString:@"txt"])
if ([file.pathExtension isEqualToString:@"txt"]) {
NSString *key = [COLOR_PALETTE_DIR stringByAppendingPathComponent:file];
NSDictionary *assetsMap = BundledAssets.shared.assets; // Access the assets map
BundledAsset *asset = assetsMap[key];
if(asset) {
NSString *filePath = [colorPaletteFolder stringByAppendingPathComponent:file];
NSDictionary *attributes = [fileManager attributesOfItemAtPath:filePath error:nil];
NSDate *lastModifiedDate = attributes[NSFileModificationDate];
if (lastModifiedDate) {
NSTimeInterval lastModifiedTimestamp = [lastModifiedDate timeIntervalSince1970];
if (lastModifiedTimestamp <= asset.version.doubleValue) {
continue;
}
}
}
[items addObject:[colorPaletteFolder stringByAppendingPathComponent:file]];
}
}

if (items.count > 0)
Expand Down
21 changes: 8 additions & 13 deletions Sources/Backup/OABackupHelper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "OABackupHelper.h"
#import "OAExportBackupTask.h"
#import "OsmAndApp.h"
#import "OAExportSettingsType.h"
#import "OASettingsItem.h"
Expand Down Expand Up @@ -116,15 +117,6 @@ + (NSString *) CHECK_CODE_URL
return CHECK_CODE_URL;
}

+ (OASettingsItem *) getRestoreItem:(NSArray<OASettingsItem *> *)items remoteFile:(OARemoteFile *)remoteFile
{
for (OASettingsItem *item in items)
{
if ([BackupUtils applyItem:item type:remoteFile.type name:remoteFile.name])
return item;
}
return nil;
}

+ (OABackupHelper *)sharedInstance
{
Expand Down Expand Up @@ -466,7 +458,7 @@ - (void)sendCode:(NSString *)email action:(NSString *)action

- (NSInteger)calculateFileSize:(OARemoteFile *)remoteFile
{
NSInteger sz = remoteFile.filesize / 1024;
NSInteger sz = remoteFile.filesize;
if (remoteFile.item.type == EOASettingsItemTypeFile)
{
OAFileSettingsItem *flItem = (OAFileSettingsItem *) remoteFile.item;
Expand All @@ -475,10 +467,13 @@ - (NSInteger)calculateFileSize:(OARemoteFile *)remoteFile
NSString *mapId = flItem.fileName.lowerCase;
const auto res = _app.resourcesManager->getResourceInRepository(QString::fromNSString(mapId));
if (res)
sz = res->size / 1024;
sz = res->size;
}
}
return sz;
if (sz < APPROXIMATE_FILE_SIZE_BYTES) {
sz = APPROXIMATE_FILE_SIZE_BYTES; // take into account 100 KB overhead for small file
}
return sz / 1024;
}

- (NSString *)downloadFile:(NSString *)filePath
Expand Down Expand Up @@ -558,7 +553,7 @@ - (NSString *)downloadFile:(NSString *)filePath
// progress.startWork((int) (remoteFile.getFilesize() / 1024));

if (listener)
[listener onFileDownloadDone:type fileName:fileName error:error];
[listener onFileDownloadDone:type fileName:fileName estSize:sz error:error];
[operationLog finishOperation];
return error;
}
Expand Down
Loading

0 comments on commit 363fd0b

Please sign in to comment.