コード例 #1
0
        /// <summary>
        /// 导入一整个目录的资源文件
        /// </summary>
        /// <param name="directoryPath"></param>
        /// <param name="targetFileName"></param>
        public static void ImportAssets(string directoryPath, string targetFileName)
        {
            if (Directory.Exists(directoryPath))
            {
                var files    = Directory.GetFiles(directoryPath);
                var replList = new List <AssetsReplacer>();

                //清空LOG文件
                logPath = Path.GetFileNameWithoutExtension(targetFileName) + ".txt";
                File.WriteAllText(logPath, "", Encoding.UTF8);

                for (int i = 0; i < files.Length; i++)
                {
                    //锁定范围(920,1000]
                    //if (i >= 920 && i <= 960) continue;
                    //if (i >= 1000 && i <= 1500) continue;
                    //if (i >= int.Parse(arr[0]) && i <= int.Parse(arr[1])) continue;

                    var    splitArr = Path.GetFileNameWithoutExtension(files[i]).Split(new string[] { "--" }, StringSplitOptions.None);
                    string name;

                    if (splitArr.Length == 2)
                    {
                        name = splitArr[1];
                    }
                    else
                    {
                        name = splitArr[3];
                    }

                    var temp = GenReplacerFromMemory(long.Parse(name), files[i], splitArr);
                    //var temp = GenReplacerFromMemoryByJson(long.Parse(name), files[i]);

                    if (temp != null)
                    {
                        replList.Add(temp);
                    }
                }

                var inst   = IEManager.AssetsFileInstance;
                var writer = new AssetsFileWriter(File.OpenWrite(targetFileName));
                //Bundle和普通的分离操作
                if (IEManager.IsBundle)
                {
                    AssetBundleFile          abFile   = inst.parentBundle.file;
                    var                      index    = FindAssetsIDInBundle(inst, abFile);
                    BundleReplacerFromAssets brfa     = new BundleReplacerFromAssets(Path.GetFileName(inst.path), null, inst.file, replList, index);
                    List <BundleReplacer>    bRepList = new List <BundleReplacer>();
                    bRepList.Add(brfa);
                    abFile.Write(writer, bRepList);
                }
                else
                {
                    inst.file.Write(writer, 0, replList, 0);
                }

                writer.Close();
            }
        }
コード例 #2
0
        public void SaveTo(List <AssetsReplacer> replacers, Stream outputStream)
        {
            if (replacers == null)
            {
                throw new ArgumentNullException(nameof(replacers));
            }

            if (outputStream == null)
            {
                throw new ArgumentNullException(nameof(outputStream));
            }

            using var bufferStream    = new MemoryStream();
            using var assetFileWriter = new AssetsFileWriter(bufferStream);

            uint fileId = 0; // ?

            var bundleReplacer = new BundleReplacerFromAssets(
                assetsFile.name, assetsFile.name, assetsFile.file, replacers, fileId
                );

            bundle.file.Write(assetFileWriter, new List <BundleReplacer>()
            {
                bundleReplacer
            });
            assetFileWriter.Flush();

            // Close the stream so the temporary file can be deleted.
            bundle.stream.Close();

            // We can't use stream in AssetsFileWriter or it will be closed when writer is disposed.
            outputStream.Position = 0;
            outputStream.SetLength(0);

            bufferStream.Position = 0;
            bufferStream.CopyTo(outputStream);
        }
コード例 #3
0
        private static object ParseReplacer(AssetsFileReader reader, bool prefReplacersInMemory)
        {
            short replacerType = reader.ReadInt16();
            byte  fileType     = reader.ReadByte();

            if (fileType == 0) //BundleReplacer
            {
                string oldName                  = reader.ReadCountStringInt16();
                string newName                  = reader.ReadCountStringInt16();
                bool   hasSerializedData        = reader.ReadByte() != 0; //guess
                long   replacerCount            = reader.ReadInt64();
                List <AssetsReplacer> replacers = new List <AssetsReplacer>();
                for (int i = 0; i < replacerCount; i++)
                {
                    AssetsReplacer assetReplacer = (AssetsReplacer)ParseReplacer(reader, prefReplacersInMemory);
                    replacers.Add(assetReplacer);
                }

                if (replacerType == 4) //BundleReplacerFromAssets
                {
                    //we have to null the assetsfile here and call init later
                    BundleReplacer replacer = new BundleReplacerFromAssets(oldName, newName, null, replacers, 0);
                    return(replacer);
                }
            }
            else if (fileType == 1)                         //AssetsReplacer
            {
                byte   unknown01       = reader.ReadByte(); //always 1
                int    fileId          = reader.ReadInt32();
                long   pathId          = reader.ReadInt64();
                int    classId         = reader.ReadInt32();
                ushort monoScriptIndex = reader.ReadUInt16();

                List <AssetPPtr> preloadDependencies = new List <AssetPPtr>();
                int preloadDependencyCount           = reader.ReadInt32();
                for (int i = 0; i < preloadDependencyCount; i++)
                {
                    AssetPPtr pptr = new AssetPPtr(reader.ReadInt32(), reader.ReadInt64());
                    preloadDependencies.Add(pptr);
                }

                if (replacerType == 0) //remover
                {
                    AssetsReplacer replacer = new AssetsRemover(fileId, pathId, classId, monoScriptIndex);
                    if (preloadDependencyCount != 0)
                    {
                        replacer.SetPreloadDependencies(preloadDependencies);
                    }

                    return(replacer);
                }
                else if (replacerType == 2) //adder/replacer?
                {
                    Hash128?          propertiesHash = null;
                    Hash128?          scriptHash     = null;
                    ClassDatabaseFile?classData      = null;
                    AssetsReplacer    replacer;

                    bool flag1 = reader.ReadByte() != 0; //no idea, couldn't get it to be 1
                    if (flag1)
                    {
                        throw new NotSupportedException("you just found a file with the mysterious flag1 set, send the file to nes");
                    }

                    bool flag2 = reader.ReadByte() != 0; //has properties hash
                    if (flag2)
                    {
                        propertiesHash = new Hash128(reader);
                    }

                    bool flag3 = reader.ReadByte() != 0; //has script hash
                    if (flag3)
                    {
                        scriptHash = new Hash128(reader);
                    }

                    bool flag4 = reader.ReadByte() != 0; //has cldb
                    if (flag4)
                    {
                        classData = new ClassDatabaseFile();
                        classData.Read(reader);
                    }

                    long bufLength = reader.ReadInt64();
                    if (prefReplacersInMemory)
                    {
                        byte[] buf = reader.ReadBytes((int)bufLength);
                        replacer = new AssetsReplacerFromMemory(fileId, pathId, classId, monoScriptIndex, buf);
                    }
                    else
                    {
                        replacer = new AssetsReplacerFromStream(fileId, pathId, classId, monoScriptIndex, reader.BaseStream, reader.Position, bufLength);
                    }

                    if (propertiesHash != null)
                    {
                        replacer.SetPropertiesHash(propertiesHash.Value);
                    }
                    if (scriptHash != null)
                    {
                        replacer.SetScriptIDHash(scriptHash.Value);
                    }
                    if (scriptHash != null)
                    {
                        replacer.SetTypeInfo(classData, null, false); //idk what the last two are supposed to do
                    }
                    if (preloadDependencyCount != 0)
                    {
                        replacer.SetPreloadDependencies(preloadDependencies);
                    }

                    return(replacer);
                }
            }
            return(null);
        }