-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.ms
More file actions
218 lines (197 loc) · 6.1 KB
/
Config.ms
File metadata and controls
218 lines (197 loc) · 6.1 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*! © 2022 imaoki | MIT License | https://github.com/imaoki */
/*-
設定ファイルの管理を行う。
@remarks 作成パラメータ
: ```maxscript
ConfigStruct filePath
```
*/
struct ConfigStruct (
/*- @prop <String|UndefinedClass> 作成パラメータ1。保存先ファイルのパス。既定値は`undefined`。 */
public _CP1_,
/*- @prop <String|UndefinedClass> 保存先ファイルのパス。既定値は`undefined`。 */
private filePath,
/*- @prop <Dictionary <Name> <Any>> 値の辞書。既定値は`undefined`。 */
private table,
/*- @prop <DotNetClass:System.DateTime> */
private dateTimeClass = DotNetClass "System.DateTime",
/*- @prop <Struct:FileUtilityStruct> */
private fileUtility,
/*- @prop <Struct:PathUtilityStruct> */
private pathUtility,
/*- @prop <Struct:StringUtilityStruct> */
private stringUtility,
/*
public fn AddValue key v = (),
public fn Clear = (),
public fn Count = (),
public fn GetFilePath = (),
public fn GetKeys = (),
public fn GetValue key default:undefined = (),
public fn HasValue key = (),
public fn Read = (),
public fn SetFilePath input = (),
public fn Write = (),
private fn isValidFilePath obj = (),
*/
/*-
値を追加する。
@param key <Name>
@param v <Any>
@returns <BooleanClass> キーが存在する場合は`true`、存在しない場合は`false`。
*/
public fn AddValue key v = (
if classOf key == Name do (
this.table[key] = v
)
this.HasValue key
),
/*-
内容を全て消去する。
@returns <OkClass>
*/
public fn Clear = (
free this.table
ok
),
/*-
値の総数を取得する。
@returns <Integer>
*/
public fn Count = (
this.table.Count
),
/*-
保存先ファイルのパスを取得する。
@returns <String|UndefinedClass>
*/
public fn GetFilePath = (
this.filePath
),
/*-
全てのキーを取得する。
@returns <Array[<Name>]>
@remarks キーの並びは通常の`sort`関数で昇順にソートされる。
*/
public fn GetKeys = (
sort (copy this.table.Keys #NoMap)
),
/*-
値を取得する。
@param key <Name>
@param default: <Any> キーが存在しない場合に返す値。既定値は`undefined`。
@returns <Any>
*/
public fn GetValue key default:undefined = (
if this.HasValue key then (
this.table[key]
)
else (
default
)
),
/*-
キーが存在するかどうかを取得する。
@param key <Name>
@returns <BooleanClass>
*/
public fn HasValue key = (
classOf key == Name and hasDictValue this.table key
),
/*-
保存先ファイルの内容を読み込んで内部状態を更新する。
@returns <BooleanClass> 読み込みに成功した場合は`true`、失敗した場合は`false`。
@remarks 保存先ファイルのパスが無効の場合は何もしない。
*/
public fn Read = (
local isSuccessful = false
if this.isValidFilePath this.filePath \
and this.fileUtility.FileExists this.filePath do (
local pattern = "* ?/\n(Dictionary #Name *"
local content = this.fileUtility.ReadAllText this.filePath
if classOf content == String \
and matchPattern content pattern:pattern ignoreCase:true do (
this.table = execute content
isSuccessful = true
)
)
isSuccessful
),
/*-
保存先ファイルのパスを設定する。
@param input <String|UndefinedClass> 拡張子を含むファイルパス。
@returns <String|UndefinedClass>
@remarks ファイルの拡張子は`.mxsconfig`に置き換えられる。
*/
public fn SetFilePath input = (
if this.isValidFilePath input do (
local dirName = this.pathUtility.GetDirectoryName input
local fileNameWE = this.pathUtility.GetFileNameWithoutExtension input
this.filePath = dirName + @"\" + fileNameWE + ".mxsconfig"
)
this.GetFilePath()
),
/*-
内容を保存先ファイルに書き出す。
@returns <BooleanClass> 書き出しに成功した場合は`true`、失敗した場合は`false`。
@remarks 保存先ファイルのパスが無効の場合は何もしない。
*/
public fn Write = (
local isSuccessful = false
if this.isValidFilePath this.filePath then (
local content = StringStream ""
local now = this.dateTimeClass.Now.ToString "yyyy/MM/dd HH:mm:ss K"
format "/* Generated by ConfigStruct at % */\n" now to:content
format "%\n" (this.stringUtility.Serialize this.table) to:content
content = content as String
this.fileUtility.WriteAllText this.filePath content
isSuccessful = this.fileUtility.FileExists this.filePath
)
isSuccessful
),
/*-
@param obj <Any>
@returns <BooleanClass>
*/
private fn isValidFilePath obj = (
classOf obj == String and this.pathUtility.HasExtension obj
),
/*- @returns <Name> */
public fn StructName = #ConfigStruct,
/*-
@param indent: <String>
@param out: <FileStream|StringStream|WindowStream> 出力先。既定値は`listener`。
@returns <OkClass>
*/
public fn Dump indent:"" out:listener = (
format "%ConfigStruct\n" indent to:out
if classOf this.filePath == String then (
format "% filePath:@\"%\"\n" indent this.filePath to:out
)
else (
format "% filePath:%\n" indent this.filePath to:out
)
format "% table:%\n" indent (this.stringUtility.Serialize this.table) to:out
ok
),
/*-
@param obj <Any>
@returns <BooleanClass>
@remarks 大文字と小文字を区別する。
*/
public fn Equals obj = (
local isEqualStructName = isStruct obj \
and isProperty obj #StructName \
and classOf obj.StructName == MAXScriptFunction \
and obj.StructName() == this.StructName()
local isEqualProperties = true
isEqualStructName and isEqualProperties
),
on Create do (
this.fileUtility = (::standardDefinitionPool[@"FileUtility.ms"])()
this.pathUtility = (::standardDefinitionPool[@"PathUtility.ms"]) (getSourceFileName())
this.stringUtility = (::standardDefinitionPool[@"StringUtility.ms"])()
this.table = Dictionary #Name
this.SetFilePath this._CP1_
)
)