Ejemplo n.º 1
0
            static void ProcessObjectProperties <T1>(object obj, BonFile bon_file)
            {
                var obj_type = obj.GetType();

                var section_name = obj.GetType() == typeof(T1) ? "Root" : obj.GetType().Name;

                foreach (var property_info in obj_type.GetProperties())
                {
                    if ((property_info.PropertyType == typeof(string)) ||
                        (property_info.PropertyType == typeof(int)) || (
                            property_info.PropertyType == typeof(float)))
                    {
                        var matching_property = GetMatchingSingleValueProperty(bon_file, section_name, property_info.Name);

                        if (matching_property != null)
                        {
                            SetSingleValueProperty(obj, property_info, matching_property);
                        }
                    }
                    else if (property_info.PropertyType.IsGenericType && property_info.PropertyType.GetGenericTypeDefinition() == typeof(List <>))
                    {
                        var matching_property = GetMatchingListValueProperty(bon_file, section_name, property_info.Name);

                        if (matching_property != null)
                        {
                            SetListValueProperty(obj, property_info, matching_property);
                        }
                    }
                    else if (property_info.PropertyType.IsClass && section_name == "root")
                    {
                        property_info.SetValue(obj, Activator.CreateInstance(property_info.PropertyType));
                        ProcessObjectProperties <T1>(property_info.GetValue(obj), bon_file);
                    }
                }
            }
Ejemplo n.º 2
0
            static ListValueProp GetMatchingListValueProperty(BonFile file, string section_name, string property_name)
            {
                foreach (var value_props in file.Sections[section_name].ListProps)
                {
                    if (NormalizePropertyName(value_props.Key) == NormalizePropertyName(property_name))
                    {
                        return(value_props.Value);
                    }
                }

                return(null);
            }
Ejemplo n.º 3
0
            static SingleValueProp GetMatchingSingleValueProperty(BonFile file, string section_name, string property_name)
            {
                foreach (var value_props in file.Sections[section_name].ValueProps)
                {
                    var normalized_file_property_name  = NormalizePropertyName(value_props.Key);
                    var normalized_param_property_name = NormalizePropertyName(property_name);

                    if (normalized_file_property_name == normalized_param_property_name)
                    {
                        return(value_props.Value);
                    }
                }

                return(null);
            }
Ejemplo n.º 4
0
        public static void Build(string pak_name, string manifest_file_path)
        {
            AssetsPak pak = new AssetsPak();

            BonFile manifest = BonFileReader.Parse(manifest_file_path);

            var output_path = Path.GetDirectoryName(manifest_file_path);

            Console.WriteLine($"Building PAK from manifest file: {manifest_file_path}");

            Console.WriteLine("Parsed BON File:");
            Console.WriteLine(manifest.ToString());

            foreach (var section in manifest.Sections)
            {
                if (section.Key == "Root")
                {
                    continue;
                }

                AssetTypes asset_type;

                Console.WriteLine($"Adding Asset: {section.Key}");

                Console.WriteLine($"Type: {section.Value.ValueProps["type"].GetValue()}");

                try
                {
                    asset_type = (AssetTypes)Enum.Parse(typeof(AssetTypes), section.Value.ValueProps["type"].GetValue());
                }
                catch (Exception)
                {
                    throw new Exception("Unrecognized 'type' property or 'type' property is missing.");
                }

                if (!section.Value.ValueProps.ContainsKey("file_path"))
                {
                    throw new Exception("Missing 'file_path' property.");
                }

                var asset_file_path = section.Value.ValueProps["file_path"].GetValue();

                switch (asset_type)
                {
                case AssetTypes.Font:

                    var font_image_path =
                        Path.Combine(output_path, asset_file_path);

                    var font_image = ResourceLoader.LoadImage(font_image_path);

                    if (!section.Value.ValueProps.ContainsKey("glyph_size"))
                    {
                        throw new Exception("Missing 'glyph_size' property.");
                    }

                    var glyph_size = section.Value.ValueProps["glyph_size"].GetIntValue();

                    var font_data = new FontData()
                    {
                        GlyphSize   = glyph_size,
                        Id          = section.Key,
                        ImageData   = font_image.ImageData,
                        ImageWidth  = font_image.Width,
                        ImageHeight = font_image.Height
                    };

                    pak.AddFont(font_data);

                    break;

                case AssetTypes.SpriteSheet:

                    var sheet_image_path = Path.Combine(output_path, asset_file_path);
                    var sheet_image      = ResourceLoader.LoadImage(sheet_image_path);

                    if (!section.Value.ValueProps.ContainsKey("cell_size"))
                    {
                        throw new Exception("Missing 'cell_size' property.");
                    }

                    var cell_size = section.Value.ValueProps["cell_size"].GetIntValue();

                    Dictionary <string, int> sprite_map = new Dictionary <string, int>();

                    if (section.Value.ListProps.ContainsKey("sprites"))
                    {
                        var sprite_map_pairs = section.Value.ListProps["sprites"].Items;

                        foreach (var sprite_map_pair in sprite_map_pairs)
                        {
                            if (!sprite_map_pair.Contains(':'))
                            {
                                continue;
                            }

                            var values = sprite_map_pair.Split(':');

                            sprite_map.Add(values[0], int.Parse(values[1]));
                        }
                    }

                    var sheet_data = new SpriteSheetData()
                    {
                        Id          = section.Key,
                        CellSize    = cell_size,
                        ImageData   = sheet_image.ImageData,
                        ImageWidth  = sheet_image.Width,
                        ImageHeight = sheet_image.Height,
                        SpriteMap   = sprite_map
                    };

                    pak.AddSheet(sheet_data);

                    break;
                }
            }

            if (!pak_name.Contains(".pak"))
            {
                pak_name += ".pak";
            }

            var pak_file_path = Path.Combine(output_path, pak_name);

            using var pak_output = File.OpenWrite(pak_file_path);

            BinaronConvert.Serialize(pak, pak_output);
        }