Skip to content

Commit

Permalink
handle inline {} and multiple spaces in array
Browse files Browse the repository at this point in the history
  • Loading branch information
chapulina committed May 25, 2018
1 parent 9f160f7 commit fdfda01
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
21 changes: 18 additions & 3 deletions gz3d/src/gzogre2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ GZ3D.Ogre2Json.prototype.Parse = function(_str)
{
var str = _str;

// { and } in new lines
str = str.replace(/{|}/gm, '\r\n$&\r\n');

// Remove leading and trailing whitespaces per line
str = str.replace(/^\s+/gm,'');
str = str.replace(/\s+$/gm,'');
str = str.replace(/^\s+/gm, '');
str = str.replace(/\s+$/gm, '');

// Remove "material " and properly add commas if more than one
str = str.replace(/^material /gm, function(match, offset)
Expand Down Expand Up @@ -100,6 +103,18 @@ GZ3D.Ogre2Json.prototype.Parse = function(_str)
return 'texture_unit';
});

// Strip name from named pass
str = str.replace(/^pass.*$/gm, function(match, offset)
{
return 'pass';
});

// Strip name from named technique
str = str.replace(/^technique.*$/gm, function(match, offset)
{
return 'technique';
});

// Remove comments
str = str.replace(/(\/\*[\w\'\s\r\n\*]*\*\/)|(\/\/.*$)/gm, '');

Expand All @@ -113,7 +128,7 @@ GZ3D.Ogre2Json.prototype.Parse = function(_str)
// If line has more than one space, it has an array
str = str.replace(/(.* .*){2,}/g, function(match)
{
var parts = match.split(' ');
var parts = match.split(/\s+/g);

var res = parts[0] + ' [';
for (var i = 1; i < (parts.length - 1); i++)
Expand Down
11 changes: 4 additions & 7 deletions gz3d/test/gzogre2json_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('GzOgre2Json tests', function() {
{
pass
{
ambient 0.7 0.8 0.9 1.0
ambient 0.7 0.8 0.9 1.0
emissive 0.2 0.3 0.4
specular 1.0 0.9 0.8 0.7
diffuse 0.0 0.0 0.0 1.0
Expand Down Expand Up @@ -379,22 +379,19 @@ fragment_program caster_fp_glsl glsl
.toBeDefined();
});

it('should handle program refs', function() {
it('should handle program refs and inline {}', function() {

let o2j = new GZ3D.Ogre2Json();
const str = `material OakTree/shadow_caster_alpha
{
technique
{ technique
{
pass
{
vertex_program_ref caster_vp_glsl
{
}
fragment_program_ref caster_fp_glsl
{
}
fragment_program_ref caster_fp_glsl {}
}
}
}
Expand Down

0 comments on commit fdfda01

Please sign in to comment.