2
0

MainForm.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Threading;
  12. namespace Analyser
  13. {
  14. public partial class MainForm : Form
  15. {
  16. //Глобальные переменные II
  17. QSPGameCode game;
  18. //Глобальные переменные
  19. ArrayList ObjectsAdded;
  20. ArrayList ObjectsDeleted;
  21. bool inited = false;
  22. string logText = "";
  23. static BackgroundWorker _bw;
  24. public MainForm()
  25. {
  26. InitializeComponent();
  27. _bw = new BackgroundWorker
  28. {
  29. WorkerReportsProgress = true,
  30. WorkerSupportsCancellation = true
  31. };
  32. _bw.DoWork += bw_DoWork;
  33. _bw.ProgressChanged += bw_ProgressChanged;
  34. _bw.RunWorkerCompleted += bw_RunWorkerCompleted;
  35. }
  36. private void btnBrowse_Click(object sender, EventArgs e)
  37. {
  38. //выбираем файл
  39. if (dlgOpen.ShowDialog() == DialogResult.OK)
  40. {
  41. Properties.Settings.Default.filepath = dlgOpen.FileName;
  42. txtStatus.Clear();
  43. logText = "";
  44. ClearMains();
  45. ParseFile();
  46. }
  47. }
  48. private delegate void PrintErrorsDelegate();
  49. private void PrintErrors()
  50. {
  51. if (this.InvokeRequired)
  52. {
  53. // Pass the same function to BeginInvoke,
  54. // but the call would come on the correct
  55. // thread and InvokeRequired will be false.
  56. this.BeginInvoke(new PrintErrorsDelegate(PrintErrors));
  57. return;
  58. }
  59. //Выводим ошибки и ворнинги
  60. //Log("Строка #" + Convert.ToString(game.GetErrorLine()) + ": " + game.GetError());
  61. Log("No of errors: " + Common.GetErrorsCount() + ", warnings: " + Common.GetWarningsCount());
  62. List<Common.QspError> errors = Common.GetErrors();
  63. foreach (Common.QspError error in errors)
  64. {
  65. int err_line = game.GetLocation(error.locationName).GetLine() + error.line;
  66. string errorMessage = "Location \"" + error.locationName + "\", Line #" + Convert.ToString(err_line) + " : " + error.text;
  67. if (error.isError)
  68. {
  69. Log("Error, " + errorMessage);
  70. }
  71. else
  72. {
  73. Log("Warning, " + errorMessage);
  74. }
  75. }
  76. PrintLog();
  77. }
  78. private void bw_DoWork(object sender, DoWorkEventArgs e)
  79. {
  80. //Устанаваливаем настройки парсера
  81. List<string> systemVars = new List<string>(txtSystemVariables.Lines);
  82. if (chkAero.Checked)
  83. systemVars.AddRange(txtSystemAeroVars.Lines);
  84. Common.SetConfig(new List<string>(txtVariableNames.Lines), systemVars, chkCurlyParse.Checked);
  85. if (CheckForFile(edtFile.Text, true))
  86. {
  87. game.ParseGame(edtFile.Text, sender, e);
  88. if (!e.Cancel)
  89. {
  90. this.Log("The file was read successfully");
  91. this.PrintLog();
  92. }
  93. }
  94. else
  95. {
  96. this.Log("File was not read.");
  97. PrintLog();
  98. }
  99. }
  100. private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  101. {
  102. this.PrintErrors();
  103. Common.ClearNonGameErrors();
  104. this.PrintLog();
  105. }
  106. private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
  107. {
  108. progressBar1.Value = e.ProgressPercentage;
  109. }
  110. private void ParseFile()
  111. {
  112. Log("Trying to read the file ...");
  113. PrintLog();
  114. _bw.RunWorkerAsync();
  115. }
  116. private void btnReset_Click(object sender, EventArgs e)
  117. {
  118. Analyser.Properties.Settings.Default.Reset();
  119. }
  120. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  121. {
  122. Analyser.Properties.Settings.Default.Save();
  123. }
  124. private void MainForm_Load(object sender, EventArgs e)
  125. {
  126. this.Text += " v " + System.Windows.Forms.Application.ProductVersion;
  127. }
  128. public void Log(string line)
  129. {
  130. logText += line + Environment.NewLine;
  131. }
  132. private delegate void PrintLogDelegate();
  133. private void PrintLog()
  134. {
  135. if (this.InvokeRequired)
  136. {
  137. // Pass the same function to BeginInvoke,
  138. // but the call would come on the correct
  139. // thread and InvokeRequired will be false.
  140. this.BeginInvoke(new PrintLogDelegate(PrintLog));
  141. return;
  142. }
  143. txtStatus.Text = logText;
  144. }
  145. private void ClearMains()
  146. {
  147. txtList1.Clear();
  148. txtList2.Clear();
  149. txtList3.Clear();
  150. }
  151. //Выводим список значений в textbox, перед списком выводим заголовок
  152. private void PrintArray(Control textbox, ArrayList val, string header)
  153. {
  154. if (textbox.Text.Length > 0)
  155. textbox.Text += Environment.NewLine;
  156. if (header.Length > 0)
  157. textbox.Text += "[" + header + "]" + Environment.NewLine;
  158. string s = String.Join(Environment.NewLine, (string[])val.ToArray(typeof(string)));
  159. textbox.Text += s;
  160. }
  161. private ArrayList SubstractArray(ArrayList search_in, ArrayList search_for)
  162. {
  163. ArrayList res = new ArrayList();
  164. string tosearch = Environment.NewLine + String.Join(Environment.NewLine, (string[])search_in.ToArray(typeof(string))) + Environment.NewLine;
  165. tosearch = tosearch.ToUpper();
  166. foreach (string titem in search_for)
  167. {
  168. if (!tosearch.Contains(Environment.NewLine + titem.ToUpper() + Environment.NewLine))
  169. res.Add(titem);
  170. }
  171. return res;
  172. }
  173. private bool CheckForFile(string filename, bool silent)
  174. {
  175. if (filename != "")
  176. {
  177. if (File.Exists(filename))
  178. {
  179. return true;
  180. }
  181. else
  182. {
  183. if (!silent)
  184. MessageBox.Show("Input file was not found!");
  185. }
  186. }
  187. else
  188. {
  189. if (!silent)
  190. MessageBox.Show("Select input file!");
  191. }
  192. return false;
  193. }
  194. private void SortArray(ArrayList arr)
  195. {
  196. string[] tarr = (string[])arr.ToArray(typeof(string));
  197. Array.Sort(tarr);
  198. arr.Clear();
  199. arr.AddRange(tarr);
  200. }
  201. //************************************************************
  202. //************************************************************
  203. //************************************************************
  204. //Локации
  205. private void btnTry1_Click(object sender, EventArgs e)
  206. {
  207. ClearMains();
  208. ArrayList LocationsDefined = new ArrayList();
  209. ArrayList LocationsReferenced = new ArrayList();
  210. ArrayList LocationsUnDefined = new ArrayList();
  211. ArrayList LocationsUnReferenced = new ArrayList();
  212. foreach (Common.QspLocationLink loc in Common.locationLinks)
  213. {
  214. if (loc.LocationExists)
  215. LocationsDefined.Add(loc.LocationName);
  216. if (loc.LocationCalled)
  217. LocationsReferenced.Add(loc.LocationName);
  218. if (!loc.LocationExists)
  219. LocationsUnDefined.Add(loc.LocationName);
  220. if (!loc.LocationCalled)
  221. LocationsUnReferenced.Add(loc.LocationName);
  222. }
  223. if (chkSortLocations.Checked)
  224. {
  225. SortArray(LocationsDefined);
  226. SortArray(LocationsReferenced);
  227. SortArray(LocationsUnDefined);
  228. SortArray(LocationsUnReferenced);
  229. }
  230. Log("We process the names of locations ...");
  231. PrintArray(txtList1, LocationsDefined, "Existing locations");
  232. Log("Added locations: " + LocationsDefined.Count.ToString());
  233. Log("We process the reference to locations...");
  234. PrintArray(txtList2, LocationsReferenced, "References to locations");
  235. Log("Added reference to location: " + LocationsReferenced.Count.ToString());
  236. Log("We begin to search for incorrect links ...");
  237. PrintArray(txtList3, LocationsUnDefined, "Incorrect links");
  238. Log("Found incorrect links: " + LocationsUnDefined.Count.ToString());
  239. Log("We begin to search for lost locations - those for which there are no references ...");
  240. PrintArray(txtList3, LocationsUnReferenced, "Lost locations");
  241. Log("Found lost locations: " + LocationsUnReferenced.Count.ToString());
  242. PrintLog();
  243. }
  244. //Переменные
  245. private void btnTry2_Click(object sender, EventArgs e)
  246. {
  247. ClearMains();
  248. ArrayList TextVars = new ArrayList();
  249. ArrayList NumericVars = new ArrayList();
  250. ArrayList TextVars_unassigned = new ArrayList();
  251. ArrayList NumericVars_unassigned = new ArrayList();
  252. ArrayList TextVars_unused = new ArrayList();
  253. ArrayList NumericVars_unused = new ArrayList();
  254. Log("Search for variables ...");
  255. foreach (Common.QspVariable var in Common.vars)
  256. {
  257. if (var.IsString)
  258. {
  259. if (!var.Assigned)
  260. TextVars_unassigned.Add(var.Name);
  261. if (!var.Used)
  262. TextVars_unused.Add(var.Name);
  263. TextVars.Add(var.Name);
  264. }
  265. else
  266. {
  267. if (!var.Assigned)
  268. NumericVars_unassigned.Add(var.Name);
  269. if (!var.Used)
  270. NumericVars_unused.Add(var.Name);
  271. NumericVars.Add(var.Name);
  272. }
  273. }
  274. if (chkSortVariables.Checked)
  275. {
  276. SortArray(TextVars);
  277. SortArray(NumericVars);
  278. }
  279. PrintArray(txtList1, TextVars, "String variables");
  280. PrintArray(txtList2, NumericVars, "Numeric variables");
  281. Log("Added variables: " + Common.vars.Count.ToString());
  282. if (chkSortVariables.Checked)
  283. SortArray(TextVars_unassigned);
  284. PrintArray(txtList3, TextVars_unassigned, "no init variables:");
  285. Log("Found using of uninitialized text variables: " + TextVars_unassigned.Count.ToString());
  286. if (chkSortVariables.Checked)
  287. SortArray(NumericVars_unassigned);
  288. PrintArray(txtList3, NumericVars_unassigned, "");
  289. Log("Found using of uninitialized numeric variables: " + NumericVars_unassigned.Count.ToString());
  290. if (chkSortVariables.Checked)
  291. SortArray(TextVars_unused);
  292. PrintArray(txtList3, TextVars_unused, "Unused variables:");
  293. Log("Found unused string variables: " + TextVars_unused.Count.ToString());
  294. if (chkSortVariables.Checked)
  295. SortArray(NumericVars_unused);
  296. PrintArray(txtList3, NumericVars_unused, "");
  297. Log("Found unused numeric variables: " + NumericVars_unused.Count.ToString());
  298. PrintLog();
  299. }
  300. //Предметы
  301. private void btnTry3_Click(object sender, EventArgs e)
  302. {
  303. ClearMains();
  304. ArrayList ObjectsAdded = new ArrayList();
  305. ArrayList ObjectsDeleted = new ArrayList();
  306. ArrayList ObjectsUnDefined = new ArrayList();
  307. ArrayList ObjectsLost = new ArrayList();
  308. foreach (Common.QspObj obj in Common.objects)
  309. {
  310. if (obj.Added)
  311. ObjectsAdded.Add(obj.Name);
  312. if (obj.Removed)
  313. ObjectsDeleted.Add(obj.Name);
  314. if (!obj.Added)
  315. ObjectsUnDefined.Add(obj.Name);
  316. if (!obj.Removed)
  317. ObjectsLost.Add(obj.Name);
  318. }
  319. if (chkSortObjects.Checked)
  320. {
  321. SortArray(ObjectsAdded);
  322. SortArray(ObjectsDeleted);
  323. SortArray(ObjectsUnDefined);
  324. SortArray(ObjectsLost);
  325. }
  326. Log("Search of items...");
  327. PrintArray(txtList1, ObjectsAdded, "Added items");
  328. PrintArray(txtList2, ObjectsDeleted, "Remove item");
  329. Log("Items adding: " + ObjectsAdded.Count.ToString());
  330. Log("Items removing: " + ObjectsDeleted.Count.ToString());
  331. Log("Search of nonexisting items...");
  332. PrintArray(txtList3, ObjectsUnDefined, "Invalid deletion");
  333. Log("Found invalid deletions: " + ObjectsUnDefined.Count.ToString());
  334. Log("Looking for lost items - those that are not deleted during the game ...");
  335. PrintArray(txtList3, ObjectsLost, "Lost items");
  336. Log("Lost items found: " + ObjectsLost.Count.ToString());
  337. PrintLog();
  338. }
  339. //Действия
  340. private void btnTry4_Click(object sender, EventArgs e)
  341. {
  342. ClearMains();
  343. ArrayList ActsAdded = new ArrayList();
  344. ArrayList ActsDeleted = new ArrayList();
  345. ArrayList ActsUnDefined = new ArrayList();
  346. foreach (Common.QspAct act in Common.acts)
  347. {
  348. if (act.Added)
  349. ActsAdded.Add(act.Name);
  350. if (act.Removed)
  351. ActsDeleted.Add(act.Name);
  352. if (!act.Added)
  353. ActsUnDefined.Add(act.Name);
  354. }
  355. if (chkSortObjects.Checked)
  356. {
  357. SortArray(ActsAdded);
  358. SortArray(ActsDeleted);
  359. SortArray(ActsUnDefined);
  360. }
  361. Log("Search for actions...");
  362. PrintArray(txtList1, ActsAdded, "Add an action");
  363. PrintArray(txtList2, ActsDeleted, "Remove an action");
  364. Log("Added action: " + ActsAdded.Count.ToString());
  365. Log("Removed action: " + ActsDeleted.Count.ToString());
  366. Log("We begin to search for deletion of nonexistent actions...");
  367. PrintArray(txtList3, ActsUnDefined, "Invalid deletion");
  368. Log("Invalid deletions found: " + ActsUnDefined.Count.ToString());
  369. PrintLog();
  370. }
  371. //************************************************************
  372. //************************************************************
  373. //************************************************************
  374. private void btnReload_Click(object sender, EventArgs e)
  375. {
  376. txtStatus.Clear();
  377. logText = "";
  378. ClearMains();
  379. ParseFile();
  380. }
  381. private void setBindingContext(Control ctrl)
  382. {
  383. ctrl.Visible = true;
  384. if (ctrl.HasChildren)
  385. foreach (Control child in ctrl.Controls)
  386. setBindingContext(child);
  387. }
  388. private void MainForm_Shown(object sender, EventArgs e)
  389. {
  390. if (!inited)
  391. {
  392. setBindingContext(this);
  393. btnBrowse.Select();
  394. game = new QSPGameCode();
  395. // ParseFile();
  396. ObjectsDeleted = new ArrayList();
  397. ObjectsAdded = new ArrayList();
  398. inited = true;
  399. }
  400. }
  401. private void PrintNonGameErrors()
  402. {
  403. List<Common.QspError> errors = Common.GetErrors();
  404. foreach (Common.QspError error in errors)
  405. {
  406. if (error.isError && (error.line == Common.INVALID_INDEX))
  407. {
  408. MessageBox.Show("Error: " + error.text);
  409. }
  410. }
  411. Common.ClearNonGameErrors();
  412. }
  413. private void btnExportCsv_Click(object sender, EventArgs e)
  414. {
  415. //Делаем экспорт текста в csv
  416. string FileName = edtFile.Text;
  417. if (CheckForFile(FileName, false))
  418. {
  419. if (Common.GetErrorsCount() > 0)
  420. {
  421. MessageBox.Show("There are syntax errors in the file, export is not possible!");
  422. return;
  423. }
  424. edtFile.Enabled = false;
  425. btnBrowse.Enabled = false;
  426. bool success = false;
  427. if (radComma.Checked)
  428. {
  429. success = Common.ExportToCsv(FileName, ",");
  430. }
  431. else
  432. {
  433. success = Common.ExportToCsv(FileName, ";");
  434. }
  435. if (!success)
  436. {
  437. PrintNonGameErrors();
  438. }
  439. edtFile.Enabled = true;
  440. btnBrowse.Enabled = true;
  441. }
  442. }
  443. private void btnTranslateFromCsv_Click(object sender, EventArgs e)
  444. {
  445. //Делаем перевод текста на основе csv
  446. string FileName = edtFile.Text;
  447. if (CheckForFile(FileName, false))
  448. {
  449. if (Common.GetErrorsCount() > 0)
  450. {
  451. MessageBox.Show("There are syntax errors in the file, export is not possible!");
  452. return;
  453. }
  454. string CsvFile = Common.GenerateCsvName(FileName);
  455. if (!CheckForFile(FileName, true))
  456. {
  457. MessageBox.Show("CSV file was not found!");
  458. }
  459. if (edtSuffixCsv.Text.Trim(Common.WhiteSpace).Length == 0)
  460. {
  461. MessageBox.Show("Enter an extension of the conversion file.");
  462. }
  463. edtFile.Enabled = false;
  464. btnBrowse.Enabled = false;
  465. bool IgnoreEmptyTranslations = chkIgnoreEmptyTranslationsCsv.Checked;
  466. bool success = false;
  467. if (radComma.Checked)
  468. {
  469. success = Common.TranslateFromCsv(FileName, edtSuffixCsv.Text, IgnoreEmptyTranslations, ",");
  470. }
  471. else
  472. {
  473. success = Common.TranslateFromCsv(FileName, edtSuffixCsv.Text, IgnoreEmptyTranslations, ";");
  474. }
  475. if (!success)
  476. {
  477. PrintNonGameErrors();
  478. }
  479. edtFile.Enabled = true;
  480. btnBrowse.Enabled = true;
  481. }
  482. }
  483. private void btnBeautify_Click(object sender, EventArgs e)
  484. {
  485. string FileName = edtFile.Text;
  486. if (CheckForFile(FileName, false))
  487. {
  488. edtFile.Enabled = false;
  489. btnBrowse.Enabled = false;
  490. bool success = false;
  491. success = game.Beautify(FileName);
  492. if (!success)
  493. {
  494. PrintNonGameErrors();
  495. }
  496. edtFile.Enabled = true;
  497. btnBrowse.Enabled = true;
  498. }
  499. }
  500. private void btnCancel_Click(object sender, EventArgs e)
  501. {
  502. _bw.CancelAsync();
  503. Log("Cancelled loading of file.");
  504. PrintLog();
  505. }
  506. }
  507. }