Exemple #1
0
        public static void StoreBinaryWebResponseIntoCache(string hash, flash.utils.ByteArray response)
        {
            if (SaveToOfflineCache && WebCachePath != null)
            {
                if (!System.IO.Directory.Exists(WebCachePath))
                {
                    System.IO.Directory.CreateDirectory(WebCachePath);
                }
                var path = System.IO.Path.Combine(WebCachePath, hash);

                // write byte array to disk
                response.position = 0;
                using (var fs = new FileStream(path, FileMode.Create))
                {
                    response.getRawStream().CopyTo(fs);
                }
                response.position = 0;
            }
        }
Exemple #2
0
        public static flash.utils.ByteArray LoadBinaryWebResponseFromCache(string hash)
        {
            if (Offline && WebCachePath != null)
            {
                var path = System.IO.Path.Combine(WebCachePath, hash);

#if PLATFORM_MONOTOUCH || PLATFORM_MONOMAC
                if (File.Exists(path))
                {
                    return(flash.utils.ByteArray.loadFromPath(path));
                }
                else
                {
                    return(null);
                }
#elif PLATFORM_MONODROID
                Stream stream = Application.Context.Assets.Open(path);
                flash.utils.ByteArray data = new flash.utils.ByteArray();
                data.readFromStream(stream);
                return(data);
#endif
            }
            return(null);
        }
		public void uploadFromByteArray(ByteArray data, uint byteArrayOffset) {
			throw new System.NotImplementedException();
		}
Exemple #4
0
        public static object LoadResource(string path, string mimeType = null)
        {
            var ext = Path.GetExtension(path).ToLowerInvariant();

            if (path.StartsWith("http:") || path.StartsWith("https:"))
            {
                // We probably want to create a lower level at some point, no need to go through all this
                flash.net.URLRequest urlRequest = new flash.net.URLRequest(path);
                flash.net.URLLoader  urlLoader  = new flash.net.URLLoader(urlRequest);
                urlLoader.dataFormat = flash.net.URLLoaderDataFormat.BINARY;
                urlLoader.load(urlRequest, true);                               // We want the result synchronously before leaving this function
                if (urlLoader.bytesLoaded == 0)
                {
                    // If empty, we consider this an error for the moment
                    Console.WriteLine("Add better error handling for Player.LoadResource() - Path: " + path);
                    return(new flash.display.Bitmap(new flash.display.BitmapData(32, 32)));
                }

                flash.utils.ByteArray dataAsByteArray = urlLoader.data as flash.utils.ByteArray;
                if (dataAsByteArray == null)
                {
                    throw new NotImplementedException();                        // This case should not actually happen, did we miss something in the URL loader implementation?
                }

                switch (ext)
                {
                case ".bmp":
                case ".png":
                case ".jpg":
                case ".jpeg":
                case ".atf":
                case ".tif":
                    // load as bitmap
                    return(new flash.display.Bitmap(flash.display.BitmapData.loadFromByteArray(dataAsByteArray)));

                default:
                    throw new NotImplementedException("HTTP loader for " + ext);
                }
            }

            // handle byte arrays
            if (mimeType == "application/octet-stream")
            {
                // load as byte array
                return(flash.utils.ByteArray.loadFromPath(path));
            }

            switch (ext)
            {
            case ".swf":
                // loading of swf's is not supported, so create a dummy sprite
                return(new flash.display.Sprite());

            case ".bmp":
            case ".png":
            case ".jpg":
            case ".jpeg":
            case ".atf":
            case ".tif":
                // load as bitmap
                return(new flash.display.Bitmap(flash.display.BitmapData.loadFromPath(path)));

            case ".jxr":
            {
                // TODO we dont support jxr loading yet so rename them to png instead
                // you will have to manually convert your jxr images to png
                var rename = Path.ChangeExtension(path, ".png");
                return(new flash.display.Bitmap(flash.display.BitmapData.loadFromPath(rename)));
            }

            case ".json":
            {
                var newPath  = PlayScript.Player.ResolveResourcePath(path);
                var jsonText = System.IO.File.ReadAllText(newPath);
                return(_root.JSON.parse(jsonText));
            }

            default:
                throw new NotImplementedException("Loader for " + ext);
            }
        }
