mainscreen.go 7.9 KB

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