Exemplo n.º 1
0
// tilDump creates a dump directory and dumps the TIL file's squares using the
// pillars constructed based on the MIN format, once for each image config
// (pal).
func tilDump(tilName string) (err error) {
	squares, err := til.Parse(tilName)
	if err != nil {
		return err
	}
	nameWithoutExt := tilName[:len(tilName)-len(path.Ext(tilName))]
	minName := nameWithoutExt + ".min"
	pillars, err := min.Parse(minName)
	if err != nil {
		return err
	}
	imgName := nameWithoutExt + ".cel"
	relPalPaths := imgconf.GetRelPalPaths(imgName)
	for _, relPalPath := range relPalPaths {
		conf, err := cel.GetConf(imgName, relPalPath)
		if err != nil {
			return err
		}
		var palDir string
		if len(relPalPaths) > 1 {
			dbg.Println("using pal:", relPalPath)
			palDir = path.Base(relPalPath) + "/"
		}
		bar, err = barcli.New(len(squares))
		if err != nil {
			return err
		}
		levelFrames, err := cel.DecodeAll(imgName, conf)
		if err != nil {
			return err
		}
		dumpDir := path.Clean(dumpPrefix+"_squares_/"+nameWithoutExt) + "/" + palDir
		// prevent directory traversal
		if !strings.HasPrefix(dumpDir, dumpPrefix) {
			return fmt.Errorf("path (%s) contains no dump prefix (%s).", dumpDir, dumpPrefix)
		}
		err = os.MkdirAll(dumpDir, 0755)
		if err != nil {
			return err
		}
		err = dumpSquares(squares, pillars, levelFrames, dumpDir)
		if err != nil {
			return err
		}
	}
	return nil
}
Exemplo n.º 2
0
// dungeonDump creates a dump directory and stores the dungeon, which has been
// constructed based on the given DUN files, as a png image once for each image
// config (pal).
func dungeonDump(dungeonName string) (err error) {
	dunNames, err := dunconf.GetDunNames(dungeonName)
	if err != nil {
		return err
	}
	dungeon := dun.New()
	for _, dunName := range dunNames {
		err = dungeon.Parse(dunName)
		if err != nil {
			return fmt.Errorf("failed to parse %q: %s", dungeonName, err)
		}
	}
	colCount, err := dunconf.GetColCount(dungeonName)
	if err != nil {
		return err
	}
	rowCount, err := dunconf.GetRowCount(dungeonName)
	if err != nil {
		return err
	}
	nameWithoutExt, err := dun.GetLevelName(dunNames[0])
	if err != nil {
		return err
	}
	minName := nameWithoutExt + ".min"
	pillars, err := min.Parse(minName)
	if err != nil {
		return err
	}
	imgName := nameWithoutExt + ".cel"
	relPalPaths := imgconf.GetRelPalPaths(imgName)
	for _, relPalPath := range relPalPaths {
		conf, err := cel.GetConf(imgName, relPalPath)
		if err != nil {
			return err
		}
		var palDir string
		if len(relPalPaths) > 1 {
			dbg.Println("using pal:", relPalPath)
			palDir = dungeonName + "/"
		}
		levelFrames, err := cel.DecodeAll(imgName, conf)
		if err != nil {
			return err
		}
		dumpDir := path.Clean(dumpPrefix+"_dungeons_/") + "/" + palDir
		// prevent directory traversal
		if !strings.HasPrefix(dumpDir, dumpPrefix) {
			return fmt.Errorf("path (%s) contains no dump prefix (%s).", dumpDir, dumpPrefix)
		}
		err = os.MkdirAll(dumpDir, 0755)
		if err != nil {
			return err
		}
		dungeonPath := dumpDir + dungeonName + ".png"
		if len(relPalPaths) > 1 {
			palName := path.Base(relPalPath)
			palNameWithoutExt := palName[:len(palName)-len(path.Ext(palName))]
			dungeonPath = dumpDir + dungeonName + "_" + palNameWithoutExt + ".png"
		}
		dbg.Println("Creating image:", path.Base(dungeonPath))
		img := dungeon.Image(colCount, rowCount, pillars, levelFrames)
		err = imgutil.WriteFile(dungeonPath, img)
		if err != nil {
			return err
		}
	}
	return nil
}