Example #1
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Start: XLPakTool.exe <path_to_game_pak>");
                return;
            }

            var gamePakPath = args[0];

            Console.Title = "XLPakTool - " + gamePakPath;

            Log("Info", "Create file system...");
            if (XLPack.CreateFileSystem())
            {
                Log("Info", "Done");

                Log("Info", "Connect log handler...");
                _logHandler = XLPack.SetFileLogHandler("pack.log", LogHandler);
                Log("Info", "Done");

                MountFileSystem(gamePakPath);
                Thread.Sleep(1000);

                while (true)
                {
                    Console.Write($"~{_globalPath}$ ");
                    var command = Console.ReadLine();
                    var parse   = CommandParser(command);

                    if (parse.Length <= 0)
                    {
                        continue;
                    }

                    var cmd     = parse[0];
                    var cmdArgs = new string[parse.Length - 1];
                    if (cmdArgs.Length > 0)
                    {
                        Array.Copy(parse, 1, cmdArgs, 0, cmdArgs.Length);
                    }

                    if (cmd == "quit")
                    {
                        break;
                    }
                    if (cmd == "exit")
                    {
                        break;
                    }

                    switch (cmd)
                    {
                    case "help":
                        Log("Help", "cd <path> -> move at folders");
                        Log("Help", "ls [path] -> get files");
                        Log("Help", "cp <scr> <dest> -> copy file from src to dest");
                        Log("Help", "rm <path> -> remove path");
                        Log("Help", "fstat <file path> -> Get file stat (doesn't work if one of the fields is missing information)");
                        Log("Help", "fsize <file path> -> Get file size");
                        Log("Help", "fgetmd5 <file path> -> Get file md5 as string");
                        Log("Help", "fstat1 <file path> -> Get file timestamps");
                        Log("Help", "makemd5 <file path> -> Re-calculate and Set the MD5 of a file");
                        Log("Help", "fsetmd5 <hash> <file path> -> Manually enter a value for a file's md5 field");
                        Console.WriteLine("--------------------------------");
                        Log("Help", "To export file(s)/dir:");
                        Log("Help", "cp <src> /fs/<dest>");
                        Log("Help", "To import file(s)/dir:");
                        Log("Help", "cp /fs/<src> <dest>");
                        break;

                    case "cd":
                        if (cmdArgs.Length == 0)
                        {
                            Log("Info", "cd <toDir>");
                        }
                        else
                        {
                            var cmdPath = cmdArgs[0];
                            var prePath = _globalPath;
                            _globalPath = AbsolutePath(cmdPath);
                            if (!_globalPath.EndsWith("/") && !_globalPath.EndsWith("\\"))
                            {
                                _globalPath += "/";
                            }

                            if (!IsDirectory(_globalPath))
                            {
                                _globalPath = prePath;
                            }
                        }

                        break;

                    case "ls":
                        var path = _globalPath;

                        if (cmdArgs.Length > 0)
                        {
                            path  = AbsolutePath(cmdArgs[0]);
                            path += "/";
                        }

                        var files = GetFiles(path);
                        if (files.Count > 0)
                        {
                            foreach (var(file, isDirectory) in files)
                            {
                                if (isDirectory)
                                {
                                    Console.BackgroundColor = ConsoleColor.DarkBlue;
                                }
                                Console.WriteLine(file.Replace(path, ""));
                                Console.ResetColor();
                            }
                        }
                        else
                        {
                            Console.WriteLine("------ EMPTY ------");
                        }

                        break;

                    case "cp":
                        if (cmdArgs.Length < 2)
                        {
                            Log("Info", "cp <src> <dest>");
                        }
                        else
                        {
                            var src  = AbsolutePath(cmdArgs[0]);
                            var dest = AbsolutePath(cmdArgs[1]);

                            bool exist;

                            if (src.StartsWith("/fs"))
                            {
                                var realyPath = src.Replace("/fs", _fsPath);
                                exist = File.Exists(realyPath);
                                if (!exist)
                                {
                                    exist = Directory.Exists(realyPath);
                                }
                            }
                            else
                            {
                                exist = IsPathExist(src);
                            }

                            if (!exist)
                            {
                                Log("Warn", "Bad source path: {0}", src);
                            }
                            else
                            {
                                var dir    = IsDirectory(src);
                                var result = dir ? XLPack.CopyDir(src, dest) : XLPack.Copy(src, dest);

                                Console.WriteLine(result ? "Done" : "Copy failed...");
                            }
                        }

                        break;

                    case "rm":
                        if (cmdArgs.Length == 0)
                        {
                            Log("Info", "rm <path>");
                        }
                        else
                        {
                            path = AbsolutePath(cmdArgs[0]);
                            if (IsDirectory(path))
                            {
                                Console.WriteLine(XLPack.DeleteDir(path) ? "Done" : "Remove failed...");
                            }
                            else
                            {
                                Console.WriteLine(XLPack.FDelete(path) ? "Done" : "Remove failed...");
                            }
                        }

                        break;

                    case "fstat":
                        if (cmdArgs.Length == 0)
                        {
                            Log("Info", "fstat <file path>");
                        }
                        else
                        {
                            path = cmdArgs[0];
                            var temp = GetFileStat2(path);
                            if (temp == null)
                            {
                                Log("Warn", "[File] Doesn't exist or get stat...");
                            }
                            else
                            {
                                Log("File", path);
                                Log("File", $"Size: {temp.Size}");
                                Log("File", $"CreationTime: {temp.CreateTime}");
                                Log("File", $"ModifiedTime: {temp.ModifyTime}");
                                Log("File", $"MD5: {temp.Hash}");
                            }
                        }
                        break;

                    case "fstat1":
                        if (cmdArgs.Length == 0)
                        {
                            Log("Info", "fstat <file path>");
                        }
                        else
                        {
                            path = cmdArgs[0];
                            var temp = GetFileStat(path);
                            Log("File", $"CreationTime: {DateTime.FromFileTime(temp.creationTime)}");
                            Log("File", $"ModifiedTime: {DateTime.FromFileTime(temp.modifiedTime)}");
                        }
                        break;

                    case "fsize":
                        if (cmdArgs.Length == 0)
                        {
                            Log("Info", "fsize <file path>");
                        }
                        else
                        {
                            path = cmdArgs[0];
                            var temp = GetFileSize(path);
                            if (temp < 0)
                            {
                                Log("Warn", "[File] Doesn't exist ...");
                            }
                            else
                            {
                                Log("File", $"Size: {temp}");
                            }
                        }
                        break;

                    case "fgetmd5":
                        if (cmdArgs.Length == 0)
                        {
                            Log("Info", "fgetmd5 <file path>");
                        }
                        else
                        {
                            path = cmdArgs[0];
                            var temp = GetFileMD5(path);
                            if (temp == null)
                            {
                                Log("Warn", "[File] Doesn't exist ...");
                            }
                            else
                            {
                                Log("File", $"MD5: {temp}");
                            }
                        }
                        break;

                    case "fsetmd5":
                        if (cmdArgs.Length < 2)
                        {
                            Log("Info", "fsetmd5 <md5hash> <file path>");
                        }
                        else
                        {
                            var hash = cmdArgs[0];
                            path = cmdArgs[1];
                            if (!SetFileMD5(path, hash))
                            {
                                Log("Warn", "[File] Doesn't exist ...");
                            }
                            else
                            {
                                Log("File", $"File MD5 updated to {hash}");
                            }
                        }
                        break;

                    case "makemd5":
                        if (cmdArgs.Length == 0)
                        {
                            Log("Info", "makemd5 <file path>");
                        }
                        else
                        {
                            path = cmdArgs[0];
                            if (!ReCalculateFileMD5(path))
                            {
                                Log("Warn", "[File] Doesn't exist or failed to update ...");
                            }
                            else
                            {
                                Log("File", $"File MD5 updated");
                            }
                        }
                        break;

                    case "fusemd5":
                        if (cmdArgs.Length == 0)
                        {
                            Log("Info", "fusemd5 <file path>");
                        }
                        else
                        {
                            path = cmdArgs[0];
                            var temp = UseMD5(path);
                            Log("File", $"UseMD5: {temp}");
                        }
                        break;

                    case "struct":
                        var tree = new TreeDictionary("/master");
                        GetFileSystemStruct(tree);
                        // TODO ... save to file...
                        break;

                    case "exportfilelist":
                        ExportFileList();
                        break;
                    }
                }

                Destroy();
            }
            else
            {
                Log("Error", "Cannot create file system");
            }
        }