关闭

lua 中 loadstring 应用(用于游戏中解析字符串公式)

532人阅读 评论(0) 收藏 举报
分类:
local  str = '{attrKey}{(operator)num2*skilllevel},持续{buff_time+buff_add_time*skilllevel}秒'




function dealBuffDescript(buffId)
-- body
-- local totalNameTable = CSV:GetTable("total_name")
-- local buffTable = CSV:GetTable("skill_buff")
-- local buffInfoTable = CSV:GetTable("skill_buff_info")


if buffId< 0  then return end
if buffTable[buffId] == nil then return end
local buffTableInfo = buffTable[buffId]
local buffType = buffTableInfo.trigger_type
local info = buffInfoTable[buffType]
if info == nil then return end


local arrtPosId = 0
local function canAddOperator(str )
-- body
local startPos,endPos,resStr = string.find(str,"(operator)")
if startPos ~= nil and resStr == "operator" then
return true,string.sub(str,endPos+2,string.len(str))
end
return false,str
end 
-- 解析lua code
local function loadLuaCode(code)
-- body
local func = loadstring(code)
if func == nil then return nil end
return func()
end 
local function analysisLuaCode(luaCode)
-- body
if luaCode == "attrKey" then
arrtPosId = arrtPosId+1
else
local valuePos = arrtPosId  --属性为多个加成
if arrtPosId <= 0 then
valuePos = 1
end
local addOperate,luaCode = canAddOperator(luaCode)
local num2 = buffTableInfo.num_2[valuePos]
local skilllevel = 10
local buff_time = buffTableInfo.buff_time
local buff_add_time = buffTableInfo.buff_add_time
local num3 = buffTableInfo.num_3[1] 
local num4 = buffTableInfo.num_4[1]
local newLuaCode = "local num2="..num2.."  local skilllevel="..skilllevel.."  buff_time="..buff_time.."  buff_add_time="..buff_add_time.."  num3="..num3.."  num4="..num4.."  return "..luaCode
return loadLuaCode(newLuaCode)
end
end 
local function analysisLuaStr(analyStr)
-- body
local startPos,endPos = string.find(analyStr,"%b{}")
if startPos == nil then return analyStr end
local code = string.sub(analyStr,startPos,endPos)
local startStr = string.sub(analyStr,0,startPos-1)
code = string.sub(code,2,string.len(code)-1)
local value = analysisLuaCode(code) or ""
local newStr = startStr..value..string.sub(analyStr,endPos+1,-1)
return analysisLuaStr(newStr)
end 


local listPos = 1
-- if buffType == ConstantConfig_buff_type.buff_icebound or buffType == ConstantConfig_buff_type.buff_hp or buffType == ConstantConfig_buff_type.buff_mp then
-- listPos = info.num_2[1]
-- end
if buffType >=2 and buffType <= 4 then
listPos = buffTableInfo.num_1[1]
end
if listPos < 0 then return end
local desInfo = totalNameTable[info.info]
local desStr = desInfo.info[listPos]
if desStr == nil then return end
print(desStr)
print(analysisLuaStr(desStr))

end


dealBuffDescript(1)
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:7944次
    • 积分:183
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章分类