forked from Xrysnow/lstgx_LuaCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
136 lines (129 loc) · 3.94 KB
/
main.lua
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
--
--lstg.DoFile('jit_test.lua')
local fu = cc.FileUtils:getInstance()
fu:setPopupNotify(true)
-- note: GLView is opened in display.lua
-- it's necessary for FrameInit
require('config')
require('cocos.init')
setmetatable(_G, nil)
-- package.path may not end with ';'
package.path = package.path .. ';?/__init__.lua;'
require('cc.ext')
require('cc.to_string')
require('cc.color')
require('i18n')
require('audio')
local _path = {}
local _path_rec = {}
for _, v in ipairs(fu:getSearchPaths()) do
local p = string.gsub(v, '\\', '/')
p = string.gsub(p, '//', '/')
if not _path_rec[p] then
table.insert(_path, p)
_path_rec[p] = true
end
end
fu:setSearchPaths(_path)
local sp = fu:getSearchPaths()
local _sp = '=== Search Path ===\n{'
for _, v in ipairs(sp) do
_sp = string.format('%s\n %q', _sp, v)
end
lstg.SystemLog(_sp .. '\n}')
local function main()
lstg.SystemLog('start main')
lstg.FrameInit()
if lstg.GetPlatform() == 'android' then
-- change src path to 'sdcard/lstg/src' if it exists
local sd = require('platform.android.native').getSDCardPath()
if sd and sd ~= '' then
local src = sd .. '/lstg/src'
if fu:isDirectoryExist(src) then
local paths = {}
for _, v in ipairs(sp) do
if v ~= 'assets/src/' then
table.insert(paths, v)
end
end
table.insert(paths, src)
fu:setSearchPaths(paths)
lstg.SystemLog(string.format('change src path to %q', src))
end
end
require('platform.android.native').setOrientationLandscape()
end
lstg.DoFile('launch')
lstg.SystemLog('start app')
require("app.MyApp"):create():run()
end
local TRACE_MAX_LEVEL = 16
local function traceback(msg, level)
local ret = ''
if msg then
ret = msg .. '\n'
end
ret = ret .. 'stack traceback:'
while true do
local info = debug.getinfo(level, "Slnf")
if not info then
break
end
local msgs = {}
local source = info.source or ''
if info.short_src == '[C]' then
source = '[C]'
else
source = string.format('[%s]', source)
end
table.insert(msgs, string.format(' %s', source, info.currentline, info.name or ""))
if info.currentline > 0 then
table.insert(msgs, string.format('%d:', info.currentline))
end
if info.namewhat and info.name then
table.insert(msgs, string.format(' in function \'%s\'', info.name))
else
if info.what == 'm' or info.linedefined == 0 then
table.insert(msgs, ' in main chunk')
elseif info.what == 'C' then
table.insert(msgs, string.format(' at %s', tostring(info.func)))
else
table.insert(msgs, string.format(' in function <%s:%d>', source, info.linedefined))
end
end
ret = ret .. '\n' .. table.concat(msgs, '')
level = level + 1
if level > TRACE_MAX_LEVEL then
ret = ret .. '\n...'
break
end
end
return ret
end
-- note: in app.run, only __G__TRACKBACK__ will take effect
__G__TRACKBACK__ = function(msg)
--msg = debug.traceback(msg, 3)
msg = traceback(msg, 3)
print(msg)
lstg.SystemLog(msg)
if plus and plus.isMobile() then
require('cc.ui.MessageBox').OK('Error', msg, function()
--ex.OnExit()
cc.Director:getInstance():endToLua()
end)
for _, v in ipairs({ 'FrameFunc' }) do
_G[v] = function()
end
end
else
lstg.MessageBox(msg, 'ERROR')
lstg.FrameEnd()
os.exit()
end
lstg.SystemLog('caught error in main')
return msg
end
local status, msg = xpcall(main, __G__TRACKBACK__)
if not status then
lstg.SystemLog('=== Error Message ===\n' .. msg)
end