static List <MaterialPatch> DiffMaterials(BflytFile _or, BflytFile _ed, string layoutname)
        {
            (BflytMaterial.TextureReference, BflytMaterial.TextureTransofrm)? OriginalTex(string name, BflytMaterial m)
            {
                var id = _or.Tex1.Textures.IndexOf(name);

                if (id == -1)
                {
                    return(null);
                }

                if (!m.Textures.Any(x => x.TextureId == id))
                {
                    return(null);
                }

                var tx = m.Textures.First(x => x.TextureId == id);
                var tr = m.TextureTransformations[Array.IndexOf(m.Textures, tx)];

                return(tx, tr);
            }

            List <MaterialPatch> materials = new List <MaterialPatch>();

            if (_ed.Mat1 != null && _or.Mat1 != null)
            {
                var edMat = _ed.Mat1;
                foreach (var orM in _or.Mat1.Materials)
                {
                    var edM = edMat.Materials.Where(x => x.Name == orM.Name).FirstOrDefault();
                    if (edM == null)
                    {
                        continue;
                    }

                    MaterialPatch m = new MaterialPatch();

                    if (edM.ForegroundColor != orM.ForegroundColor)
                    {
                        m.ForegroundColor = edM.ForegroundColor.AsHexLEString();
                    }

                    if (edM.BackgroundColor != orM.BackgroundColor)
                    {
                        m.BackgroundColor = edM.BackgroundColor.AsHexLEString();
                    }

                    var transforms = new List <MaterialPatch.TexTransform>();
                    var refs       = new List <MaterialPatch.TexReference>();
                    for (int i = 0; i < edM.Textures.Length; i++)
                    {
                        var tx  = edM.Textures[i];
                        var tf  = edM.TextureTransformations[i];
                        var tex = _ed.Tex1.Textures[tx.TextureId];

                        var original = OriginalTex(tex, orM);

                        if (original != null)
                        {
                            var(ortx, ortf) = original.Value;

                            if (ortf != tf)
                            {
                                transforms.Add(new MaterialPatch.TexTransform()
                                {
                                    Name     = tex,
                                    Rotation = ortf.Rotation == tf.Rotation ? (float?)null : tf.Rotation,
                                    ScaleX   = ortf.ScaleX == tf.ScaleX ? (float?)null : tf.ScaleX,
                                    ScaleY   = ortf.ScaleY == tf.ScaleY ? (float?)null : tf.ScaleY,
                                    X        = ortf.X == tf.X ? (float?)null : tf.X,
                                    Y        = ortf.Y == tf.Y ? (float?)null : tf.Y
                                });
                            }
                            ;

                            if (ortx != tx)
                            {
                                refs.Add(new MaterialPatch.TexReference()
                                {
                                    Name  = tex,
                                    WrapS = (byte)tx.WrapS,
                                    WrapT = (byte)tx.WrapT
                                });
                            }
                        }
                        else
                        {
                            throw new Exception($"A texture reference to {tex} in the edited layout is not part of the original file: {layoutname}");
                        }
                    }

                    m.Transforms = transforms.Count == 0 ? null : transforms.ToArray();
                    m.Refs       = refs.Count == 0 ? null : refs.ToArray();

                    if (m.IsEmpty())
                    {
                        continue;
                    }

                    m.MaterialName = orM.Name;
                    materials.Add(m);
                }
            }

            if (materials.Count == 0)
            {
                materials = null;
            }

            return(materials);
        }
Exemple #2
0
        static List <MaterialPatch> DiffMaterials(BflytFile _or, BflytFile _ed, string layoutname)
        {
            (BflytMaterial.TextureReference, BflytMaterial.TextureTransofrm)? OriginalTex(string name, BflytMaterial m)
            {
                var id = _or.Tex1.Textures.IndexOf(name);

                if (id == -1)
                {
                    return(null);
                }

                if (!m.Textures.Any(x => x.TextureId == id))
                {
                    return(null);
                }

                var tx = m.Textures.First(x => x.TextureId == id);
                var tr = m.TextureTransformations[Array.IndexOf(m.Textures, tx)];

                return(tx, tr);
            }

            List <MaterialPatch> materials = new List <MaterialPatch>();

            if (_ed.Mat1 != null && _or.Mat1 != null)
            {
                var edMat = _ed.Mat1;
                foreach (var orM in _or.Mat1.Materials)
                {
                    var edM = edMat.Materials.Where(x => x.Name == orM.Name).FirstOrDefault();
                    if (edM == null)
                    {
                        continue;
                    }

                    MaterialPatch m = new MaterialPatch();

                    if (edM.ForegroundColor != orM.ForegroundColor)
                    {
                        m.ForegroundColor = edM.ForegroundColor.AsHexLEString();
                    }

                    if (edM.BackgroundColor != orM.BackgroundColor)
                    {
                        m.BackgroundColor = edM.BackgroundColor.AsHexLEString();
                    }

                    var transforms = new List <MaterialPatch.TexTransform>();
                    var refs       = new List <MaterialPatch.TexReference>();
                    for (int i = 0; i < edM.Textures.Length; i++)
                    {
                        var tx  = edM.Textures[i];
                        var tf  = edM.TextureTransformations[i];
                        var tex = _ed.Tex1.Textures[tx.TextureId];

                        var original = OriginalTex(tex, orM);

                        if (original != null)
                        {
                            var(ortx, ortf) = original.Value;

                            if (ortf != tf)
                            {
                                transforms.Add(new MaterialPatch.TexTransform()
                                {
                                    Name     = tex,
                                    Rotation = ortf.Rotation == tf.Rotation ? (float?)null : tf.Rotation,
                                    ScaleX   = ortf.ScaleX == tf.ScaleX ? (float?)null : tf.ScaleX,
                                    ScaleY   = ortf.ScaleY == tf.ScaleY ? (float?)null : tf.ScaleY,
                                    X        = ortf.X == tf.X ? (float?)null : tf.X,
                                    Y        = ortf.Y == tf.Y ? (float?)null : tf.Y
                                });
                            }
                            ;

                            if (ortx != tx)
                            {
                                refs.Add(new MaterialPatch.TexReference()
                                {
                                    Name  = tex,
                                    WrapS = (byte)tx.WrapS,
                                    WrapT = (byte)tx.WrapT
                                });
                            }
                        }

                        /*
                         *      At some point they started adding multiple materials with the same name but different texture references, this breaks
                         *      the following assumption:
                         *
                         *      else throw new Exception($"A texture reference to {tex} in the edited layout is not part of the original file: {layoutname}");
                         *
                         *      For now i'm leaving diffing commented so diffing doesn't crash but materials are probably to be considered broken until this is rewritten.
                         *      Guess an option could be matching materials by index.
                         */
                    }

                    m.Transforms = transforms.Count == 0 ? null : transforms.ToArray();
                    m.Refs       = refs.Count == 0 ? null : refs.ToArray();

                    if (m.IsEmpty())
                    {
                        continue;
                    }

                    m.MaterialName = orM.Name;
                    materials.Add(m);
                }
            }

            if (materials.Count == 0)
            {
                materials = null;
            }

            return(materials);
        }