Skip to content

unison.vfs — 虚拟文件系统

unison.vfs 提供统一的文件访问接口,优先从已挂载的 VFS(CPK 加密包)中查找文件,找不到时自动 fallback 到真实文件系统。

使用方式

python
from unison import vfs

基础函数

这些函数由 <pymodules> 配置从后端模块(如 exio3)映射而来,具体可用性取决于配置。

mount(path, mount_point, *, encryption_key=0)

挂载 CPK 文件到虚拟文件系统。

python
vfs.mount("Lib.cpk", "Lib/", encryption_key=-231)
vfs.mount("data.cpk", "data/")
参数类型说明
pathstrCPK 文件路径
mount_pointstr挂载点,通常以 / 结尾
encryption_keyint加密密钥,0=不加密,负数=有密钥

find_file(path)

检查文件是否存在于 VFS 中。

python
if vfs.find_file("Scripts/main.py"):
    print("文件存在")
参数类型说明
pathstr虚拟路径
返回bool文件是否存在

get_file(path)

从 VFS 读取文件内容(字节)。

python
data = vfs.get_file("data/config.json")
参数类型说明
pathstr虚拟路径
返回bytes文件内容

list_dir(directory)

列出 VFS 目录中的文件。

python
files = vfs.list_dir("Scripts/")
参数类型说明
directorystr虚拟目录路径
返回list文件路径列表

get_doc_dir()

获取用户文档目录路径。

python
doc_dir = vfs.get_doc_dir()

| 返回 | str | 文档目录绝对路径 |

get_real_path(virtual_path)

将虚拟路径转换为真实文件系统路径。

python
real = vfs.get_real_path("Scripts/main.py")
参数类型说明
virtual_pathstr虚拟路径
返回str真实文件系统路径

混合 API

以下 API 由引擎自动注册,优先使用 VFS,VFS 不可用时 fallback 到真实文件系统。

file_exists(path)

检查文件是否存在(VFS 优先,找不到则查真实文件系统)。

python
if vfs.file_exists("data/save.json"):
    print("文件可用")
参数类型说明
pathstr文件路径(虚拟或真实)
返回bool文件是否存在

read_file_bytes(path)

读取文件为字节(VFS 优先)。

python
raw = vfs.read_file_bytes("textures/icon.png")
参数类型说明
pathstr文件路径
返回bytes文件原始字节

read_file_text(path)

读取文件为 UTF-8 文本(VFS 优先)。

python
code = vfs.read_file_text("Scripts/main.py")
参数类型说明
pathstr文件路径
返回strUTF-8 文本内容

list_files(directory)

列出目录中的所有文件(VFS 优先)。

python
all_files = vfs.list_files("data/")
参数类型说明
directorystr目录路径
返回list[str]完整路径列表