/* * L9Cut.cs - Controls the L9cut utility to extract datafiles and obtain the * title of a game. * * Copyright (C) 2004 - 2011 Andreas Scherrer * * This is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this software; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, * USA. */ using System; using System.Diagnostics; using System.IO; using System.Windows.Forms; namespace Level9 { /// /// Controls the L9cut utility to extract datafiles and obtain the title /// of a game. /// public class L9Cut { // -- Constants ------------------------------------------------------ /// /// The filename of a dummy L9cut output file. L9cut requires an output /// file even if we only want game information. /// const string DUMMY_DAT = "\\dummy.dat"; /// /// The answer 'yes' if L9cut asks for protection removal. /// const string L9CUT_YES = "Y"; /// /// The answer 'no' if L9cut asks for protection removal. /// const string L9CUT_NO = "N"; /// /// The L9cut parameter for a list of missing patches. /// const string L9CUT_MISSING_PATCHES = "-mp"; /// /// The L9cut parameter for a list of detected datafiles. /// const string L9CUT_DETECTED_DATAFILES = "-ld"; /// /// The L9cut parameter for a list of missing datafiles. /// const string L9CUT_MISSING_DATAFILES = "-md"; // -- Methods -------------------------------------------------------- /// /// Provides the game title of a given Level 9 datafile. /// /// the path of a datafile /// /// the game title or null if an error occurred /// public static string GetGameTitle(string datafile) { string title = null; try { string dummyDat = Config.GetAppDataPath() + DUMMY_DAT; string output = ExecL9Cut(datafile, dummyDat, L9CUT_NO); title = TextUtils.Substring(output, "\"", "\""); if (title == null) { title = TextUtils.Substring(output, "V1 datafile (", ")"); } if (title == null) { title = Path.GetFileName(datafile); } if (File.Exists(dummyDat)) { File.Delete(dummyDat); } } catch (Exception ex) { Logger.Log(typeof(L9Cut), "GetGameTitle", ex); } Logger.Log(typeof(L9Cut), "GetGameTitle: " + title); return title; } /// /// Extracts a Level 9 datafile. /// /// the path of an input file /// the path of an output file /// /// true if the datafile should be patchedm, else false /// /// the L9Cut console output public static string ExtractDatafile(string inFile, string outFile, bool patch) { return ExecL9Cut(inFile, outFile, patch ? L9CUT_YES : L9CUT_NO); } /// /// Provides a list of all detected datafiles. /// /// a list of detected datafiles public static string GetDetectedDatafiles() { return ExecL9Cut(L9CUT_DETECTED_DATAFILES, null, null); } /// /// Provides a list of missing datafiles. /// /// a list of missing datafiles public static string GetMissingDatafiles() { return ExecL9Cut(L9CUT_MISSING_DATAFILES, null, null); } /// /// Provides a list of missing patches. /// /// a list of missing patches public static string GetMissingPatches() { return ExecL9Cut(L9CUT_MISSING_PATCHES, null, null); } /// /// Executes L9cut with the given arguments. /// /// the first program argument /// the second program argument /// /// if L9cut asks for protection removal, stdin is the answer /// /// the L9Cut console output static string ExecL9Cut(string arg1, string arg2, string stdin) { string output = null; if (CheckL9Cut()) { try { Process l9Cut = new Process(); l9Cut.StartInfo.FileName = Config.GetL9CutPath(); if (arg1 != null) { l9Cut.StartInfo.Arguments = "\"" + arg1 + "\""; if (arg2 != null) { l9Cut.StartInfo.Arguments += " \"" + arg2 + "\""; } } l9Cut.StartInfo.CreateNoWindow = true; l9Cut.StartInfo.UseShellExecute = false; l9Cut.StartInfo.RedirectStandardError = true; l9Cut.StartInfo.RedirectStandardOutput = true; l9Cut.StartInfo.RedirectStandardInput = true; l9Cut.Start(); if (stdin != null) { l9Cut.StandardInput.Write(stdin); } output = l9Cut.StandardOutput.ReadToEnd(); if (output == null || output.Length == 0) { output = l9Cut.StandardError.ReadToEnd(); } l9Cut.WaitForExit(); } catch (Exception ex) { Logger.Log(typeof(L9Cut), "ExecL9Cut", ex); } } return output; } /// /// Checks for 'l9cut.exe' in the application directory. /// /// true if L9cut exists, else false static bool CheckL9Cut() { bool found = true; if (!File.Exists(Config.GetL9CutPath())) { ErrorHandler.Handle(typeof(L9Cut), "\"" + Config.L9CUT_EXE + "\" " + "not found in application directory!"); found = true; } return found; } } }