using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.IO; using System.Runtime.InteropServices; using System.Diagnostics; using System.Text.RegularExpressions; using System.Collections; using System.Windows.Forms; namespace ITFInstaller { public class InstallProcess { protected string m_savingPath = null; protected Settings m_Settings = null; protected string m_exeBody = "x360_ITF_Techno"; protected string[] m_exeAllowed = {"d","f","r"}; protected PlatformSettings m_platformSettings = null; protected OutputFeedback m_outputFeedback = null; protected List m_mapList = new List(); public StreamWriter m_processOutputStream = null; FileProxy m_fileProxy = null; VirtualFat m_virtualFat = new VirtualFat(); public InstallProcess(string _savingPath,PlatformSettings _platformSettings, OutputFeedback _outputFeedback) { m_savingPath = _savingPath; m_platformSettings = _platformSettings; m_outputFeedback = _outputFeedback; } public List MapList { get { return m_mapList; } } public void Execute(string exeName,string Arguments,ref string output) { ProcessStartInfo startInfo = new ProcessStartInfo(exeName); startInfo.Arguments = Arguments; bool redirect = m_processOutputStream!=null; if (redirect) { startInfo.RedirectStandardError = true; startInfo.RedirectStandardOutput = false; startInfo.UseShellExecute = false; } /* if (output != null) { startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.UseShellExecute = false; } else { startInfo.WindowStyle = ProcessWindowStyle.Hidden; }*/ Process process = null; process = Process.Start(startInfo); /* if (output != null) { output = process.StandardOutput.ReadToEnd(); error = process.StandardError.ReadToEnd(); }*/ if (redirect) { string stdError = process.StandardError.ReadToEnd(); if (stdError.Length != 0) stdError = "--- " + exeName + " " + Arguments + ": \n" + stdError; m_outputFeedback.addText(stdError); process.WaitForExit(); m_processOutputStream.Write(stdError); } else process.WaitForExit(); } public void MkDirConsole(string directory) { m_fileProxy.CreateDirectory(m_platformSettings.DriveKit + directory); } public void cookDatas() { //TODO removed Hardcoded platform XBOX360 string commandLine = @"directory=" + m_Settings.DataPath + ";all=1;" + "platform=" + m_platformSettings.Plateform; string Output = ""; Execute(m_Settings.ExePath + @"\UA_ResourceCooker.exe", commandLine,ref Output); m_outputFeedback.addText(Output); } public void generatePack() { //todo hardcoded platform remove string commandLine = @"directory=" + m_Settings.DataPath + ";packfile=1;" + "platform=" + m_platformSettings.Plateform; string Output = ""; Execute(m_Settings.ExePath + @"\UA_ResourceCooker.exe", commandLine, ref Output); m_outputFeedback.addText(Output); } UInt32 getVersionFile(string _filename) { FileStream streamFile = File.OpenRead(_filename); if (streamFile != null) { Array FileData = Array.CreateInstance(typeof(byte), 4); byte[] bData = (byte[])FileData; int nbDataRead = streamFile.Read(bData, 0, (int)4); //endian swap Array.Reverse(bData); return BitConverter.ToUInt32(bData, 0); } return 0; } protected bool FilenameIsValid(string filename) { int length = Path.GetFileName(filename).Length; if (filename.IndexOfAny(new char[]{'*','+','/'}) != -1) return false; return length <= 42; } public void Start(Settings settings) { m_Settings = settings; m_processOutputStream = null; try { System.IO.Directory.CreateDirectory("Log"); } catch (System.Exception) { } try { m_processOutputStream = new StreamWriter("Log\\win32_itfinstaller.log"); } catch (System.Exception) { } //support only x360 m_fileProxy = new x360FileProxy(settings.ConsoleIP); List filesConsole = new List(); FileSearch searcher = new FileSearch(); List FiltersAllowed = new List(); searcher.SearchExtensions.Add(".ckd"); foreach (string filter in m_platformSettings.FiltersStandard) searcher.SearchExtensions.Add("." + filter); searcher.ExcludedFolders.Add(m_Settings.DataPath + @"\itf_cooked"); searcher.ExcludedFolders.Add(m_Settings.DataPath + @"\AutoSave"); FileInfo[] filesRequired = searcher.Search(m_Settings.DataPath); searcher.ExcludedFolders.Clear(); FileInfo[] filesCooked = searcher.Search(m_Settings.DataPath + @"\itf_cooked\" + m_fileProxy.getName()); FileInfo[] filesPC = new FileInfo[filesRequired.Length + filesCooked.Length]; filesRequired.CopyTo(filesPC, 0); filesCooked.CopyTo(filesPC, filesRequired.Length); if (settings.RunCook) { cookDatas(); } if (settings.UsePack) { generatePack(); //copy pack string bundleName = @"\bundle_" + m_platformSettings.Plateform + ".ipk"; string srcName = m_Settings.DataPath + bundleName; string dstName = m_platformSettings.DriveKit+ m_Settings.RootStart +@"\"+m_Settings.DestinationConsoleDataPath + bundleName; //MessageBox.Show("Copy pack disable"); DateTime dtPack = new DateTime(); m_fileProxy.CopyFile(srcName, dstName, ref dtPack); return; } //MAKE THE ROOT DIRECTORY makeRootDirectory(); //COPY ALL EXE SPECIFIC TO THE PLATFORM copyConsoleExecutable(); DateTime dt = new DateTime(); string virtualFatPath = m_savingPath + m_Settings.DestinationConsoleDataPath + "_"+ m_fileProxy.getName()+".fat"; if (File.Exists(virtualFatPath)) m_virtualFat.DeSerialize(virtualFatPath); //parse the files to know the files to delete on the target m_outputFeedback.addText("Deleting old files..\r\n"); Hashtable tablePC = new Hashtable(); foreach (FileInfo fileInfo in filesPC) { string directory = fileInfo.DirectoryName; string drive = Path.GetPathRoot(directory); directory = directory.Replace(drive, ""); string filenameDestination = m_platformSettings.DriveKit + m_Settings.RootStart + @"\" + directory + @"\" + fileInfo.Name; tablePC[filenameDestination] = null; } //remove the file on the target List filesToDelete = m_virtualFat.Clean(tablePC); foreach (string file in filesToDelete) { m_fileProxy.DeleteFile(file); m_virtualFat.RemoveFile(file); } m_outputFeedback.addText("Deleting old files DONE\r\n"); //save the status m_virtualFat.Serialize(virtualFatPath); m_outputFeedback.addText("Copy files..\r\n"); string[] versionCheck = m_platformSettings.FiltersCookForce; //copy the missing file or file updated foreach (FileInfo fileInfo in filesPC) { string directory = fileInfo.DirectoryName; string drive = Path.GetPathRoot(directory); directory = directory.Replace(drive,""); if (!FilenameIsValid(fileInfo.FullName)) { string invalidName = "Invalid name " + fileInfo.FullName; m_outputFeedback.addText(invalidName); continue; } UInt32 version = 0; bool bRequiredVersion = versionCheck.Any(fileInfo.Name.Contains); if (bRequiredVersion) version = getVersionFile(fileInfo.FullName); UInt32 size =(UInt32) fileInfo.Length; { string filenameDestination = m_platformSettings.DriveKit + m_Settings.RootStart + @"\" + directory + @"\" + fileInfo.Name; if (!m_virtualFat.isFileMatch(filenameDestination, fileInfo.LastWriteTime, version, size)) if (m_fileProxy.CopyFile(fileInfo.FullName, filenameDestination, ref dt)) m_virtualFat.addFile(filenameDestination, version, dt,size); } } m_outputFeedback.addText("Copy files Done..\r\n"); //save the status m_virtualFat.Serialize(virtualFatPath); m_outputFeedback.addText("Installation Done..\r\n"); // parseMaps(); m_processOutputStream.Close(); m_processOutputStream = null; } public void makeRootDirectory() { MkDirConsole(m_Settings.RootDestination); MkDirConsole(m_Settings.RootStart); } public void parseMaps() { m_mapList.Clear(); string root = m_platformSettings.DriveKit + m_Settings.RootStart+@"\"; root = root.ToLower(); string Command = "/b /s /x " + m_Settings.ConsoleIP + " " + root + "*.isc"; string Output = ""; Execute(m_platformSettings.DirectoryCommand, Command, ref Output); //now we have the list files to compare string[] files = Regex.Split(Output, "\r\n"); string workingName = ""; foreach (string line in files) { workingName = line.Replace(root, ""); workingName = workingName.Replace(m_Settings.DestinationConsoleDataPath, ""); m_mapList.Add(workingName); } } public void copyConsoleExecutable() { string exename; DateTime dt = new DateTime(); foreach (string prefix in m_exeAllowed) { exename = m_platformSettings.Plateform + m_exeBody + prefix + m_platformSettings.PlateformExtension; m_fileProxy.CopyFile(m_Settings.ExePath + @"\" + exename, m_platformSettings.DriveKit + m_Settings.RootStart + @"\" + exename,ref dt); } } } }