Load MonoGame.Framework manually at runtime

This lets us have one build that will in theory work on Windows and
Linux.
This commit is contained in:
Drew DeVault 2015-06-18 12:23:27 -04:00
parent c91dff52a9
commit bebaed2b5b
5 changed files with 34 additions and 3 deletions

View File

@ -4,6 +4,7 @@ using System.Linq;
using System.Net.Sockets;
using TrueCraft.Core;
using System.Threading;
using System.Reflection;
namespace TrueCraft.Client
{
@ -11,12 +12,27 @@ namespace TrueCraft.Client
{
public static void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyResolve += AppDomain_CurrentDomain_AssemblyResolve;
var thread = new Thread(() => Main_Thread(args));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
static Assembly AppDomain_CurrentDomain_AssemblyResolve (object sender, ResolveEventArgs args)
{
var assemblyName = new AssemblyName(args.Name);
if (assemblyName.Name != "MonoGame.Framework")
return null;
if (RuntimeInfo.IsLinux)
return Assembly.LoadFile("MonoGame.Framework.Linux.dll");
if (RuntimeInfo.IsWindows)
return Assembly.LoadFile("MonoGame.Framework.Windows.dll");
// TODO: OSX support
return null;
}
// We need to spawn the main thread manually so we can register the assembly resolver
// and manage apartment state ourselves.
private static void Main_Thread(string[] args)

View File

@ -2,7 +2,7 @@ using System;
using System.Diagnostics;
using System.IO;
namespace TrueCraft.Launcher
namespace TrueCraft.Core
{
public static class RuntimeInfo
{

View File

@ -317,6 +317,7 @@
<Compile Include="TerrainGen\Decorators\LiquidDecorator.cs" />
<Compile Include="Logic\BlockRepository.cs" />
<Compile Include="TrueCraftUser.cs" />
<Compile Include="RuntimeInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>

View File

@ -20,6 +20,13 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<CustomCommands>
<CustomCommands>
<Command type="AfterBuild" name="Remove MonoGame" command="rm ${TargetDir}/MonoGame.Framework.dll" />
<Command type="AfterBuild" command="cp ${SolutionDir}/packages/MonoGame.Framework.Linux.3.4.0.459/lib/net40/MonoGame.Framework.dll ${TargetDir}/MonoGame.Framework.Linux.dll" />
<Command type="AfterBuild" command="cp ${SolutionDir}/packages/MonoGame.Framework.WindowsGL.3.4.0.459/lib/net40/MonoGame.Framework.dll ${TargetDir}/MonoGame.Framework.Windows.dll" />
</CustomCommands>
</CustomCommands>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
@ -28,6 +35,13 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<CustomCommands>
<CustomCommands>
<Command type="AfterBuild" name="Remove MonoGame" command="rm ${TargetDir}/MonoGame.Framework.dll" />
<Command type="AfterBuild" command="cp ${SolutionDir}/packages/MonoGame.Framework.Linux.3.4.0.459/lib/net40/MonoGame.Framework.dll ${TargetDir}/MonoGame.Framework.Linux.dll" />
<Command type="AfterBuild" command="cp ${SolutionDir}/packages/MonoGame.Framework.WindowsGL.3.4.0.459/lib/net40/MonoGame.Framework.dll ${TargetDir}/MonoGame.Framework.Windows.dll" />
</CustomCommands>
</CustomCommands>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
@ -41,7 +55,6 @@
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RuntimeInfo.cs" />
<Compile Include="LauncherWindow.cs" />
<Compile Include="Views\LoginView.cs" />
<Compile Include="Views\MainMenuView.cs" />
@ -100,4 +113,4 @@
<EmbeddedResource Include="Content\truecraft-logo.png" />
<EmbeddedResource Include="Content\default-server-icon.png" />
</ItemGroup>
</Project>
</Project>

View File

@ -5,6 +5,7 @@ using System.Threading.Tasks;
using System.Diagnostics;
using System.Linq;
using System.IO;
using TrueCraft.Core;
namespace TrueCraft.Launcher.Views
{