Example #1
0
        private static void ApplyEmip(string[] args)
        {
            HashSet <string> flags    = GetFlags(args);
            string           emipFile = args[1];
            string           rootDir  = args[2];

            if (!File.Exists(emipFile))
            {
                Console.WriteLine($"File {emipFile} does not exist!");
                return;
            }

            InstallerPackageFile instPkg = new InstallerPackageFile();
            FileStream           fs      = File.OpenRead(emipFile);
            AssetsFileReader     r       = new AssetsFileReader(fs);

            instPkg.Read(r, true);

            Console.WriteLine($"Installing emip...");
            Console.WriteLine($"{instPkg.modName} by {instPkg.modCreators}");
            Console.WriteLine(instPkg.modDescription);

            foreach (var affectedFile in instPkg.affectedFiles)
            {
                string affectedFileName = Path.GetFileName(affectedFile.path);
                string affectedFilePath = Path.Combine(rootDir, affectedFile.path);

                if (affectedFile.isBundle)
                {
                    string decompFile = $"{affectedFilePath}.decomp";
                    string modFile    = $"{affectedFilePath}.mod";
                    string bakFile    = GetNextBackup(affectedFilePath);

                    if (bakFile == null)
                    {
                        return;
                    }

                    if (flags.Contains("-md"))
                    {
                        decompFile = null;
                    }

                    Console.WriteLine($"Decompressing {affectedFileName} to {decompFile??"memory"}...");
                    AssetBundleFile       bun  = DecompressBundle(affectedFilePath, decompFile);
                    List <BundleReplacer> reps = new List <BundleReplacer>();

                    foreach (var rep in affectedFile.replacers)
                    {
                        var bunRep = (BundleReplacer)rep;
                        if (bunRep is BundleReplacerFromAssets)
                        {
                            //read in assets files from the bundle for replacers that need them
                            string assetName = bunRep.GetOriginalEntryName();
                            var    bunRepInf = BundleHelper.GetDirInfo(bun, assetName);
                            long   pos       = bun.bundleHeader6.GetFileDataOffset() + bunRepInf.offset;
                            bunRep.Init(bun.reader, pos, bunRepInf.decompressedSize);
                        }
                        reps.Add(bunRep);
                    }

                    Console.WriteLine($"Writing {modFile}...");
                    FileStream       mfs = File.OpenWrite(modFile);
                    AssetsFileWriter mw  = new AssetsFileWriter(mfs);
                    bun.Write(mw, reps, instPkg.addedTypes); //addedTypes does nothing atm

                    mfs.Close();
                    bun.Close();

                    Console.WriteLine($"Swapping mod file...");
                    File.Move(affectedFilePath, bakFile);
                    File.Move(modFile, affectedFilePath);

                    if (!flags.Contains("-kd") && !flags.Contains("-md") && File.Exists(decompFile))
                    {
                        File.Delete(decompFile);
                    }

                    Console.WriteLine($"Done.");
                }
                else //isAssetsFile
                {
                    string modFile = $"{affectedFilePath}.mod";
                    string bakFile = GetNextBackup(affectedFilePath);

                    if (bakFile == null)
                    {
                        return;
                    }

                    FileStream            afs    = File.OpenRead(affectedFilePath);
                    AssetsFileReader      ar     = new AssetsFileReader(afs);
                    AssetsFile            assets = new AssetsFile(ar);
                    List <AssetsReplacer> reps   = new List <AssetsReplacer>();

                    foreach (var rep in affectedFile.replacers)
                    {
                        var assetsReplacer = (AssetsReplacer)rep;
                        reps.Add(assetsReplacer);
                    }

                    Console.WriteLine($"Writing {modFile}...");
                    FileStream       mfs = File.OpenWrite(modFile);
                    AssetsFileWriter mw  = new AssetsFileWriter(mfs);
                    assets.Write(mw, 0, reps, 0, instPkg.addedTypes);

                    mfs.Close();
                    ar.Close();

                    Console.WriteLine($"Swapping mod file...");
                    File.Move(affectedFilePath, bakFile);
                    File.Move(modFile, affectedFilePath);

                    Console.WriteLine($"Done.");
                }
            }

            return;
        }