Ejemplo n.º 1
0
        public static LayoutPatch Diff(SarcData original, SarcData edited)
        {
            List <LayoutFilePatch> Patches = new List <LayoutFilePatch>();

            if (!ScrambledEquals <string>(original.Files.Keys, edited.Files.Keys))
            {
                MessageBox.Show("The provided archives don't have the same files");
                return(null);
            }
            var    targetPatch    = SzsPatcher.DetectSarc(original, DefaultTemplates.templates);
            string skipLayoutName = targetPatch != null ? targetPatch.MainLayoutName : "";

            bool hasAtLeastAnExtraGroup = false;             //Used to detect if animations are properly implemented

            foreach (var f in original.Files.Keys.Where(x => x.EndsWith(".bflyt")))
            {
                if (original.Files[f].SequenceEqual(edited.Files[f]))
                {
                    continue;
                }
                BflytFile        or          = new BflytFile(original.Files[f]);
                BflytFile        ed          = new BflytFile(edited.Files[f]);
                string[]         orPaneNames = or.GetPaneNames();
                string[]         edPaneNames = ed.GetPaneNames();
                List <PanePatch> curFile     = new List <PanePatch>();
                for (int i = 0; i < edPaneNames.Length; i++)
                {
                    if (ed[i].data.Length < 0x4C || IgnorePaneList.Contains(ed[i].name))
                    {
                        continue;
                    }
                    if (f == skipLayoutName && (targetPatch?.targetPanels?.Contains(edPaneNames[i]) ?? false))
                    {
                        continue;
                    }
                    var j = Array.IndexOf(orPaneNames, edPaneNames[i]);
                    if (j == -1)
                    {
                        continue;
                    }

                    PanePatch curPatch = new PanePatch()
                    {
                        PaneName = edPaneNames[i]
                    };

                    curPatch.UsdPatches = MakeUsdPatch(or, i, ed, j);
                    if (ed[i].data.SequenceEqual(or[j].data))
                    {
                        if (curPatch.UsdPatches == null)
                        {
                            continue;
                        }
                        curFile.Add(curPatch);
                        continue;
                    }

                    var orPan = new BflytFile.PropertyEditablePanel(or[j]);
                    var edPan = new BflytFile.PropertyEditablePanel(ed[i]);
                    if (!VecEqual(edPan.Position, orPan.Position))
                    {
                        curPatch.Position = ToNullVec(edPan.Position);
                    }
                    if (!VecEqual(edPan.Rotation, orPan.Rotation))
                    {
                        curPatch.Rotation = ToNullVec(edPan.Rotation);
                    }
                    if (!VecEqual(edPan.Scale, orPan.Scale))
                    {
                        curPatch.Scale = ToNullVec(edPan.Scale);
                    }
                    if (!VecEqual(edPan.Size, orPan.Size))
                    {
                        curPatch.Size = ToNullVec(edPan.Size);
                    }
                    if (edPan.Visible != orPan.Visible)
                    {
                        curPatch.Visible = edPan.Visible;
                    }

                    if (edPan.originX != orPan.originX)
                    {
                        curPatch.OriginX = (byte)edPan.originX;
                    }
                    if (edPan.originY != orPan.originY)
                    {
                        curPatch.OriginY = (byte)edPan.originY;
                    }
                    if (edPan.ParentOriginX != orPan.ParentOriginX)
                    {
                        curPatch.ParentOriginX = (byte)edPan.ParentOriginX;
                    }
                    if (edPan.ParentOriginY != orPan.ParentOriginY)
                    {
                        curPatch.ParentOriginY = (byte)edPan.ParentOriginY;
                    }

                    if (edPan.name == "pic1")
                    {
                        if (edPan.ColorData[0] != orPan.ColorData[0])
                        {
                            curPatch.ColorTL = edPan.ColorData[0].ToString("X8");
                        }
                        if (edPan.ColorData[1] != orPan.ColorData[1])
                        {
                            curPatch.ColorTR = edPan.ColorData[1].ToString("X8");
                        }
                        if (edPan.ColorData[2] != orPan.ColorData[2])
                        {
                            curPatch.ColorBL = edPan.ColorData[2].ToString("X8");
                        }
                        if (edPan.ColorData[3] != orPan.ColorData[3])
                        {
                            curPatch.ColorBR = edPan.ColorData[3].ToString("X8");
                        }
                    }
                    curFile.Add(curPatch);
                }

                List <ExtraGroup> extraGroups = new List <ExtraGroup>();
                string[]          ogPanes     = or.GetGroupNames();
                foreach (var p_ in ed.Panels.Where(x => x is Grp1Pane))
                {
                    var p = ((Grp1Pane)p_);
                    if (ogPanes.Contains(p.GroupName))
                    {
                        continue;
                    }
                    extraGroups.Add(new ExtraGroup()
                    {
                        GroupName = p.GroupName, Panes = p.Panes.ToArray()
                    });
                    hasAtLeastAnExtraGroup = true;
                }
                if (extraGroups.Count == 0)
                {
                    extraGroups = null;
                }

                List <MaterialPatch> materials = new List <MaterialPatch>();
                if (ed.GetMat != null && or.GetMat != null)
                {
                    var edMat = ed.GetMat;
                    foreach (var orM in or.GetMat.Materials)
                    {
                        var edM = edMat.Materials.Where(x => x.Name == orM.Name).FirstOrDefault();
                        if (edM == null)
                        {
                            continue;
                        }
                        if (edM.ForegroundColor == orM.ForegroundColor && edM.BackgroundColor == orM.BackgroundColor)
                        {
                            continue;
                        }
                        MaterialPatch m = new MaterialPatch()
                        {
                            MaterialName = orM.Name
                        };
                        if (edM.ForegroundColor != orM.ForegroundColor)
                        {
                            m.ForegroundColor = edM.ForegroundColor.ToString("X8");
                        }
                        if (edM.BackgroundColor != orM.BackgroundColor)
                        {
                            m.BackgroundColor = edM.BackgroundColor.ToString("X8");
                        }
                        materials.Add(m);
                    }
                }
                if (materials.Count == 0)
                {
                    materials = null;
                }

                if (curFile.Count > 0 || extraGroups?.Count > 0 || materials?.Count > 0)
                {
                    Patches.Add(new LayoutFilePatch()
                    {
                        FileName = f, Patches = curFile.ToArray(), Materials = materials?.ToArray(), AddGroups = extraGroups?.ToArray()
                    });
                }
            }
            if (Patches.Count == 0)             //animation edits depend on bflyt changes so this is relevant
            {
                MessageBox.Show("Couldn't find any difference");
                return(null);
            }

            List <AnimFilePatch> AnimPatches = new List <AnimFilePatch>();

            foreach (var f in original.Files.Keys.Where(x => x.EndsWith(".bflan")))
            {
                if (original.Files[f].SequenceEqual(edited.Files[f]))
                {
                    continue;
                }
                Bflan anim = new Bflan(edited.Files[f]);
                AnimPatches.Add(new AnimFilePatch()
                {
                    FileName = f, AnimJson = BflanSerializer.ToJson(anim)
                });
            }
            if (AnimPatches.Count == 0)
            {
                AnimPatches = null;
            }
            else if (!hasAtLeastAnExtraGroup)
            {
                MessageBox.Show("This theme uses custom animations but doesn't have custom group in the layouts, this means that the nxtheme will work on the firmware it has been developed on but it may break on older or newer ones. It's *highly recommended* to create custom groups to handle animations");
            }

            return(new LayoutPatch()
            {
                PatchName = "diffPatch" + (targetPatch == null ? "" : " for " + targetPatch.TemplateName),
                AuthorName = "autoDiff",
                Files = Patches.ToArray(),
                Anims = AnimPatches?.ToArray(),
                Ready8X = true                 //Aka tell the patcher to not fix this layout
            });
        }
Ejemplo n.º 2
0
        /////////////////////////////
        ////////// Methods //////////
        /////////////////////////////

        /////////////////////////////////////////
        ////////// Callbacks of Editor //////////

        private void OnEnable()
        {
            _materialPatch = target as MaterialPatch;
        }