public override void OnGetGraphic(ImagePackage package,FilePath path){
			
			// Already resized?
			ResizedImage resized=ResizedImages.Get(path.Path);
			
			if(resized!=null){
				
				// Sure is!
				package.GotGraphic(resized.Image);
				
				return;
				
			}
			
			if(Callback.WillDelay){
				
				// Buffer this call until later - we're not on the main thread.
				
				// Create the callback:
				ResourcesProtocolCallback callback=new ResourcesProtocolCallback(package,path);
				
				// Hook up the protocol handler for speed later:
				callback.Protocol=this;
				
				// Request it to run:
				callback.Go();
				
				return;
			}
			
			if(path.Filetype=="spa"){
				// Animated.
				
				// Load the binary file - Note: the full file should be called file.spa.bytes for this to work in Unity.
				byte[] binary=((TextAsset)Resources.Load(path.Path)).bytes;
				
				if(binary!=null){
					// Apply it now:
					package.GotGraphic(new SPA(path.Url,binary));
					return;
				}
				
			}else{
				// Image
				Texture2D image=(Texture2D)Resources.Load(path.Directory+path.Filename);
				
				// Resize the image:
				resized=ResizedImages.Add(path.Path,image);
				
				if(image!=null){
					package.GotGraphic(resized.Image);
					return;
				}
				
			}
			
			package.GotGraphic("Image not found in resources ("+path.Directory+path.Filename+" from URL '"+path.Url+"').");
			
		}
		// Textual stuff; usually .html files
		public override void OnGetText(TextPackage package,FilePath path){
			
			// Get the bytes:
			string data=System.Text.Encoding.UTF8.GetString(System.IO.File.ReadAllBytes(path.Path));
			
			// Let the package know:
			package.GotText(data,null);
			
		}
		/// <summary>Attempts to get a graphic from the given location using this protocol.</summary>
		/// <param name="package">The image request. GotGraphic must be called on this when the protocol is done.</param>
		/// <param name="path">The location of the file to retrieve using this protocol.</param>
		public override void OnGetGraphic(ImagePackage package,FilePath path){
			if(path.Filetype=="spa"){
				// Grab a runtime SPA:
				package.GotGraphic(SPA.Get(path.Path));
				return;
			}
			
			package.GotGraphic(ImageCache.Get(path.Path));
		}
		// Raw binary data
		public override void OnGetData(DataPackage package,FilePath path){
			
			// Get the bytes:
			byte[] data=System.IO.File.ReadAllBytes(path.Path);
			
			// Let the package know:
			package.GotData(data,null);
			
		}
        /// <summary>Attempts to get a graphic from the given location using this protocol.</summary>
        /// <param name="package">The image request. GotGraphic must be called on this when the protocol is done.</param>
        /// <param name="path">The location of the file to retrieve using this protocol.</param>
        public override void OnGetGraphic(ImagePackage package,FilePath path){
			
			// Create it:
            Texture2D tex = new Texture2D(0,0);
			
			// Load it:
            tex.LoadImage(System.IO.File.ReadAllBytes(path.Path));
			
			// Let the package know:
            package.GotGraphic(tex);
        }
		public override void OnGetGraphic(ImagePackage package,FilePath path){
			
			if(Callback.WillDelay){
				
				// Buffer this call until later - we're not on the main thread.
				
				// Create the callback:
				ResourcesProtocolCallback callback=new ResourcesProtocolCallback(package,path);
				
				// Hook up the protocol handler for speed later:
				callback.Protocol=this;
				
				// Request it to run:
				callback.Go();
				
				return;
			}
			
			if(path.Filetype=="spa"){
				// Animated.
				// Load the binary file - Note: the full file should be called file.spa.bytes for this to work in Unity.
				byte[] binary=((TextAsset)Resources.Load(path.Path)).bytes;
				
				if(binary!=null){
					// Apply it now:
					package.GotGraphic(new SPA(path.Url,binary));
					return;
				}
				
			#if !MOBILE
			}else if(ContentType.IsVideo(path.Filetype)){
				// Video
				MovieTexture movie=(MovieTexture)Resources.Load(path.Directory+path.Filename,typeof(MovieTexture));
				
				if(movie!=null){
					package.GotGraphic(movie);
					return;
				}
				
			#endif
			}else{
				// Image
				Texture2D image=(Texture2D)Resources.Load(path.Directory+path.Filename);
				
				if(image!=null){
					package.GotGraphic(image);
					return;
				}
				
			}
			
			package.GotGraphic("Image not found in resources ("+path.Directory+path.Filename+" from URL '"+path.Url+"').");
			
		}
		public HttpErrorInfo(HttpRequest request){
			
			// Store the request:
			Request=request;
			
			// Grab the URL:
			Url=new FilePath(request.Url,"");
			
			// Grab the message:
			Message=request.Error;
			
		}
		public FileErrorInfo(TextPackage package){
			
			// Store the package:
			Package=package;
			
			// Grab the URL:
			Url=new FilePath(package.Url,"");
			
			// Grab the message:
			Message=package.Error;
			
		}
		public override void OnGetGraphic(ImagePackage package,FilePath path){
			// Work like a proper browser - let's go get the image from the web.
			// Second is for video - it's called when the video is ready for playback but
			// Not necessarily fully downloaded.
			
			// First, was this graphic already requested?
			// If so, we'll provide that immediately.
			ImagePackage previousRequest=GetFromCache(path.Url);
			
			if(previousRequest!=null){
				// Great - provide this packages result first.
				// This prevents any flashing if innerHTML is loaded.
				package.GotCached(previousRequest);
				return;
			}
			
			HttpRequest request=new HttpRequest(path.Url,GotGraphicResult);
			request.OnRequestReady+=GotGraphicResult;
			request.ExtraData=package;
			request.Send();
		}
		/// <summary>Get the file at the given path as a MovieTexture (Unity Pro only!), Texture2D,
		/// SPA Animation or DynamicTexture using this protocol.
		/// Once it's been retrieved, this must call package.GotGraphic(theObject) internally.</summary>
		public virtual void OnGetGraphic(ImagePackage package,FilePath path){}
		/// <summary>Submits the given form to the given path using this protocol.</summary>
		public virtual void OnPostForm(FormData form,Element formElement,FilePath path){}
		/// <summary>Get generic binary at the given path using this protocol. Used for e.g. fonts.
		/// Once it's been retrieved, this must call package.GotData(theText) internally.</summary>
		public virtual void OnGetData(DataPackage package,FilePath path){}
		/// <summary>Get the file at the given path as some html text using this protocol.
		/// Once it's been retrieved, this must call package.GotText(theText) internally.</summary>
		public virtual void OnGetText(TextPackage package,FilePath path){}
		/// <summary>Sets up the filepath to the given url which may be relative to a given location.</summary>
		/// <param name="src">The file to get.</param>
		/// <param name="relativeTo">The path the file to get is relative to, if any. May be null.</param>
		private void SetPath(string src,string relativeTo){
			File=new FilePath(src,relativeTo,false);
			FileType=File.Filetype.ToLower();
		}
		/// <summary>Does the item at the given location have full access to the code security domain?
		/// Used by Nitro. If it does not have full access, the Nitro security domain is asked about the path instead.
		/// If you're unsure, leave this false! If your game uses simple web browsing, 
		/// this essentially stops somebody writing dangerous Nitro on a remote webpage and directing your game at it.</summary>
		public virtual bool FullAccess(FilePath path){
			return false;
		}
