forked from leitwolf/QuickXDev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickx.py
398 lines (363 loc) · 13.4 KB
/
quickx.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Author: lonewolf
# Date: 2013-10-26 11:23:48
#
import sublime
import sublime_plugin
import functools
import os
import datetime
import json
import re
import subprocess
import sys
import time
import codecs
try:
import helper
import rebuild
import definition
except ImportError:
from . import helper
from . import rebuild
from . import definition
TEMP_PATH=""
DEFINITION_LIST=[]
USER_DEFINITION_LIST=[]
luaTemplate="""--
-- Author: ${author}
-- Date: ${date}
--
"""
compile_scripts_bat="""@echo off
set DIR=%~dp0
%DIR%win32\php.exe "%DIR%lib\compile_scripts.php" %*
"""
# init plugin,load definitions
def init():
global TEMP_PATH
TEMP_PATH=sublime.packages_path()+"/User/QuickXDev.cache"
global DEFINITION_LIST
DEFINITION_LIST=json.loads(definition.data)
global USER_DEFINITION_LIST
path=os.path.join(TEMP_PATH,"user_definition.json")
if os.path.exists(path):
USER_DEFINITION_LIST=json.loads(helper.readFile(path))
def checkRoot():
# quick_cocos2dx_root
settings = helper.loadSettings("QuickXDev")
quick_cocos2dx_root = settings.get("quick_cocos2dx_root", "")
if len(quick_cocos2dx_root)==0:
sublime.error_message("quick_cocos2dx_root no set")
return False
return quick_cocos2dx_root
class LuaNewFileCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
self.window.run_command("hide_panel")
title = "untitle"
on_done = functools.partial(self.on_done, dirs[0])
v = self.window.show_input_panel(
"File Name:", title + ".lua", on_done, None, None)
v.sel().clear()
v.sel().add(sublime.Region(0, len(title)))
def on_done(self, path, name):
filePath = os.path.join(path, name)
if os.path.exists(filePath):
sublime.error_message("Unable to create file, file exists.")
else:
code = luaTemplate
# add attribute
settings = helper.loadSettings("QuickXDev")
format = settings.get("date_format", "%Y-%m-%d %H:%M:%S")
date = datetime.datetime.now().strftime(format)
code = code.replace("${date}", date)
author=settings.get("author", "Your Name")
code = code.replace("${author}", author)
# save
helper.writeFile(filePath, code)
v=sublime.active_window().open_file(filePath)
# cursor
v.run_command("insert_snippet",{"contents":code})
sublime.status_message("Lua file create success!")
def is_enabled(self, dirs):
return len(dirs) == 1
class QuickxRunWithPlayerCommand(sublime_plugin.WindowCommand):
def __init__(self,window):
super(QuickxRunWithPlayerCommand,self).__init__(window)
self.process=None
def run(self, dirs):
# root
quick_cocos2dx_root = checkRoot()
if not quick_cocos2dx_root:
return
# player path for platform
playerPath=""
if sublime.platform()=="osx":
playerPath=quick_cocos2dx_root+"/player/bin/mac/quick-x-player.app/Contents/MacOS/quick-x-player"
elif sublime.platform()=="windows":
playerPath=quick_cocos2dx_root+"/player/bin/win32/quick-x-player.exe"
if playerPath=="" or not os.path.exists(playerPath):
sublime.error_message("player no exists")
return
args=[playerPath]
# param
path=dirs[0]
args.append("-workdir")
args.append(os.path.split(path)[0])
args.append("-file")
args.append("scripts/main.lua")
args.append("-load-framework")
configPath=path+"/config.lua"
if os.path.exists(configPath):
f=codecs.open(configPath,"r","utf-8")
width=640
height=960
while True:
line=f.readline()
if line:
# debug
m=re.match("^DEBUG\s*=\s*(\d+)",line)
if m:
debug=m.group(1)
if debug=="0":
args.append("-disable-write-debug-log")
args.append("-disable-console")
elif debug=="1":
args.append("-disable-write-debug-log")
args.append("-console")
else:
args.append("-write-debug-log")
args.append("-console")
# resolution
m=re.match("^CONFIG_SCREEN_WIDTH\s*=\s*(\d+)",line)
if m:
width=m.group(1)
m=re.match("^CONFIG_SCREEN_HEIGHT\s*=\s*(\d+)",line)
if m:
height=m.group(1)
else:
break
f.close()
args.append("-size")
args.append(width+"x"+height)
if self.process:
try:
self.process.terminate()
except Exception:
pass
if sublime.platform()=="osx":
self.process=subprocess.Popen(args)
elif sublime.platform()=="windows":
self.process=subprocess.Popen(args)
def is_enabled(self, dirs):
if len(dirs)!=1:
return False
mainLuaPath=dirs[0]+"/main.lua"
if not os.path.exists(mainLuaPath):
return False
return True
def is_visible(self, dirs):
return self.is_enabled(dirs)
class QuickxGotoDefinitionCommand(sublime_plugin.TextCommand):
def run(self, edit):
# select text
sel=self.view.substr(self.view.sel()[0])
if len(sel)==0:
return
quick_cocos2dx_root = checkRoot()
if not quick_cocos2dx_root:
return
# find all match file
matchList=[]
showList=[]
for item in DEFINITION_LIST:
for key in item[0]:
if key==sel:
matchList.append(item)
showList.append(item[1])
for item in USER_DEFINITION_LIST:
for key in item[0]:
if key==sel:
matchList.append(item)
showList.append(item[1])
if len(matchList)==0:
sublime.status_message("Can not find definition '%s'"%(sel))
elif len(matchList)==1:
filepath=os.path.join(quick_cocos2dx_root,matchList[0][2])
if os.path.exists(filepath):
self.view.window().open_file(filepath+":"+str(matchList[0][3]),sublime.ENCODED_POSITION)
else:
sublime.status_message("%s not exists"%(filepath))
else:
# multi match
self.matchList=matchList
self.quick_cocos2dx_root=quick_cocos2dx_root
on_done = functools.partial(self.on_done)
self.view.window().show_quick_panel(showList,on_done)
def on_done(self,index):
if index==-1:
return
item=self.matchList[index]
filepath=os.path.join(self.quick_cocos2dx_root,item[2])
filepath=os.path.abspath(filepath)
if os.path.exists(filepath):
self.view.window().open_file(filepath+":"+str(item[3]),sublime.ENCODED_POSITION)
else:
sublime.status_message("%s not exists"%(filepath))
def is_enabled(self):
return helper.checkFileExt(self.view.file_name(),"lua")
def is_visible(self):
return self.is_enabled()
class QuickxRebuildUserDefinitionCommand(sublime_plugin.WindowCommand):
def __init__(self,window):
super(QuickxRebuildUserDefinitionCommand,self).__init__(window)
self.lastTime=0
def run(self, dirs):
curTime=time.time()
if curTime-self.lastTime<3:
sublime.status_message("Rebuild frequently!")
return
self.lastTime=curTime
global USER_DEFINITION_LIST
USER_DEFINITION_LIST=rebuild.rebuild(dirs[0],TEMP_PATH)
path=os.path.join(TEMP_PATH, "user_definition.json")
data=json.dumps(USER_DEFINITION_LIST)
if not os.path.exists(TEMP_PATH):
os.makedirs(TEMP_PATH)
helper.writeFile(path,data)
sublime.status_message("Rebuild user definition complete!")
def is_enabled(self, dirs):
return len(dirs)==1
def is_visible(self, dirs):
return self.is_enabled(dirs)
class QuickxCreateNewProjectCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
quick_cocos2dx_root = checkRoot()
if not quick_cocos2dx_root:
return
cmdPath=""
if sublime.platform()=="osx":
cmdPath=quick_cocos2dx_root+"/bin/create_project.sh"
elif sublime.platform()=="windows":
cmdPath=quick_cocos2dx_root+"/bin/create_project.bat"
if cmdPath=="" or not os.path.exists(cmdPath):
sublime.error_message("command no exists")
return
self.cmdPath=cmdPath
self.window.run_command("hide_panel")
packageName="com.mygames.game01"
on_done = functools.partial(self.on_done, dirs[0])
v = self.window.show_input_panel(
"Package Name:", packageName, on_done, None, None)
v.sel().clear()
v.sel().add(sublime.Region(0, len(packageName)))
def on_done(self, path, packageName):
if packageName=="":
sublime.error_message("PackageName must not empty!")
return
dotIndex=packageName.rfind(".")
if dotIndex==-1:
sublime.error_message("PackageName must two levels,i.e. 'com.game01'.")
return
dirName=packageName[dotIndex+1:]
for item in os.listdir(path):
if item==dirName:
sublime.error_message("Folder '%s' already exists."%(dirName))
return
args=[self.cmdPath,packageName]
if sublime.platform()=="osx":
subprocess.Popen(args,cwd=path)
elif sublime.platform()=="windows":
child=subprocess.Popen(args,cwd=path)
child.wait()
self.window.run_command("refresh_folder_list")
def is_enabled(self, dirs):
return len(dirs)==1
def is_visible(self, dirs):
return self.is_enabled(dirs)
class QuickxCompileScriptsCommand(sublime_plugin.WindowCommand):
def run(self, dirs):
settings = helper.loadSettings("QuickXDev")
quick_cocos2dx_root = settings.get("quick_cocos2dx_root", "")
if len(quick_cocos2dx_root)==0:
sublime.error_message("quick_cocos2dx_root no set")
return
cmdPath=""
if sublime.platform()=="osx":
cmdPath=quick_cocos2dx_root+"/bin/compile_scripts.sh"
elif sublime.platform()=="windows":
cmdPath=quick_cocos2dx_root+"/bin/compile_scripts.bat"
if not os.path.exists(cmdPath):
helper.writeFile(cmdPath,compile_scripts_bat)
if cmdPath=="" or not os.path.exists(cmdPath):
sublime.error_message("compile_scripts no exists")
return
self.cmdPath=cmdPath
self.compile_scripts_key=settings.get("compile_scripts_key", "")
self.window.run_command("hide_panel")
output="res/game.zip"
on_done = functools.partial(self.on_done, dirs[0])
v = self.window.show_input_panel(
"Output File:", output, on_done, None, None)
v.sel().clear()
v.sel().add(sublime.Region(4, 8))
def on_done(self, path, output):
if output=="":
sublime.error_message("Output File must not empty!")
return
arr=os.path.split(path)
path=arr[0]
src=arr[1]
args=[self.cmdPath,"-i",src,"-o",output]
if self.compile_scripts_key!="":
args.append("-e")
args.append("xxtea_zip")
args.append("-ek")
args.append(self.compile_scripts_key)
if sublime.platform()=="osx":
subprocess.Popen(args,cwd=path,env={"luajit":"/usr/local/bin/luajit"})
elif sublime.platform()=="windows":
child=subprocess.Popen(args,cwd=path)
child.wait()
self.window.run_command("refresh_folder_list")
def is_enabled(self, dirs):
return len(dirs)==1
def is_visible(self, dirs):
return self.is_enabled(dirs)
class QuickxListener(sublime_plugin.EventListener):
def __init__(self):
self.lastTime=0
def on_post_save(self, view):
filename=view.file_name()
if not filename:
return
if not helper.checkFileExt(filename,"lua"):
return
# rebuild user definition
curTime=time.time()
if curTime-self.lastTime<2:
return
self.lastTime=curTime
a=rebuild.rebuildSingle(filename,TEMP_PATH)
arr=a[0]
path=a[1]
# remove prev
global USER_DEFINITION_LIST
for item in USER_DEFINITION_LIST:
if item[2]==path:
USER_DEFINITION_LIST.remove(item)
USER_DEFINITION_LIST.extend(arr)
path=os.path.join(TEMP_PATH, "user_definition.json")
data=json.dumps(USER_DEFINITION_LIST)
if not os.path.exists(TEMP_PATH):
os.makedirs(TEMP_PATH)
helper.writeFile(path,data)
sublime.status_message("Current file definition rebuild complete!")
# st3
def plugin_loaded():
sublime.set_timeout(init, 200)
# st2
if not helper.isST3():
init()