LuaEditDebug.dpr 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //TO-DO : Get the LuaEditApplication Path from registy, Application.ExeName is the Host
  2. library LuaEditDebug;
  3. uses
  4. SysUtils,
  5. Messages,
  6. Dialogs,
  7. Classes,
  8. Windows,
  9. Controls,
  10. Registry,
  11. Forms,
  12. lua,
  13. LuaUtils,
  14. RTDebug,
  15. Themes,
  16. UxTheme,
  17. JvDockGlobals,
  18. JvDockControlForm,
  19. About in '..\About.pas' {frmAbout},
  20. GoToLine in '..\GoToLine.pas' {frmGotoLine},
  21. Main in '..\Main.pas' {frmMain},
  22. ProjectTree in '..\ProjectTree.pas' {frmProjectTree},
  23. Replace in '..\Replace.pas' {frmReplace},
  24. ReplaceQuerry in '..\ReplaceQuerry.pas' {frmReplaceQuerry},
  25. Search in '..\Search.pas' {frmSearch},
  26. Stack in '..\Stack.pas' {frmStack},
  27. Splash in '..\Splash.pas' {frmSplash},
  28. Watch in '..\Watch.pas' {frmWatch},
  29. FunctionList in '..\FunctionList.pas' {frmFunctionList},
  30. AddToPrj in '..\AddToPrj.pas' {frmAddToPrj},
  31. RemFromPrj in '..\RemFromPrj.pas' {frmRemoveFile},
  32. ErrorLookup in '..\ErrorLookup.pas' {frmErrorLookup},
  33. LuaStack in '..\LuaStack.pas' {frmLuaStack},
  34. PrintSetup in '..\PrintSetup.pas' {frmPrintSetup},
  35. PrjSettings in '..\PrjSettings.pas' {frmPrjOptions},
  36. CntString in '..\CntString.pas' {frmCntString},
  37. Connecting in '..\Connecting.pas' {frmConnecting},
  38. Contributors in '..\Contributors.pas' {frmContributors},
  39. LuaOutput in '..\LuaOutput.pas' {frmLuaOutput},
  40. Breakpoints in '..\Breakpoints.pas' {frmBreakpoints},
  41. RegSetFileType in '..\RegSetFileType.pas',
  42. LuaLocals in '..\LuaLocals.pas' {frmLuaLocals},
  43. LuaEditMessages in '..\LuaEditMessages.pas' {frmLuaEditMessages},
  44. AddBreakpoint in '..\AddBreakpoint.pas' {frmAddBreakpoint},
  45. LuaGlobals in '..\LuaGlobals.pas' {frmLuaGlobals},
  46. EditorSettings in '..\EditorSettings.pas' {frmEditorSettings},
  47. ExSaveExit in '..\ExSaveExit.pas' {frmExSaveExit},
  48. AsciiTable in '..\AsciiTable.pas' {frmAsciiTable},
  49. ReadOnlyMsgBox in '..\ReadOnlyMsgBox.pas' {frmReadOnlyMsgBox},
  50. Rings in '..\Rings.pas' {frmRings},
  51. FindInFiles in '..\FindInFiles.pas' {frmFindInFiles},
  52. FindWindow1 in '..\FindWindow1.pas' {frmFindWindow1},
  53. FindWindow2 in '..\FindWindow2.pas' {frmFindWindow2},
  54. InternalBrowser in '..\InternalBrowser.pas' {frmInternalBrowser},
  55. SearchPath in '..\SearchPath.pas' {frmSearchPath},
  56. SIFReport in '..\SIFReport.pas' {frmSIFReport};
  57. {$R *.res}
  58. function InitForms: TfrmMain;
  59. var
  60. pReg: TRegistry;
  61. begin
  62. RTAssert(0, true, 'LuaEditDebug Application='+IntToHex(Integer(Application), 8), '', 0);
  63. RTAssert(0, true, 'Creating...', '', 0);
  64. // leave those important lines here...
  65. // There is a bug with the themes and forms placed in dll
  66. // The dll don't have the time to free the theme handle and the when it does it,
  67. // the host application already did it so... Runtime Error 216
  68. ThemeServices.ApplyThemeChange;
  69. InitThemeLibrary;
  70. Application.CreateForm(TfrmMain, frmMain);
  71. Application.CreateForm(TfrmAbout, frmAbout);
  72. Application.CreateForm(TfrmGotoLine, frmGotoLine);
  73. Application.CreateForm(TfrmReplace, frmReplace);
  74. Application.CreateForm(TfrmReplaceQuerry, frmReplaceQuerry);
  75. Application.CreateForm(TfrmSearch, frmSearch);
  76. Application.CreateForm(TfrmAddToPrj, frmAddToPrj);
  77. Application.CreateForm(TfrmRemoveFile, frmRemoveFile);
  78. Application.CreateForm(TfrmErrorLookup, frmErrorLookup);
  79. Application.CreateForm(TfrmPrintSetup, frmPrintSetup);
  80. Application.CreateForm(TfrmPrjOptions, frmPrjOptions);
  81. Application.CreateForm(TfrmCntString, frmCntString);
  82. Application.CreateForm(TfrmConnecting, frmConnecting);
  83. Application.CreateForm(TfrmContributors, frmContributors);
  84. Application.CreateForm(TfrmAddBreakpoint, frmAddBreakpoint);
  85. Application.CreateForm(TfrmEditorSettings, frmEditorSettings);
  86. Application.CreateForm(TfrmAsciiTable, frmAsciiTable);
  87. Application.CreateForm(TfrmFindInFiles, frmFindInFiles);
  88. Application.CreateForm(TfrmFindWindow1, frmFindWindow1);
  89. Application.CreateForm(TfrmFindWindow2, frmFindWindow2);
  90. Application.CreateForm(TfrmInternalBrowser, frmInternalBrowser);
  91. Application.CreateForm(TfrmSearchPath, frmSearchPath);
  92. Application.CreateForm(TfrmSIFReport, frmSIFReport);
  93. if (Application.MainForm = nil) then
  94. RTAssert(0, true, 'Creating done Nil', '', 0)
  95. else
  96. RTAssert(0, (frmMain = Application.MainForm), 'Creating done frmMain', 'Creating done '+Application.MainForm.Name, 0);
  97. // Initialize a few things
  98. frmMain.CheckButtons;
  99. frmMain.LoadEditorSettings;
  100. // Create registry class and switching root key to local machine
  101. pReg := TRegistry.Create;
  102. pReg.RootKey := HKEY_LOCAL_MACHINE;
  103. if pReg.OpenKey('\Software\LuaEdit', False) then
  104. LoadDockTreeFromFile(pReg.ReadString('ApplicationPath') + 'LuaEdit.dck');
  105. // Free registry class and return main form
  106. pReg.Free;
  107. Result :=frmMain;
  108. end;
  109. procedure UnInitForms;
  110. Var
  111. MainFormHandle: HWnd;
  112. i: Integer;
  113. begin
  114. try
  115. RTAssert(0, true, 'Freeing', '', 0);
  116. MainFormHandle :=frmMain.Handle;
  117. for i :=0 to Screen.CustomFormCount-1 do
  118. if (Screen.CustomForms[0]<>Nil) then
  119. begin
  120. RTAssert(0, true, ' '+Screen.CustomForms[0].Name+'('+Screen.CustomForms[0].ClassName+') Free (screen)', '', 0);
  121. Screen.CustomForms[0].Free;
  122. end;
  123. repeat
  124. Sleep(100);
  125. Until not(IsWindow(MainFormHandle));
  126. RTAssert(0, true, 'Free Done', '', 0);
  127. except
  128. RTAssert(0, true, 'Free exception', '', 0);
  129. end;
  130. // leave those important lines here...
  131. // There is a bug with the themes and forms placed in dll
  132. // The dll don't have the time to free the theme handle and the when it does it,
  133. // the host application already did it so... Runtime Error 216
  134. ThemeServices.ApplyThemeChange;
  135. FreeThemeLibrary;
  136. end;
  137. function LuaEditDebugOpen :Plua_State; cdecl;
  138. begin
  139. Result :=lua_open;
  140. //Store in LuaRegistry Only!!!!!
  141. LuaSetTableLightUserData(Result, LUA_REGISTRYINDEX, 'LuaEditDebug_MainForm',
  142. InitForms);
  143. end;
  144. function LuaEditDebugStartFile(LState :Plua_State; Filename :PChar):Integer; cdecl;
  145. Var
  146. MainForm :TfrmMain;
  147. pLuaUnit: TLuaUnit;
  148. begin
  149. RTAssert(0, true, 'Starting '+Filename, '', 0);
  150. Result :=LUA_ERRRUN;
  151. MainForm :=LuaGetTableLightUserData(LState, LUA_REGISTRYINDEX, 'LuaEditDebug_MainForm');
  152. if (MainForm<>Nil)
  153. then with MainForm do
  154. begin
  155. MainForm.Visible :=True;
  156. repeat
  157. Sleep(100);
  158. Until IsWindowVisible(MainForm.Handle);
  159. RTAssert(0, true, ' AddFileInProject', '', 0);
  160. pLuaUnit := AddFileInProject(FileName, False, LuaSingleUnits);
  161. RTAssert(0, true, ' AddFileInTab', '', 0);
  162. pLuaUnit.IsLoaded := True;
  163. AddFileInTab(pLuaUnit);
  164. MonitorFileToRecent(FileName);
  165. RTAssert(0, true, ' CheckButtons', '', 0);
  166. CheckButtons;
  167. MainForm.LuaState :=LState;
  168. ExecuteCurrent(True, 0, '', -1);
  169. end;
  170. end;
  171. function LuaEditDebugStart(LuaState :Plua_State; Code :PChar):Integer; cdecl;
  172. Var
  173. xTempFile :TFileStream;
  174. xTempFileName,
  175. TempPath :String;
  176. function UniqueFileName(BasePath :String) :String;
  177. Var
  178. loopCounter :Integer;
  179. begin
  180. if (BasePath[Length(BasePath)]<>'\')
  181. then BasePath :=BasePath + '\';
  182. loopCounter :=30;
  183. repeat
  184. sleep(250);
  185. DateTimeToString(Result, 'yyyy-mm-dd-hh-nn-ss-z', Now);
  186. Result :=BasePath+Result+'.lua';
  187. Dec(loopCounter);
  188. Until not(FileExists(Result)) or (loopCounter=-1);
  189. if (loopCounter=-1)
  190. then raise Exception.Create('Cannot Work on '+BasePath);
  191. end;
  192. begin
  193. try
  194. SetLength(TempPath, MAX_PATH);
  195. GetTempPath(MAX_PATH, PChar(TempPath));
  196. TempPath :=PChar(TempPath); //adjust Length to reflect the new real size
  197. xTempFilename :=UniqueFileName(TempPath);
  198. RTAssert(0, true, 'Filling '+xTempFilename, '', 0);
  199. xTempFile :=TFileStream.Create(xTempFilename, fmCreate);
  200. xTempFile.Write(Code^, Length(Code^));
  201. xTempFile.Free;
  202. Result :=LuaEditDebugStartFile(LuaState, PChar(xTempFileName));
  203. except
  204. Result := LUA_ERRRUN;
  205. end;
  206. end;
  207. procedure LuaEditDebugClose(LuaState :Plua_State); cdecl;
  208. Var
  209. MainForm :TfrmMain;
  210. begin
  211. MainForm :=LuaGetTableLightUserData(LuaState, LUA_REGISTRYINDEX, 'LuaEditDebug_MainForm');
  212. if (MainForm<>Nil)
  213. then UnInitForms;
  214. RTAssert(0, true, 'lua_close', '', 0);
  215. lua_close(LuaState);
  216. RTAssert(0, true, 'lua_done', '', 0);
  217. end;
  218. exports
  219. LuaEditDebugOpen,
  220. LuaEditDebugStartFile,
  221. LuaEditDebugStart,
  222. LuaEditDebugClose;
  223. begin
  224. end.