stringed_interface.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Filename:- stringed_interface.cpp
  2. //
  3. // This file contains functions that StringEd wants to call to do things like load/save, they can be modified
  4. // for use ingame, but must remain functionally the same...
  5. //
  6. // Please try and put modifications for whichever games this is used for inside #defines, so I can copy the same file
  7. // into each project.
  8. //
  9. //////////////////////////////////////////////////
  10. //
  11. // stuff common to all qcommon files...
  12. #include "../server/server.h"
  13. #include "../game/q_shared.h"
  14. #include "qcommon.h"
  15. //
  16. //////////////////////////////////////////////////
  17. #pragma warning ( disable : 4511 ) // copy constructor could not be generated
  18. #pragma warning ( disable : 4512 ) // assignment operator could not be generated
  19. #pragma warning ( disable : 4663 ) // C++ language change: blah blah template crap blah blah
  20. #include "stringed_interface.h"
  21. #include "stringed_ingame.h"
  22. #include <string>
  23. using namespace std;
  24. #ifdef _STRINGED
  25. #include <stdlib.h>
  26. #include <memory.h>
  27. #include "generic.h"
  28. #endif
  29. // this just gets the binary of the file into memory, so I can parse it. Called by main SGE loader
  30. //
  31. // returns either char * of loaded file, else NULL for failed-to-open...
  32. //
  33. unsigned char *SE_LoadFileData( const char *psFileName, int *piLoadedLength /* = 0 */)
  34. {
  35. unsigned char *psReturn = NULL;
  36. if ( piLoadedLength )
  37. {
  38. *piLoadedLength = 0;
  39. }
  40. #ifdef _STRINGED
  41. if (psFileName[1] == ':')
  42. {
  43. // full-path filename...
  44. //
  45. FILE *fh = fopen( psFileName, "rb" );
  46. if (fh)
  47. {
  48. long lLength = filesize(fh);
  49. if (lLength > 0)
  50. {
  51. psReturn = (unsigned char *) malloc( lLength + 1);
  52. if (psReturn)
  53. {
  54. int iBytesRead = fread( psReturn, 1, lLength, fh );
  55. if (iBytesRead != lLength)
  56. {
  57. // error reading file!!!...
  58. //
  59. free(psReturn);
  60. psReturn = NULL;
  61. }
  62. else
  63. {
  64. psReturn[ lLength ] = '\0';
  65. if ( piLoadedLength )
  66. {
  67. *piLoadedLength = iBytesRead;
  68. }
  69. }
  70. fclose(fh);
  71. }
  72. }
  73. }
  74. }
  75. else
  76. #endif
  77. {
  78. // local filename, so prepend the base dir etc according to game and load it however (from PAK?)
  79. //
  80. unsigned char *pvLoadedData;
  81. int iLen = FS_ReadFile( psFileName, (void **)&pvLoadedData );
  82. if (iLen>0)
  83. {
  84. psReturn = pvLoadedData;
  85. if ( piLoadedLength )
  86. {
  87. *piLoadedLength = iLen;
  88. }
  89. }
  90. }
  91. return psReturn;
  92. }
  93. // called by main SGE code after loaded data has been parsedinto internal structures...
  94. //
  95. void SE_FreeFileDataAfterLoad( unsigned char *psLoadedFile )
  96. {
  97. #ifdef _STRINGED
  98. if ( psLoadedFile )
  99. {
  100. free( psLoadedFile );
  101. }
  102. #else
  103. if ( psLoadedFile )
  104. {
  105. FS_FreeFile( psLoadedFile );
  106. }
  107. #endif
  108. }
  109. #ifndef _STRINGED
  110. // quake-style method of doing things since their file-list code doesn't have a 'recursive' flag...
  111. //
  112. int giFilesFound;
  113. static void SE_R_ListFiles( const char *psExtension, const char *psDir, string &strResults )
  114. {
  115. // Com_Printf(va("Scanning Dir: %s\n",psDir));
  116. char **sysFiles, **dirFiles;
  117. int numSysFiles, i, numdirs;
  118. dirFiles = FS_ListFiles( psDir, "/", &numdirs);
  119. for (i=0;i<numdirs;i++)
  120. {
  121. if (dirFiles[i][0] && dirFiles[i][0] != '.') // skip blanks, plus ".", ".." etc
  122. {
  123. char sDirName[MAX_QPATH];
  124. sprintf(sDirName, "%s/%s", psDir, dirFiles[i]);
  125. //
  126. // for some reason the quake filesystem in this game now returns an extra slash on the end,
  127. // didn't used to. Sigh...
  128. //
  129. if (sDirName[strlen(sDirName)-1] == '/')
  130. {
  131. sDirName[strlen(sDirName)-1] = '\0';
  132. }
  133. SE_R_ListFiles( psExtension, sDirName, strResults );
  134. }
  135. }
  136. sysFiles = FS_ListFiles( psDir, psExtension, &numSysFiles );
  137. for(i=0; i<numSysFiles; i++)
  138. {
  139. char sFilename[MAX_QPATH];
  140. sprintf(sFilename,"%s/%s", psDir, sysFiles[i]);
  141. // Com_Printf("%sFound file: %s",!i?"\n":"",sFilename);
  142. strResults += sFilename;
  143. strResults += ';';
  144. giFilesFound++;
  145. // read it in...
  146. //
  147. /* byte *pbData = NULL;
  148. int iSize = FS_ReadFile( sFilename, (void **)&pbData);
  149. if (pbData)
  150. {
  151. FS_FreeFile( pbData );
  152. }
  153. */
  154. }
  155. FS_FreeFileList( sysFiles );
  156. FS_FreeFileList( dirFiles );
  157. }
  158. #endif
  159. // replace this with a call to whatever your own code equivalent is.
  160. //
  161. // expected result is a ';'-delineated string (including last one) containing file-list search results
  162. //
  163. int SE_BuildFileList( const char *psStartDir, string &strResults )
  164. {
  165. #ifndef _STRINGED
  166. giFilesFound = 0;
  167. strResults = "";
  168. SE_R_ListFiles( sSE_INGAME_FILE_EXTENSION, psStartDir, strResults );
  169. return giFilesFound;
  170. #else
  171. // .ST files...
  172. //
  173. int iFilesFound = BuildFileList( va("%s\\*%s",psStartDir, sSE_INGAME_FILE_EXTENSION), // LPCSTR psPathAndFilter,
  174. true // bool bRecurseSubDirs
  175. );
  176. extern string strResult;
  177. strResults = strResult;
  178. return iFilesFound;
  179. #endif
  180. }
  181. /////////////////////// eof ///////////////////////