Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
6.8 KiB

  1. //===== Copyright � 2005-2006, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: build a sheet data file and a large image out of multiple images
  4. //
  5. //===========================================================================//
  6. #include "amalg_texture_parser.h"
  7. #include "tier0/platform.h"
  8. #include "tier0/progressbar.h"
  9. #include "mathlib/mathlib.h"
  10. #include "tier2/tier2.h"
  11. #include "filesystem.h"
  12. #include "tier1/utlstringmap.h"
  13. #include "tier1/strtools.h"
  14. #include "tier1/utlmap.h"
  15. #include "tier2/fileutils.h"
  16. #include "stdlib.h"
  17. #include "materialobjects/dmeamalgtexture.h"
  18. #include "tier1/utlbuffer.h"
  19. #include "tier2/utlstreambuffer.h"
  20. #include "tier0/dbg.h"
  21. static void ApplyMacros( char * in_buf )
  22. {
  23. CUtlVector<char *> Words;
  24. V_SplitString( in_buf, " ", Words);
  25. if ( ( Words.Count() == 4 ) && (! stricmp( Words[0],"ga_frame") ) )
  26. {
  27. // ga_frame frm1 frm2 n -> frame frm1{r=a},frm1{g=a},frm1{b=a},frm2{a=a} n
  28. sprintf( in_buf, "frame %s{r=0},%s{g=a},%s{b=0},%s{a=a} %s",
  29. Words[1], Words[1], Words[1], Words[2], Words[3] );
  30. }
  31. Words.PurgeAndDeleteElements();
  32. }
  33. static char *MoveToStart( char *pLineBuffer )
  34. {
  35. // Kill newline '\n'
  36. char *pChop = strchr( pLineBuffer, '\n' );
  37. if ( pChop )
  38. *pChop = 0;
  39. // Kill '//' remove comment lines.
  40. char *comment = Q_strstr( pLineBuffer, "//" );
  41. if ( comment )
  42. *comment = 0;
  43. // Move to start of non-whitespace
  44. char *in_str = pLineBuffer;
  45. while( ( in_str[0]==' ' ) || ( in_str[0]=='\t') )
  46. in_str++;
  47. return in_str;
  48. }
  49. CAmalgamatedTextureParser::CAmalgamatedTextureParser()
  50. {
  51. m_pSourceFile = NULL;
  52. m_pShtFile = NULL;
  53. m_pTgaFile = NULL;
  54. }
  55. void CAmalgamatedTextureParser::Init( const char *pSourceFileName, const char *pTgaFileName, const char *pShtFileName )
  56. {
  57. m_pSourceFile = pSourceFileName;
  58. m_pShtFile = pShtFileName;
  59. m_pTgaFile = pTgaFileName;
  60. }
  61. int CAmalgamatedTextureParser::ParsePackingMode( char *word )
  62. {
  63. // Read in the packing mode requested.
  64. int eRequestedMode = PCKM_INVALID;
  65. if ( !stricmp( word, "flat" ) || !stricmp( word, "rgba" ) )
  66. {
  67. eRequestedMode = PCKM_FLAT;
  68. }
  69. else if ( !stricmp( word, "rgb+a" ) )
  70. {
  71. eRequestedMode = PCKM_RGB_A;
  72. }
  73. if ( eRequestedMode == PCKM_INVALID )
  74. {
  75. Warning( "*** line %d: invalid packmode specified, allowed values are 'rgba' or 'rgb+a'!\n", m_NumActualLinesRead );
  76. }
  77. return eRequestedMode;
  78. }
  79. int CAmalgamatedTextureParser::ParseSequenceType( char *word )
  80. {
  81. int eMode;
  82. // Figure out the sequence type
  83. char const *szSeqType = StringAfterPrefix( word, "sequence" );
  84. if ( !stricmp( szSeqType, "" ) || !stricmp( szSeqType, "-rgba" ) )
  85. {
  86. eMode = SQM_RGBA;
  87. }
  88. else if ( !stricmp( szSeqType, "-rgb" ) )
  89. {
  90. eMode = SQM_RGB;
  91. }
  92. else if ( !stricmp( szSeqType, "-a" ) )
  93. {
  94. eMode = SQM_ALPHA;
  95. }
  96. else
  97. {
  98. Warning( "*** line %d: invalid sequence type '%s', allowed 'sequence-rgba' or 'sequence-rgb' or 'sequence-a'!\n", m_NumActualLinesRead, word ),
  99. exit( -1 );
  100. }
  101. return eMode;
  102. }
  103. void CAmalgamatedTextureParser::ParseFrameImages( CUtlVector<char *> &words, CUtlVector<char *> &outImageNames )
  104. {
  105. for ( int i = 0; i < words.Count() - 2; i++ )
  106. {
  107. char *fnamebuf = words[i+1];
  108. outImageNames.AddToTail( fnamebuf );
  109. }
  110. }
  111. bool CAmalgamatedTextureParser::ReadFile( CDmeAmalgamatedTexture &amalgTex )
  112. {
  113. // FIXME handle file not found.
  114. CRequiredInputTextFile f( m_pSourceFile );
  115. char linebuffer[4096];
  116. m_NumActualLinesRead = 0;
  117. while ( f.ReadLine( linebuffer, sizeof(linebuffer) ) )
  118. {
  119. ++m_NumActualLinesRead;
  120. char *in_str = MoveToStart( linebuffer );
  121. if ( in_str[0] == NULL )
  122. continue;
  123. strlwr( in_str ); // send string to lowercase.
  124. ApplyMacros( in_str );
  125. CUtlVector<char *> words;
  126. V_SplitString( in_str, " ", words);
  127. if ( ( words.Count() == 1) && (! stricmp( words[0],"loop" ) ) )
  128. {
  129. amalgTex.SetCurrentSequenceClamp( false );
  130. }
  131. else if ( ( words.Count() == 2 ) && (! stricmp( words[0], "packmode" ) ) )
  132. {
  133. // Read in the packing mode requested.
  134. int eRequestedMode = ParsePackingMode( words[1] );
  135. if ( eRequestedMode == PCKM_INVALID )
  136. {
  137. Msg( "Invalid packing mode." );
  138. return false;
  139. }
  140. amalgTex.SetPackingMode( eRequestedMode );
  141. }
  142. else if ( ( words.Count() == 2) && StringHasPrefix( words[0], "sequence" ) )
  143. {
  144. int seq_no = atoi( words[1] );
  145. if ( seq_no != amalgTex.GetSequenceCount() )
  146. {
  147. Warning( "Sequence number mismatch.\n" );
  148. }
  149. // Figure out the sequence type
  150. int mode = ParseSequenceType( words[0] );
  151. amalgTex.CreateNewSequence( mode );
  152. }
  153. else if ( ( words.Count() >= 3) && (! stricmp( words[0],"frame" ) ) )
  154. {
  155. if ( amalgTex.CurrentSequenceExists() )
  156. {
  157. float ftime = atof( words[ words.Count() - 1 ] );
  158. CUtlVector<char *> imageNames;
  159. ParseFrameImages( words, imageNames );
  160. amalgTex.CreateFrame( imageNames, ftime );
  161. }
  162. }
  163. else
  164. {
  165. Warning( "*** line %d: Bad command \"%s\"!\n", m_NumActualLinesRead, in_str ),
  166. exit( -1 );
  167. }
  168. words.PurgeAndDeleteElements();
  169. }
  170. return true;
  171. }
  172. bool CAmalgamatedTextureParser::ReadFileUsingBuffer( CDmeAmalgamatedTexture &amalgTex )
  173. {
  174. CUtlBuffer buf( 0, 0, CUtlBuffer::READ_ONLY | CUtlBuffer::TEXT_BUFFER );
  175. if ( !g_pFullFileSystem->ReadFile( m_pSourceFile, NULL, buf ) )
  176. {
  177. Warning( "CAmalgamatedTextureParser: Unable to open file '%s'\n", m_pSourceFile );
  178. return false;
  179. }
  180. char linebuffer[4096];
  181. m_NumActualLinesRead = 0;
  182. while ( buf.IsValid() )
  183. {
  184. buf.GetLine( linebuffer, sizeof(linebuffer) );
  185. ++m_NumActualLinesRead;
  186. char *in_str = MoveToStart( linebuffer );
  187. if ( in_str[0] == NULL )
  188. continue;
  189. strlwr( in_str ); // send string to lowercase.
  190. ApplyMacros( in_str );
  191. CUtlVector<char *> words;
  192. V_SplitString( in_str, " ", words);
  193. if ( ( words.Count() == 1) && (! stricmp( words[0],"loop" ) ) )
  194. {
  195. amalgTex.SetCurrentSequenceClamp( false );
  196. }
  197. else if ( ( words.Count() == 2 ) && (! stricmp( words[0], "packmode" ) ) )
  198. {
  199. // Read in the packing mode requested.
  200. int eRequestedMode = ParsePackingMode( words[1] );
  201. amalgTex.SetPackingMode( eRequestedMode );
  202. }
  203. else if ( ( words.Count() == 2) && StringHasPrefix( words[0], "sequence" ) )
  204. {
  205. int seq_no = atoi( words[1] );
  206. if ( seq_no != amalgTex.GetSequenceCount() )
  207. {
  208. Warning( "Sequence number mismatch.\n" );
  209. }
  210. // Figure out the sequence type
  211. int mode = ParseSequenceType( words[0] );
  212. amalgTex.CreateNewSequence( mode );
  213. }
  214. else if ( ( words.Count() >= 3) && (! stricmp( words[0],"frame" ) ) )
  215. {
  216. if ( amalgTex.CurrentSequenceExists() )
  217. {
  218. float ftime = atof( words[ words.Count() - 1 ] );
  219. CUtlVector<char *> imageNames;
  220. ParseFrameImages( words, imageNames );
  221. amalgTex.CreateFrame( imageNames, ftime );
  222. }
  223. }
  224. else
  225. {
  226. printf( "*** line %d: Bad command \"%s\"!\n", m_NumActualLinesRead, in_str ),
  227. exit( -1 );
  228. }
  229. words.PurgeAndDeleteElements();
  230. }
  231. return true;
  232. }