forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TitleContainer.WinRT.cs
57 lines (48 loc) · 1.77 KB
/
TitleContainer.WinRT.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// MonoGame - Copyright (C) The MonoGame Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
using System.IO;
using System.Threading.Tasks;
using Windows.ApplicationModel.Resources.Core;
namespace Microsoft.Xna.Framework
{
partial class TitleContainer
{
static internal ResourceContext ResourceContext;
static internal ResourceMap FileResourceMap;
static partial void PlatformInit()
{
Location = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;
ResourceContext = new Windows.ApplicationModel.Resources.Core.ResourceContext();
FileResourceMap = ResourceManager.Current.MainResourceMap.GetSubtree("Files");
}
private static async Task<Stream> OpenStreamAsync(string name)
{
NamedResource result;
if (FileResourceMap != null && FileResourceMap.TryGetValue(name, out result))
{
var resolved = result.Resolve(ResourceContext);
try
{
var storageFile = await resolved.GetValueAsFileAsync();
var randomAccessStream = await storageFile.OpenReadAsync();
return randomAccessStream.AsStreamForRead();
}
catch (IOException)
{
// The file must not exist... return a null stream.
return null;
}
}
else
{
return null;
}
}
private static Stream PlatformOpenStream(string safeName)
{
return Task.Run(() => OpenStreamAsync(safeName).Result).Result;
}
}
}