Exemplo n.º 1
0
        public static void Process(Context ctx)
        {
            var accessNeeded = FileAccess.Read;

            if (ctx.Options.SignSave || ctx.Options.ReplaceFileDest != null && ctx.Options.ReplaceFileSource != null ||
                ctx.Options.RepackSource != null || ctx.Options.TrimSave)
            {
                accessNeeded = FileAccess.ReadWrite;
            }

            using (var file = new LocalStorage(ctx.Options.InFile, accessNeeded))
            {
                bool signNeeded = ctx.Options.SignSave;

                var save             = new SaveDataFileSystem(ctx.Keyset, file, ctx.Options.IntegrityLevel, true);
                FileSystemManager fs = ctx.Horizon.Fs;

                fs.Register("save", save);

                if (ctx.Options.Validate)
                {
                    save.Verify(ctx.Logger);
                }

                if (ctx.Options.OutDir != null)
                {
                    fs.Register("output", new LocalFileSystem(ctx.Options.OutDir));

                    FsUtils.CopyDirectoryWithProgress(fs, "save:/", "output:/", logger: ctx.Logger);

                    fs.Unmount("output");
                }

                if (ctx.Options.DebugOutDir != null)
                {
                    string dir = ctx.Options.DebugOutDir;

                    ExportSaveDebug(ctx, dir, save);
                }

                try
                {
                    if (ctx.Options.ReplaceFileDest != null && ctx.Options.ReplaceFileSource != null)
                    {
                        string destFilename = ctx.Options.ReplaceFileDest;
                        if (!destFilename.StartsWith("/"))
                        {
                            destFilename = '/' + destFilename;
                        }

                        using (IFile inFile = new LocalFile(ctx.Options.ReplaceFileSource, OpenMode.Read))
                        {
                            using (IFile outFile = save.OpenFile(destFilename, OpenMode.ReadWrite))
                            {
                                if (inFile.GetSize() != outFile.GetSize())
                                {
                                    outFile.SetSize(inFile.GetSize());
                                }

                                inFile.CopyTo(outFile, ctx.Logger);

                                ctx.Logger.LogMessage($"Replaced file {destFilename}");
                            }
                        }

                        signNeeded = true;
                    }

                    if (ctx.Options.RepackSource != null)
                    {
                        fs.Register("input", new LocalFileSystem(ctx.Options.RepackSource));

                        fs.CleanDirectoryRecursively("save:/");
                        fs.Commit("save");

                        FsUtils.CopyDirectoryWithProgress(fs, "input:/", "save:/", logger: ctx.Logger);

                        fs.Commit("save");
                        fs.Unmount("input");

                        signNeeded = true;
                    }
                }
                finally
                {
                    if (signNeeded)
                    {
                        save.Commit(ctx.Keyset);
                        signNeeded = false;
                    }
                }

                if (ctx.Options.TrimSave)
                {
                    save.FsTrim();
                    signNeeded = true;
                    ctx.Logger.LogMessage("Trimmed save file");
                }

                if (signNeeded)
                {
                    if (save.Commit(ctx.Keyset))
                    {
                        ctx.Logger.LogMessage("Successfully signed save file");
                    }
                    else
                    {
                        ctx.Logger.LogMessage("Unable to sign save file. Do you have all the required keys?");
                    }

                    fs.Unmount("save");
                    return;
                }

                if (ctx.Options.ListFiles)
                {
                    foreach (DirectoryEntry entry in save.EnumerateEntries())
                    {
                        ctx.Logger.LogMessage(entry.FullPath);
                    }
                }

                ctx.Logger.LogMessage(save.Print());
                //ctx.Logger.LogMessage(PrintFatLayout(save.SaveDataFileSystemCore));

                fs.Unmount("save");
            }
        }