Skip to content
Permalink
Browse files

[Assets] Some improvements so that unit tests can compile

  • Loading branch information
xen2 committed Nov 8, 2018
1 parent 5015242 commit 4944a4e454169feb424a1413dc21c496940de720
@@ -168,20 +168,15 @@

<Target Name="XenkoRunAssemblyProcessor" DependsOnTargets="ResolveProjectReferences">
<WriteLinesToFile File="$(IntermediateOutputPath)XenkoReferences.cache" Lines="@(ReferencePath)" Overwrite="true" />
<ItemGroup>
<XenkoAddReference Include="@(ReferencePath)" Condition="'%(ReferencePath.ExternallyResolved)' == 'true' Or '%(ReferencePath.BuildReference)' == 'true'" />
</ItemGroup>
<PropertyGroup>
<XenkoAssemblyProcessorOptions Condition="'$(XenkoAssemblyProcessorOptions)' == ''">--auto-notify-property --parameter-key --auto-module-initializer --serialization</XenkoAssemblyProcessorOptions>
<!-- If building user solutions (not unit tests), provide assembly processor with ProjectReferences paths so that they can be readded to assembly references for serialization module initializer (otherwise .exe don't have real reference on Game assemblies with auto load scene game) -->
<XenkoAssemblyProcessorOptions Condition="'$(XenkoUnitTest)' != 'true'">$(XenkoAssemblyProcessorOptions) @(_ResolvedProjectReferencePaths->'--add-reference=%22%(Identity)%22',' ')</XenkoAssemblyProcessorOptions>
<XenkoAssemblyProcessorOptions Condition="'$(DocumentationFile)' != ''">$(XenkoAssemblyProcessorOptions) --docfile="$(DocumentationFile)"</XenkoAssemblyProcessorOptions>
<!-- Physics, Particles and SpriteStudio assembly is actually hard coded/referenced here, remove once a better system is in place! -->
<!-- Please keep in sync with AssemblyRecompiler.cs -->
<XenkoAssemblyProcessorOptions>$(XenkoAssemblyProcessorOptions) --add-reference="$(XenkoPackageXenkoPlatformBin)\Xenko.Physics.dll"</XenkoAssemblyProcessorOptions>
<XenkoAssemblyProcessorOptions>$(XenkoAssemblyProcessorOptions) --add-reference="$(XenkoPackageXenkoPlatformBin)\Xenko.Particles.dll"</XenkoAssemblyProcessorOptions>
<XenkoAssemblyProcessorOptions>$(XenkoAssemblyProcessorOptions) --add-reference="$(XenkoPackageXenkoPlatformBin)\Xenko.SpriteStudio.Runtime.dll"</XenkoAssemblyProcessorOptions>
<XenkoAssemblyProcessorOptions>$(XenkoAssemblyProcessorOptions) --add-reference="$(XenkoPackageXenkoPlatformBin)\Xenko.Native.dll"</XenkoAssemblyProcessorOptions>
<XenkoAssemblyProcessorOptions>$(XenkoAssemblyProcessorOptions) --add-reference="$(XenkoPackageXenkoPlatformBin)\Xenko.UI.dll"</XenkoAssemblyProcessorOptions>
<XenkoAssemblyProcessorOptions>$(XenkoAssemblyProcessorOptions) --add-reference="$(XenkoPackageXenkoPlatformBin)\Xenko.Navigation.dll"</XenkoAssemblyProcessorOptions>
<XenkoAssemblyProcessorOptions>$(XenkoAssemblyProcessorOptions) --add-reference="$(XenkoPackageXenkoPlatformBin)\$(XenkoGraphicsApi)\Xenko.Video.dll"</XenkoAssemblyProcessorOptions>
<XenkoAssemblyProcessorOptions>$(XenkoAssemblyProcessorOptions) @(XenkoAddReference->'--add-reference=%22%(Identity)%22',' ')</XenkoAssemblyProcessorOptions>
<XenkoAssemblyProcessorOptions Condition="'$(XenkoAutoTesting)' == 'true'">$(XenkoAssemblyProcessorOptions) --add-reference="$(XenkoPackageXenkoPlatformBin)\Xenko.Games.Testing.dll"</XenkoAssemblyProcessorOptions>
<XenkoAssemblyProcessorOptions>$(XenkoAssemblyProcessorOptions) --references-file="$(IntermediateOutputPath)XenkoReferences.cache"</XenkoAssemblyProcessorOptions>
<XenkoAssemblyProcessorOptions>$(XenkoAssemblyProcessorOptions) --platform=$(XenkoPlatform) --targetFramework=$(XenkoNETFrameworkVersion) "$(IntermediateOutputPath)$(TargetName)$(TargetExt)"</XenkoAssemblyProcessorOptions>
@@ -278,11 +273,12 @@
<!--asset OutputPath for other platforms. Note: this is overridden for UnitTests projects. -->
<XenkoCompileAssetOutputPath Condition="'$(XenkoCompileAssetOutputPath)' == ''">$(TargetDir)data</XenkoCompileAssetOutputPath>

<XenkoCompileAssetCommand Condition="'$(XenkoCompileAssetCommand)' == ''">$(XenkoPackageXenkoSdkBin)\Xenko.Core.Assets.CompilerApp.exe</XenkoCompileAssetCommand>
<XenkoCompileAssetCommand Condition="'$(XenkoCompileAssetCommand)' == ''">$(MSBuildThisFileDirectory)..\sources\assets\Xenko.Core.Assets.CompilerApp\bin\$(Configuration)\net472\Xenko.Core.Assets.CompilerApp.exe</XenkoCompileAssetCommand>

<!--<XenkoCompileAssetCommandProxy Condition="'$(DisableAssetCompilerExecServerProxy)' == 'true' Or '$(XenkoDisableAssetCompilerExecServerProxy)' == 'true'">&quot;$(XenkoCompileAssetCommand)&quot;</XenkoCompileAssetCommandProxy>
<XenkoCompileAssetCommandProxy Condition="'$(XenkoCompileAssetCommandProxy)' == ''">&quot;$(XenkoPackageXenkoSdkBin)\Xenko.Core.Assets.CompilerClient.exe&quot;</XenkoCompileAssetCommandProxy>-->
<XenkoCompileAssetCommandProxy>&quot;$(XenkoCompileAssetCommand)&quot;</XenkoCompileAssetCommandProxy>

<XenkoCompileAssetCommandProxy Condition="'$(DisableAssetCompilerExecServerProxy)' == 'true' Or '$(XenkoDisableAssetCompilerExecServerProxy)' == 'true'">&quot;$(XenkoCompileAssetCommand)&quot;</XenkoCompileAssetCommandProxy>
<XenkoCompileAssetCommandProxy Condition="'$(XenkoCompileAssetCommandProxy)' == ''">&quot;$(XenkoPackageXenkoSdkBin)\Xenko.Core.Assets.CompilerClient.exe&quot;</XenkoCompileAssetCommandProxy>

<!-- Quite hard-coded for our current way to find default game settings -->
<XenkoGameSettings Condition="'$([System.IO.File]::Exists($(MSBuildProjectDirectory)\Assets\GameSettings.xkgamesettings))'== 'true' ">$(MSBuildProjectDirectory)\Assets\GameSettings.xkgamesettings</XenkoGameSettings>
<XenkoGameSettings Condition="'$(XenkoGameSettings)' == '' and '$([System.IO.File]::Exists($(MSBuildProjectDirectory)\..\Assets\Shared\GameSettings.xkgamesettings))' == 'true'">$(MSBuildProjectDirectory)\..\Assets\Shared\GameSettings.xkgamesettings</XenkoGameSettings>
@@ -24,7 +24,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "00-Targets.Private", "00-Ta
..\sources\targets\Xenko.Core.UWP.Cpp.targets = ..\sources\targets\Xenko.Core.UWP.Cpp.targets
..\sources\targets\Xenko.Core.UWP.CSharp.targets = ..\sources\targets\Xenko.Core.UWP.CSharp.targets
..\sources\targets\Xenko.GlobalSettings.targets = ..\sources\targets\Xenko.GlobalSettings.targets
..\sources\targets\Xenko.InternalReferences.targets = ..\sources\targets\Xenko.InternalReferences.targets
..\sources\native\Xenko.Native.targets = ..\sources\native\Xenko.Native.targets
..\sources\targets\Xenko.PostSettings.References.targets = ..\sources\targets\Xenko.PostSettings.References.targets
..\sources\targets\Xenko.PostSettings.targets = ..\sources\targets\Xenko.PostSettings.targets
@@ -599,7 +599,6 @@ public static PackageContainer LoadProject(ILogger log, string filePath)
ResourceFolders = { "Resources" },
FullPath = packagePath,
};
package.Meta.Version = TryGetPackageVersion(projectPath) ?? new PackageVersion("1.0.0");
return new SolutionProject(package, Guid.NewGuid(), projectPath) { IsImplicitProject = !packageExists };
}
else
@@ -989,6 +988,7 @@ private void LoadAssemblyReferencesForPackage(ILogger log, PackageLoadParameters
if (loadParameters == null) throw new ArgumentNullException(nameof(loadParameters));
var assemblyContainer = loadParameters.AssemblyContainer ?? AssemblyContainer.Default;

// TODO: Add support for loading from packages
var project = Container as SolutionProject;
if (project == null || project.FullPath == null || project.Type != ProjectType.Library)
return;
@@ -1021,19 +1021,28 @@ private void LoadAssemblyReferencesForPackage(ILogger log, PackageLoadParameters
return;
}

var assembly = assemblyContainer.LoadAssemblyFromPath(assemblyPath, log);
// Check if assembly is already loaded in appdomain (for Xenko core assemblies that are not plugins)
var assembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => string.Compare(x.GetName().Name, Path.GetFileNameWithoutExtension(assemblyPath), StringComparison.InvariantCultureIgnoreCase) == 0);

