Advertisement
Al_Tasin

Script_Loader

Apr 30th, 2020
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.70 KB | None | 0 0
  1. gg.require('8.55.1')
  2. local ver = 'Script loader v2.0'
  3. -- https://gameguardian.net/forum/files/file/140-script-loader/
  4.  
  5. local config = {}
  6. local save_filename = ''
  7. local open_menu = false
  8.  
  9. function get_scripts()
  10.     local empty = {}
  11.     local ret = empty  
  12.     if config['separate_lists'] ~= nil then
  13.         local target = gg.getTargetInfo()
  14.         ret = config[target['packageName']]
  15.         if ret == nil then config[target['packageName']] = empty end
  16.     else
  17.         ret = config['scripts']
  18.         if ret == nil then config['scripts'] = empty end
  19.     end
  20.     if ret == nil then ret = empty end
  21.     return ret
  22. end
  23.  
  24. function main_menu()
  25.     local options = {}
  26.     local scripts = get_scripts()
  27.    
  28.     for i, v in ipairs(scripts) do
  29.         options[i] = v
  30.     end
  31.    
  32.     local add = #options + 1
  33.     options[add] = 'Add script';
  34.    
  35.     local remove = #options + 1
  36.     options[remove] = 'Remove script';
  37.    
  38.     local toggle = #options + 1
  39.     options[toggle] = config['open_after_run'] == nil and 'Open menu after run script' or 'Hide menu after run script'
  40.    
  41.     local mode = #options + 1
  42.     options[mode] = config['separate_lists'] ~= nil and 'One list for all' or 'Separate lists for apps'
  43.    
  44.     local exit = #options + 1
  45.     options[exit] = 'Exit';
  46.    
  47.     selected = gg.choice(options, nil, ver..'\n'..(config['separate_lists'] ~= nil and gg.getTargetInfo()['label'] or 'One list for all'))
  48.     if scripts[selected] ~= nil then
  49.         run_script(scripts[selected])
  50.         if config['open_after_run'] ~= nil then
  51.             open_menu = true
  52.         end
  53.     end
  54.     if selected == add then
  55.         add_script()
  56.         save_config()
  57.         open_menu = true
  58.     end
  59.     if selected == remove then
  60.         remove_script()
  61.         save_config()
  62.         open_menu = true
  63.     end
  64.     if selected == toggle then
  65.         config['open_after_run'] = config['open_after_run'] == nil and 1 or nil
  66.         save_config()
  67.         open_menu = true
  68.     end
  69.     if selected == mode then
  70.         config['separate_lists'] = config['separate_lists'] == nil and 1 or nil
  71.         save_config()
  72.         open_menu = true
  73.     end
  74.     if selected == exit then
  75.         os.exit()
  76.     end
  77. end
  78.  
  79. function run_script(file)
  80.     print(file..' start')
  81.     local script = loadfile(file)
  82.     local msg = nil
  83.     if script == nil then
  84.         msg = 'Failed load script: '..file
  85.     else
  86.         local ret, err = pcall(script)
  87.         if not ret then        
  88.             msg = 'Script error: '..err
  89.             print(msg)
  90.             msg = nil
  91.         end
  92.     end
  93.     if msg ~= nil then
  94.         print(msg)
  95.         gg.alert(msg)
  96.     end
  97.     print(file..' finish')
  98. end
  99.  
  100. function add_script()
  101.     local scripts = get_scripts()
  102.     local info = nil
  103.     while true do
  104.         info = gg.prompt({'Select script file:'}, {config['last_file']}, {'file'})
  105.         if info == nil then return end
  106.         local test = loadfile(info[1])
  107.         if test == nil then
  108.             gg.alert('Failed load script: '..info[1])
  109.         else
  110.             break
  111.         end
  112.     end
  113.     scripts[#scripts + 1] = info[1]
  114.     config['last_file'] = info[1]
  115. end
  116.  
  117. function remove_script()
  118.     local scripts = get_scripts()
  119.     local selected = gg.choice(scripts, nil, 'Select script for remove:')
  120.     if selected == nil then return end
  121.     for i = selected, #scripts - 1 do
  122.         scripts[i] = scripts[i + 1]
  123.     end
  124.     scripts[#scripts] = nil
  125. end
  126.  
  127. function load_config()
  128.     local chunk = loadfile(save_filename)
  129.     if chunk == nil then
  130.         config = {}
  131.     else
  132.         config = chunk()
  133.     end
  134.     if config['last_file'] == nil then
  135.         config['last_file'] = get_file()
  136.     end
  137. end
  138.  
  139. function save_config()
  140.     gg.saveVariable(config, save_filename)
  141. end
  142.  
  143. function get_file()
  144.     local file = gg.getFile()
  145.     if file:sub(1, 1) == '@' then file = file:sub(2, #file) end
  146.     return file
  147. end
  148.  
  149. save_filename = get_file()..'.cfg'
  150. load_config()
  151.  
  152. gg.toast(ver, true)
  153. --gg.setVisible(false)
  154. while true do
  155.     if gg.isVisible() then
  156.         gg.setVisible(false)
  157.         open_menu = true       
  158.     end
  159.     if open_menu then
  160.         open_menu = false
  161.         main_menu()
  162.     else
  163.         gg.sleep(100)
  164.     end
  165. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement