warehouse aplpha test pass

This commit is contained in:
cuu
2021-10-24 19:29:20 +08:00
parent 94245a58e8
commit 5cb318cd56
12 changed files with 426 additions and 67 deletions

View File

@@ -72,9 +72,16 @@ func (self *ImageDownloadProcessPage) Init() {
self.LoadingLabel = LoadingLabel
self.Downloader = grab.NewClient()
self.Downloading = make(chan bool)
self.Downloading = make(chan bool,1)
}
func (self *ImageDownloadProcessPage) SetDownloading(v bool) {
for len(self.Downloading) > 0 {
<- self.Downloading
}
self.Downloading <- v
}
func (self *ImageDownloadProcessPage) OnLoadCb() {
@@ -104,10 +111,8 @@ func (self *ImageDownloadProcessPage) OnLoadCb() {
self.req,_ = grab.NewRequest("/tmp",self.URL)
self.resp = self.Downloader.Do(self.req)
for len(self.Downloading) > 0 {
<-self.Downloading
}
self.Downloading <- true
self.SetDownloading(true)
go self.UpdateProcessInterval(400)
@@ -116,10 +121,10 @@ func (self *ImageDownloadProcessPage) OnLoadCb() {
}
func (self *ImageDownloadProcessPage) UpdateProcessInterval(ms int) {
ms_total := 0
t := gotime.NewTicker(gotime.Duration(ms) * gotime.Millisecond)
defer t.Stop()
L:
for {
select {
case <-t.C:
@@ -127,14 +132,17 @@ func (self *ImageDownloadProcessPage) UpdateProcessInterval(ms int) {
self.resp.BytesComplete(),
self.resp.Size,
100*self.resp.Progress())
ms_total += ms
if(ms_total > 10000) {
fmt.Println("Get preview image timeout")
break L
}
case <-self.resp.Done:
// download is complete
break
break L
case v:= <-self.Downloading:
if v == false {
t.Stop()
break
break L
}
}
}
@@ -184,7 +192,7 @@ func (self *ImageDownloadProcessPage) KeyDown(ev *event.Event) {
if UI.IsKeyMenuOrB(ev.Data["Key"]) {
self.Downloading <- false
self.SetDownloading(false)
self.ReturnToUpLevelPage()
self.Screen.Draw()

View File

@@ -8,6 +8,7 @@ import (
"strings"
"encoding/json"
"path"
"path/filepath"
"github.com/veandco/go-sdl2/ttf"
@@ -15,6 +16,7 @@ import (
//"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"
)
@@ -60,10 +62,18 @@ func (self *LoadHousePage) Init() {
self.LoadingLabel.SetColor(self.TextColor)
self.Downloader = grab.NewClient()
self.Downloading = make(chan bool)
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 {
@@ -77,7 +87,7 @@ func (self *LoadHousePage) OnLoadCb() {
//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("%s/aria2downloads%s",
@@ -93,21 +103,16 @@ func (self *LoadHousePage) OnLoadCb() {
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
json.Unmarshal(byteValue, &result)
for _, repo := range result.List {
self.Parent.MyStack.Push(repo)
}
self.Parent.MyStack.Push(result.List)
self.Leave()
} else {
self.req,_ = grab.NewRequest("/tmp",self.URL)
self.resp = self.Downloader.Do(self.req)
for len(self.Downloading) > 0 {
<-self.Downloading
}
self.Downloading <- true
self.SetDownloading(true)
fmt.Println("Start Download index json to /tmp,grab")
go self.UpdateProcessInterval(400)
}
@@ -115,9 +120,10 @@ func (self *LoadHousePage) OnLoadCb() {
}
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:
@@ -125,20 +131,25 @@ func (self *LoadHousePage) UpdateProcessInterval(ms int) {
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
break
case v:= <-self.Downloading:
fmt.Println("Grab Download House done")
break L
case v:= <- self.Downloading:
if v == false {
t.Stop()
break
break L
}
}
}
//dst_filename := self.resp.Filename
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,"/")
@@ -154,14 +165,21 @@ func (self *LoadHousePage) UpdateProcessInterval(ms int) {
home_path,menu_file)
}
dl_file := path.Join("/tmp",filename)
if UI.IsDirectory( path.Base(local_menu_file) ) == false {
merr := os.MkdirAll( path.Base(local_menu_file), os.ModePerm)
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 {
@@ -171,24 +189,22 @@ func (self *LoadHousePage) UpdateProcessInterval(ms int) {
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
json.Unmarshal(byteValue, &result)
for _, repo := range result.List {
self.Parent.MyStack.Push(repo)
}
self.Parent.MyStack.Push(result.List)
self.Leave()
} else {
fmt.Println(err)
self.Screen.MsgBox.SetText("Fetch house failed")
self.Screen.MsgBox.Draw()
self.Screen.SwapAndShow()
time.BlockDelay(500)
}
}
func (self *LoadHousePage) Leave() {
self.Downloading <- false
self.SetDownloading(false)
self.ReturnToUpLevelPage()
self.Screen.Draw()

View File

@@ -0,0 +1,5 @@
{
"SO_FILE":"",
"NAME":"Warehouse"
}

View File

@@ -19,10 +19,12 @@ type WareHousePlugin struct {
MainPage *WareHouse
}
var aria2dl_folder = "%s/aria2downloads%s"
func (self *WareHousePlugin) Init(main_screen *UI.MainScreen) {
self.MainPage = NewWareHouse()
self.MainPage.SetScreen(main_screen)
self.MainPage.SetName("Tiny cloud")
self.MainPage.SetName("Warehouse")
self.MainPage.Init()
}

View File

@@ -13,7 +13,7 @@ type WareHouseListItem struct {
Type string
Value map[string]string
Parent *WareHouse
}
@@ -86,7 +86,7 @@ func (self *WareHouseListItem) Draw() {
if self.Type == "source" || self.Type == "dir" {
_,h := self.Icons["ware"].Size()
self.Icons["ware"].NewCoord(4,(self.Height - h)/2)
self.Icons["ware"].NewCoord(4,self.PosY + (self.Height - h)/2)
self.Icons["ware"].DrawTopLeft()
}
@@ -96,13 +96,13 @@ func (self *WareHouseListItem) Draw() {
_icon = "appdling"
}
_,h := self.Icons[_icon].Size()
self.Icons[_icon].NewCoord(4,(self.Height - h )/2)
self.Icons[_icon].NewCoord(4,self.PosY + (self.Height - h )/2)
self.Icons[_icon].DrawTopLeft()
}
if self.Type == "add_house" {
_,h := self.Icons["add"].Size()
self.Icons["add"].NewCoord(4,(self.Height - h)/2)
self.Icons["add"].NewCoord(4,self.PosY+(self.Height - h)/2)
self.Icons["add"].DrawTopLeft()
}

View File

@@ -11,6 +11,7 @@ import (
"io/ioutil"
"path/filepath"
"encoding/json"
"reflect"
"database/sql"
_ "github.com/mattn/go-sqlite3"
@@ -228,6 +229,12 @@ func (self *WareHouse) SyncTasks() []map[string]string {
return ret
}
func IsSlice(v interface{}) bool {
if reflect.TypeOf(v).Kind() == reflect.Slice || reflect.TypeOf(v).Kind() == reflect.Array {
return true
}
return false
}
func (self *WareHouse) SyncList() {
@@ -240,10 +247,17 @@ func (self *WareHouse) SyncList() {
var repos []map[string]string
fmt.Printf("SyncList: %+v\n", self.MyStack)
stk := self.MyStack.Last()
stk_len := self.MyStack.Length()
repos = append(repos, stk.(map[string]string))
if IsSlice(stk) {
repos = append(repos, stk.([]map[string]string)...)
}else {
repos = append(repos, stk.(map[string]string))
}
add_new_house := make(map[string]string)
add_new_house["title"] = "Add new warehouse..."
@@ -266,7 +280,8 @@ func (self *WareHouse) SyncList() {
}
for _, u := range repos {
li := &WareHouseListItem{}
fmt.Printf("%+v\n",u)
li := NewWareHouseListItem()
li.Parent = self
li.PosX = start_x
li.PosY = start_y + last_height
@@ -285,7 +300,8 @@ func (self *WareHouse) SyncList() {
if err != nil {
log.Fatal( err )
}
local_menu_file := fmt.Sprintf("%s/aria2download%s",home_path,menu_file)
local_menu_file := fmt.Sprintf(aria2dl_folder,home_path,menu_file)
fmt.Println("for loop ",local_menu_file)
if UI.FileExists(local_menu_file) {
li.ReadOnly = false
}else {
@@ -350,12 +366,16 @@ func (self *WareHouse) Init() {
self.RemovePage.StartOrAEvent = self.RemoveGame
self.RemovePage.Name = "Are you sure?"
self.RemovePage.Init()
self.Keyboard = UI.NewKeyboard()
self.Keyboard.Name = "Enter warehouse addr"
self.Keyboard.FootMsg = [5]string{"Nav.","Add","ABC","Backspace","Enter"}
self.Keyboard.Screen = self.Screen
self.Keyboard.Init()
self.Keyboard.SetPassword("github.com/cuu/warehouse")
self.Keyboard.Caller = self
self.PreviewPage = NewImageDownloadProcessPage()
self.PreviewPage.Screen = self.Screen
self.PreviewPage.Name ="Preview"
@@ -399,7 +419,7 @@ func (self *WareHouse) ResetHouse() {
remote_file_url := cur_li.Value["file"]
parts := strings.Split(remote_file_url,"raw.githubusercontent.com")
menu_file := parts[1]
local_menu_file := fmt.Sprintf("%s/aria2download%s",home_path,menu_file)
local_menu_file := fmt.Sprintf(aria2dl_folder,home_path,menu_file)
local_menu_file_path := filepath.Dir(local_menu_file)
fmt.Println(local_menu_file)
@@ -467,11 +487,14 @@ func (self *WareHouse) UrlIsDownloading(url string) (string,bool) {
if uris,err := self.rpcc.GetURIs(v.Gid);err == nil {
for _,x := range uris {
if x.URI == url {
fmt.Println(x.URI," ",url)
return v.Gid,true
}
}
}
}
}else {
log.Fatal(err)
}
return "",false
}
@@ -480,6 +503,7 @@ func (self *WareHouse) RemoveGame() {
if self.PsIndex > len(self.MyList) -1 {
return
}
fmt.Println("RemoveGame")
cur_li := self.MyList[self.PsIndex].(*WareHouseListItem)
fmt.Println("Remove cur_li._Value",cur_li.Value)
@@ -505,7 +529,7 @@ func (self *WareHouse) RemoveGame() {
remote_file_url := cur_li.Value["file"]
parts := strings.Split(remote_file_url,"raw.githubusercontent.com")
menu_file := parts[1]
local_menu_file := fmt.Sprintf("%s/aria2download%s",home_path,menu_file)
local_menu_file := fmt.Sprintf(aria2dl_folder,home_path,menu_file)
local_menu_file_path := filepath.Dir(local_menu_file)
gid,ret := self.UrlIsDownloading(remote_file_url)
@@ -532,14 +556,14 @@ func (self *WareHouse) Click() {
}
cur_li := self.MyList[self.PsIndex].(*WareHouseListItem)
home_path, _ := os.UserHomeDir()
fmt.Println("cur_li._Value",cur_li.Value)
fmt.Println("Click cur_li._Value",cur_li.Value)
if cur_li.Value["type"] == "source" || cur_li.Value["type"] == "dir" {
remote_file_url := cur_li.Value["file"]
parts := strings.Split(remote_file_url,"raw.githubusercontent.com")//assume master branch
menu_file := parts[1]
local_menu_file := fmt.Sprintf("%s/aria2download%s",home_path,menu_file)
fmt.Println(local_menu_file)
local_menu_file := fmt.Sprintf(aria2dl_folder,home_path,menu_file)
fmt.Println("warehouse click: ",local_menu_file)
if UI.FileExists(local_menu_file) == false {
self.LoadHouse()
}else {
@@ -559,11 +583,8 @@ func (self *WareHouse) Click() {
byteValue, _ := ioutil.ReadAll(jsonFile)
json.Unmarshal(byteValue, &result)
self.MyStack.Push(result.List)
for _, repo := range result.List {
self.MyStack.Push(repo)
}
self.SyncList()
self.Screen.Draw()
self.Screen.SwapAndShow()
@@ -581,17 +602,28 @@ func (self *WareHouse) Click() {
remote_file_url := cur_li.Value["file"]
parts := strings.Split(remote_file_url,"raw.githubusercontent.com")//assume master branch
menu_file := parts[1]
local_menu_file := fmt.Sprintf("%s/aria2download%s",home_path,menu_file)
local_menu_file := fmt.Sprintf(aria2dl_folder,home_path,menu_file)
fmt.Println("Click on game ", local_menu_file)
if UI.FileExists(local_menu_file) == false {
gid,ret := self.UrlIsDownloading(remote_file_url)
if ret == false {
gid,err := self.rpcc.AddURI([]string{remote_file_url},"out:"+menu_file)
outfile := struct {
Out string `json:"out"`
}{Out:menu_file}
gid,err := self.rpcc.AddURI([]string{remote_file_url},outfile)
if err != nil {
log.Fatal(err)
}else {
fmt.Println("Warehouse Click game is downloading, ",gid)
fmt.Println(remote_file_url)
self.Aria2DownloadingGid = gid
}
} else {
fmt.Println(self.rpcc.TellStatus(gid,"status","totalLength","completedLength"))
self.Screen.MsgBox.SetText("Getting the game now")
@@ -601,7 +633,7 @@ func (self *WareHouse) Click() {
self.Screen.TitleBar.Redraw()
}
}else {
fmt.Println("file downloaded") //maybe check it if is installed fst,then execute it
fmt.Println("file downloaded ", cur_li.Value) //maybe check it if is installed fst,then execute it
if cur_li.Value["type"] == "launcher" && cur_li.ReadOnly == false {
local_menu_file_path := filepath.Dir(local_menu_file)
game_sh := filepath.Join(local_menu_file_path,cur_li.Value["title"],cur_li.Value["title"]+".sh")
@@ -656,6 +688,7 @@ func (self *WareHouse) raw_github_com(url string) (bool,string) {
func (self *WareHouse) OnKbdReturnBackCb() {
inputed:= strings.Join(self.Keyboard.Textarea.MyWords,"")
inputed = strings.Replace(inputed,"http://","",-1)
inputed = strings.Replace(inputed,"https://","",-1)
@@ -746,7 +779,7 @@ func (self *WareHouse) OnReturnBackCb() {
self.FootMsg[2] = "Remove"
self.FootMsg[1] = "Preview"
}
self.SyncList()
self.RestoreScrolled()
@@ -904,6 +937,12 @@ func (self *WareHouse) KeyDown(ev *event.Event) {
func (self *WareHouse) Draw() {
self.ClearCanvas()
if self.PsIndex > len(self.MyList) -1 {
self.PsIndex = len(self.MyList) -1
}
if self.PsIndex < 0 {
self.PsIndex = 0
}
if len(self.MyList) == 0 {
return
} else {
@@ -913,7 +952,7 @@ func (self *WareHouse) Draw() {
self.Ps.Draw()
for _,v := range self.MyList {
_,y := v.Coord()
if y > self.Height + self.Height/2 {
if y > (self.Height + self.Height/2) {
break
}
if y < 0 {