Exemplo n.º 1
0
        private static TreeNode ToTreeNode(this RARC.Directory Dir, int FolderClosedID, ImageList images)
        {
            TreeNode Final = new TreeNode(Dir.Name)
            {
                ImageIndex = FolderClosedID, SelectedImageIndex = FolderClosedID
            };

            foreach (KeyValuePair <string, object> item in Dir.Items)
            {
                if (item.Value is RARC.Directory dir)
                {
                    Final.Nodes.Add(dir.ToTreeNode(FolderClosedID, images));
                }
                else if (item.Value is RARC.File file)
                {
                    FileInfo fi         = new FileInfo(file.Name);
                    int      imageindex = 2;
                    if (images.Images.ContainsKey("*" + fi.Extension))
                    {
                        imageindex = images.Images.IndexOfKey("*" + fi.Extension);
                    }
                    Final.Nodes.Add(new TreeNode(file.Name)
                    {
                        ImageIndex = imageindex, SelectedImageIndex = imageindex
                    });
                }
            }
            return(Final);
        }
Exemplo n.º 2
0
 public static void ReOrderDirectory(this RARC.Directory Dir, TreeNodeCollection Nodes)
 {
     string[] NewFileOrder = new string[Nodes.Count];
     for (int i = 0; i < Nodes.Count; i++)
     {
         NewFileOrder[i] = Nodes[i].Text;
     }
     Dir.SortItemsByOrder(NewFileOrder);
 }
