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) } } }
func checkMd5(path string) bool { Log.PrintfI("start check %v md5.\n", path) if !tools.CheckValidZip(path) { Log.PrintfE("server template %v isn't a valid zip file.", path) return false } name := strings.Split(filepath.Base(path), ".")[0] m := strings.Split(name, "_") if len(m) != 2 && len(m) != 3 { Log.PrintfE("parse server teamplate name faild.%v\n", m) return false } str, err := tools.Md5(path) if err != nil { Log.PrintfE("get md5 error %v\n", err) return false } Log.PrintfI("%v %v\n", path, str) return strings.ToUpper(m[1]) == str }
func getServerTempale(path string) (string, error) { Files, err := ioutil.ReadDir(path) if err != nil { return "", err } var list info for _, v := range Files { if v.IsDir() { continue } if !tools.CheckValidZip(path+v.Name()) || !strings.Contains(v.Name(), ".zip") { continue } list = append(list, v) } if len(list) == 0 { return "", errors.New("can't find valid file.") } list.Sort() return list[0].Name(), nil }