Пример #1
0
func getServerTemplate(path string) {
	url := getUrl() + "/download?path=" + path
	Log.PrintfI("start download server template %v\n", url)
	resp, err := client().Get(url)
	if err != nil {
		Log.PrintfE("%v\n", err)
		return
	}
	defer resp.Body.Close()
	name := filepath.Base(path)
	//应该判断下Config.TmpDir 是否包含"/"
	File, err := os.Create(Config.TmpDir + "/" + name)
	if err != nil {
		Log.PrintfF("%v\n", err)
		return
	}
	io.Copy(File, resp.Body)
	File.Close()
	if Config.CheckMd5 {
		if !checkMd5(Config.TmpDir + "/" + name) {
			Log.PrintfF("%v\n", "md5 different")
		}
	}
	tools.Unzip(Config.TmpDir+"/"+name, Config.ProgramHome, Log)
}
Пример #2
0
func Update() {
	var m url.Values = make(url.Values)
	m.Set("gameid", Config.GameId)
	if updateDir != "" {
		m.Set("dir", updateDir)
	}
	url := getUrl()
	resq, err := client().PostForm(url+"update", m)
	if err != nil {
		Log.PrintfE("%s\n", err)
		return
	}
	if resq.StatusCode != 200 {
		Log.PrintfE("ErrorCode: %d\n", resq.StatusCode)
		return
	}
	defer resq.Body.Close()
	var x updateResult
	buf, _ := ioutil.ReadAll(resq.Body)
	err = json.Unmarshal(buf, &x)
	if err != nil {
		return
	}
	tmpdir := Config.TmpDir + "/" + filepath.Base(x.Path)
	err = os.MkdirAll(tmpdir, 0644)
	if err != nil {
		Log.PrintfE("%s\n", err)
		return
	}
	var updatelist []string
	for _, v := range x.Files {
		url := fmt.Sprintf("%sdownload?path=%s%s", url, x.Path, v)
		Log.PrintfI("%s\n", url)
		b, err := tools.Wget(url, tmpdir+"/"+v)
		if !b {
			Log.PrintfF("%s\n", err)
		}
		if !tools.CheckValidZip(tmpdir + "/" + v) {
			Log.PrintfW("%v isn't a valid zip file.\n", tmpdir+"/"+v)
			continue
		}
		if !checkMd5(tmpdir + "/" + v) {
			Log.PrintfF("check %v md5 faild.\n", tmpdir+"/"+v)
		}
		updatelist = append(updatelist, tmpdir+"/"+v)
	}
	Log.PrintfI("update files %v\n", updatelist)
	for _, zipfile := range updatelist {
		Log.PrintfI("start unzip %v\n", zipfile)
		if err := tools.Unzip(zipfile, Config.ProgramHome, Log); err != nil {
			Log.PrintfE("%v\n", err)
		}
	}
}