Exemple #1
0
        /// <exception cref="CopeException"><c>CopeException</c>.</exception>
        private void WriteDesignFile(string designFilePath, string archiveOutputPath)
        {
            if (File.Exists(designFilePath))
            {
                try
                {
                    File.Delete(designFilePath);
                }
                catch (Exception ex)
                {
                    string msg = "Tried to delete archive design file, but failed. Path: " + designFilePath;
                    LoggingManager.SendError("ArchiveCreator - " + msg);
                    LoggingManager.HandleException(ex);
                    throw new CopeException(ex, msg);
                }
            }

            try
            {
                StreamWriter designFile = File.CreateText(designFilePath);
                designFile.WriteLine(string.Format(Resources.ArchiveDesign, ArchiveAlias, TocName, Regex));
                designFile.Flush();
                designFile.Close();
            }
            catch (Exception ex)
            {
                string msg = "Tried to write archive design file, but failed. Path: " + designFilePath;
                LoggingManager.SendError("ArchiveCreator - " + msg);
                LoggingManager.HandleException(ex);
                throw new CopeException(ex, msg);
            }
        }
        static private void TryToGetKeyProvider()
        {
            const string flbPath = "simulation\\attrib\\fieldnames.flb";

            if (!ToolSettings.IsInRetributionMode)
            {
                LoggingManager.SendMessage("ModManager - not in Retribuion-mode, no need to load KeyProvider");
                return;
            }
            LoggingManager.SendMessage("ModManager - Trying to load the KeyProvider (FLB-file)");
            try
            {
                FSNodeFile file = FileManager.AttribTree.RootNode.GetFileByPath(flbPath);
                if (file == null)
                {
                    UIHelper.ShowWarning("Unable to find the FLB file! You probably won't be able to open RBFs.");
                    LoggingManager.SendWarning("ModManager - Failed to load FLB-file from path " + flbPath);
                    return;
                }
                UniFile uni     = file.GetUniFile();
                var     flbFile = new FieldNameFile(uni);
                flbFile.ReadData();
                RBFKeyProvider = flbFile;
            }
            catch (Exception ex)
            {
                UIHelper.ShowError("Unable to open or read the FLB file! You probably won't be able to open RBFs.");
                LoggingManager.SendError("ModManager - Error while loading FLB-file");
                LoggingManager.HandleException(ex);
                return;
            }
            LoggingManager.SendMessage("ModManager - Successfully loaded KeyProvider from " + flbPath);
        }
Exemple #3
0
        /// <exception cref="CopeException"><c>CopeException</c>.</exception>
        public ArchivePackerInfo PackArchive(string outputPath)
        {
            if (!ArchiveToolHelper.IsArchiveToolPresent())
            {
                string msg = "Archive tool has not been found! Searched at: " +
                             ArchiveToolHelper.GetArchiveToolPath();
                LoggingManager.SendError("ArchiveCreator - " + msg);
                throw new CopeException(msg);
            }
            if (!outputPath.EndsWith('\\'))
            {
                outputPath += '\\';
            }

            string uniqueName     = ArchiveName + '_' + DateTime.Now.ToProperString('_');
            string designFilePath = ArchiveToolHelper.GetArchiveToolDirectory() + uniqueName + ".sga_design";

            WriteDesignFile(designFilePath, outputPath + ArchiveName);

            string  arguments = string.Format(ARCHIVE_ARGUMENTS, InputDirectory.RemoveLast(1), outputPath + ArchiveName, uniqueName);
            Process packer    = StartPacker(arguments);

            if (packer == null || packer.Id == 0 || packer.Id == 1)
            {
                LoggingManager.SendError("ArchiveCreator - failed to start packer. Arguments: " + arguments);
                throw new CopeException("Failed to start archive.exe");
            }

            return(new ArchivePackerInfo(packer, designFilePath, outputPath + ArchiveName));
        }
Exemple #4
0
        /// <exception cref="CopeException">Could not start archive.exe</exception>
        private static Process StartPacker(string arguments)
        {
            Process archivePacker = null;

            try
            {
                string archiveToolPath = ArchiveToolHelper.GetArchiveToolPath();
                var    startInfo       = new ProcessStartInfo
                {
                    Arguments        = arguments,
                    CreateNoWindow   = true,
                    ErrorDialog      = true,
                    UseShellExecute  = false,
                    FileName         = archiveToolPath,
                    WorkingDirectory = archiveToolPath.SubstringBeforeLast('\\')
                };

                LoggingManager.SendMessage("ArchiveCreator - Starting archive.exe with " + arguments);
                archivePacker = Process.Start(startInfo);
                return(archivePacker);
            }
            catch (Exception ex)
            {
                if (archivePacker != null)
                {
                    archivePacker.Kill();
                    archivePacker.Dispose();
                }
                LoggingManager.SendError("ArchiveCreator - Failed to start archive.exe");
                LoggingManager.HandleException(ex);
                throw new CopeException(ex, "Could not start archive.exe" + ex.Message);
            }
        }