Exemple #5
0
        public static object LoadResource(string path, string mimeType = null)
        {
            var ext = Path.GetExtension(path).ToLowerInvariant();

            if (path.StartsWith("http:") || path.StartsWith("https:"))
            {
                // We probably want to create a lower level at some point, no need to go through all this
                flash.net.URLRequest urlRequest = new flash.net.URLRequest(path);
                flash.net.URLLoader  urlLoader  = new flash.net.URLLoader(urlRequest);
                urlLoader.dataFormat = flash.net.URLLoaderDataFormat.BINARY;
                urlLoader.load(urlRequest, true);                               // We want the result synchronously before leaving this function
                if (urlLoader.bytesLoaded == 0)
                {
                    // If empty, we consider this an error for the moment
                    Console.WriteLine("Add better error handling for Player.LoadResource() - Path: " + path);
                    return(new flash.display.Bitmap(new flash.display.BitmapData(32, 32)));
                }

                flash.utils.ByteArray dataAsByteArray = urlLoader.data as flash.utils.ByteArray;
                if (dataAsByteArray == null)
                {
                    throw new NotImplementedException();                        // This case should not actually happen, did we miss something in the URL loader implementation?
                }

                if (mimeType != null && mimeType.StartsWith("image/"))
                {
                    return(new flash.display.Bitmap(flash.display.BitmapData.loadFromByteArray(dataAsByteArray)));
                }

                switch (ext)
                {
                case ".bmp":
                case ".png":
                case ".jpg":
                case ".jpeg":
                case ".atf":
                case ".tif":
                    // load as bitmap
                    return(new flash.display.Bitmap(flash.display.BitmapData.loadFromByteArray(dataAsByteArray)));

                default:
                    throw new NotImplementedException("HTTP loader for " + ext);
                }
            }

            // handle byte arrays
            if (mimeType == "application/octet-stream")
            {
                // load as byte array
                return(flash.utils.ByteArray.loadFromPath(path));
            }

            switch (ext)
            {
            case ".swf":
                // loading of swf's is not supported, so create a dummy sprite
                return(new flash.display.Sprite());

            case ".bmp":
            case ".png":
            case ".jpg":
            case ".jpeg":
            case ".atf":
            case ".tif":
                // load as bitmap
                return(new flash.display.Bitmap(flash.display.BitmapData.loadFromPath(path)));

            case ".jxr":
            {
                // TODO we dont support jxr loading yet so rename them to png instead
                // you will have to manually convert your jxr images to png
                var rename = Path.ChangeExtension(path, ".png");
                return(new flash.display.Bitmap(flash.display.BitmapData.loadFromPath(rename)));
            }

            case ".json":
            {
#if PLATFORM_MONOTOUCH || PLATFORM_MONOMAC
                var newPath  = PlayScript.Player.ResolveResourcePath(path);
                var jsonText = System.IO.File.ReadAllText(newPath);
#elif PLATFORM_MONODROID
                var    jsonText = "";
                Stream stream;
                try
                {
                    stream = Application.Context.Assets.Open(path);
                }
                // if cannot load as assets, try loading it as plain file
                // disable the ex defined but not used "warning as error"
                                        #pragma warning disable 0168
                catch (Java.IO.FileNotFoundException ex)
                                        #pragma warning restore 0168
                {
                    stream = File.OpenRead(path);
                }
                using (StreamReader sr = new System.IO.StreamReader(stream))
                {
                    jsonText = sr.ReadToEnd();
                    sr.Close();
                }
#else
                var jsonText = "";
#endif
                return(_root.JSON.parse(jsonText));
            }

            default:
                throw new NotImplementedException("Loader for " + ext);
            }
        }
Exemple #6
0
		public static flash.utils.ByteArray LoadBinaryWebResponseFromCache(string hash)
		{
			if (Offline && WebCachePath != null) {
				var path = 	System.IO.Path.Combine(WebCachePath, hash); 

#if PLATFORM_MONOTOUCH || PLATFORM_MONOMAC
				if (File.Exists(path)) {
					return flash.utils.ByteArray.loadFromPath(path);
				} else {
					return null;
				}
#elif PLATFORM_MONODROID
				Stream stream = Application.Context.Assets.Open(path);
				flash.utils.ByteArray data = new flash.utils.ByteArray();
				data.readFromStream( stream );
				return data;
#endif
			}
			return null;
		}