sfPrivate.cs
上传用户:lctyggxszx
上传日期:2022-06-30
资源大小:280k
文件大小:14k
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using AxShockwaveFlashObjects;
- using System.Xml;
- using Mueller.Wddx;
- using System.IO;
- using System.Diagnostics;
- namespace SharpFlash
- {
- /// <summary>
- /// Summary description for sfPrivate.
- /// </summary>
- public class sfPrivate
- {
- private static WddxSerializer wddxSerializer = new WddxSerializer();
- private static WddxDeserializer wddxDeserializer = new WddxDeserializer();
-
- //These libraries can be static for now.. depends on what their use is.
- public sfPrivate() {
- }
- public static void Command(Form form, AxShockwaveFlash flashApp, string list, String method) {
- switch(method) {
- case "build":
- sfPrivate.build(form, flashApp, list);
- break;
- case "checkVersion":
- sfPrivate.checkVersion(form, flashApp, list);
- break;
-
- }
- }
- public static void build(Form form, AxShockwaveFlash flashApp, string list) {
- bool showControlBox = true;
- bool showMinimizeBox = true;
- bool showMaximizeBox = true;
-
- // First we need to deserialize the string to get a list of all of the build options
- System.Collections.Hashtable options = (System.Collections.Hashtable)wddxDeserializer.Deserialize(list);
-
- // just wrap this whole thing in a try/catch block to handle
- // any errors that might result from building
- try
- {
- string fileName = Directory.GetCurrentDirectory() + "\sfBuild.cs";
- FileStream codeStream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
- StreamWriter codeWriter = new StreamWriter(codeStream);
- System.Collections.Hashtable project = (System.Collections.Hashtable)options["project"];
- System.Collections.ArrayList window = (System.Collections.ArrayList)options["window"];
- System.Collections.Hashtable output = (System.Collections.Hashtable)options["output"];
-
- System.Collections.Hashtable window0 = (System.Collections.Hashtable)window[0];
- //Console.WriteLine("nnlist = " + list);
-
- string title = project["title"].ToString();
- string exeName = output["location"].ToString().Replace("\", "\\");
-
- // if using absolute paths, we mark the mainSwf as the abs location
- // otherwise, using relative paths, we'll use Application.StartupPath in the
- // swf location
- string mainSwf = window0["swf"].ToString();
- mainSwf = mainSwf.Replace("\", "\\");
-
- // use my SWFHeaderInfo class to get the width and height of the movie
- // so we can create a form that matches it perfectly.
- com.darronschall.SWFHeaderInfo swf = new com.darronschall.SWFHeaderInfo(mainSwf);
- if (swf.Status != "OK")
- {
- 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>");
- return;
- }
- string borderStyle = "";
- switch (window0["border"].ToString())
- {
- case "none":
- borderStyle = "System.Windows.Forms.FormBorderStyle.None";
- break;
- case "normal":
- Console.WriteLine("resizable = " + window0["resizable"].ToString());
- if ((bool)window0["resizable"])
- {
- borderStyle = "System.Windows.Forms.FormBorderStyle.Sizable";
- }
- else
- {
- borderStyle = "System.Windows.Forms.FormBorderStyle.FixedSingle";
- }
- Console.WriteLine("close = " + window0["close"].ToString());
- if (!((bool)window0["close"]))
- {
- showControlBox = false;
- }
- Console.WriteLine("maximize = " + window0["maximize"].ToString());
- if (!((bool)window0["maximize"]))
- {
- showMaximizeBox = false;
- }
- Console.WriteLine("minimize = " + window0["minimize"].ToString());
- if (!((bool)window0["minimize"]))
- {
- showMinimizeBox = false;
- }
- break;
- case "thin":
- //Console.WriteLine(window0["resizable"].ToString());
- if ((bool)window0["resizable"])
- {
- borderStyle = "System.Windows.Forms.FormBorderStyle.SizableToolWindow";
- }
- else
- {
- borderStyle = "System.Windows.Forms.FormBorderStyle.FixedToolWindow";
- }
- if (!((bool)window0["close"]))
- {
- showControlBox = false;
- }
- break;
- default:
- // Invalid border style
- //Console.WriteLine(window0["border"].ToString());
- break;
- }
- 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";
-
- // Write to the file using StreamWriter class
- codeWriter.BaseStream.Seek(0, SeekOrigin.End);
- codeWriter.Write("using System;n"
- + "using System.Drawing;n"
- + "using System.Collections;n"
- + "using System.ComponentModel;n"
- + "using System.Windows.Forms;n"
- + "using System.Data;n"
- + "using System.Threading;n"
- + "namespace SharpFlashn"
- + "{n"
- + " public class SharpFlash : System.Windows.Forms.Formn"
- + " {n"
- + " private AxShockwaveFlashObjects.AxShockwaveFlash flashApp;n"
- + " private System.ComponentModel.Container components = null;n"
- + " public SharpFlash()n"
- + " {n"
- + " InitializeComponent();n"
- + " flashApp.Movie = Application.StartupPath + "\\" + mainSwf.Substring(mainSwf.LastIndexOf("\") + 1) + "";n"
- + " flashApp.FSCommand += new AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEventHandler(flashApp_FSCommandEventHandler);n"
- + " }n"
- + " protected override void Dispose( bool disposing )n"
- + " {n"
- + " if( disposing )n"
- + " {n"
- + " if (components != null) n"
- + " {n"
- + " components.Dispose();n"
- + " }n"
- + " }n"
- + " base.Dispose( disposing );n"
- + " }n"
- + " private void InitializeComponent()n"
- + " {n"
- + " System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SharpFlash));n"
- + " this.flashApp = new AxShockwaveFlashObjects.AxShockwaveFlash();n"
- + " ((System.ComponentModel.ISupportInitialize)(this.flashApp)).BeginInit();n"
- + " this.SuspendLayout();n"
- + " this.flashApp.Enabled = true;n"
- + " this.flashApp.Location = new System.Drawing.Point(0, 0);n"
- + " this.flashApp.Name = "flashApp";n"
- + " this.flashApp.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("flashApp.OcxState")));n"
- + " this.flashApp.Size = new System.Drawing.Size(" + swf.xMax + ", " + swf.yMax + ");n"
- + " this.flashApp.TabIndex = 0;n"
- + " this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);n"
- + " this.ClientSize = new System.Drawing.Size(" + swf.xMax + ", " + swf.yMax + ");n"
- + " this.Controls.Add(this.flashApp);n"
- + " this.FormBorderStyle = " + borderStyle + ";n"
- + " this.Name = "SharpFlash";n"
- + " this.Text = "" + title + "";n"
- + " this.Closing += new System.ComponentModel.CancelEventHandler(this.SharpFlash_Closing);n"
- + " ((System.ComponentModel.ISupportInitialize)(this.flashApp)).EndInit();n"
- + " this.ControlBox = " + showControlBox.ToString().ToLower() + ";n"
- + " this.MinimizeBox = " + showMinimizeBox.ToString().ToLower() + ";n"
- + " this.MaximizeBox = " + showMaximizeBox.ToString().ToLower() + ";n"
- + " this.ResumeLayout(false);n"
- + " }n"
- + " [STAThread]n"
- + " static void Main() n"
- + " {n"
- + " Application.Run(new SharpFlash());n"
- + " }n"
- + " private void flashApp_FSCommandEventHandler(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEvent e)n"
- + " {n"
- + " String library = e.command.Substring(0,e.command.IndexOf("."));n"
- + " String methodName = e.command.Substring(e.command.IndexOf(".")+1);n"
- + " switch(library) {n"
- + " case "sfApplication":n"
- + " sfApplication.Command(this,flashApp,e.args,methodName);n"
- + " break;n"
- + " case "sfFile":n"
- + " sfFile.Command(this,flashApp,e.args,methodName);n"
- + " break;n"
- // don't let applications built with sharpflash have access to the sfPrivate functions
- // in future versions.. make sure the next 3 lines get commented out
- // for some reason adding these 3 lines of code in breaks the fscommand handler???
- //+ " case "sfPrivate":n"
- //+ " sfPrivate.Command(this,flashApp,e.args,methodName);n"
- //+ " break;n"
- + " case "sfSystem": n"
- + " sfSystem.Command(this,flashApp,e.args,methodName);n"
- + " break;n"
- + " case "sfUI": n"
- + " sfUI.Command(this,flashApp,e.args,methodName);n"
- + " break;n"
- + " case "sfWindow":n"
- + " sfWindow.Command(this,flashApp,e.args,methodName);n"
- + " break;n"
-
- + " }n"
- + "n"
- + " }n"
- + " private void SharpFlash_Closing(object sender, System.ComponentModel.CancelEventArgs e)n"
- + " {n"
- + " Application.Exit();n"
- + " }n"
- + " }n"
- + "}n");
-
-
- codeWriter.Flush();
- codeWriter.Close();
-
- // now that the file is written, we need to run the command to build the
- // executable from the .cs file, making sure we link in all of
- // our necessary .dll's and assembly information
- FileStream batchStream = new FileStream(Directory.GetCurrentDirectory() + "\compile.bat", FileMode.Create);
- StreamWriter batchWriter = new StreamWriter(batchStream);
- command = command + " > compile.bat";
- batchWriter.WriteLine(command);
- batchWriter.WriteLine("start compile.bat");
- batchWriter.Close();
- Process p = new Process();
- ProcessStartInfo pinfo = new ProcessStartInfo();
- pinfo.FileName = "compile";
- pinfo.WindowStyle = ProcessWindowStyle.Hidden;
- p.StartInfo = pinfo;
- p.Start();
-
- // wait for the process to finish before continuing...
- p.WaitForExit();
- // after we finish compiling, we need to delete the .cs and batch files we created.
-
- File.Delete(Directory.GetCurrentDirectory() + "\compile.bat");
- File.Delete(Directory.GetCurrentDirectory() + "\sfBuild.cs");
-
- // copy the .swf file to the same directory as the output file
- string path = exeName.Substring(0, exeName.LastIndexOf("\\"));
- Console.WriteLine(path);
-
- string from = mainSwf;
- string to = exeName.Substring(0, exeName.LastIndexOf("\")) + mainSwf.Substring(mainSwf.LastIndexOf("\\") + 1);
- from = from.Replace("\\", "\");
- to = to.Replace("\\", "\");
- if (from != to)
- {
- File.Copy(from, to, true);
- }
-
- // copy all of the required .dll's to the output directory as well
-
- File.Copy(Application.StartupPath + "\AxInterop.ShockwaveFlashObjects.dll", path + "\AxInterop.ShockwaveFlashObjects.dll" , true);
- File.Copy(Application.StartupPath + "\Interop.ShockwaveFlashObjects.dll", path + "\Interop.ShockwaveFlashObjects.dll" , true);
- //File.Copy(Application.StartupPath + "\NZipLib.dll", path + "\NZipLib.dll" , true);
- File.Copy(Application.StartupPath + "\sfApplication.dll", path + "\sfApplication.dll" , true);
- File.Copy(Application.StartupPath + "\sfFile.dll", path + "\sfFile.dll" , true);
- //File.Copy(Application.StartupPath + "\sfPrivate.dll", path + "\sfPrivate.dll" , true);
- File.Copy(Application.StartupPath + "\sfSystem.dll", path + "\sfSystem.dll" , true);
- File.Copy(Application.StartupPath + "\sfSystemDialogs.dll", path + "\sfSystemDialogs.dll" , true);
- File.Copy(Application.StartupPath + "\sfSystemMessageBox.dll", path + "\sfSystemMessageBox.dll" , true);
- File.Copy(Application.StartupPath + "\sfSystemRegistry.dll", path + "\sfSystemRegistry.dll" , true);
- File.Copy(Application.StartupPath + "\sfUI.dll", path + "\sfUI.dll" , true);
- File.Copy(Application.StartupPath + "\sfWindow.dll", path + "\sfWindow.dll" , true);
- //File.Copy(Application.StartupPath + "\SWFHeaderInfo.dll", path + "\SWFHeaderInfo.dll" , true);
- File.Copy(Application.StartupPath + "\WDDX.Net.dll", path + "\WDDX.Net.dll" , true);
- }
- catch (Exception e)
- {
- // Assuming build failure if we're here... send false back
- // to the flash app with the message attached.
- flashApp.SetVariable("$SFData", "<sf><callback><id>" + options["callback_id"].ToString() + "</id>"
- + "<args>"
- + "<arg>" + wddxSerializer.Serialize(false).ToString() + "</arg>"
- + "<arg>" + wddxSerializer.Serialize(e.Message).ToString() + "</arg>"
- + "</args></callback></sf>");
- Console.WriteLine("Wrote Error: " + "<sf><callback><id>" + options["callback_id"].ToString() + "</id>"
- + "<args>"
- + "<arg>" + wddxSerializer.Serialize(false).ToString() + "</arg>"
- + "<arg>" + wddxSerializer.Serialize(e.Message).ToString() + "</arg>"
- + "</args></callback></sf>");
- return;
- }
- // Now we send a message back to the flash client with the callback id
- // and argument
- flashApp.SetVariable("$SFData", "<sf><callback><id>" + options["callback_id"].ToString() + "</id>"
- + "<args>"
- + "<arg>" + wddxSerializer.Serialize(true).ToString() + "</arg>"
- + "</args></callback></sf>");
- Console.WriteLine("Wrote Success: " + "<sf><callback><id>" + options["callback_id"].ToString() + "</id>"
- + "<args>"
- + "<arg>" + wddxSerializer.Serialize(true).ToString() + "</arg>"
- + "</args></callback></sf>");
- }
- public static void checkVersion(Form form, AxShockwaveFlash flashApp, string list)
- {
- // :TODO: write me :-)
- }
-
- }
- }