Example #1
0
        public static ResourcePak LoadPak(string pak_name)
        {
            var path = Path.Combine(root_path,
                                    !pak_name.Contains(".pak") ? pak_name + ".pak" : pak_name);

            using var file = File.OpenRead(path);

            ResourcePak pak = Serializer.Deserialize <ResourcePak>(file);

            return(pak);
        }
Example #2
0
        public void LoadContentPack(string pak_name)
        {
            ResourcePak pak = ResourceLoader.LoadPak(pak_name);

            if (pak.TotalResourcesCount == 0)
            {
                return;
            }

            int res_name_map_idx = 0;

            m_pak_res_map.Add(pak_name, new string[pak.TotalResourcesCount]);

            if (pak.Images != null)
            {
                foreach (var image_res in pak.Images)
                {
                    Texture2D texture = ResourceLoader.LoadTexture(image_res.Value);
                    m_loaded_resources.Add(texture.Id, texture);
                    m_pak_res_map[pak_name][res_name_map_idx++] = image_res.Key;
                }
            }

            if (pak.Atlases != null)
            {
                foreach (var atlas_res in pak.Atlases)
                {
                    TextureAtlas atlas = ResourceLoader.LoadAtlas(atlas_res.Value);
                    m_loaded_resources.Add(atlas.Id, atlas);
                    m_pak_res_map[pak_name][res_name_map_idx++] = atlas_res.Key;
                }
            }

            if (pak.Fonts != null)
            {
                foreach (var font_res in pak.Fonts)
                {
                    Font font = ResourceLoader.LoadFont(font_res.Value);
                    m_loaded_resources.Add(font.Id, font);
                    m_pak_res_map[pak_name][res_name_map_idx++] = font_res.Key;
                }
            }

            if (pak.Shaders != null)
            {
                foreach (var shader_res in pak.Shaders)
                {
                    ShaderProgram shader = ResourceLoader.LoadShader(shader_res.Value);
                    m_loaded_resources.Add(shader.Id, shader);
                    m_pak_res_map[pak_name][res_name_map_idx++] = shader_res.Key;
                }
            }

            if (pak.TextFiles != null)
            {
                foreach (var txt_res in pak.TextFiles)
                {
                    TextFile text_file = ResourceLoader.LoadTextFile(txt_res.Value);
                    m_loaded_resources.Add(text_file.Id, text_file);
                    m_pak_res_map[pak_name][res_name_map_idx++] = txt_res.Key;
                }
            }

            //foreach (var sfx_res in pak.Sfx)
            //{
            //    Effect effect = _loader.LoadEffect(sfx_res.Value);

            //    _loaded_resources.Add(effect.Id, effect);
            //}

            //foreach (var song_res in pak.Songs)
            //{
            //    Song song = _loader.LoadSong(song_res.Value);

            //    _loaded_resources.Add(song.Id, song);
            //}
        }
