JD2022-TU1/main/tools/legacy/ITFInstaller/ITFInstaller.cs

334 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Principal;
using System.Threading;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Xml;
using PluginCommon;
namespace ITFInstaller
{
public partial class ITFInstaller : Form
{
private Settings m_Settings = new Settings();
private InstallProcess m_InstallProcess = null;
private OutputFeedback m_outputFeedback = new OutputFeedback();
readonly string m_savingPath = "coreConfig/configPlugin/";
readonly string m_SettingsSuffix = "InstallSettings_";
Thread trdInstalling = null;
PlatformSettings m_platformSettings = null;
float m_timeInstallationSecond;
EngineSettings engineSettings = null;
public ITFInstaller(string[] args)
{
CommandLineArgs cmdArgs = new CommandLineArgs(args);
engineSettings = new EngineSettings(cmdArgs);
InitializeComponent();
if (engineSettings.isClientRectValid())
{
engineSettings.applyToForm(this);
}
engineSettings.applyCurrentDirectory();
propertyGridSettings.SelectedObject = m_Settings;
String userName = WindowsIdentity.GetCurrent().Name;
int index = userName.LastIndexOf('\\');
if (index != -1)
userName = userName.Substring(index+1);
m_Settings.RootDestination = userName;
findSettingsFiles();
setEnableControls(true);
m_platformSettings = new PlatformSettings();
m_platformSettings.load(m_savingPath + "platformSettingsInstallerX360.xml");
m_InstallProcess = new InstallProcess(m_savingPath,m_platformSettings, m_outputFeedback);
progressBarInstallation.Enabled = false;
progressBarInstallation.Style = ProgressBarStyle.Blocks;
}
public void Execute(string exeName,string Arguments)
{
ProcessStartInfo startInfo = new ProcessStartInfo(exeName);
startInfo.Arguments = Arguments;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process process = null;
process = Process.Start(startInfo);
process.WaitForExit();
}
private void btnRun_Click(object sender, EventArgs e)
{
string defaultCommandLineMap = "root=" + m_Settings.DestinationConsoleDataPath + @"\";
string commandLine = m_platformSettings.DriveKit + m_Settings.RootStart + @"\x360_Techno_r.xex " + defaultCommandLineMap + ";graphic=directx;newdata=1";
string Command = "/x " + m_Settings.ConsoleIP + " " + commandLine;
Execute(m_platformSettings.RebootCommand, Command);
}
private void ThreadTaskInstalling()
{
m_InstallProcess.Start(m_Settings);
}
private void btnInstall_Click(object sender, EventArgs e)
{
if (m_Settings != null)
{
textBoxOutput.Text = "";
trdInstalling = new Thread(new ThreadStart(this.ThreadTaskInstalling));
trdInstalling.Start();
setEnableControls(false);
timerInstalling.Enabled = true;
progressBarInstallation.Enabled = true;
progressBarInstallation.Style = ProgressBarStyle.Marquee;
m_timeInstallationSecond = 0;
labelTimeInstallation.Text = "";
labelInstalling.Text = "Installing...";
}
}
private void findSettingsFiles()
{
DirectoryInfo di = new DirectoryInfo(m_savingPath);
FileInfo[] rgFiles = di.GetFiles("*.xml");
foreach (FileInfo fi in rgFiles)
{
if (fi.FullName.Contains(m_SettingsSuffix))
{
string name = fi.FullName.Replace(m_SettingsSuffix, "");
name = Path.GetFileNameWithoutExtension(name);
comboBoxSettingsList.Items.Add(name);
}
}
if (comboBoxSettingsList.Items.Count > 0)
{
comboBoxSettingsList.SelectedIndex = 0;
doSelectSettings(0);
}
}
private void doSelectSettings(int index)
{
string settingsName = comboBoxSettingsList.Items[index].ToString();
string settingsPath = getSettingsFileName(settingsName);
m_Settings = new Settings();
m_Settings.load(settingsPath);
propertyGridSettings.SelectedObject = m_Settings;
textBoxSettingsName.Text = settingsName;
}
private string getSettingsFileName(string source)
{
string settingsPath = m_savingPath + m_SettingsSuffix + source + ".xml";
return settingsPath;
}
private void btnSaveSettings_Click(object sender, EventArgs e)
{
if (textBoxSettingsName.Text != "")
{
string settingsPath = getSettingsFileName(textBoxSettingsName.Text);
m_Settings.save(settingsPath);
}
}
private void setEnableControls(bool value)
{
propertyGridSettings.Enabled = value;
btnInstall.Enabled = value;
btnSaveSettings.Enabled = value;
comboBoxSettingsList.Enabled = value;
textBoxSettingsName.Enabled = value;
progressBarInstallation.Enabled = value;
btnAbort.Enabled = !value;
}
private void timerInstalling_Tick(object sender, EventArgs e)
{
if (trdInstalling != null && !trdInstalling.IsAlive)
{
finishInstallation();
return;
}
m_timeInstallationSecond += timerInstalling.Interval*0.001f;//to milli to seconds
labelTimeInstallation.Text = m_timeInstallationSecond.ToString() + " seconds";
textBoxOutput.Text += m_outputFeedback.getText();
if (textBoxOutput.Text.Length > 0)
{
textBoxOutput.Select(textBoxOutput.Text.Length - 1, textBoxOutput.Text.Length - 1);
textBoxOutput.ScrollToCaret();
}
}
private void finishInstallation()
{
setEnableControls(true);
timerInstalling.Enabled = false;
progressBarInstallation.Value = 0;
progressBarInstallation.Maximum = 0;
progressBarInstallation.Style = ProgressBarStyle.Blocks;
labelInstalling.Text = "Installation done";
textBoxOutput.Text += m_outputFeedback.getText();
}
private void btnAbort_Click(object sender, EventArgs e)
{
if (trdInstalling!=null)
{
trdInstalling.Abort();
finishInstallation();
labelInstalling.Text = "Installation aborted";
}
}
private void btnReboot_Click(object sender, EventArgs e)
{
string Command = "/x " + m_Settings.ConsoleIP + " ";
Execute(m_platformSettings.RebootCommand, Command);
}
private void btn_LaunchMap(object sender, EventArgs e)
{
MapLauncher mapLauncher = new MapLauncher();
string root = m_Settings.DataPath;
mapLauncher.parseGetISCFiles(m_platformSettings,m_Settings);
mapLauncher.Visible = true;
}
private void ITFInstaller_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] drop_files = (string[])e.Data.GetData(DataFormats.FileDrop);
List<String> files = new List<String>();
FileSearch searcher = new FileSearch();
List<string> FiltersAllowed = new List<string>();
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");
searcher.Recursive = true;
foreach (string drop_file in drop_files)
{
FileInfo[] file_infos = searcher.Search(drop_file);
foreach (FileInfo file_info in file_infos)
{
files.Add(file_info.FullName);
}
}
listBoxFilesToCook.Items.Clear();
//generate a file with files to cook ...
foreach (string file in files)
{
string relativePath = file.Replace(m_Settings.DataPath+@"\", "");
listBoxFilesToCook.Items.Add(relativePath);
}
}
}
private void ITFInstaller_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void btnCookFiles_Click(object sender, EventArgs e)
{
string filename = "cookfiles.txt";
File.Delete(filename);
System.IO.StreamWriter file = new System.IO.StreamWriter(filename);
foreach (string item in listBoxFilesToCook.Items)
{
file.WriteLine(item);
}
file.Close();
string commandLine = @"directory=" + m_Settings.DataPath + ";files=" + filename + ";platform=" + m_platformSettings.Plateform;
string Output = "";
m_InstallProcess.Execute(m_Settings.ExePath + @"\UA_ResourceCooker.exe", commandLine, ref Output);
m_outputFeedback.addText(Output);
listBoxFilesToCook.Items.Add("Cook files done");
}
}
public class OutputFeedback
{
protected string m_text;
public void addText(string _text)
{
lock (this)
{
m_text += _text;
}
}
public string getText()
{
string textToReturn;
lock (this)
{
textToReturn = m_text;
m_text = "";
}
return textToReturn;
}
}
}