用资产包和包折叠进行开发Developing with asset packages and package folding

重要

如果你打算将应用提交到 Microsoft Store,你需要联系 Windows 开发人员支持并获得批准才能使用资产包和包折叠。If you intend to submit your app to the Store, you need to contact Windows developer support and get approval to use asset packages and package folding.

资产包可以减小总体包大小,并缩短将应用发布到 Microsoft Store 的时长。Asset packages can decrease the overall packaging size and publishing time for your apps to the Store. 有关资产包及其如何加快开发迭代速度的详细信息,请参阅资产包简介You can learn more about asset packages and how it can speed up your development iterations at Introduction to asset packages.

如果你正在考虑为应用使用资产包,或者已经确定要使用资产包,则你可能想了解资产包会给你的开发流程带来哪些改变。If you are thinking about using asset packages for your app or already know that you want to use it, then you are probably wondering about how asset packages will change your development process. 简要地说,由于资产包具有包折叠特性,你的应用开发流程将保持不变。In short, app development for you stays the same - this is possible because of package folding for asset packages.

拆分应用后的文件访问File access after splitting your app

要明白包折叠为什么不会影响你的开发流程,我们首先需要知道当你将应用拆分成多个包(资产包或资源包)时会发生什么情况。To understand how package folding doesn’t impact your development process, let’s step back first to understand what happens when you split your app into multiple packages (with either asset packages or resource packages).

从高层次上看,将应用的部分文件拆分到其他包(非体系结构程序包)后,你将无法直接访问那些相对于你的代码运行位置的文件。At a high level, when you split some of your app’s files into other packages (that are not architecture packages), you will not be able to access those files directly relative to where your code runs. 原因在于这些软件包跟你的体系结构程序包安装到了不同的目录。This is because these packages are all installed into different directories from where your architecture package is installed. 例如,如果您正在玩游戏,并且您的游戏已本地化为法语和德语,并且您是为 x86 和 x64 计算机而构建的,则应在游戏的应用捆绑包中安装这些应用包文件:For example, if you’re making a game and your game is localized into French and German and you built for both x86 and x64 machines, then you should have these app package files within the app bundle of your game:

  • MyGame_1.0_x86.appxMyGame_1.0_x86.appx
  • MyGame_1.0_x64.appxMyGame_1.0_x64.appx
  • MyGame_1.0_language-fr.appxMyGame_1.0_language-fr.appx
  • MyGame_1.0_language-de.appxMyGame_1.0_language-de.appx

当你的游戏安装到用户的计算机时,每个应用包文件在 WindowsApps 目录中都有其自己的文件夹。When your game is installed to a user’s machine, each app package file will have its own folder in the WindowsApps directory. 因此,对于运行 64 位 Windows 的法国用户,你的游戏将如下所示:So for a French user running 64-bit Windows, your game will look like this:

C:\Program Files\WindowsApps\
|-- MyGame_1.0_x64
|   `-- …
|-- MyGame_1.0_language-fr
|   `-- …
`-- …(other apps)

请注意,不适用于用户的应用包文件将不会安装 (x86 和德语包) 。Note that the app package files that are not applicable to the user will not be installed (the x86 and German packages).

对于该用户,你的游戏的主可执行文件将位于 MyGame_1.0_x64 文件夹中,它将从该文件夹运行,并且通常只能访问该文件夹中的文件。For this user, your game’s main executable will be within the MyGame_1.0_x64 folder and will run from there, and normally, it will only have access to the files within this folder. 要访问 MyGame_1.0_language-fr 文件夹中的文件,你必须使用 MRT API 或 PackageManager API。In order to access the files in the MyGame_1.0_language-fr folder, you would have to use either the MRT APIs or the PackageManager APIs. MRT.LOG Api 可以从安装的语言中自动选择最适合的文件,你可以在 windows.applicationmodel.resources.core上了解有关 mrt.log api 的详细信息。The MRT APIs can automatically select the most appropriate file from the languages installed, you can find out more about MRT APIs at Windows.ApplicationModel.Resources.Core. 或者,你也可以使用 PackageManager 类找到法语语言包的安装位置。Alternatively, you can find the installed location of the French language package using the PackageManager Class. 你不应该对应用程序包的安装位置做出任何假设,因为安装位置可能会发生变化,并且可能因用户而异。You should never assume the installed location of the packages of your app since this can change and can vary between users.

资产包折叠Asset package folding

如何访问资产包中的文件?So how can you access the files in your asset packages? 你可以继续使用你正在用来访问体系结构程序包中任何其他文件的文件访问 API。Well, you can continue to use the file access APIs you are using to access any other file in your architecture package. 这是因为资产包文件在通过包折叠过程安装时会折叠到你的体系结构程序包中。This is because asset package files will be folded into your architecture package when it is installed through the package folding process. 此外,由于资产包文件本来就应该是体系结构程序包中的文件,这意味着你在开发过程中从松散文件部署转换为打包部署时,不必更改所用的 API。Furthermore, since asset package files should originally be files within your architecture packages, this means that you would not have to change API usage when you move from loose files deployment to packaged deployment in your development process.

为了更深入地了解包折叠的工作原理,我们来看一个示例。To understand more about how package folding works, let’s start with an example. 假设你有一个游戏项目,其文件结构如下:If you have a game project with the following file structure:

MyGame
|-- Audios
|   |-- Level1
|   |   `-- ...
|   `-- Level2
|       `-- ...
|-- Videos
|   |-- Level1
|   |   `-- ...
|   `-- Level2
|       `-- ...
|-- Engine
|   `-- ...
|-- XboxLive
|   `-- ...
`-- Game.exe

