Currently, game data and image files are included in the Engine project. In this lesson, we’ll move them out.
They aren’t code, so it makes sense to get them out of a class library project. This will also help if we add more files. This technique always copies every file in the folders – giving us one less thing for us to remember (or forget).
We’re going to do this with a “post-build event”
Step 1: Create GameFiles folder
Create this new folder under your solution’s top-level directory
Step 2: Move files from Engine directory to GameFiles directory
Cut-and-paste GameData folder
Cut-and-paste Image folder
Step 3: Create post-build event to copy files
In the WPFUI project, right-click and select “Properties”.
On the properties screen, select the “Build Events” tab.
Copy this line into the “Post-build event command line box”. This command will run after each time we build the project/solution, copying all the files and folders in the GameFiles folder to the project’s output folder – the location where the executable version of the program is located.
xcopy “$(SolutionDir)GameFiles\*.*” “$(ProjectDir)$(OutDir)” /s /y
Step 4: Test the game
Rebuild the solution and make sure the games still runs correctly.
NEXT LESSON: Lesson 19.4: Replace BaseNotificationClass with Fody PropertyChanged Notification
PREVIOUS LESSON: Lesson 19.2: Identifying refactoring targets
So — MSDos is not dead!
MS-DOS is always there, waiting for us. π
Hi Scott, this change has broken my unit tests – not sure if it did for you.
It appears to be because we have constants in our Factories using relative paths to our game data files.
Since our executing directory for the unit tests is not the WPFUI bin location, the tests fail.
I wasn’t sure of the best course of action to fix this. Simply changing directory in the unit tests could do it but seems hacky.
Hi Julian,
I think I deleted the unit test project when we added the dynamic player attributes. You could try adding an XCOPY for the unit test project, but I plan to completely redo the unit tests some time after finishing the refactoring. With the new solution structure, and removal of the circular project dependencies, it should be much easier to create unit tests.
If you do the XCOPY, please post a comment here, for anyone else who may want to run the original unit tests.
I can confirm using the same xcopy in the post build of the unit tests project made them work.
Thanks
Thank you for sharing that, Julian!
Of course including the post build xcopy in the unit test project would also probably work.
(Visual Studio 2019) Is there a way to get these files to still appear in the Solution Explorer? “Add existing Item” doesn’t mimic the folder structure, and maintaining that by manually recreating the folders and adding the objects every time one is added seems tedious.
I don’t know a way to include those files in Solution Explorer.
I’m not sure what requires manually recreating the folders and adding the objects” refers to. Are you talking about after each build? If so, adding the “xcopy” command to the post-build event (in step 3) should copy the game data and image files every time you build. Is that not happening, or are you referring to something else?
Hi!
For those who get error code 4 while copying, it is due to spaces in the folder and/or filenames.
To solve this you will need to close the destination and target with quotes, like this:
xcopy “$(SolutionDir)GameFiles\*.*” “$(ProjectDir)$(OutDir)” /s /y
Hi Johan,
Thanks for sharing that. I updated the lesson to use that format.
Hello, I left a comment earlier today about not having the post-build command running correctly, and I think I found the issue just now, having to do with the different “types” of quotation marks
When I copied/pasted your xcopy code, it copied with the character β (more curly quotations) which VS was not parsing correctly. Replacing all of them with ” (straight quotations) made the post-build run correctly. You don’t have to respond to this, but maybe check the formatting on this page so others don’t have the same issue.
Thanks for everything you do! I’ve been really enjoying going through these lessons.
Thanks for letting me know. The source code formatting tool for this site broke a while back and I had to add a replace it. I thought I caught all the “smart quotes” and replaced them, but it looks like there are a few more. I’ll see if I can correct this lesson tomorrow.
Hi!
For those who get error couldn’t copy files from Engine/Images because its not exists:
Step1: Copy the Images and GameData folders, paste to your solutionβs top-level “GameFiles” directory in your windows explorer.
Step2: Delete the Engine/Images and Engine/GameData from your solution inside VisualStudio.
Step3: Post-build event command: xcopy β$(SolutionDir)GameFiles\*.*β β$(ProjectDir)$(OutDir)β /s /y
It should works properly now π
In my Step2 I just deleted the Engine/Images and Engine/GameData folders in my windows explorer instead of Visual Studio’s Solution Explorer and When I wanted to build my solution I got some errors.
Thanks for sharing that tip
Hi Scott
I got the exited code 4 even with the new format, can you checkout my code please : https://github.com/Balsero/Super-Adventure
Thank you so much !
Hi Jonathan,
It looks like the problem was that my page formatted the xcopy command with “smart quotes”, and the build had a problem with that. I’ve changed the quotes to standard quotes in the lesson, so you can re-copy it or just go into the project properties and replace them there.
Please let me know if that doesn’t fix the error.
Still no working I dont know a I take the right one xcopy.
I pushed the change to your GitHub repository. Can you try pulling it down and see if that works?
It works ! Thank you so much !
Glad there is a way to do this because I would definitely forget to set those settings on new files. I kept getting error codes during the build, and for some reason after adding the quotes it was still erroring, but this worked for me:
robocopy “$(ProjectDir)GameData” *.* “$(ProjectDir)$(OutDir)GameData” /e /b
robocopy “$(ProjectDir)Images” *.* “$(ProjectDir)$(OutDir)Images” /e /b
exit 0
One odd thing was that both xcopy and robocopy always included the WPFUI folder in (ProjectDir) when they ran. I couldn’t find an equivalent to the (ProjectDir) macro that doesn’t include that folder and wound up moving the GameData and Images folders into the WPFUI folder. The “exit 0” line was only needed for robocopy because otherwise it returns 1 when it is successful, which Visual Studio mistakes for an error message.
From what I’ve seen, the double quotes are probably the problem. Either WordPress (what this site is running) or something in the copy-paste converts the quotes to “smart quotes”, which (I believe) get handled differently.
But, doing things like this to automate builds and deployments is pretty useful to set up for your apps. This is especially true when building more professional software and doing things like deploying to test and production environments. I haven’t gone deep into doing this before, but it’s something I’m working on more.