コード例 #1
0
        /// <summary>
        /// Load Pixels from file
        /// </summary>
        /// <param name="path">Path to pixels file</param>
        /// <returns>Pixels</returns>
        public static Pixels LoadFromFile(string path)
        {
            Pixels ret = null;

            try
            {
                if (path != null)
                {
                    if (File.Exists(path))
                    {
                        PixelsDataContract pixels_data = null;
                        using (ZipArchive archive = ZipFile.Open(path, ZipArchiveMode.Read))
                        {
                            try
                            {
                                ZipArchiveEntry entry = archive.GetEntry("meta.json");
                                if (entry != null)
                                {
                                    using (Stream stream = entry.Open())
                                    {
                                        pixels_data = serializer.ReadObject(stream) as PixelsDataContract;
                                    }
                                }
                                if (pixels_data != null)
                                {
                                    Dictionary <string, JavascriptContext> scripts = new Dictionary <string, JavascriptContext>();
                                    Dictionary <string, Image>             sprites = new Dictionary <string, Image>();
                                    Dictionary <string, WaveOut>           sounds  = new Dictionary <string, WaveOut>();
                                    foreach (PixelsAssetDataContract asset in pixels_data.Assets)
                                    {
                                        try
                                        {
                                            if (asset != null)
                                            {
                                                EPixelsAssetType pixels_asset_type;
                                                if (Enum.TryParse(asset.Type, out pixels_asset_type))
                                                {
                                                    entry = archive.GetEntry(asset.Entry);
                                                    if (entry != null)
                                                    {
                                                        switch (pixels_asset_type)
                                                        {
                                                        case EPixelsAssetType.Script:
                                                            using (StreamReader reader = new StreamReader(entry.Open()))
                                                            {
                                                                JavascriptContext context = new JavascriptContext();
                                                                context.SetParameter("pixels", new { path, entry = Path.GetFileNameWithoutExtension(asset.Entry) });
                                                                context.Run(reader.ReadToEnd());
                                                                scripts.Add(Path.GetFileNameWithoutExtension(asset.Entry), context);
                                                            }
                                                            break;

                                                        case EPixelsAssetType.Sprite:
                                                            using (Stream stream = entry.Open())
                                                            {
                                                                Image image = Image.FromStream(stream);
                                                                if (image != null)
                                                                {
                                                                    sprites.Add(Path.GetFileNameWithoutExtension(asset.Entry), image);
                                                                }
                                                            }
                                                            break;

                                                        case EPixelsAssetType.Sound:
                                                            using (StreamMediaFoundationReader reader = new StreamMediaFoundationReader(entry.Open()))
                                                            {
                                                                WaveOut wave_out = new WaveOut(WaveCallbackInfo.FunctionCallback());
                                                                wave_out.Init(reader);
                                                                sounds.Add(Path.GetFileNameWithoutExtension(asset.Entry), wave_out);
                                                            }
                                                            break;
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    Console.Error.WriteLine("Pixels asset type \"" + asset.Type + "\" is invalid.");
                                                }
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            Console.Error.WriteLine(e.Message);
                                        }
                                    }
                                    ret = new Pixels(scripts, sprites, sounds);
                                }
                            }
                            catch (Exception e)
                            {
                                Console.Error.WriteLine(e.Message);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
            }
            return(ret);
        }