123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- package Warehouse
- import (
- "fmt"
- "os"
- "io/ioutil"
- gotime "time"
- "strings"
- "encoding/json"
- "path"
- "path/filepath"
-
- "github.com/veandco/go-sdl2/ttf"
-
- //"github.com/cuu/gogame/image"
- //"github.com/cuu/gogame/draw"
- "github.com/cuu/gogame/color"
- "github.com/cuu/gogame/event"
- //"github.com/cuu/gogame/time"
- "github.com/clockworkpi/LauncherGoDev/sysgo/UI"
- "github.com/cuu/grab"
- )
- type WareHouseIndex struct {
- List []map[string]string `json:"list"`
- }
- type LoadHousePage struct {
- UI.Page
- ListFontObj *ttf.Font
- URLColor *color.Color
- TextColor *color.Color
- Downloader *grab.Client
- resp *grab.Response
- req *grab.Request
-
- URL string
- Downloading chan bool
- LoadingLabel UI.LabelInterface
- Parent *WareHouse
- }
- func NewLoadHousePage() *LoadHousePage {
- p := &LoadHousePage{}
- p.ListFontObj = UI.MyLangManager.TrFont("varela18")
- p.URLColor = UI.MySkinManager.GiveColor("URL")
- p.TextColor = UI.MySkinManager.GiveColor("Text")
- p.FootMsg = [5]string{"Nav.","","","Back","Cancel"}
- return p
- }
- func (self *LoadHousePage) Init() {
-
- self.PosX = self.Index * self.Screen.Width
- self.Width = self.Screen.Width
- self.Height = self.Screen.Height
- self.CanvasHWND = self.Screen.CanvasHWND
- self.LoadingLabel = UI.NewLabel()
- self.LoadingLabel.SetCanvasHWND(self.CanvasHWND)
- self.LoadingLabel.Init("Loading",self.ListFontObj,nil)
- self.LoadingLabel.SetColor(self.TextColor)
- self.Downloader = grab.NewClient()
- self.Downloading = make(chan bool,1)
-
- }
- func (self *LoadHousePage) SetDownloading(v bool) {
- for len(self.Downloading) > 0 {
- <- self.Downloading
- }
- self.Downloading <- v
- }
- func (self *LoadHousePage) OnLoadCb() {
-
- if len(self.URL) < 10 {
- return
- }
- self.ClearCanvas()
- self.Screen.Refresh()
- //parts := strings.Split(self.URL,"/")
- //filename := strings.TrimSpace(parts[len(parts)-1])
- local_dir := strings.Split(self.URL,"raw.githubusercontent.com")
- home_path, _ := os.UserHomeDir()
- fmt.Println("LoadHouse OnLoadCb")
- if len(local_dir) > 1 {
- menu_file := local_dir[1]
- local_menu_file := fmt.Sprintf(aria2dl_folder,
- home_path,menu_file)
- if UI.FileExists(local_menu_file) {
- var result WareHouseIndex
- jsonFile, err := os.Open(local_menu_file)
- if err != nil {
- fmt.Println(err)
- return
- }
- defer jsonFile.Close()
- byteValue, _ := ioutil.ReadAll(jsonFile)
- json.Unmarshal(byteValue, &result)
- self.Parent.MyStack.Push(result.List)
-
- self.Leave()
- } else {
-
- self.req,_ = grab.NewRequest("/tmp",self.URL)
- self.resp = self.Downloader.Do(self.req)
- self.SetDownloading(true)
- fmt.Println("Start Download index json to /tmp,grab")
- go self.UpdateProcessInterval(400)
- }
-
- }
- }
- func (self *LoadHousePage) UpdateProcessInterval(ms int) {
- ms_total := 0
- t := gotime.NewTicker(gotime.Duration(ms) * gotime.Millisecond)
- defer t.Stop()
- L:
- for {
- select {
- case <-t.C:
- fmt.Printf(" transferred %v / %v bytes (%.2f%%)\n",
- self.resp.BytesComplete(),
- self.resp.Size,
- 100*self.resp.Progress())
- ms_total += ms
- if(ms_total > 5000) {
- fmt.Println("LoadHouse Timeout")
- break L
- }
- case <-self.resp.Done:
- // download is complete
- fmt.Println("Grab Download House done")
- break L
- case v:= <- self.Downloading:
- if v == false {
- break L
- }
- }
- }
-
- dst_filename := self.resp.Filename
- fmt.Println("dst_filename ",dst_filename)
-
- if err := self.resp.Err(); err == nil {//download successfully
- home_path, _ := os.UserHomeDir()
- parts := strings.Split(self.URL,"/")
- filename := strings.TrimSpace(parts[len(parts)-1])
- local_dir := strings.Split(self.URL,"raw.githubusercontent.com")
- local_menu_file := ""
- menu_file := ""
-
- if len(local_dir) > 1 {
- menu_file = local_dir[1]
- local_menu_file = fmt.Sprintf(aria2dl_folder,
- home_path,menu_file)
- }
- dl_file := path.Join("/tmp",filename)
- fmt.Println("dl_file: ",dl_file)
- fmt.Println(local_menu_file)
-
- if UI.IsDirectory( filepath.Dir(local_menu_file) ) == false {
- merr := os.MkdirAll( filepath.Dir(local_menu_file), os.ModePerm)
- if merr != nil {
- panic(merr)
- }
- }
- UI.CopyFile(dl_file,local_menu_file)
-
- os.Remove(dl_file)
-
- var result WareHouseIndex
- jsonFile, err := os.Open(local_menu_file)
- if err != nil {
- fmt.Println(err)
- return
- }
- defer jsonFile.Close()
- byteValue, _ := ioutil.ReadAll(jsonFile)
- json.Unmarshal(byteValue, &result)
- self.Parent.MyStack.Push(result.List)
- self.Leave()
-
- } else {
- fmt.Println(err)
- self.Screen.ShowMsg("Fetch house failed",500)
- }
-
- }
- func (self *LoadHousePage) Leave() {
- self.SetDownloading(false)
-
- self.ReturnToUpLevelPage()
- self.Screen.Refresh()
- self.URL = ""
-
- }
- func (self *LoadHousePage) KeyDown(ev *event.Event) {
- if UI.IsKeyMenuOrB(ev.Data["Key"]) {
- self.Leave()
- }
-
- }
- func (self *LoadHousePage) Draw() {
- self.ClearCanvas()
- w,_ := self.LoadingLabel.Size()
- self.LoadingLabel.NewCoord( (UI.Width - w)/2,(UI.Height-44)/2);
- self.LoadingLabel.Draw()
-
- }
|