2014-10-29 00:47:51 -04:00
|
|
|
//
|
|
|
|
|
// DataManager.m
|
2015-05-12 21:48:30 -04:00
|
|
|
// Moonlight
|
2014-10-29 00:47:51 -04:00
|
|
|
//
|
|
|
|
|
// Created by Diego Waxemberg on 10/28/14.
|
2015-05-12 21:48:30 -04:00
|
|
|
// Copyright (c) 2014 Moonlight Stream. All rights reserved.
|
2014-10-29 00:47:51 -04:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#import "DataManager.h"
|
2015-09-30 20:34:52 -07:00
|
|
|
#import "TemporaryApp.h"
|
2015-12-02 00:25:38 -08:00
|
|
|
#import "TemporarySettings.h"
|
2014-10-29 00:47:51 -04:00
|
|
|
|
2015-12-02 00:25:38 -08:00
|
|
|
@implementation DataManager {
|
|
|
|
|
NSManagedObjectContext *_managedObjectContext;
|
|
|
|
|
AppDelegate *_appDelegate;
|
2015-09-30 20:34:52 -07:00
|
|
|
}
|
|
|
|
|
|
2014-10-29 00:47:51 -04:00
|
|
|
- (id) init {
|
|
|
|
|
self = [super init];
|
2015-12-02 00:25:38 -08:00
|
|
|
|
2017-09-15 00:51:37 -07:00
|
|
|
// HACK: Avoid calling [UIApplication delegate] off the UI thread to keep
|
|
|
|
|
// Main Thread Checker happy.
|
|
|
|
|
if ([NSThread isMainThread]) {
|
2020-11-01 16:50:02 -06:00
|
|
|
_appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
|
2017-09-15 00:51:37 -07:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
dispatch_sync(dispatch_get_main_queue(), ^{
|
2020-11-01 16:50:02 -06:00
|
|
|
self->_appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
|
2017-09-15 00:51:37 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-21 16:17:05 -05:00
|
|
|
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
|
2018-05-30 01:19:46 -07:00
|
|
|
[_managedObjectContext setParentContext:[_appDelegate managedObjectContext]];
|
2015-12-02 00:25:38 -08:00
|
|
|
|
2014-10-29 00:47:51 -04:00
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-02 00:25:38 -08:00
|
|
|
- (void) updateUniqueId:(NSString*)uniqueId {
|
|
|
|
|
[_managedObjectContext performBlockAndWait:^{
|
|
|
|
|
[self retrieveSettings].uniqueId = uniqueId;
|
|
|
|
|
[self saveData];
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) getUniqueId {
|
|
|
|
|
__block NSString *uid;
|
|
|
|
|
|
|
|
|
|
[_managedObjectContext performBlockAndWait:^{
|
|
|
|
|
uid = [self retrieveSettings].uniqueId;
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
return uid;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-02 18:13:32 -07:00
|
|
|
- (void) saveSettingsWithBitrate:(NSInteger)bitrate
|
|
|
|
|
framerate:(NSInteger)framerate
|
|
|
|
|
height:(NSInteger)height
|
|
|
|
|
width:(NSInteger)width
|
2021-07-11 09:10:55 -07:00
|
|
|
audioConfig:(NSInteger)audioConfig
|
2018-06-02 18:13:32 -07:00
|
|
|
onscreenControls:(NSInteger)onscreenControls
|
|
|
|
|
optimizeGames:(BOOL)optimizeGames
|
|
|
|
|
multiController:(BOOL)multiController
|
2022-07-21 20:09:22 -05:00
|
|
|
swapABXYButtons:(BOOL)swapABXYButtons
|
2018-06-02 18:13:32 -07:00
|
|
|
audioOnPC:(BOOL)audioOnPC
|
2023-11-03 23:13:44 -05:00
|
|
|
preferredCodec:(uint32_t)preferredCodec
|
2022-02-02 13:37:07 -03:00
|
|
|
useFramePacing:(BOOL)useFramePacing
|
2020-03-27 20:26:01 -07:00
|
|
|
enableHdr:(BOOL)enableHdr
|
2020-11-01 11:50:00 -06:00
|
|
|
btMouseSupport:(BOOL)btMouseSupport
|
|
|
|
|
absoluteTouchMode:(BOOL)absoluteTouchMode
|
|
|
|
|
statsOverlay:(BOOL)statsOverlay {
|
2015-12-02 00:25:38 -08:00
|
|
|
|
|
|
|
|
[_managedObjectContext performBlockAndWait:^{
|
|
|
|
|
Settings* settingsToSave = [self retrieveSettings];
|
|
|
|
|
settingsToSave.framerate = [NSNumber numberWithInteger:framerate];
|
|
|
|
|
settingsToSave.bitrate = [NSNumber numberWithInteger:bitrate];
|
|
|
|
|
settingsToSave.height = [NSNumber numberWithInteger:height];
|
|
|
|
|
settingsToSave.width = [NSNumber numberWithInteger:width];
|
2021-07-11 09:10:55 -07:00
|
|
|
settingsToSave.audioConfig = [NSNumber numberWithInteger:audioConfig];
|
2015-12-02 00:25:38 -08:00
|
|
|
settingsToSave.onscreenControls = [NSNumber numberWithInteger:onscreenControls];
|
2018-06-02 18:13:32 -07:00
|
|
|
settingsToSave.optimizeGames = optimizeGames;
|
|
|
|
|
settingsToSave.multiController = multiController;
|
2022-07-21 20:09:22 -05:00
|
|
|
settingsToSave.swapABXYButtons = swapABXYButtons;
|
2018-06-02 18:13:32 -07:00
|
|
|
settingsToSave.playAudioOnPC = audioOnPC;
|
2023-11-03 23:13:44 -05:00
|
|
|
settingsToSave.preferredCodec = preferredCodec;
|
2022-02-02 13:37:07 -03:00
|
|
|
settingsToSave.useFramePacing = useFramePacing;
|
2018-06-02 18:13:32 -07:00
|
|
|
settingsToSave.enableHdr = enableHdr;
|
2020-03-27 20:26:01 -07:00
|
|
|
settingsToSave.btMouseSupport = btMouseSupport;
|
2020-11-01 11:50:00 -06:00
|
|
|
settingsToSave.absoluteTouchMode = absoluteTouchMode;
|
|
|
|
|
settingsToSave.statsOverlay = statsOverlay;
|
2015-12-02 00:25:38 -08:00
|
|
|
|
|
|
|
|
[self saveData];
|
|
|
|
|
}];
|
|
|
|
|
}
|
2014-12-29 13:03:07 -05:00
|
|
|
|
2015-12-02 00:25:38 -08:00
|
|
|
- (void) updateHost:(TemporaryHost *)host {
|
|
|
|
|
[_managedObjectContext performBlockAndWait:^{
|
|
|
|
|
// Add a new persistent managed object if one doesn't exist
|
2016-03-07 16:12:10 -08:00
|
|
|
Host* parent = [self getHostForTemporaryHost:host withHostRecords:[self fetchRecords:@"Host"]];
|
2016-01-21 11:29:17 -05:00
|
|
|
if (parent == nil) {
|
2018-04-22 06:44:22 +02:00
|
|
|
NSEntityDescription* entity = [NSEntityDescription entityForName:@"Host" inManagedObjectContext:self->_managedObjectContext];
|
|
|
|
|
parent = [[Host alloc] initWithEntity:entity insertIntoManagedObjectContext:self->_managedObjectContext];
|
2015-12-02 00:25:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Push changes from the temp host to the persistent one
|
2016-01-21 14:03:07 -05:00
|
|
|
[host propagateChangesToParent:parent];
|
|
|
|
|
|
|
|
|
|
[self saveData];
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) updateAppsForExistingHost:(TemporaryHost *)host {
|
|
|
|
|
[_managedObjectContext performBlockAndWait:^{
|
2016-03-07 16:12:10 -08:00
|
|
|
Host* parent = [self getHostForTemporaryHost:host withHostRecords:[self fetchRecords:@"Host"]];
|
2016-01-21 14:03:07 -05:00
|
|
|
if (parent == nil) {
|
|
|
|
|
// The host must exist to be updated
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NSMutableSet *applist = [[NSMutableSet alloc] init];
|
2016-03-07 16:12:10 -08:00
|
|
|
NSArray *appRecords = [self fetchRecords:@"App"];
|
2016-01-21 14:03:07 -05:00
|
|
|
for (TemporaryApp* app in host.appList) {
|
|
|
|
|
// Add a new persistent managed object if one doesn't exist
|
2016-03-07 16:12:10 -08:00
|
|
|
App* parentApp = [self getAppForTemporaryApp:app withAppRecords:appRecords];
|
2016-01-21 14:03:07 -05:00
|
|
|
if (parentApp == nil) {
|
2018-04-22 06:44:22 +02:00
|
|
|
NSEntityDescription* entity = [NSEntityDescription entityForName:@"App" inManagedObjectContext:self->_managedObjectContext];
|
|
|
|
|
parentApp = [[App alloc] initWithEntity:entity insertIntoManagedObjectContext:self->_managedObjectContext];
|
2016-01-21 14:03:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[app propagateChangesToParent:parentApp withHost:parent];
|
|
|
|
|
|
|
|
|
|
[applist addObject:parentApp];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parent.appList = applist;
|
|
|
|
|
|
|
|
|
|
[self saveData];
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-02 00:25:38 -08:00
|
|
|
- (TemporarySettings*) getSettings {
|
|
|
|
|
__block TemporarySettings *tempSettings;
|
|
|
|
|
|
|
|
|
|
[_managedObjectContext performBlockAndWait:^{
|
|
|
|
|
tempSettings = [[TemporarySettings alloc] initFromSettings:[self retrieveSettings]];
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
return tempSettings;
|
2014-10-29 00:47:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (Settings*) retrieveSettings {
|
2014-11-26 17:57:16 -05:00
|
|
|
NSArray* fetchedRecords = [self fetchRecords:@"Settings"];
|
2014-10-29 00:47:51 -04:00
|
|
|
if (fetchedRecords.count == 0) {
|
|
|
|
|
// create a new settings object with the default values
|
2015-12-02 00:25:38 -08:00
|
|
|
NSEntityDescription* entity = [NSEntityDescription entityForName:@"Settings" inManagedObjectContext:_managedObjectContext];
|
|
|
|
|
Settings* settings = [[Settings alloc] initWithEntity:entity insertIntoManagedObjectContext:_managedObjectContext];
|
2014-11-26 17:57:16 -05:00
|
|
|
|
2014-10-29 00:47:51 -04:00
|
|
|
return settings;
|
|
|
|
|
} else {
|
|
|
|
|
// we should only ever have 1 settings object stored
|
|
|
|
|
return [fetchedRecords objectAtIndex:0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-21 16:24:02 -05:00
|
|
|
- (void) removeApp:(TemporaryApp*)app {
|
|
|
|
|
[_managedObjectContext performBlockAndWait:^{
|
2016-03-07 16:12:10 -08:00
|
|
|
App* managedApp = [self getAppForTemporaryApp:app withAppRecords:[self fetchRecords:@"App"]];
|
2016-01-21 16:24:02 -05:00
|
|
|
if (managedApp != nil) {
|
2018-04-22 06:44:22 +02:00
|
|
|
[self->_managedObjectContext deleteObject:managedApp];
|
2016-01-21 16:24:02 -05:00
|
|
|
[self saveData];
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-02 00:25:38 -08:00
|
|
|
- (void) removeHost:(TemporaryHost*)host {
|
|
|
|
|
[_managedObjectContext performBlockAndWait:^{
|
2016-03-07 16:12:10 -08:00
|
|
|
Host* managedHost = [self getHostForTemporaryHost:host withHostRecords:[self fetchRecords:@"Host"]];
|
2016-01-21 11:29:17 -05:00
|
|
|
if (managedHost != nil) {
|
2018-04-22 06:44:22 +02:00
|
|
|
[self->_managedObjectContext deleteObject:managedHost];
|
2016-01-21 11:29:17 -05:00
|
|
|
[self saveData];
|
|
|
|
|
}
|
2015-12-02 00:25:38 -08:00
|
|
|
}];
|
2015-01-01 23:40:52 -05:00
|
|
|
}
|
|
|
|
|
|
2015-07-11 18:47:05 -07:00
|
|
|
- (void) saveData {
|
2015-12-02 00:25:38 -08:00
|
|
|
NSError* error;
|
2018-05-30 01:19:46 -07:00
|
|
|
if ([_managedObjectContext hasChanges] && ![_managedObjectContext save:&error]) {
|
2015-12-02 00:25:38 -08:00
|
|
|
Log(LOG_E, @"Unable to save hosts to database: %@", error);
|
2014-10-29 00:47:51 -04:00
|
|
|
}
|
|
|
|
|
|
2015-12-02 00:25:38 -08:00
|
|
|
[_appDelegate saveContext];
|
2014-11-26 17:57:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-12-02 00:25:38 -08:00
|
|
|
- (NSArray*) getHosts {
|
|
|
|
|
__block NSMutableArray *tempHosts = [[NSMutableArray alloc] init];
|
2015-09-30 20:34:52 -07:00
|
|
|
|
2015-12-02 00:25:38 -08:00
|
|
|
[_managedObjectContext performBlockAndWait:^{
|
|
|
|
|
NSArray *hosts = [self fetchRecords:@"Host"];
|
2015-09-30 20:34:52 -07:00
|
|
|
|
2015-12-02 00:25:38 -08:00
|
|
|
for (Host* host in hosts) {
|
|
|
|
|
[tempHosts addObject:[[TemporaryHost alloc] initFromHost:host]];
|
|
|
|
|
}
|
|
|
|
|
}];
|
2015-09-30 20:34:52 -07:00
|
|
|
|
2015-12-02 00:25:38 -08:00
|
|
|
return tempHosts;
|
2015-07-11 18:47:05 -07:00
|
|
|
}
|
|
|
|
|
|
2016-01-21 11:29:17 -05:00
|
|
|
// Only call from within performBlockAndWait!!!
|
2016-03-07 16:12:10 -08:00
|
|
|
- (Host*) getHostForTemporaryHost:(TemporaryHost*)tempHost withHostRecords:(NSArray*)hosts {
|
2016-01-21 11:29:17 -05:00
|
|
|
for (Host* host in hosts) {
|
|
|
|
|
if ([tempHost.uuid isEqualToString:host.uuid]) {
|
|
|
|
|
return host;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only call from within performBlockAndWait!!!
|
2016-03-07 16:12:10 -08:00
|
|
|
- (App*) getAppForTemporaryApp:(TemporaryApp*)tempApp withAppRecords:(NSArray*)apps {
|
2016-01-21 11:29:17 -05:00
|
|
|
for (App* app in apps) {
|
|
|
|
|
if ([app.id isEqualToString:tempApp.id] &&
|
2018-06-02 20:44:12 -07:00
|
|
|
[app.host.uuid isEqualToString:tempApp.host.uuid]) {
|
2016-01-21 11:29:17 -05:00
|
|
|
return app;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-26 17:57:16 -05:00
|
|
|
- (NSArray*) fetchRecords:(NSString*)entityName {
|
2015-09-30 20:34:52 -07:00
|
|
|
NSArray* fetchedRecords;
|
2014-11-26 17:57:16 -05:00
|
|
|
|
2015-12-02 00:25:38 -08:00
|
|
|
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
|
|
|
|
|
NSEntityDescription* entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:_managedObjectContext];
|
|
|
|
|
[fetchRequest setEntity:entity];
|
|
|
|
|
|
|
|
|
|
NSError* error;
|
|
|
|
|
fetchedRecords = [_managedObjectContext executeFetchRequest:fetchRequest error:&error];
|
|
|
|
|
//TODO: handle errors
|
2014-11-26 17:57:16 -05:00
|
|
|
|
|
|
|
|
return fetchedRecords;
|
|
|
|
|
}
|
2014-10-29 00:47:51 -04:00
|
|
|
|
|
|
|
|
@end
|
2014-11-26 17:57:16 -05:00
|
|
|
|