forked from Blendman974/faceclaw
Unify Android and iOS config formats
This commit is contained in:
1 parent
35a4ae7064
commit
210b111e27
18 files changed
+1116
-427
No files matched your search
@@ -1,6 +1,17 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
/** Developer config transfer through the app container, before workers start. */
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
/** Converts between this phone's settings and the transfer document (Kotlin SettingsTransfer). */
|
||||
@protocol FaceclawConfigCodec <NSObject>
|
||||
/** valuesJson is a JSON object of the settings; nil when they cannot be exported. */
|
||||
- (nullable NSString *)exportValuesJson:(NSString *)valuesJson platform:(NSString *)platform appVersion:(nullable NSString *)appVersion;
|
||||
/** A JSON object: {"ok":true,"settings":{...},"skipped":[...],"sourcePlatform":...} or {"ok":false,"message":...}. */
|
||||
- (NSString *)importPlanDocument:(NSString *)document platform:(NSString *)platform;
|
||||
@end
|
||||
|
||||
/** Developer config transfer through the app container, before workers start (scripts/config-transfer.md). */
|
||||
@interface FaceclawConfigPort : NSObject
|
||||
+ (void)processPendingRequest;
|
||||
+ (void)processDirectory:(NSString *)directory defaults:(NSUserDefaults *)defaults domain:(NSString *)domain;
|
||||
+ (void)processDirectory:(NSString *)directory defaults:(NSUserDefaults *)defaults domain:(NSString *)domain
|
||||
codec:(id<FaceclawConfigCodec>)codec appVersion:(nullable NSString *)appVersion;
|
||||
@end
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -1,57 +1,112 @@
|
||||
#import "FaceclawConfigPort.h"
|
||||
#include <math.h>
|
||||
// The app links FaceclawKit; tests/ios-config-native.cjs builds this file for macOS without it
|
||||
// and supplies its own codec.
|
||||
#if __has_include(<FaceclawKit/FaceclawKit.h>)
|
||||
#import <FaceclawKit/FaceclawKit.h>
|
||||
@interface FaceclawKitConfigCodec : NSObject <FaceclawConfigCodec>
|
||||
@end
|
||||
@implementation FaceclawKitConfigCodec
|
||||
- (NSString *)exportValuesJson:(NSString *)valuesJson platform:(NSString *)platform appVersion:(NSString *)appVersion {
|
||||
return [FaceclawKitSettingsTransfer.shared exportValuesJson:valuesJson platform:platform appVersion:appVersion];
|
||||
}
|
||||
- (NSString *)importPlanDocument:(NSString *)document platform:(NSString *)platform {
|
||||
return [FaceclawKitSettingsTransfer.shared importPlanDocument:document platform:platform];
|
||||
}
|
||||
@end
|
||||
#define FACECLAW_CONFIG_CODEC [FaceclawKitConfigCodec new]
|
||||
#endif
|
||||
|
||||
static NSString *const kPlatform = @"ios";
|
||||
static NSString *const kChangeToken = @"ios.settings.changeToken";
|
||||
|
||||
@implementation FaceclawConfigPort
|
||||
+ (void)processPendingRequest {
|
||||
#ifdef FACECLAW_CONFIG_CODEC
|
||||
NSString *library = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject;
|
||||
[self processDirectory:[library stringByAppendingPathComponent:@"FaceclawConfigPort"]
|
||||
defaults:NSUserDefaults.standardUserDefaults domain:NSBundle.mainBundle.bundleIdentifier];
|
||||
defaults:NSUserDefaults.standardUserDefaults domain:NSBundle.mainBundle.bundleIdentifier
|
||||
codec:FACECLAW_CONFIG_CODEC
|
||||
appVersion:NSBundle.mainBundle.infoDictionary[@"CFBundleShortVersionString"]];
|
||||
#endif
|
||||
}
|
||||
+ (BOOL)validSettings:(id)settings {
|
||||
if (![settings isKindOfClass:NSDictionary.class] || [settings count] > 4096) return NO;
|
||||
for (id key in settings) {
|
||||
// Typed locals, not messages to id: FaceclawKit declares other `count`/`length` properties.
|
||||
+ (BOOL)validSettings:(id)candidate {
|
||||
if (![candidate isKindOfClass:NSDictionary.class]) return NO;
|
||||
NSDictionary *settings = candidate;
|
||||
if (settings.count > 4096) return NO;
|
||||
for (id candidateKey in settings) {
|
||||
if (![candidateKey isKindOfClass:NSString.class]) return NO;
|
||||
NSString *key = candidateKey;
|
||||
id value = settings[key];
|
||||
if (![key isKindOfClass:NSString.class] || ![key length] || [key length] > 512 ||
|
||||
if (!key.length || key.length > 512 ||
|
||||
!([value isKindOfClass:NSString.class] || [value isKindOfClass:NSNumber.class])) return NO;
|
||||
}
|
||||
return YES;
|
||||
}
|
||||
+ (void)processDirectory:(NSString *)directory defaults:(NSUserDefaults *)defaults domain:(NSString *)domain {
|
||||
/** The app's settings: strings and finite numbers (booleans included) in its persistent domain. */
|
||||
+ (NSDictionary *)exportableSettings:(NSDictionary *)domain {
|
||||
NSMutableDictionary *settings = [NSMutableDictionary new];
|
||||
for (NSString *key in domain) {
|
||||
id value = domain[key];
|
||||
if ([value isKindOfClass:NSString.class] ||
|
||||
([value isKindOfClass:NSNumber.class] && isfinite([value doubleValue]))) settings[key] = value;
|
||||
}
|
||||
[settings removeObjectForKey:kChangeToken];
|
||||
return settings;
|
||||
}
|
||||
+ (void)processDirectory:(NSString *)directory defaults:(NSUserDefaults *)defaults domain:(NSString *)domain
|
||||
codec:(id<FaceclawConfigCodec>)codec appVersion:(NSString *)appVersion {
|
||||
NSString *requestPath = [directory stringByAppendingPathComponent:@"request.json"];
|
||||
NSData *data = [NSData dataWithContentsOfFile:requestPath];
|
||||
if (!data) return;
|
||||
NSFileManager *fm = NSFileManager.defaultManager;
|
||||
// Consume once. A failed import must never be replayed at a later launch.
|
||||
[fm removeItemAtPath:requestPath error:nil];
|
||||
id request = data.length <= 2 * 1024 * 1024 ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
|
||||
// A push embeds a document of up to 2 MiB as a JSON string, which escaping can enlarge.
|
||||
id request = data.length <= 8 * 1024 * 1024 ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
|
||||
BOOL valid = [request isKindOfClass:NSDictionary.class];
|
||||
NSString *identifier = valid && [request[@"id"] isKindOfClass:NSString.class] ? request[@"id"] : @"";
|
||||
NSMutableDictionary *reply = [@{@"id":identifier, @"ok":@NO, @"message":@"Invalid config request"} mutableCopy];
|
||||
[defaults synchronize];
|
||||
NSDictionary *before = [defaults persistentDomainForName:domain] ?: @{};
|
||||
if (valid && [request[@"operation"] isEqual:@"pull"]) {
|
||||
NSMutableDictionary *settings = [NSMutableDictionary new];
|
||||
for (NSString *key in before) {
|
||||
id value = before[key];
|
||||
if ([value isKindOfClass:NSString.class] || [value isKindOfClass:NSNumber.class]) settings[key] = value;
|
||||
NSDictionary *settings = [self exportableSettings:before];
|
||||
NSData *values = [NSJSONSerialization dataWithJSONObject:settings options:0 error:nil];
|
||||
NSString *document = values ? [codec exportValuesJson:[[NSString alloc] initWithData:values encoding:NSUTF8StringEncoding]
|
||||
platform:kPlatform appVersion:appVersion] : nil;
|
||||
if (document) {
|
||||
reply[@"ok"] = @YES; reply[@"message"] = @"Config exported";
|
||||
reply[@"document"] = document; reply[@"count"] = @(settings.count);
|
||||
} else reply[@"message"] = @"Could not encode settings";
|
||||
} else if (valid && [request[@"operation"] isEqual:@"push"] && [request[@"document"] isKindOfClass:NSString.class]) {
|
||||
NSData *planData = [[codec importPlanDocument:request[@"document"] platform:kPlatform] dataUsingEncoding:NSUTF8StringEncoding];
|
||||
id plan = planData ? [NSJSONSerialization JSONObjectWithData:planData options:0 error:nil] : nil;
|
||||
NSDictionary *settings = [plan isKindOfClass:NSDictionary.class] ? plan[@"settings"] : nil;
|
||||
if (![plan isKindOfClass:NSDictionary.class] || ![plan[@"ok"] isEqual:@YES]) {
|
||||
id message = [plan isKindOfClass:NSDictionary.class] ? plan[@"message"] : nil;
|
||||
reply[@"message"] = [message isKindOfClass:NSString.class] ? message : @"Invalid config";
|
||||
} else if (![self validSettings:settings]) {
|
||||
reply[@"message"] = @"Invalid config";
|
||||
} else {
|
||||
NSError *error = nil;
|
||||
NSData *backup = [NSPropertyListSerialization dataWithPropertyList:before format:NSPropertyListBinaryFormat_v1_0 options:0 error:&error];
|
||||
if (backup && [backup writeToFile:[directory stringByAppendingPathComponent:@"previous.plist"] options:NSDataWritingAtomic error:&error]) {
|
||||
NSMutableDictionary *next = [before mutableCopy];
|
||||
[next addEntriesFromDictionary:settings];
|
||||
next[kChangeToken] = NSUUID.UUID.UUIDString;
|
||||
[defaults setPersistentDomain:next forName:domain];
|
||||
if ([defaults synchronize]) {
|
||||
reply[@"ok"] = @YES; reply[@"message"] = @"Config imported";
|
||||
reply[@"count"] = @(settings.count);
|
||||
if ([plan[@"skipped"] isKindOfClass:NSArray.class]) reply[@"skipped"] = plan[@"skipped"];
|
||||
if ([plan[@"sourcePlatform"] isKindOfClass:NSString.class]) reply[@"sourcePlatform"] = plan[@"sourcePlatform"];
|
||||
} else {
|
||||
[defaults setPersistentDomain:before forName:domain]; [defaults synchronize];
|
||||
reply[@"message"] = @"Could not persist config; restored previous settings";
|
||||
}
|
||||
} else reply[@"message"] = @"Could not back up existing settings; import skipped";
|
||||
}
|
||||
[settings removeObjectForKey:@"ios.settings.changeToken"];
|
||||
reply[@"config"] = @{@"schema":@1, @"settings":settings}; reply[@"ok"] = @YES;
|
||||
reply[@"message"] = @"Config exported";
|
||||
} else if (valid && [request[@"operation"] isEqual:@"push"] && [self validSettings:request[@"settings"]]) {
|
||||
NSError *error = nil;
|
||||
NSData *backup = [NSPropertyListSerialization dataWithPropertyList:before format:NSPropertyListBinaryFormat_v1_0 options:0 error:&error];
|
||||
if (backup && [backup writeToFile:[directory stringByAppendingPathComponent:@"previous.plist"] options:NSDataWritingAtomic error:&error]) {
|
||||
NSMutableDictionary *next = [before mutableCopy];
|
||||
[next addEntriesFromDictionary:request[@"settings"]];
|
||||
next[@"ios.settings.changeToken"] = NSUUID.UUID.UUIDString;
|
||||
[defaults setPersistentDomain:next forName:domain];
|
||||
if ([defaults synchronize]) {
|
||||
reply[@"ok"] = @YES; reply[@"message"] = @"Config imported";
|
||||
reply[@"count"] = @([request[@"settings"] count]);
|
||||
} else {
|
||||
[defaults setPersistentDomain:before forName:domain]; [defaults synchronize];
|
||||
reply[@"message"] = @"Could not persist config; restored previous settings";
|
||||
}
|
||||
} else reply[@"message"] = @"Could not back up existing settings; import skipped";
|
||||
}
|
||||
NSData *response = [NSJSONSerialization dataWithJSONObject:reply options:NSJSONWritingPrettyPrinted error:nil];
|
||||
[response writeToFile:[directory stringByAppendingPathComponent:@"response.json"] options:NSDataWritingAtomic error:nil];
|
||||
|
||||
Reference in new issue
Block a user