HplTrans.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * Copyright © 2011-2020 Frictional Games
  3. *
  4. * This file is part of Amnesia: A Machine For Pigs.
  5. *
  6. * Amnesia: A Machine For Pigs is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. * Amnesia: A Machine For Pigs is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Amnesia: A Machine For Pigs. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. using System;
  19. using System.Diagnostics;
  20. using System.ComponentModel;
  21. using System.IO;
  22. using System.Collections;
  23. using System.Collections.Specialized;
  24. using System.Xml;
  25. using System.Windows.Forms;
  26. namespace HplHelper
  27. {
  28. public class cTransEntry
  29. {
  30. public String msName="";
  31. public String msText="";
  32. public cTransEntry()
  33. {
  34. }
  35. }
  36. //----------------------------------------
  37. public class cTransCategory
  38. {
  39. public String msName="";
  40. public ArrayList mvEntries;
  41. public HplTrans mpTrans;
  42. public cTransCategory(HplTrans apTrans)
  43. {
  44. mvEntries = new ArrayList();
  45. mpTrans = apTrans;
  46. }
  47. public cTransEntry AddEntry(String asName)
  48. {
  49. cTransEntry Entry = new cTransEntry();
  50. Entry.msName = asName;
  51. mvEntries.Add(Entry);
  52. mpTrans.mpCurrentEntry = Entry;
  53. return Entry;
  54. }
  55. public void RemoveEntry(String asName)
  56. {
  57. for(int i=0; i< mvEntries.Count; i++)
  58. {
  59. cTransEntry Entry = (cTransEntry)mvEntries[i];
  60. if(Entry.msName == asName)
  61. {
  62. mvEntries.RemoveAt(i);
  63. mpTrans.mpCurrentEntry = null;
  64. break;
  65. }
  66. }
  67. }
  68. public cTransEntry GetEntry(String asName)
  69. {
  70. for(int i=0; i< mvEntries.Count; i++)
  71. {
  72. cTransEntry Entry = (cTransEntry)mvEntries[i];
  73. if(Entry.msName == asName)
  74. {
  75. return Entry;
  76. }
  77. }
  78. return null;
  79. }
  80. }
  81. //----------------------------------------
  82. /// <summary>
  83. /// Summary description for HplTrans.
  84. /// </summary>
  85. public class HplTrans
  86. {
  87. public ArrayList mvDirectories;
  88. public ArrayList mvCategories;
  89. public frmMain mpMainForm;
  90. public cTransCategory mpCurrentCat=null;
  91. public cTransEntry mpCurrentEntry=null;
  92. public bool mbChanged;
  93. public HplTrans(frmMain apMainForm)
  94. {
  95. mvCategories = new ArrayList();
  96. mpMainForm = apMainForm;
  97. mvDirectories = new ArrayList();
  98. mbChanged = false;
  99. }
  100. //------------------------------------------
  101. public cTransCategory AddCategory(String asName)
  102. {
  103. cTransCategory Cat = new cTransCategory(this);
  104. Cat.msName = asName;
  105. mvCategories.Add(Cat);
  106. /*
  107. mpMainForm.objTransCategories.Items.Add(asName);
  108. //The form list object
  109. for(int i=0; i<mpMainForm.objTransCategories.Items.Count; i++)
  110. {
  111. String sName = (String)mpMainForm.objTransCategories.Items[i];
  112. if(sName == asName)
  113. {
  114. mpMainForm.objTransCategories.SelectedIndex = i;
  115. break;
  116. }
  117. }
  118. */
  119. SetChanged();
  120. mpCurrentEntry = null;
  121. return Cat;
  122. }
  123. //------------------------------------------
  124. public void RemoveCategory(String asName)
  125. {
  126. //The internal list
  127. for(int i=0; i<mvCategories.Count; i++)
  128. {
  129. cTransCategory Cat = (cTransCategory)mvCategories[i];
  130. if(Cat.msName == asName)
  131. {
  132. mvCategories.RemoveAt(i);
  133. break;
  134. }
  135. }
  136. /*
  137. //The form list object
  138. for(int i=0; i<mpMainForm.objTransCategories.Items.Count; i++)
  139. {
  140. String sName = (String)mpMainForm.objTransCategories.Items[i];
  141. if(sName == asName)
  142. {
  143. mpMainForm.objTransCategories.Items.RemoveAt(i);
  144. if(mpMainForm.objTransCategories.SelectedIndex<0)
  145. mpMainForm.objTransCategories.SelectedIndex = 0;
  146. if(mpMainForm.objTransCategories.Items.Count<=0)
  147. mpMainForm.objTransCategories.Text = "";
  148. break;
  149. }
  150. }
  151. */
  152. SetChanged();
  153. }
  154. //------------------------------------------
  155. public void RenameCategory(String asName, String asNewName)
  156. {
  157. //The internal list
  158. for(int i=0; i<mvCategories.Count; i++)
  159. {
  160. cTransCategory Cat = (cTransCategory)mvCategories[i];
  161. if(Cat.msName == asName)
  162. {
  163. Cat.msName = asNewName;
  164. break;
  165. }
  166. }
  167. /*
  168. //The form list object
  169. for(int i=0; i<mpMainForm.objTransCategories.Items.Count; i++)
  170. {
  171. String sName = (String)mpMainForm.objTransCategories.Items[i];
  172. if(sName == asName)
  173. {
  174. mpMainForm.objTransCategories.Items[i] = asNewName;
  175. break;
  176. }
  177. }
  178. */
  179. SetChanged();
  180. //TODO: Sort the list and set the correct new selected index.
  181. }
  182. //------------------------------------------
  183. public void AddEntry(String asName)
  184. {
  185. if(mpCurrentCat!= null)
  186. {
  187. mpCurrentCat.AddEntry(asName);
  188. SetChanged();
  189. }
  190. }
  191. public void RemoveEntry(String asName)
  192. {
  193. if(mpCurrentCat!= null)
  194. {
  195. mpCurrentCat.RemoveEntry(asName);
  196. SetChanged();
  197. }
  198. }
  199. public void RenameCurrentEntry(String asNewName)
  200. {
  201. if(mpCurrentEntry!= null)
  202. {
  203. mpCurrentEntry.msName = asNewName;
  204. SetChanged();
  205. }
  206. }
  207. //------------------------------------------
  208. public void Load(String asFile)
  209. {
  210. mpCurrentCat = null;
  211. mpCurrentEntry = null;
  212. Clear();
  213. XmlDocument Doc = new XmlDocument();
  214. Doc.Load(asFile);
  215. XmlElement DocRoot = (XmlElement)Doc.FirstChild;
  216. /////////////////////////////////////////////////
  217. //Iterate all categories
  218. for(int child_count=0;child_count< DocRoot.ChildNodes.Count;child_count++)
  219. {
  220. XmlElement CatElem = (XmlElement)DocRoot.ChildNodes[child_count];
  221. if(CatElem.Name == "RESOURCES")
  222. {
  223. for(int dir=0;dir< CatElem.ChildNodes.Count;dir++)
  224. {
  225. XmlElement DirElem = (XmlElement)CatElem.ChildNodes[dir];
  226. String sPath = DirElem.GetAttribute("Path");
  227. mvDirectories.Add(sPath);
  228. }
  229. }
  230. else
  231. {
  232. cTransCategory pCat = new cTransCategory(this);
  233. pCat.msName = CatElem.GetAttribute("Name");
  234. mvCategories.Add(pCat);
  235. for(int entry=0; entry<CatElem.ChildNodes.Count; entry++)
  236. {
  237. XmlElement EntryElem = (XmlElement)CatElem.ChildNodes[entry];
  238. cTransEntry pEntry = new cTransEntry();
  239. pEntry.msName = EntryElem.GetAttribute("Name");
  240. String sText = EntryElem.InnerText;
  241. String sNewString = "";
  242. for(int i=0; i<sText.Length; ++i)
  243. {
  244. if(sText[i]=='[')
  245. {
  246. bool bFoundCommand = true;
  247. String sCommand = "";
  248. int lCount =1;
  249. while(sText[i+lCount] != ']' && i+lCount<sText.Length && lCount < 16)
  250. {
  251. sCommand += sText[i+lCount];
  252. lCount++;
  253. }
  254. if(sCommand=="br")
  255. {
  256. sNewString += "\r\n";
  257. }
  258. else if(sCommand[0]=='u')
  259. {
  260. int lNum = int.Parse(sCommand.Substring(1));
  261. sNewString += (char)lNum;
  262. }
  263. else
  264. {
  265. bFoundCommand = false;
  266. }
  267. //Go forward or add [ to string
  268. if(bFoundCommand)
  269. {
  270. i += lCount;
  271. }
  272. else
  273. {
  274. sNewString += sText[i];
  275. }
  276. }
  277. else
  278. {
  279. sNewString += sText[i];
  280. }
  281. }
  282. /*for(int i=0; i<sText.Length; ++i)
  283. {
  284. if(sText[i]=='[' && sText.Length > i + 4)
  285. {
  286. String sSign = sText.Substring(i,4);
  287. if(sSign == "[br]")
  288. {
  289. sNewString += "\r\n";
  290. i+=3;
  291. }
  292. else
  293. {
  294. sNewString += sText[i];
  295. }
  296. }
  297. else sNewString += sText[i];
  298. }*/
  299. pEntry.msText = sNewString;
  300. pCat.mvEntries.Add(pEntry);
  301. }
  302. }
  303. }
  304. mbChanged = false;
  305. }
  306. //------------------------------------------
  307. public void Save(String asFile)
  308. {
  309. XmlDocument Doc = new XmlDocument();
  310. XmlElement DocRoot = Doc.CreateElement("LANGUAGE");
  311. Doc.AppendChild(DocRoot);
  312. /////////////////////////////////
  313. // Save Directories
  314. XmlElement ResourceElem = Doc.CreateElement("RESOURCES");
  315. DocRoot.AppendChild(ResourceElem);
  316. for(int dir=0; dir < mvDirectories.Count; ++dir)
  317. {
  318. String sPath = (String)mvDirectories[dir];
  319. XmlElement DirElem = Doc.CreateElement("Directory");
  320. ResourceElem.AppendChild(DirElem);
  321. DirElem.SetAttribute("Path",sPath);
  322. }
  323. /////////////////////////////////
  324. // Save categories
  325. for(int cat=0; cat<mvCategories.Count; cat++)
  326. {
  327. cTransCategory pCat = (cTransCategory)mvCategories[cat];
  328. XmlElement CatElem = Doc.CreateElement("CATEGORY");
  329. DocRoot.AppendChild(CatElem);
  330. CatElem.SetAttribute("Name",pCat.msName);
  331. for(int entry=0; entry< pCat.mvEntries.Count; entry++)
  332. {
  333. cTransEntry pEntry = (cTransEntry)pCat.mvEntries[entry];
  334. XmlElement EntryElem = Doc.CreateElement("Entry");
  335. CatElem.AppendChild(EntryElem);
  336. EntryElem.SetAttribute("Name",pEntry.msName);
  337. String sText = pEntry.msText;
  338. String sNewString = "";
  339. for(int i=0; i<sText.Length; ++i)
  340. {
  341. if(sText[i]=='\n')
  342. {
  343. sNewString += "[br]";
  344. }
  345. else if(sText[i]=='\r')
  346. {
  347. //Do nothing...
  348. }
  349. else if(sText[i] > 255)
  350. {
  351. sNewString += "[u"+((int)sText[i]).ToString()+"]";
  352. }
  353. else
  354. {
  355. sNewString += sText[i];
  356. }
  357. }
  358. XmlText EntryTextElem = Doc.CreateTextNode(sNewString);
  359. EntryElem.AppendChild(EntryTextElem);
  360. }
  361. }
  362. Doc.Save(asFile);
  363. mbChanged = false;
  364. }
  365. //------------------------------------------
  366. public void SetCurrentCat(String asName)
  367. {
  368. //The internal list
  369. for(int i=0; i<mvCategories.Count; i++)
  370. {
  371. cTransCategory Cat = (cTransCategory)mvCategories[i];
  372. if(Cat.msName == asName)
  373. {
  374. mpCurrentCat = Cat;
  375. return;
  376. }
  377. }
  378. mpCurrentCat = null;
  379. }
  380. //------------------------------------------
  381. public void SetCurrentEntry(String asName)
  382. {
  383. if(mpCurrentCat==null) return;
  384. //The internal list
  385. for(int i=0; i<mpCurrentCat.mvEntries.Count; i++)
  386. {
  387. cTransEntry Entry = (cTransEntry)mpCurrentCat.mvEntries[i];
  388. if(Entry.msName == asName)
  389. {
  390. mpCurrentEntry = Entry;
  391. return;
  392. }
  393. }
  394. mpCurrentEntry = null;
  395. }
  396. //------------------------------------------
  397. public void SetCurrentEntryText(String asText)
  398. {
  399. if(mpCurrentEntry!=null)
  400. {
  401. mpCurrentEntry.msText = asText;
  402. SetChanged();
  403. }
  404. }
  405. //------------------------------------------
  406. public cTransCategory GetCategory(String asName)
  407. {
  408. for(int i=0; i<mvCategories.Count; i++)
  409. {
  410. cTransCategory Cat = (cTransCategory)mvCategories[i];
  411. if(Cat.msName == asName)
  412. {
  413. return Cat;
  414. }
  415. }
  416. return null;
  417. }
  418. //------------------------------------------
  419. public void Clear()
  420. {
  421. mvCategories.Clear();
  422. mvDirectories.Clear();
  423. }
  424. //------------------------------------------
  425. /**
  426. * Fills up a translation with categories and entries taken from a source one
  427. * If the invoking translation has stuff, it will add the ones which are not already in
  428. */
  429. public void CopyStructureFromTrans(HplTrans apSrcTrans, bool abClear)
  430. {
  431. bool bTransHasNonPresentEntries = false;
  432. if(abClear)
  433. Clear();
  434. ArrayList vAddedCategoryNames = new ArrayList();
  435. ArrayList vAddedCategoryCount = new ArrayList();
  436. ArrayList vAddedEntries = new ArrayList();
  437. ArrayList vAddedEntryTexts = new ArrayList();
  438. for(int i=0;i<apSrcTrans.mvDirectories.Count; ++i)
  439. {
  440. String sDir = (String)apSrcTrans.mvDirectories[i];
  441. if(mvDirectories.IndexOf(sDir)==-1)
  442. mvDirectories.Add(sDir);
  443. }
  444. for(int i=0;i<apSrcTrans.mvCategories.Count; ++i)
  445. {
  446. cTransCategory srcCat = (cTransCategory)apSrcTrans.mvCategories[i];
  447. cTransCategory cat = GetCategory(srcCat.msName);
  448. if(cat==null)
  449. {
  450. cat = AddCategory(srcCat.msName);
  451. }
  452. for(int j=0;j<srcCat.mvEntries.Count; ++j)
  453. {
  454. cTransEntry srcEntry = (cTransEntry)srcCat.mvEntries[j];
  455. cTransEntry entry = cat.GetEntry(srcEntry.msName);
  456. if(entry==null)
  457. {
  458. bTransHasNonPresentEntries = true;
  459. entry = cat.AddEntry(srcEntry.msName);
  460. vAddedEntries.Add(entry);
  461. vAddedEntryTexts.Add(srcEntry.msText);
  462. int lCatIndex = vAddedCategoryNames.IndexOf(cat.msName);
  463. if(lCatIndex==-1)
  464. {
  465. lCatIndex = vAddedCategoryNames.Add(cat.msName);
  466. vAddedCategoryCount.Add(0);
  467. }
  468. vAddedCategoryCount[lCatIndex] = (int)vAddedCategoryCount[lCatIndex] + 1;
  469. }
  470. }
  471. }
  472. if(abClear==false && bTransHasNonPresentEntries)
  473. {
  474. String sCategoryNames = "";
  475. for(int i=0;i<vAddedCategoryNames.Count; ++i)
  476. sCategoryNames += (String)vAddedCategoryNames[i] + ": " + vAddedCategoryCount[i] + " entries, ";
  477. sCategoryNames = sCategoryNames.Substring(0, sCategoryNames.Length-2);
  478. DialogResult lRes = MessageBox.Show(mpMainForm, "There are entries in Base Language that are not present in loaded file, in categories:\n " + sCategoryNames + "\nWant to add base text for them?",
  479. "Warning!",
  480. MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  481. if(lRes == DialogResult.Yes)
  482. {
  483. mbChanged = true;
  484. for(int i=0;i<vAddedEntries.Count; ++i)
  485. {
  486. cTransEntry entry = (cTransEntry) vAddedEntries[i];
  487. entry.msText = (String) vAddedEntryTexts[i];
  488. }
  489. }
  490. }
  491. }
  492. //------------------------------------------
  493. public void FillStructureFromTrans(HplTrans apSrcTrans)
  494. {
  495. }
  496. //------------------------------------------
  497. public void SetChanged()
  498. {
  499. mbChanged = true;
  500. }
  501. }
  502. }