]> git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/shaders.cpp
Q3map2:
[xonotic/netradiant.git] / radiant / shaders.cpp
1 /*
2    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #include "shaders.h"
23
24 #include "ifilesystem.h"
25
26 #include "stream/stringstream.h"
27
28 #include "gtkdlgs.h"
29
30 void ViewShader( const char *pFile, const char *pName ){
31         char* pBuff = 0;
32         //int nSize =
33         vfsLoadFile( pFile, reinterpret_cast<void**>( &pBuff ) );
34         if ( pBuff == 0 ) {
35                 globalErrorStream() << "Failed to load shader file " << pFile << "\n";
36                 return;
37         }
38         // look for the shader declaration
39         StringOutputStream strFind( string_length( pName ) );
40         strFind << LowerCase( pName );
41         StringOutputStream strLook( string_length( pBuff ) );
42         strLook << LowerCase( pBuff );
43         // offset used when jumping over commented out definitions
44         int length = string_length( pBuff );
45         std::size_t nOffset = 0;
46         bool startOK = false;
47         bool endOK = false;
48         while ( !startOK || !endOK ){
49                 const char* substr = strstr( strLook.c_str() + nOffset, strFind.c_str() );
50                 if ( substr == 0 ) {
51                         break;
52                 }
53                 std::size_t nStart = substr - strLook.c_str();
54                 startOK = endOK = false;
55                 if ( nStart == 0 ){
56                         startOK = true;
57                 }
58                 //validate found one...
59                 for ( const char* i = substr - 1; i > strLook.c_str(); i-- ){
60                         if( (strncmp( i, "\t", 1 ) == 0) || (strncmp( i, " ", 1 ) == 0) ){
61                                 startOK = true;
62                                 continue;
63                         }
64                         else if ( (strncmp( i, "\n", 1 ) == 0) || (strncmp( i, "\r", 1 ) == 0) ){
65                                 startOK = true;
66                                 break;
67                         }
68                         else{
69                                 startOK = false;
70                                 break;
71                         }
72                 }
73                 for ( const char* i = substr + strlen( strFind.c_str() ); i < strLook.c_str() + strlen( strLook.c_str() ); i++ ){
74                         if( (strncmp( i, "\t", 1 ) == 0) || (strncmp( i, " ", 1 ) == 0) ){
75                                 endOK = true;
76                                 continue;
77                         }
78                         else if ( (strncmp( i, "\n", 1 ) == 0) || (strncmp( i, "\r", 1 ) == 0) || (strncmp( i, "{", 1 ) == 0) || (strncmp( i, ":q3map", 6 ) == 0) ){
79                                 endOK = true;
80                                 break;
81                         }
82                         else{
83                                 endOK = false;
84                                 break;
85                         }
86                 }
87                 if( !startOK || !endOK ){
88                         nOffset = nStart + 1;
89                 }
90                 else{
91                         //globalErrorStream() << "Validated successfully" << "\n";
92                         nOffset = nStart;
93                         //fix cr+lf
94                         for ( const char* i = strLook.c_str(); i < substr ; i++ ){
95                                 if ( (strncmp( i, "\r\n", 2 ) == 0) ){
96                                 nOffset--;
97                                 }
98                         }
99                 }
100                 /*// we have found something, maybe it's a commented out shader name?
101                 char *strCheck = new char[string_length( strLook.c_str() ) + 1];
102                 strcpy( strCheck, strLook.c_str() );
103                 strCheck[nStart] = 0;
104                 char *pCheck = strrchr( strCheck, '\n' );
105                 // if there's a commentary sign in-between we'll continue
106                 if ( pCheck && strstr( pCheck, "//" ) ) {
107                         delete[] strCheck;
108                         nOffset = nStart + 1;
109                         continue;
110                 }
111                 delete[] strCheck;
112                 nOffset = nStart;
113                 break;*/
114         }
115         //fix up length
116         for ( const char* i = strLook.c_str(); i < strLook.c_str() + strlen( strLook.c_str() ) - 1; i++ ){
117                 if ( (strncmp( i, "\r\n", 2 ) == 0) ){
118                 length--;
119                 }
120         }
121         // now close the file
122         vfsFreeFile( pBuff );
123
124         DoTextEditor( pFile, static_cast<int>( nOffset ), length );
125 }