-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for dynamic assets (#27)
- Loading branch information
Showing
9 changed files
with
140 additions
and
10 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace Rive.Maui; | ||
|
||
public class DynamicAsset | ||
{ | ||
public DynamicAsset() | ||
{ | ||
} | ||
|
||
public DynamicAsset(string name, string filename, string extension) | ||
{ | ||
Name = name; | ||
Filename = filename; | ||
Extension = extension; | ||
} | ||
|
||
public required string Name { get; set; } | ||
public required string Filename { get; set; } | ||
public required string Extension { get; set; } | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using Android.Content; | ||
using Rive.Android.Core; | ||
|
||
namespace Rive.Maui; | ||
|
||
public class AssetLoader : FileAssetLoader | ||
{ | ||
private readonly Context _context; | ||
private readonly List<DynamicAsset> _dynamicAssets; | ||
|
||
public AssetLoader(Context context, List<DynamicAsset> dynamicAssets) | ||
{ | ||
_context = context; | ||
_dynamicAssets = dynamicAssets; | ||
} | ||
|
||
public override bool LoadContents(FileAsset asset, byte[] inBandBytes) | ||
{ | ||
var dynamicAsset = _dynamicAssets.FirstOrDefault(x => string.Equals(x.Name, asset.Name, StringComparison.OrdinalIgnoreCase)); | ||
if (dynamicAsset == null) | ||
return false; | ||
|
||
var resourceIdentifier = _context.Resources?.GetIdentifier(dynamicAsset.Filename, "drawable", _context.PackageName) ?? 0; | ||
if (resourceIdentifier == 0) | ||
return false; | ||
|
||
using var stream = _context.Resources?.OpenRawResource(resourceIdentifier); | ||
if (stream == null) | ||
return false; | ||
|
||
using var memoryStream = new MemoryStream(); | ||
stream.CopyTo(memoryStream); | ||
|
||
switch (asset) | ||
{ | ||
case ImageAsset imageAsset: | ||
imageAsset.Decode(memoryStream.ToArray()); | ||
return true; | ||
case FontAsset fontAsset: | ||
fontAsset.Decode(memoryStream.ToArray()); | ||
return true; | ||
case AudioAsset audioAsset: | ||
audioAsset.Decode(memoryStream.ToArray()); | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
} |
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
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
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
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
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
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