Common.cs 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace Analyser
  6. {
  7. public class Common
  8. {
  9. const int QSP_MAXSTATSNAMES = 100;
  10. const int QSP_STATSLEVELS = 3;
  11. const int QSP_STATMAXARGS = 10;
  12. const int QSP_OPSLEVELS = 2;
  13. const int QSP_MAXOPSNAMES = 100;
  14. const int QSP_OPMAXARGS = 10;
  15. public const int QSP_STACKSIZE = 30;
  16. public const int QSP_MAXITEMS = 100;
  17. public const int INVALID_INDEX = -1;
  18. static public char[] WhiteSpace = {' ', '\t', '\r', '\n'};
  19. //Разделители - " \t&'\"()[]=!<>+-/*:,{}"
  20. static public char[] Delimiters = " \t&'\"()[]=!<>+-/*:,{}".ToCharArray();
  21. static public char[] Digits = "0123456789".ToCharArray();
  22. static public char[] LatinLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqsrtuvwxyz".ToCharArray();
  23. static public char[] RussianLetters = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъьэюя".ToCharArray();
  24. public struct QspStatement
  25. {
  26. public int ExtArg;
  27. public int MinArgsCount;
  28. public int MaxArgsCount;
  29. public int[] ArgsTypes;
  30. }
  31. public struct QspStatementName
  32. {
  33. public string Name;
  34. public int NameLen;
  35. public int Code;
  36. }
  37. public enum QspStatementType
  38. {
  39. Unknown,
  40. Label,
  41. Comment,
  42. Act,
  43. For,
  44. Local,
  45. If,
  46. ElseIf,
  47. Else,
  48. End,
  49. AddObj,
  50. ClA,
  51. Clear,
  52. CloseAll,
  53. Close,
  54. ClS,
  55. CmdClear,
  56. CopyArr,
  57. DelAct,
  58. DelObj,
  59. Dynamic,
  60. Exec,
  61. Exit,
  62. KillQST,
  63. DelLib,
  64. FreeLib,
  65. GoSub,
  66. GoTo,
  67. AddQST,
  68. AddLib,
  69. IncLib,
  70. Jump,
  71. KillAll,
  72. KillObj,
  73. KillVar,
  74. MClear,
  75. Menu,
  76. MNL,
  77. MPL,
  78. MP,
  79. Msg,
  80. NL,
  81. OpenGame,
  82. OpenQst,
  83. Play,
  84. PL,
  85. P,
  86. RefInt,
  87. SaveGame,
  88. SetTimer,
  89. Set,
  90. ShowActs,
  91. ShowInput,
  92. ShowObjs,
  93. ShowVars,
  94. UnSelect,
  95. View,
  96. Wait,
  97. XGoTo,
  98. Last_Statement
  99. };
  100. public struct QspFunction
  101. {
  102. public int Priority;
  103. public int ResType;
  104. public int MinArgsCount;
  105. public int MaxArgsCount;
  106. public int[] ArgsTypes;
  107. }
  108. public struct QspFunctionName
  109. {
  110. public string Name;
  111. public int NameLen;
  112. public int Code;
  113. }
  114. public enum QspFunctionType
  115. {
  116. Unknown,
  117. Start,
  118. End,
  119. Value,
  120. OpenBracket,
  121. Minus,
  122. Comma,
  123. CloseBracket,
  124. Mul,
  125. Div,
  126. Add,
  127. Sub,
  128. Mod,
  129. Ne,
  130. Leq,
  131. Geq,
  132. Eq,
  133. Lt,
  134. Gt,
  135. And,
  136. Or,
  137. Append,
  138. First_Function,
  139. Not = First_Function,
  140. Loc,
  141. Obj,
  142. Min,
  143. Max,
  144. Rand,
  145. IIf,
  146. RGB,
  147. Len,
  148. IsNum,
  149. LCase,
  150. UCase,
  151. Input,
  152. Str,
  153. Val,
  154. ArrSize,
  155. IsPlay,
  156. Desc,
  157. Trim,
  158. GetObj,
  159. StrComp,
  160. StrFind,
  161. StrPos,
  162. Mid,
  163. ArrPos,
  164. ArrComp,
  165. Instr,
  166. Replace,
  167. Func,
  168. DynEval,
  169. Rnd,
  170. CountObj,
  171. MsecsCount,
  172. QSPVer,
  173. UserText,
  174. CurLoc,
  175. SelObj,
  176. SelAct,
  177. MainText,
  178. StatText,
  179. CurActs,
  180. Last_Operation
  181. };
  182. static public QspStatement[] qspStats;
  183. static QspStatementName[,] qspStatsNames;
  184. static int[] qspStatsNamesCounts;
  185. static int qspStatMaxLen;
  186. static public QspFunction[] qspOps;
  187. static QspFunctionName[,] qspOpsNames;
  188. static int[] qspOpsNamesCounts;
  189. static int qspOpMaxLen;
  190. static bool inited = false;
  191. public class QspVariable
  192. {
  193. public bool IsString;
  194. public string Name;
  195. public bool Assigned;
  196. public bool Used;
  197. public QspVariable(string name, bool assigned, bool used)
  198. {
  199. IsString = name.IndexOf('$') == 0;
  200. Name = name;
  201. Assigned = assigned;
  202. Used = used;
  203. }
  204. }
  205. public class QspLocationLink
  206. {
  207. public string LocationName;
  208. public bool LocationExists;
  209. public bool LocationCalled;
  210. public QspLocationLink(string name, bool exists, bool called)
  211. {
  212. LocationName = name;
  213. LocationExists = exists;
  214. LocationCalled = called;
  215. }
  216. }
  217. public class QspObj
  218. {
  219. public string Name;
  220. public bool Added;
  221. public bool Removed;
  222. public QspObj(string name, bool added, bool removed)
  223. {
  224. Name = name;
  225. Added = added;
  226. Removed = removed;
  227. }
  228. }
  229. public class QspAct
  230. {
  231. public string Name;
  232. public bool Added;
  233. public bool Removed;
  234. public QspAct(string name, bool added, bool removed)
  235. {
  236. Name = name;
  237. Added = added;
  238. Removed = removed;
  239. }
  240. }
  241. static public List<QspVariable> vars;
  242. static public List<QspLocationLink> locationLinks;
  243. static public List<QspObj> objects;
  244. static public List<QspAct> acts;
  245. static public List<string> callerVariables;
  246. static public List<string> systemVariables;
  247. static public bool curlyParsing;
  248. static public string currentLocation;
  249. public enum QuoteType
  250. {
  251. None,
  252. Single,
  253. Double
  254. }
  255. public struct QspError
  256. {
  257. public string locationName; //Локация, на которой встретилась ошибка
  258. public string text; //Текст сообщения об ошибке
  259. public int line; //Номер строки в локации
  260. public bool isError; //Тип ошибки, если "true" - ошибка синтаксиса, если "false" - предупреждение
  261. }
  262. static List<QspError> m_errors;
  263. static int m_errorCounter;
  264. public static int GetErrorsCount()
  265. {
  266. return m_errorCounter;
  267. }
  268. static int m_warningCounter;
  269. public static int GetWarningsCount()
  270. {
  271. return m_warningCounter;
  272. }
  273. public Common()
  274. {
  275. if (!inited)
  276. {
  277. inited = true;
  278. qspStats = new QspStatement[(int)QspStatementType.Last_Statement];
  279. qspStatsNames = new QspStatementName[QSP_STATSLEVELS, QSP_MAXSTATSNAMES];
  280. qspStatsNamesCounts = new int[QSP_STATSLEVELS];
  281. qspOps = new QspFunction[(int)QspFunctionType.Last_Operation];
  282. qspOpsNames = new QspFunctionName[QSP_OPSLEVELS, QSP_MAXOPSNAMES];
  283. qspOpsNamesCounts = new int[QSP_OPSLEVELS];
  284. m_errors = new List<QspError>();
  285. InitQsp();
  286. ClearErrors();
  287. }
  288. }
  289. static public void ClearErrors()
  290. {
  291. m_errors.Clear();
  292. m_errorCounter = 0;
  293. m_warningCounter = 0;
  294. }
  295. static public void ClearNonGameErrors()
  296. {
  297. for (int index = 0; index < m_errors.Count; ++index)
  298. {
  299. if (m_errors[index].isError && (m_errors[index].line == INVALID_INDEX))
  300. {
  301. m_errors.RemoveAt(index);
  302. --index;
  303. m_errorCounter--;
  304. }
  305. }
  306. }
  307. static public void SubmitError(string text, int line)
  308. {
  309. QspError e;
  310. e.locationName = currentLocation;
  311. e.text = text;
  312. e.line = line;
  313. e.isError = true;
  314. m_errors.Add(e);
  315. m_errorCounter++;
  316. }
  317. static public void SubmitWarning(string text, int line)
  318. {
  319. QspError w;
  320. w.locationName = currentLocation;
  321. w.text = text;
  322. w.line = line;
  323. w.isError = false;
  324. m_errors.Add(w);
  325. m_warningCounter++;
  326. }
  327. static public List<QspError> GetErrors()
  328. {
  329. return m_errors;
  330. }
  331. static public bool StartOfMultiWordOperator(string op)
  332. {
  333. return op.ToUpper().Equals("ADD") || op.ToUpper().Equals("DEL");
  334. }
  335. static public void qspAddStatement(int statCode, int extArg, /*QSP_STATEMENT func,*/ int minArgs, int maxArgs, params int[] marker)
  336. {
  337. int i;
  338. qspStats[statCode].ExtArg = extArg;
  339. //qspStats[statCode].Func = func;
  340. qspStats[statCode].MinArgsCount = minArgs;
  341. qspStats[statCode].MaxArgsCount = maxArgs;
  342. qspStats[statCode].ArgsTypes = new int[QSP_STATMAXARGS];
  343. if (maxArgs > 0)
  344. {
  345. for (i = 0; i < maxArgs; ++i)
  346. qspStats[statCode].ArgsTypes[i] = marker[i];
  347. }
  348. }
  349. static public void qspAddStatName(int statCode, string statName, int level)
  350. {
  351. int len = statName.Length;
  352. int count = qspStatsNamesCounts[level];
  353. qspStatsNames[level, count].Name = statName;
  354. qspStatsNames[level, count].NameLen = len;
  355. qspStatsNames[level, count].Code = statCode;
  356. qspStatsNamesCounts[level] = count + 1;
  357. /* Max length */
  358. if (len > qspStatMaxLen) qspStatMaxLen = len;
  359. }
  360. static public void qspAddOperation(int opCode, int priority, /*QSP_FUNCTION func,*/ int resType, int minArgs, int maxArgs, params int[] marker)
  361. {
  362. int i;
  363. qspOps[opCode].Priority = priority;
  364. //qspOps[opCode].Func = func;
  365. qspOps[opCode].ResType = resType;
  366. qspOps[opCode].MinArgsCount = minArgs;
  367. qspOps[opCode].MaxArgsCount = maxArgs;
  368. qspOps[opCode].ArgsTypes = new int[QSP_OPMAXARGS];
  369. if (maxArgs > 0)
  370. {
  371. for (i = 0; i < maxArgs; ++i)
  372. qspOps[opCode].ArgsTypes[i] = marker[i];
  373. }
  374. }
  375. static public void qspAddOpName(int opCode, string opName, int level)
  376. {
  377. int len = opName.Length;
  378. int count = qspOpsNamesCounts[level];
  379. qspOpsNames[level, count].Name = opName;
  380. qspOpsNames[level, count].NameLen = len;
  381. qspOpsNames[level, count].Code = opCode;
  382. qspOpsNamesCounts[level] = count + 1;
  383. /* Max length */
  384. if (len > qspOpMaxLen) qspOpMaxLen = len;
  385. }
  386. static public void InitQsp()
  387. {
  388. qspStatMaxLen = 0;
  389. // код оператора, ExtArg, минимальное кол-во аргументов, максимальное кол-во аргументов, типы аргументов
  390. // 0 - авто
  391. // 1 - строка
  392. // 2 - числовой
  393. qspAddStatement((int)QspStatementType.Else, 0, 0, 0);
  394. qspAddStatement((int)QspStatementType.ElseIf, 0, 1, 1, 2);
  395. qspAddStatement((int)QspStatementType.End, 0, 0, 0);
  396. qspAddStatement((int)QspStatementType.Local, 0, 0, 0);
  397. qspAddStatement((int)QspStatementType.Set, 0, 0, 0);
  398. qspAddStatement((int)QspStatementType.If, 0, 1, 1, 2);
  399. qspAddStatement((int)QspStatementType.Act, 0, 1, 2, 1, 1);
  400. qspAddStatement((int)QspStatementType.For, 0, 0, 0);
  401. qspAddStatement((int)QspStatementType.AddObj, 0, 1, 3, 1, 1, 2);
  402. qspAddStatement((int)QspStatementType.ClA, 3, 0, 0);
  403. qspAddStatement((int)QspStatementType.CloseAll, 1, 0, 0);
  404. qspAddStatement((int)QspStatementType.Close, 0, 0, 1, 1);
  405. qspAddStatement((int)QspStatementType.ClS, 4, 0, 0);
  406. qspAddStatement((int)QspStatementType.CmdClear, 2, 0, 0);
  407. qspAddStatement((int)QspStatementType.CopyArr, 0, 2, 4, 1, 1, 2, 2);
  408. qspAddStatement((int)QspStatementType.DelAct, 0, 1, 1, 1);
  409. qspAddStatement((int)QspStatementType.DelObj, 0, 1, 1, 1);
  410. qspAddStatement((int)QspStatementType.Dynamic, 0, 1, 10, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  411. qspAddStatement((int)QspStatementType.Exec, 0, 1, 1, 1);
  412. qspAddStatement((int)QspStatementType.Exit, 0, 0, 0);
  413. qspAddStatement((int)QspStatementType.KillQST, 6, 0, 0);
  414. qspAddStatement((int)QspStatementType.DelLib, 6, 0, 0);
  415. qspAddStatement((int)QspStatementType.FreeLib, 6, 0, 0);
  416. qspAddStatement((int)QspStatementType.GoSub, 0, 1, 10, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  417. qspAddStatement((int)QspStatementType.GoTo, 1, 1, 10, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  418. qspAddStatement((int)QspStatementType.AddQST, 1, 1, 1, 1);
  419. qspAddStatement((int)QspStatementType.AddLib, 1, 1, 1, 1);
  420. qspAddStatement((int)QspStatementType.IncLib, 1, 1, 1, 1);
  421. qspAddStatement((int)QspStatementType.Jump, 0, 1, 1, 1);
  422. qspAddStatement((int)QspStatementType.KillAll, 5, 0, 0);
  423. qspAddStatement((int)QspStatementType.KillObj, 1, 0, 1, 2);
  424. qspAddStatement((int)QspStatementType.KillVar, 0, 0, 2, 1, 2);
  425. qspAddStatement((int)QspStatementType.Menu, 0, 1, 3, 1, 2, 2);
  426. qspAddStatement((int)QspStatementType.MClear, 1, 0, 0);
  427. qspAddStatement((int)QspStatementType.MNL, 5, 0, 1, 1);
  428. qspAddStatement((int)QspStatementType.MPL, 3, 0, 1, 1);
  429. qspAddStatement((int)QspStatementType.MP, 1, 1, 1, 1);
  430. qspAddStatement((int)QspStatementType.Clear, 0, 0, 0);
  431. qspAddStatement((int)QspStatementType.NL, 4, 0, 1, 1);
  432. qspAddStatement((int)QspStatementType.PL, 2, 0, 1, 1);
  433. qspAddStatement((int)QspStatementType.P, 0, 1, 1, 1);
  434. qspAddStatement((int)QspStatementType.Msg, 0, 1, 1, 1);
  435. qspAddStatement((int)QspStatementType.OpenGame, 0, 0, 1, 1);
  436. qspAddStatement((int)QspStatementType.OpenQst, 0, 1, 1, 1);
  437. qspAddStatement((int)QspStatementType.Play, 0, 1, 2, 1, 2);
  438. qspAddStatement((int)QspStatementType.RefInt, 0, 0, 0);
  439. qspAddStatement((int)QspStatementType.SaveGame, 0, 0, 1, 1);
  440. qspAddStatement((int)QspStatementType.SetTimer, 0, 1, 1, 2);
  441. qspAddStatement((int)QspStatementType.ShowActs, 0, 1, 1, 2);
  442. qspAddStatement((int)QspStatementType.ShowInput, 3, 1, 1, 2);
  443. qspAddStatement((int)QspStatementType.ShowObjs, 1, 1, 1, 2);
  444. qspAddStatement((int)QspStatementType.ShowVars, 2, 1, 1, 2);
  445. qspAddStatement((int)QspStatementType.UnSelect, 0, 0, 0);
  446. qspAddStatement((int)QspStatementType.View, 0, 0, 1, 1);
  447. qspAddStatement((int)QspStatementType.Wait, 0, 1, 1, 2);
  448. qspAddStatement((int)QspStatementType.XGoTo, 0, 1, 10, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  449. /* Names */
  450. qspAddStatName((int)QspStatementType.Else, "ELSE", 2);
  451. qspAddStatName((int)QspStatementType.ElseIf, "ELSEIF", 1);
  452. qspAddStatName((int)QspStatementType.End, "END", 2);
  453. qspAddStatName((int)QspStatementType.Local, "LOCAL", 2);
  454. qspAddStatName((int)QspStatementType.Set, "SET", 2);
  455. qspAddStatName((int)QspStatementType.Set, "LET", 2);
  456. qspAddStatName((int)QspStatementType.If, "IF", 2);
  457. qspAddStatName((int)QspStatementType.Act, "ACT", 2);
  458. qspAddStatName((int)QspStatementType.For, "FOR", 2);
  459. qspAddStatName((int)QspStatementType.AddObj, "ADDOBJ", 2);
  460. qspAddStatName((int)QspStatementType.AddObj, "ADD OBJ", 2);
  461. qspAddStatName((int)QspStatementType.ClA, "CLA", 2);
  462. qspAddStatName((int)QspStatementType.CloseAll, "CLOSE ALL", 1);
  463. qspAddStatName((int)QspStatementType.Close, "CLOSE", 2);
  464. qspAddStatName((int)QspStatementType.ClS, "CLS", 2);
  465. qspAddStatName((int)QspStatementType.CmdClear, "CMDCLEAR", 2);
  466. qspAddStatName((int)QspStatementType.CmdClear, "CMDCLR", 2);
  467. qspAddStatName((int)QspStatementType.CopyArr, "COPYARR", 2);
  468. qspAddStatName((int)QspStatementType.DelAct, "DELACT", 2);
  469. qspAddStatName((int)QspStatementType.DelAct, "DEL ACT", 2);
  470. qspAddStatName((int)QspStatementType.DelObj, "DELOBJ", 2);
  471. qspAddStatName((int)QspStatementType.DelObj, "DEL OBJ", 2);
  472. qspAddStatName((int)QspStatementType.Dynamic, "DYNAMIC", 2);
  473. qspAddStatName((int)QspStatementType.Exec, "EXEC", 2);
  474. qspAddStatName((int)QspStatementType.Exit, "EXIT", 2);
  475. qspAddStatName((int)QspStatementType.KillQST, "KILLQST", 2);
  476. qspAddStatName((int)QspStatementType.DelLib, "DELLIB", 2);
  477. qspAddStatName((int)QspStatementType.FreeLib, "FREELIB", 2);
  478. qspAddStatName((int)QspStatementType.GoSub, "GOSUB", 2);
  479. qspAddStatName((int)QspStatementType.GoSub, "GS", 2);
  480. qspAddStatName((int)QspStatementType.GoTo, "GOTO", 2);
  481. qspAddStatName((int)QspStatementType.GoTo, "GT", 2);
  482. qspAddStatName((int)QspStatementType.AddQST, "ADDQST", 2);
  483. qspAddStatName((int)QspStatementType.AddLib, "ADDLIB", 2);
  484. qspAddStatName((int)QspStatementType.IncLib, "INCLIB", 2);
  485. qspAddStatName((int)QspStatementType.Jump, "JUMP", 2);
  486. qspAddStatName((int)QspStatementType.KillAll, "KILLALL", 2);
  487. qspAddStatName((int)QspStatementType.KillObj, "KILLOBJ", 2);
  488. qspAddStatName((int)QspStatementType.KillVar, "KILLVAR", 2);
  489. qspAddStatName((int)QspStatementType.Menu, "MENU", 2);
  490. qspAddStatName((int)QspStatementType.MClear, "*CLEAR", 2);
  491. qspAddStatName((int)QspStatementType.MClear, "*CLR", 2);
  492. qspAddStatName((int)QspStatementType.MNL, "*NL", 2);
  493. qspAddStatName((int)QspStatementType.MPL, "*PL", 1);
  494. qspAddStatName((int)QspStatementType.MP, "*P", 2);
  495. qspAddStatName((int)QspStatementType.Clear, "CLEAR", 2);
  496. qspAddStatName((int)QspStatementType.Clear, "CLR", 2);
  497. qspAddStatName((int)QspStatementType.NL, "NL", 2);
  498. qspAddStatName((int)QspStatementType.PL, "PL", 1);
  499. qspAddStatName((int)QspStatementType.P, "P", 2);
  500. qspAddStatName((int)QspStatementType.Msg, "MSG", 2);
  501. qspAddStatName((int)QspStatementType.OpenGame, "OPENGAME", 2);
  502. qspAddStatName((int)QspStatementType.OpenQst, "OPENQST", 2);
  503. qspAddStatName((int)QspStatementType.Play, "PLAY", 0);
  504. qspAddStatName((int)QspStatementType.RefInt, "REFINT", 2);
  505. qspAddStatName((int)QspStatementType.SaveGame, "SAVEGAME", 2);
  506. qspAddStatName((int)QspStatementType.SetTimer, "SETTIMER", 1);
  507. qspAddStatName((int)QspStatementType.ShowActs, "SHOWACTS", 2);
  508. qspAddStatName((int)QspStatementType.ShowInput, "SHOWINPUT", 2);
  509. qspAddStatName((int)QspStatementType.ShowObjs, "SHOWOBJS", 2);
  510. qspAddStatName((int)QspStatementType.ShowVars, "SHOWSTAT", 2);
  511. qspAddStatName((int)QspStatementType.UnSelect, "UNSELECT", 1);
  512. qspAddStatName((int)QspStatementType.UnSelect, "UNSEL", 2);
  513. qspAddStatName((int)QspStatementType.View, "VIEW", 2);
  514. qspAddStatName((int)QspStatementType.Wait, "WAIT", 2);
  515. qspAddStatName((int)QspStatementType.XGoTo, "XGOTO", 2);
  516. qspAddStatName((int)QspStatementType.XGoTo, "XGT", 2);
  517. //Математические операторы и функции
  518. int i;
  519. for (i = 0; i < QSP_OPSLEVELS; ++i) qspOpsNamesCounts[i] = 0;
  520. qspOpMaxLen = 0;
  521. // код оператора, приоритет, тип результата, минимальное кол-во аргументов, максимальное кол-во аргументов, типы аргументов
  522. // 0 - авто
  523. // 1 - строка
  524. // 2 - числовой
  525. qspAddOperation((int)QspFunctionType.Value, 0, 0, 0, 0);
  526. qspAddOperation((int)QspFunctionType.Start, 127, 0, 0, 0);
  527. qspAddOperation((int)QspFunctionType.End, 0, 0, 0, 0);
  528. qspAddOperation((int)QspFunctionType.OpenBracket, 127, 0, 0, 0);
  529. qspAddOperation((int)QspFunctionType.CloseBracket, 0, 0, 0, 0);
  530. qspAddOperation((int)QspFunctionType.Minus, 18, 2, 1, 1, 2);
  531. qspAddOperation((int)QspFunctionType.Add, 14, 0, 2, 2, 0, 0);
  532. qspAddOperation((int)QspFunctionType.Sub, 14, 2, 2, 2, 2, 2);
  533. qspAddOperation((int)QspFunctionType.Mul, 17, 2, 2, 2, 2, 2);
  534. qspAddOperation((int)QspFunctionType.Div, 17, 2, 2, 2, 2, 2);
  535. qspAddOperation((int)QspFunctionType.Mod, 16, 2, 2, 2, 2, 2);
  536. qspAddOperation((int)QspFunctionType.Ne, 10, 2, 2, 2, 0, 0);
  537. qspAddOperation((int)QspFunctionType.Leq, 10, 2, 2, 2, 0, 0);
  538. qspAddOperation((int)QspFunctionType.Geq, 10, 2, 2, 2, 0, 0);
  539. qspAddOperation((int)QspFunctionType.Eq, 10, 2, 2, 2, 0, 0);
  540. qspAddOperation((int)QspFunctionType.Lt, 10, 2, 2, 2, 0, 0);
  541. qspAddOperation((int)QspFunctionType.Gt, 10, 2, 2, 2, 0, 0);
  542. qspAddOperation((int)QspFunctionType.Append, 12, 1, 2, 2, 1, 1);
  543. qspAddOperation((int)QspFunctionType.Comma, 0, 1, 2, 2, 1, 1);
  544. qspAddOperation((int)QspFunctionType.And, 7, 2, 2, 2, 2, 2);
  545. qspAddOperation((int)QspFunctionType.Or, 6, 2, 2, 2, 2, 2);
  546. qspAddOperation((int)QspFunctionType.Loc, 8, 2, 1, 1, 1);
  547. qspAddOperation((int)QspFunctionType.Obj, 8, 2, 1, 1, 1);
  548. qspAddOperation((int)QspFunctionType.Not, 8, 2, 1, 1, 2);
  549. qspAddOperation((int)QspFunctionType.Min, 30, 0, 1, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  550. qspAddOperation((int)QspFunctionType.Max, 30, 0, 1, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  551. qspAddOperation((int)QspFunctionType.Rand, 30, 2, 1, 2, 2, 2);
  552. qspAddOperation((int)QspFunctionType.IIf, 30, 0, 3, 3, 2, 0, 0);
  553. qspAddOperation((int)QspFunctionType.RGB, 30, 2, 3, 4, 2, 2, 2, 2);
  554. qspAddOperation((int)QspFunctionType.Len, 30, 2, 1, 1, 1);
  555. qspAddOperation((int)QspFunctionType.IsNum, 30, 2, 1, 1, 0);
  556. qspAddOperation((int)QspFunctionType.LCase, 30, 1, 1, 1, 1);
  557. qspAddOperation((int)QspFunctionType.UCase, 30, 1, 1, 1, 1);
  558. qspAddOperation((int)QspFunctionType.Input, 30, 1, 1, 1, 1);
  559. qspAddOperation((int)QspFunctionType.Str, 30, 1, 1, 1, 1);
  560. qspAddOperation((int)QspFunctionType.Val, 30, 2, 1, 1, 0);
  561. qspAddOperation((int)QspFunctionType.ArrSize, 30, 2, 1, 1, 1);
  562. qspAddOperation((int)QspFunctionType.IsPlay, 30, 2, 1, 1, 1);
  563. qspAddOperation((int)QspFunctionType.Desc, 30, 1, 1, 1, 1);
  564. qspAddOperation((int)QspFunctionType.Trim, 30, 1, 1, 1, 1);
  565. qspAddOperation((int)QspFunctionType.GetObj, 30, 1, 1, 1, 2);
  566. qspAddOperation((int)QspFunctionType.StrComp, 30, 2, 2, 2, 1, 1);
  567. qspAddOperation((int)QspFunctionType.StrFind, 30, 1, 2, 3, 1, 1, 2);
  568. qspAddOperation((int)QspFunctionType.StrPos, 30, 2, 2, 3, 1, 1, 2);
  569. qspAddOperation((int)QspFunctionType.Mid, 30, 1, 2, 3, 1, 2, 2);
  570. qspAddOperation((int)QspFunctionType.ArrPos, 30, 2, 2, 3, 1, 0, 2);
  571. qspAddOperation((int)QspFunctionType.ArrComp, 30, 2, 2, 3, 1, 0, 2);
  572. qspAddOperation((int)QspFunctionType.Instr, 30, 2, 2, 3, 1, 1, 2);
  573. qspAddOperation((int)QspFunctionType.Replace, 30, 1, 2, 3, 1, 1, 1);
  574. qspAddOperation((int)QspFunctionType.Func, 30, 0, 1, 10, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  575. qspAddOperation((int)QspFunctionType.DynEval, 30, 0, 1, 10, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  576. qspAddOperation((int)QspFunctionType.Rnd, 30, 2, 0, 0);
  577. qspAddOperation((int)QspFunctionType.CountObj, 30, 2, 0, 0);
  578. qspAddOperation((int)QspFunctionType.MsecsCount, 30, 2, 0, 0);
  579. qspAddOperation((int)QspFunctionType.QSPVer, 30, 1, 0, 0);
  580. qspAddOperation((int)QspFunctionType.UserText, 30, 1, 0, 0);
  581. qspAddOperation((int)QspFunctionType.CurLoc, 30, 1, 0, 0);
  582. qspAddOperation((int)QspFunctionType.SelObj, 30, 1, 0, 0);
  583. qspAddOperation((int)QspFunctionType.SelAct, 30, 1, 0, 0);
  584. qspAddOperation((int)QspFunctionType.MainText, 30, 1, 0, 0);
  585. qspAddOperation((int)QspFunctionType.StatText, 30, 1, 0, 0);
  586. qspAddOperation((int)QspFunctionType.CurActs, 30, 1, 0, 0);
  587. /* Names */
  588. qspAddOpName((int)QspFunctionType.CloseBracket, ")", 1);
  589. qspAddOpName((int)QspFunctionType.Add, "+", 1);
  590. qspAddOpName((int)QspFunctionType.Sub, "-", 1);
  591. qspAddOpName((int)QspFunctionType.Mul, "*", 1);
  592. qspAddOpName((int)QspFunctionType.Div, "/", 1);
  593. qspAddOpName((int)QspFunctionType.Mod, "MOD", 1);
  594. qspAddOpName((int)QspFunctionType.Ne, "!", 1);
  595. qspAddOpName((int)QspFunctionType.Ne, "<>", 0);
  596. qspAddOpName((int)QspFunctionType.Leq, "<=", 0);
  597. qspAddOpName((int)QspFunctionType.Leq, "=<", 0);
  598. qspAddOpName((int)QspFunctionType.Geq, ">=", 0);
  599. qspAddOpName((int)QspFunctionType.Geq, "=>", 0);
  600. qspAddOpName((int)QspFunctionType.Eq, "=", 1);
  601. qspAddOpName((int)QspFunctionType.Lt, "<", 1);
  602. qspAddOpName((int)QspFunctionType.Gt, ">", 1);
  603. qspAddOpName((int)QspFunctionType.Append, "&", 1);
  604. qspAddOpName((int)QspFunctionType.Comma, ",", 1);
  605. qspAddOpName((int)QspFunctionType.And, "AND", 1);
  606. qspAddOpName((int)QspFunctionType.Or, "OR", 1);
  607. qspAddOpName((int)QspFunctionType.Loc, "LOC", 1);
  608. qspAddOpName((int)QspFunctionType.Obj, "OBJ", 1);
  609. qspAddOpName((int)QspFunctionType.Not, "NO", 1);
  610. qspAddOpName((int)QspFunctionType.Min, "MIN", 1);
  611. qspAddOpName((int)QspFunctionType.Min, "$MIN", 1);
  612. qspAddOpName((int)QspFunctionType.Max, "MAX", 1);
  613. qspAddOpName((int)QspFunctionType.Max, "$MAX", 1);
  614. qspAddOpName((int)QspFunctionType.Rand, "RAND", 1);
  615. qspAddOpName((int)QspFunctionType.IIf, "IIF", 1);
  616. qspAddOpName((int)QspFunctionType.IIf, "$IIF", 1);
  617. qspAddOpName((int)QspFunctionType.RGB, "RGB", 1);
  618. qspAddOpName((int)QspFunctionType.Len, "LEN", 1);
  619. qspAddOpName((int)QspFunctionType.IsNum, "ISNUM", 1);
  620. qspAddOpName((int)QspFunctionType.LCase, "LCASE", 1);
  621. qspAddOpName((int)QspFunctionType.LCase, "$LCASE", 1);
  622. qspAddOpName((int)QspFunctionType.UCase, "UCASE", 1);
  623. qspAddOpName((int)QspFunctionType.UCase, "$UCASE", 1);
  624. qspAddOpName((int)QspFunctionType.Input, "INPUT", 1);
  625. qspAddOpName((int)QspFunctionType.Input, "$INPUT", 1);
  626. qspAddOpName((int)QspFunctionType.Str, "STR", 1);
  627. qspAddOpName((int)QspFunctionType.Str, "$STR", 1);
  628. qspAddOpName((int)QspFunctionType.Val, "VAL", 1);
  629. qspAddOpName((int)QspFunctionType.ArrSize, "ARRSIZE", 1);
  630. qspAddOpName((int)QspFunctionType.IsPlay, "ISPLAY", 1);
  631. qspAddOpName((int)QspFunctionType.Desc, "DESC", 1);
  632. qspAddOpName((int)QspFunctionType.Desc, "$DESC", 1);
  633. qspAddOpName((int)QspFunctionType.Trim, "TRIM", 1);
  634. qspAddOpName((int)QspFunctionType.Trim, "$TRIM", 1);
  635. qspAddOpName((int)QspFunctionType.GetObj, "GETOBJ", 1);
  636. qspAddOpName((int)QspFunctionType.GetObj, "$GETOBJ", 1);
  637. qspAddOpName((int)QspFunctionType.StrComp, "STRCOMP", 1);
  638. qspAddOpName((int)QspFunctionType.StrFind, "STRFIND", 1);
  639. qspAddOpName((int)QspFunctionType.StrFind, "$STRFIND", 1);
  640. qspAddOpName((int)QspFunctionType.StrPos, "STRPOS", 1);
  641. qspAddOpName((int)QspFunctionType.Mid, "MID", 1);
  642. qspAddOpName((int)QspFunctionType.Mid, "$MID", 1);
  643. qspAddOpName((int)QspFunctionType.ArrPos, "ARRPOS", 1);
  644. qspAddOpName((int)QspFunctionType.ArrComp, "ARRCOMP", 1);
  645. qspAddOpName((int)QspFunctionType.Instr, "INSTR", 1);
  646. qspAddOpName((int)QspFunctionType.Replace, "REPLACE", 1);
  647. qspAddOpName((int)QspFunctionType.Replace, "$REPLACE", 1);
  648. qspAddOpName((int)QspFunctionType.Func, "FUNC", 1);
  649. qspAddOpName((int)QspFunctionType.Func, "$FUNC", 1);
  650. qspAddOpName((int)QspFunctionType.DynEval, "DYNEVAL", 1);
  651. qspAddOpName((int)QspFunctionType.DynEval, "$DYNEVAL", 1);
  652. qspAddOpName((int)QspFunctionType.Rnd, "RND", 1);
  653. qspAddOpName((int)QspFunctionType.CountObj, "COUNTOBJ", 1);
  654. qspAddOpName((int)QspFunctionType.MsecsCount, "MSECSCOUNT", 1);
  655. qspAddOpName((int)QspFunctionType.QSPVer, "QSPVER", 1);
  656. qspAddOpName((int)QspFunctionType.QSPVer, "$QSPVER", 1);
  657. qspAddOpName((int)QspFunctionType.UserText, "USER_TEXT", 1);
  658. qspAddOpName((int)QspFunctionType.UserText, "$USER_TEXT", 1);
  659. qspAddOpName((int)QspFunctionType.UserText, "USRTXT", 1);
  660. qspAddOpName((int)QspFunctionType.UserText, "$USRTXT", 1);
  661. qspAddOpName((int)QspFunctionType.CurLoc, "CURLOC", 1);
  662. qspAddOpName((int)QspFunctionType.CurLoc, "$CURLOC", 1);
  663. qspAddOpName((int)QspFunctionType.SelObj, "SELOBJ", 1);
  664. qspAddOpName((int)QspFunctionType.SelObj, "$SELOBJ", 1);
  665. qspAddOpName((int)QspFunctionType.SelAct, "SELACT", 1);
  666. qspAddOpName((int)QspFunctionType.SelAct, "$SELACT", 1);
  667. qspAddOpName((int)QspFunctionType.MainText, "MAINTXT", 1);
  668. qspAddOpName((int)QspFunctionType.MainText, "$MAINTXT", 1);
  669. qspAddOpName((int)QspFunctionType.StatText, "STATTXT", 1);
  670. qspAddOpName((int)QspFunctionType.StatText, "$STATTXT", 1);
  671. qspAddOpName((int)QspFunctionType.CurActs, "CURACTS", 1);
  672. qspAddOpName((int)QspFunctionType.CurActs, "$CURACTS", 1);
  673. }
  674. static public int qspGetStatCode(string s)
  675. {
  676. if (s.Length == 0)
  677. return (int)QspStatementType.Unknown;
  678. if (s[0] == ':')
  679. return (int)QspStatementType.Label;
  680. if (s[0] == '!')
  681. return (int)QspStatementType.Comment;
  682. int len;
  683. if (s.Length < qspStatMaxLen)
  684. len = s.Length;
  685. else
  686. len = qspStatMaxLen;
  687. string name = s.Substring(0, len).ToUpper().Trim(WhiteSpace);
  688. for (int i = 0; i < QSP_STATSLEVELS; ++i)
  689. {
  690. for (int j = 0; j < qspStatsNamesCounts[i]; j++)
  691. {
  692. if (name.Equals(qspStatsNames[i,j].Name))
  693. return qspStatsNames[i,j].Code;
  694. }
  695. }
  696. return (int)QspStatementType.Unknown;
  697. }
  698. static public int qspGetFunctionCode(string s, bool functionsOnly)
  699. {
  700. if (s.Length == 0)
  701. return (int)QspFunctionType.End;
  702. int len;
  703. if (s.Length < qspOpMaxLen)
  704. len = s.Length;
  705. else
  706. len = qspOpMaxLen;
  707. string name = s.Substring(0, len).ToUpper().Trim(WhiteSpace);
  708. for (int i = 0; i < QSP_OPSLEVELS; ++i)
  709. {
  710. for (int j = 0; j < qspOpsNamesCounts[i]; j++)
  711. {
  712. if ((name.Equals(qspOpsNames[i, j].Name)) &&
  713. (!functionsOnly || (qspOpsNames[i, j].Code >= (int)QspFunctionType.First_Function)))
  714. return qspOpsNames[i, j].Code;
  715. }
  716. }
  717. return (int)QspFunctionType.Unknown;
  718. }
  719. static public QspVariable GetVar(string name)
  720. {
  721. foreach (QspVariable var in vars)
  722. {
  723. if (var.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
  724. return var;
  725. }
  726. return null;
  727. }
  728. static public void AddVar(string name, bool assigned, bool used)
  729. {
  730. QspVariable var = GetVar(name);
  731. if (var == null)
  732. {
  733. var = new QspVariable(name, assigned, used);
  734. vars.Add(var);
  735. }
  736. else
  737. {
  738. if (assigned)
  739. var.Assigned = assigned;
  740. if (used)
  741. var.Used = used;
  742. }
  743. }
  744. static public QspLocationLink GetLocationLink(string name)
  745. {
  746. foreach (QspLocationLink loc in locationLinks)
  747. {
  748. if (loc.LocationName.Equals(name, StringComparison.OrdinalIgnoreCase))
  749. return loc;
  750. }
  751. return null;
  752. }
  753. static public void AddLocationLink(string name, bool exists, bool called)
  754. {
  755. QspLocationLink loc = GetLocationLink(name);
  756. if (loc == null)
  757. {
  758. loc = new QspLocationLink(name, exists, called);
  759. locationLinks.Add(loc);
  760. }
  761. else
  762. {
  763. if (exists)
  764. loc.LocationExists = exists;
  765. if (called)
  766. loc.LocationCalled = called;
  767. }
  768. }
  769. static public QspObj GetObj(string name)
  770. {
  771. foreach (QspObj obj in objects)
  772. {
  773. if (obj.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
  774. return obj;
  775. }
  776. return null;
  777. }
  778. static public void AddObj(string name, bool added, bool removed)
  779. {
  780. QspObj obj = GetObj(name);
  781. if (obj == null)
  782. {
  783. obj = new QspObj(name, added, removed);
  784. objects.Add(obj);
  785. }
  786. else
  787. {
  788. if (added)
  789. obj.Added = added;
  790. if (removed)
  791. obj.Removed = removed;
  792. }
  793. }
  794. static public QspAct GetAct(string name)
  795. {
  796. foreach (QspAct act in acts)
  797. {
  798. if (act.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
  799. return act;
  800. }
  801. return null;
  802. }
  803. static public void AddAct(string name, bool added, bool removed)
  804. {
  805. QspAct act = GetAct(name);
  806. if (act == null)
  807. {
  808. act = new QspAct(name, added, removed);
  809. acts.Add(act);
  810. }
  811. else
  812. {
  813. if (added)
  814. act.Added = added;
  815. if (removed)
  816. act.Removed = removed;
  817. }
  818. }
  819. static public void SetConfig(List<string> callerVars, List<string> systemVars, bool EnableCurlyParsing)
  820. {
  821. callerVariables = callerVars;
  822. systemVariables = systemVars;
  823. curlyParsing = EnableCurlyParsing;
  824. }
  825. static public bool IsNumber(String s)
  826. {
  827. foreach (char c in s.ToCharArray())
  828. {
  829. if (Array.IndexOf(Digits, c) == INVALID_INDEX)
  830. return false;
  831. }
  832. return true;
  833. }
  834. static public bool ListContainsIgnoreCase(List<string> stringList, string value)
  835. {
  836. bool contains = null != stringList.Find(delegate(string str)
  837. {
  838. return str.Equals(value, StringComparison.OrdinalIgnoreCase);
  839. });
  840. return contains;
  841. }
  842. static public string GenerateCsvName(string FileName)
  843. {
  844. string result = FileName;
  845. if (result.EndsWith(".txt"))
  846. result = result.Substring(0, result.Length - 4);
  847. result = result + ".csv";
  848. return result;
  849. }
  850. static public bool ExportToCsv(string srcFile, string delimiter)
  851. {
  852. //Выгружаем исходный код в CSV
  853. //4 колонки: номер+локация, оригинал, место для перевода, место для комментария
  854. string dstFile = GenerateCsvName(srcFile);
  855. StreamReader fi = null;
  856. if (!OpenStreamForReading(ref fi, srcFile))
  857. {
  858. return false;
  859. }
  860. StreamWriter fo = null;
  861. if (!OpenStreamForWriting(ref fo, dstFile))
  862. {
  863. fi.Close();
  864. return false;
  865. }
  866. string line;
  867. int line_counter = 0;
  868. int quoted_line_counter = 0;
  869. bool inside = false;
  870. string locName = null;
  871. int quote = (int)QuoteType.None;
  872. string quotedText = "";
  873. bool quotedTextCompleted = false;
  874. while ((line = fi.ReadLine()) != null)
  875. {
  876. line_counter++;
  877. line = line.Trim(WhiteSpace);
  878. if ((line.Length > 0) && (line[0] == '#'))
  879. {
  880. //имя локации
  881. locName = line.Substring(1).Trim(WhiteSpace);
  882. inside = true;
  883. }
  884. else if ((line.Length > 38) && line.StartsWith("--- ") && line.EndsWith(" ---------------------------------"))
  885. {
  886. inside = false;
  887. locName = null;
  888. }
  889. else if (inside)
  890. {
  891. //Разбираем только строки внутри локаций
  892. int pos = 0;
  893. //Разбор строки
  894. while (pos < line.Length)
  895. {
  896. char c = line[pos];
  897. if (c == '\'')
  898. {
  899. //Апостроф
  900. if (quote == (int)QuoteType.None)
  901. {
  902. quote = (int)QuoteType.Single;
  903. quotedText = "";
  904. }
  905. else if (quote == (int)QuoteType.Single)
  906. {
  907. if ((pos + 1 < line.Length) && (line[pos + 1] == '\''))
  908. {
  909. //Экранированный апостроф
  910. pos++;
  911. quotedText += c;
  912. }
  913. else
  914. {
  915. quote = (int)QuoteType.None;
  916. quotedTextCompleted = true;
  917. }
  918. }
  919. else
  920. {
  921. quotedText += c;
  922. }
  923. }
  924. else if (c == '"')
  925. {
  926. //Кавычка
  927. if (quote == (int)QuoteType.None)
  928. {
  929. quote = (int)QuoteType.Double;
  930. quotedText = "";
  931. }
  932. else if (quote == (int)QuoteType.Double)
  933. {
  934. if ((pos + 1 < line.Length) && (line[pos + 1] == '"'))
  935. {
  936. //Экранированная кавычка
  937. pos++;
  938. quotedText += c;
  939. }
  940. else
  941. {
  942. quote = (int)QuoteType.None;
  943. quotedTextCompleted = true;
  944. }
  945. }
  946. else
  947. {
  948. quotedText += c;
  949. }
  950. }
  951. else if (quote != (int)QuoteType.None)
  952. {
  953. //Строка текста внутри кавычек или апострофов
  954. quotedText += c;
  955. }
  956. else if (c == '{')
  957. {
  958. //Пропускаем содержимое фигурных скобок
  959. int curlyLevel = 1;
  960. pos++;
  961. int curlyQuote = (int)QuoteType.None;
  962. while (pos < line.Length)
  963. {
  964. char c2 = line[pos];
  965. if (c2 == '\'')
  966. {
  967. //Апостроф
  968. if (curlyQuote == (int)QuoteType.None)
  969. {
  970. curlyQuote = (int)QuoteType.Single;
  971. }
  972. else if (curlyQuote == (int)QuoteType.Single)
  973. {
  974. if ((pos + 1 < line.Length) && (line[pos + 1] == '\''))
  975. {
  976. //Экранированный апостроф
  977. pos++;
  978. }
  979. else
  980. {
  981. curlyQuote = (int)QuoteType.None;
  982. }
  983. }
  984. }
  985. else if (c2 == '"')
  986. {
  987. //Кавычка
  988. if (curlyQuote == (int)QuoteType.None)
  989. {
  990. curlyQuote = (int)QuoteType.Double;
  991. }
  992. else if (curlyQuote == (int)QuoteType.Double)
  993. {
  994. if ((pos + 1 < line.Length) && (line[pos + 1] == '"'))
  995. {
  996. //Экранированная кавычка
  997. pos++;
  998. }
  999. else
  1000. {
  1001. curlyQuote = (int)QuoteType.None;
  1002. }
  1003. }
  1004. }
  1005. else if (curlyQuote == (int)QuoteType.None)
  1006. {
  1007. if (c2 == '{')
  1008. {
  1009. curlyLevel++;
  1010. }
  1011. else if (c2 == '}')
  1012. {
  1013. curlyLevel--;
  1014. if (curlyLevel == 0)
  1015. break;
  1016. }
  1017. }
  1018. pos++;
  1019. }
  1020. }
  1021. if (quotedTextCompleted)
  1022. {
  1023. //Формируем строку для csv-файла
  1024. //4 колонки: номер+локация, оригинал, место для перевода, место для комментария
  1025. if (quotedText.Trim(WhiteSpace).Length > 0)
  1026. {
  1027. quoted_line_counter++;
  1028. string csv_line = "";
  1029. csv_line += CsvEscape(quoted_line_counter.ToString() + ":" + locName) + delimiter;
  1030. csv_line += CsvEscape(quotedText) + delimiter;
  1031. csv_line += CsvEscape("") + delimiter;
  1032. csv_line += CsvEscape("");
  1033. //Записываем строку в csv-файл
  1034. fo.WriteLine(csv_line);
  1035. }
  1036. quotedTextCompleted = false;
  1037. }
  1038. pos++;
  1039. }
  1040. }
  1041. }
  1042. fi.Close();
  1043. fo.Close();
  1044. return true;
  1045. }
  1046. static public string CsvEscape(string src)
  1047. {
  1048. //Форматируем строку для записи в csv-файл
  1049. string result = "\"" + src.Replace("\"", "\"\"") + "\"";
  1050. return result;
  1051. }
  1052. static public string CsvUnEscape(string src)
  1053. {
  1054. //Преобразовываем строку из csv-формата в обычный текст
  1055. string result = src;
  1056. if (src.StartsWith("\""))
  1057. {
  1058. result = result.Substring(1, result.Length - 2);
  1059. result = result.Replace("\"\"", "\"");
  1060. }
  1061. return result;
  1062. }
  1063. static public bool TranslateFromCsv(string srcFile, string suffix, bool IgnoreEmptyTranslations, string delimiter)
  1064. {
  1065. //Создаем перевод на основе загруженного файла и CSV
  1066. //Проверяем соответствие номера, локации, оригинального текста, в случае несовпадения выдаем ошибку и не сохраняем файл
  1067. //Для пустых строк заполняем текстом оригинала, либо выдаем ошибку - в зависимости от настроек.
  1068. string dstFile = srcFile;
  1069. if (dstFile.EndsWith(".txt"))
  1070. dstFile = dstFile.Substring(0, dstFile.Length - 4);
  1071. dstFile = dstFile + suffix + ".txt";
  1072. string csvFile = GenerateCsvName(srcFile);
  1073. StreamReader fi_src = null;
  1074. if (!OpenStreamForReading(ref fi_src, srcFile))
  1075. {
  1076. return false;
  1077. }
  1078. StreamReader fi_csv = null;
  1079. if (!OpenStreamForReading(ref fi_csv, csvFile))
  1080. {
  1081. fi_src.Close();
  1082. return false;
  1083. }
  1084. StreamWriter fo = null;
  1085. if (!OpenStreamForWriting(ref fo, dstFile))
  1086. {
  1087. fi_src.Close();
  1088. fi_csv.Close();
  1089. return false;
  1090. }
  1091. string line;
  1092. string line_cut;
  1093. int line_counter = 0;
  1094. int quoted_line_counter = 0;
  1095. bool inside = false;
  1096. string locName = null;
  1097. int quote = (int)QuoteType.None;
  1098. string quotedText = "";
  1099. bool quotedTextCompleted = false;
  1100. string rawText = "";
  1101. bool aborted = false;
  1102. while ((line = fi_src.ReadLine()) != null)
  1103. {
  1104. line_counter++;
  1105. line_cut = line.Trim(WhiteSpace);
  1106. if ((line_cut.Length > 0) && (line_cut[0] == '#'))
  1107. {
  1108. //имя локации
  1109. locName = line_cut.Substring(1).Trim(WhiteSpace);
  1110. inside = true;
  1111. fo.WriteLine(line);
  1112. }
  1113. else if ((line_cut.Length > 38) && line_cut.StartsWith("--- ") && line_cut.EndsWith(" ---------------------------------"))
  1114. {
  1115. inside = false;
  1116. locName = null;
  1117. fo.WriteLine(line);
  1118. }
  1119. else if (inside)
  1120. {
  1121. //Разбираем только строки внутри локаций
  1122. int pos = 0;
  1123. //Разбор строки
  1124. while (pos < line.Length)
  1125. {
  1126. char c = line[pos];
  1127. if (c == '\'')
  1128. {
  1129. //Апостроф
  1130. if (quote == (int)QuoteType.None)
  1131. {
  1132. quote = (int)QuoteType.Single;
  1133. quotedText = "";
  1134. rawText += c;
  1135. }
  1136. else if (quote == (int)QuoteType.Single)
  1137. {
  1138. if ((pos + 1 < line.Length) && (line[pos + 1] == '\''))
  1139. {
  1140. //Экранированный апостроф
  1141. pos++;
  1142. quotedText += c;
  1143. }
  1144. else
  1145. {
  1146. quote = (int)QuoteType.None;
  1147. quotedTextCompleted = true;
  1148. }
  1149. }
  1150. else
  1151. {
  1152. quotedText += c;
  1153. }
  1154. }
  1155. else if (c == '"')
  1156. {
  1157. //Кавычка
  1158. if (quote == (int)QuoteType.None)
  1159. {
  1160. quote = (int)QuoteType.Double;
  1161. quotedText = "";
  1162. rawText += c;
  1163. }
  1164. else if (quote == (int)QuoteType.Double)
  1165. {
  1166. if ((pos + 1 < line.Length) && (line[pos + 1] == '"'))
  1167. {
  1168. //Экранированная кавычка
  1169. pos++;
  1170. quotedText += c;
  1171. }
  1172. else
  1173. {
  1174. quote = (int)QuoteType.None;
  1175. quotedTextCompleted = true;
  1176. }
  1177. }
  1178. else
  1179. {
  1180. quotedText += c;
  1181. }
  1182. }
  1183. else if (quote != (int)QuoteType.None)
  1184. {
  1185. //Строка текста внутри кавычек или апострофов
  1186. quotedText += c;
  1187. }
  1188. else if (c == '{')
  1189. {
  1190. //Пропускаем содержимое фигурных скобок
  1191. int curlyLevel = 1;
  1192. rawText += c;
  1193. pos++;
  1194. int curlyQuote = (int)QuoteType.None;
  1195. while (pos < line.Length)
  1196. {
  1197. char c2 = line[pos];
  1198. rawText += c2;
  1199. if (c2 == '\'')
  1200. {
  1201. //Апостроф
  1202. if (curlyQuote == (int)QuoteType.None)
  1203. {
  1204. curlyQuote = (int)QuoteType.Single;
  1205. }
  1206. else if (curlyQuote == (int)QuoteType.Single)
  1207. {
  1208. if ((pos + 1 < line.Length) && (line[pos + 1] == '\''))
  1209. {
  1210. //Экранированный апостроф
  1211. rawText += line[pos + 1];
  1212. pos++;
  1213. }
  1214. else
  1215. {
  1216. curlyQuote = (int)QuoteType.None;
  1217. }
  1218. }
  1219. }
  1220. else if (c2 == '"')
  1221. {
  1222. //Кавычка
  1223. if (curlyQuote == (int)QuoteType.None)
  1224. {
  1225. curlyQuote = (int)QuoteType.Double;
  1226. }
  1227. else if (curlyQuote == (int)QuoteType.Double)
  1228. {
  1229. if ((pos + 1 < line.Length) && (line[pos + 1] == '"'))
  1230. {
  1231. //Экранированная кавычка
  1232. rawText += line[pos + 1];
  1233. pos++;
  1234. }
  1235. else
  1236. {
  1237. curlyQuote = (int)QuoteType.None;
  1238. }
  1239. }
  1240. }
  1241. else if (curlyQuote == (int)QuoteType.None)
  1242. {
  1243. if (c2 == '{')
  1244. {
  1245. curlyLevel++;
  1246. }
  1247. else if (c2 == '}')
  1248. {
  1249. curlyLevel--;
  1250. if (curlyLevel == 0)
  1251. break;
  1252. }
  1253. }
  1254. pos++;
  1255. }
  1256. }
  1257. else
  1258. {
  1259. rawText += c;
  1260. }
  1261. if (quotedTextCompleted)
  1262. {
  1263. //Читаем строку из файла перевода
  1264. string translation = quotedText;
  1265. if (quotedText.Trim(WhiteSpace).Length > 0)
  1266. {
  1267. translation = "";
  1268. quoted_line_counter++;
  1269. bool csvLineCompleted = false;
  1270. string csv_line = null;
  1271. int valuesCount = 0;
  1272. string thirdValue = "";
  1273. int csvQuote = (int)QuoteType.None;
  1274. string csvText = "";
  1275. bool csvValueCompleted = false;
  1276. int csvLineIndex = INVALID_INDEX;
  1277. string csvLocName = "";
  1278. string csvOriginalLine = "";
  1279. while (!csvLineCompleted && ((csv_line = fi_csv.ReadLine()) != null))
  1280. {
  1281. int csvPos = 0;
  1282. //Разбор строки
  1283. while (csvPos < csv_line.Length)
  1284. {
  1285. int newValuesCount = valuesCount;
  1286. char c3 = csv_line[csvPos];
  1287. if (c3 == '"')
  1288. {
  1289. csvText += c3;
  1290. //Кавычка
  1291. if (csvQuote == (int)QuoteType.None)
  1292. {
  1293. csvQuote = (int)QuoteType.Double;
  1294. }
  1295. else if (csvQuote == (int)QuoteType.Double)
  1296. {
  1297. if ((csvPos + 1 < csv_line.Length) && (csv_line[csvPos + 1] == '"'))
  1298. {
  1299. //Экранированная кавычка
  1300. csvText += csv_line[csvPos + 1];
  1301. csvPos++;
  1302. }
  1303. else
  1304. {
  1305. csvQuote = (int)QuoteType.None;
  1306. }
  1307. }
  1308. }
  1309. else if (csvQuote != (int)QuoteType.None)
  1310. {
  1311. //Строка текста внутри кавычек
  1312. csvText += c3;
  1313. }
  1314. else if (c3.ToString() == delimiter)
  1315. {
  1316. newValuesCount++;
  1317. csvValueCompleted = true;
  1318. }
  1319. else
  1320. {
  1321. csvText += c3;
  1322. }
  1323. if ((csvQuote == (int)QuoteType.None) && (csvPos == csv_line.Length - 1))
  1324. {
  1325. newValuesCount++;
  1326. csvValueCompleted = true;
  1327. }
  1328. if (csvValueCompleted)
  1329. {
  1330. for (int iter = valuesCount + 1; iter <= newValuesCount; iter++)
  1331. {
  1332. csvText = CsvUnEscape(csvText.Trim(WhiteSpace));
  1333. if (iter == 1) //Первая колонка - имя локации и номер строки
  1334. {
  1335. string[] csvFirst = csvText.Split(":".ToCharArray());
  1336. if (csvFirst.Length == 2)
  1337. {
  1338. csvLineIndex = Convert.ToInt32(csvFirst[0]);
  1339. csvLocName = csvFirst[1];
  1340. }
  1341. }
  1342. else if (iter == 2) //Вторая колонка - оригинал
  1343. {
  1344. csvOriginalLine = csvText;
  1345. }
  1346. else if (iter == 3) //Третья колонка - перевод
  1347. {
  1348. thirdValue = csvText;
  1349. }
  1350. csvText = "";
  1351. }
  1352. csvValueCompleted = false;
  1353. valuesCount = newValuesCount;
  1354. }
  1355. csvPos++;
  1356. }
  1357. if (csvQuote == (int)QuoteType.None)
  1358. {
  1359. csvLineCompleted = true;
  1360. }
  1361. else
  1362. {
  1363. csvText += Environment.NewLine;
  1364. }
  1365. }
  1366. if ((csv_line == null) || (csvLineCompleted &&
  1367. ((csvLocName != locName) || (quoted_line_counter != csvLineIndex) || (csvOriginalLine != quotedText))))
  1368. {
  1369. SubmitError("CSV-file does not match the original! Generate it again.", INVALID_INDEX);
  1370. aborted = true;
  1371. break;
  1372. }
  1373. if (!csvLineCompleted)
  1374. {
  1375. SubmitError("CSV-file damaged! Line number: " + quoted_line_counter.ToString(), INVALID_INDEX);
  1376. aborted = true;
  1377. break;
  1378. }
  1379. translation = thirdValue;
  1380. if (translation.Length == 0)
  1381. {
  1382. if (IgnoreEmptyTranslations)
  1383. {
  1384. translation = quotedText;
  1385. }
  1386. else
  1387. {
  1388. SubmitError("Line number " + quoted_line_counter + " is not converted!", INVALID_INDEX);
  1389. aborted = true;
  1390. break;
  1391. }
  1392. }
  1393. }
  1394. rawText += translation;
  1395. rawText += c;
  1396. quotedTextCompleted = false;
  1397. }
  1398. pos++;
  1399. }
  1400. if (aborted)
  1401. break;
  1402. if (quote == (int)QuoteType.None)
  1403. {
  1404. //Записываем строку в файл перевода
  1405. fo.WriteLine(rawText);
  1406. rawText = "";
  1407. }
  1408. }
  1409. else
  1410. {
  1411. fo.WriteLine(line);
  1412. }
  1413. }
  1414. fi_src.Close();
  1415. fi_csv.Close();
  1416. fo.Close();
  1417. if (aborted)
  1418. {
  1419. //Убить корявый перевод
  1420. File.Delete(dstFile);
  1421. }
  1422. return !aborted;
  1423. }
  1424. static public bool OpenStreamForReading(ref StreamReader fi, string file)
  1425. {
  1426. bool result = false;
  1427. try
  1428. {
  1429. fi = new StreamReader(file, Encoding.Default);
  1430. result = true;
  1431. }
  1432. catch (Exception e)
  1433. {
  1434. SubmitError("File \"" + file + "\" cannot be opened for reading (it can be opened by another program).", INVALID_INDEX);
  1435. result = false;
  1436. }
  1437. return result;
  1438. }
  1439. static public bool OpenStreamForWriting(ref StreamWriter fo, string file)
  1440. {
  1441. bool result = false;
  1442. try
  1443. {
  1444. fo = new StreamWriter(file, false, Encoding.Default);
  1445. result = true;
  1446. }
  1447. catch (Exception e)
  1448. {
  1449. SubmitError("File \"" + file + "\" cannot be opened for writing (it can be opened by another program).", INVALID_INDEX);
  1450. result = false;
  1451. }
  1452. return result;
  1453. }
  1454. }
  1455. }