func GetString(conf *allegro.Config, sec, key string, def string) string { val, ok := conf.Get(sec, key) if !ok { return def } return val }
func GetBool(conf *allegro.Config, sec, key string, def bool) bool { var val bool vals, ok := conf.Get(sec, key) if !ok { return def } _, err := fmt.Sscanf(vals, "%t", &val) if err != nil { log.Printf("%s.%s=%q not parseable as boolean", sec, key, vals) log.Printf("Defaulting to %s.%s=%t", sec, key, def) return def } return val }
func GetInt(conf *allegro.Config, sec, key string, def int) int { var val int vals, ok := conf.Get(sec, key) // Don't want to log something for every missing option if !ok { return def } _, err := fmt.Sscanf(vals, "%d", &val) if err != nil { log.Printf("%s.%s=%q not parseable as integer", sec, key, vals) log.Printf("Defaulting to %s.%s=%d", sec, key, def) return def } return val }
func loadFontConfig(rawConfig *allegro.Config, name, prefix, directory string) (FontConfig, bool) { var fontConf FontConfig if prefix != "" { fontConf.Name = prefix + "." + name } else { fontConf.Name = name } fname, ok := rawConfig.Get(name, "filename") if !ok { log.Printf("Resource %v has no filename field") log.Printf("Skipping resource") return fontConf, false } var filename string if fname != "builtin" { filename = path.Join(directory, fname) _, err := os.Open(filename) if os.IsNotExist(err) { log.Printf("Resource %v's assigned file did not exist: %v", name, filename) log.Printf("Skipping resource") return fontConf, false } } else { filename = fname } fontConf.Filename = filename strSize, ok := rawConfig.Get(name, "size") if !ok { strSize = DEFAULT_SIZE } if !sizeRegexp.MatchString(strSize) { log.Printf("Resource %v's size field is invalid: %v", name, strSize) log.Printf("Defaulting to %v", DEFAULT_SIZE) strSize = DEFAULT_SIZE } size, _ := strconv.Atoi(strSize) fontConf.Size = size return fontConf, true }
func loadTileConfig(rawConfig *allegro.Config, name, prefix, directory string) (TileConfig, bool) { var tileConf TileConfig if prefix != "" { tileConf.Name = prefix + "." + name } else { tileConf.Name = name } fname, ok := rawConfig.Get(name, "filename") if !ok { log.Printf("Resource %v has no filename field") log.Printf("Skipping resource") return tileConf, false } filename := path.Join(directory, fname) _, err := os.Open(filename) if os.IsNotExist(err) { log.Printf("Resource %v's assigned file did not exist: %v", name, filename) log.Printf("Skipping resource") return tileConf, false } tileConf.Filename = filename position, ok := rawConfig.Get(name, "position") if !ok { position = "0,0" } else if !positionRegexp.MatchString(position) { log.Printf("Resource %v's position field was not valid: %v", name, position) log.Printf("Using default of 0,0") position = "0,0" } // Already checked format with regexp, so don't need to check error codes // and the like split := strings.Split(position, ",") x, _ := strconv.Atoi(split[0]) y, _ := strconv.Atoi(split[1]) tileConf.X = x tileConf.Y = y dimensions, ok := rawConfig.Get(name, "dimensions") if !ok { dimensions = "0,0" } else if !dimensionsRegexp.MatchString(dimensions) { log.Printf("Resource %v's dimensions filed was not valid: %v", name, dimensions) log.Printf("Using default of 0,0") dimensions = "0,0" } split = strings.Split(dimensions, ",") w, _ := strconv.Atoi(split[0]) h, _ := strconv.Atoi(split[1]) tileConf.W = w tileConf.H = h offset, ok := rawConfig.Get(name, "offset") if !ok { offset = "0,0" } else if !offsetRegexp.MatchString(dimensions) { log.Printf("Resource %v's offset filed was not valid: %v", name, offset) log.Printf("Using default of 0,0") offset = "0,0" } split = strings.Split(offset, ",") ox, _ := strconv.Atoi(split[0]) oy, _ := strconv.Atoi(split[1]) tileConf.OffX = ox tileConf.OffY = oy return tileConf, true }