forked from fyhuang/enjoy2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.m
69 lines (56 loc) · 1.94 KB
/
Config.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Config.m
// Enjoy
//
// Created by Sam McCall on 4/05/09.
//
#include "JSONKit/JSONKit.h"
@implementation Config
-(id) init {
if(self=[super init]) {
entries = [[NSMutableDictionary alloc] init];
}
return self;
}
@synthesize name, entries;
-(void) setTarget:(Target*)target forAction:(id)jsa {
[entries setValue:target forKey: [jsa stringify]];
}
-(Target*) getTargetForAction: (id) jsa {
return [entries objectForKey: [jsa stringify]];
}
-(void) saveJSONTo:(NSURL *)filename {
NSMutableDictionary *mapping_dict = [[NSMutableDictionary alloc] init];
[mapping_dict setObject:name forKey:@"name"];
[mapping_dict setObject:@"Enjoy2-1.1" forKey:@"format"];
NSMutableDictionary *mapping_entries = [[NSMutableDictionary alloc] init];
for (id key in entries) {
[mapping_entries setObject:[[entries objectForKey:key] stringify] forKey:key];
}
[mapping_dict setObject:mapping_entries forKey:@"entries"];
// Convert to JSON, write to file
NSData *json_data = [mapping_dict JSONData];
[json_data writeToURL:filename atomically:true];
[json_data release];
[mapping_entries release];
[mapping_dict release];
}
-(Config*) loadSkelFromJSON:(NSData *)jsonData {
NSDictionary *dict = [jsonData objectFromJSONData];
name = [dict objectForKey:@"name"];
return self;
}
-(Config*) loadFromJSON:(NSData *)jsonData withConfigList:(NSArray*)configs {
NSDictionary *jd = [jsonData objectFromJSONData];
NSString *jname = [jd objectForKey:@"name"];
if (![jname isEqualToString:name]) {
[NSException raise:@"Loading from JSON with different name" format:@"Loading from JSON with different name", nil];
}
NSDictionary *entries_d = [jd objectForKey:@"entries"];
for(id key in entries_d) {
NSString *value = [entries_d objectForKey:key];
[entries setObject: [Target unstringify:value withConfigList:configs] forKey:key];
}
return self;
}
@end