How do I include “content” files in subdirectories in Windows 8.1 Phone?

时间秒杀一切 提交于 2019-12-06 03:07:29

You'll need to copy the files early enough that they get included in the Appx package. Your solution of copying to the Appx directory post-build on the desktop is limited to the build machine which runs the app directly out of its own folder rather than doing a full deployment. Since the files aren't in the deployment package they won't be available on other systems. This is more obvious on the phone since you'll always need to deploy the app before running it, but it is ultimately the same thing.

The manual way is to include the files in the Visual Studio project. You can copy the files to the project directory, show all files, and then select and include the new files. If they aren't automatically added as Content (most image types like the png in your example will be) then you'll need to set that explicitly so that they are included in the appxpackage when built.

This is pretty much the scenario you said you didn't want.

Instead you can automate adding the items to the project file. The vcxproj file is an XML file (scheme here) and you can write a script to add your asset files to ItemGroups in the project and flag them as DeploymentContent:

<ItemGroup>
    <Image Include="images\items\block.png" />
    <None Include="Assets\frotz.foo">
        <DeploymentContent>true</DeploymentContent>
    </None>
    <None Include="Assets\wibble.foo">
        <DeploymentContent>true</DeploymentContent>
    </None>
</ItemGroup>

Lastly, you can create the appxpackage yourself with makeappx.exe rather than letting Visual Studio do it for you. This will let you have full control over the files in the package, including the ability to pull them in from outside of the VS project. See App packages and deployment and the App package (MakeAppx.exe) documentation.

If people are interested in the Batch file I use to unpack / pack the AppX, here it is:

@echo off

@REM ==================================
@REM User variables
@REM ==================================
set GAME_DIR=C:\development\MyAwesomeGame
set GAME_VERSION=1.0.0.0
set PVK_PUBLISHER=SECRETSECRETSECRETSECRETSECRET
@REM Uncomment for beta builds:
@REM set APPX_BUILD=_Beta
@REM Uncomment for debug builds:
@REM set APPX_BUILD=_Debug

set RESOURCETOOL_FLAVOR=compile_for_winrt
set GAME_NAME=Game.WindowsPhone
set GAME_TOOLS_DIR=%GAME_DIR%\tools
set GAME_DATA_DIR=%GAME_DIR%\data
set PFX_FILE=%GAME_DIR%\proj.win8.1\%GAME_NAME%_StoreKey.pfx
set RESOURCETOOL_OUTPUT=%GAME_DIR%\processed_game_data\win8.1
set MY_START_DIR=%cd%

@REM ==================================
@REM Tools
@REM ==================================
set WIN_SDK_BIN_DIR=C:\Program Files (x86)\Windows Kits\8.0\bin\x64
if not exist "%WIN_SDK_BIN_DIR%" set WIN_SDK_BIN_DIR=C:\Program Files\Windows Kits\8.0\bin\x86
set WINRAR_EXE=%GAME_TOOLS_DIR%\WinRAR.exe
set MAKECERT_EXE=%WIN_SDK_BIN_DIR%\makecert.exe
set MAKEAPPX_EXE=%WIN_SDK_BIN_DIR%\makeappx.exe
set SIGNTOOL_EXE=%WIN_SDK_BIN_DIR%\signtool.exe
set POWERSHELL_EXE=%SYSTEMROOT%\system32\WindowsPowerShell\v1.0\powershell.exe
set PVK_FILE=None
set CER_FILE=None

@REM ==================================
@REM Prerequisites check
@REM ==================================
if not exist "%MAKECERT_EXE%" goto err_no_MAKECERT_EXE
if not exist "%MAKEAPPX_EXE%" goto err_no_MAKEAPPX_EXE
if not exist "%SIGNTOOL_EXE%" goto err_no_SIGNTOOL_EXE
if not exist "%WINRAR_EXE%" goto err_no_WINRAR_EXE
if not exist "%POWERSHELL_EXE%" goto err_no_POWERSHELL_EXE

@REM ==================================
@REM Build options
@REM ==================================
set do_verbose=n
set do_all=y
set build_arm_platform=y
set build_win32_platform=y
set do_delete_tmp_dir=y
set do_copy_resources=y
set do_unpack_source_appx=y
set do_create_appx=y
set do_sign_appx=y
set do_install_appx=y
set do_create_rar=y

