Appearance
unison.config — 配置系统
配置系统提供 XML 配置解析、加密存储、XOR 编解码等功能。
使用方式
python
from unison import config引擎设置 (settings)
通过 config.settings 可以只读访问引擎配置。
python
width = config.settings.window_width # 1280
height = config.settings.window_height # 720
scene_path = config.settings.scene_path # "%WORK_DIR%/scene"
for fs in config.settings.filesystems:
print(fs.name, fs.type, fs.root)settings 属性
| 属性 | 类型 | 说明 |
|---|---|---|
window_width | int | 窗口宽度 |
window_height | int | 窗口高度 |
show_log_window | bool | 是否显示日志窗口 |
create_main_window | bool | 是否创建主窗口 |
python_thread_enable | bool | Python 线程是否启用 |
config_found | bool | 是否找到配置文件 |
sys_path | list[str] | Python sys.path 列表 |
filesystems | list[FileSystemEntry] | 文件系统列表 |
lib_paths | list[str] | 库路径列表 |
scene_path | str | 场景路径 |
FileSystemEntry
| 属性 | 类型 | 说明 |
|---|---|---|
name | str | 文件系统名称 |
type | str | 类型 |
root | str | 根路径 |
loader_type | str | 加载器类型 |
encryption_key | int | 加密密钥 |
ConfigNode — XML 配置节点
用于解析和操作 XML 配置文件。
python
node = config.ConfigNode.from_xml_file("config.xml")
# 或从加密文件
node = config.ConfigNode.from_encrypted_file("data.binxml", key="mykey")静态方法
| 方法 | 说明 |
|---|---|
from_xml_file(filepath) | 从 XML 文件创建 |
from_xml_string(xml_str) | 从 XML 字符串创建 |
from_encrypted_file(filepath, key='unison') | 从加密文件创建 |
实例方法
| 方法 | 说明 |
|---|---|
get(key, default='') | 获取字符串配置值 |
get_int(key, default=0) | 获取整数配置值 |
get_float(key, default=0.0) | 获取浮点配置值 |
get_bool(key, default=false) | 获取布尔配置值 |
set(key, value) | 设置配置值 |
has(key) | 检查键是否存在 |
keys() | 获取所有键名列表 |
find_node(path) | 按路径查找子节点,返回 XmlNode 或 None |
find_nodes_by_name(name) | 按名称查找所有子节点 |
save_to_file(filepath) | 保存到 XML 文件 |
save_to_encrypted_file(filepath, key) | 保存到加密文件 |
to_xml_string() | 转换为 XML 字符串 |
clear() | 清空所有数据 |
XmlNode
XML 节点对象。
python
node = config.ConfigNode.from_xml_file("ui.xml")
root = node.find_node("root")
print(root.name) # 节点名
print(root.text) # 文本内容
print(root.attributes) # 属性字典
for child in root.get_children():
print(child.name, child.get_attr("id"))属性
| 属性 | 类型 | 说明 |
|---|---|---|
name | str | 节点名称 |
text | str | 节点文本内容 |
attributes | dict | 属性字典 |
方法
| 方法 | 说明 |
|---|---|
get_attr(attr, default='') | 获取指定属性的值 |
get_children(name='') | 获取子节点列表,可选按名称过滤 |
XML 便捷函数
python
# 查找节点
node = config.xml_find("root/menu/button1")
nodes = config.xml_find_all("button")
# 获取节点属性/文本
text = config.xml_get_text(node, "默认文本")
attr = config.xml_get_attr(node, "id", "unknown")
# 保存
config.xml_save("output.xml")
config.xml_save_encrypted("output.binxml", key="mykey")
# 解析
node = config.parse_xml_string("<root><item id='1'/></root>")
node = config.parse_xml_file("data.xml")函数列表
| 函数 | 说明 |
|---|---|
xml_find(path) | 按路径查找节点,返回 XmlNode 或 None |
xml_find_all(name) | 按名称查找所有节点 |
xml_get_attr(node, attr, default) | 获取节点属性 |
xml_get_text(node, default) | 获取节点文本 |
xml_get_children(node, name) | 获取子节点列表 |
xml_save(filepath) | 保存 XML 到文件 |
xml_save_encrypted(filepath, key) | 加密保存 XML |
parse_xml_string(xml) | 解析 XML 字符串 |
parse_xml_file(filepath) | 解析 XML 文件 |
加密函数
encrypt_xml(input_path, output_path, key='unison')
加密 XML 文件到 binxml 格式。
decrypt_xml(input_path, output_path, key='unison')
解密 XML 文件。
is_encrypted(path)
检查文件是否为加密文件。
XOR 编解码
字符串级别
python
enc = config.xor_encrypt_string("hello", "mykey")
dec = config.xor_decrypt_string(enc, "mykey")
hex_str = config.xor_encrypt_to_hex("hello", "mykey")
dec = config.xor_decrypt_from_hex(hex_str, "mykey")文件级别
python
config.xor_encrypt_file("input.txt", "output.enc", "mykey")
config.xor_decrypt_file("output.enc", "decrypted.txt", "mykey")函数列表
| 函数 | 说明 |
|---|---|
xor_encrypt_string(plaintext, key) | XOR 字符串加密 |
xor_decrypt_string(ciphertext, key) | XOR 字符串解密 |
xor_encrypt_to_hex(plaintext, key) | XOR 加密并转 Hex |
xor_decrypt_from_hex(hexStr, key) | Hex 解码后 XOR 解密 |
xor_encrypt_file(input, output, key) | XOR 加密文件 |
xor_decrypt_file(input, output, key) | XOR 解密文件 |