Added a simple live asset rebuild system.

master
Andrew Russell 2018-02-23 17:02:00 +10:00
parent b002a69ed3
commit 693f827d64
5 changed files with 132 additions and 1 deletions

3
.gitignore vendored
View File

@ -288,6 +288,9 @@ __pycache__/
*.xsd.cs
# MacOS Nonsense
.DS_Store
#
# Ignores for FNA-Template

View File

@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
namespace FNATemplate
{
#if DEBUG
static class AssetRebuild
{
public static bool Run()
{
try
{
// This file is generated at build time (see ContentRebuilder.targets)
string infoPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AssetRebuildInfo.txt");
if(!File.Exists(infoPath))
{
Debug.WriteLine("AssetRebuild: Could not find file " + infoPath);
return false;
}
string[] lines = File.ReadAllLines(infoPath);
if(lines.Length < 2)
{
Debug.WriteLine("AssetRebuild: Missing data in " + infoPath);
return false;
}
string msBuildDirectory = lines[0];
if(!Directory.Exists(msBuildDirectory))
{
Debug.WriteLine("AssetRebuild: Could not directory " + msBuildDirectory);
return false;
}
string projectPath = lines[1];
if(!File.Exists(projectPath))
{
Debug.WriteLine("AssetRebuild: Could not find " + projectPath);
return false;
}
string msBuildPath;
if(Environment.OSVersion.Platform == PlatformID.Win32NT)
{
msBuildPath = Path.Combine(msBuildDirectory, "MSBuild");
}
else
{
// The msbuild command isn't where we expect it on Xamarin, so just rely on PATH.
msBuildPath = "msbuild";
}
Process process = new Process();
process.StartInfo.FileName = msBuildPath;
process.StartInfo.Arguments = @"/t:BuildContentOnly";
process.StartInfo.WorkingDirectory = Path.GetDirectoryName(projectPath); // <- MSBuild will automatically find the csproj file.
process.Start();
process.WaitForExit();
Debug.WriteLine("AssetRebuild: Completed");
return true;
}
catch(Exception e)
{
Debug.WriteLine("AssetRebuild: Failed with: " + e.ToString());
return false;
}
}
}
#endif
}

View File

@ -62,6 +62,7 @@
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Compile Include="AssetRebuild.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="FNATemplateGame.cs" />
@ -86,6 +87,7 @@
<Import Project="$(MSBuildExtensionsPath)\MonoGame\v3.0\MonoGame.Content.Builder.targets" />
<Import Project="..\build\BuildShaders.targets" />
<Import Project="..\build\CopyFNALibs.targets" />
<Import Project="..\build\ContentRebuilder.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">

View File

@ -5,6 +5,7 @@ using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace FNATemplate
{
@ -46,18 +47,51 @@ namespace FNATemplate
base.LoadContent();
}
protected override void UnloadContent()
{
Content.Unload();
spriteBatch.Dispose();
exampleEffect.Dispose();
base.UnloadContent();
}
KeyboardState lastKeyboardState;
protected override void Update(GameTime gameTime)
{
// Insert your game update logic here.
KeyboardState keyboardState = Keyboard.GetState();
//
// Asset Rebuilding:
#if DEBUG
const Keys assetRebuildKey = Keys.F5;
if(keyboardState.IsKeyDown(assetRebuildKey) && lastKeyboardState.IsKeyUp(assetRebuildKey))
{
if(AssetRebuild.Run())
{
UnloadContent();
LoadContent();
}
}
#endif
//
// Insert your game update logic here.
//
lastKeyboardState = keyboardState;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
//
// Replace this with your own drawing code.
//
GraphicsDevice.Clear(Color.CornflowerBlue);

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Allow fast rebuild of MonoGame content. Expects MonoGame.Content.Builder.targets to have been included. -->
<Target Name="BuildContentOnly" DependsOnTargets="BuildContent;BuildShaders;_CopySourceItemsToOutputDirectory" />
<!-- So what we call at runtime matches what we're building now... -->
<Target Name="WriteAssetRebuildInfo" BeforeTargets="Compile"
Condition="'$(Configuration)' == 'Debug'">
<WriteLinesToFile
File="$(OutputPath)AssetRebuildInfo.txt"
Lines="$(MSBuildToolsPath);$(MSBuildProjectFullPath)"
Overwrite="true"
Encoding="Unicode"/>
</Target>
</Project>