/HaRepacker/FHMapper/Settings.cs

https://github.com/lastbattle/Harepacker-resurrected · C# · 185 lines · 130 code · 18 blank · 37 comment · 14 complexity · 318dac8529bc005f718c7aaa1a3c5b8d MD5 · raw file

  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Text.RegularExpressions;
  8. using System.Windows.Forms;
  9. using HaRepacker.GUI;
  10. namespace HaRepacker
  11. {
  12. public partial class Settings : Form
  13. {
  14. public List<Object> settings;
  15. public FHMapper.FHMapper main;
  16. public Settings()
  17. {
  18. InitializeComponent();
  19. }
  20. private void Settings_Load(object sender, EventArgs e)
  21. {
  22. PrevTBox.Text = settings.ToArray()[0].ToString();
  23. PrevCBox.Checked = (bool)settings.ToArray()[1];
  24. NextTBox.Text = settings.ToArray()[2].ToString();
  25. NextCBox.Checked = (bool)settings.ToArray()[3];
  26. ForceTBox.Text = settings.ToArray()[4].ToString();
  27. ForceCBox.Checked = (bool)settings.ToArray()[5];
  28. XTBox.Text = settings.ToArray()[6].ToString();
  29. XCBox.Checked = (bool)settings.ToArray()[7];
  30. YTBox.Text = settings.ToArray()[8].ToString();
  31. YCBox.Checked = (bool)settings.ToArray()[9];
  32. TypeTBox.Text = settings.ToArray()[10].ToString();
  33. TypeCBox.Checked = (bool)settings.ToArray()[11];
  34. FilepathTBox.Text = settings.ToArray()[12].ToString();
  35. FilepathCBox.Checked = (bool)settings.ToArray()[13];
  36. SizeTBox.Text = settings.ToArray()[14].ToString();
  37. SizeCBox.Checked = (bool)settings.ToArray()[15];
  38. }
  39. private void CancelBtn_Click(object sender, EventArgs e)
  40. { this.Close(); }
  41. private void ConfirmBtn_Click(object sender, EventArgs e)
  42. {
  43. bool doOverwrite = true;
  44. string errorTBox = "";
  45. try {
  46. //Can this be done better?
  47. errorTBox = HaRepacker.Properties.Resources.FHSettingsPrevValue;
  48. int.Parse(PrevTBox.Text);
  49. errorTBox = HaRepacker.Properties.Resources.FHSettingsNextValue;
  50. int.Parse(NextTBox.Text);
  51. errorTBox = HaRepacker.Properties.Resources.FHSettingsForceValue;
  52. int.Parse(ForceTBox.Text);
  53. errorTBox = HaRepacker.Properties.Resources.FHSettingsXValue;
  54. int.Parse(XTBox.Text);
  55. errorTBox = HaRepacker.Properties.Resources.FHSettingsYValue;
  56. int.Parse(YTBox.Text);
  57. errorTBox = HaRepacker.Properties.Resources.FHSettingsPortalValue;
  58. int.Parse(TypeTBox.Text);
  59. double.Parse(SizeTBox.Text);
  60. }
  61. catch { doOverwrite = false; MessageBox.Show(string.Format(HaRepacker.Properties.Resources.FHSettingsInvalidMessage, errorTBox), HaRepacker.Properties.Resources.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning); }
  62. if (doOverwrite)
  63. {
  64. string theSettings;
  65. using (TextReader settingsFile = new StreamReader(FHMapper.FHMapper.SettingsPath))
  66. theSettings = settingsFile.ReadToEnd();
  67. // The below should be shorted down using Regex.Replace *Done*
  68. theSettings= Regex.Replace(theSettings, @"(?<=!DPt:)-?\d*(?=!)", PrevTBox.Text);
  69. /*theSettings = theSettings.Remove(Regex.Match(theSettings, @"(?<=!DPt:)-?\d*(?=!)").Index, Regex.Match(theSettings, @"(?<=!DPt:)-?\d*(?=!)").Length);// Regex: One or zero -, Any number of digits with the prefix !DPt: and suffix !
  70. theSettings = theSettings.Insert(Regex.Match(theSettings, @"(?<=!DPt:)!").Index, PrevTBox.Text);*/
  71. theSettings = Regex.Replace(theSettings, @"(?<=!DPc:)\w+(?=!)", PrevCBox.Checked.ToString());
  72. /* theSettings = theSettings.Remove(Regex.Match(theSettings, @"(?<=!DPc:)\w+(?=!)").Index, Regex.Match(theSettings, @"(?<=!DPc:)\w+(?=!)").Length);// Regex: One or more number of alphanumeric chars with the prefix !DPc: and suffix !
  73. theSettings = theSettings.Insert(Regex.Match(theSettings, @"(?<=!DPc:)!").Index, PrevCBox.Checked.ToString());*/
  74. theSettings = Regex.Replace(theSettings, @"(?<=!DNt:)-?\d*(?=!)", NextTBox.Text);
  75. /*theSettings = theSettings.Remove(Regex.Match(theSettings, @"(?<=!DNt:)-?\d*(?=!)").Index, Regex.Match(theSettings, @"(?<=!DNt:)-?\d*(?=!)").Length);
  76. theSettings = theSettings.Insert(Regex.Match(theSettings, @"(?<=!DNt:)!").Index, NextTBox.Text);*/
  77. theSettings = Regex.Replace(theSettings, @"(?<=!DNc:)\w+(?=!)", NextCBox.Checked.ToString());
  78. /*theSettings = theSettings.Remove(Regex.Match(theSettings, @"(?<=!DNc:)\w+(?=!)").Index, Regex.Match(theSettings, @"(?<=!DNc:)\w+(?=!)").Length);
  79. theSettings = theSettings.Insert(Regex.Match(theSettings, @"(?<=!DNc:)!").Index, NextCBox.Checked.ToString());*/
  80. theSettings = Regex.Replace(theSettings, @"(?<=!DFt:)-?\d*(?=!)",ForceTBox.Text);
  81. /*theSettings = theSettings.Remove(Regex.Match(theSettings, @"(?<=!DFt:)-?\d*(?=!)").Index, Regex.Match(theSettings, @"(?<=!DFt:)-?\d*(?=!)").Length);
  82. theSettings = theSettings.Insert(Regex.Match(theSettings, @"(?<=!DFt:)!").Index, ForceTBox.Text);*/
  83. theSettings = Regex.Replace(theSettings, @"(?<=!DFc:)\w+(?=!)", ForceCBox.Checked.ToString());
  84. /* theSettings = theSettings.Remove(Regex.Match(theSettings, @"(?<=!DFc:)\w+(?=!)").Index, Regex.Match(theSettings, @"(?<=!DFc:)\w+(?=!)").Length);
  85. theSettings = theSettings.Insert(Regex.Match(theSettings, @"(?<=!DFc:)!").Index, ForceCBox.Checked.ToString());*/
  86. theSettings = Regex.Replace(theSettings, @"(?<=!DXt:)-?\d*(?=!)", XTBox.Text);
  87. /*theSettings = theSettings.Remove(Regex.Match(theSettings, @"(?<=!DXt:)-?\d*(?=!)").Index, Regex.Match(theSettings, @"(?<=!DXt:)-?\d*(?=!)").Length);
  88. theSettings = theSettings.Insert(Regex.Match(theSettings, @"(?<=!DXt:)!").Index, XTBox.Text);*/
  89. theSettings = Regex.Replace(theSettings, @"(?<=!DXc:)\w+(?=!)", XCBox.Checked.ToString());
  90. /*theSettings = theSettings.Remove(Regex.Match(theSettings, @"(?<=!DXc:)\w+(?=!)").Index, Regex.Match(theSettings, @"(?<=!DXc:)\w+(?=!)").Length);
  91. theSettings = theSettings.Insert(Regex.Match(theSettings, @"(?<=!DXc:)!").Index, XCBox.Checked.ToString());*/
  92. theSettings = Regex.Replace(theSettings, @"(?<=!DYt:)-?\d*(?=!)", YTBox.Text);
  93. /*theSettings = theSettings.Remove(Regex.Match(theSettings, @"(?<=!DYt:)-?\d*(?=!)").Index, Regex.Match(theSettings, @"(?<=!DYt:)-?\d*(?=!)").Length);
  94. theSettings = theSettings.Insert(Regex.Match(theSettings, @"(?<=!DYt:)!").Index, YTBox.Text);*/
  95. theSettings = Regex.Replace(theSettings, @"(?<=!DYc:)\w+(?=!)", YCBox.Checked.ToString());
  96. /* theSettings = theSettings.Remove(Regex.Match(theSettings, @"(?<=!DYc:)\w+(?=!)").Index, Regex.Match(theSettings, @"(?<=!DYc:)\w+(?=!)").Length);
  97. theSettings = theSettings.Insert(Regex.Match(theSettings, @"(?<=!DYc:)!").Index, YCBox.Checked.ToString());*/
  98. theSettings = Regex.Replace(theSettings, @"(?<=!DTt:)-?\d*(?=!)", TypeTBox.Text);
  99. /*theSettings = theSettings.Remove(Regex.Match(theSettings, @"(?<=!DTt:)\d*(?=!)").Index, Regex.Match(theSettings, @"(?<=!DTt:)\d*(?=!)").Length);
  100. theSettings = theSettings.Insert(Regex.Match(theSettings, @"(?<=!DTt:)!").Index, TypeTBox.Text);*/
  101. theSettings = Regex.Replace(theSettings, @"(?<=!DTc:)\w+(?=!)", TypeCBox.Checked.ToString());
  102. /* theSettings = theSettings.Remove(Regex.Match(theSettings, @"(?<=!DTc:)\w+(?=!)").Index, Regex.Match(theSettings, @"(?<=!DTc:)\w+(?=!)").Length);
  103. theSettings = theSettings.Insert(Regex.Match(theSettings, @"(?<=!DTc:)!").Index, TypeCBox.Checked.ToString());*/
  104. theSettings = Regex.Replace(theSettings, @"(?<=!DFPt:)C:(%\w+)+.wz(?=!)", FilepathTBox.Text.Replace('/','%'));
  105. /*theSettings = theSettings.Remove(Regex.Match(theSettings, @"(?<=!DFPt:)C:(%\w+)+.wz(?=!)").Index, Regex.Match(theSettings, @"(?<=!DFPt:)C:(%\w+)+.wz(?=!)").Length);
  106. theSettings = theSettings.Insert(Regex.Match(theSettings, @"(?<=!DFPt:)!").Index, FilepathTBox.Text.Replace('/', '%'));*/
  107. theSettings = Regex.Replace(theSettings, @"(?<=!DFPc:)\w+(?=!)", FilepathCBox.Checked.ToString());
  108. /* theSettings = theSettings.Remove(Regex.Match(theSettings, @"(?<=!DFPc:)\w+(?=!)").Index, Regex.Match(theSettings, @"(?<=!DFPc:)\w+(?=!)").Length);
  109. theSettings = theSettings.Insert(Regex.Match(theSettings, @"(?<=!DFPc:)!").Index, FilepathCBox.Checked.ToString());*/
  110. theSettings = Regex.Replace(theSettings, @"(?<=!DSt:)\d*,?\d*(?=!)", SizeTBox.Text);
  111. /*theSettings = theSettings.Remove(Regex.Match(theSettings, @"(?<=!DSt:)\d*,?\d*(?=!)").Index, Regex.Match(theSettings, @"(?<=!DSt:)\d*,?\d*(?=!)").Length);
  112. theSettings = theSettings.Insert(Regex.Match(theSettings, @"(?<=!DSt:)!").Index, SizeTBox.Text);*/
  113. theSettings = Regex.Replace(theSettings, @"(?<=!DSc:)\w+(?=!)", SizeCBox.Checked.ToString());
  114. /*theSettings = theSettings.Remove(Regex.Match(theSettings, @"(?<=!DSc:)\w+(?=!)").Index, Regex.Match(theSettings, @"(?<=!DSc:)\w+(?=!)").Length);
  115. theSettings = theSettings.Insert(Regex.Match(theSettings, @"(?<=!DSc:)!").Index, SizeCBox.Checked.ToString());*/
  116. using (TextWriter settingsWriter = new StreamWriter(FHMapper.FHMapper.SettingsPath))
  117. settingsWriter.Write(theSettings);
  118. main.ParseSettings();
  119. this.Close();
  120. }
  121. }
  122. private void OpenFileBtn_Click(object sender, EventArgs e)
  123. {
  124. OpenFileDialog openFile = new OpenFileDialog();
  125. openFile.Filter = "All wz files | *.wz";
  126. openFile.ShowDialog();
  127. string buf = openFile.FileName;
  128. buf= buf.Replace('\\','/');
  129. FilepathTBox.Text = buf;
  130. }
  131. private void PrevTBox_KeyPress(object sender, KeyPressEventArgs e)
  132. {
  133. if (e.KeyChar ==(char) Keys.Space)
  134. e.Handled = true;
  135. }
  136. private void NextTBox_KeyPress(object sender, KeyPressEventArgs e)
  137. {
  138. if (e.KeyChar == (char)Keys.Space)
  139. e.Handled = true;
  140. }
  141. private void ForceTBox_KeyPress(object sender, KeyPressEventArgs e)
  142. {
  143. if (e.KeyChar == (char)Keys.Space)
  144. e.Handled = true;
  145. }
  146. private void XTBox_KeyPress(object sender, KeyPressEventArgs e)
  147. {
  148. if (e.KeyChar == (char)Keys.Space)
  149. e.Handled = true;
  150. }
  151. private void YTBox_KeyPress(object sender, KeyPressEventArgs e)
  152. {
  153. if (e.KeyChar == (char)Keys.Space)
  154. e.Handled = true;
  155. }
  156. private void TypeTBox_KeyPress(object sender, KeyPressEventArgs e)
  157. {
  158. if (e.KeyChar == (char)Keys.Space)
  159. e.Handled = true;
  160. }
  161. private void SizeTBox_KeyPress(object sender, KeyPressEventArgs e)
  162. {
  163. if (e.KeyChar == (char)Keys.Space)
  164. e.Handled = true;
  165. }
  166. }
  167. }