-
Notifications
You must be signed in to change notification settings - Fork 1
/
StorageDeviceManager.cs
131 lines (112 loc) · 2.55 KB
/
StorageDeviceManager.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Storage;
public class StorageDeviceManager : GameComponent
{
private bool wasDeviceConnected;
private bool showDeviceSelector;
public bool isDone = true;
public StorageDevice Device;
public PlayerIndex? Player;
public PlayerIndex PlayerToPrompt;
public int RequiredBytes;
public event EventHandler DeviceSelected;
public event EventHandler<EventArgs> DeviceSelectorCanceled;
public event EventHandler<EventArgs> DeviceDisconnected;
public StorageDeviceManager(Game game)
: this(game, null, 0)
{
}
public StorageDeviceManager(Game game, PlayerIndex player)
: this(game, player, 0)
{
}
public StorageDeviceManager(Game game, int requiredBytes)
: this(game, null, requiredBytes)
{
}
public StorageDeviceManager(Game game, PlayerIndex player, int requiredBytes)
: this(game, (PlayerIndex?)player, requiredBytes)
{
}
private StorageDeviceManager(Game game, PlayerIndex? player, int requiredBytes)
: base(game)
{
Player = player;
RequiredBytes = requiredBytes;
PlayerToPrompt = PlayerIndex.One;
}
public void PromptForDevice()
{
if (isDone)
{
isDone = false;
showDeviceSelector = true;
}
}
public override void Update(GameTime gameTime)
{
bool flag = false;
if (Device != null)
{
flag = Device.IsConnected;
if (!flag && wasDeviceConnected)
{
FireDeviceDisconnectedEvent();
}
}
try
{
if (!Guide.IsVisible && showDeviceSelector)
{
showDeviceSelector = false;
if (Player.HasValue)
{
StorageDevice.BeginShowSelector(Player.Value, RequiredBytes, 0, (AsyncCallback)deviceSelectorCallback, (object)null);
}
else
{
StorageDevice.BeginShowSelector(RequiredBytes, 0, (AsyncCallback)deviceSelectorCallback, (object)null);
}
}
}
catch (GamerServicesNotAvailableException)
{
}
catch (GuideAlreadyVisibleException)
{
}
wasDeviceConnected = flag;
}
private void deviceSelectorCallback(IAsyncResult ar)
{
Device = StorageDevice.EndShowSelector(ar);
if (Device != null)
{
if (this.DeviceSelected != null)
{
this.DeviceSelected(this, null);
}
wasDeviceConnected = true;
}
else
{
if (this.DeviceSelectorCanceled != null)
{
this.DeviceSelectorCanceled(this, null);
}
showDeviceSelector = false;
}
isDone = true;
}
private void FireDeviceDisconnectedEvent()
{
Device = null;
if (this.DeviceDisconnected != null)
{
this.DeviceDisconnected(this, null);
}
showDeviceSelector = false;
}
}