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

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)
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值