Example #3
0
        public static ResourcePak Load(string path)
        {
            var fileBytes = File.ReadAllBytes(path);

            var pakReader = new MessagePackReader(fileBytes);

            // Read Into Nested Object
            pakReader.ReadArrayHeader();

            var pakeName = pakReader.ReadString();

            var pak = new ResourcePak(pakeName);

            int numberImages = pakReader.ReadMapHeader();

            if (numberImages > 0)
            {
                pak.Images = new Dictionary <string, ImageData>();

                for (int i = 0; i < numberImages; ++i)
                {
                    var key = pakReader.ReadString();

                    pakReader.ReadArrayHeader();

                    var imageId = pakReader.ReadString();

                    var imageData   = pakReader.ReadBytes();
                    int imageWidth  = pakReader.ReadInt32();
                    int imageHeight = pakReader.ReadInt32();

                    pak.Images.Add(key, new ImageData()
                    {
                        Id     = imageId,
                        Data   = imageData?.ToArray(),
                        Width  = imageWidth,
                        Height = imageHeight
                    });
                }
            }

            int numberAtlases = pakReader.ReadMapHeader();

            if (numberAtlases > 0)
            {
                pak.Atlases = new Dictionary <string, TextureAtlasData>();

                for (int i = 0; i < numberAtlases; i++)
                {
                    var key = pakReader.ReadString();
                    pakReader.ReadArrayHeader();
                    var atlasId        = pakReader.ReadString();
                    var atlasImageData = pakReader.ReadBytes();
                    int imageWidth     = pakReader.ReadInt32();
                    int imageHeight    = pakReader.ReadInt32();

                    var atlasData = new TextureAtlasData()
                    {
                        Id     = atlasId,
                        Data   = atlasImageData?.ToArray(),
                        Atlas  = new Dictionary <string, SRect>(),
                        Width  = imageWidth,
                        Height = imageHeight,
                    };

                    int rectsCount = pakReader.ReadMapHeader();

                    if (rectsCount > 0)
                    {
                        for (int j = 0; j < rectsCount; ++j)
                        {
                            var rectKey = pakReader.ReadString();

                            pakReader.ReadArrayHeader();
                            var rectX = pakReader.ReadInt32();
                            var rectY = pakReader.ReadInt32();
                            var rectW = pakReader.ReadInt32();
                            var rectH = pakReader.ReadInt32();

                            atlasData.Atlas.Add(rectKey, new SRect(rectX, rectY, rectW, rectH));
                        }
                    }

                    atlasData.RuntimeUpdatable = pakReader.ReadBoolean();


                    pak.Atlases.Add(key, atlasData);
                }
            }

            int shadersCount = pakReader.ReadMapHeader();

            if (shadersCount > 0)
            {
                pak.Shaders = new Dictionary <string, ShaderProgramData>();

                for (int i = 0; i < shadersCount; ++i)
                {
                    var shaderKey = pakReader.ReadString();

                    pakReader.ReadArrayHeader();

                    var      shaderId         = pakReader.ReadString();
                    var      vertexShaderData = pakReader.ReadBytes();
                    var      fragShaderData   = pakReader.ReadBytes();
                    string[] samplers         = null;
                    string[] @params          = null;

                    int samplersCount = pakReader.ReadArrayHeader();

                    if (samplersCount > 0)
                    {
                        samplers = new string[samplersCount];

                        for (int j = 0; j < samplersCount; ++j)
                        {
                            samplers[j] = pakReader.ReadString();
                        }
                    }

                    int paramsCount = pakReader.ReadArrayHeader();

                    if (paramsCount > 0)
                    {
                        @params = new string[paramsCount];

                        for (int k = 0; k < paramsCount; ++k)
                        {
                            @params[k] = pakReader.ReadString();
                        }
                    }

                    pak.Shaders.Add(shaderKey, new ShaderProgramData()
                    {
                        Id             = shaderId,
                        VertexShader   = vertexShaderData?.ToArray(),
                        FragmentShader = fragShaderData?.ToArray(),
                        Samplers       = samplers,
                        Params         = @params
                    });
                }
            }

            int fontsSize = pakReader.ReadMapHeader();

            if (fontsSize > 0)
            {
                pak.Fonts = new Dictionary <string, FontData>();

                for (int i = 0; i < fontsSize; ++i)
                {
                    var fontKey = pakReader.ReadString();

                    pakReader.ReadArrayHeader();

                    var fontId = pakReader.ReadString();

                    var fontData = new FontData()
                    {
                        Id = fontId
                    };

                    pakReader.ReadArrayHeader();

                    var fontImageDataId = pakReader.ReadString();
                    var fontImageData   = pakReader.ReadBytes();
                    int fontImageWidth  = pakReader.ReadInt32();
                    int fontImageHeight = pakReader.ReadInt32();

                    fontData.FontSheet = new ImageData()
                    {
                        Id     = fontImageDataId,
                        Width  = fontImageWidth,
                        Height = fontImageHeight,
                        Data   = fontImageData?.ToArray()
                    };

                    int charCount = pakReader.ReadArrayHeader();

                    if (charCount > 0)
                    {
                        fontData.Chars = new char[charCount];

                        for (int j = 0; j < charCount; ++j)
                        {
                            fontData.Chars[j] = pakReader.ReadChar();
                        }
                    }

                    int glyphRectCount = pakReader.ReadArrayHeader();

                    if (glyphRectCount > 0)
                    {
                        fontData.GlyphRects = new SRect[glyphRectCount];

                        for (int k = 0; k < glyphRectCount; ++k)
                        {
                            pakReader.ReadArrayHeader();

                            int x = pakReader.ReadInt32();
                            int y = pakReader.ReadInt32();
                            int w = pakReader.ReadInt32();
                            int h = pakReader.ReadInt32();

                            fontData.GlyphRects[k] = new SRect(x, y, w, h);
                        }
                    }

                    int glyphCroppingsCount = pakReader.ReadArrayHeader();

                    if (glyphCroppingsCount > 0)
                    {
                        fontData.GlyphCroppings = new SRect[glyphCroppingsCount];

                        for (int l = 0; l < glyphCroppingsCount; ++l)
                        {
                            pakReader.ReadArrayHeader();

                            int x = pakReader.ReadInt32();
                            int y = pakReader.ReadInt32();
                            int w = pakReader.ReadInt32();
                            int h = pakReader.ReadInt32();

                            fontData.GlyphCroppings[l] = new SRect(x, y, w, h);
                        }
                    }

                    int glyphKerningsCount = pakReader.ReadArrayHeader();

                    if (glyphKerningsCount > 0)
                    {
                        fontData.GlyphKernings = new SVec3[glyphKerningsCount];

                        for (int k = 0; k < glyphKerningsCount; ++k)
                        {
                            pakReader.ReadArrayHeader();

                            float x = (float)pakReader.ReadDouble();
                            float y = (float)pakReader.ReadDouble();
                            float z = (float)pakReader.ReadDouble();

                            fontData.GlyphKernings[k] = new SVec3(x, y, z);
                        }
                    }

                    fontData.LineSpacing = pakReader.ReadInt32();
                    fontData.Spacing     = pakReader.ReadInt32();
                    fontData.DefaultChar = pakReader.ReadChar();

                    pak.Fonts.Add(fontKey, fontData);
                }
            }

            int textCount = pakReader.ReadMapHeader();

            if (textCount > 0)
            {
                pak.TextFiles = new Dictionary <string, TextFileData>();

                var textKey = pakReader.ReadString();

                pakReader.ReadArrayHeader();

                var textId = pakReader.ReadString();

                int textRowCount = pakReader.ReadArrayHeader();

                var textData = new TextFileData()
                {
                    Id       = textId,
                    TextData = new byte[textRowCount][]
                };

                for (int i = 0; i < textRowCount; ++i)
                {
                    textData.TextData[i] = pakReader.ReadBytes().Value.ToArray();
                }

                pak.TextFiles.Add(textKey, textData);
            }

            pak.TotalResourcesCount = pakReader.ReadInt32();

            return(pak);
        }