Jypeli  5
The simple game programming library
XboxFileManager.cs
Siirry tämän tiedoston dokumentaatioon.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework.Storage;
6 using Microsoft.Xna.Framework.GamerServices;
7 using System.Reflection;
8 using System.IO;
9 
10 namespace Jypeli
11 {
13  {
14  delegate void AssertCallback( string fileName );
15 
16  StorageDevice device = null;
17  StorageContainer container = null;
18 
19  public XboxFileManager()
20  {
21  StorageDevice.DeviceChanged += new EventHandler<EventArgs>( StorageDevice_DeviceChanged );
22  }
23 
24  void StorageDevice_DeviceChanged( object sender, EventArgs e )
25  {
26  if ( device == null || device.IsConnected )
27  return;
28 
29  device = null;
30  container = null;
31  }
32 
33  void AssertDevice( AssertCallback callback, string fileName )
34  {
35  if ( device == null )
36  {
37  AsyncCallback asCallback = delegate( IAsyncResult result )
38  {
39  StorageDevice.EndShowSelector( result );
40  callback( fileName );
41  };
42 
43  StorageDevice.BeginShowSelector( asCallback, null );
44  }
45  else
46  callback( fileName );
47  }
48 
49  void AssertContainer( AssertCallback callback, string fileName )
50  {
51  if ( container == null )
52  {
53  AsyncCallback asCallback = delegate( IAsyncResult result )
54  {
55  device.EndOpenContainer( result );
56  callback( fileName );
57  };
58 
59  device.BeginOpenContainer( Game.Name, asCallback, null );
60  }
61  else
62  callback( fileName );
63  }
64 
65  void AssertDevContainer( AssertCallback callback, string fileName )
66  {
67  if ( container != null )
68  callback( fileName );
69  else if ( device != null )
70  AssertContainer( callback, fileName );
71  else
72  AssertDevice( delegate { AssertContainer( callback, fileName ); }, fileName );
73  }
74 
75  void AssertDevContainerWait()
76  {
77  if ( device == null )
78  {
79  IAsyncResult result = StorageDevice.BeginShowSelector( null, null );
80  result.AsyncWaitHandle.WaitOne();
81  }
82 
83  if ( container == null )
84  {
85  IAsyncResult result = device.BeginOpenContainer( Game.Name, null, null );
86  result.AsyncWaitHandle.WaitOne();
87  }
88  }
89 
90  public override bool Exists( string fileName )
91  {
92  MakeAbsolute(ref fileName);
93  AssertDevContainerWait();
94  return container.FileExists( fileName );
95  }
96 
97  public override StorageFile Open( string fileName, bool write )
98  {
99  MakeAbsolute( ref fileName );
100  AssertDevContainerWait();
101 
102  FileMode fileMode = write ? FileMode.Create : FileMode.Open;
103  FileAccess fileAccess = write ? FileAccess.ReadWrite : FileAccess.Read;
104  return new StorageFile( fileName, container.OpenFile( fileName, fileMode, fileAccess ) );
105  }
106 
107  public override void Delete( string fileName )
108  {
109  MakeAbsolute( ref fileName );
110  AssertDevContainer( doDeleteFile, fileName );
111  }
112 
113  public override bool ChDir( string path )
114  {
115  MakeAbsolute( ref path );
116  AssertDevContainer( doChangeDir, path );
117  return true;
118  }
119 
120  public override void MkDir( string path )
121  {
122  MakeAbsolute( ref path );
123  AssertDevContainer( doMakeDir, path );
124  }
125 
126  public override void RmDir( string path )
127  {
128  MakeAbsolute( ref path );
129  AssertDevContainer( doRmDir, path );
130  }
131 
132  public override IList<string> GetFileList()
133  {
134  AssertDevContainer( doMakeDir, _currentDir );
135  string[] fileList = container.GetFileNames( _currentDir + "\\*" );
136  return fileList.ToList<string>().AsReadOnly();
137  }
138 
139  private void doDeleteFile( string fileName )
140  {
141  MakeAbsolute( ref fileName );
142  container.DeleteFile( fileName );
143  }
144 
145  private void doChangeDir( string path )
146  {
147  MakeAbsolute( ref path );
148 
149  if ( !container.DirectoryExists( path ) )
150  throw new DirectoryNotFoundException( "Directory " + path + " was not found." );
151 
152  _currentDir = path;
153  }
154 
155  private void doMakeDir( string path )
156  {
157  MakeAbsolute( ref path );
158  container.CreateDirectory( path );
159  }
160 
161  private void doRmDir( string path )
162  {
163  MakeAbsolute( ref path );
164  container.DeleteDirectory( path );
165  }
166  }
167 }
override void MkDir(string path)
Luo uuden hakemiston.
override void RmDir(string path)
Poistaa hakemiston.
override bool Exists(string fileName)
override bool ChDir(string path)
Vaihtaa nykyistä hakemistoa.
Peliluokka reaaliaikaisille peleille.
Definition: DebugScreen.cs:10
override StorageFile Open(string fileName, bool write)
static string Name
Pelin nimi.
Definition: Game.cs:140
override IList< string > GetFileList()
override void Delete(string fileName)