OSDN Git Service

Doutaku を 1.0 にアップデート
[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     
50     case canNotStoreType
51 }
52
53
54 protocol DefaultValuePrimitive { }
55 extension Bool: DefaultValuePrimitive { }
56 extension Int: DefaultValuePrimitive { }
57 extension UInt: DefaultValuePrimitive { }
58 extension Float: DefaultValuePrimitive { }
59 extension Double: DefaultValuePrimitive { }
60 extension CGFloat: DefaultValuePrimitive { }
61 extension Data: DefaultValuePrimitive { }
62 extension String: DefaultValuePrimitive { }
63 extension URL: DefaultValuePrimitive { }
64 extension Array: DefaultValuePrimitive { }
65 extension Dictionary: DefaultValuePrimitive { }
66
67
68 // FIXME: value [["key": Date()]] returns true but it can not into UserDefaults.
69 func isDefaultValuePrimitive(_ value: Any) -> Bool {
70     
71     if value is [Any] {
72         
73         switch value {
74             
75         case _ as [DefaultValuePrimitive]: return true
76             
77         default: return false
78             
79         }
80     }
81     
82     if value is [AnyHashable: Any] {
83         
84         switch value {
85             
86         case _ as [String: DefaultValuePrimitive]: return true
87             
88         default: return false
89             
90         }
91     }
92     
93     if value is DefaultValuePrimitive {
94         
95         return true
96     }
97     
98     return false
99 }
100
101 // FIXME: value [["key": <Any Struct>]] returns true but it can not KeyedUnarchive.
102 func canCoding(_ value: Any) -> Bool {
103     
104     if value is [Any] {
105         
106         switch value {
107             
108         case _ as [NSCoding]: return true
109             
110         default: return false
111             
112         }
113     }
114     
115     if value is [AnyHashable: Any] {
116         
117         switch value {
118             
119         case _ as [String: NSCoding]: return true
120             
121         default: return false
122             
123         }
124     }
125     
126     if value is NSCoding {
127         
128         return true
129     }
130     
131     return false
132 }
133
134 extension DefaultKeys {
135     
136     static let settings: [DefaultKeys: Any?] = [
137         
138         .slotItemSortDescriptors: nil,
139         .shipviewSortDescriptors: nil,
140         .powerupSupportSortDecriptors: nil,
141         
142         .showsDebugMenu: false,
143         .degugPrintLevel: nil,
144         
145         .prevReloadDate: nil,
146         
147         .hideMaxKaryoku: false,
148         .hideMaxLucky: false,
149         .hideMaxRaisou: false,
150         .hideMaxSoukou: false,
151         .hideMaxTaiku: false,
152         
153         .showsPlanColor: true,
154         .plan01Color: #colorLiteral(red: 0.000, green: 0.043, blue: 0.518, alpha: 1),
155         .plan02Color: #colorLiteral(red: 0.800, green: 0.223, blue: 0.000, alpha: 1),
156         .plan03Color: #colorLiteral(red: 0.539, green: 0.012, blue: 0.046, alpha: 1),
157         .plan04Color: #colorLiteral(red: 0.000, green: 0.535, blue: 0.535, alpha: 1),
158         .plan05Color: #colorLiteral(red: 0.376, green: 0.035, blue: 0.535, alpha: 1),
159         .plan06Color: #colorLiteral(red: 0.535, green: 0.535, blue: 0.000, alpha: 1),
160         
161         .minimumColoredShipCount: 0,
162         
163         .playFinishMissionSound: true,
164         .playFinishNyukyoSound: true,
165         .playFinishKenzoSound: true,
166         
167         .showLevelOneShipInUpgradableList: true,
168         .showsExcludedShipInUpgradableList: true,
169         
170         .showEquipmentType: nil,
171         
172         .fleetViewPosition: nil,
173         .fleetViewShipOrder: nil,
174         .repairTime: nil,
175         
176         .lastHasCombinedView: false,
177         .autoCombinedView: true,
178         .useSwipeChangeCombinedView: false,
179         
180         .appendKanColleTag: true,
181         .showsListWindowAtScreenshot: false,
182         .screenshotPreviewZoomValue: 0.4,
183         .screenshotEditorColumnCount: 2,
184         .scrennshotEditorType: nil,
185         .screenshotButtonSize: nil,
186         .useMask: false,
187         .screenShotBorderWidth: 0.0,
188         .screenShotSaveDirectory: nil,
189         
190         .cleanOldHistoryItems: false,
191         .cleanSinceDays: 90,
192         
193         .notifyTimeSignal: false,
194         .notifyTimeBeforeTimeSignal: 5,
195         .playNotifyTimeSignalSound: false,
196         
197         .sakutekiCalclationSterategy: nil,
198         .formula33Factor: 1.0
199         
200     ]
201     
202     static let defaults: [String: Any] = settings
203         .compactMap { (k: DefaultKeys, v: Any?) -> (key: String, value: Any)? in
204             
205             guard let value = v else {
206                 
207                 return nil
208             }
209             
210             if isDefaultValuePrimitive(value) {
211                 
212                 return (key: k.rawValue, value: value)
213             }
214             
215             if canCoding(value) {
216                 
217                 return (key: k.rawValue, value: NSKeyedArchiver.archivedData(withRootObject: value))
218             }
219             
220             print("DefaultKeys can not store this type", type(of: value))
221             
222             return nil
223         }
224         .reduce(into: [String: Any]()) { (dict: inout [String: Any], pair) in
225             
226             dict[pair.key] = pair.value
227     }
228     
229 }