Exemplo n.º 3
0
        static void RunScript(string Filename, string[] parameters)
        {
            Console.WriteLine("Loading {0}...", Filename);
            string[] Lines = File.ReadAllLines(Filename);
            Console.WriteLine("Script Loaded.");
            Thread.Sleep(100);
            RARC   CurrentArchive = null;
            string ErrorMessage   = "";

            for (int i = 0; i < Lines.Length; i++)
            {
                if (Lines[i].StartsWith("//") || string.IsNullOrWhiteSpace(Lines[i]))
                {
                    continue;
                }
                string   Line   = string.Format(Lines[i], parameters);
                string[] Params = Line.Split(' ');
                Console.WriteLine("Executing Line {1}: \"{0}\"", Lines[i], i);
                switch (Params[0])
                {
                case "new":     //new
                    if (CurrentArchive is null)
                    {
                        CurrentArchive         = new RARC();
                        CurrentArchive["Root"] = null;
                        Console.WriteLine("New Archive Created Successfully");
                    }
                    else
                    {
                        ErrorMessage = string.Format("Could not create a new archive:\n\tAn archive is already loaded in memory!");
                        goto Error;
                    }
                    break;

                case "open":     //open <filepath>
                    if (CurrentArchive is null)
                    {
                        if (Params.Length < 2)
                        {
                            ErrorMessage = string.Format("Incomplete Syntax - Expected\nopen <filepath>");
                            goto Error;
                        }
                        if (!File.Exists(Params[1]))
                        {
                            ErrorMessage = string.Format("File {0} could not be found", Params[1]);
                            goto Error;
                        }
                        bool IsYaz0 = YAZ0.Check(Params[1]);
                        bool IsYay0 = YAY0.Check(Params[1]);
                        CurrentArchive = IsYaz0 ? new RARC(YAZ0.DecompressToMemoryStream(Params[1]), Params[1]) : (IsYay0 ? new RARC(YAY0.DecompressToMemoryStream(Params[1]), Params[1]) : new RARC(Params[1]));
                        Console.WriteLine("Archive opened successfully!");
                    }
                    else
                    {
                        ErrorMessage = string.Format("Could not open the archive:\n\tAn archive is already loaded in memory!");
                        goto Error;
                    }
                    break;

                case "save":     //save [filepath]
                    if (CurrentArchive is null)
                    {
                        ErrorMessage = string.Format("Save Failed! No Archive Loaded");
                        goto Error;
                    }
                    CurrentArchive.Save(Params.Length == 1 ? CurrentArchive.FileName : Params[1]);
                    Console.WriteLine("Archive saved successfully!");
                    break;

                case "compress":     //compress <filepath> [-yz|-yy|-yzf]
                    if (Params.Length < 3)
                    {
                        ErrorMessage = string.Format("Incomplete Syntax - Expected\ncompress <filepath> [-yz|-yy|-yzf]");
                        goto Error;
                    }
                    if (!File.Exists(Params[1]))
                    {
                        ErrorMessage = string.Format("File {0} could not be found", Params[1]);
                        goto Error;
                    }
                    switch (Params[2])
                    {
                    case "-yz":
                    case "-yzf":
                        Console.WriteLine("Compressing, please wait... {0}");
                        YAZ0.Compress(Params[1], Params[2].Contains("f"));
                        break;

                    case "-yy":
                        Console.WriteLine("Compressing, please wait... {0}");
                        YAY0.Compress(Params[1]);
                        break;

                    default:
                        ErrorMessage = string.Format("Encoding mode {0} doesn't exist", Params[1]);
                        goto Error;
                    }

                    Console.WriteLine("Compress complete!");
                    break;

                case "replace":
                case "add":     //add <filepath> <archivepath>
                    string func = Params[0].Equals("replace") ? "replaced" : "added";
                    if (Params.Length < 3)
                    {
                        ErrorMessage = string.Format("Incomplete Syntax - Expected\n{0} <filepath> <archivepath>", Params[0]);
                        goto Error;
                    }
                    if (CurrentArchive is null)
                    {
                        ErrorMessage = string.Format("Add failed! No archive loaded");
                        goto Error;
                    }
                    if (!File.Exists(Params[1]) && !Directory.Exists(Params[1]))
                    {
                        ErrorMessage = string.Format("File or Directory {0} could not be found", Params[1]);
                        goto Error;
                    }
                    if (CurrentArchive.ItemExists(Params[1]))
                    {
                        if (Params[0].Equals("replace"))
                        {
                            CurrentArchive[Params[1]] = null;
                        }
                        else
                        {
                            ErrorMessage = string.Format("An item already exists at {0}", Params[1]);
                            goto Error;
                        }
                    }
                    if (File.GetAttributes(Params[1]) == FileAttributes.Directory)
                    {
                        CurrentArchive[Params[2]] = new RARC.Directory(Params[1], CurrentArchive);
                        Console.WriteLine("Folder {0} {1} successfully", Params[1], func);
                    }
                    else
                    {
                        CurrentArchive[Params[2]] = new RARC.File(Params[1]);
                        Console.WriteLine("File {0} {1} successfully", Params[1], func);
                    }
                    break;

                case "delete":     //delete <archivepath>
                    if (Params.Length < 2)
                    {
                        ErrorMessage = string.Format("Incomplete Syntax - Expected\ndelete <archivepath>");
                        goto Error;
                    }
                    if (CurrentArchive is null)
                    {
                        ErrorMessage = string.Format("Delete failed! No archive loaded");
                        goto Error;
                    }
                    if (!CurrentArchive.ItemExists(Params[1]))
                    {
                        ErrorMessage = string.Format("Can't delete the non-existant item {0}", Params[1]);
                        goto Error;
                    }

                    CurrentArchive[Params[1]] = null;
                    Console.WriteLine("Deleted {0} successfully", Params[1]);
                    break;

                case "rename":
                case "move":     //move <archivepath> <archivepath>
                    func = Params[0].Equals("rename") ? "Renamed" : "Moved";
                    if (Params.Length < 3)
                    {
                        ErrorMessage = string.Format("Incomplete Syntax - Expected\n{0} <archivepath> <archivepath>", Params[0]);
                        goto Error;
                    }
                    if (CurrentArchive is null)
                    {
                        ErrorMessage = string.Format("{0} failed! No archive loaded", Params[0]);
                        goto Error;
                    }
                    if (!CurrentArchive.ItemExists(Params[1]))
                    {
                        ErrorMessage = string.Format("Can't {1} a non-existant item {0}", Params[1], func);
                        goto Error;
                    }
                    if (CurrentArchive.ItemExists(Params[2]))
                    {
                        ErrorMessage = string.Format("An item already exists at {0}", Params[1]);
                        goto Error;
                    }

                    CurrentArchive.MoveItem(Params[1], Params[2]);
                    Console.WriteLine("{2} {0} to {1} successfully", Params[1], Params[2], func);
                    break;

                case "extract":     //extract <archivepath> <filepath> [-o]
                    if (Params.Length < 3)
                    {
                        ErrorMessage = string.Format("Incomplete Syntax - Expected\nextract <archivepath> <filepath>");
                        goto Error;
                    }
                    if (CurrentArchive is null)
                    {
                        ErrorMessage = string.Format("Extract failed! No archive loaded");
                        goto Error;
                    }
                    if (!CurrentArchive.ItemExists(Params[1]))
                    {
                        ErrorMessage = string.Format("Can't extract the non-existant item {0}", Params[1]);
                        goto Error;
                    }
                    if (CurrentArchive[Params[1]] is RARC.File efile)
                    {
                        if (File.Exists(Params[2]) && !Params.Contains("-o"))
                        {
                            ErrorMessage = string.Format("There is already a file on your system at {0}, consider adding the -o parameter if you want to overwrite it", Params[2]);
                            goto Error;
                        }
                        efile.Save(Params[2]);
                    }
                    else if (CurrentArchive[Params[1]] is RARC.Directory edir)
                    {
                        if (Directory.Exists(Params[2]) && !Params.Contains("-o"))
                        {
                            ErrorMessage = string.Format("There is already a file on your system at {0}, consider adding the -o parameter if you want to overwrite it", Params[2]);
                            goto Error;
                        }
                        edir.Export(Params[2]);
                    }
                    break;

                case "edit":     //edit <archivepath> <Parameter[]>
                    if (Params.Length < 3)
                    {
                        ErrorMessage = string.Format("Incomplete Syntax - Expected\nedit <archivepath> <parameter[]>\nParameters include:\n-id <new id>\n-setcompressed <-yz>\n-loadmain\n-loadaux\n-loaddvd\n-auto");
                        goto Error;
                    }
                    if (CurrentArchive is null)
                    {
                        ErrorMessage = string.Format("Edit failed! No archive loaded");
                        goto Error;
                    }
                    object getitem = CurrentArchive[Params[1]];
                    if (getitem is RARC.Directory)
                    {
                        ErrorMessage = string.Format("Edit failed! Cannot change properties of folders!");
                        goto Error;
                    }
                    if (getitem is RARC.File file)
                    {
                        bool mram = true, aram = false, dvd = false, compressed = false, compressedyaz0 = false;
                        for (int x = 2; x < Params.Length; x++)
                        {
                            switch (Params[x])
                            {
                            case "-id":
                                if (CurrentArchive.KeepFileIDsSynced)
                                {
                                    ErrorMessage = string.Format("Edit failed! Cannot change File ID because the Archive is set to Automatically calculate file ID's", Params[x]);
                                    goto Error;
                                }
                                if (short.TryParse(Params[++x], out short newid))
                                {
                                    file.ID = newid;
                                }
                                else
                                {
                                    ErrorMessage = string.Format("Edit failed! Could not parse {0} as a int16 (short)", Params[x]);
                                    goto Error;
                                }
                                break;

                            case "-setcompressed":
                                if (x + 1 != Params.Length - 1 && Params[x + 1].Equals("-yz"))
                                {
                                    compressedyaz0 = true;
                                }
                                compressed = true;
                                break;

                            case "-loadmain":
                                mram = true;
                                aram = false;
                                dvd  = false;
                                break;

                            case "-loadaux":
                                mram = false;
                                aram = true;
                                dvd  = false;
                                break;

                            case "loaddvd":
                                mram = false;
                                aram = false;
                                dvd  = true;
                                break;

                            case "auto":
                                if (file.FileData[0] == 0x59 && file.FileData[1] == 0x61 && file.FileData[2] == 0x7A && file.FileData[3] == 0x30)
                                {
                                    compressed     = true;
                                    compressedyaz0 = true;
                                }
                                if (file.Name.Contains(".rel"))
                                {
                                    mram = false;
                                    aram = true;
                                    dvd  = false;
                                }
                                else
                                {
                                    mram = true;
                                    aram = false;
                                    dvd  = false;
                                }
                                if (!CurrentArchive.KeepFileIDsSynced && file.ID == -1)
                                {
                                    file.ID = CurrentArchive.NextFreeFileID;
                                }
                                break;

                            default:
                                ErrorMessage = string.Format("Edit failed! Unknown file property {0}", Params[x]);
                                goto Error;
                            }
                        }
                        RARC.FileAttribute loadattribute = mram ? RARC.FileAttribute.PRELOAD_TO_MRAM : (aram ? RARC.FileAttribute.PRELOAD_TO_ARAM : (dvd ? RARC.FileAttribute.LOAD_FROM_DVD : 0));
                        file.FileSettings = RARC.FileAttribute.FILE | (compressed ? RARC.FileAttribute.COMPRESSED : 0) | loadattribute | (compressedyaz0 ? RARC.FileAttribute.YAZ0_COMPRESSED : 0);
                    }
                    break;

                default:
                    ErrorMessage = string.Format("Invalid Command {0}", Params[0]);
                    goto Error;
                }
                Console.WriteLine();
            }

            return;

Error:
            Console.WriteLine(ErrorMessage);
            Thread.Sleep(1000);
            return;
        }