コード例 #1
0
        static int Pack(CommandLineArguments cmdargs)
        {
            // Handle no-arg case
            if (cmdargs.Count == 1)
            {
                WriteMessage(MessageType.Error, "Error 904: No arguments detected\n");
                return 904;
            }

            // Handle help request
            if (cmdargs[1].Type == CommandLineArgument.ArgumentType.Option && cmdargs[1].Name == "?")
            {
                Console.WriteLine(Resources.PackHelp);
                return mcInteropMode ? 903 : 0;
            }

            // Define option variables
            Boolean saveState = false;
            Boolean trimArchive = false;
            Boolean lockArchive = false;
            Boolean verbose = false;
            Boolean patch = false;
            String input = null;
            String output = null;

            // Get options and arguments
            for (int i = 1; i < cmdargs.Count; i++)
            {
                if (cmdargs[i].Type == CommandLineArgument.ArgumentType.Argument)
                {
                    if (input == null)
                    {
                        input = cmdargs[i].Name;
                        output = Path.GetDirectoryName(input) + ".mcpak";
                    }
                    else if (input != null)
                    {
                        output = cmdargs[i].Name;
                    }
                }
                else
                {
                    switch (cmdargs[i].Name.ToLower())
                    {
                        case "savestate":
                            saveState = true;
                            break;
                        case "trim":
                            saveState = true;
                            trimArchive = true;
                            break;
                        case "lock":
                            lockArchive = true;
                            break;
                        case "patch":
                            patch = true;
                            break;
                        case "verbose":
                            verbose = true;
                            break;
                    }
                }
            }

            // Validate
            if (input == null)
            {
                WriteMessage(MessageType.Error, "Error 907: Input path not specified!\n");
                return 907;
            }
            if (!File.Exists(input))
            {
                WriteMessage(MessageType.Error, "Error 909: Specified input file does not exist!\n");
                return 909;
            }

            // Pack Resources
            //#if !DEBUG
            try
            {
            //#endif
                // Create Resource Manager
                CharacterResourceManager resmgr = null;

                // Load resource manager according to the input file supplied
                if (Path.GetExtension(input) == ".mcres")
                {
                    resmgr = CharacterResourceManager.FromXML(input);
                    resmgr.FileSystemProxy = new DefaultFileSystemProxy(Path.GetDirectoryName(input));
                }
                else if (Path.GetExtension(input) == ".mcpak")
                {
                    AFSFileSystemProxy proxy = new AFSFileSystemProxy(input);
                    PackageEntry mcresEntry = proxy.Archive.TryGetEntry("character.mcres");

                    if (mcresEntry == null)
                    {
                        WriteMessage(MessageType.Error, "Error 911: Cannot find resource descriptor entry!\n");
                        return 911;
                    }

                    using (ExStream exs = new ExStream())
                    {
                        proxy.Archive.Extract(mcresEntry, exs);
                        exs.Position = 0;
                        resmgr = CharacterResourceManager.FromXML(exs);
                        resmgr.FileSystemProxy = proxy;
                    }
                }
                else if (Path.GetExtension(input) == ".ini")
                {
                    WriteMessage(MessageType.Error, "Error 912: Directly packing Legacy directories is not supported!\n" +
                        "      You may try using LEGACY command on the input file (directory) first.\n" +
                        "      For more information type \"mcu LEGACY /?\"\n");
                    return 912;
                }
                else
                {
                    WriteMessage(MessageType.Error, "Error 913: Input file extension [{0}] not recognized!\n", Path.GetExtension(input));
                    return 913;
                }

                // Load state if needed
                if (saveState)
                {
                    try
                    {
                        Stream stateStream = resmgr.FileSystemProxy.GetSavedStateStream();
                        if (stateStream != null)
                            LoadCharacterFromStream(resmgr, stateStream);
                        else
                        {
                            if (!trimArchive)
                            {
                                WriteMessage(MessageType.Routine, "No saved state found! No state will be saved into the archive!\n");
                                saveState = false;
                            }
                            else
                            {
                                WriteMessage(MessageType.Error, "Error 914: No saved state found! Cannot apply /trim option!\n");
                                return 914;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!trimArchive)
                        {
                            WriteMessage(MessageType.Routine, "No saved state found! No state will be saved into the archive!\n");
                            saveState = false;
                        }
                        else
                        {
                            WriteMessage(MessageType.Error, "Error 914: No saved state found! Cannot apply /trim option!\n");
                            return 914;
                        }
                        throw ex;
                    }
                }

                // Create packager
                CharacterResourcePacker packer = new CharacterResourcePacker(output, resmgr);
                packer.SaveState = saveState;
                packer.LockCharacter = lockArchive;
                packer.OmitUnusedResources = trimArchive;
                packer.IsPatch = patch;

                // Set up verbose mode if needed
                if (verbose)
                {
                    packer.ArchiveGenerator.NotifyDetails = (String s) =>
                        {
                            Console.Write("      {0}", s);
                        };
                }

                // Start making archive
                if (verbose) WriteMessage(MessageType.Routine, "");
                packer.PackResources();
                if (verbose) Console.WriteLine();

                // Report success
                if (!packer.ArchiveGenerator.HasErrors)
                {
                    WriteMessage(MessageType.Routine, "Success!\n");
                    return 0;
                }
                else
                {
                    WriteMessage(MessageType.Routine, "Error 918: Archive generator reported error(s)!\n");
                    return 918;
                }

            //#if !DEBUG
            }
            catch (Exception ex)
            {
                WriteMessage(MessageType.Error, "An exception of type {0} occured!\n" +
                    "      Exception Message: {1}\n" +
                    "      Stack Trace:\n{2}", ex.GetType().Name,
                    ex.Message, ex.StackTrace);
                return 999;
            }
            //#endif
        }
コード例 #2
0
        private void Menu_SaveCharacterStateClicked(object sender, EventArgs e)
        {
            if (!resourceManager.AllowChange)
            {
                MessageBox.Show(Localization.LocalizationDictionary.Instance["ERR_601"], 
                    Localization.LocalizationDictionary.Instance["ERR_000"], 
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = String.Format("{0}|*.mcs|{1}|*.mcpak",
                Localization.LocalizationDictionary.Instance["EXT_MCS"],
                Localization.LocalizationDictionary.Instance["EXT_MCPAK"]);
            if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
            {
                if (sfd.FilterIndex == 1)
                {
                    SaveCharacterToFile(sfd.FileName);
                }
                else if (sfd.FilterIndex == 2)
                {
                    PackerOptionsDialog optdlg = new PackerOptionsDialog(resourceManager);
                    optdlg.CharacterName = resourceManager.Name;
                    if (optdlg.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
                    {
                        CharacterResourcePacker packer = new CharacterResourcePacker(sfd.FileName, resourceManager);
                        packer.ImageProcessors = resourceManager.Processors;
                        packer.SaveState = optdlg.SaveState;
                        packer.LockCharacter = optdlg.LockFile;
                        packer.OmitUnusedResources = optdlg.OmitUnusedFiles;
                        packer.CharacterName = optdlg.CharacterName;
                        packer.PackResources();
                    }
                }
            }

            
        }