Skip to main content

A python package for parsing FGUI project files

Project description

FairyGUI-Python

在Python中实现的,对于FGUI项目进行解析的代码。最多可以解析到元件的层级。

未引用任何第三方库,仅使用Python自带的库。

目前功能还比较简陋,但已经满足了自己的大部分需求,欢迎各位大佬提出建议和PR,一起完善这个小项目。

示例代码

from fairy_gui import *

# 加载一个FGUI项目
fgui_file_path:str = r"D:\FGUIProject\FGUIProject.fairy"
project:FGUIProject = FGUIProject(fgui_file_path)

# 获取项目的主分支(目前仅支持)
branch:FGUIBranch = project.main_branch

# 获取该分支下所有包
package_list:list[FGUIPackage] = branch.package_list

# 获取某个名字的包
master_package:FGUIPackage = branch.get_package_by_name("master")

# 获取指定url对应的资源
component_url:str = "ui://pkgidandresid"
resource:FGUIResource = branch.get_resource_by_url(component_url)

# 获取一个组件资源内部的元件
obj_list:list[FGUIObject] = resource.object_list
test_obj:FGUIObject = obj_list[0]

# 获取对其他资源的引用关系,会返回有效的引用资源和无效的引用资源
# 包层级的引用(包括包内所有资源,以及资源内所有元件)
pkg_ref_list, pkg_invalid_ref_list = master_package.get_references()
# 资源层级的引用(包括资源内所有元件)
res_ref_list, res_invalid_ref_list = resource.get_references()
# 元件层级的引用
obj_ref_list, obj_invalid_ref_list = test_obj.get_references()

# 你也可以判断一个对应的资源文件是否真的存在
if resource.file_exists is False:
    print("资源文件不存在")
else:
    print("资源文件存在于:" + resource.full_path)

想把依赖关系可视化?试试这个:

import networkx as nx
from pyvis.network import Network

from fairy_gui import *

fgui_file_path:str = r"D:\FGUIProject\FGUIProject.fairy"
project:FGUIProject = FGUIProject(fgui_file_path)

# 获取项目的主分支(目前仅支持)
branch:FGUIBranch = project.main_branch

network_map:dict[str,list[str]] = dict()

for package in branch.package_list:
    print(f"Package: {package.package_name}")
    package_name_set:set[str] = set()
    all_references, _ = package.get_references()
    for reference_url in all_references:
        reference_package_name:str = branch.get_package_by_id(reference_url.package_id).package_name
        package_name_set.add(reference_package_name)

    for package_name in package_name_set:
        print(f"    {package_name}")
        if package.package_name not in network_map:
            network_map[package.package_name] = []
        network_map[package.package_name].append(package_name)

# 绘制网络关系图
print("=" * 20)
G = nx.Graph()

for package_name in network_map:
    for reference_package_name in network_map[package_name]:
        G.add_edge(package_name, reference_package_name)

# 画图
nx.draw(G, with_labels=True)

nt = Network(
    height="100vh",
    width="100%",
    directed=True,
    notebook=False,  # 必须设置为 False 才能启用完整响应式支持
    bgcolor="#ffffff",
    cdn_resources="in_line"  # 内联资源,避免 CDN 依赖
)

nt.toggle_physics(False)

# 手动计算网格坐标
grid_spacing = 400  # 像素间距

# 按照引用数量排序,从多到少排序
nodes = list(G.nodes())
nodes.sort(key=lambda x: G.degree[x], reverse=True)


for idx, node in enumerate(nodes):
    row = idx // 20
    col = idx % 20
    x = col * grid_spacing
    y = row * grid_spacing
    nt.add_node(
        node,
        x=x,
        y=y,
        label=node,
        font={"size": 14, "color": "#333"},  # 优化标签可读性
        shape="box",  # 节点显示为方块
        color="#4CAF50"
    )

# 设置边的样式为直线
nt.set_edge_smooth('false')

# 添加边
for edge in G.edges():
    nt.add_edge(edge[0], edge[1], width=1.2, color="#666")

# 生成 HTML 并注入自定义 CSS
html_content = nt.generate_html()

# 插入响应式 CSS 样式
css_injection = """
<style>
  body { margin: 0; padding: 0; overflow: hidden; }
  #mynetwork { 
    width: 100vw !important; 
    height: 100vh !important;
  }
</style>
"""
html_content = html_content.replace("</head>", css_injection + "</head>")


with open("fgui_net.html", "w", encoding="utf-8") as f:
    f.write(html_content)

print("Export network to fgui_net.html")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_fgui-0.0.1.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_fgui-0.0.1-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file py_fgui-0.0.1.tar.gz.

File metadata

  • Download URL: py_fgui-0.0.1.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.1

File hashes

Hashes for py_fgui-0.0.1.tar.gz
Algorithm Hash digest
SHA256 57c73b579470037546d3cf1b8920f9c8ce7c19ac831a3324f2b3430092f2b67a
MD5 fcaaba0a9d1a0a08749831aa6545a180
BLAKE2b-256 596e64e0f226ce96f994c61c5880f79c6dafd6f9d16051b8c9ec4a81cea1a3ad

See more details on using hashes here.

File details

Details for the file py_fgui-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: py_fgui-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.1

File hashes

Hashes for py_fgui-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6b471649fdb22c1ad2d29d79861768afbeb2089157db70423910e02bc4e21881
MD5 7e30d563352219bb34133c802b6cf0e5
BLAKE2b-256 d778fdb54eb0f50ee46455d6eb89cf9365bad7878a0b10a3131621b6c8dc8419

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page