load_house_page.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package Warehouse
  2. import (
  3. "fmt"
  4. "os"
  5. "io/ioutil"
  6. gotime "time"
  7. "strings"
  8. "encoding/json"
  9. "path"
  10. "path/filepath"
  11. "github.com/veandco/go-sdl2/ttf"
  12. //"github.com/cuu/gogame/image"
  13. //"github.com/cuu/gogame/draw"
  14. "github.com/cuu/gogame/color"
  15. "github.com/cuu/gogame/event"
  16. "github.com/cuu/gogame/time"
  17. "github.com/clockworkpi/LauncherGoDev/sysgo/UI"
  18. "github.com/cuu/grab"
  19. )
  20. type LoadHousePage struct {
  21. UI.Page
  22. ListFontObj *ttf.Font
  23. URLColor *color.Color
  24. TextColor *color.Color
  25. Downloader *grab.Client
  26. resp *grab.Response
  27. req *grab.Request
  28. URL string
  29. Downloading chan bool
  30. LoadingLabel UI.LabelInterface
  31. Parent *WareHouse
  32. }
  33. func NewLoadHousePage() *LoadHousePage {
  34. p := &LoadHousePage{}
  35. p.ListFontObj = UI.MyLangManager.TrFont("varela18")
  36. p.URLColor = UI.MySkinManager.GiveColor("URL")
  37. p.TextColor = UI.MySkinManager.GiveColor("Text")
  38. p.FootMsg = [5]string{"Nav.","","","Back","Cancel"}
  39. return p
  40. }
  41. func (self *LoadHousePage) Init() {
  42. self.PosX = self.Index * self.Screen.Width
  43. self.Width = self.Screen.Width
  44. self.Height = self.Screen.Height
  45. self.CanvasHWND = self.Screen.CanvasHWND
  46. self.LoadingLabel = UI.NewLabel()
  47. self.LoadingLabel.SetCanvasHWND(self.CanvasHWND)
  48. self.LoadingLabel.Init("Loading",self.ListFontObj,nil)
  49. self.LoadingLabel.SetColor(self.TextColor)
  50. self.Downloader = grab.NewClient()
  51. self.Downloading = make(chan bool,1)
  52. }
  53. func (self *LoadHousePage) SetDownloading(v bool) {
  54. for len(self.Downloading) > 0 {
  55. <- self.Downloading
  56. }
  57. self.Downloading <- v
  58. }
  59. func (self *LoadHousePage) OnLoadCb() {
  60. if len(self.URL) < 10 {
  61. return
  62. }
  63. self.ClearCanvas()
  64. self.Screen.Draw()
  65. self.Screen.SwapAndShow()
  66. //parts := strings.Split(self.URL,"/")
  67. //filename := strings.TrimSpace(parts[len(parts)-1])
  68. local_dir := strings.Split(self.URL,"raw.githubusercontent.com")
  69. home_path, _ := os.UserHomeDir()
  70. fmt.Println("LoadHouse OnLoadCb")
  71. if len(local_dir) > 1 {
  72. menu_file := local_dir[1]
  73. local_menu_file := fmt.Sprintf("%s/aria2downloads%s",
  74. home_path,menu_file)
  75. if UI.FileExists(local_menu_file) {
  76. var result WareHouseIndex
  77. jsonFile, err := os.Open(local_menu_file)
  78. if err != nil {
  79. fmt.Println(err)
  80. return
  81. }
  82. defer jsonFile.Close()
  83. byteValue, _ := ioutil.ReadAll(jsonFile)
  84. json.Unmarshal(byteValue, &result)
  85. self.Parent.MyStack.Push(result.List)
  86. self.Leave()
  87. } else {
  88. self.req,_ = grab.NewRequest("/tmp",self.URL)
  89. self.resp = self.Downloader.Do(self.req)
  90. self.SetDownloading(true)
  91. fmt.Println("Start Download index json to /tmp,grab")
  92. go self.UpdateProcessInterval(400)
  93. }
  94. }
  95. }
  96. func (self *LoadHousePage) UpdateProcessInterval(ms int) {
  97. ms_total := 0
  98. t := gotime.NewTicker(gotime.Duration(ms) * gotime.Millisecond)
  99. defer t.Stop()
  100. L:
  101. for {
  102. select {
  103. case <-t.C:
  104. fmt.Printf(" transferred %v / %v bytes (%.2f%%)\n",
  105. self.resp.BytesComplete(),
  106. self.resp.Size,
  107. 100*self.resp.Progress())
  108. ms_total += ms
  109. if(ms_total > 5000) {
  110. fmt.Println("LoadHouse Timeout")
  111. break L
  112. }
  113. case <-self.resp.Done:
  114. // download is complete
  115. fmt.Println("Grab Download House done")
  116. break L
  117. case v:= <- self.Downloading:
  118. if v == false {
  119. break L
  120. }
  121. }
  122. }
  123. dst_filename := self.resp.Filename
  124. fmt.Println("dst_filename ",dst_filename)
  125. if err := self.resp.Err(); err == nil {//download successfully
  126. home_path, _ := os.UserHomeDir()
  127. parts := strings.Split(self.URL,"/")
  128. filename := strings.TrimSpace(parts[len(parts)-1])
  129. local_dir := strings.Split(self.URL,"raw.githubusercontent.com")
  130. local_menu_file := ""
  131. menu_file := ""
  132. if len(local_dir) > 1 {
  133. menu_file = local_dir[1]
  134. local_menu_file = fmt.Sprintf("%s/aria2downloads%s",
  135. home_path,menu_file)
  136. }
  137. dl_file := path.Join("/tmp",filename)
  138. fmt.Println("dl_file: ",dl_file)
  139. fmt.Println(local_menu_file)
  140. if UI.IsDirectory( filepath.Dir(local_menu_file) ) == false {
  141. merr := os.MkdirAll( filepath.Dir(local_menu_file), os.ModePerm)
  142. if merr != nil {
  143. panic(merr)
  144. }
  145. }
  146. UI.CopyFile(dl_file,local_menu_file)
  147. os.Remove(dl_file)
  148. var result WareHouseIndex
  149. jsonFile, err := os.Open(local_menu_file)
  150. if err != nil {
  151. fmt.Println(err)
  152. return
  153. }
  154. defer jsonFile.Close()
  155. byteValue, _ := ioutil.ReadAll(jsonFile)
  156. json.Unmarshal(byteValue, &result)
  157. self.Parent.MyStack.Push(result.List)
  158. self.Leave()
  159. } else {
  160. fmt.Println(err)
  161. self.Screen.MsgBox.SetText("Fetch house failed")
  162. self.Screen.MsgBox.Draw()
  163. self.Screen.SwapAndShow()
  164. time.BlockDelay(500)
  165. }
  166. }
  167. func (self *LoadHousePage) Leave() {
  168. self.SetDownloading(false)
  169. self.ReturnToUpLevelPage()
  170. self.Screen.Draw()
  171. self.Screen.SwapAndShow()
  172. self.URL = ""
  173. }
  174. func (self *LoadHousePage) KeyDown(ev *event.Event) {
  175. if UI.IsKeyMenuOrB(ev.Data["Key"]) {
  176. self.Leave()
  177. }
  178. }
  179. func (self *LoadHousePage) Draw() {
  180. self.ClearCanvas()
  181. w,_ := self.LoadingLabel.Size()
  182. self.LoadingLabel.NewCoord( (UI.Width - w)/2,(UI.Height-44)/2);
  183. self.LoadingLabel.Draw()
  184. }