版权声明:本文转自http://blog.csdn.net/huutu 转载请带上 http://www.liveslives.com/ https://blog.csdn.net/cp790621656/article/details/82590675
本文基于我对slua做了一系列修改的基础,需要看完我其他SLua文章才能套用到其他项目。
https://blog.csdn.net/huutu/article/category/5971455
16年做lua的时候,测试发现不能跨文件引用message。
https://blog.csdn.net/huutu/article/details/50705705
然后项目也没有这个跨文件的需求,所以就扔着。
现在的项目有这个需求,然后来看一下。
跨文件引用的情况
上图的message引用了下图的message
然后编译出lua。脚本中的message_type显示如下
PET_PB_GM_PET_PROPERTY_ENTRY
这个东西是找不到的。
正常的同文件引用,生成的代码像下面
.message_type = rank_pbTable.GM_OTHERINFO_PLAYERSKILL_ENTRY
对比一下,发现 PET_PB_GM_PET_PROPERTY_ENTRY 正确的应该是
pet_pbTable.GM_PET_PROPERTY_ENTRY
修改成这个就可以了。
随手写了个python脚本来做这一部分修改:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
def FixCrossProtoImport(varFilePath):
tmpTxtFile=open(varFilePath,"r")
print "fileName:",tmpTxtFile.name
tmpAllLine=tmpTxtFile.readlines()
tmpTxtFile.close()
tmpTxtFile=open(varFilePath,"w")
tmpSelfTableName=""
tmpPBNameList=[]
for tmpLine in tmpAllLine:
if 'local ' and 'Table = {}' in tmpLine:
print(tmpLine)
tmpSelfTableName=tmpLine[6:-6]
print(tmpSelfTableName)
if 'require("' in tmpLine:
print(tmpLine)
tmpPBName=tmpLine.split('"')[1]
print(tmpPBName)
tmpPBNameList.append(tmpPBName)
tmpLineRequireLuaFile="local "+tmpPBName+'Table=require("ProtoLua/'+tmpPBName+'.lua")\n'
tmpTxtFile.write(tmpLineRequireLuaFile)
print(tmpLineRequireLuaFile)
#修改跨文件引用 .message_type
if ".message_type" in tmpLine:
#print(tmpLine)
#print("tmpSelfTableName:"+tmpSelfTableName)
if ("= "+tmpSelfTableName in tmpLine)==False:
print(tmpLine)
for tmpPBName in tmpPBNameList:
if tmpPBName.upper() in tmpLine:
print(tmpPBName.upper())
tmpLine= tmpLine.replace(tmpPBName.upper()+"_",tmpPBName+"Table.")
print(tmpLine)
tmpTxtFile.write(tmpLine)
tmpTxtFile.write("return "+tmpSelfTableName)
tmpTxtFile.close()
return
#先获取所有lua文件
tmpFileAndDirList=os.listdir("./newlua/")
for tmpFilePath in tmpFileAndDirList:
if tmpFilePath.endswith(".lua"):
FixCrossProtoImport("./newlua/"+tmpFilePath)