OSDN Git Service

HMMasterMissionCommandクラスをSwiftで書き換え
[kcd/KCD.git] / KCD / HMTSVSupport.m
1 //
2 //  HMTSVSupport.m
3 //  KCD
4 //
5 //  Created by Hori,Masaki on 2014/03/23.
6 //  Copyright (c) 2014年 Hori,Masaki. All rights reserved.
7 //
8
9 #import "HMTSVSupport.h"
10
11 #import "KCD-Swift.h"
12
13 #define PATH_KEY2(key1, key2) [NSString stringWithFormat:@"%@.%@", (key1), (key2)]
14 #define PATH_KEY3(key1, key2, key3) [NSString stringWithFormat:@"%@.%@.%@", (key1), (key2), (key3)]
15
16 @interface HMTSVSupport ()
17 @property HMLocalDataStore *store;
18 @end
19 @implementation HMTSVSupport
20
21 - (NSManagedObjectContext *)managedObjectContext
22 {
23         if(self.store) {
24                 return self.store.managedObjectContext;
25         }
26         
27         self.store = [HMLocalDataStore oneTimeEditor];
28         
29         return self.store.managedObjectContext;
30 }
31
32 #pragma mark## Save to Text file ##
33 - (NSArray *)allObjectOfEntityName:(NSString *)entityName sortDescriptors:(NSArray *)sortDescriptors
34 {
35         NSError *error = nil;
36         
37         NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:entityName];
38         [fetch setSortDescriptors:sortDescriptors];
39         NSArray *fetchedArray = [self.managedObjectContext executeFetchRequest:fetch
40                                                                                                                                          error:&error];
41         if(error) {
42                 NSLog(@"%@", [error localizedDescription]);
43                 return nil;
44         }
45         
46         return fetchedArray;
47 }
48 - (NSArray *)allObjectOfEntityName:(NSString *)entityName sortBy:(NSString *)propertyName ascending:(BOOL)ascending
49 {
50         NSSortDescriptor *sort = nil;
51         
52         if(propertyName) {
53                 sort = [NSSortDescriptor sortDescriptorWithKey:propertyName ascending:ascending];
54         }
55         
56         return [self allObjectOfEntityName:entityName sortDescriptors:sort ? @[sort] : nil];
57 }
58 - (NSArray *)allObjectOfEntityName:(NSString *)entityName
59 {
60         return [self allObjectOfEntityName:entityName sortDescriptors:nil];
61 }
62 - (void)saveLFSeparetedArray:(NSArray *)array toFile:(NSString *)path
63 {
64         NSError *error = nil;
65         
66         NSString *saveText = [array componentsJoinedByString:@"\n"];
67         [saveText writeToFile:path
68                            atomically:YES
69                                  encoding:NSUTF8StringEncoding
70                                         error:&error];
71         if(error) {
72                 NSLog(@"%@", [error localizedDescription]);
73         }
74 }
75 - (NSData *)dataFromLFSeparatedArray:(NSArray *)array
76 {
77         NSString *saveText = [array componentsJoinedByString:@"\n"];
78         return [saveText dataUsingEncoding:NSUTF8StringEncoding];
79 }
80 - (NSData *)dataOfKaihatuHistory
81 {
82         NSArray *allObject = [self allObjectOfEntityName:@"KaihatuHistory"
83                                                                                           sortBy:@"date"
84                                                                                    ascending:YES];
85         if([allObject count] == 0) return nil;
86         
87         NSMutableArray *array = [NSMutableArray array];
88         for(HMKaihatuHistory *obj in allObject) {
89                 NSString *element = [[NSString alloc] initWithFormat:@"%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@",
90                                                          obj.date, obj.fuel, obj.bull,
91                                                          obj.steel, obj.bauxite, obj.kaihatusizai,
92                                                          obj.name, obj.flagShipName, obj.flagShipLv, obj.commanderLv
93                                                          ];
94                 [array addObject:element];
95         }
96         
97         return [self dataFromLFSeparatedArray:array];
98 }
99 - (NSData *)dataOfKenzoHistory
100 {
101         NSArray *allObject = [self allObjectOfEntityName:@"KenzoHistory"
102                                                                                           sortBy:@"date"
103                                                                                    ascending:YES];
104         if([allObject count] == 0) return nil;
105         
106         NSMutableArray *array = [NSMutableArray array];
107         for(HMKenzoHistory *obj in allObject) {
108                 NSString *element = [[NSString alloc] initWithFormat:@"%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@",
109                                                          obj.date, obj.fuel, obj.bull,
110                                                          obj.steel, obj.bauxite, obj.kaihatusizai,
111                                                          obj.name, obj.sTypeId, obj.flagShipName,
112                                                          obj.flagShipLv, obj.commanderLv
113                                                          ];
114                 [array addObject:element];
115         }
116         
117         return [self dataFromLFSeparatedArray:array];
118 }
119 - (NSData *)dataOfKenzoMark
120 {
121         NSArray *allObject = [self allObjectOfEntityName:@"KenzoMark"
122                                                                                           sortBy:@"kDockId"
123                                                                                    ascending:YES];
124         if([allObject count] == 0) return nil;
125         
126         NSMutableArray *array = [NSMutableArray array];
127         for(HMKenzoMark *obj in allObject) {
128                 NSString *element = [[NSString alloc] initWithFormat:@"%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@\t%@",
129                                                          obj.created_ship_id, obj.fuel, obj.bull, obj.steel,
130                                                          obj.bauxite, obj.kaihatusizai, obj.kDockId,
131                                                          obj.flagShipName, obj.flagShipLv, obj.commanderLv
132                                                          ];
133                 [array addObject:element];
134         }
135         
136         return [self dataFromLFSeparatedArray:array];
137 }
138 - (IBAction)save:(id)sender
139 {
140         NSSavePanel *panel = [NSSavePanel savePanel];
141         [panel setAllowedFileTypes:@[@"kcdlocaldata"]];
142         
143         [panel beginWithCompletionHandler:^(NSInteger result) {
144                 if(result != NSOKButton) return;
145                 
146                 NSString *path = [panel.URL path];
147                 
148                 NSFileWrapper *fileWrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:nil];
149                 [fileWrapper setFilename:[path lastPathComponent]];
150                 [fileWrapper addRegularFileWithContents:[self dataOfKaihatuHistory]
151                                                           preferredFilename:@"kaihatu.tsv"];
152                 [fileWrapper addRegularFileWithContents:[self dataOfKenzoHistory]
153                                                           preferredFilename:@"kenzo.tsv"];
154                 [fileWrapper addRegularFileWithContents:[self dataOfKenzoMark]
155                                                           preferredFilename:@"kenzoMark.tsv"];
156                 
157                 [fileWrapper writeToFile:path atomically:YES updateFilenames:NO];
158         }];
159 }
160
161 #pragma mark## Initialize from Text ##
162 - (NSArray *)arrayFromLFSeparatedStringData:(NSData *)data
163 {
164         NSString *content = [[NSString alloc] initWithData:data
165                                                                                           encoding:NSUTF8StringEncoding];
166         return [content componentsSeparatedByString:@"\x0a"];
167 }
168
169 - (NSArray *)arrayFromTabSeparatedString:(NSString *)string
170 {
171         return [string componentsSeparatedByString:@"\t"];
172 }
173
174 - (BOOL)isEmptyEntityName:(NSString *)name
175 {
176         NSError *error = nil;
177         NSFetchRequest *fetch;
178         NSInteger num;
179         
180         fetch = [[NSFetchRequest alloc] init];
181         [fetch setEntity:[NSEntityDescription entityForName:name
182                                                                  inManagedObjectContext:self.managedObjectContext]];
183         num = [self.managedObjectContext countForFetchRequest:fetch
184                                                                                                         error:&error];
185         fetch = nil;
186         if(error) {
187                 NSLog(@"%@", [error localizedDescription]);
188                 return NO;
189         }
190         
191         return num == 0;
192 }
193
194 - (void)buildKaihatuHistoryFromData:(NSData *)data
195 {
196         NSString *entityName = @"KaihatuHistory";
197         NSArray *contents;
198         HMLocalDataStore *lds = [HMLocalDataStore oneTimeEditor];
199         NSManagedObjectContext *moc = [lds managedObjectContext];
200         
201         contents = [self arrayFromLFSeparatedStringData:data];
202         id attribute;
203         for(attribute in contents) {
204                 NSArray *attr = [self arrayFromTabSeparatedString:attribute];
205                 if(attr.count == 0) continue;
206                 if([attr[6] isKindOfClass:[NSNull class]]) continue;
207                 if([attr[6] isEqual:@"(null)"]) continue;
208                 
209                 NSArray *array = [lds objectsWithEntityName:entityName
210                                                                         sortDescriptors:nil
211                                                                                   predicate:[NSPredicate predicateWithFormat:@"date = %@", [NSDate dateWithString:attr[0]]]
212                                                                                           error:NULL];
213                 if(array.count != 0) continue;
214                 
215                 HMKaihatuHistory *obj = [NSEntityDescription insertNewObjectForEntityForName:entityName
216                                                                                                                           inManagedObjectContext:moc];
217                 obj.date = [NSDate dateWithString:attr[0]];
218                 obj.fuel = @([attr[1] integerValue]);
219                 obj.bull = @([attr[2] integerValue]);
220                 obj.steel = @([attr[3] integerValue]);
221                 obj.bauxite = @([attr[4] integerValue]);
222                 obj.kaihatusizai = @([attr[5] integerValue]);
223                 obj.name = attr[6];
224                 obj.flagShipName = attr[7];
225                 obj.flagShipLv = @([attr[8] integerValue]);
226                 obj.commanderLv = @([attr[9] integerValue]);
227         }
228 }
229 - (void)buildKenzoHistoryFromData:(NSData *)data
230 {
231         NSString *entityName = @"KenzoHistory";
232         NSArray *contents;
233         HMLocalDataStore *lds = [HMLocalDataStore oneTimeEditor];
234         
235         contents = [self arrayFromLFSeparatedStringData:data];
236         id attribute;
237         for(attribute in contents) {
238                 NSArray *attr = [self arrayFromTabSeparatedString:attribute];
239                 if(attr.count == 0) continue;
240                 if([attr[6] isKindOfClass:[NSNull class]]) continue;
241                 if([attr[6] isEqual:@"(null)"]) continue;
242                                 
243                 NSArray *array = [lds objectsWithEntityName:entityName
244                                                                         sortDescriptors:nil
245                                                                                   predicate:[NSPredicate predicateWithFormat:@"date = %@", [NSDate dateWithString:attr[0]]]
246                                                                                           error:NULL];
247                 if(array.count != 0) continue;
248                 
249                 HMKenzoHistory *obj = [NSEntityDescription insertNewObjectForEntityForName:entityName
250                                                                                                                         inManagedObjectContext:lds.managedObjectContext];
251                 
252                 obj.date = [NSDate dateWithString:attr[0]];
253                 obj.fuel = @([attr[1] integerValue]);
254                 obj.bull = @([attr[2] integerValue]);
255                 obj.steel = @([attr[3] integerValue]);
256                 obj.bauxite = @([attr[4] integerValue]);
257                 obj.kaihatusizai = @([attr[5] integerValue]);
258                 obj.name = attr[6];
259                 obj.sTypeId = @([attr[7] integerValue]);
260                 obj.flagShipName = attr[8];
261                 obj.flagShipLv = @([attr[9] integerValue]);
262                 obj.commanderLv = @([attr[10] integerValue]);
263         }
264 }
265 - (void)buildKenzoMarkFromData:(NSData *)data
266 {
267         NSString *entityName = @"KenzoMark";
268         NSArray *contents;
269         if([self isEmptyEntityName:entityName]) {
270                 HMLocalDataStore *lds = [HMLocalDataStore oneTimeEditor];
271                 NSManagedObjectContext *moc = [lds managedObjectContext];
272                 
273                 contents = [self arrayFromLFSeparatedStringData:data];
274                 id attribute;
275                 for(attribute in contents) {
276                         NSArray *attr = [self arrayFromTabSeparatedString:attribute];
277                         if([attr count] < 1) continue;
278                         
279                         HMKenzoMark *obj = [NSEntityDescription insertNewObjectForEntityForName:entityName
280                                                                                                                                 inManagedObjectContext:moc];
281                         obj.created_ship_id = @([attr[0] integerValue]);
282                         obj.fuel = @([attr[1] integerValue]);
283                         obj.bull = @([attr[2] integerValue]);
284                         obj.steel = @([attr[3] integerValue]);
285                         obj.bauxite = @([attr[4] integerValue]);
286                         obj.kaihatusizai = @([attr[5] integerValue]);
287                         obj.kDockId = @([attr[6] integerValue]);
288                         obj.flagShipName = attr[7];
289                         obj.flagShipLv = @([attr[8] integerValue]);
290                         obj.commanderLv = @([attr[9] integerValue]);
291                 }
292         }
293         
294 }
295 - (IBAction)load:(id)sender
296 {
297         NSOpenPanel *panel = [NSOpenPanel openPanel];
298         [panel setAllowedFileTypes:@[@"kcdlocaldata"]];
299         
300         [panel beginWithCompletionHandler:^(NSInteger result) {
301                 if(result != NSOKButton) return;
302                 
303                 NSFileWrapper *wrapper = [[NSFileWrapper alloc] initWithURL:panel.URL
304                                                                                                                         options:0
305                                                                                                                           error:NULL];
306                 NSDictionary *children = [wrapper fileWrappers];
307                 NSFileWrapper *kaihatu = [children objectForKey:@"kaihatu.tsv"];
308                 [self buildKaihatuHistoryFromData:[kaihatu regularFileContents]];
309                 
310                 NSFileWrapper *kenzo = [children objectForKey:@"kenzo.tsv"];
311                 [self buildKenzoHistoryFromData:[kenzo regularFileContents]];
312                 
313                 NSFileWrapper *kenzoMark = [children objectForKey:@"kenzoMark.tsv"];
314                 [self buildKenzoMarkFromData:[kenzoMark regularFileContents]];
315         }];
316 }
317 @end