Beispiel #16
0
		/// <summary>Submits this form.</summary>
		public void submit(){
			
			// Generate a nice dictionary of the form contents.
			
			// Step 1: find the unique names of the elements:
			Dictionary<string,string> uniqueValues=new Dictionary<string,string>();
			
			List<Element> allInputs=GetAllInputs();
			
			foreach(Element element in allInputs){
				string type=element["type"];
				if(type=="submit"||type=="button"){
					// Don't want buttons in here.
					continue;
				}
				
				string name=element["name"];
				if(name==null){
					name="";
				}
				
				// Step 2: For each one, get their value.
				// We might have a name repeated, in which case we check if they are radio boxes.
				
				if(uniqueValues.ContainsKey(name)){
					// Ok the element is already added - we have two with the same name.
					// If they are radio, then only overwrite value if the new addition is checked.
					// Otherwise, overwrite - furthest down takes priority.
					if(element.Tag=="input"){
						InputTag tag=(InputTag)(element.Handler);
						if(tag.Type==InputType.Radio&&!tag.Checked){
							// Don't overwrite.
							continue;
						}
					}
				}
				string value=element.value;
				if(value==null){
					value="";
				}
				uniqueValues[name]=value;
			}
			
			FormData formData=new FormData(uniqueValues);
			
			// Hook up the form element:
			formData.form=Element;
			
			object result=Element.Run("onsubmit",formData);
			
			if( !string.IsNullOrEmpty(Action) && (result==null||!(bool)result) ){
				// Post to Action.
				FilePath path=new FilePath(Action,Element.Document.basepath,false);
				
				FileProtocol fileProtocol=path.Handler;
				
				if(fileProtocol!=null){
					fileProtocol.OnPostForm(formData,Element,path);
				}
			}
		}
		/// <summary>Loads a link into the given document.</summary>
		/// <param name="path">The path the link was pointing at.</param>
		/// <param name="document">The document the link will load into.</param>
		private void LoadIntoDocument(FilePath path,Document document){
			
			// Clear the document so it's obvious to the player the link is now loading:
			document.innerHTML="";
			document.location=path;
			
			// Load the html. Note that path.Url is fully resolved at this point:
			TextPackage package=new TextPackage(path.Url,"");
			package.ExtraData=document;
			package.Get(GotLinkText);
			
		}
