mainscreen.go 8.0 KB

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