Go で書き直した Ikemen
Revisão | ef94d4fe26dee96d686100aba6e803e7906df0b4 (tree) |
---|---|
Hora | 2016-11-27 00:08:03 |
Autor | SUEHIRO <supersuehiro@user...> |
Commiter | SUEHIRO |
windows用のdefファイルを読み込みたい
@@ -3,4 +3,4 @@ GOPATH=$PWD/go | ||
3 | 3 | export GOPATH |
4 | 4 | go fmt ./src/*.go |
5 | 5 | # godoc -src ./src .* > godoc.txt |
6 | -go generate ./src/main.go && GODEBUG=cgocheck=0 go run ./src/*.go | |
6 | +go generate ./src/main.go && go run ./src/*.go |
@@ -5,13 +5,14 @@ import ( | ||
5 | 5 | "io/ioutil" |
6 | 6 | "math" |
7 | 7 | "os" |
8 | + "path/filepath" | |
8 | 9 | "strings" |
10 | + "unicode" | |
9 | 11 | ) |
10 | 12 | |
11 | 13 | const ( |
12 | - IMax = int32(^uint32(0) >> 1) | |
13 | - IErr = ^IMax | |
14 | - PathDelimiters = "/\\" | |
14 | + IMax = int32(^uint32(0) >> 1) | |
15 | + IErr = ^IMax | |
15 | 16 | ) |
16 | 17 | |
17 | 18 | var randseed int32 |
@@ -176,22 +177,58 @@ func LoadText(filename string) (string, error) { | ||
176 | 177 | return AsciiToString(bytes), nil |
177 | 178 | } |
178 | 179 | func LoadFile(file *string, deffile string, load func(string) error) error { |
179 | - var filepath string | |
180 | - if li := strings.LastIndexAny(deffile, PathDelimiters); li >= 0 { | |
181 | - filepath = deffile[:li+1] + *file | |
182 | - if _, err := os.Stat(filepath); os.IsNotExist(err) { | |
183 | - filepath = "data/" + *file | |
180 | + var fp string | |
181 | + isNotExist := func() bool { | |
182 | + if _, err := os.Stat(fp); !os.IsNotExist(err) { | |
183 | + return false | |
184 | 184 | } |
185 | + var pattern string | |
186 | + for _, r := range fp { | |
187 | + if r >= 'A' && r <= 'Z' || r >= 'a' && r <= 'z' { | |
188 | + pattern += "[" + string(unicode.ToLower(r)) + | |
189 | + string(unicode.ToLower(r)+'A'-'a') + "]" | |
190 | + } else if r == '*' || r == '?' || r == '[' { | |
191 | + pattern += "\\" + string(r) | |
192 | + } else { | |
193 | + pattern += string(r) | |
194 | + } | |
195 | + } | |
196 | + if m, _ := filepath.Glob(pattern); len(m) > 0 { | |
197 | + fp = m[0] | |
198 | + return false | |
199 | + } | |
200 | + return true | |
201 | + } | |
202 | + *file = strings.Replace(*file, "\\", "/", -1) | |
203 | + defdir := filepath.Dir(strings.Replace(deffile, "\\", "/", -1)) | |
204 | + if defdir == "." { | |
205 | + fp = *file | |
206 | + } else if defdir == "/" { | |
207 | + fp = defdir + *file | |
185 | 208 | } else { |
186 | - filepath = "data/" + *file | |
187 | - } | |
188 | - if _, err := os.Stat(filepath); os.IsNotExist(err) { | |
189 | - filepath = *file | |
209 | + fp = defdir + "/" + *file | |
210 | + } | |
211 | + if isNotExist() { | |
212 | + _else := false | |
213 | + if defdir != "data" { | |
214 | + fp = "data/" + *file | |
215 | + if isNotExist() { | |
216 | + _else = true | |
217 | + } | |
218 | + } else { | |
219 | + _else = true | |
220 | + } | |
221 | + if _else { | |
222 | + if defdir != "." { | |
223 | + fp = *file | |
224 | + isNotExist() | |
225 | + } | |
226 | + } | |
190 | 227 | } |
191 | - if err := load(filepath); err != nil { | |
192 | - return Error(fmt.Sprintf("%s:\n%s", filepath, err.Error())) | |
228 | + if err := load(fp); err != nil { | |
229 | + return Error(fmt.Sprintf("%s:\n%s", fp, err.Error())) | |
193 | 230 | } |
194 | - *file = filepath | |
231 | + *file = fp | |
195 | 232 | return nil |
196 | 233 | } |
197 | 234 | func SplitAndTrim(str, sep string) (ss []string) { |
@@ -315,20 +352,23 @@ func readLayout(pre string, is IniSection) *Layout { | ||
315 | 352 | l := newLayout() |
316 | 353 | is.ReadF32(pre+"offset", &l.offset[0], &l.offset[1]) |
317 | 354 | is.ReadI32(pre+"displaytime", &l.displaytime) |
318 | - if str := is["facing"]; len(str) > 0 { | |
355 | + if str := is[pre+"facing"]; len(str) > 0 { | |
319 | 356 | if Atoi(str) < 0 { |
320 | 357 | l.facing = -1 |
321 | 358 | } else { |
322 | 359 | l.facing = 1 |
323 | 360 | } |
324 | 361 | } |
325 | - if str := is["vfacing"]; len(str) > 0 { | |
362 | + if str := is[pre+"vfacing"]; len(str) > 0 { | |
326 | 363 | if Atoi(str) < 0 { |
327 | 364 | l.vfacing = -1 |
328 | 365 | } else { |
329 | 366 | l.vfacing = 1 |
330 | 367 | } |
331 | 368 | } |
369 | + var ln int32 | |
370 | + is.ReadI32(pre+"layerno", &ln) | |
371 | + l.layerno = I32ToI16(Min(2, ln)) | |
332 | 372 | is.ReadF32(pre+"scale", &l.scale[0], &l.scale[1]) |
333 | 373 | return l |
334 | 374 | } |