Esempio n. 1
0
        public void ComputeMinMaxNormals(object[] nmlMeshIds, AnimatorEditor dstAnimatorEditor, object[] adjacentAnimatorEditorMeshIdPairs, double adjacentSquaredDistance, bool worldCoordinates)
        {
            string path      = Path.GetDirectoryName(Parser.file.Parser.FilePath);
            string file      = Path.GetFileNameWithoutExtension(Parser.file.Parser.FilePath);
            string backupExt = Path.GetExtension(Parser.file.Parser.FilePath);

            backupExt = backupExt == String.Empty ? backupExt = "None" : backupExt.Substring(1);
            backupExt = (string)Properties.Settings.Default["BackupExtension" + backupExt];
            UnityParser   srcFileParser = new UnityParser(path + @"\" + file + ".bak0" + backupExt);
            Unity3dEditor srcFileEditor = new Unity3dEditor(srcFileParser);

            srcFileEditor.GetAssetNames(true);
            Animator srcAnimator = null;

            if (dstAnimatorEditor.Parser.classID() != UnityClassID.Animator)
            {
                Component baseAsset = srcFileParser.Cabinet.Components.Find
                                      (
                    delegate(Component asset)
                {
                    return(asset is NotLoaded && asset.classID() == UnityClassID.GameObject && ((NotLoaded)asset).Name == dstAnimatorEditor.Parser.m_GameObject.instance.m_Name);
                }
                                      );
                srcAnimator = srcFileEditor.OpenVirtualAnimator(srcFileParser.Cabinet.Components.IndexOf(baseAsset));
            }
            else
            {
                Component baseAsset = srcFileParser.Cabinet.Components.Find
                                      (
                    delegate(Component asset)
                {
                    return(asset is NotLoaded && asset.classID() == UnityClassID.Animator && ((NotLoaded)asset).Name == dstAnimatorEditor.Parser.m_GameObject.instance.m_Name);
                }
                                      );
                srcAnimator = srcFileEditor.OpenAnimator(srcFileParser.Cabinet.Components.IndexOf(baseAsset));
            }
            AnimatorEditor srcAnimatorEditor = new AnimatorEditor(srcAnimator);

            Component srcNmlAsset = srcFileParser.Cabinet.Components.Find
                                    (
                delegate(Component asset)
            {
                return(asset is NotLoaded && asset.classID() == UnityClassID.MonoBehaviour && ((NotLoaded)asset).Name == Parser.m_Name);
            }
                                    );
            NmlMonoBehaviour srcNml = srcFileEditor.OpenNmlMonoBehaviour(srcFileParser.Cabinet.Components.IndexOf(srcNmlAsset));

            ComputeMinMaxNormals(nmlMeshIds, dstAnimatorEditor, srcNml, -1, srcAnimatorEditor, adjacentAnimatorEditorMeshIdPairs, adjacentSquaredDistance, worldCoordinates);
        }
Esempio n. 2
0
        public static bool ApplyMod(UnityParser parser, UnityParser modParser, bool saveOriginals)
        {
            UnityParser originals      = new UnityParser(parser);
            int         firstNewPathID = -1;

            for (int i = 0; i < modParser.Cabinet.Components.Count; i++)
            {
                int       pathID = modParser.Cabinet.Components[i].pathID;
                Component comp   = parser.Cabinet.FindComponent(pathID);
                if (comp != null)
                {
                    originals.Cabinet.Components.Add(comp);
                }
                else
                {
                    if (firstNewPathID < 0)
                    {
                        firstNewPathID = pathID;
                    }
                    originals.Cabinet.Components.Add(modParser.Cabinet.Components[i]);
                }
            }
            Match  m             = Regex.Match(modParser.FilePath, @"(--*\d+)\.", RegexOptions.CultureInvariant);
            int    idx           = m.Success ? m.Groups[1].Index : modParser.FilePath.Length;
            string originalsName = modParser.FilePath.Substring(0, idx) + "-org-" + firstNewPathID + Path.GetExtension(parser.FilePath);

            if (File.Exists(originalsName))
            {
                Report.ReportLog(Path.GetFileName(modParser.FilePath) + " has already been applied.");
                return(false);
            }
            if (saveOriginals)
            {
                originals.WriteArchive(originalsName, false, null, false, true);
            }

            bool success = true;

            using (Stream stream = File.OpenRead(modParser.FilePath))
            {
                m = Regex.Match(modParser.FilePath, @"-(-*\d+)\.", RegexOptions.CultureInvariant);
                if (!m.Success || !int.TryParse(m.Groups[1].Value, out firstNewPathID))
                {
                    Report.ReportLog("Warning! Mod filename \"" + Path.GetFileName(modParser.FilePath) + "\" is malformed or lies in a folder with a confusing name.");
                    firstNewPathID = -1;
                }
                List <KeyValuePair <string, AssetInfo> > srcContainer = null;
                for (int i = 0; i < modParser.Cabinet.Components.Count; i++)
                {
                    Component src   = modParser.Cabinet.Components[i];
                    NotLoaded asset = src as NotLoaded;
                    if (asset != null)
                    {
                        src = modParser.Cabinet.LoadComponent(stream, i, asset);
                    }
                    if (firstNewPathID == -1 || src.pathID < firstNewPathID)
                    {
                        Component dest = parser.Cabinet.FindComponent(src.pathID);
                        if (dest.classID2 == src.classID2)
                        {
                            if (src.classID2 == UnityClassID.AssetBundle)
                            {
                                srcContainer = ((AssetBundle)src).m_Container;
                            }
                            else
                            {
                                parser.Cabinet.ReplaceSubfile(dest, src);
                            }

                            if (parser.Cabinet.Bundle != null)
                            {
                                switch (src.classID2)
                                {
                                case UnityClassID.Animator:
                                    parser.Cabinet.Bundle.RegisterForUpdate(((Animator)src).m_GameObject.asset);
                                    break;

                                case UnityClassID.MeshRenderer:
                                case UnityClassID.SkinnedMeshRenderer:
                                case UnityClassID.Material:
                                case UnityClassID.Shader:
                                case UnityClassID.Texture2D:
                                    parser.Cabinet.Bundle.RegisterForUpdate(src);
                                    break;
                                }
                            }
                        }
                        else
                        {
                            Report.ReportLog("Unexpected asset in " + Path.GetFileName(parser.FilePath) + " with PathID=" + src.pathID + " was not replaced.");
                            success = false;
                        }
                    }
                    else
                    {
                        parser.Cabinet.ReplaceSubfile(-1, src, null);

                        if (parser.Cabinet.Bundle != null)
                        {
                            switch (src.classID2)
                            {
                            case UnityClassID.TextAsset:
                            case UnityClassID.Material:
                            case UnityClassID.MonoBehaviour:
                            case UnityClassID.Shader:
                            case UnityClassID.Texture2D:
                                parser.Cabinet.Bundle.AddComponent(src);
                                break;
                            }
                        }
                    }
                }
                if (parser.Cabinet.Bundle != null && modParser.Cabinet.Bundle != null)
                {
                    for (int j = 0; j < srcContainer.Count;)
                    {
                        var       firstEntry = srcContainer[j++];
                        Component firstAsset = ((NotLoaded)firstEntry.Value.asset.asset).replacement;
                        if (firstAsset == null)
                        {
                            continue;
                        }
                        if (parser.Cabinet.Bundle.FindComponent(firstEntry.Key, firstEntry.Value.asset.asset.classID2) == null)
                        {
                            List <Component> entries = new List <Component>();
                            entries.Add(firstAsset);
                            while (j < srcContainer.Count && srcContainer[j].Key == firstEntry.Key && srcContainer[j].Value.preloadIndex == firstEntry.Value.preloadIndex)
                            {
                                Component asset = ((NotLoaded)srcContainer[j++].Value.asset.asset).replacement;
                                entries.Add(asset);
                            }
                            parser.Cabinet.Bundle.AddComponents(firstEntry.Key, entries);
                        }
                        else
                        {
                            parser.Cabinet.Bundle.RegisterForUpdate(firstAsset);
                        }
                    }

                    Unity3dEditor editor = new Unity3dEditor(modParser);
                    foreach (Animator vAnim in editor.VirtualAnimators)
                    {
                        Component gameObj = parser.Cabinet.FindComponent(vAnim.m_GameObject.asset.pathID);
                        parser.Cabinet.Bundle.AddComponent(gameObj);
                    }
                }
            }
            return(success);
        }
Esempio n. 3
0
        public static bool ApplyMod(UnityParser parser, UnityParser modParser, bool saveOriginals)
        {
            UnityParser originals = new UnityParser(parser);
            int firstNewPathID = -1;
            for (int i = 0; i < modParser.Cabinet.Components.Count; i++)
            {
                int pathID = modParser.Cabinet.Components[i].pathID;
                Component comp = parser.Cabinet.FindComponent(pathID);
                if (comp != null)
                {
                    originals.Cabinet.Components.Add(comp);
                }
                else
                {
                    if (firstNewPathID < 0)
                    {
                        firstNewPathID = pathID;
                    }
                    originals.Cabinet.Components.Add(modParser.Cabinet.Components[i]);
                }
            }
            Match m = Regex.Match(modParser.FilePath, @"(--*\d+)\.", RegexOptions.CultureInvariant);
            int idx = m.Success ? m.Groups[1].Index : modParser.FilePath.Length;
            string originalsName = modParser.FilePath.Substring(0, idx) + "-org-" + firstNewPathID + Path.GetExtension(parser.FilePath);
            if (File.Exists(originalsName))
            {
                Report.ReportLog(Path.GetFileName(modParser.FilePath) + " has already been applied.");
                return false;
            }
            if (saveOriginals)
            {
                originals.WriteArchive(originalsName, false, null, false, true);
            }

            bool success = true;
            using (Stream stream = File.OpenRead(modParser.FilePath))
            {
                m = Regex.Match(modParser.FilePath, @"-(-*\d+)\.", RegexOptions.CultureInvariant);
                if (!m.Success || !int.TryParse(m.Groups[1].Value, out firstNewPathID))
                {
                    Report.ReportLog("Warning! Mod filename \"" + Path.GetFileName(modParser.FilePath) + "\" is malformed or lies in a folder with a confusing name.");
                    firstNewPathID = -1;
                }
                List<KeyValuePair<string, AssetInfo>> srcContainer = null;
                for (int i = 0; i < modParser.Cabinet.Components.Count; i++)
                {
                    Component src = modParser.Cabinet.Components[i];
                    NotLoaded asset = src as NotLoaded;
                    if (asset != null)
                    {
                        src = modParser.Cabinet.LoadComponent(stream, i, asset);
                    }
                    if (firstNewPathID == -1 || src.pathID < firstNewPathID)
                    {
                        Component dest = parser.Cabinet.FindComponent(src.pathID);
                        if (dest.classID2 == src.classID2)
                        {
                            if (src.classID2 == UnityClassID.AssetBundle)
                            {
                                srcContainer = ((AssetBundle)src).m_Container;
                            }
                            else
                            {
                                parser.Cabinet.ReplaceSubfile(dest, src);
                            }

                            if (parser.Cabinet.Bundle != null)
                            {
                                switch (src.classID2)
                                {
                                case UnityClassID.Animator:
                                    parser.Cabinet.Bundle.RegisterForUpdate(((Animator)src).m_GameObject.asset);
                                    break;
                                case UnityClassID.MeshRenderer:
                                case UnityClassID.SkinnedMeshRenderer:
                                case UnityClassID.Material:
                                case UnityClassID.Shader:
                                case UnityClassID.Texture2D:
                                    parser.Cabinet.Bundle.RegisterForUpdate(src);
                                    break;
                                }
                            }
                        }
                        else
                        {
                            Report.ReportLog("Unexpected asset in " + Path.GetFileName(parser.FilePath) + " with PathID=" + src.pathID + " was not replaced.");
                            success = false;
                        }
                    }
                    else
                    {
                        parser.Cabinet.ReplaceSubfile(-1, src, null);

                        if (parser.Cabinet.Bundle != null)
                        {
                            switch (src.classID2)
                            {
                            case UnityClassID.TextAsset:
                            case UnityClassID.Material:
                            case UnityClassID.MonoBehaviour:
                            case UnityClassID.Shader:
                            case UnityClassID.Texture2D:
                                parser.Cabinet.Bundle.AddComponent(src);
                                break;
                            }
                        }
                    }
                }
                if (parser.Cabinet.Bundle != null && modParser.Cabinet.Bundle != null)
                {
                    for (int j = 0; j < srcContainer.Count; )
                    {
                        var firstEntry = srcContainer[j++];
                        Component firstAsset = ((NotLoaded)firstEntry.Value.asset.asset).replacement;
                        if (firstAsset == null)
                        {
                            continue;
                        }
                        if (parser.Cabinet.Bundle.FindComponent(firstEntry.Key, firstEntry.Value.asset.asset.classID2) == null)
                        {
                            List<Component> entries = new List<Component>();
                            entries.Add(firstAsset);
                            while (j < srcContainer.Count && srcContainer[j].Key == firstEntry.Key && srcContainer[j].Value.preloadIndex == firstEntry.Value.preloadIndex)
                            {
                                Component asset = ((NotLoaded)srcContainer[j++].Value.asset.asset).replacement;
                                entries.Add(asset);
                            }
                            parser.Cabinet.Bundle.AddComponents(firstEntry.Key, entries);
                        }
                        else
                        {
                            parser.Cabinet.Bundle.RegisterForUpdate(firstAsset);
                        }
                    }

                    Unity3dEditor editor = new Unity3dEditor(modParser);
                    foreach (Animator vAnim in editor.VirtualAnimators)
                    {
                        Component gameObj = parser.Cabinet.FindComponent(vAnim.m_GameObject.asset.pathID);
                        parser.Cabinet.Bundle.AddComponent(gameObj);
                    }
                }
            }
            return success;
        }
Esempio n. 4
0
        public void PasteAllMarked()
        {
            int components = Parser.Cabinet.Components.Count;

            try
            {
                Parser.Cabinet.BeginLoadingSkippedComponents();
                foreach (object obj in Gui.Scripting.Variables.Values)
                {
                    if (obj is Unity3dEditor && (Unity3dEditor)obj != this)
                    {
                        Unity3dEditor       editor  = (Unity3dEditor)obj;
                        HashSet <Component> remove  = new HashSet <Component>();
                        HashSet <Component> replace = new HashSet <Component>();
                        foreach (Component asset in editor.Marked)
                        {
                            Component loaded;
                            if (asset is NotLoaded)
                            {
                                loaded = editor.Parser.Cabinet.LoadComponent(asset.pathID);
                                remove.Add(asset);
                                replace.Add(loaded);
                            }
                            else
                            {
                                loaded = asset;
                            }
                            switch (asset.classID2)
                            {
                            case UnityClassID.Texture2D:
                                Texture2D tex = (Texture2D)loaded;
                                tex.Clone(Parser.Cabinet);
                                break;

                            case UnityClassID.Cubemap:
                                Cubemap cubemap = (Cubemap)loaded;
                                cubemap.Clone(Parser.Cabinet);
                                break;

                            case UnityClassID.Material:
                                Material mat = (Material)loaded;
                                mat.Clone(Parser.Cabinet);
                                break;

                            case UnityClassID.Mesh:
                                Mesh mesh = (Mesh)loaded;
                                mesh.Clone(Parser.Cabinet);
                                break;

                            case UnityClassID.Shader:
                                Shader shader = (Shader)loaded;
                                shader.Clone(Parser.Cabinet);
                                break;

                            case UnityClassID.Sprite:
                                Sprite sprite = (Sprite)loaded;
                                sprite.Clone(Parser.Cabinet);
                                break;

                            case UnityClassID.Animator:
                                Parser.Cabinet.MergeTypeDefinition(loaded.file, UnityClassID.GameObject);
                                Parser.Cabinet.MergeTypeDefinition(loaded.file, UnityClassID.Transform);
                                Animator anim = (Animator)loaded;
                                anim.m_GameObject.instance.Clone(Parser.Cabinet);
                                break;

                            case UnityClassID.GameObject:
                                Parser.Cabinet.MergeTypeDefinition(loaded.file, UnityClassID.GameObject);
                                Parser.Cabinet.MergeTypeDefinition(loaded.file, UnityClassID.Transform);
                                GameObject gameObj = (GameObject)loaded;
                                Component  clone   = gameObj.Clone(Parser.Cabinet);

                                Animator vAnim = new Animator(Parser.Cabinet, 0, 0, 0);
                                vAnim.m_Avatar     = new PPtr <Avatar>((Component)null);
                                vAnim.m_GameObject = new PPtr <GameObject>(clone);
                                if (loaded.file.Bundle.numContainerEntries(gameObj.m_Name, UnityClassID.GameObject) > 1)
                                {
                                    Report.ReportLog("Warning! Animator " + gameObj.m_Name + " has multiple entries in the AssetBundle's Container.");
                                }
                                vAnim.file.Bundle.AddComponent(vAnim.m_GameObject.instance.m_Name, clone);
                                VirtualAnimators.Add(vAnim);
                                if (loaded != asset)
                                {
                                    foreach (Animator a in editor.VirtualAnimators)
                                    {
                                        if (a.m_GameObject.asset == asset)
                                        {
                                            a.m_GameObject = new PPtr <GameObject>(loaded);
                                            break;
                                        }
                                    }
                                }
                                break;

                            case UnityClassID.Avatar:
                                Avatar avatar = (Avatar)loaded;
                                avatar.Clone(Parser.Cabinet);
                                break;

                            case UnityClassID.MonoBehaviour:
                                MonoBehaviour monoB = (MonoBehaviour)loaded;
                                monoB.Clone(Parser.Cabinet);
                                break;

                            case UnityClassID.TextAsset:
                                TextAsset text = (TextAsset)loaded;
                                text.Clone(Parser.Cabinet);
                                break;
                            }
                        }

                        do
                        {
                            HashSet <Tuple <Component, Component> > loopSet = new HashSet <Tuple <Component, Component> >(AssetCabinet.IncompleteClones);
                            AssetCabinet.IncompleteClones.Clear();
                            foreach (var pair in loopSet)
                            {
                                Component  src  = pair.Item1;
                                Component  dest = pair.Item2;
                                Type       t    = src.GetType();
                                MethodInfo info = t.GetMethod("CopyTo", new Type[] { t });
                                info.Invoke(src, new object[] { dest });
                            }
                        }while (AssetCabinet.IncompleteClones.Count > 0);

                        foreach (Component asset in remove)
                        {
                            editor.Marked.Remove(asset);
                        }
                        foreach (Component asset in replace)
                        {
                            editor.Marked.Add(asset);
                        }
                    }
                }
            }
            finally
            {
                Parser.Cabinet.EndLoadingSkippedComponents();
                if (components != Parser.Cabinet.Components.Count)
                {
                    Changed = true;
                }
            }
        }
        public void ComputeUVNormalBlendData(object[] uvnbMeshIds, AnimatorEditor dstAnimatorEditor, object[] adjacentAnimatorEditorMeshIdPairs, double adjacentSquaredDistance, bool worldCoordinates)
        {
            string path      = Path.GetDirectoryName(Parser.file.Parser.FilePath);
            string file      = Path.GetFileNameWithoutExtension(Parser.file.Parser.FilePath);
            string backupExt = Path.GetExtension(Parser.file.Parser.FilePath);

            backupExt = backupExt == String.Empty ? "None" : backupExt.Substring(1);
            backupExt = (string)Properties.Settings.Default["BackupExtension" + backupExt];
            UnityParser   srcFileParser = new UnityParser(path + @"\" + file + ".bak0" + backupExt);
            Unity3dEditor srcFileEditor = new Unity3dEditor(srcFileParser);

            srcFileEditor.GetAssetNames(true);
            Animator srcAnimator = null;

            if (dstAnimatorEditor.Parser.classID() != UnityClassID.Animator)
            {
                Component baseAsset = srcFileParser.Cabinet.Components.Find
                                      (
                    delegate(Component asset)
                {
                    return(asset is NotLoaded && asset.classID() == UnityClassID.GameObject && ((NotLoaded)asset).Name == dstAnimatorEditor.Parser.m_GameObject.instance.m_Name);
                }
                                      );
                srcAnimator = srcFileEditor.OpenVirtualAnimator(srcFileParser.Cabinet.Components.IndexOf(baseAsset));
            }
            else
            {
                Component baseAsset = srcFileParser.Cabinet.Components.Find
                                      (
                    delegate(Component asset)
                {
                    return(asset is NotLoaded && asset.classID() == UnityClassID.Animator && ((NotLoaded)asset).Name == dstAnimatorEditor.Parser.m_GameObject.instance.m_Name);
                }
                                      );
                srcAnimator = srcFileEditor.OpenAnimator(srcFileParser.Cabinet.Components.IndexOf(baseAsset));
            }
            AnimatorEditor srcAnimatorEditor = new AnimatorEditor(srcAnimator);

            Transform srcUVNBFrame = srcAnimatorEditor.Frames.Find
                                     (
                delegate(Transform asset)
            {
                return(asset.m_GameObject.instance.m_Name == Parser.m_GameObject.instance.m_Name);
            }
                                     );
            UVNormalBlendMonoBehaviour srcUVNB = null;

            srcUVNBFrame.m_GameObject.instance.m_Component.Find
            (
                delegate(KeyValuePair <UnityClassID, PPtr <Component> > componentEntry)
            {
                Component asset = componentEntry.Value.asset;
                if (asset is NotLoaded && asset.classID() == UnityClassID.MonoBehaviour && ((NotLoaded)asset).replacement is UVNormalBlendMonoBehaviour)
                {
                    srcUVNB = asset as UVNormalBlendMonoBehaviour;
                    return(true);
                }
                if (asset is UVNormalBlendMonoBehaviour)
                {
                    srcUVNB = (UVNormalBlendMonoBehaviour)asset;
                    return(true);
                }
                return(false);
            }
            );

            ComputeUVNormalBlendData(uvnbMeshIds, dstAnimatorEditor, srcUVNB, -1, srcAnimatorEditor, adjacentAnimatorEditorMeshIdPairs, adjacentSquaredDistance, worldCoordinates);
        }