예제 #1
0
파일: Test.cs 프로젝트: root3x69/Wolvenkit
        static void Main(string[] args)
        {
            int    mode        = -1;
            string bnk         = "";
            string wem         = "";
            int    wid         = -1;
            string folder      = "";
            int    playlist_id = -1;

            int argc = args.Length;

            if (argc < 2)
            {
                show_usage();
            }

            bnk = args[1];

            // preparing
            if (args[0] == "--merge-headers" || args[0] == "--music" || args[0] == "add-new-music" || args[0] == "--playlist-id-from-track" || args[0] == "--export-playlist" || args[0] == "reimport-playlist")
            {
                if (argc != 3)
                {
                    show_usage();
                }
            }
            else
            {
                if (args[0] == "--cue")
                {
                    Console.WriteLine("Entering mode: cue given wav");
                    var wav = args[1];
                    if (string.IsNullOrEmpty(wav))
                    {
                        Console.WriteLine("Invalid wem file");
                        Environment.Exit(0);
                    }

                    Console.WriteLine("Reading WAVE...");
                    var wavinput = new WemFile();
                    wavinput.LoadFromFile(wav, WwAudioFileType.Wav);
                    Console.WriteLine("Done!\n");
                    Console.WriteLine("Preparing WAVE...");
                    wavinput.Generate();
                    wavinput.Calculate_Riff_Size();
                    wavinput.WriteToFile(wav + ".cued");
                    Console.WriteLine("Done!");
                }
                else if (argc != 2)
                {
                    show_usage();
                }
            }

            if (args[0] == "--merge-headers")
            {
                if (argc != 3)
                {
                    show_usage();
                }
                var of = args[1];
                var mf = args[2];
                Console.WriteLine("Original file: " + of);
                Console.WriteLine("Modified file: " + mf);

                var input = new WemFile();
                input.LoadFromFile(of, WwAudioFileType.Wem);
                var output = new WemFile();
                output.LoadFromFile(mf, WwAudioFileType.Wem);
                Console.WriteLine("Files loaded!");

                output.merge_headers(input);
                output.Merge_Datas(input);
                output.Calculate_Riff_Size();
                output.WriteToFile(mf + ".wkitmerge");
                Console.WriteLine("Merging completed!");
            }
            else if (args[0] == "--music")
            {
                mode = Soundbank.MODE_BUILD_MUSIC;
                Console.WriteLine("Entering mode : Build music");
                wem = args[2];
                if (string.IsNullOrEmpty(wem))
                {
                    Console.WriteLine("Invalid wem file");
                    Environment.Exit(0);
                }
            }
            else if (args[0] == "--add-new-music")
            {
                mode = Soundbank.MODE_ADD_NEW_MUSIC;
                Console.WriteLine("Entering mode : Add new music");
                wem = args[2];
                if (string.IsNullOrEmpty(wem))
                {
                    Console.WriteLine("Invalid wem file");
                    Environment.Exit(0);
                }
            }
            else if (args[0] == "--playlist-id-from-track")
            {
                mode = Soundbank.MODE_PLAYLIST_ID;
                Console.WriteLine("Entering mode : Playlist from track");
                if (!int.TryParse(args[2], out wid))
                {
                    Console.WriteLine("ID is not an integer");
                    Environment.Exit(0);
                }
            }
            else if (args[0] == "--export-playlist")
            {
                mode = Soundbank.MODE_EXPORT_PLAYLIST;
                Console.WriteLine("Entering mode : Export playlist");
                if (!int.TryParse(args[2], out playlist_id))
                {
                    Console.WriteLine("Playlist ID is not an integer");
                    Environment.Exit(0);
                }
            }
            else if (args[0] == "--reimport-playlist")
            {
                mode = Soundbank.MODE_REIMPORT_PLAYLIST;
                Console.WriteLine("Entering mode : Reimport playlist");
                if (!int.TryParse(args[2], out playlist_id))
                {
                    Console.WriteLine("Playlist ID is not an integer");
                    Environment.Exit(0);
                }
            }
            else if (args[0] == "--debug")
            {
                mode = Soundbank.MODE_DEBUG;
                Console.WriteLine("Entering mode : Debug mode");
            }
            else
            {
                mode   = Soundbank.MODE_BUILD;
                bnk    = args[0];
                folder = args[1];
            }

            if (String.IsNullOrEmpty(bnk))
            {
                Console.WriteLine("Invalid bnk file");
                Environment.Exit(0);
            }
            Console.WriteLine("Sound bank file is : " + bnk);

            Console.WriteLine("Reading soundbank started");
            Console.WriteLine("----------------------------------");
            Soundbank bank = new Soundbank(bnk);

            bank.readFile();
            Console.WriteLine("Reading soundbank done");

            if (mode == Soundbank.MODE_DEBUG)
            {
                // checked
                Console.WriteLine("");
                bank.debugSound();
            }
            else if (mode == Soundbank.MODE_BUILD_MUSIC)
            {
                Console.WriteLine("Rebuilding new music...");
                bank.rebuild_music(wem);
                bank.build_bnk(bnk + ".rebuilt");
            }
            else if (mode == Soundbank.MODE_ADD_NEW_MUSIC)
            {
                Console.WriteLine("Adding new music...");
            }
            else if (mode == Soundbank.MODE_PLAYLIST_ID)
            {
                // checked
                if (wid != -1)
                {
                    Console.WriteLine("Extracting playlist...");
                    bank.get_playlist_ids((uint)wid);
                }
            }
            else if (mode == Soundbank.MODE_EXPORT_PLAYLIST)
            {
                // checked
                if (playlist_id != -1)
                {
                    Console.WriteLine("Exporting playlist...");
                    bank.export_playlist((uint)playlist_id);
                    Console.WriteLine("Done!");
                }
            }
            else if (mode == Soundbank.MODE_REIMPORT_PLAYLIST)
            {
                if (playlist_id != -1)
                {
                    Console.WriteLine("Reimporting playlist...");
                    bank.reimport_playlist((uint)playlist_id);
                    bank.build_bnk(bnk + ".rebuilt");
                    Console.WriteLine("Done!");
                }
            }
            else
            {
                if (bank._dataIndex == null || !bank._dataIndex._isSet)
                {
                    Console.WriteLine("Soundbank does not contains embedded files");
                }
                Console.WriteLine("Rebuilding sounds...");
                bank.read_wems(folder);
                bank.rebuild_data();
                if (File.Exists(bnk + ".rebuilt"))
                {
                    File.Delete(bnk + ".rebuilt");
                }
                bank.build_bnk(bnk + ".rebuilt");
                Console.WriteLine("Done!");
            }
        }
