Skip to content

Instantly share code, notes, and snippets.

@cyco
Created March 1, 2015 16:59
Show Gist options
  • Save cyco/b72d381a089c24d53351 to your computer and use it in GitHub Desktop.
Save cyco/b72d381a089c24d53351 to your computer and use it in GitHub Desktop.
diff --git a/OpenEmuSystem/OEBindingsController.m b/OpenEmuSystem/OEBindingsController.m
index 25aaa42..98735d5 100644
--- a/OpenEmuSystem/OEBindingsController.m
+++ b/OpenEmuSystem/OEBindingsController.m
@@ -140,6 +140,10 @@ static NSString *configurationsFolderPath = nil;
[self OE_setupBindingsController];
[self OE_setupNotificationObservation];
[bindingsControllers setObject:self forKey:configurationName];
+
+ dispatch_after(1.0, dispatch_get_main_queue(), ^{
+ [self OE_loadDevicePlayerBindings];
+ });
});
ret = self;
}
@@ -158,6 +162,94 @@ static NSString *configurationsFolderPath = nil;
[nc addObserver:self selector:@selector(OE_HIDManagerDidRemoveDeviceNotification:) name:OEDeviceManagerDidRemoveDeviceHandlerNotification object:nil];
}
+- (void)OE_loadDevicePlayerBindings
+{
+ NSLog(@"load");
+ NSString *path = [[[[self class] filePathForConfigurationWithName:@"none"] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"PlayerDeviceMappings.oe"];
+ NSDictionary *mappings = [NSDictionary dictionaryWithContentsOfFile:path];
+ NSArray *previousDevices = [mappings objectForKey:@"names"];
+
+ NSArray *deviceHandlers = [[OEDeviceManager sharedDeviceManager] deviceHandlers];
+ NSMutableArray *currentDevices = [NSMutableArray arrayWithCapacity:[deviceHandlers count]];
+ for(id deviceHandler in deviceHandlers)
+ {
+ [currentDevices addObject:[deviceHandler valueForKeyPath:@"deviceDescription.product"]];
+ }
+ [currentDevices sortUsingSelector:@selector(caseInsensitiveCompare:)];
+
+
+ if(![previousDevices isEqualToArray:currentDevices])
+ {
+ NSLog(@"didn't bother to work this out");
+ return;
+ }
+
+ NSLog(@"ok, we can probably restore mappings");
+ NSArray *sortedDeviceHandlers = [[[OEDeviceManager sharedDeviceManager] deviceHandlers] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
+ NSString *keyPath = @"deviceDescription.product";
+ return [[obj1 valueForKeyPath:keyPath] caseInsensitiveCompare:[obj2 valueForKeyPath:keyPath]];
+ }];
+
+
+ [mappings enumerateKeysAndObjectsUsingBlock:^(id systemIdentifier, id obj, BOOL *stop) {
+ if([systemIdentifier isEqualTo:@"name"]) return;
+
+ OESystemBindings *bindings = [self systemBindingsForSystemIdentifier:systemIdentifier];
+ if(!bindings) return;
+
+ [obj enumerateKeysAndObjectsUsingBlock:^(id controller, id player, BOOL *stop) {
+ OEDeviceHandler *handler = [sortedDeviceHandlers objectAtIndex:[controller integerValue]];
+ NSInteger playerNumber = [player integerValue];
+
+ NSLog(@"%15@ | Player %lu | %@", systemIdentifier, playerNumber, [handler valueForKeyPath:@"deviceDescription.product"]);
+
+ [bindings setDeviceHandler:handler forPlayer:playerNumber];
+ }];
+ }];
+}
+
+- (void)OE_synchronizeDevicePlayerBindings
+{
+ NSLog(@"store");
+ NSArray *deviceHandlers = [[OEDeviceManager sharedDeviceManager] deviceHandlers];
+ NSMutableArray *deviceNames = [NSMutableArray arrayWithCapacity:[deviceHandlers count]];
+ for(id deviceHandler in deviceHandlers)
+ {
+ [deviceNames addObject:[deviceHandler valueForKeyPath:@"deviceDescription.product"]];
+ }
+ [deviceNames sortUsingSelector:@selector(caseInsensitiveCompare:)];
+ NSMutableDictionary *mappings = [NSMutableDictionary dictionaryWithCapacity:[[self systemBindings] count]];
+ for(OESystemBindings *bindings in [self systemBindings])
+ {
+ for(NSUInteger i=1; i <= [bindings numberOfPlayers]; i++)
+ {
+ OESystemController *controller = [bindings systemController];
+ NSString *systemIdentifier = [controller systemIdentifier];
+
+ OEDevicePlayerBindings *devicePlayerBindings = [bindings devicePlayerBindingsForPlayer:i];
+
+ NSMutableDictionary *systemMapping = [mappings objectForKey:systemIdentifier] ?: [NSMutableDictionary dictionaryWithCapacity:[bindings numberOfPlayers]];
+ if(devicePlayerBindings)
+ {
+ NSString *deviceProduct = [[devicePlayerBindings deviceHandler] valueForKeyPath:@"deviceDescription.product"];
+ NSUInteger index = [deviceNames indexOfObject:deviceProduct];
+ if(index != NSNotFound)
+ {
+ NSLog(@"%15@ | Player %lu | %@", systemIdentifier, (unsigned long)i, deviceProduct);
+
+ [systemMapping setObject:@(i) forKey:[NSString stringWithFormat:@"%lu", (unsigned long)index]];
+ [mappings setObject:systemMapping forKey:systemIdentifier];
+ }
+ }
+ }
+ }
+
+ [mappings setObject:deviceNames forKey:@"names"];
+
+ NSString *path = [[[[self class] filePathForConfigurationWithName:@"none"] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"PlayerDeviceMappings.oe"];
+ [mappings writeToFile:path atomically:YES];
+}
+
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
@@ -221,13 +313,15 @@ static NSString *configurationsFolderPath = nil;
{
[systemReps setObject:[ctrl OE_dictionaryRepresentation] forKey:identifier];
}];
-
+
+ [self OE_synchronizeDevicePlayerBindings];
+
if([NSKeyedArchiver archiveRootObject:systemReps toFile:[self filePath]])
{
requiresSynchronization = NO;
return YES;
}
-
+
return NO;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment