PhysFS.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* PhysFS.cs - (c)2003 Gregory S. Read
  2. * Provides access to PhysFS API calls not specific to file handle access.
  3. */
  4. using System;
  5. namespace PhysFS_NET
  6. {
  7. public class PhysFS
  8. {
  9. /* Initialize
  10. * Inits the PhysFS API. This normally does not need to be called unless
  11. * the API has been manually deinitialized since the PhysFS_DLL class
  12. * initializes just before the first call is made into the DLL.
  13. * Parameters
  14. * none
  15. * Returns
  16. * none
  17. * Exceptions
  18. * PhysFSException - An error occured in the PhysFS API
  19. */
  20. public static void Initialize()
  21. {
  22. // Initialize the physfs library, raise an exception if error
  23. if(PhysFS_DLL.PHYSFS_init("") == 0)
  24. throw new PhysFSException();
  25. }
  26. /* Deinitialize
  27. * Deinits the PhysFS API. It is recommended that this method be called
  28. * by the application before exiting in order to gracefully deallocate
  29. * resources and close all filehandles, etc.
  30. * Parameters
  31. * none
  32. * Returns
  33. * none
  34. * Exceptions
  35. * PhysFSException - An error occured in the PhysFS API
  36. */
  37. public static void Deinitialize()
  38. {
  39. // Deinit, raise an exception if an error occured
  40. if(PhysFS_DLL.PHYSFS_deinit() == 0)
  41. throw new PhysFSException();
  42. }
  43. /* BaseDir
  44. * Gets the base directory configured for PhysFS. See the PhysFS API
  45. * documentation for more information.
  46. * Parameters
  47. * none
  48. * Returns
  49. * A string value representing the Base Directory
  50. * Exceptions
  51. * none
  52. */
  53. public static string BaseDir
  54. {
  55. get
  56. {
  57. // Return the current base directory
  58. return PhysFS_DLL.PHYSFS_getBaseDir();
  59. }
  60. }
  61. /* WriteDir
  62. * Gets or sets the write directory configured for PhysFS. See the PhysFS API
  63. * documentation for more information.
  64. * Parameters
  65. * set - Path to set the WriteDir property to
  66. * Returns
  67. * A string value representing the Write Directory
  68. * Exceptions
  69. * PhysFSException - An error occured in the PhysFS API when
  70. * settings the write directory.
  71. */
  72. public static string WriteDir
  73. {
  74. get
  75. {
  76. // Return the current write directory
  77. return PhysFS_DLL.PHYSFS_getWriteDir();
  78. }
  79. set
  80. {
  81. // Set the write directory and raise an exception if an error occured
  82. if(PhysFS_DLL.PHYSFS_setWriteDir(value) == 0)
  83. throw new PhysFSException();
  84. }
  85. }
  86. /* UserDir
  87. * Gets or sets the write directory configured for PhysFS. See the PhysFS API
  88. * documentation for more information.
  89. * Parameters
  90. * set - Path to set the WriteDir property to
  91. * Returns
  92. * A string value representing the Write Directory
  93. * Exceptions
  94. * PhysFSException - An error occured in the PhysFS API when
  95. * settings the write directory.
  96. */
  97. public static string UserDir
  98. {
  99. get
  100. {
  101. // Return the current user directory
  102. return PhysFS_DLL.PHYSFS_getUserDir();
  103. }
  104. }
  105. public static void AddToSearchPath(string NewDir, bool Append)
  106. {
  107. if(PhysFS_DLL.PHYSFS_addToSearchPath(NewDir, Append?1:0) == 0)
  108. throw new PhysFSException();
  109. }
  110. public static void RemoveFromSearchPath(string OldDir)
  111. {
  112. if(PhysFS_DLL.PHYSFS_removeFromSearchPath(OldDir) == 0)
  113. throw new PhysFSException();
  114. }
  115. public unsafe static string[] GetSearchPath()
  116. {
  117. byte** p; // Searchpath list from PhysFS dll
  118. string[] pathlist; // List converted to an array
  119. // Get the CDROM drive listing
  120. p = PhysFS_DLL.PHYSFS_getSearchPath();
  121. // Convert the C-style array to a .NET style array
  122. pathlist = PhysFS_DLL.BytePPToArray(p);
  123. // Free the original list since we're done with it
  124. PhysFS_DLL.PHYSFS_freeList(p);
  125. return pathlist;
  126. }
  127. public unsafe static string[] GetCDROMDrives()
  128. {
  129. byte** p; // CDROM list from PhysFS dll
  130. string[] cdromlist; // List converted to an array
  131. // Get the CDROM drive listing
  132. p = PhysFS_DLL.PHYSFS_getCdRomDirs();
  133. // Convert the C-style array to a .NET style array
  134. cdromlist = PhysFS_DLL.BytePPToArray(p);
  135. // Free the original list since we're done with it
  136. PhysFS_DLL.PHYSFS_freeList(p);
  137. return cdromlist;
  138. }
  139. public static void MkDir(string Dirname)
  140. {
  141. if(PhysFS_DLL.PHYSFS_mkdir(Dirname) == 0)
  142. throw new PhysFSException();
  143. }
  144. public static void Delete(string Filename)
  145. {
  146. if(PhysFS_DLL.PHYSFS_delete(Filename) == 0)
  147. throw new PhysFSException();
  148. }
  149. public static string GetRealDir(string Filename)
  150. {
  151. string RetValue;
  152. RetValue = PhysFS_DLL.PHYSFS_getRealDir(Filename);
  153. if(RetValue == null)
  154. throw new PhysFSException("File not found in search path.");
  155. // Return the real file path of the specified filename
  156. return RetValue;
  157. }
  158. public unsafe static string[] EnumerateFiles(string Dirname)
  159. {
  160. byte** p; // File list from PhysFS dll
  161. string[] filelist; // List converted to an array
  162. // Get the CDROM drive listing
  163. p = PhysFS_DLL.PHYSFS_enumerateFiles(Dirname);
  164. // Convert the C-style array to a .NET style array
  165. filelist = PhysFS_DLL.BytePPToArray(p);
  166. // Free the original list since we're done with it
  167. PhysFS_DLL.PHYSFS_freeList(p);
  168. return filelist;
  169. }
  170. public static bool IsDirectory(string Filename)
  171. {
  172. // Return true if non-zero, otherwise return false
  173. return (PhysFS_DLL.PHYSFS_isDirectory(Filename) == 0)?false:true;
  174. }
  175. }
  176. }