set /p do_all=Complete process? (default on) [y/n]: 
if not "%do_all%"=="n" goto skip_do_all
set /p build_arm_platform=Build ARM? (default on) [y/n]: 
set /p build_win32_platform=Build Win32? (default on) [y/n]: 
set /p do_verbose=Verbose output? (default off) [y/n]: 
set /p do_delete_tmp_dir=Delete TMP dir? (default on [y/n]): 
set /p do_copy_resources=Copy resources? (default on [y/n]): 
set /p do_unpack_source_appx=Unpack AppX? (default on) [y/n]: 
set /p do_create_appx=Create AppX? (default on) [y/n]: 
set /p do_sign_appx=Sign AppX? (default on) [y/n]: 
set /p do_install_appx=Install AppX? (default off) [y/n]: 
set /p do_create_rar=Create Rar? (default on) [y/n]: 
:skip_do_all

@REM ==================================
@REM Build verbosity settings
@REM ==================================
set XCOPY_VERBOSE=/q
if not "%do_verbose%"=="y" goto :skip_do_verbose
@echo on
set MAKEAPPX_VERBOSE=/v
set XCOPY_VERBOSE=
:skip_do_verbose

@REM ==================================
@REM Select current platform
@REM ==================================

if "%build_arm_platform%"=="n" goto end_arm_build
set WIN8_PLATFORM=ARM
goto run_appx_build
:end_arm_build

if "%build_win32_platform%"=="n" goto end_win32_build
set WIN8_PLATFORM=Win32
goto run_appx_build
:end_win32_build

cd "%MY_START_DIR%"

@echo ######## Done. ########
goto end

@REM ==================================
@REM Start platform dependent build here
@REM ==================================
:run_appx_build

@REM ==================================
@REM Platform AppX variables
@REM ==================================
set APPX_NAME=%GAME_NAME%_%GAME_VERSION%_%WIN8_PLATFORM%%APPX_BUILD%
set APPX_DIR=%GAME_DIR%\AppPackages\%GAME_NAME%\%APPX_NAME%_Test
set APPX_SOURCE_PATH=%APPX_DIR%\%APPX_NAME%.appx
set APPX_TEMP_DIR=%APPX_DIR%\_TMP_TMP_TMP_

@REM ==================================
@REM Remove temp dir
@REM ==================================
if "%do_unpack_source_appx%"=="n" if "%do_copy_resources%"=="n" if "%do_create_appx%"=="n" goto skip_temp_dir
if "%do_delete_tmp_dir%"=="n" goto skip_delete_temp_dir
@echo ######## Removing temporary directory ########
if exist "%APPX_TEMP_DIR%" call rmdir /S /Q "%APPX_TEMP_DIR%"
if exist "%APPX_TEMP_DIR%" goto err_APPX_TEMP_EXISTS
:skip_delete_temp_dir
mkdir "%APPX_TEMP_DIR%"
cd "%APPX_TEMP_DIR%"
:skip_temp_dir

@REM ==================================
@REM Unpack source AppX
@REM ==================================
if "%do_unpack_source_appx%"=="n" goto skip_unpack_source_appx
if not exist "%APPX_SOURCE_PATH%" goto err_no_APPX_SOURCE_PATH
@echo ######## Unpacking source AppX ########
"%WINRAR_EXE%" x "%APPX_SOURCE_PATH%"
:skip_unpack_source_appx

@REM ==================================
@REM Copy game resources
@REM ==================================
if "%do_copy_resources%"=="n" goto skip_copy_resources
if not exist "%RESOURCETOOL_OUTPUT%" goto err_no_RESOURCETOOL_OUTPUT
echo ######## Copying game's resources ########
xcopy /C /Y /I /R /S %XCOPY_VERBOSE% "%RESOURCETOOL_OUTPUT%\*" "%APPX_TEMP_DIR%\*"
xcopy /C /Y /I /R /S %XCOPY_VERBOSE% "%GAME_DATA_DIR%\shaders\*.cso" "%APPX_TEMP_DIR%\shaders"
:skip_copy_resources

@REM ==================================
@REM Create AppX
@REM ==================================
if "%do_create_appx%"=="n" goto skip_create_appx
@echo ######## Packaging new AppX ########
if exist "%APPX_SOURCE_PATH%" del "%APPX_SOURCE_PATH%"
if exist "%APPX_SOURCE_PATH%" goto err_cant_delete_APPX_SOURCE_PATH
call "%MAKEAPPX_EXE%" pack /l /o %MAKEAPPX_VERBOSE% /d "%APPX_TEMP_DIR%" /p "%APPX_SOURCE_PATH%" > NUL
:skip_create_appx

