Created
October 4, 2021 08:02
-
-
Save dellis1972/8a16abceb3b4b2222b38f8bee221534b to your computer and use it in GitHub Desktop.
MonoGame DownloadContentManager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Collections.Generic; | |
using Microsoft.Xna.Framework; | |
using Microsoft.Xna.Framework.Content; | |
using Xamarin.Google.Android.Play.Core.AssetPacks; | |
using Xamarin.Google.Android.Play.Core.AssetPacks.Model; | |
namespace MonoGameAndroidAds | |
{ | |
public class AssetPackContentManager : DownloadContentManager | |
{ | |
IAssetPackManager assetPackManager; | |
HashSet<string> assetPacks = new HashSet<string> (); | |
Dictionary<string, AssetPackLocation> assetlocationCache = new Dictionary<string, AssetPackLocation> (); | |
public AssetPackContentManager (IServiceProvider serviceProvider, IAssetPackManager assetPackManager, string rootDirectory = "Content") | |
: base(serviceProvider, rootDirectory) | |
{ | |
this.assetPackManager = assetPackManager; | |
} | |
public override void RegisterContent (string pack, string location = "") | |
{ | |
if (!assetPacks.Contains (pack)) | |
assetPacks.Add (pack); | |
} | |
public override void UnregisterContent (string pack) | |
{ | |
if (assetPacks.Contains (pack)) | |
assetPacks.Remove (pack); | |
} | |
protected override Stream OpenStream(string assetName) | |
{ | |
foreach (var pack in assetPacks) { | |
string path = GetAbsoluteAssetPath (pack, assetName); | |
if (!string.IsNullOrEmpty (path) && File.Exists (path)) { | |
MemoryStream memStream = new MemoryStream(); | |
using (Stream stream = File.Open (path, FileMode.Open)) { | |
stream.CopyTo(memStream); | |
memStream.Seek(0, SeekOrigin.Begin); | |
} | |
return memStream; | |
} | |
} | |
// revert back to the normal load process. | |
return base.OpenStream (assetName); | |
} | |
public override (bool result, string state) DownloadContent (string pack) | |
{ | |
var location = assetPackManager.GetPackLocation (pack); | |
if (location?.AssetsPath () == null) { | |
if (states.ContainsKey (pack)) | |
return (result:false, state:states[pack]); | |
assetPackManager.Fetch (new string[] { pack }); | |
states[pack] = "downloading"; | |
return (result:false, state:states[pack]); | |
} else { | |
return (result:true, state:"complete"); | |
} | |
} | |
AssetPackLocation GetPackLocation (string assetPack) | |
{ | |
if (assetlocationCache.ContainsKey (assetPack)) | |
return assetlocationCache[assetPack]; | |
var assetPackLocation = assetPackManager.GetPackLocation (assetPack); | |
if (assetPackLocation != null) | |
assetlocationCache[assetPack] = assetPackLocation; | |
return assetPackLocation; | |
} | |
string GetAbsoluteAssetPath(string assetPack, string relativeAssetPath) { | |
AssetPackLocation assetPackPath = GetPackLocation(assetPack); | |
if (assetPackPath == null) | |
return null; | |
string assetsFolderPath = assetPackPath.AssetsPath() ?? null; | |
if (assetsFolderPath == null) { | |
// asset pack is not ready | |
return null; | |
} | |
string assetPath = Path.Combine(assetsFolderPath, RootDirectory, relativeAssetPath.Replace ("\\", "/") + ".xnb"); | |
return assetPath; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Collections.Generic; | |
using Microsoft.Xna.Framework; | |
using Microsoft.Xna.Framework.Content; | |
using Xamarin.Google.Android.Play.Core.AssetPacks; | |
using Xamarin.Google.Android.Play.Core.AssetPacks.Model; | |
namespace MonoGameAndroidAds | |
{ | |
public abstract class DownloadContentManager : ContentManager { | |
protected Dictionary<string, string> states = new Dictionary<string, string> (); | |
public DownloadContentManager(IServiceProvider serviceProvider, string rootDirectory = "Content") | |
: base(serviceProvider, rootDirectory) | |
{ | |
} | |
public virtual void RegisterContent (string pack, string location = "") | |
{ | |
} | |
public virtual void UnregisterContent (string pack) | |
{ | |
} | |
public virtual bool ContentAvailable (string pack) | |
{ | |
return states.ContainsKey (pack) && states[pack] == "complete"; | |
} | |
public virtual (bool result, string state) DownloadContent (string pack) | |
{ | |
return (result:false, state:"failed"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment