mainscreen.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package main
  2. import (
  3. //"os"
  4. "fmt"
  5. "log"
  6. "io/ioutil"
  7. // "strconv"
  8. "strings"
  9. // "runtime"
  10. "path/filepath"
  11. //os/exec"
  12. "encoding/json"
  13. "sort"
  14. "github.com/go-ini/ini"
  15. "github.com/yookoala/realpath"
  16. "github.com/clockworkpi/LauncherGoDev/sysgo/UI"
  17. "github.com/clockworkpi/LauncherGoDev/sysgo/UI/Emulator"
  18. "github.com/clockworkpi/LauncherGoDev/Menu/GameShell/10_Settings"
  19. "github.com/clockworkpi/LauncherGoDev/Menu/GameShell/98_TinyCloud"
  20. "github.com/clockworkpi/LauncherGoDev/Menu/GameShell/99_PowerOFF"
  21. )
  22. var (
  23. UIPluginList = []*UI.UIPlugin {
  24. &UI.UIPlugin{1,"", "Menu/GameShell/10_Settings", "Settings", &Settings.APIOBJ},
  25. &UI.UIPlugin{1,"", "Menu/GameShell/98_TinyCloud", "TinyCloud", &TinyCloud.APIOBJ},
  26. &UI.UIPlugin{1,"", "Menu/GameShell/99_PowerOFF", "PowerOFF", &PowerOFF.APIOBJ},
  27. }
  28. )
  29. func ReunionPagesIcons(self *UI.MainScreen) {
  30. type Tup struct {
  31. FileName string
  32. OrigIdx int
  33. }
  34. var tmp []Tup
  35. for i,p := range self.Pages {
  36. p_icons := p.GetIcons()
  37. for i,x := range p_icons {
  38. var t Tup
  39. if x.GetFileName() != ""{
  40. if strings.Contains(x.GetFileName(),"_") == false {
  41. t = Tup{"98_"+x.GetFileName(),i}
  42. }else {
  43. t = Tup{x.GetFileName(),i}
  44. }
  45. }else{
  46. t = Tup{"",i}
  47. }
  48. tmp = append(tmp,t)
  49. }
  50. sort.Slice(tmp, func(i, j int) bool { return tmp[i].FileName < tmp[j].FileName })
  51. //fmt.Println(tmp)
  52. var retro_games_idx []int
  53. retro_games_dir := "20_Retro Games"
  54. for _,x := range tmp {
  55. if strings.HasPrefix(x.FileName, retro_games_dir) {
  56. retro_games_idx = append(retro_games_idx,x.OrigIdx)
  57. }
  58. }
  59. if len(retro_games_idx) > 1 {
  60. p_icons_0_link_page := p_icons[retro_games_idx[0]].GetLinkPage().(*UI.Page)
  61. for i:=1;i<len(retro_games_idx);i++ {
  62. icons_other_page := p_icons[retro_games_idx[i]].GetLinkPage().GetIcons()
  63. p_icons_0_link_page.Icons = append(p_icons_0_link_page.Icons, icons_other_page...)
  64. }
  65. var tmpswap []Tup
  66. onlyone := false
  67. for _,x := range tmp {
  68. if strings.HasPrefix(x.FileName,retro_games_dir) == false{
  69. tmpswap = append(tmpswap,x)
  70. }
  71. if strings.HasPrefix(x.FileName,retro_games_dir) == true && onlyone == false {
  72. tmpswap = append(tmpswap,x)
  73. onlyone = true
  74. }
  75. }
  76. tmp = tmpswap
  77. }
  78. var new_icons []UI.IconItemInterface
  79. for _,x := range tmp {
  80. new_icons = append(new_icons, p_icons[x.OrigIdx])
  81. }
  82. self.Pages[i].(*UI.Page).Icons = new_icons
  83. }
  84. }
  85. func ReadTheDirIntoPages(self *UI.MainScreen, _dir string, pglevel int, cur_page UI.PageInterface) {
  86. if UI.FileExists(_dir) == false && UI.IsDirectory(_dir) == false {
  87. return
  88. }
  89. files,err := ioutil.ReadDir(_dir)
  90. if err != nil {
  91. log.Fatal(err)
  92. return
  93. }
  94. for _,f := range files { // already sorted
  95. if UI.IsDirectory( _dir +"/"+f.Name()) {
  96. if pglevel == 0 {
  97. page := UI.NewPage()
  98. page.Name = self.ExtraName(f.Name())
  99. self.Pages = append(self.Pages, page)
  100. ReadTheDirIntoPages(self,_dir+"/"+f.Name(),pglevel+1, self.Pages[ len(self.Pages) - 1] )
  101. }else{ // on cur_page now
  102. i2:= self.ExtraName(f.Name())
  103. iconitem := UI.NewIconItem()
  104. iconitem.FileName = f.Name()
  105. iconitem.AddLabel(i2,self.IconFont)
  106. if UI.FileExists(filepath.Join(_dir,f.Name(),i2+".png")) { //eg: 20_Prog/Prog.png , cut 20_
  107. iconitem.ImageName = filepath.Join(_dir,f.Name(),i2+".png")
  108. }else if UI.FileExists( UI.SkinMap(_dir+"/"+i2+".png")) {
  109. iconitem.ImageName = UI.SkinMap(_dir+"/"+i2+".png")
  110. }else {
  111. //fmt.Println( UI.SkinMap(_dir+"/"+i2+".png") )
  112. untitled := UI.NewUntitledIcon()
  113. untitled.Init()
  114. if len(i2) > 1 {
  115. untitled.SetWords(string(i2[0]),string(i2[1]))
  116. }else if len(i2) == 1 {
  117. untitled.SetWords(string(i2[0]),string(i2[0]))
  118. }else {
  119. untitled.SetWords("G","s")
  120. }
  121. iconitem.ImgSurf = untitled.Surface()
  122. iconitem.ImageName = ""
  123. }
  124. if self.IsPluginPackage(_dir+"/"+f.Name()) {
  125. p_c := UI.PluginConfig{}
  126. dat, err := ioutil.ReadFile(_dir+"/"+f.Name()+"/" +UI.Plugin_flag)
  127. UI.ShowErr(err)
  128. err = json.Unmarshal(dat, &p_c)
  129. if err == nil {
  130. if p_c.NAME == "" {
  131. p_c.NAME = f.Name()
  132. }
  133. so_file := filepath.Join(_dir,f.Name(),p_c.SO_FILE)
  134. if UI.FileExists(so_file) && UI.IsAFile(so_file) {
  135. pi,err := UI.LoadPlugin(_dir+"/"+f.Name()+"/"+p_c.SO_FILE)
  136. UI.Assert(err)
  137. iconitem.CmdInvoke = UI.InitPlugin(pi,self)
  138. if iconitem.CmdInvoke != nil {
  139. iconitem.MyType = UI.ICON_TYPES["FUNC"]
  140. iconitem.CmdPath = f.Name()
  141. cur_page.AppendIcon(iconitem)
  142. }
  143. } else {
  144. for _,v := range UIPluginList {
  145. if v.LabelText == p_c.NAME {
  146. v.EmbInterface.Init(self)
  147. iconitem.CmdInvoke = v.EmbInterface
  148. if iconitem.CmdInvoke != nil {
  149. iconitem.MyType = UI.ICON_TYPES["FUNC"]
  150. iconitem.CmdPath = f.Name()
  151. cur_page.AppendIcon(iconitem)
  152. }
  153. }
  154. }
  155. }
  156. }
  157. //Init it
  158. }else if self.IsEmulatorPackage(_dir+"/"+f.Name()) {
  159. a_c := Emulator.ActionConfig{}
  160. a_c.FILETYPE="file"
  161. a_c.TITLE = "Game"
  162. cfg, err := ini.Load(_dir+"/"+f.Name()+"/" +UI.Emulator_flag)
  163. UI.ShowErr(err)
  164. err = cfg.MapTo(&a_c)
  165. if err == nil {
  166. //fmt.Println(a_c)
  167. if UI.FileExists(filepath.Join(_dir,f.Name(),"retroarch-local.cfg")) {
  168. a_c.RETRO_CONFIG = UI.CmdClean( filepath.Join(_dir,f.Name(),"retroarch-local.cfg") )
  169. fmt.Println("a local retroarch cfg: ",a_c.RETRO_CONFIG)
  170. }
  171. em := Emulator.NewMyEmulator()
  172. em.EmulatorConfig = &a_c
  173. em.Init(self)
  174. iconitem.CmdInvoke = em
  175. if iconitem.CmdInvoke != nil {
  176. iconitem.MyType = UI.ICON_TYPES["Emulator"]
  177. iconitem.CmdPath = f.Name()
  178. cur_page.AppendIcon(iconitem)
  179. }
  180. }else {
  181. fmt.Println("ReadTheDirIntoPages EmulatorConfig ",err)
  182. }
  183. }else if self.IsExecPackage(_dir+"/"+f.Name()) {
  184. iconitem.MyType = UI.ICON_TYPES["EXE"]
  185. rel_path,err := realpath.Realpath( filepath.Join(_dir,f.Name(),i2+".sh"))
  186. if err != nil {
  187. rel_path,_ = filepath.Abs(filepath.Join(_dir,f.Name(),i2+".sh"))
  188. }
  189. iconitem.CmdPath = rel_path
  190. UI.MakeExecutable( iconitem.CmdPath )
  191. cur_page.AppendIcon(iconitem)
  192. }else {
  193. iconitem.MyType = UI.ICON_TYPES["DIR"]
  194. linkpage := UI.NewPage()
  195. linkpage.Name = i2
  196. iconitem.LinkPage = linkpage
  197. cur_page.AppendIcon(iconitem)
  198. ReadTheDirIntoPages(self,_dir+"/"+f.Name(),pglevel+1, iconitem.LinkPage)
  199. }
  200. }
  201. } else if UI.IsAFile(_dir+"/"+f.Name()) && (pglevel > 0) {
  202. if strings.HasSuffix(strings.ToLower(f.Name()),UI.IconExt) {
  203. i2 := self.ExtraName(f.Name())
  204. iconitem := UI.NewIconItem()
  205. rel_path,err := realpath.Realpath( _dir+"/"+f.Name() )
  206. if err != nil {
  207. rel_path,_ = filepath.Abs(_dir+"/"+f.Name())
  208. }
  209. iconitem.CmdPath = rel_path
  210. iconitem.FileName = f.Name()
  211. UI.MakeExecutable( iconitem.CmdPath )
  212. iconitem.MyType = UI.ICON_TYPES["EXE"]
  213. if UI.FileExists( UI.SkinMap( _dir+"/"+ UI.ReplaceSuffix(i2,"png"))) {
  214. iconitem.ImageName = UI.SkinMap( _dir+"/"+ UI.ReplaceSuffix(i2,"png"))
  215. }else {
  216. untitled:= UI.NewUntitledIcon()
  217. untitled.Init()
  218. if len(i2) > 1 {
  219. untitled.SetWords(string(i2[0]),string(i2[1]))
  220. }else if len(i2) == 1 {
  221. untitled.SetWords(string(i2[0]),string(i2[0]))
  222. }else {
  223. untitled.SetWords("G","s")
  224. }
  225. iconitem.ImgSurf = untitled.Surface()
  226. iconitem.ImageName = ""
  227. }
  228. iconitem.AddLabel(strings.Split(i2,".")[0], self.IconFont)
  229. iconitem.LinkPage = nil
  230. cur_page.AppendIcon(iconitem)
  231. }
  232. }
  233. }
  234. }