Skip to content

Commit

Permalink
Android: Fix loading ogg song asset by copy assets to app Internal St…
Browse files Browse the repository at this point in the history
…orage
  • Loading branch information
ryancheung committed Apr 17, 2024
1 parent d4314c4 commit 4727334
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
27 changes: 5 additions & 22 deletions src/Content/ContentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,6 @@ protected T ReadAsset<T>(string assetName, Action<IDisposable> recordDisposableO
}
}

#if ANDROID
var inputStream = ((Android.Runtime.InputStreamInvoker) stream).BaseInputStream;
var s = new MemoryStream();
stream.CopyTo(s);
stream = s;
stream.Seek(0, SeekOrigin.Begin);
#endif

// Check for XNB header
stream.Read(xnbHeader, 0, xnbHeader.Length);
if ( xnbHeader[0] == 'X' &&
Expand Down Expand Up @@ -601,28 +593,19 @@ private Stream CheckRawExtensions(string assetName, string[] extensions)
{
// Concatenate the file name with valid extensions.
string fileNamePlusExt = fileName + ext;

#if ANDROID
try
{
return TitleContainer.OpenStream(fileNamePlusExt);
}
catch (Exception) { return null; }
#endif

if (File.Exists(fileNamePlusExt))
{
return TitleContainer.OpenStream(fileNamePlusExt);
}
}

// If we got here, we need to try the slower path :(
// If we got here, we need to try the slower path :(. We load android raw assets here :)
fileName = MonoGame.Utilities.FileHelpers.NormalizeFilePathSeparators(
assetName
Path.Combine(RootDirectory, assetName)
);
try
{
return OpenStream(fileName);
return TitleContainer.OpenStream(fileName);
}
catch
{
Expand All @@ -632,7 +615,7 @@ private Stream CheckRawExtensions(string assetName, string[] extensions)
string fileNamePlusExt = fileName + ext;
try
{
return OpenStream(fileNamePlusExt);
return TitleContainer.OpenStream(fileNamePlusExt);
}
catch
{
Expand Down Expand Up @@ -725,4 +708,4 @@ private static void RemoveContentManager(ContentManager contentManager)

#endregion
}
}
}
2 changes: 2 additions & 0 deletions src/FNAPlatform/SDL2_FNAPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,8 @@ private static string GetBaseDirectory()
return AppDomain.CurrentDomain.BaseDirectory;
}
}
if (OSVersion.Equals("Android")) // FNA.NET use INTERNAL_STORAGE as base path and assets will be copied there.
return Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string result = SDL.SDL_GetBasePath();
if (string.IsNullOrEmpty(result))
{
Expand Down
25 changes: 24 additions & 1 deletion src/TitleContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ public static class TitleContainer
{
#region Public Static Methods

#if ANDROID
[System.Runtime.Versioning.SupportedOSPlatform("Android21.0")]
#endif
public static Stream OpenStream(string name)
{
string safeName = MonoGame.Utilities.FileHelpers.NormalizeFilePathSeparators(name);

#if ANDROID
return Android.App.Application.Context.Assets.Open(safeName);
return AndroidOpenStream(safeName);
#endif

#if CASE_SENSITIVITY_HACK
Expand Down Expand Up @@ -139,6 +142,26 @@ private static string SearchCase(string name, string[] list)
#endif

#endregion

#if ANDROID
[System.Runtime.Versioning.SupportedOSPlatform("Android21.0")]
public static FileStream AndroidOpenStream(string name)
{
var path = Path.Combine(TitleLocation.Path, name);
if (!File.Exists(path))
{
var inputStream = Android.App.Application.Context.Assets.Open(name);

Directory.CreateDirectory(Path.GetDirectoryName(path)); // Ensure subdirectories
FileStream s = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
inputStream.CopyTo(s);
s.Seek(0, SeekOrigin.Begin);
return s;
}
else
return File.OpenRead(path);
}
#endif
}
}

3 changes: 0 additions & 3 deletions src/TitleLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ public static string Path
{
get
{
#if ANDROID
return ""; // There's no way to get title location in android.
#endif
return FNAPlatform.TitleLocation;
}
}
Expand Down

0 comments on commit 4727334

Please sign in to comment.