PageRenderTime 485ms CodeModel.GetById 221ms app.highlight 12ms RepoModel.GetById 250ms app.codeStats 0ms

/src/away3d/loaders/parsers/AWDParser.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 200 lines | 118 code | 21 blank | 61 comment | 8 complexity | d6e684c6b6bbe0fa946c1640baa2b351 MD5 | raw file
  1package away3d.loaders.parsers
  2{
  3	import away3d.library.assets.IAsset;
  4	import away3d.arcane;
  5	import away3d.events.AssetEvent;
  6	import away3d.events.ParserEvent;
  7	import away3d.loaders.misc.ResourceDependency;
  8	
  9	use namespace arcane;
 10	
 11	/**
 12	 * The AWDParser class is a wrapper for both AWD1Parser and AWD2Parser, and will
 13	 * find the right concrete parser for an AWD file.
 14	 */
 15	public class AWDParser extends ParserBase
 16	{
 17		private var _parser:ParserBase;
 18		
 19		public function AWDParser()
 20		{
 21			super(ParserDataFormat.BINARY);
 22		}
 23		
 24		/**
 25		 * Indicates whether or not a given file extension is supported by the parser.
 26		 * @param extension The file extension of a potential file to be parsed.
 27		 * @return Whether or not the given file type is supported.
 28		 */
 29		public static function supportsType(suffix:String):Boolean
 30		{
 31			return (suffix.toLowerCase() == 'awd');
 32		}
 33		
 34		/**
 35		 * Tests whether a data block can be parsed by the parser.
 36		 * @param data The data block to potentially be parsed.
 37		 * @return Whether or not the given data is supported.
 38		 */
 39		public static function supportsData(data:*):Boolean
 40		{
 41			return (AWD1Parser.supportsData(data) || AWD2Parser.supportsData(data));
 42		}
 43		
 44		/**
 45		 * @inheritDoc
 46		 */
 47		public override function get dependencies():Vector.<ResourceDependency>
 48		{
 49			return _parser? _parser.dependencies : super.dependencies;
 50		}
 51		
 52		/**
 53		 * @inheritDoc
 54		 */
 55		public override function get parsingComplete():Boolean
 56		{
 57			return _parser? _parser.parsingComplete : false;
 58		}
 59		
 60		/**
 61		 * @inheritDoc
 62		 */
 63		public override function get parsingPaused():Boolean
 64		{
 65			return _parser? _parser.parsingPaused : false;
 66		}
 67		
 68		/**
 69		 * @private
 70		 * Delegate to the concrete parser.
 71		 */
 72		arcane override function resolveDependency(resourceDependency:ResourceDependency):void
 73		{
 74			if (_parser)
 75				_parser.resolveDependency(resourceDependency);
 76		}
 77		
 78		/**
 79		 * @private
 80		 * Delegate to the concrete parser.
 81		 */
 82		arcane override function resolveDependencyFailure(resourceDependency:ResourceDependency):void
 83		{
 84			if (_parser)
 85				_parser.resolveDependencyFailure(resourceDependency);
 86		}
 87		
 88		/**
 89		 * @private
 90		 * Delagate to the concrete parser.
 91		 */
 92		arcane override function resolveDependencyName(resourceDependency:ResourceDependency, asset:IAsset):String
 93		{
 94			if (_parser)
 95				return _parser.resolveDependencyName(resourceDependency, asset);
 96			return asset.name;
 97		}
 98		
 99		arcane override function resumeParsingAfterDependencies():void
100		{
101			if (_parser)
102				_parser.resumeParsingAfterDependencies();
103		}
104		
105		/**
106		 * Find the right conrete parser (AWD1Parser or AWD2Parser) and delegate actual
107		 * parsing to it.
108		 */
109		protected override function proceedParsing():Boolean
110		{
111			if (!_parser) {
112				// Inspect data to find correct parser. AWD2 parser
113				// file inspection is the most reliable
114				if (AWD2Parser.supportsData(_data))
115					_parser = new AWD2Parser();
116				else
117					_parser = new AWD1Parser();
118				_parser.materialMode = materialMode;
119				// Listen for events that need to be bubbled
120				_parser.addEventListener(ParserEvent.PARSE_COMPLETE, onParseComplete);
121				_parser.addEventListener(ParserEvent.READY_FOR_DEPENDENCIES, onReadyForDependencies);
122				_parser.addEventListener(ParserEvent.PARSE_ERROR, onParseError);
123				_parser.addEventListener(AssetEvent.ASSET_COMPLETE, onAssetComplete);
124				_parser.addEventListener(AssetEvent.ANIMATION_SET_COMPLETE, onAssetComplete);
125				_parser.addEventListener(AssetEvent.ANIMATION_STATE_COMPLETE, onAssetComplete);
126				_parser.addEventListener(AssetEvent.ANIMATION_NODE_COMPLETE, onAssetComplete);
127				_parser.addEventListener(AssetEvent.STATE_TRANSITION_COMPLETE, onAssetComplete);
128				_parser.addEventListener(AssetEvent.TEXTURE_COMPLETE, onAssetComplete);
129				_parser.addEventListener(AssetEvent.CONTAINER_COMPLETE, onAssetComplete);
130				_parser.addEventListener(AssetEvent.GEOMETRY_COMPLETE, onAssetComplete);
131				_parser.addEventListener(AssetEvent.MATERIAL_COMPLETE, onAssetComplete);
132				_parser.addEventListener(AssetEvent.MESH_COMPLETE, onAssetComplete);
133				_parser.addEventListener(AssetEvent.ENTITY_COMPLETE, onAssetComplete);
134				_parser.addEventListener(AssetEvent.SKELETON_COMPLETE, onAssetComplete);
135				_parser.addEventListener(AssetEvent.SKELETON_POSE_COMPLETE, onAssetComplete);
136				
137				_parser.parseAsync(_data);
138			}
139			
140			// Return MORE_TO_PARSE while delegate parser is working. Once the delegate
141			// finishes parsing, this dummy parser instance will be stopped as well as
142			// a result of the delegate's PARSE_COMPLETE event (onParseComplete).
143			return MORE_TO_PARSE;
144		}
145		
146		/**
147		 * @private
148		 * Just bubble events from concrete parser.
149		 */
150		private function onParseError(ev:ParserEvent):void
151		{
152			dispatchEvent(ev.clone());
153		}
154		
155		/**
156		 * @private
157		 * Just bubble events from concrete parser.
158		 */
159		private function onReadyForDependencies(ev:ParserEvent):void
160		{
161			dispatchEvent(ev.clone());
162		}
163		
164		/**
165		 * @private
166		 * Just bubble events from concrete parser.
167		 */
168		private function onAssetComplete(ev:AssetEvent):void
169		{
170			dispatchEvent(ev.clone());
171		}
172		
173		/**
174		 * @private
175		 * Just bubble events from concrete parser.
176		 */
177		private function onParseComplete(ev:ParserEvent):void
178		{
179			_parser.removeEventListener(ParserEvent.READY_FOR_DEPENDENCIES, onReadyForDependencies);
180			_parser.removeEventListener(ParserEvent.PARSE_COMPLETE, onParseComplete);
181			_parser.removeEventListener(ParserEvent.PARSE_ERROR, onParseError);
182			_parser.removeEventListener(AssetEvent.ASSET_COMPLETE, onAssetComplete);
183			_parser.removeEventListener(AssetEvent.ANIMATION_SET_COMPLETE, onAssetComplete);
184			_parser.removeEventListener(AssetEvent.ANIMATION_STATE_COMPLETE, onAssetComplete);
185			_parser.removeEventListener(AssetEvent.ANIMATION_NODE_COMPLETE, onAssetComplete);
186			_parser.removeEventListener(AssetEvent.STATE_TRANSITION_COMPLETE, onAssetComplete);
187			_parser.removeEventListener(AssetEvent.TEXTURE_COMPLETE, onAssetComplete);
188			_parser.removeEventListener(AssetEvent.CONTAINER_COMPLETE, onAssetComplete);
189			_parser.removeEventListener(AssetEvent.GEOMETRY_COMPLETE, onAssetComplete);
190			_parser.removeEventListener(AssetEvent.MATERIAL_COMPLETE, onAssetComplete);
191			_parser.removeEventListener(AssetEvent.MESH_COMPLETE, onAssetComplete);
192			_parser.removeEventListener(AssetEvent.ENTITY_COMPLETE, onAssetComplete);
193			_parser.removeEventListener(AssetEvent.SKELETON_COMPLETE, onAssetComplete);
194			_parser.removeEventListener(AssetEvent.SKELETON_POSE_COMPLETE, onAssetComplete);
195			
196			finishParsing();
197		}
198	}
199}
200