@REM ==================================
@REM Check if AppX creation was succesfull
@REM ==================================
if "%do_create_appx%"=="n" if "%do_sign_appx%"=="n" if not "%do_install_appx%"=="y" goto skip_check_appx_created
if not exist "%APPX_SOURCE_PATH%" goto err_no_APPX_SOURCE_PATH
:skip_check_appx_created

@REM ==================================
@REM Sign AppX
@REM ==================================
if "%do_sign_appx%"=="n" goto skip_sign_appx
@echo ######## Signing AppX file ########
if not exist "%PFX_FILE%" goto err_no_PFX_FILE
"%SIGNTOOL_EXE%" sign /a /v /fd sha256 /f "%PFX_FILE%" "%APPX_SOURCE_PATH%"
:skip_sign_appx

@REM ==================================
@REM Check for install script
@REM ==================================
if not "%do_install_appx%"=="y" if "%do_create_rar%"=="n" goto skip_install_script_check
set APPX_INSTALLSCRIPT=%APPX_DIR%\Add-AppDevPackage.ps1
set APPX_CERTIFICATE=%APPX_DIR%\%APPX_NAME%.cer
if not exist "%APPX_INSTALLSCRIPT%" goto err_no_APPX_INSTALLSCRIPT
if not exist "%APPX_CERTIFICATE%" goto err_no_APPX_CERTIFICATE
:skip_install_script_check

@REM ==================================
@REM Install AppX
@REM ==================================
if not "%WIN8_PLATFORM%"=="Win32" goto skip_install_appx
if not "%do_install_appx%"=="y" goto skip_install_appx
@echo ######## Installing AppX file ########
"%POWERSHELL_EXE%" "%APPX_INSTALLSCRIPT%"
:skip_install_appx

@REM ==================================
@REM Create rar to distribute
@REM ==================================
if "%do_create_rar%"=="n" goto skip_create_rar
cd %APPX_DIR%
call "%WINRAR_EXE%" a "%GAME_DIR%\%APPX_NAME%.rar" "Add-AppDevPackage.resources" "Dependencies" "Add-AppDevPackage.ps1" "%APPX_NAME%.appx" "%APPX_NAME%.appxsym" "%APPX_NAME%.cer"
:skip_create_rar

@REM ==================================
@REM Loop other platforms
@REM ==================================
if "%WIN8_PLATFORM%"=="ARM" goto end_arm_build
if "%WIN8_PLATFORM%"=="Win32" goto end_win32_build

@REM =================================
@REM Error messages
@REM ==================================

:err_APPX_TEMP_EXISTS
@echo The temporary directory already exists and cannot be removed:
@echo     %APPX_TEMP_DIR%
goto end

:err_no_MAKECERT_EXE
@echo Missing %MAKECERT_EXE%
goto end

:err_no_MAKEAPPX_EXE
@echo Missing %MAKEAPPX_EXE%
goto end

:err_no_SIGNTOOL_EXE
@echo Missing %SIGNTOOL_EXE%
goto end

:err_no_WINRAR_EXE
@echo Missing %WINRAR_EXE%
goto end

:err_no_POWERSHELL_EXE
@echo Missing %POWERSHELL_EXE%
goto end

:err_no_RESOURCETOOL_OUTPUT
@echo Missing %RESOURCETOOL_OUTPUT%
goto end

:err_no_APPX_SOURCE_PATH
@echo Missing %APPX_SOURCE_PATH%
goto end

:err_no_PFX_FILE
@echo Missing %PFX_FILE%
goto end

:err_no_APPX_INSTALLSCRIPT
@echo Missing %APPX_INSTALLSCRIPT%
goto end

err_no_APPX_CERTIFICATE
@echo Missing %APPX_CERTIFICATE%
goto end

:err_cant_delete_APPX_SOURCE_PATH
@echo Can't delete %APPX_SOURCE_PATH%
goto end

:end
pause

The way to solve this is like this:

  • Write a tool that renames your entire directory structure like this:

    script/game/level.lua becomes script#game#level.lua

  • Then add all the files (that are now in the root directory) to your project and set their property Content=Yes

  • Change your paths in your game, replacing '/' with '#'. I hope you already relayed all fopen calls to a platform specific implementation (like we do).

You could write a script that adds new files, but for now it's easy enough to do it each time after an SVN update that adds new files.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!