• R/O
  • HTTP
  • SSH
  • HTTPS

KCD: Commit

main code repository.


Commit MetaInfo

Revision68dd014f1212855f1a285f26634abe2c5fda54ca (tree)
Time2017-01-21 21:30:51
Authormasakih <masakih@user...>
Commitermasakih

Log Message

HMAPIResponseをSwiftに変換した

Change Summary

Incremental Difference

--- a/KCD.xcodeproj/project.pbxproj
+++ b/KCD.xcodeproj/project.pbxproj
@@ -218,7 +218,6 @@
218218 F4BDEB71187252F30069D0CE /* KCDTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F4BDEB70187252F30069D0CE /* KCDTests.m */; };
219219 F4BDEB7B187253130069D0CE /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F4BDEB7A187253130069D0CE /* WebKit.framework */; };
220220 F4BEEE9F18D211280006F70C /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = F4BEEE9D18D211280006F70C /* Localizable.strings */; };
221- F4BEEFE91B0CE312004702B8 /* HMAPIResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = F4BEEFE81B0CE312004702B8 /* HMAPIResponse.m */; };
222221 F4BEEFFC1B11A5A7004702B8 /* HMExternalBrowserWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = F4BEEFFE1B11A5A7004702B8 /* HMExternalBrowserWindowController.xib */; };
223222 F4BEEFFF1B11A5C2004702B8 /* HMMediumShipViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = F4BEF0011B11A5C2004702B8 /* HMMediumShipViewController.xib */; };
224223 F4BEF0021B11A5CC004702B8 /* HMFleetMinimumViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = F4BEF0041B11A5CC004702B8 /* HMFleetMinimumViewController.xib */; };
@@ -247,6 +246,7 @@
247246 F4C6686C1C5451ED00601AFB /* HMStrengthenListViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = F4C6686E1C5451ED00601AFB /* HMStrengthenListViewController.xib */; };
248247 F4C6686F1C54524900601AFB /* HMVerticalFleetViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = F4C668711C54524900601AFB /* HMVerticalFleetViewController.xib */; };
249248 F4C668721C54525300601AFB /* HMCombileViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = F4C668741C54525300601AFB /* HMCombileViewController.xib */; };
249+ F4CF25A81E325ABA00C02A66 /* HMAPIResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4CF25A71E325ABA00C02A66 /* HMAPIResponse.swift */; };
250250 F4D05BC61E0ABD6800688D66 /* HMSlotItemWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4D05BC51E0ABD6800688D66 /* HMSlotItemWindowController.swift */; };
251251 F4D05BC81E0C0DA100688D66 /* HMAirBaseWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4D05BC71E0C0DA100688D66 /* HMAirBaseWindowController.swift */; };
252252 F4D05BCA1E0CADA100688D66 /* HMExternalBrowserWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4D05BC91E0CADA100688D66 /* HMExternalBrowserWindowController.swift */; };
@@ -585,8 +585,6 @@
585585 F4BEEEA118D211360006F70C /* ja */ = {isa = PBXFileReference; lastKnownFileType = text; name = ja; path = ja.lproj/Localizable.strings.sed; sourceTree = "<group>"; };
586586 F4BEEEA418D211500006F70C /* ja */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.make; name = ja; path = ja.lproj/Makefile; sourceTree = "<group>"; };
587587 F4BEEEA718D323520006F70C /* Makefile */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.make; path = Makefile; sourceTree = SOURCE_ROOT; };
588- F4BEEFE71B0CE312004702B8 /* HMAPIResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMAPIResponse.h; sourceTree = "<group>"; };
589- F4BEEFE81B0CE312004702B8 /* HMAPIResponse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMAPIResponse.m; sourceTree = "<group>"; };
590588 F4C041B91875CAF5009273EB /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; };
591589 F4C1190318A71AEA005D5B25 /* HMJSONReciever.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMJSONReciever.h; sourceTree = "<group>"; };
592590 F4C1190418A71AEA005D5B25 /* HMJSONReciever.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMJSONReciever.m; sourceTree = "<group>"; };
@@ -603,6 +601,7 @@
603601 F4C5A2331C37F87500533FB6 /* EquipmentEnhancementListBuilder */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = EquipmentEnhancementListBuilder; sourceTree = BUILT_PRODUCTS_DIR; };
604602 F4C5A2351C37F87600533FB6 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
605603 F4C5A23D1C380E0400533FB6 /* HMEnhancementListItem.plist */ = {isa = PBXFileReference; lastKnownFileType = file.bplist; path = HMEnhancementListItem.plist; sourceTree = "<group>"; };
604+ F4CF25A71E325ABA00C02A66 /* HMAPIResponse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMAPIResponse.swift; sourceTree = "<group>"; };
606605 F4D05BC51E0ABD6800688D66 /* HMSlotItemWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMSlotItemWindowController.swift; sourceTree = "<group>"; };
607606 F4D05BC71E0C0DA100688D66 /* HMAirBaseWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMAirBaseWindowController.swift; sourceTree = "<group>"; };
608607 F4D05BC91E0CADA100688D66 /* HMExternalBrowserWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HMExternalBrowserWindowController.swift; sourceTree = "<group>"; };
@@ -976,8 +975,7 @@
976975 F4C1190418A71AEA005D5B25 /* HMJSONReciever.m */,
977976 F4C1190618A746EB005D5B25 /* HMJSONTracker.h */,
978977 F4C1190718A746EB005D5B25 /* HMJSONTracker.m */,
979- F4BEEFE71B0CE312004702B8 /* HMAPIResponse.h */,
980- F4BEEFE81B0CE312004702B8 /* HMAPIResponse.m */,
978+ F4CF25A71E325ABA00C02A66 /* HMAPIResponse.swift */,
981979 F494DC5D18C30AE100AE97E4 /* HMMissionStatus.h */,
982980 F494DC5E18C30AE100AE97E4 /* HMMissionStatus.m */,
983981 F494DC6B18CC054800AE97E4 /* HMNyukyoDockStatus.h */,
@@ -1563,7 +1561,6 @@
15631561 F4E5FF0C1E1645280026868C /* HMScreenshotListViewController.swift in Sources */,
15641562 F44BC71B1E29DCB4004644E3 /* HMMemberKDockCommand.swift in Sources */,
15651563 F4AC136F1E0963DB00851147 /* HMProgressPanel.swift in Sources */,
1566- F4BEEFE91B0CE312004702B8 /* HMAPIResponse.m in Sources */,
15671564 F4AA592A1E1D1387001667AF /* HMIgnoreZeroTransformer.swift in Sources */,
15681565 F4AC136D1E0802C000851147 /* HMPreferencePanelController.swift in Sources */,
15691566 F44BC6E91E232B15004644E3 /* HMKaisouLockCommand.swift in Sources */,
@@ -1657,6 +1654,7 @@
16571654 F44BC6C91E22314B004644E3 /* HMUnknownComand.swift in Sources */,
16581655 F44BC6DD1E228345004644E3 /* HMUpdateQuestListCommand.swift in Sources */,
16591656 F495462B1B14CBA600E0D8D0 /* HMBookmarkManager.m in Sources */,
1657+ F4CF25A81E325ABA00C02A66 /* HMAPIResponse.swift in Sources */,
16601658 F43F3D521A39CC7500D6DAF7 /* HMKCMasterSlotItemObject.m in Sources */,
16611659 F44BC6F71E23DA4A004644E3 /* HMDestroyShipCommand.swift in Sources */,
16621660 F4AA59141E1C951F001667AF /* HMTimerCountFormatter.swift in Sources */,
--- a/KCD/HMAPIResponse.h
+++ /dev/null
@@ -1,27 +0,0 @@
1-//
2-// HMAPIResponse.h
3-// KCD
4-//
5-// Created by Hori,Masaki on 2015/05/21.
6-// Copyright (c) 2015年 Hori,Masaki. All rights reserved.
7-//
8-
9-#import <Foundation/Foundation.h>
10-
11-@interface HMAPIResponse : NSObject
12-
13-@property (readonly) NSString *api;
14-@property (nonatomic, readonly) NSDictionary<NSString *, NSString *> *parameter;
15-@property (nonatomic, readonly) id json;
16-@property (readonly) NSDate *date;
17-@property (nonatomic, readonly) BOOL success;
18-@property (readonly) NSString *errorString;
19-
20-#if ENABLE_JSON_LOG
21-@property (nonatomic, copy) NSArray *argumentArray;
22-#endif
23-
24-+ (instancetype)apiResponseWithRequest:(NSURLRequest *)request data:(NSData *)data;
25-- (instancetype)initWithRequest:(NSURLRequest *)request data:(NSData *)data;
26-
27-@end
--- a/KCD/HMAPIResponse.m
+++ /dev/null
@@ -1,140 +0,0 @@
1-//
2-// HMAPIResponse.m
3-// KCD
4-//
5-// Created by Hori,Masaki on 2015/05/21.
6-// Copyright (c) 2015年 Hori,Masaki. All rights reserved.
7-//
8-
9-#import "HMAPIResponse.h"
10-
11-#import "HMAppDelegate.h"
12-
13-
14-@interface HMAPIResponse ()
15-@property (copy, readwrite) NSString *api;
16-@property (nonatomic, copy, readwrite) NSDictionary *parameter;
17-@property (nonatomic, strong, readwrite) id json;
18-@property (strong, readwrite) NSDate *date;
19-@property (nonatomic, readwrite) BOOL success;
20-@property (copy, readwrite) NSString *errorString;
21-
22-@property (nonatomic, copy) NSData *jsonData;
23-@property (nonatomic, copy) NSString *paramString;
24-
25-@end
26-
27-@implementation HMAPIResponse
28-
29-+ (instancetype)apiResponseWithRequest:(NSURLRequest *)request data:(NSData *)data
30-{
31- return [[self alloc] initWithRequest:request data:data];
32-}
33-- (instancetype)initWithRequest:(NSURLRequest *)request data:(NSData *)data
34-{
35- self = [super init];
36- if(self) {
37- NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
38- if(![string hasPrefix:@"svdata="]) {
39- HMAppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
40- [appDelegate logLineReturn:@"recive data has not prefix svdata=. api -> %@.", request.URL.path];
41- [appDelegate logLineReturn:@"Original strings -> %@", string];
42- return nil;
43- }
44- string = [string substringFromIndex:strlen("svdata=")];
45- _jsonData = [string dataUsingEncoding:NSUTF8StringEncoding];
46-
47- NSData *requestBodyData = [request HTTPBody];
48- _paramString = [[NSString alloc] initWithData:requestBodyData encoding:NSUTF8StringEncoding];
49-
50- _api = request.URL.path;
51- _date = [NSDate dateWithTimeIntervalSinceNow:0];
52- }
53-
54- return self;
55-}
56-
57-- (void)parseJSON
58-{
59- NSError *error = nil;
60- id json = [NSJSONSerialization JSONObjectWithData:self.jsonData
61- options:NSJSONReadingAllowFragments
62- error:&error];
63- if(error) {
64- HMAppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
65- [appDelegate logLineReturn:@"Fail decode JSON data %@", error];
66- return;
67- }
68- if(![json isKindOfClass:[NSDictionary class]]) {
69- self.errorString = @"JSON is NOT NSDictionary.";
70- return;
71- }
72- self.json = json;
73- self.success = YES;
74- if(![[json objectForKey:@"api_result"] isEqual:@1]) {
75- self.success = NO;
76- }
77-}
78-
79-- (id)json
80-{
81- if(_json) return _json;
82-
83- [self parseJSON];
84- return _json;
85-}
86-
87-- (void)parseParameter
88-{
89- NSString *unescape = self.paramString.stringByRemovingPercentEncoding;
90- NSArray *pair = [unescape componentsSeparatedByString:@"&"];
91- NSMutableDictionary *dict = [NSMutableDictionary new];
92- for(NSString *p in pair) {
93- NSArray *pp = [p componentsSeparatedByString:@"="];
94- if([pp count] != 2) {
95- NSLog(@"API (%@): Bad Argument: pair is odd.", self.api);
96- continue;
97- }
98- [dict setObject:pp[1] forKey:pp[0]];
99- }
100- _parameter = dict;
101-
102-#if ENABLE_JSON_LOG
103- NSMutableArray *array = [NSMutableArray new];
104- for(NSString *p in pair) {
105- NSArray *pp = [p componentsSeparatedByString:@"="];
106- if([pp count] != 2) {
107- NSLog(@"API (%@): Bad Argument: pair is odd.", self.api);
108- continue;
109- }
110- [array addObject:@{@"key": pp[0], @"value": pp[1]}];
111- }
112- self.argumentArray = array;
113-#endif
114-
115-}
116-- (NSDictionary *)parameter
117-{
118- if(_parameter) return _parameter;
119-
120- [self parseParameter];
121- return _parameter;
122-}
123-#if ENABLE_JSON_LOG
124-- (NSArray *)argumentArray
125-{
126- if(_argumentArray) return _argumentArray;
127-
128- [self parseParameter];
129- return _argumentArray;
130-}
131-#endif
132-
133-- (BOOL)success
134-{
135- if(_json) return _success;
136-
137- [self parseJSON];
138- return _success;
139-}
140-@end
--- /dev/null
+++ b/KCD/HMAPIResponse.swift
@@ -0,0 +1,87 @@
1+//
2+// HMAPIResponse.swift
3+// KCD
4+//
5+// Created by Hori,Masaki on 2017/01/20.
6+// Copyright © 2017年 Hori,Masaki. All rights reserved.
7+//
8+
9+import Cocoa
10+
11+fileprivate func parseJSON(_ data: Data) -> [String:Any]? {
12+ guard let j = try? JSONSerialization.jsonObject(with: data, options: [.allowFragments]),
13+ let json = j as? [String:Any]
14+ else { return nil }
15+ return json
16+}
17+fileprivate func parseParameter(_ string: String) -> [String:String] {
18+ let piar = string.components(separatedBy: "&")
19+ return piar.reduce(Dictionary<String,String>()) {
20+ var dict = $0
21+ let p = $1.components(separatedBy: "=")
22+ if p.count == 2 {
23+ dict[p[0]] = p[1]
24+ }
25+ return dict
26+ }
27+}
28+
29+class HMAPIResponse: NSObject {
30+ let api: String
31+ let parameter: [String:String]
32+ let json: [String:Any]
33+ let date: Date
34+ var success: Bool {
35+ if let r = json["api_result"] as? Int { return r == 1 }
36+ return false
37+ }
38+
39+ #if ENABLE_JSON_LOG
40+ let argumentArray: [[String:String]]
41+ #endif
42+
43+ init?(request: NSURLRequest, data: Data) {
44+ date = Date()
45+
46+ let prefix = "svdata="
47+ guard let string = String(data: data, encoding: .utf8),
48+ string.hasPrefix(prefix)
49+ else {
50+ print("data is wrong")
51+ return nil
52+ }
53+ let start = string.index(string.startIndex, offsetBy: prefix.characters.count)
54+ let jsonString = string[start..<string.endIndex]
55+ guard let jsonData = jsonString.data(using: .utf8),
56+ let j = parseJSON(jsonData)
57+ else {
58+ print("Can not parse JSON")
59+ return nil
60+ }
61+ json = j
62+
63+ guard let requestBodyData = request.httpBody,
64+ let paramStr = String(data: requestBodyData, encoding: .utf8),
65+ let unescaped = paramStr.removingPercentEncoding
66+ else {
67+ print("Can not parse Parameter")
68+ return nil
69+ }
70+ parameter = parseParameter(unescaped)
71+
72+ guard let a = request.url?.path
73+ else {
74+ print("URLRequest is wrong")
75+ return nil
76+ }
77+ api = a
78+
79+ #if ENABLE_JSON_LOG
80+ argumentArray = parameter.map { (key, value) in
81+ ["key": key, "value": value]
82+ }
83+ #endif
84+
85+ super.init()
86+ }
87+}
--- a/KCD/HMJSONReciever.m
+++ b/KCD/HMJSONReciever.m
@@ -12,7 +12,6 @@
1212
1313 #import "CustomHTTPProtocol.h"
1414 #import "HMAppDelegate.h"
15-#import "HMAPIResponse.h"
1615
1716
1817 @interface HMJSONReciever ()
@@ -78,7 +77,7 @@
7877 NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
7978 [[NSApp delegate] logLineReturn:@"body -> \n%@", string];
8079 #else
81- HMAPIResponse *apiResult = [HMAPIResponse apiResponseWithRequest:protocol.request data:data];
80+ HMAPIResponse *apiResult = [[HMAPIResponse alloc] initWithRequest:protocol.request data:data];
8281 if(apiResult) {
8382 [self.queueu enqueue:apiResult];
8483 }
--- a/KCD/KCD-Bridging-Header.h
+++ b/KCD/KCD-Bridging-Header.h
@@ -23,8 +23,6 @@
2323
2424 #import "HMScreenshotModel.h"
2525
26-#import "HMAPIResponse.h"
27-
2826
2927 // Core Data
3028 #import "HMCoreDataManager.h"
Show on old repository browser