示例#1
0
        private static string GetPath(IBaseDirectory baseDirectory)
        {
            var path = Path.Combine(baseDirectory.FullPath, "Content", TargetPlatformUtility.GetExecutingPlatform().ToString());

            Directory.CreateDirectory(path);
            return(path);
        }
 /// <summary>
 /// The attempt load.
 /// </summary>
 /// <param name="path">
 /// The path.
 /// </param>
 /// <param name="name">
 /// The name.
 /// </param>
 /// <returns>
 /// The <see cref="object"/>.
 /// </returns>
 public IRawAsset AttemptLoad(string path, string name, ref DateTime?lastModified, bool noTranslate = false)
 {
     try
     {
         var stream = global::Android.App.Application.Context.Assets.Open(
             TargetPlatformUtility.GetExecutingPlatform().ToString() + Path.DirectorySeparatorChar +
             (noTranslate ? name : name.Replace('.', Path.DirectorySeparatorChar)) + ".asset");
         using (var reader = new StreamReader(stream, Encoding.UTF8))
         {
             return
                 (new DictionaryBasedRawAsset(
                      JsonConvert.DeserializeObject <Dictionary <string, object> >(reader.ReadToEnd())));
         }
     } catch (Java.IO.FileNotFoundException)
     {
         try
         {
             var stream = global::Android.App.Application.Context.Assets.Open(
                 (noTranslate ? name : name.Replace('.', Path.DirectorySeparatorChar)) + ".asset");
             using (var reader = new StreamReader(stream, Encoding.UTF8))
             {
                 return
                     (new DictionaryBasedRawAsset(
                          JsonConvert.DeserializeObject <Dictionary <string, object> >(reader.ReadToEnd())));
             }
         } catch (Java.IO.FileNotFoundException)
         {
             return(null);
         }
     }
 }
        private static string EmbeddedNameToAssetName(string assemblyName, string resourceName)
        {
            if (resourceName.StartsWith(assemblyName + "."))
            {
                resourceName = resourceName.Substring(assemblyName.Length + 1);
            }

            if (resourceName.StartsWith("Resources."))
            {
                resourceName = resourceName.Substring("Resources.".Length);
            }

            if (!resourceName.Contains("."))
            {
                return(null);
            }

            // Strip off the file extension.
            resourceName = resourceName.Substring(0, resourceName.LastIndexOf('.'));

            if (resourceName.EndsWith("-" + TargetPlatformUtility.GetExecutingPlatform()))
            {
                resourceName = resourceName.Substring(0, resourceName.Length - (TargetPlatformUtility.GetExecutingPlatform().ToString().Length + 1));
            }

            return(resourceName);
        }
        protected EmbeddedAssetFsLayer(bool isSource)
        {
            Func <string, bool> filter;

            if (isSource)
            {
                filter = (resource) => !resource.EndsWith("-" + TargetPlatformUtility.GetExecutingPlatform() + ".bin");
            }
            else
            {
                filter = (resource) => resource.EndsWith("-" + TargetPlatformUtility.GetExecutingPlatform() + ".bin");
            }

            var assets = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
                          where !assembly.IsDynamic
                          where assembly.GetName().Name != "mscorlib" && assembly.GetName().Name != "System" &&
                          !assembly.GetName().Name.StartsWith("System.")
                          from resource in assembly.GetManifestResourceNames()
                          where resource.StartsWith(assembly.GetName().Name) && filter(resource)
                          let name = EmbeddedNameToAssetName(assembly.GetName().Name, resource)
                                     where !string.IsNullOrWhiteSpace(name)
                                     select new EmbeddedAssetFsFile(name, assembly, resource))
                         .ToList();

            _knownAssets = new Dictionary <string, EmbeddedAssetFsFile>();
            foreach (var asset in assets)
            {
                _knownAssets[asset.Name] = asset;
            }
        }
示例#5
0
        /// <summary>
        /// The attempt load.
        /// </summary>
        /// <param name="path">
        /// The path.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        public IRawAsset AttemptLoad(string path, string name, ref DateTime?lastModified, bool noTranslate = false)
        {
            var file1 =
                new FileInfo(
                    Path.Combine(
                        path,
                        TargetPlatformUtility.GetExecutingPlatform().ToString(),
                        (noTranslate ? name : name.Replace('.', Path.DirectorySeparatorChar)) + ".bin"));
            var attempt1 = this.AttemptLoadOfFile(file1, name);

            if (attempt1 != null)
            {
                lastModified = file1.LastWriteTime;
                return(attempt1);
            }

            var file2 =
                new FileInfo(
                    Path.Combine(path, (noTranslate ? name : name.Replace('.', Path.DirectorySeparatorChar)) + ".bin"));
            var attempt2 = this.AttemptLoadOfFile(file2, name);

            if (attempt2 != null)
            {
                lastModified = file2.LastWriteTime;
            }

            return(attempt2);
        }
