Exemple #1
0
 public static BackgroundWorker SaveMod(UnityParser modParser, string path, bool background)
 {
     return(modParser.WriteArchive(path, false, null, background, true));
 }
Exemple #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);
        }
Exemple #3
0
 public static void WriteUnity3d([DefaultVar] UnityParser parser)
 {
     parser.WriteArchive(parser.FilePath, true, ".unit-y3d", false);
 }