PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/task/RunEmulatorTask.cs

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