// Otherwise, load assembly from its file
if (assembly == null)
{
log.Error($"Unable to load assembly reference [{assemblyPath}]");
}
assembly = assemblyContainer.LoadAssemblyFromPath(assemblyPath, log);

loadedAssembly.Assembly = assembly;
if (assembly == null)
{
log.Error($"Unable to load assembly reference [{assemblyPath}]");
}

if (assembly != null)
{
// Register assembly in the registry
AssemblyRegistry.Register(assembly, AssemblyCommonCategories.Assets);
// Note: we should investigate so that this can also be done for Xenko core assemblies (right now they use module initializers)
if (assembly != null)
{
// Register assembly in the registry
AssemblyRegistry.Register(assembly, AssemblyCommonCategories.Assets);
}
}

loadedAssembly.Assembly = assembly;
}
catch (Exception ex)
{
@@ -76,7 +76,16 @@ IEnumerator IEnumerable.GetEnumerator()
public Package Find(Dependency dependency)
{
if (dependency == null) throw new ArgumentNullException(nameof(dependency));
return Find(dependency.Name, new PackageVersionRange(dependency.Version));
switch (dependency.Type)
{
case DependencyType.Package:
return Find(dependency.Name, new PackageVersionRange(dependency.Version));
case DependencyType.Project:
// Project versions might not be properly loaded so we check only by name
return packages.FirstOrDefault(package => package.Meta.Name == dependency.Name && package.Container is SolutionProject);
default:
throw new ArgumentException($"Unhandled value: {dependency.Type}");
}
}

/// <summary>
@@ -98,6 +98,19 @@ private async Task PreLoadPackageDependencies(ILogger log, SolutionProject proje
var msProject = VSProjectHelper.LoadProject(project.FullPath, platform: "NoPlatform");
try
{
var packageVersion = msProject.GetPropertyValue("PackageVersion");
if (!string.IsNullOrEmpty(packageVersion))
package.Meta.Version = new PackageVersion(packageVersion);

package.Meta.Name = msProject.GetPropertyValue("PackageId") ?? msProject.GetPropertyValue("AssemblyName") ?? package.Meta.Name;

var projectIsExecutable = msProject.GetPropertyValue("OutputType");
project.Type = projectIsExecutable.ToLowerInvariant() == "winexe" || projectIsExecutable.ToLowerInvariant() == "exe" ? ProjectType.Executable : ProjectType.Library;

// TODO: Platform might be incorrect if Xenko is not restored yet (it won't include Xenko targets)
if (project.Type == ProjectType.Executable)
project.Platform = VSProjectHelper.GetPlatformTypeFromProject(msProject) ?? PlatformType.Shared;

foreach (var packageReference in msProject.GetItems("PackageReference").ToList())
{
if (packageReference.HasMetadata("Version") && PackageVersionRange.TryParse(packageReference.GetMetadataValue("Version"), out var packageRange))
@@ -210,31 +223,8 @@ private async Task PreLoadPackageDependencies(ILogger log, SolutionProject proje

// Now that our references are upgraded, let's do a real nuget restore (download files)
log.Verbose($"Restore NuGet packages for {project.Name}...");
await VSProjectHelper.RestoreNugetPackages(log, project.FullPath);

// Load some informations about the project
try
{
// Load a project without specifying a platform to make sure we get the correct platform type
var msProject = VSProjectHelper.LoadProject(project.FullPath, platform: "NoPlatform");
try
{

var projectIsExecutable = msProject.GetPropertyValue("OutputType");
project.Type = projectIsExecutable.ToLowerInvariant() == "winexe" || projectIsExecutable.ToLowerInvariant() == "exe" ? ProjectType.Executable : ProjectType.Library;
if (project.Type == ProjectType.Executable)
project.Platform = VSProjectHelper.GetPlatformTypeFromProject(msProject) ?? PlatformType.Shared;
}
finally
{
msProject.ProjectCollection.UnloadAllProjects();
msProject.ProjectCollection.Dispose();
}
}
catch (Exception ex)
{
log.Error($"Unexpected exception while loading project [{project.FullPath.ToWindowsPath()}]", ex);
}
if (loadParameters.AutoCompileProjects)
await VSProjectHelper.RestoreNugetPackages(log, project.FullPath);

project.FlattenedDependencies.Clear();
project.DirectDependencies.Clear();
@@ -289,6 +279,7 @@ private async Task PreLoadPackageDependencies(ILogger log, SolutionProject proje
{
// Load package
var loadedProject = LoadProject(log, file, true, loadParameters);
loadedProject.Package.Meta.Name = projectDependency.Name;
loadedProject.Package.Meta.Version = projectDependency.Version;
Projects.Add(loadedProject);

@@ -226,6 +226,8 @@ public StandalonePackage([NotNull] Package package)
: base(package)
{
}

public override string ToString() => $"Package: {package.Meta.Name}";
}

public enum DependencyType
@@ -384,6 +386,8 @@ protected override void SavePackage()
if (!IsImplicitProject)
base.SavePackage();
}

public override string ToString() => $"Project: {Name}";
}

public sealed class ProjectCollection : ObservableCollection<PackageContainer>
@@ -193,12 +193,6 @@ private Assembly LoadAssemblyFromPathInternal([NotNull] string assemblyFullPath)
Assembly.Load(assembly.FullName);
}

// Make sure that all referenced assemblies are loaded here
foreach (var assemblyRef in assembly.GetReferencedAssemblies())
{
Assembly.Load(assemblyRef);
}

// Make sure that Module initializer are called
if (assembly.GetTypes().Length > 0)
{
@@ -46,8 +46,10 @@
</ItemGroup>
<PropertyGroup>
<XenkoAssemblyProcessorOptions Condition="'$(XenkoAssemblyProcessorOptions)' == ''">--auto-notify-property --parameter-key --auto-module-initializer --serialization</XenkoAssemblyProcessorOptions>
<!-- If building user solutions (not unit tests), provide assembly processor with ProjectReferences paths so that they can be readded to assembly references for serialization module initializer (otherwise .exe don't have real reference on Game assemblies with auto load scene game) -->
<XenkoAssemblyProcessorOptions Condition="'$(XenkoUnitTest)' != 'true'">$(XenkoAssemblyProcessorOptions) @(XenkoAddReference->'--add-reference=%22%(Identity)%22',' ')</XenkoAssemblyProcessorOptions>
<XenkoAssemblyProcessorOptions Condition="'$(DocumentationFile)' != ''">$(XenkoAssemblyProcessorOptions) --docfile="$(DocumentationFile)"</XenkoAssemblyProcessorOptions>
<!-- Physics, Particles and SpriteStudio assembly is actually hard coded/referenced here, remove once a better system is in place! -->
<!-- Please keep in sync with AssemblyRecompiler.cs -->
<XenkoAssemblyProcessorOptions>$(XenkoAssemblyProcessorOptions) @(XenkoAddReference->'--add-reference=%22%(Identity)%22',' ')</XenkoAssemblyProcessorOptions>
<XenkoAssemblyProcessorOptions Condition="'$(DocumentationFile)' != ''">$(XenkoAssemblyProcessorOptions) --docfile="$(DocumentationFile)"</XenkoAssemblyProcessorOptions>
<XenkoAssemblyProcessorOptions>$(XenkoAssemblyProcessorOptions) --references-file="$(IntermediateOutputPath)XenkoReferences.cache"</XenkoAssemblyProcessorOptions>
<XenkoAssemblyProcessorOptions>$(XenkoAssemblyProcessorOptions) --platform=$(XenkoPlatform) --targetFramework=$(XenkoNETFrameworkVersion) "$(IntermediateOutputPath)$(TargetName)$(TargetExt)"</XenkoAssemblyProcessorOptions>

0 comments on commit 4944a4e

Please sign in to comment.
You can’t perform that action at this time.