OSDN Git Service

コメントを追加
[kcd/KCD.git] / KCD / DefaultSettings.swift
1 /*
2  DefaultSettings.swift
3  
4  Copyright (c) 2017, Hori, Masaki.
5  All rights reserved.
6  
7  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8  
9  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
10  
11  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
12  
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  
15  */
16
17 /*
18  
19  DefaultSettings.swift
20  
21  CotEditor
22  https://coteditor.com
23  
24  Created by 1024jp on 2017-02-14.
25  
26  ------------------------------------------------------------------------------
27  
28  © 2016-2017 1024jp
29  
30  Licensed under the Apache License, Version 2.0 (the "License");
31  you may not use this file except in compliance with the License.
32  You may obtain a copy of the License at
33  
34  https://www.apache.org/licenses/LICENSE-2.0
35  
36  Unless required by applicable law or agreed to in writing, software
37  distributed under the License is distributed on an "AS IS" BASIS,
38  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39  See the License for the specific language governing permissions and
40  limitations under the License.
41  
42  */
43
44 import Foundation
45 import AppKit.NSColor
46
47
48 enum DefaultsKeyError: Error {
49     case canNotStoreType
50 }
51
52
53 protocol DefaultValuePrimitive { }
54 extension Bool: DefaultValuePrimitive { }
55 extension Int: DefaultValuePrimitive { }
56 extension UInt: DefaultValuePrimitive { }
57 extension Float: DefaultValuePrimitive { }
58 extension Double: DefaultValuePrimitive { }
59 extension CGFloat: DefaultValuePrimitive { }
60 extension Data: DefaultValuePrimitive { }
61 extension String: DefaultValuePrimitive { }
62 extension URL: DefaultValuePrimitive { }
63 extension Array: DefaultValuePrimitive { }
64 extension Dictionary: DefaultValuePrimitive { }
65
66
67 // FIXME: value [["key": Date()]] returns true but it can not into UserDefaults.
68 func isDefaultValuePrimitive(_ value: Any) -> Bool {
69     
70     if value is [Any] {
71         
72         switch value {
73         case _ as [DefaultValuePrimitive]: return true
74         default: return false
75         }
76     }
77     
78     if value is [AnyHashable: Any] {
79         
80         switch value {
81         case _ as [String: DefaultValuePrimitive]: return true
82         default: return false
83         }
84     }
85     
86     if value is DefaultValuePrimitive {
87         
88         return true
89     }
90     
91     return false
92 }
93
94 // FIXME: value [["key": <Any Struct>]] returns true but it can not KeyedUnarchive.
95 func canCoding(_ value: Any) -> Bool {
96     
97     if value is [Any] {
98         
99         switch value {
100         case _ as [NSCoding]: return true
101         default: return false
102         }
103     }
104     
105     if value is [AnyHashable: Any] {
106         
107         switch value {
108         case _ as [String: NSCoding]: return true
109         default: return false
110         }
111     }
112     
113     if value is NSCoding {
114         
115         return true
116     }
117     
118     return false
119 }
120
121 extension DefaultKeys {
122     
123     static let settings: [DefaultKeys: Any?] = [
124         
125         .slotItemSortDescriptors: nil,
126         .shipviewSortDescriptors: nil,
127         .powerupSupportSortDecriptors: nil,
128         
129         .showsDebugMenu: false,
130         .degugPrintLevel: nil,
131         
132         .prevReloadDate: nil,
133         
134         .hideMaxKaryoku: false,
135         .hideMaxLucky: false,
136         .hideMaxRaisou: false,
137         .hideMaxSoukou: false,
138         .hideMaxTaiku: false,
139         
140         .showsPlanColor: true,
141         .plan01Color: #colorLiteral(red: 0.000, green: 0.043, blue: 0.518, alpha: 1),
142         .plan02Color: #colorLiteral(red: 0.800, green: 0.223, blue: 0.000, alpha: 1),
143         .plan03Color: #colorLiteral(red: 0.539, green: 0.012, blue: 0.046, alpha: 1),
144         .plan04Color: #colorLiteral(red: 0.000, green: 0.535, blue: 0.535, alpha: 1),
145         .plan05Color: #colorLiteral(red: 0.376, green: 0.035, blue: 0.535, alpha: 1),
146         .plan06Color: #colorLiteral(red: 0.535, green: 0.535, blue: 0.000, alpha: 1),
147         
148         .minimumColoredShipCount: 0,
149         
150         .playFinishMissionSound: true,
151         .playFinishNyukyoSound: true,
152         .playFinishKenzoSound: true,
153         
154         .showLevelOneShipInUpgradableList: true,
155         .showsExcludedShipInUpgradableList: true,
156         
157         .showEquipmentType: nil,
158         
159         .fleetViewPosition: nil,
160         .fleetViewShipOrder: nil,
161         .repairTime: nil,
162         
163         .lastHasCombinedView: false,
164         .autoCombinedView: true,
165         .useSwipeChangeCombinedView: false,
166         
167         .appendKanColleTag: true,
168         .showsListWindowAtScreenshot: false,
169         .screenshotPreviewZoomValue: 0.4,
170         .screenshotEditorColumnCount: 2,
171         .scrennshotEditorType: nil,
172         .screenshotButtonSize: nil,
173         .useMask: false,
174         .screenShotBorderWidth: 0.0,
175         .screenShotSaveDirectory: nil,
176         
177         .cleanOldHistoryItems: false,
178         .cleanSinceDays: 90,
179         
180         .notifyTimeSignal: false,
181         .notifyTimeBeforeTimeSignal: 5,
182         .playNotifyTimeSignalSound: false,
183         
184         .sakutekiCalclationSterategy: nil,
185         .formula33Factor: 1.0
186         
187     ]
188     
189     static let defaults: [String: Any] = settings
190         .flatMap { (k: DefaultKeys, v: Any?) -> (key: String, value: Any)? in
191             
192             guard let value = v else { return nil }
193             
194             if isDefaultValuePrimitive(value) {
195                 
196                 return (key: k.rawValue, value: value)
197             }
198             
199             if canCoding(value) {
200                 
201                 return (key: k.rawValue, value: NSKeyedArchiver.archivedData(withRootObject: value))
202             }
203             
204             print("DefaultKeys can not store this type", type(of: value))
205             
206             return nil
207         }
208         .reduce(into: [String: Any]()) { (dict: inout [String: Any], pair) in
209             dict[pair.key] = pair.value
210     }
211     
212 }