1.通过最顶层结构检索到所有子结构
1)get_lib.sh
#!/bin/bash
grep -F '\Pp2\' proto/$1.php | grep "=> " | awk -F' ' '{print $3}' | sed "s/'//g" | awk -F'\' '{print $3}'
2)get_all_struct.php
<?php
function get_file_name($input_path, &$global_struct_list)
{
if($input_path != "")
{
$global_struct_list[] = $input_path;
}
ob_start();
passthru("./get_lib.sh $input_path");
$all_names_str = ob_get_contents();
ob_end_clean();
$all_names = explode("\n", $all_names_str);
foreach($all_names as $sub_node)
{
if($sub_node != "")
{
get_file_name($sub_node, $global_struct_list);
}
}
}
2.使用时,直接给顶层结构名称,
test.php
<?php
require("comm_define.php");
require("get_all_struct.php");
$global_struct_list = array();
get_file_name("GuildDispatchList", $global_struct_list);
$global_struct_list = array_unique($global_struct_list);
foreach($global_struct_list as $sub_struct)
{
require("proto/$sub_struct.php");
}
$p = new Pp2\GuildDispatchList();
$p->parseFromString("\x08\x00\x10\xD0\xE3\xB0\xF6\x05\x1A\x0F\x08\x01\x10\x00");
$p->dump();
?>
本文介绍了一种使用PHP和Shell脚本结合的方法,来解析并遍历PHP中定义的复杂结构。通过自定义的get_file_name函数,递归地获取指定顶层结构下所有的子结构名称,最终实现对整个结构体系的加载和实例化。
7

被折叠的 条评论
为什么被折叠?



