-
Notifications
You must be signed in to change notification settings - Fork 804
/
__init__.py
291 lines (239 loc) · 8.5 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import pygame
import validators
import commands
from UI.constants import Width,Height,ICON_TYPES,RUNSYS
from UI.simple_name_space import SimpleNamespace
from UI.page import Page
from UI.label import Label
from UI.icon_item import IconItem
from UI.keys_def import CurKeys, IsKeyMenuOrB, IsKeyStartOrA
from UI.skin_manager import MySkinManager
from UI.lang_manager import MyLangManager
from libs.DBUS import is_wifi_connected_now,get_wifi_ip
from collections import deque
from enum import Enum
import os
import subprocess
import pipes
class DevilutionPage(Page):
_FootMsg = ["Nav","Check","Upgrade","Back","Play"]
_GameName = "devilutionX"
_GamePath = "/home/cpi/games/devilutionX"
_GameExecutable = _GamePath + "/build/devilutionx"
_GameExecutableRevision = _GameExecutable + ".rev"
_GameBuildScript = _GamePath + "/Packaging/cpi-gamesh/build.sh -t " + pipes.quote(os.path.dirname(os.path.abspath( __file__ )))
_GamePNG = _GamePath + "/Packaging/cpi-gamesh/Devilution.png"
_DevilutionDiabdatmpq = "/home/cpi/.local/share/diasurgical/devilution/diabdat.mpq"
_DevilutionDiabdatmpqPresent = False
_GameInstalled = False
_CiteNewUpdate = "Ahh... fresh meat!"
_CiteCheckUpdate = "Lets search the books..."
_CiteWelcome = "Well, what can I do for ya?"
_CiteCompiling = "Stay awhile and listen."
_CiteDone = "You must venture through the portal..."
_CiteFailed = "Game Over. Better luck next time!"
_GitURL = "https://github.com/diasurgical/devilutionX.git"
_GitBranch = "master"
_GitRevision = ""
_GameIcon = None
_Process = None
_Labels = {}
_Coords = {}
_ListFontObj = MyLangManager.TrFont("varela13")
_URLColor = MySkinManager.GiveColor('URL')
_TextColor = MySkinManager.GiveColor('Text')
def __init__(self):
Page.__init__(self)
def InitLabels(self):
y = 15
x = 11
yInc = 19
xGitRefLabelWidth = 48
labels = \
[["greeting",self._CiteWelcome, self._TextColor, x, y],
["status", "", self._URLColor, x, y + 72-yInc],
["comment", "", self._TextColor, x, y + 72],
["console_out","",self._URLColor, x, y + 72 + yInc],
["label_rev","GIT Revisions: ", self._TextColor, x, 132],
["label_git_rev","Source: ", self._TextColor, x, 151],
["content_git_rev","", self._URLColor, x + xGitRefLabelWidth, 151],
["label_bin_rev","Bin: ", self._TextColor, x, 170],
["content_bin_rev","", self._URLColor, x + xGitRefLabelWidth, 170]
]
for i in labels:
l = Label()
l.SetCanvasHWND(self._CanvasHWND)
l.Init(i[1],self._ListFontObj)
l.SetColor(i[2])
self._Labels[ i[0] ] = l
c = SimpleNamespace()
c.x = i[3]
c.y = i[4]
self._Coords[ i[0] ] = c
def GitGetRevision(self):
if not os.path.exists(self._GamePath):
return "game not installed"
process = subprocess.Popen("cd " + pipes.quote(self._GamePath) + "; git rev-parse HEAD",stdout=subprocess.PIPE,stderr=subprocess.STDOUT, shell=True)
self._GitRevision = process.communicate()[0].strip()
process.wait()
return self._GitRevision
def ExectuableGetRevision(self):
try:
with open(self._GameExecutableRevision, 'r') as file:
executableRevsion = file.read().replace('\n', '')
return executableRevsion
except:
return "unknown"
def InitGameDirectory(self):
try:
os.makedirs(self._GamePath)
except:
pass
self.StartShellProcess("cd " + pipes.quote(self._GamePath) + "; git clone --single-branch --branch " + pipes.quote(self._GitBranch) + " " + pipes.quote(self._GitURL) + " .")
def CheckDevilutionMPQ(self):
self._DevilutionDiabdatmpqPresent = os.path.isfile(self._DevilutionDiabdatmpq)
def CheckGameInstalled(self):
self._GameInstalled = os.path.isfile(self._GameExecutable)
def UpdateFootMsg(self):
if not self._GameInstalled:
self._FootMsg = ["Nav","","Install","Back",""]
self.UpdateLabel("status", "GIT Upgrade")
self.UpdateLabel("comment", "Press X to install")
elif not self._DevilutionDiabdatmpqPresent:
self._FootMsg = ["Nav","","Upgrade","Back","Re-check"]
self.UpdateLabel("status", "Gamefile diabdat.mpq missing")
self.UpdateLabel("comment", "see readme")
else:
self._FootMsg = ["Nav","","Upgrade","Back","Play"]
self.UpdateLabel("status", "Ready")
self.UpdateLabel("comment", self._CiteDone)
def Init(self):
Page.Init(self)
if self._Screen != None:
if self._Screen._CanvasHWND != None and self._CanvasHWND == None:
self._HWND = self._Screen._CanvasHWND
self._CanvasHWND = pygame.Surface( (self._Screen._Width,self._Screen._Height) )
if os.path.isfile(self._GamePNG):
self._GameIcon = IconItem()
self._GameIcon._ImageName = self._GamePNG
self._GameIcon._MyType = ICON_TYPES["STAT"]
self._GameIcon._Parent = self
self._GameIcon.Adjust(290,70,128,128,0)
self.InitLabels()
self.CheckDevilutionMPQ()
self.CheckGameInstalled()
self.UpdateFootMsg()
self.UpdateLabel("content_git_rev", self.GitGetRevision(), 24)
self.UpdateLabel("content_bin_rev", self.ExectuableGetRevision(), 24)
def UpdateLabel(self, label, msg, maxLen=38):
print(label + ": " + msg)
if len(msg) > maxLen:
m = msg[:maxLen] + "..."
else:
m = msg
self._Labels[label].SetText(m)
def StartShellProcess(self, cmd):
print("StartShellProcess " + cmd)
proc = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT, shell=True)
while(True):
line = proc.stdout.readline()
if line:
self.UpdateLabel("console_out", line.strip(), 48)
self._Screen.Draw()
self._Screen.SwapAndShow()
if line == '' and proc.poll() is not None:
break
self.UpdateLabel("console_out", "")
self._Screen.Draw()
self._Screen.SwapAndShow()
def GitUpgrade(self):
self.UpdateLabel("status", "GIT Upgrade")
self.UpdateLabel("comment", self._CiteCheckUpdate)
curRev = "unset"
if not os.path.exists(self._GamePath):
self.InitGameDirectory()
else:
curRev = self.GitGetRevision()
self.StartShellProcess("cd " + pipes.quote(self._GamePath) + "; git pull")
self._GitRevision = self.GitGetRevision()
self.UpdateLabel("content_git_rev", self._GitRevision, 24)
if curRev != self._GitRevision:
self.UpdateLabel("comment", self._CiteNewUpdate)
else:
self.UpdateLabel("comment", self._CiteDone)
self._Screen.Draw()
self._Screen.SwapAndShow()
def GitExectuableIsGitRevision(self):
return self.GitGetRevision() == self.ExectuableGetRevision()
def Build(self):
self.UpdateLabel("status", "Building")
self.StartShellProcess(self._GameBuildScript)
def UpgradeAndBuild(self):
self.GitUpgrade()
self.UpdateLabel("comment", self._CiteCompiling)
self._Screen.Draw()
self._Screen.SwapAndShow()
if not self.GitExectuableIsGitRevision():
self.Build()
self.UpdateLabel("content_git_rev", self.GitGetRevision(), 24)
self.UpdateLabel("content_bin_rev", self.ExectuableGetRevision(), 24)
self.UpdateLabel("status", "Done")
if self.GitExectuableIsGitRevision():
self.UpdateLabel("comment", self._CiteDone)
else:
self.UpdateLabel("comment", self._CiteFailed)
self.CheckDevilutionMPQ()
self.CheckGameInstalled()
self.UpdateFootMsg()
self._Screen.Draw()
self._Screen.SwapAndShow()
def KeyDown(self,event):
if IsKeyMenuOrB(event.key):
self.ReturnToUpLevelPage()
self._Screen.Draw()
self._Screen.SwapAndShow()
if self._DevilutionDiabdatmpqPresent and self._GameInstalled:
if IsKeyStartOrA(event.key):
pygame.event.post( pygame.event.Event(RUNSYS, message=self._GameExecutable))
if event.key == CurKeys["X"]:
self.UpgradeAndBuild()
elif not self._GameInstalled:
if event.key == CurKeys["X"]:
self.UpgradeAndBuild()
elif not self._DevilutionDiabdatmpqPresent:
if IsKeyStartOrA(event.key):
self.CheckDevilutionMPQ()
self.CheckGameInstalled()
self.UpdateFootMsg()
self._Screen.Draw()
self._Screen.SwapAndShow()
def Draw(self):
self.ClearCanvas()
if self._GameIcon != None:
self._GameIcon.Draw()
for i in self._Labels:
if i in self._Coords:
self._Labels[i].NewCoord( self._Coords[i].x, self._Coords[i].y)
self._Labels[i].Draw()
if self._HWND != None:
self._HWND.fill(MySkinManager.GiveColor('White'))
self._HWND.blit(self._CanvasHWND,(self._PosX,self._PosY,self._Width, self._Height ) )
class APIOBJ(object):
_Page = None
def __init__(self):
pass
def Init(self,main_screen):
self._Page = DevilutionPage()
self._Page._Screen = main_screen
self._Page._Name ="devilutionX"
self._Page.Init()
def API(self,main_screen):
if main_screen !=None:
main_screen.PushPage(self._Page)
main_screen.Draw()
main_screen.SwapAndShow()
OBJ = APIOBJ()
def Init(main_screen):
OBJ.Init(main_screen)
def API(main_screen):
OBJ.API(main_screen)