sfPrivate.cs
上传用户:lctyggxszx
上传日期:2022-06-30
资源大小:280k
文件大小:14k
源码类别:

FlashMX/Flex源码

开发平台:

C#

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Windows.Forms;
  5. using AxShockwaveFlashObjects;
  6. using System.Xml;
  7. using Mueller.Wddx;
  8. using System.IO;
  9. using System.Diagnostics;
  10. namespace SharpFlash
  11. {
  12. /// <summary>
  13. /// Summary description for sfPrivate.
  14. /// </summary>
  15. public class sfPrivate
  16. {
  17. private static WddxSerializer wddxSerializer = new WddxSerializer();
  18. private static WddxDeserializer wddxDeserializer = new WddxDeserializer();
  19. //These libraries can be static for now.. depends on what their use is.
  20. public sfPrivate() {
  21. }
  22. public static void Command(Form form, AxShockwaveFlash flashApp, string list, String method) {
  23. switch(method) {
  24. case "build":
  25. sfPrivate.build(form, flashApp, list);
  26. break;
  27. case "checkVersion":
  28. sfPrivate.checkVersion(form, flashApp, list);
  29. break;
  30. }
  31. }
  32. public static void build(Form form, AxShockwaveFlash flashApp, string list) {
  33. bool showControlBox = true;
  34. bool showMinimizeBox = true;
  35. bool showMaximizeBox = true;
  36. // First we need to deserialize the string to get a list of all of the build options
  37. System.Collections.Hashtable options = (System.Collections.Hashtable)wddxDeserializer.Deserialize(list);
  38. // just wrap this whole thing in a try/catch block to handle
  39. // any errors that might result from building
  40. try 
  41. {
  42. string fileName = Directory.GetCurrentDirectory() + "\sfBuild.cs";
  43. FileStream codeStream = new FileStream(fileName, FileMode.Create, FileAccess.Write); 
  44. StreamWriter codeWriter = new StreamWriter(codeStream); 
  45. System.Collections.Hashtable project = (System.Collections.Hashtable)options["project"];
  46. System.Collections.ArrayList window = (System.Collections.ArrayList)options["window"];
  47. System.Collections.Hashtable output = (System.Collections.Hashtable)options["output"];
  48. System.Collections.Hashtable window0 = (System.Collections.Hashtable)window[0];
  49. //Console.WriteLine("nnlist = " + list);
  50. string title = project["title"].ToString();
  51. string exeName = output["location"].ToString().Replace("\", "\\");
  52. // if using absolute paths, we mark the mainSwf as the abs location
  53. // otherwise, using relative paths, we'll use Application.StartupPath in the 
  54. // swf location
  55. string mainSwf = window0["swf"].ToString();
  56. mainSwf = mainSwf.Replace("\", "\\");
  57. // use my SWFHeaderInfo class to get the width and height of the movie
  58. // so we can create a form that matches it perfectly.
  59. com.darronschall.SWFHeaderInfo swf = new com.darronschall.SWFHeaderInfo(mainSwf);
  60. if (swf.Status != "OK") 
  61. {
  62. flashApp.SetVariable("$SFData", "<sf><callback><id>" + options["callback_id"].ToString() + "</id><args><arg>" + wddxSerializer.Serialize(false).ToString() + "</arg><arg>" + swf.Status + "</arg></args></callback></sf>");
  63. return;
  64. }
  65. string borderStyle = "";
  66. switch (window0["border"].ToString())
  67. {
  68. case "none":
  69. borderStyle = "System.Windows.Forms.FormBorderStyle.None";
  70. break;
  71. case "normal":
  72. Console.WriteLine("resizable = " + window0["resizable"].ToString());
  73. if ((bool)window0["resizable"]) 
  74. {
  75. borderStyle = "System.Windows.Forms.FormBorderStyle.Sizable";
  76. else 
  77. {
  78. borderStyle = "System.Windows.Forms.FormBorderStyle.FixedSingle";
  79. }
  80. Console.WriteLine("close = "  + window0["close"].ToString());
  81. if (!((bool)window0["close"])) 
  82. {
  83. showControlBox = false;
  84. }
  85. Console.WriteLine("maximize = "  + window0["maximize"].ToString());
  86. if (!((bool)window0["maximize"])) 
  87. {
  88. showMaximizeBox = false;
  89. }
  90. Console.WriteLine("minimize = "  + window0["minimize"].ToString());
  91. if (!((bool)window0["minimize"]))
  92. {
  93. showMinimizeBox = false;
  94. }
  95. break;
  96. case "thin":
  97. //Console.WriteLine(window0["resizable"].ToString());
  98. if ((bool)window0["resizable"]) 
  99. {
  100. borderStyle = "System.Windows.Forms.FormBorderStyle.SizableToolWindow";
  101. else 
  102. {
  103. borderStyle = "System.Windows.Forms.FormBorderStyle.FixedToolWindow";
  104. }
  105. if (!((bool)window0["close"])) 
  106. {
  107. showControlBox = false;
  108. }
  109. break;
  110. default:
  111. // Invalid border style
  112. //Console.WriteLine(window0["border"].ToString());
  113. break;
  114. }
  115. string command = "csc /target:winexe /out:"" + exeName + "" /r:AxInterop.ShockwaveFlashObjects.dll /r:Interop.ShockwaveFlashObjects.dll /r:sfApplication.dll /r:sfFile.dll /r:sfPrivate.dll /r:sfSystem.dll /r:sfSystemDialogs.dll /r:sfSystemMessageBox.dll /r:sfSystemRegistry.dll /r:sfUI.dll /r:sfWindow.dll /r:SharpFlashConnection.dll /r:Wddx.Net.dll /resource:"" + Directory.GetCurrentDirectory() + ""\SharpFlash.SharpFlash.resources /debug- sfBuild.cs";
  116. // Write to the file using StreamWriter class 
  117. codeWriter.BaseStream.Seek(0, SeekOrigin.End); 
  118. codeWriter.Write("using System;n"
  119. + "using System.Drawing;n"
  120. + "using System.Collections;n"
  121. + "using System.ComponentModel;n"
  122. + "using System.Windows.Forms;n"
  123. + "using System.Data;n"
  124. + "using System.Threading;n"
  125. + "namespace SharpFlashn"
  126. + "{n"
  127. + " public class SharpFlash : System.Windows.Forms.Formn"
  128. + " {n"
  129. + " private AxShockwaveFlashObjects.AxShockwaveFlash flashApp;n"
  130. + " private System.ComponentModel.Container components = null;n"
  131. + " public SharpFlash()n"
  132. + " {n"
  133. + " InitializeComponent();n"
  134. + " flashApp.Movie = Application.StartupPath + "\\" + mainSwf.Substring(mainSwf.LastIndexOf("\") + 1) + "";n"
  135. + " flashApp.FSCommand += new AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEventHandler(flashApp_FSCommandEventHandler);n"
  136. + " }n"
  137. + " protected override void Dispose( bool disposing )n"
  138. + " {n"
  139. + " if( disposing )n"
  140. + " {n"
  141. + " if (components != null) n"
  142. + " {n"
  143. + " components.Dispose();n"
  144. + " }n"
  145. + " }n"
  146. + " base.Dispose( disposing );n"
  147. + " }n"
  148. + " private void InitializeComponent()n"
  149. + " {n"
  150. + " System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SharpFlash));n"
  151. + " this.flashApp = new AxShockwaveFlashObjects.AxShockwaveFlash();n"
  152. + " ((System.ComponentModel.ISupportInitialize)(this.flashApp)).BeginInit();n"
  153. + " this.SuspendLayout();n"
  154. + " this.flashApp.Enabled = true;n"
  155. + " this.flashApp.Location = new System.Drawing.Point(0, 0);n"
  156. + " this.flashApp.Name = "flashApp";n"
  157. + " this.flashApp.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("flashApp.OcxState")));n"
  158. + " this.flashApp.Size = new System.Drawing.Size(" + swf.xMax + ", " + swf.yMax + ");n"
  159. + " this.flashApp.TabIndex = 0;n"
  160. + " this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);n"
  161. + " this.ClientSize = new System.Drawing.Size(" + swf.xMax + ", " + swf.yMax + ");n"
  162. + " this.Controls.Add(this.flashApp);n"
  163. + " this.FormBorderStyle = " + borderStyle + ";n"
  164. + " this.Name = "SharpFlash";n"
  165. + " this.Text = "" + title + "";n"
  166. + " this.Closing += new System.ComponentModel.CancelEventHandler(this.SharpFlash_Closing);n"
  167. + " ((System.ComponentModel.ISupportInitialize)(this.flashApp)).EndInit();n"
  168. + "         this.ControlBox = " + showControlBox.ToString().ToLower() + ";n"
  169. + "         this.MinimizeBox = " + showMinimizeBox.ToString().ToLower() + ";n"
  170. + "         this.MaximizeBox = " + showMaximizeBox.ToString().ToLower() + ";n"
  171. + " this.ResumeLayout(false);n"
  172. + " }n"
  173. + " [STAThread]n"
  174. + " static void Main() n"
  175. + " {n"
  176. + " Application.Run(new SharpFlash());n"
  177. + " }n"
  178. + " private void flashApp_FSCommandEventHandler(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEvent e)n"
  179. + " {n"
  180. + " String library = e.command.Substring(0,e.command.IndexOf("."));n"
  181. + " String methodName = e.command.Substring(e.command.IndexOf(".")+1);n"
  182. + " switch(library) {n"
  183. + " case "sfApplication":n"
  184. + " sfApplication.Command(this,flashApp,e.args,methodName);n"
  185. + "                 break;n"
  186. + " case "sfFile":n"
  187. + " sfFile.Command(this,flashApp,e.args,methodName);n"
  188. + "                 break;n"
  189. // don't let applications built with sharpflash have access to the sfPrivate functions
  190. // in future versions.. make sure the next 3 lines get commented out
  191. // for some reason adding these 3 lines of code in breaks the fscommand handler???
  192. //+ " case "sfPrivate":n"
  193. //+ " sfPrivate.Command(this,flashApp,e.args,methodName);n"
  194. //+ " break;n"
  195. + " case "sfSystem": n"
  196. + " sfSystem.Command(this,flashApp,e.args,methodName);n"
  197. + " break;n"
  198. + " case "sfUI": n"
  199. + " sfUI.Command(this,flashApp,e.args,methodName);n"
  200. + " break;n"
  201. + " case "sfWindow":n"
  202. + " sfWindow.Command(this,flashApp,e.args,methodName);n"
  203. + "                 break;n"
  204. + " }n"
  205. + "n"
  206. + " }n"
  207. + " private void SharpFlash_Closing(object sender, System.ComponentModel.CancelEventArgs e)n"
  208. + " {n"
  209. + " Application.Exit();n"
  210. + " }n"
  211. + " }n"
  212. + "}n"); 
  213. codeWriter.Flush(); 
  214. codeWriter.Close();
  215. // now that the file is written, we need to run the command to build the
  216. // executable from the .cs file, making sure we link in all of
  217. // our necessary .dll's and assembly information
  218. FileStream batchStream = new FileStream(Directory.GetCurrentDirectory() + "\compile.bat", FileMode.Create);
  219. StreamWriter batchWriter = new StreamWriter(batchStream);
  220. command =  command + " > compile.bat";
  221. batchWriter.WriteLine(command);
  222. batchWriter.WriteLine("start compile.bat");
  223. batchWriter.Close();
  224. Process p = new Process();
  225. ProcessStartInfo pinfo = new ProcessStartInfo();
  226. pinfo.FileName = "compile";
  227. pinfo.WindowStyle = ProcessWindowStyle.Hidden;
  228. p.StartInfo = pinfo;
  229. p.Start();
  230. // wait for the process to finish before continuing...
  231. p.WaitForExit();
  232. // after we finish compiling, we need to delete the .cs and batch files we created.
  233. File.Delete(Directory.GetCurrentDirectory() + "\compile.bat");
  234. File.Delete(Directory.GetCurrentDirectory() + "\sfBuild.cs");
  235. // copy the .swf file to the same directory as the output file
  236. string path = exeName.Substring(0, exeName.LastIndexOf("\\"));
  237. Console.WriteLine(path);
  238. string from = mainSwf;
  239. string to = exeName.Substring(0, exeName.LastIndexOf("\")) + mainSwf.Substring(mainSwf.LastIndexOf("\\") + 1);
  240. from = from.Replace("\\", "\");
  241. to = to.Replace("\\", "\");
  242. if (from != to) 
  243. {
  244. File.Copy(from, to, true);
  245. }
  246. // copy all of the required .dll's to the output directory as well
  247. File.Copy(Application.StartupPath + "\AxInterop.ShockwaveFlashObjects.dll", path + "\AxInterop.ShockwaveFlashObjects.dll" , true);
  248. File.Copy(Application.StartupPath + "\Interop.ShockwaveFlashObjects.dll", path + "\Interop.ShockwaveFlashObjects.dll" , true);
  249. //File.Copy(Application.StartupPath + "\NZipLib.dll", path + "\NZipLib.dll" , true);
  250. File.Copy(Application.StartupPath + "\sfApplication.dll", path + "\sfApplication.dll" , true);
  251. File.Copy(Application.StartupPath + "\sfFile.dll", path + "\sfFile.dll" , true);
  252. //File.Copy(Application.StartupPath + "\sfPrivate.dll", path + "\sfPrivate.dll" , true);
  253. File.Copy(Application.StartupPath + "\sfSystem.dll", path + "\sfSystem.dll" , true);
  254. File.Copy(Application.StartupPath + "\sfSystemDialogs.dll", path + "\sfSystemDialogs.dll" , true);
  255. File.Copy(Application.StartupPath + "\sfSystemMessageBox.dll", path + "\sfSystemMessageBox.dll" , true);
  256. File.Copy(Application.StartupPath + "\sfSystemRegistry.dll", path + "\sfSystemRegistry.dll" , true);
  257. File.Copy(Application.StartupPath + "\sfUI.dll", path + "\sfUI.dll" , true);
  258. File.Copy(Application.StartupPath + "\sfWindow.dll", path + "\sfWindow.dll" , true);
  259. //File.Copy(Application.StartupPath + "\SWFHeaderInfo.dll", path + "\SWFHeaderInfo.dll" , true);
  260. File.Copy(Application.StartupPath + "\WDDX.Net.dll", path + "\WDDX.Net.dll" , true);
  261. catch (Exception e) 
  262. {
  263. // Assuming build failure if we're here... send false back 
  264. // to the flash app with the message attached.
  265. flashApp.SetVariable("$SFData", "<sf><callback><id>" + options["callback_id"].ToString() + "</id>"
  266. + "<args>"
  267. + "<arg>" + wddxSerializer.Serialize(false).ToString() + "</arg>"
  268. + "<arg>" + wddxSerializer.Serialize(e.Message).ToString() + "</arg>"
  269. + "</args></callback></sf>");
  270. Console.WriteLine("Wrote Error: " + "<sf><callback><id>" + options["callback_id"].ToString() + "</id>"
  271. + "<args>"
  272. + "<arg>" + wddxSerializer.Serialize(false).ToString() + "</arg>"
  273. + "<arg>" + wddxSerializer.Serialize(e.Message).ToString() + "</arg>"
  274. + "</args></callback></sf>");
  275. return;
  276. }
  277. // Now we send a message back to the flash client with the callback id
  278. // and argument
  279. flashApp.SetVariable("$SFData", "<sf><callback><id>" + options["callback_id"].ToString() + "</id>"
  280. + "<args>"
  281. + "<arg>" + wddxSerializer.Serialize(true).ToString() + "</arg>"
  282. + "</args></callback></sf>");
  283. Console.WriteLine("Wrote Success: " + "<sf><callback><id>" + options["callback_id"].ToString() + "</id>"
  284. + "<args>"
  285. + "<arg>" + wddxSerializer.Serialize(true).ToString() + "</arg>"
  286. + "</args></callback></sf>");
  287. }
  288. public static void checkVersion(Form form, AxShockwaveFlash flashApp, string list) 
  289. {
  290. // :TODO: write me :-)
  291. }
  292. }
  293. }