Skip to content

Commit

Permalink
_OPENGL_FORCE_ES3 and _CONTENT_FORCE_ZIP* env vars
Browse files Browse the repository at this point in the history
Latter environment variables requires one of the following build vars:
* ENABLE_ZIPPING (build target 4.5+)
* ENABLE_ZIPPING_REFLECTION
ZipArchive is only available since .NET Framework 4.5+.
Feel free to replace ZipArchive with a third-party ZIP lib.

Untested.
  • Loading branch information
0x0ade committed Nov 20, 2015
1 parent 5aef25f commit cdf4707
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/Graphics/OpenGLDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ private static IntPtr glGetProcAddress(string name, IntPtr d)
#region Private Profile-specific Variables

private bool useES2;
private bool useES3;
private bool useCoreProfile;
private uint vao;

Expand Down Expand Up @@ -592,6 +593,8 @@ PresentationParameters presentationParameters
int es2Flag = (int) SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_ES;
SDL.SDL_GL_GetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, out flags);
useES2 = (flags & es2Flag) == es2Flag;
SDL.SDL_GL_GetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, out flags);
useES3 = flags == 3;

// Check for a possible Core context
int coreFlag = (int) SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_CORE;
Expand Down
8 changes: 6 additions & 2 deletions src/SDL2/SDL2_GamePlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,18 @@ public SDL2_GamePlatform(Game game) : base(game, SDL.SDL_GetPlatform())
bool forceES2 = Environment.GetEnvironmentVariable(
"FNA_OPENGL_FORCE_ES2"
) == "1";
bool forceES3 = Environment.GetEnvironmentVariable(
"FNA_OPENGL_FORCE_ES3"
) == "1";
bool forceCoreProfile = Environment.GetEnvironmentVariable(
"FNA_OPENGL_FORCE_CORE_PROFILE"
) == "1";
Window = new SDL2_GameWindow(
forceES2 ||
!forceES3 && (forceES2 ||
OSVersion.Equals("Emscripten") ||
OSVersion.Equals("Android") ||
OSVersion.Equals("iOS"),
OSVersion.Equals("iOS")),
forceES3,
forceCoreProfile
);

Expand Down
6 changes: 3 additions & 3 deletions src/SDL2/SDL2_GameWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public override string ScreenDeviceName

#region Internal Constructor

