Esempio n. 1
0
func (sh *savehelper) UnpackAll() {
	z := sh.getzip()
	defer util.MustClose(z)
	for i := 0; i < len(z.File); i++ {
		zi := z.File[i]
		fln := "saves/" + zi.Name

		if zi.FileInfo().IsDir() {
			util.Must(util.MkDirIfNotExist(fln))
		} else {
			zf, zfe := zi.Open()
			util.Must(zfe)

			fmt.Printf("Unpacking %#v...\n", zi.Name)
			pr := util.NewReadProgress(zf, zi.UncompressedSize64)

			f, fe := os.Create(fln)
			util.Must(fe)
			defer util.MustClose(f)

			_, ce := io.Copy(f, pr)
			util.Must(ce)
			util.MustClose(zf)
			util.MustClose(pr)
		}
	}
}
Esempio n. 2
0
// UnpackModpack downloads mods and unpacks file contents.
func UnpackModpack(fname string) {
	z, ze := zip.OpenReader(fname)
	util.Must(ze)
	defer util.MustClose(z)
	info := new(modpackmanifest)
	var zf *zip.File
	ov := "overrides/"
	buf := make([]byte, 32*1024)
	for _, zf = range z.File {
		if zf.Name == "manifest.json" {
			ozf, oze := zf.Open()
			util.Must(oze)
			util.Must(json.NewDecoder(ozf).Decode(info))
			util.MustClose(ozf)
			break
		}
	}
	if info != nil {
		ov = info.Overrides + "/"
		for i, xf := range info.Files {
			pid := strconv.FormatInt(int64(xf.PID), 10)
			pkg := GetPackage(pid)
			if pkg == nil {
				fmt.Printf("Package with ID %s is missing!\n", pid)
				continue
			}
			fmt.Printf("%d / %d ", i, len(info.Files))
			pkg.DownloadFileWithID(strconv.FormatInt(int64(xf.FID), 10), buf)
		}
	}
	lov := len(ov)
	for _, zf = range z.File {
		if len(zf.Name) < lov {
			continue
		}
		n := zf.Name[:lov]
		if n == ov {
			n = zf.Name[lov:]
			if n == "" {
				continue
			}
			if zf.FileInfo().IsDir() {
				util.Must(util.MkDirIfNotExist(n))
			} else {
				xf, xe := zf.Open()
				util.Must(xe)
				fmt.Printf("Unpacking %#v...\n", n)
				pr := util.NewReadProgress(xf, zf.UncompressedSize64)
				f, fe := os.Create(n)
				util.Must(fe)
				_, ce := io.CopyBuffer(f, pr, buf)
				util.Must(ce)
				util.MustClose(f)
				util.MustClose(xf)
				pr.Close()
			}
		}
	}
}
Esempio n. 3
0
// DownloadFileWithID downloads file with a specified ID and unpacks its contents if necessary.
func (pkg *MCPMPackage) DownloadFileWithID(fid string, buf []byte) {
	if buf == nil {
		buf = make([]byte, 32*1024)
	}
	po := util.GetPackageOptionsB(pkg.ptype)
	util.Must(util.MkDirIfNotExist(po.Dir))
	us := fmt.Sprintf("http://minecraft.curseforge.com/projects/%s/files/%s/download", pkg.name, fid)
	ht, hte := get(us)
	defer util.MustClose(ht.Body)
	util.Must(hte)
	fname := ht.Request.URL.Path
	fname = fname[strings.LastIndex(fname, "/")+1:]
	sav := util.DirPathJoin(po.Dir, fname)
	sf, sfe := os.OpenFile(sav, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
	util.Must(sfe)
	defer util.MustClose(sf)
	fmt.Printf("Downloading \"%s\": %s\n", pkg.name, fname)
	pr := util.NewReadProgress(ht.Body, uint64(ht.ContentLength))
	_, ce := io.CopyBuffer(sf, pr, buf)
	util.Must(ce)
	util.MustClose(pr)
	fmt.Printf("Successfully saved to \"%s\"\n", sav)
	if po.ShouldUnpack {
		switch pkg.ptype {
		case "modpacks":
			UnpackModpack(sav)
			fmt.Printf("Successfully installed modpack %#v\n", pkg.title)
			break
		case "worlds":
			svh := helper.NewSaveHelper(sav)
			svh.UnpackAll()
			fmt.Printf("Successfully unpacked world save %#v\n", pkg.title)
			break
		}
	}
}