Beispiel #1
0
// Handles the websocket requests
func logServer(logFolder string) func(ws *websocket.Conn) {
	return func(ws *websocket.Conn) {
		var wg sync.WaitGroup
		// for each log we get from the folder given
		for log := range getLogs(logFolder) {
			// Don't do anything for spectated games -- the spectator doesn't receive
			// the id of the person they are spectating, nor their own, so there'd be a mix of
			// games under the spectated "user"
			if log.Uploader != "0" {
				// if we don't know who our client is, and the game wasn't spectating
				// then write the config so that we can show a link to their games from
				// the start
				if conf.Player == "" {
					conf.Player = log.Uploader
					common.WriteLocalConfig(conf)
				}
				// do the uploading
				wg.Add(1)
				go upload(log, ws, &wg)
			}
			// Even if it's spectated, we want to update the user on what happened
			send(ws, log)
		}
		// Wait for all logs to finish uploading, close the websocket, and quit
		wg.Wait()
		ws.Close()
		// if it's debug don't quit straight away - otherwise we lose the debug information
		// that's printed out.
		if debug != "" {
			reader := bufio.NewReader(os.Stdin)
			_, _ = reader.ReadString('\n')
		}
		os.Exit(0)
	}
}
// Load the config, and get everything setup correctly
//  - Determine install locations
//  - Determine log locations
//  - Check that the log configuration for HS is setup
func checkLocalConfig() bool {
	header("Checking HSR client config")
	cf, err := os.Open(common.GetLocalConfigFile())

	suffix := "exe"
	if runtime.GOOS == "darwin" {
		suffix = "app"
	}
	suffix = fmt.Sprintf("Hearthstone.%s", suffix)

	// Here, the config file did not exist, so construct all the data
	// that we need, and write it
	if os.IsNotExist(err) {
		fmt.Printf("Determining install location:\n")
		if conf.Install, err = location.Location(); err != nil {
			fmt.Printf("Could not determine location automatically\n")
			reader := bufio.NewScanner(os.Stdin)
			fmt.Println("Please enter it: ")
			for reader.Scan() {
				path := filepath.Clean(reader.Text())
				if !strings.HasSuffix(path, suffix) {
					path = filepath.Join(path, suffix)
				}
				// Check that Hearthstone exists in the directory they specified
				_, err = os.Stat(path)
				if err != nil {
					fmt.Printf("Invalid location, tried %s\n", filepath.Dir(path))
					fmt.Println("Please enter it again:")
				} else {
					conf.Install.LogFolder = filepath.Dir(path)
					break
				}
			}
		}
		// write the config out, and reload it
		common.WriteLocalConfig(conf)
		if cf, err = os.Open(common.GetLocalConfigFile()); err != nil {
			fmt.Printf("%#v", err)
			return false
		}
	} else if err != nil {
		fmt.Printf("%#v", err)
		return false
	} else {
		fmt.Printf("Already determined install location:\n")
	}
	defer cf.Close()

	// Attempt to load the config
	if err = json.NewDecoder(cf).Decode(&conf); err != nil {
		fmt.Printf("%#v", err)
		return false
	}

	// Debug/useful information -- so if it's wrong they can change it
	fmt.Printf("\tLog folder: %#s\n", conf.Install.LogFolder)
	fmt.Printf("\tHS log config: %#s\n", conf.Install.Config)
	return true
}
// The last check the bootstrapper does is check that the version of the client that's
// installed is the latest one
func checkLatest() bool {
	var m VersionUpdate
	header("Checking version of client")

	resp, err := http.Get(update_url)
	if err != nil {
		fmt.Printf("%#v", err)
		return false
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		fmt.Printf("Server returned bad status: %s\n", resp.Status)
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Printf("%#v", err)
		return false
	}
	err = json.Unmarshal(body, &m)

	if conf.Version == m.Version {
		fmt.Printf("%s is the latest version!\n", conf.Version)
	} else {
		fmt.Printf("Need to download new version: %s\n", m.Version)
		url := fmt.Sprintf("https://s3-us-west-2.amazonaws.com/update.hearthreplay.com/hearthreplay-client-%s-%s-%s", m.Version, runtime.GOOS, runtime.GOARCH)

		resp, err = http.Get(url)

		if err != nil {
			fmt.Printf("%#v", err)
			return false
		}
		defer resp.Body.Close()
		if resp.StatusCode != http.StatusOK {
			fmt.Printf("Update server returned bad status: %s\n", resp.Status)
			return false
		}

		i, _ := strconv.Atoi(resp.Header.Get("Content-Length"))

		bar := pb.New(i).SetUnits(pb.U_BYTES).SetRefreshRate(time.Millisecond * 100)
		bar.ShowSpeed = true
		bar.Start()

		var buf bytes.Buffer
		writer := io.MultiWriter(&buf, bar)
		io.Copy(writer, resp.Body)
		err := verifiedUpdate(&buf, m)
		if err != nil {
			fmt.Printf("%#v", err)
			return false
		}
		bar.Finish()

		// if we've updated successfully, update the configuration file to reflect that
		conf.Version = m.Version
		if ok := common.WriteLocalConfig(conf); !ok {
			return false
		}
	}
	return true
}