mirror of
https://github.com/citizenfx/cfx-server-data.git
synced 2025-12-12 06:14:09 +01:00
mapmanager: fixes and some cleanup
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
client_scripts {
|
client_scripts {
|
||||||
"mapmanager_client.lua",
|
"mapmanager_shared.lua",
|
||||||
"mapmanager_shared.lua"
|
"mapmanager_client.lua"
|
||||||
}
|
}
|
||||||
|
|
||||||
server_scripts {
|
server_scripts {
|
||||||
"mapmanager_server.lua",
|
"mapmanager_shared.lua",
|
||||||
"mapmanager_shared.lua"
|
"mapmanager_server.lua"
|
||||||
}
|
}
|
||||||
|
|
||||||
resource_manifest_version "77731fab-63ca-442c-a67b-abc70f28dfa5"
|
resource_manifest_version "77731fab-63ca-442c-a67b-abc70f28dfa5"
|
||||||
@@ -16,3 +16,4 @@ server_export "changeGameType"
|
|||||||
server_export "changeMap"
|
server_export "changeMap"
|
||||||
server_export "doesMapSupportGameType"
|
server_export "doesMapSupportGameType"
|
||||||
server_export "getMaps"
|
server_export "getMaps"
|
||||||
|
server_export "roundEnded"
|
||||||
@@ -1,35 +1,5 @@
|
|||||||
maps = {}
|
local maps = {}
|
||||||
gametypes = {}
|
local gametypes = {}
|
||||||
|
|
||||||
AddEventHandler('getResourceInitFuncs', function(isPreParse, add)
|
|
||||||
if not isPreParse then
|
|
||||||
add('map', function(file)
|
|
||||||
addMap(file, GetInvokingResource())
|
|
||||||
end)
|
|
||||||
|
|
||||||
add('resource_type', function(type)
|
|
||||||
return function(params)
|
|
||||||
local resourceName = GetInvokingResource()
|
|
||||||
|
|
||||||
if type == 'map' then
|
|
||||||
maps[resourceName] = params
|
|
||||||
elseif type == 'gametype' then
|
|
||||||
gametypes[resourceName] = params
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
mapFiles = {}
|
|
||||||
|
|
||||||
function addMap(file, owningResource)
|
|
||||||
if not mapFiles[owningResource] then
|
|
||||||
mapFiles[owningResource] = {}
|
|
||||||
end
|
|
||||||
|
|
||||||
table.insert(mapFiles[owningResource], file)
|
|
||||||
end
|
|
||||||
|
|
||||||
AddEventHandler('onClientResourceStart', function(res)
|
AddEventHandler('onClientResourceStart', function(res)
|
||||||
-- parse metadata for this resource
|
-- parse metadata for this resource
|
||||||
@@ -37,7 +7,7 @@ AddEventHandler('onClientResourceStart', function(res)
|
|||||||
-- map files
|
-- map files
|
||||||
local num = GetNumResourceMetadata(res, 'map')
|
local num = GetNumResourceMetadata(res, 'map')
|
||||||
|
|
||||||
if num then
|
if num > 0 then
|
||||||
for i = 0, num-1 do
|
for i = 0, num-1 do
|
||||||
local file = GetResourceMetadata(res, 'map', i)
|
local file = GetResourceMetadata(res, 'map', i)
|
||||||
|
|
||||||
@@ -51,8 +21,6 @@ AddEventHandler('onClientResourceStart', function(res)
|
|||||||
local type = GetResourceMetadata(res, 'resource_type', 0)
|
local type = GetResourceMetadata(res, 'resource_type', 0)
|
||||||
|
|
||||||
if type then
|
if type then
|
||||||
Citizen.Trace("type " .. res .. " " .. type .. "\n")
|
|
||||||
|
|
||||||
local extraData = GetResourceMetadata(res, 'resource_type_extra', 0)
|
local extraData = GetResourceMetadata(res, 'resource_type_extra', 0)
|
||||||
|
|
||||||
if extraData then
|
if extraData then
|
||||||
@@ -68,12 +36,10 @@ AddEventHandler('onClientResourceStart', function(res)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
print('map? ', res)
|
||||||
|
|
||||||
-- handle starting
|
-- handle starting
|
||||||
if mapFiles[res] then
|
loadMap(res)
|
||||||
for _, file in ipairs(mapFiles[res]) do
|
|
||||||
parseMap(file, res)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- defer this to the next game tick to work around a lack of dependencies
|
-- defer this to the next game tick to work around a lack of dependencies
|
||||||
Citizen.CreateThread(function()
|
Citizen.CreateThread(function()
|
||||||
@@ -94,73 +60,9 @@ AddEventHandler('onResourceStop', function(res)
|
|||||||
TriggerEvent('onClientGameTypeStop', res)
|
TriggerEvent('onClientGameTypeStop', res)
|
||||||
end
|
end
|
||||||
|
|
||||||
if undoCallbacks[res] then
|
unloadMap(res)
|
||||||
for _, cb in ipairs(undoCallbacks[res]) do
|
|
||||||
cb()
|
|
||||||
end
|
|
||||||
|
|
||||||
undoCallbacks[res] = nil
|
|
||||||
mapFiles[res] = nil
|
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
undoCallbacks = {}
|
|
||||||
|
|
||||||
function parseMap(file, owningResource)
|
|
||||||
if not undoCallbacks[owningResource] then
|
|
||||||
undoCallbacks[owningResource] = {}
|
|
||||||
end
|
|
||||||
|
|
||||||
local env = {
|
|
||||||
math = math, pairs = pairs, ipairs = ipairs, next = next, tonumber = tonumber, tostring = tostring,
|
|
||||||
type = type, table = table, string = string, _G = env
|
|
||||||
}
|
|
||||||
|
|
||||||
TriggerEvent('getMapDirectives', function(key, cb, undocb)
|
|
||||||
env[key] = function(...)
|
|
||||||
local state = {}
|
|
||||||
|
|
||||||
state.add = function(k, v)
|
|
||||||
state[k] = v
|
|
||||||
end
|
|
||||||
|
|
||||||
local result = cb(state, ...)
|
|
||||||
local args = table.pack(...)
|
|
||||||
|
|
||||||
table.insert(undoCallbacks[owningResource], function()
|
|
||||||
undocb(state)
|
|
||||||
end)
|
|
||||||
|
|
||||||
return result
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
local mt = {
|
|
||||||
__index = function(t, k)
|
|
||||||
if rawget(t, k) ~= nil then return rawget(t, k) end
|
|
||||||
|
|
||||||
-- as we're not going to return nothing here (to allow unknown directives to be ignored)
|
|
||||||
local f = function()
|
|
||||||
return f
|
|
||||||
end
|
|
||||||
|
|
||||||
return function() return f end
|
|
||||||
end
|
|
||||||
}
|
|
||||||
|
|
||||||
setmetatable(env, mt)
|
|
||||||
|
|
||||||
local fileData = LoadResourceFile(owningResource, file)
|
|
||||||
local mapFunction, err = load(fileData, file, 't', env)
|
|
||||||
|
|
||||||
if not mapFunction then
|
|
||||||
Citizen.Trace("Couldn't load map " .. file .. ": " .. err .. " (type of fileData: " .. type(fileData) .. ")\n")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
mapFunction()
|
|
||||||
end
|
|
||||||
|
|
||||||
AddEventHandler('getMapDirectives', function(add)
|
AddEventHandler('getMapDirectives', function(add)
|
||||||
add('vehicle_generator', function(state, name)
|
add('vehicle_generator', function(state, name)
|
||||||
return function(opts)
|
return function(opts)
|
||||||
@@ -181,16 +83,20 @@ AddEventHandler('getMapDirectives', function(add)
|
|||||||
color1 = opts.color1 or -1
|
color1 = opts.color1 or -1
|
||||||
color2 = opts.color2 or -1
|
color2 = opts.color2 or -1
|
||||||
|
|
||||||
local hash = GetHashKey(name)
|
CreateThread(function()
|
||||||
RequestModel(hash)
|
local hash = GetHashKey(name)
|
||||||
|
RequestModel(hash)
|
||||||
|
|
||||||
LoadAllObjectsNow()
|
while not HasModelLoaded(hash) do
|
||||||
|
Wait(0)
|
||||||
|
end
|
||||||
|
|
||||||
local carGen = CreateScriptVehicleGenerator(x, y, z, heading, 5.0, 3.0, hash, color1, color2, -1, -1, true, false, false, true, true, -1)
|
local carGen = CreateScriptVehicleGenerator(x, y, z, heading, 5.0, 3.0, hash, color1, color2, -1, -1, true, false, false, true, true, -1)
|
||||||
SetScriptVehicleGenerator(carGen, true)
|
SetScriptVehicleGenerator(carGen, true)
|
||||||
SetAllVehicleGeneratorsActive(true)
|
SetAllVehicleGeneratorsActive(true)
|
||||||
|
|
||||||
state.add('cargen', carGen)
|
state.add('cargen', carGen)
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
end, function(state, arg)
|
end, function(state, arg)
|
||||||
Citizen.Trace("deleting car gen " .. tostring(state.cargen) .. "\n")
|
Citizen.Trace("deleting car gen " .. tostring(state.cargen) .. "\n")
|
||||||
|
|||||||
@@ -1,25 +1,7 @@
|
|||||||
-- loosely based on MTA's https://code.google.com/p/mtasa-resources/source/browse/trunk/%5Bmanagers%5D/mapmanager/mapmanager_main.lua
|
-- loosely based on MTA's https://code.google.com/p/mtasa-resources/source/browse/trunk/%5Bmanagers%5D/mapmanager/mapmanager_main.lua
|
||||||
|
|
||||||
maps = {}
|
local maps = {}
|
||||||
gametypes = {}
|
local gametypes = {}
|
||||||
|
|
||||||
AddEventHandler('getResourceInitFuncs', function(isPreParse, add)
|
|
||||||
add('resource_type', function(type)
|
|
||||||
return function(params)
|
|
||||||
local resourceName = GetInvokingResource()
|
|
||||||
|
|
||||||
if type == 'map' then
|
|
||||||
maps[resourceName] = params
|
|
||||||
elseif type == 'gametype' then
|
|
||||||
gametypes[resourceName] = params
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
|
|
||||||
add('map', function(file)
|
|
||||||
AddAuxFile(file)
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
|
|
||||||
local function refreshResources()
|
local function refreshResources()
|
||||||
local numResources = GetNumResources()
|
local numResources = GetNumResources()
|
||||||
@@ -47,8 +29,16 @@ end)
|
|||||||
refreshResources()
|
refreshResources()
|
||||||
|
|
||||||
AddEventHandler('onResourceStarting', function(resource)
|
AddEventHandler('onResourceStarting', function(resource)
|
||||||
if GetNumResourceMetadata(resource, 'map') then
|
local num = GetNumResourceMetadata(resource, 'map')
|
||||||
-- todo: add file
|
|
||||||
|
if num then
|
||||||
|
for i = 0, num-1 do
|
||||||
|
local file = GetResourceMetadata(resource, 'map', i)
|
||||||
|
|
||||||
|
if file then
|
||||||
|
addMap(file, resource)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if maps[resource] then
|
if maps[resource] then
|
||||||
@@ -132,7 +122,6 @@ AddEventHandler('onResourceStart', function(resource)
|
|||||||
SetGameType(gtName)
|
SetGameType(gtName)
|
||||||
|
|
||||||
print('Started gametype ' .. gtName)
|
print('Started gametype ' .. gtName)
|
||||||
TriggerClientEvent('onClientGameTypeStart', -1, getCurrentGameType())
|
|
||||||
|
|
||||||
SetTimeout(50, function()
|
SetTimeout(50, function()
|
||||||
if not currentMap then
|
if not currentMap then
|
||||||
@@ -155,6 +144,9 @@ AddEventHandler('onResourceStart', function(resource)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- handle starting
|
||||||
|
loadMap(resource)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local function handleRoundEnd()
|
local function handleRoundEnd()
|
||||||
@@ -177,6 +169,10 @@ AddEventHandler('mapmanager:roundEnded', function()
|
|||||||
SetTimeout(50, handleRoundEnd) -- not a closure as to work around some issue in neolua?
|
SetTimeout(50, handleRoundEnd) -- not a closure as to work around some issue in neolua?
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
function roundEnded()
|
||||||
|
SetTimeout(50, handleRoundEnd)
|
||||||
|
end
|
||||||
|
|
||||||
AddEventHandler('onResourceStop', function(resource)
|
AddEventHandler('onResourceStop', function(resource)
|
||||||
if resource == currentGameType then
|
if resource == currentGameType then
|
||||||
TriggerEvent('onGameTypeStop', resource)
|
TriggerEvent('onGameTypeStop', resource)
|
||||||
@@ -191,6 +187,9 @@ AddEventHandler('onResourceStop', function(resource)
|
|||||||
|
|
||||||
currentMap = nil
|
currentMap = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- unload the map
|
||||||
|
unloadMap(resource)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
AddEventHandler('rconCommand', function(commandName, args)
|
AddEventHandler('rconCommand', function(commandName, args)
|
||||||
|
|||||||
@@ -1 +1,86 @@
|
|||||||
-- shared logic file for map manager - don't call any subsystem-specific functions here
|
-- shared logic file for map manager - don't call any subsystem-specific functions here
|
||||||
|
mapFiles = {}
|
||||||
|
|
||||||
|
function addMap(file, owningResource)
|
||||||
|
if not mapFiles[owningResource] then
|
||||||
|
mapFiles[owningResource] = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
table.insert(mapFiles[owningResource], file)
|
||||||
|
end
|
||||||
|
|
||||||
|
undoCallbacks = {}
|
||||||
|
|
||||||
|
function loadMap(res)
|
||||||
|
if mapFiles[res] then
|
||||||
|
for _, file in ipairs(mapFiles[res]) do
|
||||||
|
parseMap(file, res)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function unloadMap(res)
|
||||||
|
if undoCallbacks[res] then
|
||||||
|
for _, cb in ipairs(undoCallbacks[res]) do
|
||||||
|
cb()
|
||||||
|
end
|
||||||
|
|
||||||
|
undoCallbacks[res] = nil
|
||||||
|
mapFiles[res] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function parseMap(file, owningResource)
|
||||||
|
if not undoCallbacks[owningResource] then
|
||||||
|
undoCallbacks[owningResource] = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
local env = {
|
||||||
|
math = math, pairs = pairs, ipairs = ipairs, next = next, tonumber = tonumber, tostring = tostring,
|
||||||
|
type = type, table = table, string = string, _G = env
|
||||||
|
}
|
||||||
|
|
||||||
|
TriggerEvent('getMapDirectives', function(key, cb, undocb)
|
||||||
|
env[key] = function(...)
|
||||||
|
local state = {}
|
||||||
|
|
||||||
|
state.add = function(k, v)
|
||||||
|
state[k] = v
|
||||||
|
end
|
||||||
|
|
||||||
|
local result = cb(state, ...)
|
||||||
|
local args = table.pack(...)
|
||||||
|
|
||||||
|
table.insert(undoCallbacks[owningResource], function()
|
||||||
|
undocb(state)
|
||||||
|
end)
|
||||||
|
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
local mt = {
|
||||||
|
__index = function(t, k)
|
||||||
|
if rawget(t, k) ~= nil then return rawget(t, k) end
|
||||||
|
|
||||||
|
-- as we're not going to return nothing here (to allow unknown directives to be ignored)
|
||||||
|
local f = function()
|
||||||
|
return f
|
||||||
|
end
|
||||||
|
|
||||||
|
return function() return f end
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
setmetatable(env, mt)
|
||||||
|
|
||||||
|
local fileData = LoadResourceFile(owningResource, file)
|
||||||
|
local mapFunction, err = load(fileData, file, 't', env)
|
||||||
|
|
||||||
|
if not mapFunction then
|
||||||
|
Citizen.Trace("Couldn't load map " .. file .. ": " .. err .. " (type of fileData: " .. type(fileData) .. ")\n")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
mapFunction()
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user