mainscreen.go 8.2 KB

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