Exemple #1
0
func LoadUserConfig(configLocation string) *allegro.Config {
	fname := os.ExpandEnv(configLocation)
	if exists(fname) {
		return allegro.LoadConfig(fname)
	}
	return allegro.CreateConfig()
}
Exemple #2
0
func LoadResourceManagerConfig(directory string, prefix string) (*ResourceManagerConfig, bool) {
	configFilename := path.Join(directory, "resources.ini")
	_, err := os.Open(configFilename)
	if os.IsNotExist(err) {
		return nil, false
	}

	var rmConfig ResourceManagerConfig
	rawConfig := allegro.LoadConfig(configFilename)
	for sectionName := range rawConfig.IterSections() {
		resourceType, ok := rawConfig.Get(sectionName, "type")
		if !ok {
			log.Printf("Section %v of %v resource file has no type field",
				sectionName, directory)
			log.Printf("Skipping section")
			continue
		}

		switch resourceType {
		case "tile":
			tileConfig, ok := loadTileConfig(rawConfig, sectionName, prefix, directory)
			if ok {
				rmConfig.TileConfigs = append(rmConfig.TileConfigs, tileConfig)
			}
		case "font":
			fontConfig, ok := loadFontConfig(rawConfig, sectionName, prefix, directory)
			if ok {
				rmConfig.FontConfigs = append(rmConfig.FontConfigs, fontConfig)
			}
		case "subdirectory":
			fname, ok := rawConfig.Get(sectionName, "filename")
			if !ok {
				log.Printf("Subdir %v had no filename field", sectionName)
				log.Printf("Skipping directory")
				continue
			}
			dirname := path.Join(directory, fname)
			stat, err := os.Stat(dirname)

			if err != nil || !stat.Mode().IsDir() {
				log.Printf("Subdir %v's filename field is not a directory: %v",
					sectionName, dirname)
				log.Printf("Skipping directory")
				continue
			}

			var subPrefix string
			if prefix != "" {
				subPrefix = prefix + "." + sectionName
			} else {
				subPrefix = sectionName
			}
			subConfig, ok := LoadResourceManagerConfig(dirname, subPrefix)
			if ok {
				rmConfig.Merge(subConfig)
			}
		default:
			log.Printf("Resource %q was of type %q that was not recognised",
				sectionName, resourceType)
			log.Printf("Skipping resource")
		}
	}

	return &rmConfig, true
}