示例#6
0
        public System.Collections.Generic.IEnumerable <string> GetPotentialPaths(string path, string name, bool noTranslate = false)
        {
            yield return(Path.Combine(
                             path,
                             TargetPlatformUtility.GetExecutingPlatform().ToString(),
                             (noTranslate ? name : name.Replace('.', Path.DirectorySeparatorChar)) + ".bin"));

            yield return(Path.Combine(path, (noTranslate ? name : name.Replace('.', Path.DirectorySeparatorChar)) + ".bin"));
        }
示例#7
0
        /// <summary>
        /// The attempt load.
        /// </summary>
        /// <param name="path">
        /// The path.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        /// <exception cref="InvalidDataException">
        /// </exception>
        public IRawAsset AttemptLoad(string path, string name, ref DateTime?lastModified, bool noTranslate = false)
        {
            lastModified = new DateTime(1970, 1, 1, 0, 0, 0);

            var embedded = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
                            where !assembly.IsDynamic
                            from resource in assembly.GetManifestResourceNames()
                            where
                            resource
                            == assembly.GetName().Name + "." + name + "-"
                            + TargetPlatformUtility.GetExecutingPlatform() + ".bin" ||
                            resource
                            == assembly.GetName().Name + ".Resources." + name + "-"
                            + TargetPlatformUtility.GetExecutingPlatform() + ".bin"
                            select assembly.GetManifestResourceStream(resource)).ToList();

            if (embedded.Any())
            {
                using (var reader = new BinaryReader(embedded.First()))
                {
                    switch (reader.ReadByte())
                    {
                    case CompiledAsset.FORMAT_LZMA_COMPRESSED:
                        using (var memory = new MemoryStream())
                        {
                            LzmaHelper.Decompress(reader.BaseStream, memory);
                            memory.Seek(0, SeekOrigin.Begin);
                            var serializer = new CompiledAssetSerializer();
                            var result     = (CompiledAsset)serializer.Deserialize(memory, null, typeof(CompiledAsset));
                            return(result);
                        }

                    case CompiledAsset.FORMAT_UNCOMPRESSED:
                        var ucserializer = new CompiledAssetSerializer();
                        var ucresult     = (CompiledAsset)ucserializer.Deserialize(reader.BaseStream, null, typeof(CompiledAsset));
                        return(ucresult);

                    default:
                        throw new InvalidDataException();
                    }
                }
            }

            return(null);
        }
示例#8
0
 /// <summary>
 /// The attempt load.
 /// </summary>
 /// <param name="path">
 /// The path.
 /// </param>
 /// <param name="name">
 /// The name.
 /// </param>
 /// <returns>
 /// The <see cref="object"/>.
 /// </returns>
 public IRawAsset AttemptLoad(string path, string name, ref DateTime?lastModified, bool noTranslate = false)
 {
     try
     {
         var stream1 = global::Android.App.Application.Context.Assets.Open(
             TargetPlatformUtility.GetExecutingPlatform().ToString() + Path.DirectorySeparatorChar +
             (noTranslate ? name : name.Replace('.', Path.DirectorySeparatorChar)) + ".bin");
         return(this.AttemptLoadOfStream(stream1, name));
     } catch (Java.IO.FileNotFoundException)
     {
         try
         {
             var stream2 = global::Android.App.Application.Context.Assets.Open(
                 (noTranslate ? name : name.Replace('.', Path.DirectorySeparatorChar)) + ".bin");
             return(this.AttemptLoadOfStream(stream2, name));
         } catch (Java.IO.FileNotFoundException)
         {
             return(null);
         }
     }
 }
示例#9
0
 /// <summary>
 /// The handle.
 /// </summary>
 /// <param name="asset">
 /// The asset.
 /// </param>
 /// <param name="force">
 /// The force.
 /// </param>
 /// <returns>
 /// The <see cref="IAsset"/>.
 /// </returns>
 public void Handle(IAsset asset, bool force = false)
 {
     this.HandlePlatform(asset, TargetPlatformUtility.GetExecutingPlatform(), force);
 }