예제 #2
0
        public async override Task <bool> PackAndInstallProject()
        {
            var ActiveMod = MainController.Get().ActiveMod;
            var _logger   = ServiceLocator.Default.ResolveType <ILoggerService>();

            if (ActiveMod == null)
            {
                return(false);
            }
            if (Process.GetProcessesByName("Witcher3").Length != 0)
            {
                _logger.LogString("Please close The Witcher 3 before tinkering with the files!", Logtype.Error);
                return(false);
            }

            var packsettings = new WolvenKit.Common.Model.WitcherPackSettings();

            if (packsettings != null)
            {
                MainController.Get().ProjectStatus  = EProjectStatus.Busy;
                MainController.Get().StatusProgress = 0;

                //IsToolStripBtnPackEnabled = false;

                //SaveAllFiles();

                //Create the dirs. So script only mods don't die.
                Directory.CreateDirectory(ActiveMod.PackedModDirectory);
                if (!string.IsNullOrEmpty(ActiveMod.GetDlcName()))
                {
                    Directory.CreateDirectory(ActiveMod.PackedDlcDirectory);
                }


                //------------------------PRE COOKING------------------------------------//
                // have a check if somehow users forget to add a dlc folder in their dlc :(
                // but have files inform them that it just not gonna work
                bool initialDlcCheck = true;
                if (ActiveMod.DLCFiles.Any() && string.IsNullOrEmpty(ActiveMod.GetDlcName()))
                {
                    _logger.LogString("Files in your dlc directory need to have the following structure: dlc\\DLCNAME\\files. Dlc will not be packed.", Logtype.Error);
                    initialDlcCheck = false;
                }

                #region Pre Cooking
                //Handle strings.
                //if (packsettings.Strings.Item1 || packsettings.Strings.Item2)
                {
                    //m_windowFactory.RequestStringsGUI(); TODO
                }

                // Cleanup Directories
                WccHelper.CleanupDirectories();

                // Create Virtial Links
                WccHelper.CreateVirtualLinks();

                // analyze files in dlc
                int statusanalyzedlc = -1;

                var seedfile = Path.Combine(ActiveMod.ProjectDirectory, @"cooked", $"seed_dlc{ActiveMod.Name}.files");

                if (initialDlcCheck)
                {
                    if (Directory.GetFiles(ActiveMod.DlcDirectory, "*", SearchOption.AllDirectories).Any())
                    {
                        _logger.LogString($"======== Analyzing dlc files ======== \n", Logtype.Important);
                        if (Directory.GetFiles(ActiveMod.DlcDirectory, "*.reddlc", SearchOption.AllDirectories).Any())
                        {
                            var reddlcfile = Directory.GetFiles(ActiveMod.DlcDirectory, "*.reddlc", SearchOption.AllDirectories).FirstOrDefault();
                            var analyze    = new Wcc_lite.analyze()
                            {
                                Analyzer = analyzers.r4dlc,
                                Out      = seedfile,
                                reddlc   = reddlcfile
                            };
                            statusanalyzedlc *= await Task.Run(() => MainController.Get().WccHelper.RunCommand(analyze));

                            if (statusanalyzedlc == 0)
                            {
                                _logger.LogString("Analyzing dlc failed, creating fallback seedfiles. \n", Logtype.Error);
                                WccHelper.CreateFallBackSeedFile(seedfile);
                            }
                        }
                        else
                        {
                            _logger.LogString("No reddlc found, creating fallback seedfiles. \n", Logtype.Error);
                            WccHelper.CreateFallBackSeedFile(seedfile);
                        }
                    }
                }
                #endregion
                MainController.Get().StatusProgress = 5;

                //------------------------- COOKING -------------------------------------//
                #region Cooking
                int statusCook = -1;

                // cook uncooked files
                var taskCookCol = Task.Run(() => WccHelper.Cook());
                await taskCookCol.ContinueWith(antecedent =>
                {
                    //Logger.LogString($"Cooking Collision ended with status: {antecedent.Result}", Logtype.Important);
                    statusCook = antecedent.Result;
                });

                if (statusCook == 0)
                {
                    _logger.LogString("Cooking collision finished with errors. \n", Logtype.Error);
                }

                #endregion
                MainController.Get().StatusProgress = 15;

                //------------------------- POST COOKING --------------------------------//
                #region Copy Cooked Files
                // copy mod files from Archive (cooked files) to \cooked
                if (Directory.GetFiles(ActiveMod.ModCookedDirectory, "*", SearchOption.AllDirectories).Any())
                {
                    _logger.LogString($"======== Adding cooked mod files ======== \n", Logtype.Important);
                    try
                    {
                        var di    = new DirectoryInfo(ActiveMod.ModCookedDirectory);
                        var files = di.GetFiles("*", SearchOption.AllDirectories);
                        _logger.LogString($"Found {files.Length} files in {di.FullName}. \n");
                        foreach (var fi in files)
                        {
                            string relpath = fi.FullName.Substring(ActiveMod.ModCookedDirectory.Length + 1);
                            string newpath = Path.Combine(ActiveMod.CookedModDirectory, relpath);

                            if (File.Exists(newpath))
                            {
                                _logger.LogString($"Duplicate cooked file found {newpath}. Overwriting. \n", Logtype.Important);
                                File.Delete(newpath);
                            }

                            fi.CopyToAndCreate(newpath);
                            _logger.LogString($"Copied file to cooked directory: {fi.FullName}. \n", Logtype.Normal);
                        }
                    }
                    catch (Exception)
                    {
                        _logger.LogString("Copying cooked mod files finished with errors. \n", Logtype.Error);
                    }
                    finally
                    {
                        _logger.LogString("Finished succesfully. \n", Logtype.Success);
                    }
                }

                // copy dlc files from Archive (cooked files) to \cooked
                if (Directory.GetFiles(ActiveMod.DlcCookedDirectory, "*", SearchOption.AllDirectories).Any())
                {
                    _logger.LogString($"======== Adding cooked dlc files ======== \n", Logtype.Important);
                    try
                    {
                        var di    = new DirectoryInfo(ActiveMod.DlcCookedDirectory);
                        var files = di.GetFiles("*", SearchOption.AllDirectories);
                        _logger.LogString($"Found {files.Length} files in {di.FullName}. \n");
                        foreach (var fi in files)
                        {
                            string relpath = fi.FullName.Substring(ActiveMod.DlcCookedDirectory.Length + 1);
                            string newpath = Path.Combine(ActiveMod.CookedDlcDirectory, relpath);

                            if (File.Exists(newpath))
                            {
                                _logger.LogString($"Duplicate cooked file found {newpath}. Overwriting. \n", Logtype.Important);
                                File.Delete(newpath);
                            }

                            fi.CopyToAndCreate(newpath);
                            _logger.LogString($"Copied file to cooked directory: {fi.FullName}. \n", Logtype.Normal);
                        }
                    }
                    catch (Exception)
                    {
                        _logger.LogString("Copying cooked dlc files finished with errors. \n", Logtype.Error);
                    }
                    finally
                    {
                        _logger.LogString("Finished succesfully. \n", Logtype.Success);
                    }
                }
                #endregion
                MainController.Get().StatusProgress = 20;

                //------------------------- PACKING -------------------------------------//
                #region Packing
                int statusPack = -1;

                //Handle bundle packing.
                if (packsettings.dlcPackBundles || packsettings.modPackBundles)
                {
                    // packing
                    //if (statusCookCol * statusCookTex != 0)
                    {
                        var t = WccHelper.Pack(packsettings.modPackBundles, packsettings.dlcPackBundles);
                        await t.ContinueWith(antecedent =>
                        {
                            //Logger.LogString($"Packing Bundles ended with status: {antecedent.Result}", Logtype.Important);
                            statusPack = (int)antecedent.Status;
                        });

                        if (statusPack == 0)
                        {
                            _logger.LogString("Packing bundles finished with errors. \n", Logtype.Error);
                        }
                    }
                    //else
                    //    Logger.LogString("Cooking assets failed. No bundles will be packed!\n", Logtype.Error);
                }
                #endregion
                MainController.Get().StatusProgress = 40;

                //------------------------ METADATA -------------------------------------//
                #region Metadata
                //Handle metadata generation.
                int statusMetaData = -1;

                if (packsettings.modGenMetadata || packsettings.dlcGenMetadata)
                {
                    if (statusPack == 1)
                    {
                        var t = WccHelper.CreateMetaData(packsettings.modGenMetadata,
                                                         packsettings.dlcGenMetadata);
                        await t.ContinueWith(antecedent =>
                        {
                            statusMetaData = antecedent.Result;
                            //Logger.LogString($"Creating metadata ended with status: {statusMetaData}", Logtype.Important);
                        });

                        if (statusMetaData == 0)
                        {
                            _logger.LogString("Creating metadata finished with errors. \n", Logtype.Error);
                        }
                    }
                    else
                    {
                        _logger.LogString("Packing bundles failed. No metadata will be created!\n", Logtype.Error);
                    }
                }
                #endregion
                MainController.Get().StatusProgress = 50;

                //------------------------ POST COOKING ---------------------------------//

                //---------------------------- CACHES -----------------------------------//
                #region Buildcache
                int statusCol = -1;
                int statusTex = -1;

                //Generate collision cache
                if (packsettings.modGenCollCache || packsettings.dlcGenCollCache)
                {
                    var t = WccHelper.GenerateCache(EArchiveType.CollisionCache, packsettings.modGenCollCache,
                                                    packsettings.dlcGenCollCache);
                    await t.ContinueWith(antecedent =>
                    {
                        statusCol = antecedent.Result;
                        //Logger.LogString($"Building collision cache ended with status: {statusCol}", Logtype.Important);
                    });

                    if (statusCol == 0)
                    {
                        _logger.LogString("Building collision cache finished with errors. \n", Logtype.Error);
                    }
                }

                //Handle texture caching
                if (packsettings.modGenTexCache || packsettings.dlcGenTexCache)
                {
                    var t = WccHelper.GenerateCache(EArchiveType.TextureCache, packsettings.modGenTexCache, packsettings.dlcGenTexCache);
                    await t.ContinueWith(antecedent =>
                    {
                        statusTex = antecedent.Result;
                        //Logger.LogString($"Building texture cache ended with status: {statusTex}", Logtype.Important);
                    });

                    if (statusTex == 0)
                    {
                        _logger.LogString("Building texture cache finished with errors. \n", Logtype.Error);
                    }
                }


                //Handle sound caching
                if (packsettings.modSound || packsettings.dlcSound)
                {
                    if (packsettings.modSound)
                    {
                        var soundmoddir = Path.Combine(ActiveMod.ModDirectory, EArchiveType.SoundCache.ToString());

                        // We need to have the original soundcache's so we can rebuild them when packing the mod
                        foreach (var wem in Directory.GetFiles(soundmoddir, "*.wem", SearchOption.AllDirectories))
                        {
                            // Get the file id so we can search for the parent soundcache
                            var id = Path.GetFileNameWithoutExtension(SoundCache.GetIDFromPath(wem));

                            // Find the parent bank
                            foreach (var bnk in SoundCache.info.Banks)
                            {
                                if (bnk.IncludedFullFiles.Any(x => x.Id == id) || bnk.IncludedPrefetchFiles.Any(x => x.Id == id))
                                {
                                    if (!File.Exists(Path.Combine(soundmoddir, bnk.Path)))
                                    {
                                        //TODO: Fix this somehow
                                        //var bytes = MainController.ImportFile(bnk.Path, MainController.Get().SoundManager);
                                        //File.WriteAllBytes(Path.Combine(soundmoddir, bnk.Path), bytes[0].ToArray());
                                        MainController.Get().Logger.LogString("Imported " + bnk.Path + " for rebuilding with the modded wem files!");
                                    }
                                    break;
                                }
                            }
                        }

                        foreach (var bnk in Directory.GetFiles(soundmoddir, "*.bnk", SearchOption.AllDirectories))
                        {
                            Soundbank bank = new Soundbank(bnk);
                            bank.readFile();
                            bank.read_wems(soundmoddir);
                            bank.rebuild_data();
                            File.Delete(bnk);
                            bank.build_bnk(bnk);
                            _logger.LogString("Rebuilt modded bnk " + bnk, Logtype.Success);
                        }

                        //Create mod soundspc.cache
                        if (Directory.Exists(soundmoddir) &&
                            new DirectoryInfo(soundmoddir)
                            .GetFiles("*.*", SearchOption.AllDirectories)
                            .Where(file => file.Name.ToLower().EndsWith("wem") || file.Name.ToLower().EndsWith("bnk")).Any())
                        {
                            SoundCache.Write(
                                new DirectoryInfo(soundmoddir)
                                .GetFiles("*.*", SearchOption.AllDirectories)
                                .Where(file => file.Name.ToLower().EndsWith("wem") || file.Name.ToLower().EndsWith("bnk"))
                                .ToList().Select(x => x.FullName).ToList(),
                                Path.Combine(ActiveMod.PackedModDirectory, @"soundspc.cache"));
                            _logger.LogString("Mod soundcache generated!\n", Logtype.Important);
                        }
                        else
                        {
                            _logger.LogString("Mod soundcache wasn't generated!\n", Logtype.Important);
                        }
                    }

                    if (packsettings.dlcSound)
                    {
                        var sounddlcdir = Path.Combine(ActiveMod.DlcDirectory, EArchiveType.SoundCache.ToString());

                        //Create dlc soundspc.cache
                        if (Directory.Exists(sounddlcdir) && new DirectoryInfo(sounddlcdir)
                            .GetFiles("*.*", SearchOption.AllDirectories).Any(file => file.Name.ToLower().EndsWith("wem") || file.Name.ToLower().EndsWith("bnk")))
                        {
                            SoundCache.Write(
                                new DirectoryInfo(sounddlcdir)
                                .GetFiles("*.*", SearchOption.AllDirectories)
                                .Where(file => file.Name.ToLower().EndsWith("wem") || file.Name.ToLower().EndsWith("bnk")).ToList().Select(x => x.FullName).ToList(),
                                Path.Combine(ActiveMod.PackedDlcDirectory, @"soundspc.cache"));
                            _logger.LogString("DLC soundcache generated!\n", Logtype.Important);
                        }
                        else
                        {
                            _logger.LogString("DLC soundcache wasn't generated!\n", Logtype.Important);
                        }
                    }
                }
                #endregion
                MainController.Get().StatusProgress = 60;

                //---------------------------- SCRIPTS ----------------------------------//
                #region Scripts
                bool packscriptsMod = packsettings.modScripts;
                bool packscriptsdlc = packsettings.dlcScripts;
                //Handle mod scripts
                if (packscriptsMod && Directory.Exists(Path.Combine(ActiveMod.ModDirectory, "scripts")) && Directory.GetFiles(Path.Combine(ActiveMod.ModDirectory, "scripts"), "*.*", SearchOption.AllDirectories).Any())
                {
                    if (!Directory.Exists(Path.Combine(ActiveMod.ModDirectory, "scripts")))
                    {
                        Directory.CreateDirectory(Path.Combine(ActiveMod.ModDirectory, "scripts"));
                    }
                    //Now Create all of the directories
                    foreach (string dirPath in Directory.GetDirectories(Path.Combine(ActiveMod.ModDirectory, "scripts"), "*.*",
                                                                        SearchOption.AllDirectories))
                    {
                        Directory.CreateDirectory(dirPath.Replace(Path.Combine(ActiveMod.ModDirectory, "scripts"), Path.Combine(ActiveMod.PackedModDirectory, "scripts")));
                    }

                    //Copy all the files & Replaces any files with the same name
                    foreach (string newPath in Directory.GetFiles(Path.Combine(ActiveMod.ModDirectory, "scripts"), "*.*",
                                                                  SearchOption.AllDirectories))
                    {
                        File.Copy(newPath, newPath.Replace(Path.Combine(ActiveMod.ModDirectory, "scripts"), Path.Combine(ActiveMod.PackedModDirectory, "scripts")), true);
                    }
                }

                //Handle the DLC scripts
                if (packscriptsdlc && Directory.Exists(Path.Combine(ActiveMod.DlcDirectory, "scripts")) && Directory.GetFiles(Path.Combine(ActiveMod.DlcDirectory, "scripts"), "*.*", SearchOption.AllDirectories).Any())
                {
                    if (!Directory.Exists(Path.Combine(ActiveMod.DlcDirectory, "scripts")))
                    {
                        Directory.CreateDirectory(Path.Combine(ActiveMod.DlcDirectory, "scripts"));
                    }
                    //Now Create all of the directories
                    foreach (string dirPath in Directory.GetDirectories(Path.Combine(ActiveMod.DlcDirectory, "scripts"), "*.*",
                                                                        SearchOption.AllDirectories))
                    {
                        Directory.CreateDirectory(dirPath.Replace(Path.Combine(ActiveMod.DlcDirectory, "scripts"), Path.Combine(ActiveMod.PackedDlcDirectory, "scripts")));
                    }

                    //Copy all the files & Replaces any files with the same name
                    foreach (string newPath in Directory.GetFiles(Path.Combine(ActiveMod.DlcDirectory, "scripts"), "*.*",
                                                                  SearchOption.AllDirectories))
                    {
                        File.Copy(newPath, newPath.Replace(Path.Combine(ActiveMod.DlcDirectory, "scripts"), Path.Combine(ActiveMod.PackedDlcDirectory, "scripts")), true);
                    }
                }
                #endregion
                MainController.Get().StatusProgress = 80;

                //---------------------------- STRINGS ----------------------------------//
                #region Strings
                //Copy the generated w3strings
                if (packsettings.modStrings || packsettings.dlcStrings)
                {
                    var files = Directory.GetFiles((ActiveMod.ProjectDirectory + "\\strings")).Where(s => Path.GetExtension(s) == ".w3strings").ToList();

                    if (packsettings.modStrings)
                    {
                        files.ForEach(x => File.Copy(x, Path.Combine(ActiveMod.PackedDlcDirectory, Path.GetFileName(x))));
                    }
                    if (packsettings.dlcStrings)
                    {
                        files.ForEach(x => File.Copy(x, Path.Combine(ActiveMod.PackedModDirectory, Path.GetFileName(x))));
                    }
                }
                #endregion
                MainController.Get().StatusProgress = 90;

                //---------------------------- FINALIZE ---------------------------------//

                InstallMod();

                //Report that we are done
                MainController.Get().StatusProgress = 100;
                MainController.Get().ProjectStatus  = EProjectStatus.Ready;
                return(true);
            }
            else
            {
                return(false);
            }
        }