internal SDL2_GameWindow(bool useES2, bool useCoreProfile)
internal SDL2_GameWindow(bool useES2, bool useES3, bool useCoreProfile)
{
SDL.SDL_WindowFlags initFlags = (
SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL |
Expand All @@ -178,7 +178,7 @@ internal SDL2_GameWindow(bool useES2, bool useCoreProfile)
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_DEPTH_SIZE, 24);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_STENCIL_SIZE, 8);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_DOUBLEBUFFER, 1);
if (useES2)
if (useES2 || useES3)
{
SDL.SDL_GL_SetAttribute(
SDL.SDL_GLattr.SDL_GL_RETAINED_BACKING,
Expand All @@ -190,7 +190,7 @@ internal SDL2_GameWindow(bool useES2, bool useCoreProfile)
);
SDL.SDL_GL_SetAttribute(
SDL.SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION,
2
useES2 ? 2 : useES3 ? 3 : 0 /* Maybe pass an int via useES instead? */
);
SDL.SDL_GL_SetAttribute(
SDL.SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION,
Expand Down
128 changes: 128 additions & 0 deletions src/TitleContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,50 @@
*/
#endregion

#region ENABLE_ZIPPING Option
//#define ENABLE_ZIPPING
/* ZipArchive is only available in .NET Framework 4.5+ and is
* only beneficial for Android when packaging your content in OBBs.
* Using this also requires the FNA_CONTENT_FORCE_ZIP environment
* flag to be set, which is automatically done by the unofficial
* FNADroid wrapper.
*
* As it's only available in .NET 4.5+, you also need to
* set the build target of FNA to .NET 4.5+, killing support for
* MonoKickstart (Linux, OSX) until it's updated.
*
* Finally, using FNA_CONTENT_FORCE_ZIP may break your game in
* unexpected ways.
*
* You normally won't need this.
* -ade
*/
#endregion

#region ENABLE_ZIPPING_REFLECTION Option
#define ENABLE_ZIPPING_REFLECTION
/* Unwanted cousin of ENABLE_ZIPPING, using reflection.
* It does not require you to set the build target to .NET 4.5+,
* but it still only works on .NET 4.5+ (duh).
*
* As it uses reflection, performance is going to take a hit.
*
* You NEVER need this. Whoever thinks that they need this should use
* the above or should accept the performance issues.
* -ade
*/
#endregion

#region Using Statements
using System;
using System.IO;

#if ENABLE_ZIPPING
using System.IO.Compression;
#elif ENABLE_ZIPPING_REFLECTION
using System.Reflection;
#endif

using Microsoft.Xna.Framework.Utilities;
#endregion

Expand All @@ -25,14 +65,81 @@ static internal string Location
get;
private set;
}

#if ENABLE_ZIPPING
static internal ZipArchive Zip
{
get;
private set;
}
#else
//FNA should still be able to set it to null and
//_REFLECTION needs to access this. To my knowledge,
//it's required to remove all references to the
//ZIP to allow the GC to call Finalize().
//-ade
static internal Object Zip
{
get;
private set;
}
#endif

#endregion

#if ENABLE_ZIPPING_REFLECTION
#region Internal Reflection-related Variables

internal static MethodInfo m_ZipArchive_GetEntry;
internal static object[] arg_ZipArchive_GetEntry = new object[1];
internal static MethodInfo m_ZipEntry_Open;
internal static object[] arg_ZipEntry_Open = new object[0];

#endregion
#endif

#region Static Constructor

static TitleContainer()
{
Location = AppDomain.CurrentDomain.BaseDirectory;

#if ENABLE_ZIPPING || ENABLE_ZIPPING_REFLECTION
string forcedZip = Environment.GetEnvironmentVariable("FNA_CONTENT_FORCE_ZIP");
if (!string.IsNullOrEmpty(forcedZip)) {
Location = "";
#endif
#if ENABLE_ZIPPING
//4.5+
Zip = newZipArchive(File.OpenRead(forcedZip), ZipArchiveMode.Read);
#else
//4.5+ with 4.0 as build target because reasons
//Go through all assemblies in the AppDomain and hope that System.IO.Compression has been loaded
Assembly a_System_IO_Compression = null;
Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly asm in asms) {
if (asm.GetName().Name == "System.IO.Compression") {
a_System_IO_Compression = asm;
break;
}
}
//Assembly not found? Time to use Assembly.LoadFrom
if (a_System_IO_Compression == null) {
a_System_IO_Compression = Assembly.LoadFrom("System.IO.Compression.dll");
//If it's not found, don't worry: The game will just crash telling you about it.
}
//Get the types and methods
Type t_ZipArchive = a_System_IO_Compression.GetType("System.IO.Compression.ZipArchive");
Type t_ZipEntry = a_System_IO_Compression.GetType("System.IO.Compression.ZipEntry");
m_ZipArchive_GetEntry = t_ZipArchive.GetMethod("GetEntry", new Type[] {typeof(string)});
m_ZipEntry_Open = t_ZipEntry.GetMethod("Open", new Type[0]);
//FINALLY call the constructor
//As the doc says "The stream that contains the archive to be read", we simply skip the ZipArchiveMode
Zip = t_ZipArchive.GetConstructor(new Type[] {typeof(Stream)}).Invoke(new object[] {File.OpenRead(forcedZip)});
#endif
#if ENABLE_ZIPPING || ENABLE_ZIPPING_REFLECTION
}
#endif
}

#endregion
Expand All @@ -52,6 +159,27 @@ public static Stream OpenStream(string name)
{
return File.OpenRead(safeName);
}

#if ENABLE_ZIPPING || ENABLE_ZIPPING_REFLECTION
if (Zip != null)
{
System.Console.WriteLine("TitleContainer.OpenStream name: " + name);
#endif
#if ENABLE_ZIPPING
//4.5+
System.Console.WriteLine("TitleContainer.OpenStream entry: " + Zip.GetEntry(safeName));
return Zip.GetEntry(safeName).Open();
#else
//4.5+ with 4.0 as build target because reasons
arg_ZipArchive_GetEntry[0] = safeName;
object o_ZipEntry = m_ZipArchive_GetEntry.Invoke(Zip, arg_ZipArchive_GetEntry);
System.Console.WriteLine("TitleContainer.OpenStream entry: " + o_ZipEntry);
return (Stream) m_ZipEntry_Open.Invoke(o_ZipEntry, arg_ZipEntry_Open);
#endif
#if ENABLE_ZIPPING || ENABLE_ZIPPING_REFLECTION
}
#endif

return File.OpenRead(Path.Combine(Location, safeName));
}

Expand Down

0 comments on commit cdf4707

Please sign in to comment.