Beispiel #18
0
		public override bool OnClick(UIEvent clickEvent){
			base.OnClick(clickEvent);
			
			if(!clickEvent.heldDown){
				// Time to go to our Href.
				
				#if MOBILE || UNITY_METRO
				
				// First, look for <source> elements.
				
				// Grab the kids:
				List<Element> kids=Element.childNodes;
				
				if(kids!=null){
					// For each child, grab it's src value. Favours the most suitable protocol for this platform (e.g. market:// on android).
					
					foreach(Element child in kids){
						
						if(child.Tag!="source"){
							continue;
						}
						
						// Grab the src:
						string childSrc=child["src"];
						
						if(childSrc==null){
							continue;
						}
						
						// Get the optional type - it can be Android,W8,IOS,Blackberry:
						string type=child["type"];
						
						if(type!=null){
							type=type.Trim().ToLower();
						}
						
						#if UNITY_ANDROID
							
							if(type=="android" || childSrc.StartsWith("market:")){
								
								Href=childSrc;
								
							}
							
						#elif UNITY_WP8 || UNITY_METRO
						
							if(type=="w8" || type=="wp8" || type=="windows" || childSrc.StartsWith("ms-windows-store:")){
								
								Href=childSrc;
								
							}
							
						#elif UNITY_IPHONE
							
							if(type=="ios" || childSrc.StartsWith("itms:") || childSrc.StartsWith("itms-apps:")){
								
								Href=childSrc;
								
							}
							
							
						#endif
						
					}
					
				}
				
				#endif
					
				
				if(!string.IsNullOrEmpty(Href)){
					FilePath path=new FilePath(Href,Element.Document.basepath,false);
					
					// Do we have a file protocol handler available?
					FileProtocol fileProtocol=path.Handler;
					
					if(fileProtocol!=null){
						fileProtocol.OnFollowLink(Element,path);
					}
				}
				
				clickEvent.stopPropagation();
			}
			
			return true;
		}
		public ResourcesProtocolCallback(ImagePackage package,FilePath path){
			Images=package;
			Path=path;
		}
		public override void OnFollowLink(Element linkElement,FilePath path){
			string target=linkElement["target"];
			if(target!=null && target=="_blank"){
				// Open the given url.
				Application.OpenURL(path.Url);
				return;
			}
			
			// Clear the document so it's obvious to the player the link is now loading:
			linkElement.Document.innerHTML="";
			linkElement.Document.location=path;
			
			// Load the html. Note that path.Url is fully resolved at this point:
			TextPackage package=new TextPackage(path.Url,"");
			package.ExtraData=linkElement.Document;
			package.Get(GotLinkText);
		}
		public override void OnGetData(DataPackage package,FilePath path){
			HttpRequest request=new HttpRequest(path.Url,GotDataResult);
			request.ExtraData=package;
			request.Send();
		}
		public override void OnPostForm(FormData form,Element formElement,FilePath path){
			// Post to HTTP; Action is our URL.
			HttpRequest request=new HttpRequest(path.Url,OnFormSent);
			request.ExtraData=formElement;
			request.AttachForm(form.ToUnityForm());
			request.Send();
		}
		/// <summary>The user clicked on the given link which points to the given path.</summary>
		public virtual void OnFollowLink(Element linkElement,FilePath path){}
		public override void OnFollowLink(Element linkElement,FilePath path){
			Application.LoadLevel(path.Directory+path.File);
		}
		public override void OnGetText(TextPackage package,FilePath path){
			
			if(Callback.WillDelay){
				
				// Buffer this call until later - we're not on the main thread.
				
				// Create the callback:
				ResourcesProtocolCallback callback=new ResourcesProtocolCallback(package,path);
				
				// Hook up the protocol handler for speed later:
				callback.Protocol=this;
				
				// Request it to run:
				callback.Go();
				
				return;
			}
			
			// Getting a files text content from resources.
			string text=null;
			string error=null;
			string resPath=null;
			string filetype=path.Filetype;
			
			if(filetype=="html" || filetype=="htm" || filetype=="txt"){
				resPath=path.Directory+path.Filename;
			}else{
				// The file MUST end in .bytes for this to work.
				resPath=path.Path;
			}
			
			TextAsset asset=(TextAsset)Resources.Load(resPath);
			
			if(asset==null){
				
				error="File not found in resources ("+path.Directory+path.Filename+" from URL '"+path.Url+"')";
				
				if(filetype=="css" || filetype=="ns" || filetype=="nitro"){
					error+=" Additionally, note that '."+filetype+"' files are not recognised by Unity as a text file. Try renaming the file to ."+filetype+".bytes in your Resources folder.";
				}
				
			}else{
				text=asset.text;
			}
			
			package.GotText(text,error);
			
		}
		/// <summary>Sets up the filepath to the given url which may be relative to a given location.</summary>
		/// <param name="src">The file to get.</param>
		/// <param name="relativeTo">The path the file to get is relative to, if any. May be null.</param>
		/// <param name="useResolution">True if the resolution string should be appended to the name.</param>
		private void SetPath(string src,string relativeTo,bool useResolution){
			File=new FilePath(src,relativeTo,useResolution);
			FileType=File.Filetype.ToLower();
			PixelPerfect=File.UsedResolution;
		}
		public override bool FullAccess(FilePath path){
			// This is entirely local and controlled by the developer - it's a safe protocol.
			return true;
		}
		public override void OnGetText(TextPackage package,FilePath path){
			// Work like a proper browser - Let's go grab text from the given url.
			// Note that this will only work with simple sites (no JS - nitro only) or ones built specifically for PowerUI.
			HttpRequest request=new HttpRequest(path.Url,GotTextResult);
			request.ExtraData=package;
			request.Send();
		}
		public override void OnGetData(DataPackage package,FilePath path){
			
			if(Callback.WillDelay){
				
				// Buffer this call until later - we're not on the main thread.
				
				// Create the callback:
				ResourcesProtocolCallback callback=new ResourcesProtocolCallback(package,path);
				
				// Hook up the protocol handler for speed later:
				callback.Protocol=this;
				
				// Request it to run:
				callback.Go();
				
				return;
			}
			
			// Getting a files text content from resources.
			byte[] data=null;
			string error=null;
			string resPath=path.Path;
			
			TextAsset asset=(TextAsset)Resources.Load(resPath);
			
			if(asset==null){
				
				error="File not found in resources ("+path.Directory+path.Filename+" from URL '"+path.Url+"'). Does your file in Unity end with .bytes?";
				
			}else{
				data=asset.bytes;
			}
			
			package.GotData(data,error);
			
		}
		public override void OnFollowLink(Element linkElement,FilePath path){
			
			// Grab the protocol:
			string protocol=path.Protocol;
			
			// Is it actually a web one?
			string[] names=GetNames();
			
			bool web=false;
			
			for(int i=0;i<names.Length;i++){
				
				if(protocol==names[i]){
					
					web=true;
					break;
					
				}
				
			}
			
			Document targetDocument=null;
			
			if(web){
				
				// Otherwise it's e.g. an app link. No target there - always external.
				
				// Resolve the link elements target:
				targetDocument=ResolveTarget(linkElement);
				
			}
			
			if(targetDocument==null){
				
				// Open the URL outside of Unity:
				Application.OpenURL(path.Url);
				
			}else{
				
				// Load into that document:
				LoadIntoDocument(path,targetDocument);
				
			}
			
		}