Ejemplo n.º 1
0
                // Mod mode - use ModResources facilities to load textures
                public CollectionGenerator(ModLoader.ModInfo mod_info, string base_dir, YAML.Collection mapping, GameObject gameobj)
                {
                    TargetGameObject = gameobj;
                    Mapping          = mapping;

                    // initialize texture array and spritesheet mappings
                    Logger.Debug("Generating spritesheet->id->texture mapping");
                    var tex_list = new List <Texture2D>();

                    // first the general spritesheet, if it exists
                    if (mapping.Spritesheet != null)
                    {
                        _SpritesheetID(mapping.Spritesheet);
                        tex_list.Add(mod_info.LoadTexture(Path.Combine(base_dir, mapping.Spritesheet)));
                        Logger.Debug($"New spritesheet: '{mapping.Spritesheet}', ID: {_LastSpritesheetID - 1}");
                    }

                    // ...then the clip spritesheets
                    foreach (var def in mapping.Definitions)
                    {
                        if (def.Value.Spritesheet != null)
                        {
                            _SpritesheetID(def.Value.Spritesheet);
                            tex_list.Add(mod_info.LoadTexture(Path.Combine(base_dir, def.Value.Spritesheet)));
                            Logger.Debug($"New spritesheet: '{def.Value.Spritesheet}', ID: {_LastSpritesheetID - 1}");
                        }
                    }

                    // and then turn the list into an array
                    Textures = tex_list.ToArray();
                }
Ejemplo n.º 2
0
                // Raw mode - you provide the path->texture map
                public CollectionGenerator(Dictionary <string, Texture2D> textures, string base_dir, YAML.Collection mapping, GameObject gameobj)
                {
                    TargetGameObject = gameobj;
                    Mapping          = mapping;

                    var tex_list = new List <Texture2D>();

                    // first the general spritesheet, if it exists
                    if (mapping.Spritesheet != null)
                    {
                        _SpritesheetID(mapping.Spritesheet);
                        tex_list.Add(textures[mapping.Spritesheet]);
                        Logger.Debug($"New spritesheet: '{mapping.Spritesheet}', ID: {_LastSpritesheetID - 1}");
                    }

                    // ...then the clip spritesheets
                    foreach (var def in mapping.Definitions)
                    {
                        if (def.Value.Spritesheet != null)
                        {
                            _SpritesheetID(def.Value.Spritesheet);
                            tex_list.Add(textures[def.Value.Spritesheet]);
                            Logger.Debug($"New spritesheet: '{def.Value.Spritesheet}', ID: {_LastSpritesheetID - 1}");
                        }
                    }

                    Textures = tex_list.ToArray();
                }
Ejemplo n.º 3
0
            public Collection(Dictionary <string, Texture2D> textures, YAML.Collection deserialized, string base_dir)
            {
                _DeserializedYAMLDoc = deserialized;
                GameObject           = new GameObject($"ModTheGungeon Collection '{deserialized.Name}'");

                _Generator     = new CollectionGenerator(textures, base_dir, _DeserializedYAMLDoc, GameObject);
                CollectionData = _Generator.ConstructCollection();
            }
