BigETI

TestDotLua

Nov 2nd, 2016
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.44 KB | None | 0 0
  1. TestDotLua = {}
  2. TestDotLua.Executables = {}
  3. TestDotLua.Commands = {}
  4.  
  5. TestDotLua.loadExecutableForCallback = function(callback_name)
  6.     local ret = false
  7.     local func = loadfile("scriptfiles/inline/" .. callback_name .. ".lua")
  8.     if func then
  9.         TestDotLua.Executables[callback_name] = func
  10.         ret = true
  11.     end
  12.     return ret
  13. end
  14.  
  15. TestDotLua.saveExecutableForCallback = function(callback_name, code)
  16.     local ret = false
  17.     local func = nil
  18.     xpcall(
  19.         function()
  20.             func = load(code)
  21.         end,
  22.         function(err)
  23.             print(err)
  24.         end)
  25.     if func then
  26.         TestDotLua.Executables[callback_name] = func
  27.         local f = io.open("scriptfiles/inline/" .. callback_name .. ".lua", "w")
  28.         if f then
  29.             io.output(f)
  30.             io.write(code)
  31.             io.close(f)
  32.             ret = true
  33.         end
  34.     end
  35.     return ret
  36. end
  37.  
  38. TestDotLua.loadCommand = function(command)
  39.     local ret = false
  40.     local func = loadfile("scriptfiles/commands/" .. string.lower(command) .. ".lua")
  41.     if func then
  42.         TestDotLua.Commands[string.lower(command)] = TestDotLua.createCommandData("Custom command", "A custom command", "", func)
  43.         ret = true
  44.     end
  45.     return ret
  46. end
  47.  
  48. TestDotLua.saveCommand = function(command, code)
  49.     local ret = false
  50.     local func = nil
  51.     xpcall(
  52.         function()
  53.             func = load(code)
  54.         end,
  55.         function(err)
  56.             print(err)
  57.         end)
  58.     if func then
  59.         TestDotLua.Commands[string.lower(command)] = TestDotLua.createCommandData("Custom command", "A custom command", "", func)
  60.         local f = io.open("scriptfiles/commands/" .. string.lower(command) .. ".lua", "w")
  61.         if f then
  62.             io.output(f)
  63.             io.write(code)
  64.             io.close(f)
  65.             ret = true
  66.         end
  67.     end
  68.     return ret
  69. end
  70.  
  71. TestDotLua.removeExecutableForCallback = function(callback_name)
  72.     TestDotLua.Executables[callback_name] = nil
  73. end
  74.  
  75. TestDotLua.removeCommand = function(command)
  76.     TestDotLua.Executables[string.lower(command)] = nil
  77. end
  78.  
  79. TestDotLua.executeInCallback = function(callback_name, ...)
  80.     local f = TestDotLua.Executables[callback_name]
  81.     local args = {...}
  82.     if f then
  83.         xpcall(
  84.             function()
  85.                 f(args)
  86.             end,
  87.             function(err)
  88.                 SendMessageToAll("~r~Scipt error detected at \"" .. callback_name .. "\". Please see the logs.")
  89.                 print("[ERROR] In \"" .. callback_name .. "\": " .. err)
  90.             end)
  91.     end
  92. end
  93.  
  94. -- Get command
  95. TestDotLua.getCommand = function(command)
  96.     return TestDotLua.Commands[string.lower(command)]
  97. end
  98.  
  99. -- Is command allowed
  100. TestDotLua.isCommandAllowed = function(command)
  101.     return TestDotLua.getCommand(command) or false
  102. end
  103.  
  104. -- Parse command
  105. TestDotLua.parseCommand = function(playerID, message)
  106.     local cmd = ""
  107.     local args = {}
  108.     local raw_args = ""
  109.     local first = true
  110.     for v in string.gmatch(message, "%w+") do
  111.         if first == true then
  112.             cmd = string.lower(v)
  113.             raw_args = string.sub(message, string.len(cmd) + 2)
  114.             first = false
  115.         else
  116.             table.insert(args, v)
  117.         end
  118.     end
  119.     local command = TestDotLua.getCommand(cmd)
  120.     if command then
  121.         command.command(playerID, args, raw_args)
  122.     else
  123.         if TestDotLua.loadCommand(cmd) then
  124.             TestDotLua.getCommand(cmd).command(playerID, args, raw_args)
  125.         else
  126.             SendMessageToPlayer(playerID, "Command \"/" .. cmd .. "\" not found or forbidden.")
  127.         end
  128.     end
  129. end
  130.  
  131. -- Create command data
  132. TestDotLua.createCommandData = function(description, fullDescription, requiredParams, command)
  133.     local ret = {}
  134.     ret.description = description
  135.     ret.fullDescription = fullDescription
  136.     ret.requiredParams = requiredParams
  137.     ret.command = command
  138.     return ret
  139. end
  140.  
  141. -- Show help topic
  142. TestDotLua.showHelpTopic = function(playerID, command)
  143.     local ret = false
  144.     local cmd = TestDotLua.getCommand(command)
  145.     if cmd then
  146.         SendMessageToPlayer(playerID, "")
  147.         SendMessageToPlayer(playerID, "=====Help Topic=====")
  148.         SendMessageToPlayer(playerID, "/" .. string.lower(command) .. " " .. cmd.requiredParams)
  149.         SendMessageToPlayer(playerID, "")
  150.         SendMessageToPlayer(playerID, "Description: " .. cmd.description)
  151.         SendMessageToPlayer(playerID, cmd.fullDescription)
  152.         SendMessageToPlayer(playerID, "====================")
  153.         ShowMessageToPlayer(playerID, "Usage: ~r~/" .. string.lower(command) .. " " .. string.gsub(string.gsub(cmd.requiredParams, "<", "["), ">", "]"))
  154.         ret = true
  155.     end
  156.     return ret
  157. end
  158.  
  159. TestDotLua.Commands.help = TestDotLua.createCommandData("Help topics", "This command shows help topics of all commands", "<optional: topic>", function (playerID, args, raw_args)
  160.     local h = true
  161.     if #args == 1 then
  162.         local cmd = TestDotLua.getCommand(args[1])
  163.         h = not TestDotLua.showHelpTopic(playerID, args[1])
  164.     end
  165.     if h then
  166.         local first = true
  167.         local msg = ""
  168.         SendMessageToPlayer(playerID, "=====Help Topic=====")
  169.         for k, v in pairs(TestDotLua.Commands) do
  170.             if first then
  171.                 first = false
  172.             else
  173.                 msg = msg .. ", "
  174.             end
  175.             msg = msg .. "/" .. k
  176.         end
  177.         SendMessageToPlayer(playerID, msg)
  178.     end
  179. end)
  180.  
  181. TestDotLua.Commands.inline = TestDotLua.createCommandData("Set inline code", "This command sets inline code", "<callback> <code>", function (playerID, args, raw_args)
  182.     if #args > 1 then
  183.         local code = string.sub(raw_args, string.len(args[1]) + 2)
  184.         if TestDotLua.saveExecutableForCallback(args[1], code) then
  185.             ShowMessageToPlayer(playerID, "~g~Lua code successfully compiled for callback \"" .. args[1] .. "\"")
  186.         else
  187.             ShowMessageToPlayer(playerID, "~r~Failed to compile Lua code for callback \"" .. args[1] .. "\"")
  188.         end
  189.     else
  190.         TestDotLua.showHelpTopic(playerID, "inline")
  191.     end
  192. end)
  193.  
  194. TestDotLua.Commands.setcommand = TestDotLua.createCommandData("Set command code", "This command sets command code", "<command> <code>", function (playerID, args, raw_args)
  195.     if #args > 1 then
  196.         local code = string.sub(raw_args, string.len(args[1]) + 2)
  197.         if TestDotLua.saveCommand(args[1], code) then
  198.             ShowMessageToPlayer(playerID, "~g~Lua code successfully compiled for command /" .. args[1])
  199.         else
  200.             ShowMessageToPlayer(playerID, "~r~Failed to compile Lua code for command /" .. args[1])
  201.         end
  202.     else
  203.         TestDotLua.showHelpTopic(playerID, "setcommand")
  204.     end
  205. end)
  206.  
  207. TestDotLua.Commands.invoke = TestDotLua.createCommandData("Invoke code", "This command invokes code", "<code>", function (playerID, args, raw_args)
  208.     if #args > 0 then
  209.         local func = nil
  210.         xpcall(
  211.             function()
  212.                 func = load(raw_args)
  213.             end,
  214.             function(err)
  215.                 print(err)
  216.             end)
  217.         if func then
  218.             xpcall(
  219.                 function()
  220.                     func()
  221.                     ShowMessageToPlayer(playerID, "~g~Lua code successfully invoked")
  222.                 end,
  223.                 function(err)
  224.                     print(err)
  225.                     ShowMessageToPlayer(playerID, "~r~Failed to invoke Lua code")
  226.                 end)
  227.         else
  228.             ShowMessageToPlayer(playerID, "~r~Failed to invoke Lua code")
  229.         end
  230.     else
  231.         TestDotLua.showHelpTopic(playerID, "invoke")
  232.     end
  233. end)
  234.  
  235. TestDotLua.Commands.reloadcommand = TestDotLua.createCommandData("Reload command", "This command reloads a command from file", "<command>", function (playerID, args, raw_args)
  236.     if #args == 1 then
  237.         if TestDotLua.loadCommand(args[1]) then
  238.             ShowMessageToPlayer(playerID, "~g~Command /" .. args[1] .. " successfully reloaded")
  239.         else
  240.             ShowMessageToPlayer(playerID, "~r~Failed to reload command /" .. args[1])
  241.         end
  242.     else
  243.         TestDotLua.showHelpTopic(playerID, "reloadcommand")
  244.     end
  245. end)
  246.  
  247. -- Gamemode init
  248. function OnGameModeInit()
  249.     print("[INIT ] Setting spawn points...")
  250.     SetSpawnPoint(-817.657, 178.111, 75.0)
  251.     SetSpawnPoint(-640.183, 297.111, 91.0)
  252.     print("[INIT ] Done!")
  253.    
  254.     print("[INIT ] Loading IPLs...")
  255.     LoadIPL("hei_carrier")
  256.     LoadIPL("hei_carrier_DistantLights")
  257.     LoadIPL("hei_Carrier_int1")
  258.     LoadIPL("hei_Carrier_int2")
  259.     LoadIPL("hei_Carrier_int3")
  260.     LoadIPL("hei_Carrier_int4")
  261.     LoadIPL("hei_Carrier_int5")
  262.     LoadIPL("hei_Carrier_int6")
  263.     LoadIPL("hei_carrier_LODLights")
  264.     print("[INIT ] Done!")
  265.    
  266.     print("[INIT ] Loading inline code...")
  267.     TestDotLua.loadExecutableForCallback("OnGameModeInit")
  268.     TestDotLua.loadExecutableForCallback("OnGameModeExit")
  269.     TestDotLua.loadExecutableForCallback("OnPlayerPickUpPickup")
  270.     TestDotLua.loadExecutableForCallback("OnPlayerConnect")
  271.     TestDotLua.loadExecutableForCallback("OnPlayerDisconnect")
  272.     TestDotLua.loadExecutableForCallback("OnPlayerUpdate")
  273.     TestDotLua.loadExecutableForCallback("OnPlayerSpawn")
  274.     TestDotLua.loadExecutableForCallback("OnPlayerDeath")
  275.     TestDotLua.loadExecutableForCallback("OnPlayerEnterVehicle")
  276.     TestDotLua.loadExecutableForCallback("OnPlayerExitVehicle")
  277.     TestDotLua.loadExecutableForCallback("OnPlayerEnterCheckpoint")
  278.     TestDotLua.loadExecutableForCallback("OnPlayerExitCheckpoint")
  279.     TestDotLua.loadExecutableForCallback("OnPlayerEnteringVehicle")
  280.     TestDotLua.loadExecutableForCallback("OnPlayerExitingVehicle")
  281.     TestDotLua.loadExecutableForCallback("OnPlayerDamage")
  282.     TestDotLua.loadExecutableForCallback("OnPlayerShotBullet")
  283.     TestDotLua.loadExecutableForCallback("OnProjectileImpact")
  284.     TestDotLua.loadExecutableForCallback("OnVehicleRespawn")
  285.     TestDotLua.loadExecutableForCallback("OnVehicleDamage")
  286.     print("[INIT ] Done!")
  287.    
  288.     print("")
  289.     print("===========Start===========")
  290.     print("=    Alpha test script    =")
  291.     print("= Made by BigETI (c) 2016 =")
  292.     print("===========Start===========")
  293.     print("")
  294.     TestDotLua.executeInCallback("OnGameModeInit")
  295.     return 1
  296. end
  297.  
  298. -- Gamemode exit
  299. function OnGameModeExit()
  300.     print("")
  301.     print("===========Stop============")
  302.     print("=    Alpha test script    =")
  303.     print("= Made by BigETI (c) 2016 =")
  304.     print("===========Stop============")
  305.     print("")
  306.     TestDotLua.executeInCallback("OnGameModeExit")
  307.     return 1
  308. end
  309. -- Player command
  310.     -- @param playerID : number (int)
  311.     -- @param message : string
  312. function OnPlayerCommand(playerID, message)
  313.     TestDotLua.parseCommand(playerID, message)
  314.     TestDotLua.executeInCallback("OnPlayerCommand", playerID, message)
  315.     return 1
  316. end
  317.  
  318. -- Player message
  319.     -- @param playerID : number (int)
  320.     -- @param message : string
  321. function OnPlayerMessage(playerID, message)
  322.     SendMessageToAll("~b~" .. GetPlayerName(playerID) .. "~w~(" .. playerID .. "): " .. message)
  323.     TestDotLua.executeInCallback("OnPlayerMessage", playerID, message)
  324.     return 1
  325. end
  326.  
  327. -- Player pick up pickup
  328.     -- @param pickUpID : number (int)
  329.     -- @param playerID : number (int)
  330. function OnPlayerPickUpPickup(pickUpID, playerID)
  331.     TestDotLua.executeInCallback("OnPlayerPickUpPickup", pickUpID, playerID)
  332.     return 1
  333. end
  334.  
  335. -- Player connect
  336.     -- @param playerID : number (int)
  337. function OnPlayerConnect(playerID)
  338.     SendMessageToAll("[~g~+~w~]" .. GetPlayerName(playerID) .. "(" .. playerID .. ") has joined the server")
  339.     SetPlayerModel(playerID, 240)
  340.     SetPlayerVisible(playerID, true)
  341.     TestDotLua.executeInCallback("OnPlayerConnect", playerID)
  342.     return 1
  343. end
  344.  
  345. -- Player disconnect
  346.     -- @param playerID : number (int)
  347. function OnPlayerDisconnect(playerID)
  348.     SendMessageToAll("[~r~-~w~]" .. GetPlayerName(playerID) .. "(" .. playerID .. ") has left the server")
  349.     TestDotLua.executeInCallback("OnPlayerDisconnect", playerID)
  350.     return 1
  351. end
  352.  
  353. -- Player update
  354.     -- @param playerID : number (int)
  355. function OnPlayerUpdate(playerID)
  356.     TestDotLua.executeInCallback("OnPlayerUpdate", playerID)
  357.     return 1
  358. end
  359.  
  360. -- Player spawn
  361.     -- @param playerID : number (int)
  362. function OnPlayerSpawn(playerID)
  363.     PlayerScreenFadeIn(playerID, 500)
  364.     TestDotLua.executeInCallback("OnPlayerSpawn", playerID)
  365.     return 1
  366. end
  367.  
  368. -- Player death
  369.     -- @param playerID : number (int)
  370. function OnPlayerDeath(playerID)
  371.     PlayerScreenFadeOut(playerID, 500)
  372.     TestDotLua.executeInCallback("OnPlayerDeath", playerID)
  373.     return 1
  374. end
  375.  
  376. -- Player enter vehicle
  377.     -- @param playerID : number (int)
  378.     -- @param vehicleID : number (int)
  379.     -- @param seatID : number (int)
  380. function OnPlayerEnterVehicle(playerID, vehicleID, seatID)
  381.     TestDotLua.executeInCallback("OnPlayerEnterVehicle", playerID, vehicleID, seatID)
  382.     return 1
  383. end
  384.  
  385. -- Player exit vehicle
  386.     -- @param playerID : number (int)
  387.     -- @param vehicleID : number (int)
  388.     -- @param seatID : number (int)
  389. function OnPlayerExitVehicle(playerID, vehicleID, seatID)
  390.     TestDotLua.executeInCallback("OnPlayerExitVehicle", playerID, vehicleID, seatID)
  391.     return 1
  392. end
  393.  
  394. -- Player exit checkpoint
  395.     -- @param playerID : number (int)
  396.     -- @param checkpointID : number (int)
  397. function OnPlayerEnterCheckpoint(playerID, checkpointID)
  398.     TestDotLua.executeInCallback("OnPlayerEnterCheckpoint", playerID, checkpointID)
  399.     return 1
  400. end
  401.  
  402. -- Player exit checkpoint
  403.     -- @param playerID : number (int)
  404.     -- @param checkpointID : number (int)
  405. function OnPlayerExitCheckpoint(playerID, checkpointID)
  406.     TestDotLua.executeInCallback("OnPlayerExitCheckpoint", playerID, checkpointID)
  407.     return 1
  408. end
  409.  
  410. -- Player entering vehicle
  411.     -- @param playerID : number (int)
  412.     -- @param vehicleID : number (int)
  413.     -- @param seatID : number (int)
  414. function OnPlayerEnteringVehicle(playerID, vehicleID, seatID)
  415.     TestDotLua.executeInCallback("OnPlayerEnteringVehicle", playerID, vehicleID, seatID)
  416.     return 1
  417. end
  418.  
  419. -- Player exiting vehicle
  420.     -- @param playerID : number (int)
  421.     -- @param vehicleID : number (int)
  422.     -- @param seatID : number (int)
  423. function OnPlayerExitingVehicle(playerID, vehicleID, seatID)
  424.     TestDotLua.executeInCallback("OnPlayerExitingVehicle", playerID, vehicleID, seatID)
  425.     return 1
  426. end
  427.  
  428. -- Player damage
  429.     -- @param playerID : number (int)
  430.     -- @param damagerID : number (int)
  431.     -- @param amount : number (int)
  432. function OnPlayerDamage(playerID, damagerID, amount)
  433.     TestDotLua.executeInCallback("OnPlayerDamage", playerID, damagerID, amount)
  434.     return 1
  435. end
  436.  
  437. -- Player shot bullet
  438.     -- @param playerID : number (int)
  439.     -- @param weapon : string
  440. function OnPlayerShotBullet(playerID, weapon)
  441.     TestDotLua.executeInCallback("OnPlayerShotBullet", playerID, weapon)
  442.     return 1
  443. end
  444.  
  445. -- Projectile impact
  446.     -- @param playerID : number (int)
  447.     -- @param weapon : string
  448.     -- @param x : number (float)
  449.     -- @param y : number (float)
  450.     -- @param z : number (float)
  451.     -- @param entityType : string
  452.     -- @param entityID : number (int)
  453.     -- @param entityBone : number (int)
  454. function OnProjectileImpact(playerID, weapon, x, y, z, entityType, entityID, entityBone)
  455.     TestDotLua.executeInCallback("OnProjectileImpact", playerID, weapon, x, y, z, entityType, entityID, entityBone)
  456.     return 1
  457. end
  458.  
  459. -- Vehicle respawn
  460.     -- vehicleID : number (int)
  461. function OnVehicleRespawn(vehicleID)
  462.     TestDotLua.executeInCallback("OnVehicleRespawn", vehicleID)
  463.     return 1
  464. end
  465.  
  466. -- Vehicle damage
  467.     -- vehicleID : number (int)
  468.     -- playerID : number (int)
  469.     -- engineDamageID : number (int)
  470.     -- fuelDamage : number (int)
  471. function OnVehicleDamage(vehicleID, playerID, engineDamageID, fuelDamage)
  472.     TestDotLua.executeInCallback("OnVehicleDamage", vehicleID, playerID, engineDamageID, fuelDamage)
  473.     return 1
  474. end
Advertisement
Add Comment
Please, Sign In to add comment