您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用Python编码解析CS:GO语言文件

使用Python编码解析CS:GO语言文件

此输入文件有一些新的皱纹,而原始CS:GO示例中没有这些皱纹:

通过修改的定义来解决前两个问题value_qs。由于值现在比键具有更全面的功能,因此我决定为其使用单独的QuotedString定义:

key_qs = QuotedString('"').setName("key_qs")
value_qs = QuotedString('"', escChar='\\', multiline=True).setName("value_qs")

第三个需要更多的重构。这些限定条件的使用类似于#IFDEFC中的宏-仅当环境符合条件时,它们才启用/禁用定义。其中一些条件甚至是布尔表达式:

这可能会导致定义文件中的键重复,例如以下几行:

"Menu_Dlg_Leaderboards_Lost_Connection"     "You must be connected to X@R_27_2419@ LIVE to view Leaderboards. Please check your connection and try again." [$X360]
"Menu_Dlg_Leaderboards_Lost_Connection"     "You must be connected to PlayStation®Network and Steam to view Leaderboards. Please check your connection and try again." [$PS3]
"Menu_Dlg_Leaderboards_Lost_Connection"     "You must be connected to Steam to view Leaderboards. Please check your connection and try again."

其中包含3个键“ Menu_Dlg_Leaderboards_Lost_Connection”的定义,具体取决于所设置的环境值。

为了在解析文件不会丢失这些值,我选择了在解析时通过附加条件(如果存在)来修改密钥。这段代码实现了更改:

LBRACK,RBRACK = map(Suppress, "[]")
qualExpr = Word(alphanums+'$!&|')
qualExprCondition = LBRACK + qualExpr + RBRACK

key_value = Group(key_qs + value + Optional(qualExprCondition("qual")))
def addQualifierToKey(tokens):
    tt = tokens[0]
    if 'qual' in tt:
        tt[0] += '/' + tt.pop(-1)
key_value.setParseAction(addQualifierToKey)

这样,在上面的示例中,您将获得3个键:

最后,可能是最简单的评论处理。Pyparsing内置了对跳过注释的支持,就像空白一样。您只需要定义注释的表达式,并让顶级解析器忽略它。为了支持功能,在pyparsing中预定义了几种常见的注释形式。在这种情况下,解决方案只是将最终解析器的定义更改为:

parser.ignore(dblSlashComment)

最后,最后是QuotedString的实现中存在一个错误,在该错误中,标准的空白字符串文字(例如\t和)\n未处理,仅被视为不必要的转义“ t”或“ n”。所以现在,当这一行被解析时:

"SFUI_SteamOverlay_Text"  "This feature requires Steam Community In-Game to be enabled.\n\nYou might need to restart the game after you enable this feature in Steam:\nSteam -> File -> Settings -> In-Game: Enable Steam Community In-Game\n" [$WIN32]

对于值字符串,您只会得到:

This feature requires Steam Community In-Game to be enabled.nnYou 
might need to restart the game after you enable this feature in 
Steam:nSteam -> File -> Settings -> In-Game: Enable Steam Community 
In-Gamen

代替:

This feature requires Steam Community In-Game to be enabled.

You might need to restart the game after you enable this feature in Steam:
Steam -> File -> Settings -> In-Game: Enable Steam Community In-Game

我将不得不在下一版pyparsing中修复此行为。

这是最终的解析器代码

from pyparsing import (Suppress, QuotedString, Forward, Group, Dict, 
    ZeroOrMore, Word, alphanums, Optional, dblSlashComment)

LBRACE,RBRACE = map(Suppress, "{}")

key_qs = QuotedString('"').setName("key_qs")
value_qs = QuotedString('"', escChar='\\', multiline=True).setName("value_qs")

# use this code to convert integer values to ints at parse time
def convert_integers(tokens):
    if tokens[0].isdigit():
        tokens[0] = int(tokens[0])
value_qs.setParseAction(convert_integers)

LBRACK,RBRACK = map(Suppress, "[]")
qualExpr = Word(alphanums+'$!&|')
qualExprCondition = LBRACK + qualExpr + RBRACK

value = Forward()
key_value = Group(key_qs + value + Optional(qualExprCondition("qual")))
def addQualifierToKey(tokens):
    tt = tokens[0]
    if 'qual' in tt:
        tt[0] += '/' + tt.pop(-1)
key_value.setParseAction(addQualifierToKey)

struct = (LBRACE + Dict(ZeroOrMore(key_value)) + RBRACE).setName("struct")
value <<= (value_qs | struct)
parser = Dict(key_value)
parser.ignore(dblSlashComment)

sample = open('cs_go_sample2.txt').read()
config = parser.parseString(sample)


print (config.keys())
for k in config.lang.keys():
    print ('- ' + k)

#~ config.lang.pprint()
print (config.lang.Tokens.StickerKit_comm01_burn_them_all)
print (config.lang.Tokens['SFUI_SteamOverlay_Text/$WIN32'])
python 2022/1/1 18:48:32 有327人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