Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Ball switcher: assign the ball for home team
- -- Custom content is used, not LiveCPK/game: content\ball-server is the root
- -- author: zlac, 2017
- -- originally posted on evo-web
- local new_ball_path
- local ballroot = ".\\content\\ball-server\\"
- local team_assignment_map = {}
- local competition_assignment_map = {}
- -- override_competitions contains comma-separated ID's of the competitions that allow for team-assigned balls to have precedence before competiton-assigned balls
- -- .. e.g. you've assigned an official ball for ALL exhibition mode matches (tid 0) in map_competitions.txt, but you still want to use
- -- .. home-team ball for those teams that have it assigned in map_teams.txt -> add 0 in override_competitions
- -- initially, only Exhibition mode matches are included in competition overrides
- -- BEGIN CUSTOMIZABLE LUA TABLE
- local override_competitions = {0,}
- -- END CUSTOMIZABLE LUA TABLE
- -- remove trailing and leading whitespace from string
- local function trim(s)
- return s:gsub("^%s*(.-)%s*$", "%1")
- end
- local function split(s, delim)
- local p = "[^"..table.concat(delim).."]+"
- local fields = {}
- for w in s:gmatch(p) do
- w = trim(w)
- fields[#fields+1] = w
- end
- return fields
- end
- -- splits multi-line text to individual lines
- local function to_lines(s)
- local t = {}
- local function helper(line)
- line = trim(line)
- if line:len() > 0 and line:sub(1, 1) ~= "#" then
- table.insert(t, line)
- end
- return ""
- end
- helper((s:gsub("(.-)\r?\n", helper)))
- return t
- end
- local function load_map_txt(filename)
- local delim = {",", "#"}
- local file = assert(io.open(ballroot .. filename, "r"))
- log(filename .. " found in " .. ballroot .. filename)
- local data = trim(file:read("*all"))
- data = string.gsub(data, "^\239\187\191", "") -- removes UTF BOM bytes at the beginning of the .txt file
- for i, line in pairs(to_lines(data)) do
- local fields = split(line, delim)
- if #fields > 1 then
- for i=1,#fields do
- local field = trim(fields[i])
- if field == "" then
- field = null
- end
- fields[i] = field
- end
- if fields[1] ~= nil then
- if filename == "map_teams.txt" then
- team_assignment_map[tonumber(fields[1])] = fields[2]
- log(string.format(" ==> %s ball assignment:: %s: %s", filename, fields[1], fields[2]))
- end
- if filename == "map_competitions.txt" then
- competition_assignment_map[tonumber(fields[1])] = {fields[2], fields[3], fields[4]}
- log(string.format(" ==> %s ball assignment:: %s: %s (for final match: %s | for winter match: %s)", filename, fields[1], fields[2], fields[3], fields[4]))
- end
- end
- end
- end
- file:close()
- end
- local function has_value(tab, val)
- for index, value in ipairs(tab) do
- if value == val then
- return true
- end
- end
- return false
- end
- local function which_ball_model(filename)
- return string.match(string.lower(filename), "render\\model\\ball\\ball%d+\\(ball.*%..+)")
- end
- local function which_ball_preview_tex(filename)
- return string.match(string.lower(filename), "render\\thumbnail\\ball\\(ball_%d+%.dds)")
- end
- local function make_key(ctx, filename)
- local function get_new_ball_path(ctx)
- local tid = tonumber(ctx.tournament_id)
- local ball_path
- if tid then
- if competition_assignment_map[tid] == nil and team_assignment_map[ctx.home_team] ~= nil then
- -- this particular competiton mode does not have the ball assigned via map_competitions.txt
- -- .. individual home team assignments from map_teams.txt will be used here
- ball_path = team_assignment_map[ctx.home_team]
- elseif competition_assignment_map[tid] ~= nil and team_assignment_map[ctx.home_team] ~= nil and has_value(override_competitions, tid) then
- -- this particular competiton mode HAS the ball assigned via map_competitions.txt,
- -- .. but it is also listed in override_competitions, therefore
- -- .. individual home team assignments from map_teams.txt will be used here again
- ball_path = team_assignment_map[ctx.home_team]
- else
- -- nothing else but possible competition assignment in map_competitions.txt
- -- .. ball assignment from map_competitions.txt will be used, if there is any
- if competition_assignment_map[tid] ~= nil then
- -- is this the final match of a competition (53)?
- if ctx.season == 1 then
- ball_path = competition_assignment_map[tid][3] -- use the ball assigned for winter season
- elseif ctx.match_info == 53 then
- ball_path = competition_assignment_map[tid][2] -- use the ball assigned for final match; could be nil
- else
- ball_path = competition_assignment_map[tid][1] -- use the regular ball assigned for this competition
- end
- end
- end
- end
- return ball_path
- end
- local bm_fname = which_ball_model(filename)
- if bm_fname then
- new_ball_path = get_new_ball_path(ctx)
- if new_ball_path then
- return new_ball_path .. "\\" .. bm_fname
- end
- end
- local bpt_fname = which_ball_preview_tex(filename)
- if bpt_fname then
- new_ball_path = get_new_ball_path(ctx)
- if new_ball_path then
- return new_ball_path .. "\\preview.dds"
- end
- end
- end
- local function get_filepath(ctx, filename, key)
- if new_ball_path then
- log(string.format("Ball assignment for team ID %d (competition ID %d) - %s\\%s", tonumber(ctx.home_team), tonumber(ctx.tournament_id), ballroot, key))
- return string.format("%s\\%s", ballroot, key)
- end
- end
- local function init(ctx)
- if ballroot:sub(1,1)=='.' then
- ballroot = ctx.sider_dir .. ballroot
- end
- load_map_txt("map_teams.txt")
- load_map_txt("map_competitions.txt")
- ctx.register("livecpk_make_key", make_key)
- ctx.register("livecpk_get_filepath", get_filepath)
- end
- return { init = init }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement