/// <summary>
		/// Converts the media in a valid format for this player and returns 
		/// a valid resource, if there are any.
		/// </summary>
		/// <remarks>
		/// Checks, which resource belongs to this player, change the size 
		/// or format, if needed, and creates a MediaResource.
		/// </remarks>
		/// <returns>MediaResource with the correct media or null.</returns>
		/// <param name="media">Media.</param>
		MediaResource ConvertMedia(Media media)
		{
			MediaResource res = null;

			// Are there any resources
			if (media.Resources.Count < 1)
				return null;

			// Get the last good media resource that could be found
			foreach(MediaResource mr in media.Resources) {
				if (_mediaFormats.Contains(mr.Type) && mr.Type.IsImage() && media.Resources[0].Type.IsImage() && (mr == media.Resources[0] || mr.Directives.Contains("xmarksthespot") || mr.Filename.ToLower().Contains("xmarksthespot")))
					res = mr;
				if (_mediaFormats.Contains(mr.Type) && mr.Type.IsSound() && media.Resources[0].Type.IsSound() && (mr == media.Resources[0] || mr.Directives.Contains("xmarksthespot") || mr.Filename.ToLower().Contains("xmarksthespot")))
					res = mr;
			}

			if (res == null)
				return null;

			// Create MemoryStream
			MediaResource result = new MediaResource();

			result.Filename = res.Filename;
			result.Directives = res.Directives;
			result.Type = res.Type;
			result.Data = res.Data;

			// Now remove all resources, because we don't need them anymore
			media.Resources = null;

			return result;
		}
Exemplo n.º 2
0
		/// <summary>
		/// Extracts the media from the Lua code.
		/// </summary>
		/// <param name="obj">Object.</param>
		static Media ExtractMedia (LuaTable obj)
		{
			Media result = new Media ();

			// Extract table entries
			result.Name = obj ["Name"].ToString();
			result.Id = obj ["Id"].ToString();
			result.Description = obj ["Description"].ToString();
			result.AltText = obj ["AltText"].ToString();
			// Get resources
			result.Resources = obj ["Resources"] is LuaBoolean ? null : ExtractMediaResources ((LuaTable)obj ["Resources"]);

			return result;
		}
Exemplo n.º 3
0
		/// <summary>
		/// Converts the media in a valid format for this player and returns 
		/// a valid resource, if there are any.
		/// </summary>
		/// <remarks>
		/// Checks, which resource belongs to this player, change the size 
		/// or format, if needed, and creates a MediaResource.
		/// </remarks>
		/// <returns>MediaResource with the correct media or null.</returns>
		/// <param name="media">Media.</param>
		MediaResource ConvertMedia(Media media)
		{
			MediaResource res = null;

			// Are there any resources
			if (media.Resources.Count < 1)
				return null;

			// Get the last good media resource that could be found
			foreach(MediaResource mr in media.Resources) {
				if (_mediaFormats.Contains(mr.Type) && mr.Type.IsImage() && media.Resources[0].Type.IsImage() && (mr == media.Resources[0] || mr.Directives.Contains("garmin") || mr.Filename.ToLower().Contains("garmin")))
					res = mr;
				if (_mediaFormats.Contains(mr.Type) && mr.Type.IsSound() && media.Resources[0].Type.IsSound() && (mr == media.Resources[0] || mr.Type == MediaType.FDL || mr.Directives.Contains("garmin") || mr.Filename.ToLower().Contains("garmin")))
					res = mr;
			}

			if (res == null)
				return null;

			// Create MemoryStream
			MediaResource result = new MediaResource();

			result.Filename = res.Filename;
			result.Directives = res.Directives;

			if (res.Type.IsImage()) {
				// Image
				Image imgLoader;
				Image img;
				using(MemoryStream ims = new MemoryStream(res.Data)) {
					imgLoader = Image.FromStream(ims);
					img = new Bitmap(imgLoader);
					// Do special things with the image (resize, bit depth, ...)
					if (!res.Directives.Contains("noresize") && img.Width > 230)
						img = ResizeImage(img, 230);
					// Garmin can only handle jpg
					using(MemoryStream oms = new MemoryStream()) {
						img.Save(oms, _jpegCodec, _encParams);
						result.Data = oms.ToArray();
					}
				}
				result.Type = MediaType.JPG;
			} else {
				// Sound
				if (res.Type == MediaType.FDL) {
					result.Type = MediaType.FDL;
					result.Data = res.Data;
				} else {
					result = null;
				}
			}

			// Now remove all resources, because we don't need them anymore
			media.Resources = null;

			return result;
		}
Exemplo n.º 4
0
		/// <summary>
		/// Checks the gwz file for:
		/// - Lua file
		/// - Info file
		/// - Lua code for right format
		/// - Media files for completeness.
		/// Errors could be found in the list Errors (with line and col, if known).
		/// </summary>
        /// <param name='zip'>
		/// Zip file for later use.
		/// </param>
		/// <returns>
		/// <c>true</c>, if the checked gwz was correct and complete, <c>false</c> otherwise.
		/// </returns>
		public bool CheckGWZ ( ZipFile zip, Cartridge cartridge )
		{
			// Get input stream for Lua file
			string luaCode = getLuaCode(zip,filenameGwz);
            
			// Now parse the Lua file
			Lexer lexer = new Lexer ();
			// Split the Lua code into tokens
			TokenReader tr = null;
			try
			{
				tr = lexer.Lex ( luaCode );
			}
			catch ( LuaSourceException e )
			{
				// There are something strange in the Lua file, so that the lexer couldn't create tokens
				Errors.Add ( new Error ( e.Line, e.Column, e.Message ) );
				return false;
			}
			// Jump to the first token
			tr.p = 0;
			// Save cartridge name for later use (the last is allways End of Stream token)
			cartridge.Variable = tr.tokens[tr.tokens.Count-2].Data;
			// Now parse the tokens, if it is all right with the Lua file
			SharpLua.Parser parser = new SharpLua.Parser ( tr );
			SharpLua.Ast.Chunk c = null;
			try
			{
				c = parser.Parse();
			}
			catch ( Exception e )
			{
				// Was there an error?
				if ( parser.Errors.Count > 0 )
				{
					foreach ( SharpLua.LuaSourceException error in parser.Errors )
						Errors.Add ( new Error ( error.Line, error.Column, error.Message ) );
					return false;
				}
			}
			// Now we are shure, that Lua code is allright, so we could go on
			// Create a dictionary, so we could get fast connection variable <-> media list entry
			Dictionary<String,Media> mediaDict = new Dictionary<String,Media> ();
			// Go through all statements and search cartridge info and all media definitions
			for ( int i = 0; i < c.Body.Count; i++ )
			{
				// We only check assignments in the parser result
				if ( c.Body[i] is AssignmentStatement )
				{
					AssignmentStatement statement = (AssignmentStatement) c.Body[i];
					// First get the left side ...
					Expression statementLeft = statement.Lhs[0];
					String left = getExpressionAsString ( statementLeft );
					// ... and than the right side
					Expression statementRight = statement.Rhs[0];
					String right = getExpressionAsString ( statementRight );
					// Is it an entry for the cartridge
					if ( left.Contains ( "." ) && cartridge.Variable.Equals ( left.Split ( '.' )[0] ) )
					{
						string key = left.Split ( '.' )[1];

						switch ( key )
						{
    						case "Id":
	    						cartridge.Id = right;
		    					break;
			    			case "Name":
				    			cartridge.Name = right;
					    		break;
						    case "Description":
    							cartridge.Description = removeLongString(right);
	    						break;
		    				case "Activity":
			    				cartridge.Activity = right;
				    			break;
					    	case "StartingLocationDescription":
						    	cartridge.StartingLocationDescription = removeLongString(right);
							    break;
    						case "StartingLocation":
                                if (statementRight is SharpLua.Ast.Expression.MemberExpr)
                                {
                                    // Play anywhere cartridge with starting location at 360.0 / 360.0 / 360.0
                                    cartridge.Latitude = 360.0;
                                    cartridge.Longitude = 360.0;
                                    cartridge.Altitude = 360.0;
                                }
                                else 
		    					    getZonePoints ( (SharpLua.Ast.Expression.CallExpr) statementRight, ref cartridge.Latitude, ref cartridge.Longitude, ref cartridge.Altitude );
			    				break;
				    		case "Version":
					    		cartridge.Version = right;
						    	break;
    						case "Company":
	    						cartridge.Company = right;
		    					break;
			    			case "Author":
				    			cartridge.Author = right;
					    		break;
						    case "BuilderVersion":
							    cartridge.BuilderVersion = right;
    							break;
	    					case "CreateDate":
		    					cartridge.CreateDate = right;
			    				break;
				    		case "PublishDate":
					    		cartridge.PublishDate = right;
						    	break;
    						case "UpdateDate":
	    						cartridge.UpdateDate = right;
		    					break;
			    			case "LastPlayedDate":
				    			cartridge.LastPlayedDate = right;
					    		break;
						    case "TargetDevice":
    							cartridge.TargetDevice = right;
	    						break;
		    				case "TargetDeviceVersion":
			    				cartridge.TargetDeviceVersion = right;
				    			break;
					    	case "StateId":
						    	cartridge.StateId = right;
							    break;
    						case "CountryId":
	    						cartridge.CountryId = right;
		    					break;
			    			case "Visible":
				    			cartridge.Visible = right.Equals ( "true" ) ? true : false;
					    		break;
						    case "Complete":
    							cartridge.Complete = right.Equals ( "true" ) ? true : false;
	    						break;
		    				case "UseLogging":
			    				cartridge.UseLogging = right.Equals ( "true" ) ? true : false;
				    			break;
					    	case "Media":
						    	cartridge.Splash = right;
							    break;
    						case "Icon":
	    						cartridge.Icon = right;
		    					break;
			    		}
				    }
					// Is it a ZMedia definition?
					if ( right.Equals ( "Wherigo.ZMedia" ) )
					{
						Media media = new Media ();
						media.Variable = left;
						cartridge.MediaList.Add ( media );
						mediaDict.Add ( left, media );
					}
					// Is it a ZMedia entry?
					if ( left.Contains ( "." ) &&  mediaDict.ContainsKey ( left.Split ( '.' )[0] ) )
					{
						// We found an entry to a ZMedia
						string key = left.Split ( '.' )[0];
						string value = left.Split ( '.' )[1];
						// Which key did we have found?
						switch ( value )
						{
							case "Name":
								mediaDict[key].Name = right;
								break;
							case "Description":
								mediaDict[key].Description = right;
								break;
							case "AltText":
								mediaDict[key].AltText = right;
								break;
							case "Id":
								mediaDict[key].Id = right;
								break;
							case "Resources":
								getMediaResources ( (SharpLua.Ast.Expression.TableConstructorExpr) statementRight, mediaDict[key].Resources );
								break;
						}
					}
				}
			}

			// Now check, if each media has a file in the gwz
			foreach ( Media m in cartridge.MediaList )
			{
				foreach ( MediaResource resource in m.Resources )
				{
					if ( !files.Contains ( resource.Filename.ToLower () ) )
					{
						// This filename couldn't be found in the gwz file
						Errors.Add ( new Error ( 0, 0, "Media file not found: " + resource.Filename ) );
					}
				}
			}

			// If there are no errors, than return true
			return Errors.Count == 0;
		}
