forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TitleContainer.cs
76 lines (64 loc) · 2.49 KB
/
TitleContainer.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// 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 Microsoft.Xna.Framework.Utilities;
using MonoGame.Utilities;
namespace Microsoft.Xna.Framework
{
public static partial class TitleContainer
{
static partial void PlatformInit();
static TitleContainer()
{
Location = string.Empty;
PlatformInit();
}
static internal string Location { get; private set; }
/// <summary>
/// Returns an open stream to an exsiting file in the title storage area.
/// </summary>
/// <param name="name">The filepath relative to the title storage area.</param>
/// <returns>A open stream or null if the file is not found.</returns>
public static Stream OpenStream(string name)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException("name");
// We do not accept absolute paths here.
if (Path.IsPathRooted(name))
throw new ArgumentException("Invalid filename. TitleContainer.OpenStream requires a relative path.", name);
// Normalize the file path.
var safeName = NormalizeRelativePath(name);
// Call the platform code to open the stream. Any errors
// at this point should result in a file not found.
Stream stream;
try
{
stream = PlatformOpenStream(safeName);
if (stream == null)
throw FileNotFoundException(name, null);
}
catch (FileNotFoundException)
{
throw;
}
catch (Exception ex)
{
throw new FileNotFoundException(name, ex);
}
return stream;
}
private static Exception FileNotFoundException(string name, Exception inner)
{
return new FileNotFoundException("Error loading \"" + name + "\". File not found.", inner);
}
internal static string NormalizeRelativePath(string name)
{
var uri = new Uri("file:///" + name);
var path = uri.LocalPath;
path = path.Substring(1);
return path.Replace(FileHelpers.NotSeparator, FileHelpers.Separator);
}
}
}