Skip to content

Commit

Permalink
Added native resolving of file paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomspilman committed Sep 10, 2024
1 parent 95ab28e commit 6fbebc0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
11 changes: 4 additions & 7 deletions MonoGame.Framework/Platform/Native/TitleContainer.Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// file 'LICENSE.txt', which is part of this source code package.

using System.IO;
using MonoGame.Interop;


namespace Microsoft.Xna.Framework;

Expand All @@ -15,13 +17,8 @@ static partial void PlatformInit()

private static Stream PlatformOpenStream(string safeName)
{
// TODO: How do we handle this?
//
// It seems like we still need platform specific C# code here?
//
// Or do we make the native code transform the string?
//
var absolutePath = Path.Combine(Location, safeName);
var absolutePath = MGP.Platform_MakePath(Location, safeName);

return File.OpenRead(absolutePath);
}
}
10 changes: 5 additions & 5 deletions native/monogame/common/MGM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ MGM_Video* MGM_Video_Create(const char* mediaFilePath, mgint cachedFrameNum, mgi

// TODO: Like Song above we should detect the media
// format from a native decoder libraries that are
// portable to all our target platforms:
//
// libtheora - free but limited quality/performance
// OpenH264 -
// ???
// portable to all our target platforms.
//
// It should then spin up thread which decodes the
// video/audio streams.

// TOOD: Ideally we just support OpenH264+AAC which is pretty
// much industry standard now. Anything else is not
// importaint unless a new standard comes around.

auto video = new MGM_Video();
video->duration = duration = 0;
video->width = width = 0;
Expand Down
1 change: 1 addition & 0 deletions native/monogame/include/api_MGP.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ MG_EXPORT void MGP_Platform_StartRunLoop(MGP_Platform* platform);
MG_EXPORT mgbool MGP_Platform_BeforeRun(MGP_Platform* platform);
MG_EXPORT mgbool MGP_Platform_BeforeUpdate(MGP_Platform* platform);
MG_EXPORT mgbool MGP_Platform_BeforeDraw(MGP_Platform* platform);
MG_EXPORT const char* MGP_Platform_MakePath(const char* location, const char* path);
MG_EXPORT MGP_Window* MGP_Window_Create(MGP_Platform* platform, mgint width, mgint height, const char* title);
MG_EXPORT void MGP_Window_Destroy(MGP_Window* window);
MG_EXPORT void MGP_Window_SetIconBitmap(MGP_Window* window, mgbyte* icon, mgint length);
Expand Down
5 changes: 5 additions & 0 deletions native/monogame/include/mg_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
#define MG_GENERATE_TRAP() __builtin_trap()
#endif

#ifdef _MSC_VER
#define MG_PATH_SEPARATOR "\\"
#else
#define MG_PATH_SEPARATOR "/"
#endif

inline void MG_Print_StdError(const char* file, int line, const char* message)
{
Expand Down
34 changes: 34 additions & 0 deletions native/monogame/sdl/MGP_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

#include <sdl.h>

#if _WIN32
#include <combaseapi.h>
#endif


struct MGP_Platform
{
Expand Down Expand Up @@ -213,6 +217,36 @@ void MGP_Platform_Destroy(MGP_Platform* platform)
delete platform;
}

const char* MGP_Platform_MakePath(const char* location, const char* path)
{
assert(location != nullptr);
assert(path != nullptr);

size_t length = strlen(path) + 1;
if (location[0])
length += strlen(location) + 1;

#if _WIN32
// Windows requires marshaled strings to be allocated like this.
char* fpath = (char*)CoTaskMemAlloc(length);
#else
char* fpath = (char*)malloc(length);
#endif

if (location[0])
{
strcpy_s(fpath, length, location);
strcat_s(fpath, length, MG_PATH_SEPARATOR);
strcat_s(fpath, length, path);
}
else
{
strcpy_s(fpath, length, path);
}

return fpath;
}

void MGP_Platform_BeforeInitialize(MGP_Platform* platform)
{
assert(platform != nullptr);
Expand Down

0 comments on commit 6fbebc0

Please sign in to comment.