Exemplo n.º 1
0
        public static LiveContent LoadLiveContent(ContentDefinition definition, DataManager dataManager,
                                                  LiveLoadOptions opts, CacheManager cacheManager = null)
        {
            if (definition == null)
            {
                throw new ArgumentNullException(nameof(definition));
            }
            if (dataManager == null)
            {
                throw new ArgumentNullException(nameof(dataManager));
            }
            definition = (ContentDefinition)definition.Clone();

            // Load meshes
            var meshes = new Dictionary <string, LiveMesh>();

            if (opts.LoadMeshes)
            {
                foreach (var e in definition.MeshPaths)
                {
                    var      path = new Uri(e.Value);
                    LiveMesh mesh;
                    if (!opts.UseMeshCache || (mesh = cacheManager?.GetMesh(path.AbsolutePath)) == null)
                    {
                        using (var stream = dataManager.GetStream(path))
                            mesh = Serializer.Deserialize <LiveMesh>(stream);

                        if (opts.UseMeshCache)
                        {
                            cacheManager?.AddMesh(path.AbsolutePath, mesh);
                        }
                    }

                    meshes.Add(e.Key, mesh);
                }
            }

            // Load animations
            var anims = new Dictionary <string, LiveAnim>();

            if (opts.LoadAnims)
            {
                foreach (var e in definition.AnimPaths)
                {
                    var      path = new Uri(e.Value);
                    LiveAnim anim;
                    if (!opts.UseAnimCache || (anim = cacheManager?.GetAnim(path.AbsolutePath)) == null)
                    {
                        using (var stream = dataManager.GetStream(path))
                            anim = Serializer.Deserialize <LiveAnim>(stream);

                        if (opts.UseAnimCache)
                        {
                            cacheManager?.AddAnim(path.AbsolutePath, anim);
                        }
                    }

                    anims.Add(e.Key, anim);
                }
            }

            // Load textures
            var baseTextures       = new Dictionary <string, Image <Rgba32> >();
            var renderedCoTextures = new Dictionary <string, Image <Rgba32> >();

            if (opts.LoadTextures)
            {
                var maxSize = opts.MaxTextureSize;
                if (maxSize.Height == 0 || maxSize.Height == 0)
                {
                    maxSize = Defaults.Size;
                }
                foreach (var e in definition.TexturePaths)
                {
                    var            path = new Uri(e.Value);
                    Image <Rgba32> tex;
                    if (!opts.UseTextureCache || (tex = cacheManager?.GetTexture(path.AbsolutePath)) == null)
                    {
                        using (var stream = dataManager.GetStream(path))
                            tex = Image.Load <Rgba32>(stream);
                        tex.Mutate(x => x.Resize(new Size(Math.Min(tex.Width, maxSize.Width),
                                                          Math.Min(tex.Height, maxSize.Height))));
                        if (opts.UseTextureCache)
                        {
                            cacheManager?.AddTexture(path.AbsolutePath, tex);
                        }
                    }

                    baseTextures.Add(e.Key, tex);
                }


                // Iterate over rendered coalesced textures
                foreach (var e in definition.CoTextures)
                {
                    renderedCoTextures.Add(e.Key,
                                           CoalesceTexture(baseTextures, e.Value,
                                                           new Size(Math.Min(e.Value.Width, maxSize.Width),
                                                                    Math.Min(e.Value.Height, maxSize.Height))));
                }
            }

            // Load resources
            var resources = new Dictionary <string, byte[]>();

            if (opts.LoadResources)
            {
                foreach (var e in definition.ResourcePaths)
                {
                    var    path = new Uri(e.Value);
                    byte[] resource;
                    if (!opts.UseResourceCache || (resource = cacheManager?.GetResource(path.AbsolutePath)) == null)
                    {
                        using (var stream = dataManager.GetStream(path))
                            using (var ms = new MemoryStream()) {
                                stream.CopyTo(ms);
                                resource = ms.ToArray();
                            }

                        if (opts.UseResourceCache)
                        {
                            cacheManager?.AddResource(path.AbsolutePath, resource);
                        }
                    }

                    resources.Add(e.Key, resource);
                }
            }

            // Load translations
            var translations = new Dictionary <string, Dictionary <string, string> >();

            if (opts.LoadTranslations)
            {
                foreach (var e in definition.TranslationPaths)
                {
                    var path = new Uri(e.Value);
                    Dictionary <string, string> translation;
                    if (!opts.UseTranslationCache ||
                        (translation = cacheManager?.GetTranslation(path.AbsolutePath)) == null)
                    {
                        using (var stream = dataManager.GetStream(path))
                            translation = Serializer.Deserialize <Dictionary <string, string> >(stream);

                        if (opts.UseTranslationCache)
                        {
                            cacheManager?.AddTranslation(path.AbsolutePath, translation);
                        }
                    }

                    translations.Add(e.Key, translation);
                }
            }

            return(new LiveContent {
                Definition = definition,
                Meshes = meshes,
                Anims = anims,
                Textures = baseTextures,
                RenderedCoTextures = renderedCoTextures,
                Resources = resources,
                Translations = translations
            });
        }