使用資產套件與套件摺疊進行開發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 可以自動從安裝的語言中選取最適當的檔案,您可以在 ApplicationModel 找到更多有關 Windows的 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-frThen 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.