PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/task/RunEmulatorDebugTask.cs

https://github.com/moscrif/ide
C# | 642 lines | 424 code | 150 blank | 68 comment | 80 complexity | 3a7b6a8cd43fe3304f131f2113070a91 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5. using System.Text.RegularExpressions;
  6. using Moscrif.IDE.Workspace;
  7. using System.Diagnostics;
  8. using Moscrif.IDE.Extensions;
  9. namespace Moscrif.IDE.Task
  10. {
  11. public class RunEmulatorDebugTask : ITask
  12. {
  13. List<TaskMessage> output = new List<TaskMessage>();
  14. StateEnum stateTask = StateEnum.OK;
  15. public RunEmulatorDebugTask()
  16. {
  17. }
  18. #region ITask implementation
  19. public void Initialize (object dataObject)
  20. {
  21. }
  22. public bool ExecuteTask ()
  23. {
  24. Tool.Logger.LogDebugInfo("RUN EMULATOR - Initialize",null);
  25. if (MainClass.Settings.ClearConsoleBeforRuning){
  26. MainClass.MainWindow.OutputConsole.Clear();
  27. Tool.Logger.LogDebugInfo("Clear console",null);
  28. }
  29. if(MainClass.MainWindow.RunningEmulator){
  30. output.Add( new TaskMessage(MainClass.Languages.Translate("emulator_is_running"),null,null));
  31. stateTask = StateEnum.ERROR;
  32. Tool.Logger.Error(MainClass.Languages.Translate("emulator_is_running"),null);
  33. return false;
  34. }
  35. if(String.IsNullOrEmpty(MainClass.Workspace.ActualResolution)){
  36. output.Add(new TaskMessage(MainClass.Languages.Translate("pleas_select_resolution"),null,null));
  37. Tool.Logger.Error(MainClass.Languages.Translate("pleas_select_resolution"),null);
  38. stateTask = StateEnum.ERROR;
  39. return false;
  40. }
  41. if (!File.Exists(MainClass.Workspace.ActualResolution)){
  42. output.Add(new TaskMessage(MainClass.Languages.Translate("resolution_file_not_exist",MainClass.Workspace.ActualResolution),null,null));
  43. Tool.Logger.Error(MainClass.Languages.Translate("resolution_file_not_exist",MainClass.Workspace.ActualResolution),null);
  44. stateTask = StateEnum.ERROR;
  45. return false;
  46. }
  47. if(MainClass.Workspace.ActualProject == null){
  48. output.Add(new TaskMessage(MainClass.Languages.Translate("no_project_selected"),null,null));
  49. Tool.Logger.Error(MainClass.Languages.Translate("no_project_selected"),null);
  50. stateTask = StateEnum.ERROR;
  51. return false;
  52. }
  53. string newPath =System.IO.Path.Combine(MainClass.Settings.EmulatorDirectory,"generic.ini");
  54. try{
  55. if (File.Exists(newPath)){
  56. Tool.Logger.LogDebugInfo(String.Format("Delete File ->{0}",newPath),null);
  57. File.Delete(newPath);
  58. }
  59. File.Copy(MainClass.Workspace.ActualResolution,newPath);
  60. Tool.Logger.LogDebugInfo(String.Format("File Copy {0} to {1}",MainClass.Workspace.ActualResolution,newPath),null);
  61. int selectOs = MainClass.Workspace.ActualDevice;
  62. Devices.Device d = MainClass.Workspace.ActualProject.DevicesSettings.Find(x=>x.TargetPlatformId == selectOs);
  63. if(d != null){
  64. Tool.Logger.LogDebugInfo(String.Format("Device ->{0}",d.Devicetype),null);
  65. string font = "";
  66. if(d.Includes.Fonts != null && d.Includes.Fonts.Length>0){
  67. font =string.Join(" ",d.Includes.Fonts);
  68. }
  69. using (StreamWriter stream = File.AppendText(newPath)) {
  70. stream.WriteLine();
  71. string fonts = font;
  72. stream.WriteLine("[emulation]");
  73. stream.WriteLine("fonts = " + fonts);
  74. if(d.Includes.Skin != null){
  75. stream.WriteLine("skin = "+ d.Includes.Skin.Name);
  76. stream.WriteLine("theme = " + d.Includes.Skin.Theme);
  77. }else {
  78. stream.WriteLine("skin = ");
  79. stream.WriteLine("theme = ");
  80. }
  81. stream.Flush();
  82. stream.Close();
  83. stream.Dispose();
  84. }
  85. }
  86. }catch (Exception ex){
  87. //output.Add("Create generic.ini failed!");
  88. output.Add(new TaskMessage(ex.Message));
  89. stateTask = StateEnum.ERROR;
  90. Tool.Logger.Error(ex.Message,null);
  91. return false;
  92. }
  93. string cmd = Path.Combine(MainClass.Settings.EmulatorDirectory, "moscrif.exe");
  94. if(MainClass.Platform.IsMac){
  95. string file = System.IO.Path.Combine( MainClass.Settings.EmulatorDirectory, "Moscrif.app");//.app
  96. if(!System.IO.Directory.Exists(file) ){
  97. output.Add(new TaskMessage(MainClass.Languages.Translate("emulator_not_found")));
  98. stateTask = StateEnum.ERROR;
  99. Tool.Logger.Error(MainClass.Languages.Translate("emulator_not_found"),null);
  100. return false;
  101. }
  102. file = System.IO.Path.Combine(file, "Contents");
  103. file = System.IO.Path.Combine(file, "MacOS");
  104. file = System.IO.Path.Combine(file, "Moscrif");
  105. cmd = file;
  106. Tool.Logger.LogDebugInfo(String.Format("command MAC ->{0}",cmd),null);
  107. if(!System.IO.File.Exists(file) ){
  108. output.Add(new TaskMessage(MainClass.Languages.Translate("emulator_not_found")));
  109. stateTask = StateEnum.ERROR;
  110. Tool.Logger.Error(MainClass.Languages.Translate("emulator_not_found"),null);
  111. return false;
  112. }
  113. } else {
  114. Tool.Logger.LogDebugInfo(String.Format("command WIN ->{0}",cmd),null);
  115. if(!System.IO.File.Exists(cmd) ){
  116. output.Add(new TaskMessage(MainClass.Languages.Translate("emulator_not_found")));
  117. stateTask = StateEnum.ERROR;
  118. Tool.Logger.Error(MainClass.Languages.Translate("emulator_not_found"),null);
  119. return false;
  120. }
  121. }
  122. AppFile appFile = MainClass.Workspace.ActualProject.AppFile;
  123. string projDir = Path.GetDirectoryName(appFile.ApplicationFile);
  124. if (!projDir.EndsWith(Path.DirectorySeparatorChar.ToString())) projDir += Path.DirectorySeparatorChar;
  125. string args = String.Format("/f {0} /d {1} /o console /t debug", Path.GetFileName(appFile.ApplicationFile), projDir);
  126. if(MainClass.Platform.IsMac){
  127. args = String.Format(" -f {0} -d {1} -o console -t debug", Path.GetFileName(appFile.ApplicationFile), projDir);
  128. Process []pArry = Process.GetProcesses();
  129. foreach(Process p in pArry)
  130. {
  131. if(p != null){
  132. try {
  133. string s = p.ProcessName;
  134. s = s.ToLower();
  135. if (s.CompareTo("moscrif") ==0){
  136. Tool.Logger.LogDebugInfo("Kill Emulator Mac",null);
  137. p.Kill();
  138. MainClass.MainWindow.RunningEmulator= false;
  139. }
  140. //string s = p.ProcessName;
  141. //s = s.ToLower();
  142. //Console.WriteLine("\t"+s);
  143. } catch {//(Exception ex){
  144. //Console.WriteLine(ex.Message);
  145. //Tool.Logger.Error(ex.Message,null);
  146. }
  147. }
  148. }
  149. Tool.Logger.LogDebugInfo(String.Format("args MAC ->{0} ",args),null);
  150. } else {
  151. if(MainClass.Platform.IsWindows){
  152. Process []pArry = Process.GetProcesses();
  153. foreach(Process p in pArry)
  154. {
  155. try{
  156. string s = p.ProcessName;
  157. s = s.ToLower();
  158. if (s.CompareTo("moscrif") ==0){
  159. Tool.Logger.LogDebugInfo("Kill Emulator win",null);
  160. p.Kill();
  161. MainClass.MainWindow.RunningEmulator= false;
  162. }
  163. } catch{
  164. //Console.WriteLine(ex.Message);
  165. //Tool.Logger.Error(ex.Message,null);;
  166. }
  167. }
  168. }
  169. Tool.Logger.LogDebugInfo(String.Format("args WIN ->{0} ",args),null);
  170. }
  171. try{
  172. Tool.Logger.LogDebugInfo("RUN EMULATOR -Start",null);
  173. MainClass.MainWindow.RunEmulator(cmd, args, MainClass.Settings.EmulatorDirectory,ProcessOutputChange);
  174. }catch (Exception ex){
  175. output.Add(new TaskMessage(ex.Message));
  176. stateTask = StateEnum.ERROR;
  177. Tool.Logger.Error(ex.Message,null);
  178. return false;
  179. }
  180. return true;
  181. }
  182. void ProcessOutputChange(object sender, string message)
  183. {
  184. //Gtk.Application.Invoke(delegate
  185. //{
  186. //this.output.Add( new TaskMessage(">> " + message.Trim() + "<<"));
  187. if(!message.Contains("Log-M:") && !message.Contains("Log-GC:") )
  188. MainClass.MainWindow.OutputConsole.WriteText(message);
  189. //return;
  190. /*string msg = message.Trim();
  191. if (String.IsNullOrEmpty(msg))
  192. return;*/
  193. /*string fileDitr = "";
  194. if (sender.GetType() == typeof(ProcessWrapper)) {
  195. ProcessWrapper pw = (ProcessWrapper)sender;
  196. if (pw.StartInfo != null) {
  197. fileDitr = pw.StartInfo.WorkingDirectory;
  198. }
  199. //(sender as ProcessWrapper).
  200. }*/
  201. ParseOutput(message);
  202. //Console.WriteLine(">> " + message.Trim() + "<<");
  203. //});
  204. }
  205. public void OnEndTaskWrite(object sender, string name, string status, List<TaskMessage> errors){
  206. if(EndTaskWrite!= null)
  207. EndTaskWrite(sender, name,status, errors);
  208. }
  209. // not use
  210. public void OnTaskOutputChanged(object sender, string name, string status, List<TaskMessage> errors){
  211. if(TaskOutputChanged!= null)
  212. TaskOutputChanged(sender, name,status, errors);
  213. }
  214. public event ProcessTaskHandler TaskOutputChanged;
  215. public event ProcessErrorHandler ErrorWrite;
  216. public event ProcessErrorHandler LogWrite;
  217. public event ProcessTaskHandler EndTaskWrite;
  218. public event ProcessErrorHandler LogMonitor;
  219. public event ProcessErrorHandler LogGarbageCollector;
  220. void ITask.StopTask()
  221. {
  222. }
  223. private string messageError;
  224. private bool isLog; // novy text
  225. private bool isOldLog; // predchadzajuci text
  226. private void ParseOutput(string message)
  227. {
  228. //string[] stringSeparators = new string[] {"\r\n\r\n"};
  229. //string[] stringSeparators = new string[] {"\n"};//\n\n
  230. //string[] messageSeparators = new string[] {"\n\n"};
  231. string endMessage ="\n\n";
  232. message = message.Replace("\r\n","\n");
  233. message = message.Replace("\n\r","\n");
  234. message = message.Replace("\r\r","\n");
  235. message = message.Replace("\r","\n");
  236. IEnumerable<string> allLine = message.SplitAndKeep ("\n");
  237. //string[] allMsg = message.Split(messageSeparators,StringSplitOptions.RemoveEmptyEntries);
  238. //string temp = message.Replace("\r","*");
  239. //temp = temp.Replace("\n","»");
  240. //Console.WriteLine("temp ->{0}",temp);
  241. /*foreach (string a in allMsg)
  242. Console.WriteLine("------>"+a);*/
  243. foreach (string a in allLine){
  244. Console.WriteLine("->>"+a+"<<-");
  245. //if(string.IsNullOrEmpty(a.Trim()))
  246. // continue;
  247. message = a;
  248. //Console.WriteLine("------>"+a);
  249. //MatchCollection match = Regex.Matches( dataBlock, @"(ERROR:.*?\t.*?\t.*?\t)", RegexOptions.Singleline );
  250. int indx = message.IndexOf("Error:"); // start erroru
  251. //Console.WriteLine("indx ->"+indx);
  252. if (indx >-1 ) isLog = false;
  253. if (indx <0){
  254. indx = message.IndexOf("Log-I:");
  255. if (indx >-1 ) isLog = true;
  256. }
  257. if (indx <0){
  258. indx = message.IndexOf("Log-E:");
  259. if (indx >-1 ) isLog = true;
  260. }
  261. if (indx <0){
  262. indx = message.IndexOf("Log-W:");
  263. if (indx >-1 ) isLog = true;
  264. }
  265. if (indx <0){
  266. indx = message.IndexOf("Log-M:");
  267. if (indx >-1 ) isLog = true;
  268. }
  269. if (indx <0){
  270. indx = message.IndexOf("Log-GC:");
  271. if (indx >-1 ) isLog = true;
  272. }
  273. if (indx > -1) {
  274. if (!String.IsNullOrEmpty(messageError)){
  275. if(isOldLog)
  276. GetLog(messageError);
  277. else GetError(messageError);
  278. messageError = "";
  279. }
  280. if(!message.Contains(endMessage)){
  281. isOldLog = isLog;
  282. messageError = messageError +message;
  283. continue; // neukonceny error
  284. }
  285. messageError = message; //.Remove(0, indx + 6);
  286. if (indx>0) messageError = messageError.Remove(0,indx);
  287. if(isLog)
  288. GetLog(messageError);
  289. else GetError(messageError);
  290. messageError = "";
  291. continue;
  292. }else {
  293. messageError = messageError +message;
  294. if(!messageError.Contains(endMessage)){
  295. continue; // neukonceny error
  296. }
  297. indx = messageError.IndexOf("Error:");
  298. if (indx <0){
  299. indx = messageError.IndexOf("Log-I:");
  300. }
  301. if (indx <0){
  302. indx = messageError.IndexOf("Log-E:");
  303. }
  304. if (indx <0){
  305. indx = messageError.IndexOf("Log-W:");
  306. }
  307. if (indx <0){
  308. indx = messageError.IndexOf("Log-M:");
  309. }
  310. if (indx <0){
  311. indx = messageError.IndexOf("Log-GC:");
  312. }
  313. if (indx>0) messageError = messageError.Remove(0,indx);
  314. if(isOldLog)
  315. GetLog(messageError);
  316. else GetError(messageError);
  317. messageError = "";
  318. }
  319. }
  320. }
  321. private TaskMessage GetError(string message){
  322. TaskMessage tm =new TaskMessage();
  323. if(String.IsNullOrEmpty(message)) return null;
  324. try{
  325. //Console.WriteLine("message_1->"+message+"<-");
  326. //Log-I: Log-W: Log-E:
  327. message= message.Replace("Error:","" );
  328. message =message.Replace("\t","»");
  329. message =message.Replace("\n\r","");
  330. message =message.Replace("\r\n","");
  331. message =message.Replace("\n","");
  332. message =message.Replace("\r","");
  333. //Console.WriteLine("messageError -> "+ message);
  334. //Console.WriteLine("message_1->"+message+"<-");
  335. string[] msg = message.Split('»');
  336. //Console.WriteLine("match.Count -> "+ msg.Length);
  337. if (msg.Length <3){
  338. //Tool.Logger.Error("message ->"+message+"<");
  339. return null;
  340. }
  341. /*foreach(string m in msg){
  342. Console.WriteLine(m);
  343. }*/
  344. //Console.WriteLine("msg.Length ->"+msg.Length);
  345. string error = msg[0];
  346. string filename = msg[1];
  347. string line =msg[2];
  348. if (msg[1].StartsWith("at") ){
  349. if (msg.Length <4){
  350. //Tool.Logger.Error("message ->"+message+"<");
  351. return null;
  352. }
  353. filename = msg[2];
  354. line =msg[3];
  355. //int indx = 4; // spracovanie dalsich vetiev (kazda zacina na at)
  356. if (msg.Length >4)
  357. tm.Child = GetChild(msg,4);
  358. }
  359. else{
  360. filename = msg[1];
  361. line =msg[2];
  362. }
  363. filename = filename.Replace('/',System.IO.Path.DirectorySeparatorChar);
  364. //Console.WriteLine("error -> "+ error);
  365. //Console.WriteLine("filename -> "+ filename);
  366. //Console.WriteLine("line -> "+ line);
  367. //tm =new TaskMessage(error, filename, line);
  368. tm.Message = error;
  369. tm.Line = line;
  370. tm.File = filename;
  371. this.output.Add(tm);
  372. this.stateTask = StateEnum.ERROR;
  373. messageError = null;
  374. if (ErrorWrite!=null){
  375. ErrorWrite(this,this.Name,this.StateTask.ToString(),tm);
  376. }
  377. } catch {
  378. }
  379. return tm;
  380. }
  381. private TaskMessage GetChild(string[] msg, int indx){
  382. TaskMessage tmChild =new TaskMessage();
  383. if (msg[indx].StartsWith("at")){
  384. indx++;
  385. if (indx >= msg.Length) return null;
  386. string fname = msg[indx].Replace('/',System.IO.Path.DirectorySeparatorChar);
  387. tmChild.File = fname;
  388. indx ++;
  389. if (indx < msg.Length) { // line tam byt nemusi
  390. if (!msg[indx].StartsWith("at"))
  391. tmChild.Line = msg[indx];
  392. else indx--; // ak tam line nieje (zacina na at) vratim index vratim index naspet
  393. indx++;
  394. if (indx >= msg.Length) return tmChild;
  395. tmChild.Child =GetChild(msg,indx);
  396. }
  397. }
  398. return tmChild;
  399. }
  400. private TaskMessage GetLog(string message){
  401. string originalMessage = message;
  402. TaskMessage tm =new TaskMessage();
  403. try{
  404. //Log-I: Log-W: Log-E:
  405. message= message.Replace("LOG-I","" );
  406. message= message.Replace("LOG-W","" );
  407. message= message.Replace("LOG-E","" );
  408. message= message.Replace("LOG-M","" );
  409. message= message.Replace("LOG-GC","" );
  410. message =message.Replace("\t","»");
  411. message =message.Replace("\n\r","");
  412. message =message.Replace("\r\n","");
  413. message =message.Replace("\n","");
  414. message =message.Replace("\r","");
  415. if(message.Contains("Log-GC:")){
  416. Console.WriteLine("Log-GC: 1");
  417. tm =new TaskMessage(originalMessage, "","");
  418. if (LogGarbageCollector!=null){
  419. Console.WriteLine("Log-GC: 2");
  420. LogGarbageCollector(this,this.Name,this.StateTask.ToString(),tm);
  421. }
  422. this.stateTask = StateEnum.ERROR;
  423. messageError = null;
  424. return tm;
  425. }
  426. //Console.WriteLine("messageError -> "+ message);
  427. string[] msg = message.Split('»');
  428. //Console.WriteLine("match.Count -> "+ msg.Length);
  429. if (msg.Length <3){
  430. //Tool.Logger.Error("message ->"+message+"<");
  431. return null;
  432. }
  433. string filename= msg[0];
  434. string line= msg[1];
  435. string error=msg[2];
  436. if(message.Contains("Log-M:")){
  437. tm =new TaskMessage(error, line, filename);
  438. if (LogMonitor!=null){
  439. LogMonitor(this,this.Name,this.StateTask.ToString(),tm);
  440. }
  441. this.stateTask = StateEnum.ERROR;
  442. messageError = null;
  443. return tm;
  444. }
  445. filename = filename.Replace('/',System.IO.Path.DirectorySeparatorChar);
  446. /*Console.WriteLine("error -> "+ error);
  447. Console.WriteLine("filename -> "+ filename);
  448. Console.WriteLine("line -> "+ line);
  449. Console.WriteLine("this.Name -> "+ this.Name);*/
  450. tm =new TaskMessage(error, filename, line);
  451. //this.output.Add(tm);
  452. this.stateTask = StateEnum.ERROR;
  453. messageError = null;
  454. if(filename.Contains("Log-E:")){
  455. //Console.WriteLine("YES LOGE");
  456. if (ErrorWrite!=null){
  457. TaskMessage tm2 = new TaskMessage(error,"","");
  458. //Console.WriteLine("YES ProcessErrorHandler");
  459. ErrorWrite(this,this.Name,this.StateTask.ToString(),tm2);
  460. }
  461. } //else {
  462. if (LogWrite!=null){
  463. LogWrite(this,this.Name,this.StateTask.ToString(),tm);
  464. }
  465. //}
  466. } catch {
  467. }
  468. return tm;
  469. }
  470. public string Name {
  471. get {
  472. return "Running";
  473. }
  474. }
  475. public StateEnum StateTask {
  476. get {
  477. return stateTask;
  478. }
  479. }
  480. public List<TaskMessage> Output {
  481. get {
  482. return output;
  483. }
  484. }
  485. public ITask ChildrenTask {
  486. get {
  487. return null;
  488. }
  489. }
  490. #endregion
  491. }
  492. }