Debian Codegraph @

libs/picomodel/picointernal.c

raw symbols SCIP
1 /* -----------------------------------------------------------------------------
2
3 PicoModel Library
4
5 Copyright (c) 2002, Randy Reddig & seaw0lf
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without modification,
9 are permitted provided that the following conditions are met:
10
11 Redistributions of source code must retain the above copyright notice, this list
12 of conditions and the following disclaimer.
13
14 Redistributions in binary form must reproduce the above copyright notice, this
15 list of conditions and the following disclaimer in the documentation and/or
16 other materials provided with the distribution.
17
18 Neither the names of the copyright holders nor the names of its contributors may
19 be used to endorse or promote products derived from this software without
20 specific prior written permission.
21
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 ----------------------------------------------------------------------------- */
34
35 /* todo:
36 * - fix p->curLine for parser routines. increased twice
37 */
38
39 /* dependencies */
40 #include <string.h>
41 #include "picointernal.h"
42 #include "globaldefs.h"
43
44
45
46 /* function pointers */
47 void *( *_pico_ptr_malloc )( size_t ) = malloc;
48 void ( *_pico_ptr_free )( void* ) = free;
49 void ( *_pico_ptr_load_file )( const char*, unsigned char**, int* ) = NULL;
50 void ( *_pico_ptr_free_file )( void* ) = NULL;
51 int ( *_pico_ptr_save_file )( const char*, const unsigned char*, int ) = NULL;
52 void ( *_pico_ptr_print )( int, const char* ) = NULL;
53
54 typedef union
55 {
56 float f;
57 char c[4];
58 }
59 floatSwapUnion;
60
61 /* _pico_alloc:
62 * kludged memory allocation wrapper
63 */
64 void *_pico_alloc( size_t size ){
65 void *ptr;
66
67 /* some sanity checks */
68 if ( size == 0 ) {
69 return NULL;
70 }
71 if ( _pico_ptr_malloc == NULL ) {
72 return NULL;
73 }
74
75 /* allocate memory */
76 ptr = _pico_ptr_malloc( size );
77 if ( ptr == NULL ) {
78 return NULL;
79 }
80
81 /* zero out allocated memory */
82 memset( ptr,0,size );
83
84 /* return pointer to allocated memory */
85 return ptr;
86 }
87
88 /* _pico_calloc:
89 * _pico_calloc wrapper
90 */
91 void *_pico_calloc( size_t num, size_t size ){
92 void *ptr;
93
94 /* some sanity checks */
95 if ( num == 0 || size == 0 ) {
96 return NULL;
97 }
98 if ( _pico_ptr_malloc == NULL ) {
99 return NULL;
100 }
101
102 /* allocate memory */
103 ptr = _pico_ptr_malloc( num * size );
104 if ( ptr == NULL ) {
105 return NULL;
106 }
107
108 /* zero out allocated memory */
109 memset( ptr,0,num * size );
110
111 /* return pointer to allocated memory */
112 return ptr;
113 }
114
115 /* _pico_realloc:
116 * memory reallocation wrapper (note: only grows,
117 * but never shrinks or frees)
118 */
119 void *_pico_realloc( void **ptr, size_t oldSize, size_t newSize ){
120 void *ptr2;
121
122 /* sanity checks */
123 if ( ptr == NULL ) {
124 return NULL;
125 }
126 if ( newSize < oldSize ) {
127 return *ptr;
128 }
129 if ( _pico_ptr_malloc == NULL ) {
130 return NULL;
131 }
132
133 /* allocate new pointer */
134 ptr2 = _pico_alloc( newSize );
135 if ( ptr2 == NULL ) {
136 return NULL;
137 }
138
139 /* copy */
140 if ( *ptr != NULL ) {
141 memcpy( ptr2, *ptr, oldSize );
142 _pico_free( *ptr );
143 }
144
145 /* fix up and return */
146 *ptr = ptr2;
147 return *ptr;
148 }
149
150 /* _pico_clone_alloc:
151 * handy function for quick string allocation/copy. it clones
152 * the given string and returns a pointer to the new allocated
153 * clone (which must be freed by caller of course) or returns
154 * NULL on memory alloc or param errors. if 'size' is -1 the
155 * length of the input string is used, otherwise 'size' is used
156 * as custom clone size (the string is cropped to fit into mem
157 * if needed). -sea
158 */
159 char *_pico_clone_alloc( const char *str ){
160 char* cloned;
161
162 /* sanity check */
163 if ( str == NULL ) {
164 return NULL;
165 }
166
167 /* allocate memory */
168 cloned = _pico_alloc( strlen( str ) + 1 );
169 if ( cloned == NULL ) {
170 return NULL;
171 }
172
173 /* copy input string to cloned string */
174 strcpy( cloned, str );
175
176 /* return ptr to cloned string */
177 return cloned;
178 }
179
180 /* _pico_free:
181 * wrapper around the free function pointer
182 */
183 void _pico_free( void *ptr ){
184 /* sanity checks */
185 if ( ptr == NULL ) {
186 return;
187 }
188 if ( _pico_ptr_free == NULL ) {
189 return;
190 }
191
192 /* free the allocated memory */
193 _pico_ptr_free( ptr );
194 }
195
196 /* _pico_load_file:
197 * wrapper around the loadfile function pointer
198 */
199 void _pico_load_file( const char *name, unsigned char **buffer, int *bufSize ){
200 /* sanity checks */
201 if ( name == NULL ) {
202 *bufSize = -1;
203 return;
204 }
205 if ( _pico_ptr_load_file == NULL ) {
206 *bufSize = -1;
207 return;
208 }
209 /* do the actual call to read in the file; */
210 /* BUFFER IS ALLOCATED BY THE EXTERNAL LOADFILE FUNC */
211 _pico_ptr_load_file( name,buffer,bufSize );
212 }
213
214 /* _pico_free_file:
215 * wrapper around the file free function pointer
216 */
217 void _pico_free_file( void *buffer ){
218 /* sanity checks */
219 if ( buffer == NULL ) {
220 return;
221 }
222
223 /* use default free */
224 if ( _pico_ptr_free_file == NULL ) {
225 free( buffer );
226 return;
227 }
228 /* free the allocated file */
229 _pico_ptr_free_file( buffer );
230 }
231
232 /* _pico_save_file:
233 * wrapper around the savefile function pointer
234 * returns 1 on success, 0 on failure
235 */
236 int _pico_save_file( const char *name, const unsigned char *buffer, int bufSize ){
237 if ( name == NULL || buffer == NULL || bufSize <= 0 ) {
238 return 0;
239 }
240 if ( _pico_ptr_save_file == NULL ) {
241 return 0;
242 }
243 return _pico_ptr_save_file( name, buffer, bufSize );
244 }
245
246 /* _pico_printf:
247 * wrapper around the print function pointer -sea
248 */
249 void _pico_printf( int level, const char *format, ... ){
250 char str[4096];
251 va_list argptr;
252
253 /* sanity checks */
254 if ( format == NULL ) {
255 return;
256 }
257 if ( _pico_ptr_print == NULL ) {
258 return;
259 }
260
261 /* format string */
262 va_start( argptr,format );
263 vsprintf( str,format,argptr );
264 va_end( argptr );
265
266 /* remove linefeeds */
267 if ( str[ strlen( str ) - 1 ] == '\n' ) {
268 str[ strlen( str ) - 1 ] = '\0';
269 }
270
271 /* do the actual call */
272 _pico_ptr_print( level,str );
273 }
274
275 /* _pico_first_token:
276 * trims everything after the first whitespace-delimited token
277 */
278
279 void _pico_first_token( char *str ){
280 if ( !str || !*str ) {
281 return;
282 }
283 while ( *str && !isspace( *str ) )
284 str++;
285 *str = '\0';
286 }
287
288 /* _pico_strltrim:
289 * left trims the given string -sea
290 */
291 char *_pico_strltrim( char *str ){
292 char *str1 = str, *str2 = str;
293
294 while ( isspace( *str2 ) ) str2++;
295 if ( str2 != str ) {
296 while ( *str2 != '\0' ) /* fix: ydnar */
297 *str1++ = *str2++;
298 }
299 return str;
300 }
301
302 /* _pico_strrtrim:
303 * right trims the given string -sea
304 */
305 char *_pico_strrtrim( char *str ){
306 if ( str && *str ) {
307 char *str1 = str;
308 int allspace = 1;
309
310 while ( *str1 )
311 {
312 if ( allspace && !isspace( *str1 ) ) {
313 allspace = 0;
314 }
315 str1++;
316 }
317 if ( allspace ) {
318 *str = '\0';
319 }
320 else {
321 str1--;
322 while ( ( isspace( *str1 ) ) && ( str1 >= str ) )
323 *str1-- = '\0';
324 }
325 }
326 return str;
327 }
328
329 /* _pico_strlwr:
330 * pico internal string-to-lower routine.
331 */
332 char *_pico_strlwr( char *str ){
333 char *cp;
334 for ( cp = str; *cp; ++cp )
335 {
336 if ( 'A' <= *cp && *cp <= 'Z' ) {
337 *cp += ( 'a' - 'A' );
338 }
339 }
340 return str;
341 }
342
343 /* _pico_strchcount:
344 * counts how often the given char appears in str. -sea
345 */
346 int _pico_strchcount( char *str, int ch ){
347 int count = 0;
348 while ( *str++ ) if ( *str == ch ) {
349 count++;
350 }
351 return count;
352 }
353
354 void _pico_zero_bounds( picoVec3_t mins, picoVec3_t maxs ){
355 int i;
356 for ( i = 0; i < 3; i++ )
357 {
358 mins[i] = +999999;
359 maxs[i] = -999999;
360 }
361 }
362
363 void _pico_expand_bounds( picoVec3_t p, picoVec3_t mins, picoVec3_t maxs ){
364 int i;
365 for ( i = 0; i < 3; i++ )
366 {
367 float value = p[i];
368 if ( value < mins[i] ) {
369 mins[i] = value;
370 }
371 if ( value > maxs[i] ) {
372 maxs[i] = value;
373 }
374 }
375 }
376
377 void _pico_zero_vec( picoVec3_t vec ){
378 vec[ 0 ] = vec[ 1 ] = vec[ 2 ] = 0;
379 }
380
381 void _pico_zero_vec2( picoVec2_t vec ){
382 vec[ 0 ] = vec[ 1 ] = 0;
383 }
384
385 void _pico_zero_vec4( picoVec4_t vec ){
386 vec[ 0 ] = vec[ 1 ] = vec[ 2 ] = vec[ 3 ] = 0;
387 }
388
389 void _pico_set_vec( picoVec3_t v, float a, float b, float c ){
390 v[ 0 ] = a;
391 v[ 1 ] = b;
392 v[ 2 ] = c;
393 }
394
395 void _pico_set_vec4( picoVec4_t v, float a, float b, float c, float d ){
396 v[ 0 ] = a;
397 v[ 1 ] = b;
398 v[ 2 ] = c;
399 v[ 3 ] = d;
400 }
401
402 void _pico_copy_vec( picoVec3_t src, picoVec3_t dest ){
403 dest[ 0 ] = src[ 0 ];
404 dest[ 1 ] = src[ 1 ];
405 dest[ 2 ] = src[ 2 ];
406 }
407
408 void _pico_copy_vec2( picoVec2_t src, picoVec2_t dest ){
409 dest[ 0 ] = src[ 0 ];
410 dest[ 1 ] = src[ 1 ];
411 }
412
413 void _pico_copy_vec4( picoVec4_t src, picoVec4_t dest ){
414 dest[ 0 ] = src[ 0 ];
415 dest[ 1 ] = src[ 1 ];
416 dest[ 2 ] = src[ 2 ];
417 dest[ 3 ] = src[ 3 ];
418 }
419
420 /* ydnar */
421 picoVec_t _pico_normalize_vec( picoVec3_t vec ){
422 double len, ilen;
423
424 len = sqrt( vec[ 0 ] * vec[ 0 ] + vec[ 1 ] * vec[ 1 ] + vec[ 2 ] * vec[ 2 ] );
425 if ( len == 0.0 ) {
426 return 0.0;
427 }
428 ilen = 1.0 / len;
429 vec[ 0 ] *= (picoVec_t) ilen;
430 vec[ 1 ] *= (picoVec_t) ilen;
431 vec[ 2 ] *= (picoVec_t) ilen;
432 return (picoVec_t) len;
433 }
434
435 void _pico_add_vec( picoVec3_t a, picoVec3_t b, picoVec3_t dest ){
436 dest[ 0 ] = a[ 0 ] + b[ 0 ];
437 dest[ 1 ] = a[ 1 ] + b[ 1 ];
438 dest[ 2 ] = a[ 2 ] + b[ 2 ];
439 }
440
441 void _pico_subtract_vec( picoVec3_t a, picoVec3_t b, picoVec3_t dest ){
442 dest[ 0 ] = a[ 0 ] - b[ 0 ];
443 dest[ 1 ] = a[ 1 ] - b[ 1 ];
444 dest[ 2 ] = a[ 2 ] - b[ 2 ];
445 }
446
447 void _pico_scale_vec( picoVec3_t v, float scale, picoVec3_t dest ){
448 dest[ 0 ] = v[ 0 ] * scale;
449 dest[ 1 ] = v[ 1 ] * scale;
450 dest[ 2 ] = v[ 2 ] * scale;
451 }
452
453 void _pico_scale_vec4( picoVec4_t v, float scale, picoVec4_t dest ){
454 dest[ 0 ] = v[ 0 ] * scale;
455 dest[ 1 ] = v[ 1 ] * scale;
456 dest[ 2 ] = v[ 2 ] * scale;
457 dest[ 3 ] = v[ 3 ] * scale;
458 }
459
460 picoVec_t _pico_dot_vec( picoVec3_t a, picoVec3_t b ){
461 return a[ 0 ] * b[ 0 ] + a[ 1 ] * b[ 1 ] + a[ 2 ] * b[ 2 ];
462 }
463
464 void _pico_cross_vec( picoVec3_t a, picoVec3_t b, picoVec3_t dest ){
465 dest[ 0 ] = a[ 1 ] * b[ 2 ] - a[ 2 ] * b[ 1 ];
466 dest[ 1 ] = a[ 2 ] * b[ 0 ] - a[ 0 ] * b[ 2 ];
467 dest[ 2 ] = a[ 0 ] * b[ 1 ] - a[ 1 ] * b[ 0 ];
468 }
469
470 picoVec_t _pico_calc_plane( picoVec4_t plane, picoVec3_t a, picoVec3_t b, picoVec3_t c ){
471 picoVec3_t ba, ca;
472
473 _pico_subtract_vec( b, a, ba );
474 _pico_subtract_vec( c, a, ca );
475 _pico_cross_vec( ca, ba, plane );
476 plane[ 3 ] = _pico_dot_vec( a, plane );
477 return _pico_normalize_vec( plane );
478 }
479
480 const picoColor_t picoColor_white = { 255, 255, 255, 255 };
481
482 /* separate from _pico_set_vec4 */
483 void _pico_set_color( picoColor_t c, int r, int g, int b, int a ){
484 c[ 0 ] = r;
485 c[ 1 ] = g;
486 c[ 2 ] = b;
487 c[ 3 ] = a;
488 }
489
490 void _pico_copy_color( const picoColor_t src, picoColor_t dest ){
491 dest[ 0 ] = src[ 0 ];
492 dest[ 1 ] = src[ 1 ];
493 dest[ 2 ] = src[ 2 ];
494 dest[ 3 ] = src[ 3 ];
495 }
496
497 #if GDEF_ARCH_ENDIAN_BIG
498
499 int _pico_big_long( int src ) { return src; }
500 short _pico_big_short( short src ) { return src; }
501 float _pico_big_float( float src ) { return src; }
502
503 int _pico_little_long( int src ){
504 return ( ( src & 0xFF000000 ) >> 24 ) |
505 ( ( src & 0x00FF0000 ) >> 8 ) |
506 ( ( src & 0x0000FF00 ) << 8 ) |
507 ( ( src & 0x000000FF ) << 24 );
508 }
509
510 short _pico_little_short( short src ){
511 return ( ( src & 0xFF00 ) >> 8 ) |
512 ( ( src & 0x00FF ) << 8 );
513 }
514
515 float _pico_little_float( float src ){
516 floatSwapUnion in,out;
517 in.f = src;
518 out.c[ 0 ] = in.c[ 3 ];
519 out.c[ 1 ] = in.c[ 2 ];
520 out.c[ 2 ] = in.c[ 1 ];
521 out.c[ 3 ] = in.c[ 0 ];
522 return out.f;
523 }
524 #else /*__BIG_ENDIAN__*/
525
526 int _pico_little_long( int src ) { return src; }
527 short _pico_little_short( short src ) { return src; }
528 float _pico_little_float( float src ) { return src; }
529
530 int _pico_big_long( int src ){
531 return ( ( src & 0xFF000000 ) >> 24 ) |
532 ( ( src & 0x00FF0000 ) >> 8 ) |
533 ( ( src & 0x0000FF00 ) << 8 ) |
534 ( ( src & 0x000000FF ) << 24 );
535 }
536
537 short _pico_big_short( short src ){
538 return ( ( src & 0xFF00 ) >> 8 ) |
539 ( ( src & 0x00FF ) << 8 );
540 }
541
542 float _pico_big_float( float src ){
543 floatSwapUnion in,out;
544 in.f = src;
545 out.c[ 0 ] = in.c[ 3 ];
546 out.c[ 1 ] = in.c[ 2 ];
547 out.c[ 2 ] = in.c[ 1 ];
548 out.c[ 3 ] = in.c[ 0 ];
549 return out.f;
550 }
551 #endif /*__BIG_ENDIAN__*/
552
553 /* _pico_stristr:
554 * case-insensitive strstr. -sea
555 */
556 const char *_pico_stristr( const char *str, const char *substr ){
557 const size_t sublen = strlen( substr );
558 while ( *str )
559 {
560 if ( !_pico_strnicmp( str,substr,sublen ) ) {
561 break;
562 }
563 str++;
564 }
565 if ( !( *str ) ) {
566 str = NULL;
567 }
568 return str;
569 }
570
571 /*
572 _pico_unixify()
573 changes dos \ style path separators to /
574 */
575
576 void _pico_unixify( char *path ){
577 if ( path == NULL ) {
578 return;
579 }
580 while ( *path )
581 {
582 if ( *path == '\\' ) {
583 *path = '/';
584 }
585 path++;
586 }
587 }
588
589 /* _pico_nofname:
590 * removes file name portion from given file path and converts
591 * the directory separators to un*x style. returns 1 on success
592 * or 0 when 'destSize' was exceeded. -sea
593 */
594 int _pico_nofname( const char *path, char *dest, int destSize ){
595 int left = destSize;
596 char *temp = dest;
597
598 while ( ( *dest = *path ) != '\0' )
599 {
600 if ( *dest == '/' || *dest == '\\' ) {
601 temp = ( dest + 1 );
602 *dest = '/';
603 }
604 dest++; path++;
605
606 if ( --left < 1 ) {
607 *temp = '\0';
608 return 0;
609 }
610 }
611 *temp = '\0';
612 return 1;
613 }
614
615 /* _pico_nopath:
616 * returns ptr to filename portion in given path or an empty
617 * string otherwise. given 'path' is not altered. -sea
618 */
619 const char *_pico_nopath( const char *path ){
620 if ( path == NULL ) {
621 return "";
622 }
623 const char *src;
624 src = path + ( strlen( path ) - 1 );
625
626 if ( !strchr( path,'/' ) && !strchr( path,'\\' ) ) {
627 return ( path );
628 }
629
630 while ( ( src-- ) != path )
631 {
632 if ( *src == '/' || *src == '\\' ) {
633 return ( ++src );
634 }
635 }
636 return "";
637 }
638
639 /* _pico_setfext:
640 * sets/changes the file extension for the given filename
641 * or filepath's filename portion. the given 'path' *is*
642 * altered. leave 'ext' empty to remove extension. -sea
643 */
644 char *_pico_setfext( char *path, const char *ext ){
645 char *src;
646 int remfext = 0;
647
648 src = path + ( strlen( path ) - 1 );
649
650 if ( ext == NULL ) {
651 ext = "";
652 }
653 if ( strlen( ext ) < 1 ) {
654 remfext = 1;
655 }
656 if ( strlen( path ) < 1 ) {
657 return path;
658 }
659
660 while ( ( src-- ) != path )
661 {
662 if ( *src == '/' || *src == '\\' ) {
663 return path;
664 }
665
666 if ( *src == '.' ) {
667 if ( remfext ) {
668 *src = '\0';
669 return path;
670 }
671 *( ++src ) = '\0';
672 break;
673 }
674 }
675 strcat( path,ext );
676 return path;
677 }
678
679 /* _pico_getline:
680 * extracts one line from the given buffer and stores it in dest.
681 * returns -1 on error or the length of the line on success. i've
682 * removed string trimming here. this can be done manually by the
683 * calling func.
684 */
685 int _pico_getline( char *buf, int bufsize, char *dest, int destsize ){
686 int pos;
687
688 /* check output */
689 if ( dest == NULL || destsize < 1 ) {
690 return -1;
691 }
692 memset( dest,0,destsize );
693
694 /* check input */
695 if ( buf == NULL || bufsize < 1 ) {
696 return -1;
697 }
698
699 /* get next line */
700 for ( pos = 0; pos < bufsize && pos < destsize; pos++ )
701 {
702 if ( buf[pos] == '\n' ) {
703 pos++; break;
704 }
705 dest[pos] = buf[pos];
706 }
707 /* terminate dest and return */
708 dest[pos] = '\0';
709 return pos;
710 }
711
712 /* expecting fileName to be relative vfs model path */
713 void _pico_deduce_shadername( const char* fileName, const char* srcName, picoShader_t* shader ){
714 if( srcName == NULL || fileName == NULL )
715 return;
716 char name[strlen( srcName ) + 1];
717 strcpy( name, srcName );
718 _pico_unixify( name );
719 _pico_setfext( name, NULL );
720
721 char path[strlen( fileName ) + strlen( name ) + 1];
722 _pico_nofname( fileName, path, strlen( fileName ) + strlen( name ) + 1 );
723 _pico_unixify( path );
724
725 if( !strchr( name , '/' ) ){ /* texture is likely in the folder, where model is */
726 strcat( path, name );
727 }
728 else if( name[0] == '/' || ( name[0] != '\0' && name[1] == ':' ) || strstr( name, ".." ) ){ /* absolute path or with .. */
729 const char* p = name;
730 for (; *p != '\0'; ++p )
731 if ( _pico_strnicmp( p, "/models/", 8 ) == 0 || _pico_strnicmp( p, "/textures/", 10 ) == 0 )
732 break;
733 if( *p != '\0' ){
734 strcpy( path, p + 1 );
735 }
736 else{
737 p = _pico_nopath( name );
738 strcat( path, p );
739 }
740 }
741 else{
742 PicoSetShaderName( shader, name );
743 return;
744 }
745
746 _pico_printf( PICO_NORMAL, "PICO: substituting shader name: %s -> %s", srcName, path );
747 PicoSetShaderName( shader, path );
748 }
749
750 /* deduce shadernames from bitmap or shadername paths */
751 void _pico_deduce_shadernames( picoModel_t *model ){
752 for ( int i = 0; i < model->numShaders; ++i ){
753 /* skip null shaders */
754 if ( model->shader[i] == NULL )
755 continue;
756
757 const char* mapname = model->shader[i]->mapName;
758 const char* shadername = model->shader[i]->name;
759
760 /* Detect intentional material name to not replace it with texture name.
761
762 Reimplement commits by Garux:
763 https://github.com/Garux/netradiant-custom/commit/1bd3e7ae186b55fb61e3738d2493432c0b1f5a7b
764 https://github.com/Garux/netradiant-custom/commit/ea21eee2254fb2e667732d8f1b0f83c439a89bfa
765
766 This attempts to restore proper material behaviour when the mapper knows what he is doing,
767 also called Julius' case or correct case because Julius is always correctâ„¢
768 while keeping the fallback for other situations, also called newbie's case
769 which may be compatible with third-party tools not following Quake 3 conventions.
770
771 See: https://gitlab.com/xonotic/netradiant/-/merge_requests/179#note_777324051 */
772 if ( shadername && *shadername &&
773 ( _pico_strnicmp( shadername, "models/", 7 ) == 0
774 || _pico_strnicmp( shadername, "models\\", 7 ) == 0
775 || _pico_strnicmp( shadername, "textures/", 9 ) == 0
776 || _pico_strnicmp( shadername, "textures\\", 9 ) == 0 ) )
777 {
778 _pico_deduce_shadername( model->fileName, shadername, model->shader[i] );
779 }
780 else if( mapname && *mapname )
781 _pico_deduce_shadername( model->fileName, mapname, model->shader[i] );
782 else if( shadername && *shadername )
783 _pico_deduce_shadername( model->fileName, shadername, model->shader[i] );
784 }
785 }
786
787 /* _pico_parse_skip_white:
788 * skips white spaces in current pico parser, sets *hasLFs
789 * to 1 if linefeeds were skipped, and either returns the
790 * parser's cursor pointer or NULL on error. -sea
791 */
792 void _pico_parse_skip_white( picoParser_t *p, int *hasLFs ){
793 /* sanity checks */
794 if ( p == NULL || p->cursor == NULL ) {
795 return;
796 }
797
798 /* skin white spaces */
799 while ( 1 )
800 {
801 /* sanity checks */
802 if ( p->cursor < p->buffer ||
803 p->cursor >= p->max ) {
804 return;
805 }
806 /* break for chars other than white spaces */
807 if ( *p->cursor > 0x20 ) {
808 break;
809 }
810 if ( *p->cursor == 0x00 ) {
811 return;
812 }
813
814 /* a bit of linefeed handling */
815 if ( *p->cursor == '\n' ) {
816 *hasLFs = 1;
817 p->curLine++;
818 }
819 /* go to next character */
820 p->cursor++;
821 }
822 }
823
824 /* _pico_new_parser:
825 * allocates a new ascii parser object.
826 */
827 picoParser_t *_pico_new_parser( const picoByte_t *buffer, int bufSize ){
828 picoParser_t *p;
829
830 /* sanity check */
831 if ( buffer == NULL || bufSize <= 0 ) {
832 return NULL;
833 }
834
835 /* allocate reader */
836 p = _pico_alloc( sizeof( picoParser_t ) );
837 if ( p == NULL ) {
838 return NULL;
839 }
840 memset( p,0,sizeof( picoParser_t ) );
841
842 /* allocate token space */
843 p->tokenSize = 0;
844 p->tokenMax = 1024;
845 p->token = _pico_alloc( p->tokenMax );
846 if ( p->token == NULL ) {
847 _pico_free( p );
848 return NULL;
849 }
850 /* setup */
851 p->buffer = (const char *) buffer;
852 p->cursor = p->buffer;
853 p->bufSize = bufSize;
854 p->max = p->buffer + bufSize;
855 p->curLine = 1; /* sea: new */
856
857 /* return ptr to parser */
858 return p;
859 }
860
861 /* _pico_free_parser:
862 * frees an existing pico parser object.
863 */
864 void _pico_free_parser( picoParser_t *p ){
865 /* sanity check */
866 if ( p == NULL ) {
867 return;
868 }
869
870 /* free the parser */
871 if ( p->token != NULL ) {
872 _pico_free( p->token );
873 }
874 _pico_free( p );
875 }
876
877 /* _pico_parse_ex:
878 * reads the next token from given pico parser object. if param
879 * 'allowLFs' is 1 it will read beyond linefeeds and return 0 when
880 * the EOF is reached. if 'allowLFs' is 0 it will return 0 when
881 * the EOL is reached. if 'handleQuoted' is 1 the parser function
882 * will handle "quoted" strings and return the data between the
883 * quotes as token. returns 0 on end/error or 1 on success. -sea
884 */
885 int _pico_parse_ex( picoParser_t *p, int allowLFs, int handleQuoted ){
886 int hasLFs = 0;
887 const char *old;
888
889 /* sanity checks */
890 if ( p == NULL || p->buffer == NULL ||
891 p->cursor < p->buffer ||
892 p->cursor >= p->max ) {
893 return 0;
894 }
895 /* clear parser token */
896 p->tokenSize = 0;
897 p->token[ 0 ] = '\0';
898 old = p->cursor;
899
900 /* skip whitespaces */
901 while ( p->cursor < p->max && *p->cursor <= 32 )
902 {
903 if ( *p->cursor == '\n' ) {
904 p->curLine++;
905 hasLFs++;
906 }
907 p->cursor++;
908 }
909 /* return if we're not allowed to go beyond lfs */
910 if ( ( hasLFs > 0 ) && !allowLFs ) {
911 p->cursor = old;
912 return 0;
913 }
914 /* get next quoted string */
915 if ( *p->cursor == '\"' && handleQuoted ) {
916 p->cursor++;
917 while ( p->cursor < p->max && *p->cursor )
918 {
919 if ( *p->cursor == '\\' ) {
920 if ( *( p->cursor + 1 ) == '"' ) {
921 p->cursor++;
922 }
923 p->token[ p->tokenSize++ ] = *p->cursor++;
924 continue;
925 }
926 else if ( *p->cursor == '\"' ) {
927 p->cursor++;
928 break;
929 }
930 else if ( *p->cursor == '\n' ) {
931 p->curLine++;
932 }
933 p->token[ p->tokenSize++ ] = *p->cursor++;
934 }
935 /* terminate token */
936 p->token[ p->tokenSize ] = '\0';
937 return 1;
938 }
939 /* otherwise get next word */
940 while ( p->cursor < p->max && *p->cursor > 32 )
941 {
942 if ( *p->cursor == '\n' ) {
943 p->curLine++;
944 }
945 p->token[ p->tokenSize++ ] = *p->cursor++;
946 }
947 /* terminate token */
948 p->token[ p->tokenSize ] = '\0';
949 return 1;
950 }
951
952 /* _pico_parse_first:
953 * reads the first token from the next line and returns
954 * a pointer to it. returns NULL on EOL or EOF. -sea
955 */
956 char *_pico_parse_first( picoParser_t *p ){
957 /* sanity check */
958 if ( p == NULL ) {
959 return NULL;
960 }
961
962 /* try to read next token (with lfs & quots) */
963 if ( !_pico_parse_ex( p,1,1 ) ) {
964 return NULL;
965 }
966
967 /* return ptr to the token string */
968 return p->token;
969 }
970
971 /* _pico_parse:
972 * reads the next token from the parser and returns a pointer
973 * to it. quoted strings are handled as usual. returns NULL
974 * on EOL or EOF. -sea
975 */
976 char *_pico_parse( picoParser_t *p, int allowLFs ){
977 /* sanity check */
978 if ( p == NULL ) {
979 return NULL;
980 }
981
982 /* try to read next token (with quots) */
983 if ( !_pico_parse_ex( p,allowLFs,1 ) ) {
984 return NULL;
985 }
986
987 /* return ptr to the token string */
988 return p->token;
989 }
990
991 /* _pico_parse_skip_rest:
992 * skips the rest of the current line in parser.
993 */
994 void _pico_parse_skip_rest( picoParser_t *p ){
995 while ( _pico_parse_ex( p,0,0 ) ) ;
996 }
997
998 /* _pico_parse_skip_braced:
999 * parses/skips over a braced section. returns 1 on success
1000 * or 0 on error (when there was no closing bracket and the
1001 * end of buffer was reached or when the opening bracket was
1002 * missing).
1003 */
1004 int _pico_parse_skip_braced( picoParser_t *p ){
1005 int firstToken = 1;
1006 int level;
1007
1008 /* sanity check */
1009 if ( p == NULL ) {
1010 return 0;
1011 }
1012
1013 /* set the initial level for parsing */
1014 level = 0;
1015
1016 /* skip braced section */
1017 while ( 1 )
1018 {
1019 /* read next token (lfs allowed) */
1020 if ( !_pico_parse_ex( p,1,1 ) ) {
1021 /* end of parser buffer reached */
1022 return 0;
1023 }
1024 /* first token must be an opening bracket */
1025 if ( firstToken && p->token[0] != '{' ) {
1026 /* opening bracket missing */
1027 return 0;
1028 }
1029 /* we only check this once */
1030 firstToken = 0;
1031
1032 /* update level */
1033 if ( p->token[1] == '\0' ) {
1034 if ( p->token[0] == '{' ) {
1035 level++;
1036 }
1037 if ( p->token[0] == '}' ) {
1038 level--;
1039 }
1040 }
1041 /* break if we're back at our starting level */
1042 if ( level == 0 ) {
1043 break;
1044 }
1045 }
1046 /* successfully skipped braced section */
1047 return 1;
1048 }
1049
1050 int _pico_parse_check( picoParser_t *p, int allowLFs, char *str ){
1051 if ( !_pico_parse_ex( p,allowLFs,1 ) ) {
1052 return 0;
1053 }
1054 if ( !strcmp( p->token,str ) ) {
1055 return 1;
1056 }
1057 return 0;
1058 }
1059
1060 int _pico_parse_checki( picoParser_t *p, int allowLFs, char *str ){
1061 if ( !_pico_parse_ex( p,allowLFs,1 ) ) {
1062 return 0;
1063 }
1064 if ( !_pico_stricmp( p->token,str ) ) {
1065 return 1;
1066 }
1067 return 0;
1068 }
1069
1070 int _pico_parse_int( picoParser_t *p, int *out ){
1071 char *token;
1072
1073 /* sanity checks */
1074 if ( p == NULL || out == NULL ) {
1075 return 0;
1076 }
1077
1078 /* get token and turn it into an integer */
1079 *out = 0;
1080 token = _pico_parse( p,0 );
1081 if ( token == NULL ) {
1082 return 0;
1083 }
1084 *out = atoi( token );
1085
1086 /* success */
1087 return 1;
1088 }
1089
1090 int _pico_parse_int_def( picoParser_t *p, int *out, int def ){
1091 char *token;
1092
1093 /* sanity checks */
1094 if ( p == NULL || out == NULL ) {
1095 return 0;
1096 }
1097
1098 /* get token and turn it into an integer */
1099 *out = def;
1100 token = _pico_parse( p,0 );
1101 if ( token == NULL ) {
1102 return 0;
1103 }
1104 *out = atoi( token );
1105
1106 /* success */
1107 return 1;
1108 }
1109
1110 int _pico_parse_float( picoParser_t *p, float *out ){
1111 char *token;
1112
1113 /* sanity checks */
1114 if ( p == NULL || out == NULL ) {
1115 return 0;
1116 }
1117
1118 /* get token and turn it into a float */
1119 *out = 0.0f;
1120 token = _pico_parse( p,0 );
1121 if ( token == NULL ) {
1122 return 0;
1123 }
1124 *out = (float) atof( token );
1125
1126 /* success */
1127 return 1;
1128 }
1129
1130 int _pico_parse_float_def( picoParser_t *p, float *out, float def ){
1131 char *token;
1132
1133 /* sanity checks */
1134 if ( p == NULL || out == NULL ) {
1135 return 0;
1136 }
1137
1138 /* get token and turn it into a float */
1139 *out = def;
1140 token = _pico_parse( p,0 );
1141 if ( token == NULL ) {
1142 return 0;
1143 }
1144 *out = (float) atof( token );
1145
1146 /* success */
1147 return 1;
1148 }
1149
1150 int _pico_parse_vec( picoParser_t *p, picoVec3_t out ){
1151 char *token;
1152 int i;
1153
1154 /* sanity checks */
1155 if ( p == NULL || out == NULL ) {
1156 return 0;
1157 }
1158
1159 /* zero out outination vector */
1160 _pico_zero_vec( out );
1161
1162 /* parse three vector components */
1163 for ( i = 0; i < 3; i++ )
1164 {
1165 token = _pico_parse( p,0 );
1166 if ( token == NULL ) {
1167 _pico_zero_vec( out );
1168 return 0;
1169 }
1170 out[ i ] = (float) atof( token );
1171 }
1172 /* success */
1173 return 1;
1174 }
1175
1176 int _pico_parse_vec_def( picoParser_t *p, picoVec3_t out, picoVec3_t def ){
1177 char *token;
1178 int i;
1179
1180 /* sanity checks */
1181 if ( p == NULL || out == NULL ) {
1182 return 0;
1183 }
1184
1185 /* assign default vector value */
1186 _pico_copy_vec( def,out );
1187
1188 /* parse three vector components */
1189 for ( i = 0; i < 3; i++ )
1190 {
1191 token = _pico_parse( p,0 );
1192 if ( token == NULL ) {
1193 _pico_copy_vec( def,out );
1194 return 0;
1195 }
1196 out[ i ] = (float) atof( token );
1197 }
1198 /* success */
1199 return 1;
1200 }
1201
1202 int _pico_parse_vec2( picoParser_t *p, picoVec2_t out ){
1203 char *token;
1204 int i;
1205
1206 /* sanity checks */
1207 if ( p == NULL || out == NULL ) {
1208 return 0;
1209 }
1210
1211 /* zero out outination vector */
1212 _pico_zero_vec2( out );
1213
1214 /* parse two vector components */
1215 for ( i = 0; i < 2; i++ )
1216 {
1217 token = _pico_parse( p,0 );
1218 if ( token == NULL ) {
1219 _pico_zero_vec2( out );
1220 return 0;
1221 }
1222 out[ i ] = (float) atof( token );
1223 }
1224 /* success */
1225 return 1;
1226 }
1227
1228 int _pico_parse_vec2_def( picoParser_t *p, picoVec2_t out, picoVec2_t def ){
1229 char *token;
1230 int i;
1231
1232 /* sanity checks */
1233 if ( p == NULL || out == NULL ) {
1234 return 0;
1235 }
1236
1237 /* assign default vector value */
1238 _pico_copy_vec2( def,out );
1239
1240 /* parse two vector components */
1241 for ( i = 0; i < 2; i++ )
1242 {
1243 token = _pico_parse( p,0 );
1244 if ( token == NULL ) {
1245 _pico_copy_vec2( def,out );
1246 return 0;
1247 }
1248 out[ i ] = (float) atof( token );
1249 }
1250 /* success */
1251 return 1;
1252 }
1253
1254 int _pico_parse_vec4( picoParser_t *p, picoVec4_t out ){
1255 char *token;
1256 int i;
1257
1258 /* sanity checks */
1259 if ( p == NULL || out == NULL ) {
1260 return 0;
1261 }
1262
1263 /* zero out outination vector */
1264 _pico_zero_vec4( out );
1265
1266 /* parse four vector components */
1267 for ( i = 0; i < 4; i++ )
1268 {
1269 token = _pico_parse( p,0 );
1270 if ( token == NULL ) {
1271 _pico_zero_vec4( out );
1272 return 0;
1273 }
1274 out[ i ] = (float) atof( token );
1275 }
1276 /* success */
1277 return 1;
1278 }
1279
1280 int _pico_parse_vec4_def( picoParser_t *p, picoVec4_t out, picoVec4_t def ){
1281 char *token;
1282 int i;
1283
1284 /* sanity checks */
1285 if ( p == NULL || out == NULL ) {
1286 return 0;
1287 }
1288
1289 /* assign default vector value */
1290 _pico_copy_vec4( def,out );
1291
1292 /* parse four vector components */
1293 for ( i = 0; i < 4; i++ )
1294 {
1295 token = _pico_parse( p,0 );
1296 if ( token == NULL ) {
1297 _pico_copy_vec4( def,out );
1298 return 0;
1299 }
1300 out[ i ] = (float) atof( token );
1301 }
1302 /* success */
1303 return 1;
1304 }
1305
1306 /* _pico_new_memstream:
1307 * allocates a new memorystream object.
1308 */
1309 picoMemStream_t *_pico_new_memstream( const picoByte_t *buffer, int bufSize ){
1310 picoMemStream_t *s;
1311
1312 /* sanity check */
1313 if ( buffer == NULL || bufSize <= 0 ) {
1314 return NULL;
1315 }
1316
1317 /* allocate stream */
1318 s = _pico_alloc( sizeof( picoMemStream_t ) );
1319 if ( s == NULL ) {
1320 return NULL;
1321 }
1322 memset( s,0,sizeof( picoMemStream_t ) );
1323
1324 /* setup */
1325 s->buffer = buffer;
1326 s->curPos = buffer;
1327 s->bufSize = bufSize;
1328 s->flag = 0;
1329
1330 /* return ptr to stream */
1331 return s;
1332 }
1333
1334 /* _pico_free_memstream:
1335 * frees an existing pico memorystream object.
1336 */
1337 void _pico_free_memstream( picoMemStream_t *s ){
1338 /* sanity check */
1339 if ( s == NULL ) {
1340 return;
1341 }
1342
1343 /* free the stream */
1344 _pico_free( s );
1345 }
1346
1347 /* _pico_memstream_read:
1348 * reads data from a pico memorystream into a buffer.
1349 */
1350 int _pico_memstream_read( picoMemStream_t *s, void *buffer, int len ){
1351 int ret = 1;
1352
1353 /* sanity checks */
1354 if ( s == NULL || buffer == NULL ) {
1355 return 0;
1356 }
1357
1358 if ( s->curPos + len > s->buffer + s->bufSize ) {
1359 s->flag |= PICO_IOEOF;
1360 len = s->buffer + s->bufSize - s->curPos;
1361 ret = 0;
1362 }
1363
1364 /* read the data */
1365 memcpy( buffer, s->curPos, len );
1366 s->curPos += len;
1367 return ret;
1368 }
1369
1370 /* _pico_memstream_read:
1371 * reads a character from a pico memorystream
1372 */
1373 int _pico_memstream_getc( picoMemStream_t *s ){
1374 int c = 0;
1375
1376 /* sanity check */
1377 if ( s == NULL ) {
1378 return -1;
1379 }
1380
1381 /* read the character */
1382 if ( _pico_memstream_read( s, &c, 1 ) == 0 ) {
1383 return -1;
1384 }
1385
1386 return c;
1387 }
1388
1389 /* _pico_memstream_seek:
1390 * sets the current read position to a different location
1391 */
1392 int _pico_memstream_seek( picoMemStream_t *s, long offset, int origin ){
1393 int overflow;
1394
1395 /* sanity check */
1396 if ( s == NULL ) {
1397 return -1;
1398 }
1399
1400 if ( origin == PICO_SEEK_SET ) {
1401 s->curPos = s->buffer + offset;
1402 overflow = s->curPos - ( s->buffer + s->bufSize );
1403 if ( overflow > 0 ) {
1404 s->curPos = s->buffer + s->bufSize;
1405 return offset - overflow;
1406 }
1407 return 0;
1408 }
1409 else if ( origin == PICO_SEEK_CUR ) {
1410 s->curPos += offset;
1411 overflow = s->curPos - ( s->buffer + s->bufSize );
1412 if ( overflow > 0 ) {
1413 s->curPos = s->buffer + s->bufSize;
1414 return offset - overflow;
1415 }
1416 return 0;
1417 }
1418 else if ( origin == PICO_SEEK_END ) {
1419 s->curPos = ( s->buffer + s->bufSize ) - offset;
1420 overflow = s->buffer - s->curPos;
1421 if ( overflow > 0 ) {
1422 s->curPos = s->buffer;
1423 return offset - overflow;
1424 }
1425 return 0;
1426 }
1427
1428 return -1;
1429 }
1430
1431 /* _pico_memstream_tell:
1432 * returns the current read position in the pico memorystream
1433 */
1434 long _pico_memstream_tell( picoMemStream_t *s ){
1435 /* sanity check */
1436 if ( s == NULL ) {
1437 return -1;
1438 }
1439
1440 return s->curPos - s->buffer;
1441 }