From fdfda01abeed7d56f11cb8e156a3c1df632d9b2f Mon Sep 17 00:00:00 2001 From: chapulina Date: Fri, 25 May 2018 13:50:56 -0700 Subject: [PATCH] handle inline {} and multiple spaces in array --- gz3d/src/gzogre2json.js | 21 ++++++++++++++++++--- gz3d/test/gzogre2json_test.js | 11 ++++------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/gz3d/src/gzogre2json.js b/gz3d/src/gzogre2json.js index e34c3383..a2918ca3 100644 --- a/gz3d/src/gzogre2json.js +++ b/gz3d/src/gzogre2json.js @@ -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) @@ -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, ''); @@ -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++) diff --git a/gz3d/test/gzogre2json_test.js b/gz3d/test/gzogre2json_test.js index aaae7f1d..3719d6c6 100644 --- a/gz3d/test/gzogre2json_test.js +++ b/gz3d/test/gzogre2json_test.js @@ -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 @@ -379,12 +379,11 @@ 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 { @@ -392,9 +391,7 @@ fragment_program caster_fp_glsl glsl { } - fragment_program_ref caster_fp_glsl - { - } + fragment_program_ref caster_fp_glsl {} } } }