Exemplo n.º 5
0
        /// <summary>
        /// Checks, if the media has a valid entry for this device. If yes, than return the number of the entry.
        /// </summary>
        /// <param name="media">Media with a list of entries for belonging files.</param>
        /// <returns>The number for the best entry, if there is one. Otherwise -1.</returns>
		public int CheckMedia ( Media media )
		{
			// First check, if the media allready have a valid entry
			if ( media.Entry >= 0 )
				return media.Entry;
			// Has one of the media resources a valid entry in device or contains a filename the device
			for ( int i = 0; i < media.Resources.Count; i++ )
			{
				// Has the media resource a valid device entry 
				if ( media.Resources[i].Device == device )
					return i;
				// Has the media resource the device in the filename
				if ( media.Resources[i].Filename.ToLower ().Contains ( deviceName ) )
					return i;
			}
			// No file for this device found, so check, if we could find a file, which runs on this device
			// Has one of the media resources a valid entry in device or contains a filename the device
			for ( int i = 0; i < media.Resources.Count; i++ )
			{
				// Is one of the media resources in the media formats list of the device
				if ( Array.Exists ( deviceFormats[(int) device], element => element == media.Resources[i].Type ) )
					return i;
			}

			// We didn't find any resource, which belongs to this device.
			return -1;
		}