Jypeli  5
The simple game programming library
WindowsFileManager.cs
Siirry tämän tiedoston dokumentaatioon.
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using System.Windows.Forms;
6 using System.Security.Permissions;
7 using System.Security;
8 
9 namespace Jypeli
10 {
12  {
13  private string[] pathCandidates;
14 
15  public WindowsFileManager( params string[] pathCandidates )
16  {
17  this.pathCandidates = pathCandidates;
18  }
19 
20  protected override void Initialize()
21  {
22  if ( _currentDir != null )
23  return;
24 
25  for ( int i = 0; i < pathCandidates.Length; i++ )
26  {
27  _currentDir = pathCandidates[i];
28 
29  if ( Directory.Exists( _currentDir ) )
30  return;
31  else
32  {
33  var parent = Directory.GetParent( _currentDir );
34  if ( !parent.Exists ) continue;
35  Directory.CreateDirectory( _currentDir );
36  }
37  }
38  }
39 
41  : this(WindowsLocation.ExePath, Path.Combine(WindowsLocation.MyDocuments, Game.Name))
42  {
43  }
44 
50  public override bool Exists( string fileName )
51  {
52  Initialize();
53  MakeAbsolute( ref fileName );
54  return FMAssert( File.Exists, false, false, fileName ) || FMAssert( Directory.Exists, false, false, fileName );
55  }
56 
62  public override bool ChDir( string path )
63  {
64  Initialize();
65  MakeAbsolute( ref path );
66 
67  if ( !FMAssert( Directory.Exists, false, false, path ) )
68  return false;
69 
70  _currentDir = path;
71  return true;
72  }
73 
78  public override void MkDir( string path )
79  {
80  Initialize();
81  MakeAbsolute( ref path );
82  FMAssert( Directory.CreateDirectory, true, null, path );
83  }
84 
90  public override void RmDir( string path )
91  {
92  Initialize();
93  MakeAbsolute( ref path );
94  FMAssert( Directory.Delete, true, path );
95  }
96 
101  public override IList<string> GetFileList()
102  {
103  Initialize();
104  string[] fileList = FMAssert( Directory.GetFiles, false, new string[] { }, _currentDir );
105  return fileList.ToList<string>().AsReadOnly();
106  }
107 
114  public override StorageFile Open( string fileName, bool write )
115  {
116  Initialize();
117  MakeAbsolute( ref fileName );
118  Stream stream = FMAssert( openFileStream, write, null, fileName, write );
119  return new StorageFile( fileName, stream );
120  }
121 
122  private Stream openFileStream(string fileName, bool write)
123  {
124  FileMode mode = write ? FileMode.Create : FileMode.Open;
125  FileAccess access = write ? FileAccess.ReadWrite : FileAccess.Read;
126  return new FileStream( fileName, mode, access );
127  }
128 
134  public override void Delete( string fileName )
135  {
136  Initialize();
137  MakeAbsolute( ref fileName );
138  FMAssert( File.Delete, true, fileName );
139  }
140  }
141 
145  public static class WindowsLocation
146  {
150  public static readonly string MyDocuments = Environment.GetFolderPath( Environment.SpecialFolder.MyDocuments );
151 
155  public static readonly string MyMusic = Environment.GetFolderPath( Environment.SpecialFolder.MyMusic );
156 
160  public static readonly string MyPictures = Environment.GetFolderPath( Environment.SpecialFolder.MyPictures );
161 
165  public static readonly string MyVideos = Environment.GetFolderPath( Environment.SpecialFolder.MyVideos );
166 
170  public static readonly string ExePath = Path.GetDirectoryName( Application.ExecutablePath );
171 
175  public static readonly string DataPath = Path.Combine( Path.GetDirectoryName( Application.ExecutablePath ), "Data" );
176 
180  public static readonly string ContentPath = Path.Combine( Path.GetDirectoryName( Application.ExecutablePath ), "Content" );
181  }
182 }
void FMAssert(Action func, bool write)
Definition: Assert.cs:26
override IList< string > GetFileList()
Antaa listan nykyisessä hakemistossa olevista tiedostoista.
override void Delete(string fileName)
Poistaa tiedoston. Ei heitä poikkeusta, jos tiedostoa ei ole olemassa.
override void MkDir(string path)
Luo uuden hakemiston.
override void RmDir(string path)
Tuhoaa hakemiston. Heittää poikkeuksen jos hakemisto ei ole tyhjä. Ei heitä poikkeusta, jos hakemistoa ei ole olemassa.
WindowsFileManager(params string[] pathCandidates)
override StorageFile Open(string fileName, bool write)
Avaa tiedoston.
override bool Exists(string fileName)
Kertoo onko tiedosto tai hakemisto olemassa.
Peliluokka reaaliaikaisille peleille.
Definition: DebugScreen.cs:10
override bool ChDir(string path)
Vaihtaa työhakemistoa.
Usein käytettyjä polkuja Windowsissa.