Exemple #5
0
 private void DeleteDesignFile()
 {
     if (!File.Exists(m_sDesignFilePath))
     {
         return;
     }
     try
     {
         File.Delete(m_sDesignFilePath);
     }
     catch (Exception ex)
     {
         LoggingManager.SendError("ArchiveCreator - tried to delete superfluous SGA-design file but failed");
         LoggingManager.HandleException(ex);
     }
 }
        static public void LoadSGAFile(string path)
        {
            CloseAll();

            LoggingManager.SendMessage("ModManager - Loading SGA file " + path);
            DateTime t1 = DateTime.Now;

            // the tool needs some paths to work with but SGAs of course don't provide these
            // so you need to set them up manually
            ModAttribDirectory = path.SubstringBeforeLast('.') + "\\ATTRIB\\";
            ModDataDirectory   = path.SubstringBeforeLast('.') + "\\DATA\\";
            SGAFile sga;

            try
            {
                sga = new SGAFile(path, FileAccess.Read, FileShare.Read);
            }
            catch (Exception e)
            {
                LoggingManager.SendError("ModManager - Failed to load SGA file!");
                LoggingManager.HandleException(e);
                UIHelper.ShowError("Can't open SGA file: " + e.Message + " See log file for more information");
                if (LoadingFailed != null)
                {
                    LoadingFailed();
                }
                return;
            }

            ModAttribArchives.Add(sga);
            ModDataArchives.Add(sga);
            ModName = path.SubstringAfterLast('\\');

            DateTime t2 = DateTime.Now;

            LoggingManager.SendMessage("ModManager - SGA file successfully loaded in " + (t2 - t1).TotalSeconds + " seconds");
            FileManager.FillTrees();

            TryToGetKeyProvider();
            if (SGALoaded != null)
            {
                SGALoaded();
            }
        }
        // Todo: clean up this method! it's a mess!
        /// <exception cref="CopeDoW2Exception">The global section of the selected module file is invalid!</exception>
        static public void LoadModule(string path)
        {
            if (IsAnythingLoaded)
            {
                CloseAll();
            }
            LoggingManager.SendMessage("ModManager - Loading module file " + path);
            GameDirectory = path.SubstringBeforeLast('\\', true);
            DateTime t1 = DateTime.Now;

            ModuleFile = new ModuleFile(path);

            // get basic mod information from module file
            try
            {
                ModAttribDirectory = path.SubstringBeforeLast('\\', true) +
                                     ((ModuleFile.ModuleSectionFileList)ModuleFile["attrib:common"]).GetFolderByIndex(0) +
                                     '\\';
                ModDataDirectory = path.SubstringBeforeLast('\\', true) +
                                   ((ModuleFile.ModuleSectionFileList)ModuleFile["data:common"]).GetFolderByIndex(0) +
                                   '\\';
                var globalSection = ModuleFile["global"] as ModuleFile.ModuleSectionKeyValue;
                if (globalSection != null)
                {
                    ModName      = globalSection["Name"];
                    s_sModFolder = globalSection["ModFolder"];
                    if (globalSection.KeyExists("UCSBaseIndex"))
                    {
                        UCSManager.NextIndex = uint.Parse(globalSection["UCSBaseIndex"]);
                    }
                }
                else
                {
                    throw new CopeDoW2Exception("The global section of the selected module file is invalid!");
                }
            }
            catch (CopeDoW2Exception e)
            {
                LoggingManager.SendError("ModManager - Error loading module file: " + e.Message);
                LoggingManager.HandleException(e);
                UIHelper.ShowError("Error loading module file " + path.SubstringAfterLast('\\') + ": " +
                                   e.Message + ". See log file for more information.");
                ModuleFile.Close();
                if (LoadingFailed != null)
                {
                    LoadingFailed();
                }
                return;
            }

            if (ModAttribDirectory == ModDataDirectory)
            {
                LoggingManager.SendError(
                    "ModManager - The data:common directory and the attrib:common directory of the mod overlap! May cause serious trouble!");
                UIHelper.ShowError(
                    "The data:common directory and the attrib:common directory of the mod overlap! This tool does NOT support such module files." +
                    "Please correct the module file before loading it, read the user guide for help.");
                ModuleFile.Close();
                if (LoadingFailed != null)
                {
                    LoadingFailed();
                }
                return;
            }

            // load mod data
            if (ModDataArchives == null)
            {
                ModDataArchives = new List <SGAFile>();
            }
            else
            {
                ModDataArchives.Clear();
            }

            if (ModAttribArchives == null)
            {
                ModAttribArchives = new List <SGAFile>();
            }
            else
            {
                ModAttribArchives.Clear();
            }

            LoggingManager.SendMessage("ModManager - Loading mod resources");
            List <SGAFile> currentArchives;

            foreach (ModuleFile.ModuleSection ms in ModuleFile)
            {
                if (ms is ModuleFile.ModuleSectionFileList && ms.SectionName == "attrib:common")
                {
                    currentArchives = ModAttribArchives;
                }
                else if (ms is ModuleFile.ModuleSectionFileList && ms.SectionName == "data:common")
                {
                    currentArchives = ModDataArchives;
                }
                else
                {
                    continue;
                }

                var tmp = (ModuleFile.ModuleSectionFileList)ms;
                for (int i = 0; i < tmp.ArchiveCount; i++)
                {
                    if (!File.Exists(ModuleFile.FilePath.SubstringBeforeLast('\\', true) + tmp.GetArchiveByIndex(i)))
                    {
                        continue;
                    }

                    try
                    {
                        currentArchives.Add(
                            new SGAFile(ModuleFile.FilePath.SubstringBeforeLast('\\', true) + tmp.GetArchiveByIndex(i),
                                        FileAccess.Read, FileShare.Read));
                    }
                    catch (Exception e)
                    {
                        LoggingManager.SendError("ModManager - Error loading SGA-file '" + tmp.GetArchiveByIndex(i) +
                                                 "':" + e.Message);
                        LoggingManager.HandleException(e);
                        UIHelper.ShowError("Error loading SGA-files: " + e.Message +
                                           " See log file for more information.");
                        ModuleFile.Close();
                        if (LoadingFailed != null)
                        {
                            LoadingFailed();
                        }
                        return;
                    }
                }
            }

            ModuleFile.Close();
            DateTime t2 = DateTime.Now;

            LoggingManager.SendMessage("ModManager - Module file successfully loaded in " + (t2 - t1).TotalSeconds +
                                       " seconds");
            FileManager.FillTrees();

            TryToGetKeyProvider();
            if (ModLoaded != null)
            {
                ModLoaded();
            }
        }
        /// <summary>
        /// Tries to load the specified UniFile and returns the proper FileTool.
        /// </summary>
        /// <param name="file"></param>
        /// <param name="forceText">Set this to true to always use the Text-editor.</param>
        /// <returns></returns>
        static public FileTool LoadFile(UniFile file, bool forceText = false)
        {
            if (!AllowOpeningFilesTwice && s_openTools.ContainsKey(file.FilePath))
            {
                return(s_openTools[file.FilePath]);
            }

            FileTool tmp;

            byte[]  stream   = file.ConsumeStream();
            UniFile filecopy = new UniFile(stream);

            filecopy.FilePath = file.FilePath;

            if (forceText)
            {
                try
                {
                    tmp = new TextEditor(filecopy);
                }
                catch (Exception e)
                {
                    UIHelper.ShowError("Can't open the selected file " + file.FileName + " as Text: " + e.Message);
                    return(null);
                }
            }
            else if (FileTypeManager.FileTypes.ContainsKey(filecopy.FileExtension))
            {
                tmp = FileTypeManager.LaunchFromExt(filecopy.FileName, filecopy);
            }
            else
            {
                try
                {
                    tmp = new RelicChunkyViewer(filecopy);
                }
                catch
                {
                    try
                    {
                        filecopy             = new UniFile(stream);
                        file.Stream.Position = 0;
                        tmp = new TextEditor(filecopy);
                    }
                    catch (Exception ex)
                    {
                        LoggingManager.SendError("Failed to open file");
                        LoggingManager.HandleException(ex);
                        file.Close();
                        UIHelper.ShowError("Can't open the selected file " + file.FileName + ", no suitable plugin found!");
                        return(null);
                    }
                }
            }
            tmp.OnSaved += FileTool_OnSaved;
            if (!s_openTools.ContainsKey(file.FilePath))
            {
                s_openTools.Add(file.FilePath, tmp);
            }
            if (FileLoaded != null)
            {
                FileLoaded(file, tmp);
            }
            return(tmp);
        }