Ejemplo n.º 4
0
            public Collection(ModLoader.ModInfo info, YAML.Collection deserialized, string base_dir = null)
            {
                _DeserializedYAMLDoc = deserialized;
                if (base_dir == null)
                {
                    base_dir = info.Resources.BaseDir;
                }

                GameObject = new GameObject($"ModTheGungeon Collection '{deserialized.Name}'");

                _Generator     = new CollectionGenerator(info, base_dir, _DeserializedYAMLDoc, GameObject);
                CollectionData = _Generator.ConstructCollection();
            }
            public static DumpData Dump(tk2dSpriteCollectionData coll)
            {
                var textures = new Dictionary <string, Texture>();
                var textureid_to_texture_map = new Dictionary <int, string>();
                var texture_datas            = new Dictionary <int, GridPacker.PackedData>();

                var yml = new YAML.Collection();

                // doesn't provide a legal name through command
                if (coll == null)
                {
                    return(new DumpData(yml, textures));
                }

                string first_spritesheet = "[ERROR! ERROR! ERROR!]";

                // TODO
                // get the list of textures in a better way (using the
                // actual textures instead of materials, that is, making sure
                // there are no duplicate textures)

                Console.WriteLine($"PREMAT");


                Material first_material = coll.material;

                if (coll.materials != null && coll.materials.Length == 1)
                {
                    first_material = coll.materials[0];
                }

                Console.WriteLine($"PREMAT2");
                if (coll.materials == null || coll.materials.Length == 1)
                {
                    Console.WriteLine($"IF");
                    first_spritesheet           = coll.spriteCollectionName;
                    textures[first_spritesheet] = first_material.mainTexture;
                    textureid_to_texture_map[0] = first_spritesheet;
                }
                else
                {
                    for (int i = 0; i < coll.materialInsts.Length; i++)
                    {
                        _Logger.Debug($"MATERIALINST: [{coll.materialInsts[i]}]");
                    }
                    for (int i = 0; i < coll.materials.Length; i++)
                    {
                        _Logger.Debug($"MATERIAL: [{coll.materials[i]}]");
                    }

                    Console.WriteLine($"ELSE");
                    for (int i = 0; i < coll.materialInsts.Length; i++)
                    {
                        _Logger.Debug($"Digging in material ID {i}");
                        var mat = coll.materialInsts[i];

                        var spritesheet_name = $"{coll.spriteCollectionName}_{i}";
                        if (i == 0)
                        {
                            first_spritesheet = spritesheet_name;
                        }

                        List <GridPacker.Element> elements = null;
                        for (int j = 0; j < coll.spriteDefinitions.Length; j++)
                        {
                            _Logger.Debug($"Sprite definition #{j}, looking for material ID {i}");
                            var idef = coll.spriteDefinitions[j];
                            if (idef == null)
                            {
                                continue;
                            }

                            _Logger.Debug($"Definition: {idef.name}, materialId: {idef.materialId}, wanted materialId: {i}");
                            _Logger.Debug($"post def");
                            if (idef.materialId != i)
                            {
                                continue;
                            }
                            _Logger.Debug($"Found element for spritesheet ID {i}: {idef.name}");
                            if (elements == null)
                            {
                                elements = new List <GridPacker.Element>();
                            }
                            _Logger.Debug($"Adding grid packer element {idef.name}");
                            elements.Add(new GridPacker.Element((Texture2D)idef.material.mainTexture, idef.uvs, idef.name));
                        }

                        _Logger.Debug($"Looked through all spriteDefs");

                        if (elements != null)
                        {
                            _Logger.Debug($"Sorting elements");
                            elements.Sort((x, y) => string.Compare(x.Name, y.Name));
                            _Logger.Debug($"Packing {elements.Count} entries for spritesheet ID {i}");
                            GridPacker.PackedData data = GridPacker.Pack(elements, force_pot: true);
                            texture_datas[i]           = data;
                            textures[spritesheet_name] = data.Output;
                        }
                        else
                        {
                            _Logger.Debug($"elements == null mat:[{mat}] mat.mainTexture:[{mat?.mainTexture}]");
                            if (mat != null)
                            {
                                textures[spritesheet_name] = ((Texture2D)mat.mainTexture).GetRW();
                            }
                            else
                            {
                                _Logger.Warn($"Material ID #{i} is null and has no assigned sprite defs, it will therefore be ignored");
                            }
                        }

                        _Logger.Debug($"Mapping ID {i} to texture {spritesheet_name}");
                        textureid_to_texture_map[i] = spritesheet_name;
                    }
                }

                yml.Name        = coll.spriteCollectionName;
                yml.Spritesheet = first_spritesheet;
                yml.Definitions = new Dictionary <string, YAML.Definition>();

                HashSet <string> finished_entries = new HashSet <string>();

                //var elements = new List<GridPacker.Element>();
                for (int i = 0; i < coll.spriteDefinitions.Length; i++)
                {
                    var def = coll.spriteDefinitions[i];

                    GridPacker.PackedData data;
                    if (!texture_datas.TryGetValue(def.materialId, out data))
                    {
                        throw new Exception($"Couldn't get cached PackedData of texture of material with ID {def.materialId} - this should never happen");
                    }

                    _Logger.Debug($"Reading packed texture entry of sprite definition {def.name} ({i})");
                    foreach (var ent in  data.Entries)
                    {
                        if (!finished_entries.Contains(ent.Key))
                        {
                            Console.WriteLine($"{ent.Key}");
                            finished_entries.Add(ent.Key);
                        }
                    }

                    var entry = data.Entries[def.name];

                    Console.WriteLine($"=== UVS ===");
                    Console.WriteLine($"{def.uvs[0].x} {def.uvs[0].y}");
                    Console.WriteLine($"{def.uvs[1].x} {def.uvs[1].y}");
                    Console.WriteLine($"{def.uvs[2].x} {def.uvs[2].y}");
                    Console.WriteLine($"{def.uvs[3].x} {def.uvs[3].y}");

                    for (int j = 0; j < def.uvs.Length; j++)
                    {
                        var uv = def.uvs[j];
                    }
                    var ymldef = yml.Definitions[def.name] = new YAML.Definition();

                    var tex_id           = def.materialId;
                    var spritesheet_name = textureid_to_texture_map[tex_id];
                    var spritesheet_tex  = textures[spritesheet_name];

//                    var etgmod_def = ((patch_tk2dSpriteDefinition)def);

                    ymldef.X = entry.X;
                    ymldef.Y = entry.Y;
                    ymldef.W = entry.Width;
                    ymldef.H = entry.Height;
//                    ymldef.OffsetX = etgmod_def.GetETGModOffsetX();
//                    ymldef.OffsetY = etgmod_def.GetETGModOffsetY();
//                    ymldef.ScaleW = etgmod_def.GetScaleW(spritesheet_tex);
//                    ymldef.ScaleH = etgmod_def.GetScaleH(spritesheet_tex);
                }

                // TODO ymldef.DefSize
                // TODO ymldef.DefScale
                // TODO ymldef.Offset
                // right now always handled per-def
                string packed_textures_dir = System.IO.Path.Combine(Paths.ResourcesFolder, "textures_test/");

                if (!System.IO.Directory.Exists(packed_textures_dir))
                {
                    System.IO.Directory.CreateDirectory(packed_textures_dir);
                }
                foreach (var tex in textures)
                {
                    System.IO.File.WriteAllBytes(System.IO.Path.Combine(packed_textures_dir, $"{tex.Key}.png"), ((Texture2D)tex.Value).EncodeToPNG());
                }
                System.IO.File.WriteAllBytes(System.IO.Path.Combine(Paths.ResourcesFolder, $"{yml.Name}.yml"), System.Text.Encoding.UTF8.GetBytes(YAML.Serializer.Serialize(yml)));
                return(new DumpData(yml, textures));
            }
 public DumpData(YAML.Collection collection, Dictionary <string, Texture> textures)
 {
     Collection = collection;
     Textures   = textures;
 }