Knight99

I have decided to work on a editor. For this, I have been researching on how to invoke MSBuild at runtime programmatically. I read that we can use the MSBuild API. I was wondering if anyone has managed to do this Any help is appreciated.

A method I have so far is to create a .proj file each time according to the asset I am trying to build, and then call the MSBuild as an external process like this:

System.Diagnostics.Process.Start("C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\MSBuild.exe test.proj");

I believe there is a better way though. This doesn't even work for me anyway.


Re: XNA Framework Invoking MSBuild programmatically at runtime (editor model building and loading)

Leaf.

The MSBuild Api is part of the .Net Framework, check out theses namespaces:

Microsoft.Build.Framework

Microsoft.Build.BuildEngine

The Engine class has a simple example of building a project file but unlike the sample you don't have to hard code the path to the .Net runtime, you can use

System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()

Here's an IronPython console sessions that shows how easy it can be (I use IronPython to check out .Net libs):

>>> import System
>>> import clr
>>> clr.AddReference("Microsoft.Build.Engine")
>>> from Microsoft.Build.BuildEngine import *
>>> e = Engine()
>>> e.BinPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()
>>> log = ConsoleLogger()
>>> e.RegisterLogger(log)
>>> e.BuildProjectFile("buildmulti.proj")
Build started 20/12/2006 07:14:13.
__________________________________________________
Project "X:\msbuild\buildmulti.proj" (default targets):

Target XNAContentBuild:
bin\Debug\cloudyhills.xnb is already up to date

Build succeeded.
0 Warning(s)
0 Error(s)

Time Elapsed 00:00:01.01
True
>>>

I would also suggest that you read the MSBuild docs and learn how build projects work - you could move a lot of logic into the project file itself to avoid you having to generate project files for every asset. For example if you decide on a directory layout for your level assets then you could have a single build project that builds any file in your textures directory using a texture processor and any file in your models directory using a model processor and so on.

Once you have a system set up you have some very powerful options open to you; you could build from the level editor or provide shortcuts that just run the build without starting the level editor (for users that are just editing assets) or levels can be built from the command line on a network server, or check for changes to a source control system so that as soon as an artist checks in a change the level data is rebuilt and can then be shared between users. There are many options.

Cheers,
Leaf.





Re: XNA Framework Invoking MSBuild programmatically at runtime (editor model building and loading)

Knight99

Thanks Leaf, thats a good start for me.

Ok, so I am getting an error with the build with the following .proj file (taken from Shawn's blog)

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<XnaInstall>C:\Program Files\Microsoft XNA\XNA Game Studio Express\v1.0\References\Windows\x86</XnaInstall>
</PropertyGroup>
<ItemGroup>
<PipelineAssembly Include="$(XnaInstall)\Microsoft.Xna.Framework.Content.Pipeline.TextureImporter.dll" />
</ItemGroup>
<ItemGroup>
<Content Include="MyTexture.png">
<Importer>TextureImporter</Importer>
<Processor>SpriteTextureProcessor</Processor>
</Content>
</ItemGroup>
<UsingTask TaskName="BuildContent" AssemblyName="Microsoft.Xna.Framework.Content.Pipeline, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f48fa5f25d27e8ff" />
<Target Name="Build">
<BuildContent SourceAssets="@(Content)" PipelineAssemblies="@(PipelineAssembly)" TargetPlatform="Windows" />
</Target>
</Project>

The error I get is:
Target Build:
MSBUILD : error MSB4062: The "BuildContent" task could not be loaded from the assembly Microsoft.Xna.Framework.Content.Pipeline, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f48fa5f25d27e8ff. Could not load file or assembly 'Microsoft.Xna.Framework.Content.Pipeline, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f48fa5f25d27e8ff' or one of its dependencies. The system cannot find the file specified. Confirm that the <UsingTask> declaration is correct, and that the assembly and all its dependencies are available.
Done building target "Build" in project "" -- FAILED.

I been searching and trying to solve this for a while now. Any help is appreciated.




Re: XNA Framework Invoking MSBuild programmatically at runtime (editor model building and loading)

Leaf.

The problem is exactly as the error message says, there is no matching assembly. This is probably because Shawn wrote that for beta2 and there has been a release since then so the PublicKeyToken has changed.

The fully qualified assembly name for that assembly in v1.0 is "Microsoft.Xna.Framework.Content.Pipeline, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6d5c3888ef60e27d".

Cheers,
Leaf.







Re: XNA Framework Invoking MSBuild programmatically at runtime (editor model building and loading)

Knight99

Damn it, I knew that was the problem but I wasn't able to find out where to get the updated public key. Sigh. Anyways, thanks a lot for the help.