如果你打算将游戏拆分成 3 个包:一个 x64 体系结构程序包、一个音频资产包和一个视频资产包。你的游戏将分成以下几个包:If you want to split your game into 3 packages: an x64 architecture package, an asset package for audios, and an asset package for videos, your game will be divided into these packages:

MyGame_1.0_x64.appx
|-- Engine
|   `-- ...
|-- XboxLive
|   `-- ...
`-- Game.exe
MyGame_1.0_Audios.appx
`-- Audios
    |-- Level1
    |   `-- ...
    `-- Level2
        `-- ...
MyGame_1.0_Videos.appx
`-- Videos
    |-- Level1
    |   `-- ...
    `-- Level2
        `-- ...

安装游戏时,将首先部署 x64 程序包。When you install your game, the x64 package will be deployed first. 然后,两个资产包仍会被部署到它们自己的文件夹,就像前面的示例 MyGame_1.0_language-fr 那样。Then the two asset packages will still be deployed to their own folders, just like MyGame_1.0_language-fr from our previous example. 但是,由于包折叠,资产包文件还将硬链接以显示在 MyGame_1.0_x64 文件夹中。虽然这些文件在两个位置显示,但它们不会占用磁盘空间两次。However, because of package folding, the asset package files will also be hard linked to appear in the MyGame_1.0_x64 folder (so even though the files appear in two locations, they do not take up twice the disk space). 资产包文件显示的位置恰好是它们相对于程序包根目录的位置。The location in which the asset package files will appear in is exactly the location that they are at relative to the root of the package. 因此,部署后游戏的最终布局为:So here’s what the final layout of the deployed game will look like:

C:\Program Files\WindowsApps\
|-- MyGame_1.0_x64
|   |-- Audios
|   |   |-- Level1
|   |   |   `-- ...
|   |   `-- Level2
|   |       `-- ...
|   |-- Videos
|   |   |-- Level1
|   |   |   `-- ...
|   |   `-- Level2
|   |       `-- ...
|   |-- Engine
|   |   `-- ...
|   |-- XboxLive
|   |   `-- ...
|   `-- Game.exe
|-- MyGame_1.0_Audios
|   `-- Audios
|       |-- Level1
|       |   `-- ...
|       `-- Level2
|           `-- ...
|-- MyGame_1.0_Videos
|   `-- Videos
|       |-- Level1
|       |   `-- ...
|       `-- Level2
|           `-- ...
`-- …(other apps)

为资产包使用包折叠时,你仍然可以按以前的方式访问已拆分到资产包中的文件(注意,体系结构文件夹具有与原始项目文件夹完全相同的结构),你可以添加资产包或在资产包之间移动文件而不对代码造成任何影响。When using package folding for asset packages, you can still access the files you’ve split into asset packages the same way (notice that the architecture folder has the exact same structure as the original project folder), and you can add asset packages or move files between asset packages without impacting your code.

下面,我们来看一个更复杂的包折叠示例。Now for a more complicated package folding example. 假设你想要根据级别拆分文件,并且想要保留与原始项目文件夹相同的结构,则你的程序包应该如下所示:Let’s say that you want to split your files based on level instead, and if you want to keep the same structure as the original project folder, your packages should look like this:

MyGame_1.0_x64.appx
|-- Engine
|   `-- ...
|-- XboxLive
|   `-- ...
`-- Game.exe
MyGame_Level1.appx
|-- Audios
|   `-- Level1
|       `-- ...
`-- Videos
    `-- Level1
        `-- ...

MyGame_Level2.appx
|-- Audios
|   `-- Level2
|       `-- ...
`-- Videos
    `-- Level2
        `-- ...

这将允许 MyGame_Level1 包中的 Level1 文件夹和文件与 MyGame_Level2 包中的 Level2 文件夹和文件在包折叠期间合并到 AudiosVideos 文件夹。This will allow the Level1 folders and files in the MyGame_Level1 package and Level2 folders and files in the MyGame_Level2 package to be merged into the Audios and Videos folders during package folding. 由此可见,作为一般规则,在映射文件中为打包文件指定的相对路径或 MakeAppx.exe 的打包布局是你应在包折叠后用来访问它们的路径。So as a general rule, the relative path designated for packaged files in the mapping file or packaging layout for MakeAppx.exe is the path you should use to access them after package folding.

最后,如果不同资产包中的两个文件具有相同的相对路径,将在包折叠期间导致冲突。Lastly, if there are two files in different asset packages that have the same relative paths, this will cause a collision during package folding. 一旦发生冲突,应用部署将产生错误并失败。If a collision occurs, the deployment of your app will result in an error and fail. 此外,由于包折叠使用了硬链接,因此如果使用了资产包,你的应用将无法部署到非 NTFS 驱动器。Also, because package folding takes advantage of hard links, if you do use asset packages, your app will not be able to be deployed to non-NTFS drives. 如果你知道用户可能会将你的应用移动到可移动驱动器,则不应该使用资产包。If you know your app will likely be moved to removable drives by your users, then you should not use asset packages.