⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dotnetscriptengine.cs

📁 语音视频功能 里面实现了基本的QQ与语音对话
💻 CS
字号:
using System;
using System.Text;
using System.IO;
using System.Reflection;
using System.Runtime;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;

namespace DotNetScriptEngine
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class DotNetScriptEngine
	{	
		private const int NoOrErrorRetValue = -1;	

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[MTAThread]
		static int Main(string[] args)
		{	
			int retValue = NoOrErrorRetValue;

			if (args.Length == 0)
			{
				//Create file associations from .dnml files to the script engine
				CreateFileAssociation();
				Console.WriteLine("File association for .dnml files has been created");
				Console.ReadLine();
				return NoOrErrorRetValue;
			}	
			else if (args[0].ToLower() == "remove")
			{
				//Remove file associations for .dnml files to the script engine
				RemoveFileAssociation();
				Console.WriteLine("File association for .dnml files has been removed");
				Console.ReadLine();
				return NoOrErrorRetValue;
			}

			AssemblyGenerator assGen = null;
			
			try
			{
				//load the csml text from the file
				string fileContents = GetFileContent(args);

				if (fileContents.Length > 0 && errMsg.Length == 0)
				{		
					try
					{	
						//Create a new AssemblyGenerator class, based on the csml text
						assGen = new AssemblyGenerator(fileContents);
						if (assGen != null || errMsg.Length == 0)
						{
							//Create an assembly in memmory
							Assembly assembly = assGen.CreateAssembly();			

							//Use reflection to call the Main function in the assembly
							retValue = RunScript(assembly, assGen.script.EntryPoint);					

							//I know i dont have too, but it makes me feel better
							assembly = null;
						}
					}
					catch (Exception ex)
					{
						LogAllErrMsgs("Error:  An exception occurred while compiling the script", ex);
					}
				}							
			}
			catch (Exception ex)
			{				
				LogAllErrMsgs("Error:  An exception occurred while running the script file", ex);				
			}
			
			//Log out all error messages to user
			DisplayErrorMsg();

			//if assGen was not created or if user wants console to remain open, then ReadLine
			if (assGen == null)
				Console.ReadLine();
			else if (assGen.script.WaitForUserAction)
				Console.ReadLine();

			//return a value to the user
			return retValue;
		}

		private static string GetFileContent(string[] args)
		{
			//Just to see what args are being passed in
			//foreach (string arg in args)
			//	Console.WriteLine(arg);

			if (args.Length > 0)
			{
				string fileName = args[0];
				//string fileName = @"E:\My Documents\My Articles\Dot Net Script\Source\VB test.dnml";

				if (fileName.Length > 0 && File.Exists(fileName))
				{
					StreamReader reader = null;
					string fileContents = string.Empty;

					try
					{
						reader = new StreamReader(fileName);
						fileContents = reader.ReadToEnd().Trim();
					}
					catch (Exception ex)
					{
						LogAllErrMsgs("Error:  An exception occurred while reading the code from the script file", ex);
					}
					finally
					{
						if (reader != null)
							reader.Close();
					}

					if (fileContents.Length == 0)
					{
						errMsg.Append("Error:  The dnml Script file is empty\r\n");
					}

					return fileContents;
				}
				else
				{
					errMsg.Append("Error:  The dnml Script file does not exist\r\n");
				}
			}
			else
			{
				errMsg.Append("Error:  An exception occurred while reading command arguments\r\n");
			}
			
			//else return an empty string
			return string.Empty;			
		}

		private static int RunScript(Assembly assembly, string entryPoint)
		{
			try
			{
				//cant call the entry method if the assembly is null
				if (assembly != null)
				{
					//Use reflection to call the static Main function
					Module[] mods = assembly.GetModules(false);
					Type[] types = mods[0].GetTypes();

					//loop through each class that was defined and look for the first occurrance of the entry point method
					foreach (Type type in types)
					{
						MethodInfo mi = type.GetMethod(entryPoint, BindingFlags.Public | BindingFlags.Static);
						if (mi != null)
						{
							if (mi.ReturnType.Name != "Int32")
							{
								//if the entry point method doesnt return an Int32, then return the error constant
								mi.Invoke(null, null);
								return NoOrErrorRetValue;
							}
							else
							{
								//if the entry point method does return in Int32, then capture it and return it
								return (int)mi.Invoke(null, null);
							}
						}
					}

					//if it got here, then there was no entry point method defined.  Tell user about it
					DotNetScriptEngine.LogAllErrMsgs("Dot Net Script Engine could not find the public static " + entryPoint + " entry function to execute");
				}
			}
			catch (Exception ex)
			{
				LogAllErrMsgs("Error:  An exception occurred while executing the script", ex);			
			}

			return NoOrErrorRetValue;
		}

		private static void DisplayErrorMsg()
		{
			if (errMsg.Length > 0)
			{
				Console.WriteLine(errMsg.ToString());
				Console.WriteLine("");				
			}
		}

		private static StringBuilder errMsg = new StringBuilder();
		internal static void LogAllErrMsgs(string customMsg)
		{
			LogAllErrMsgs(customMsg, null);
		}

		internal static void LogAllErrMsgs(string customMsg, Exception ex)
		{
			//put the error message into builder
			errMsg.Append("\r\n").Append("\r\n").Append(customMsg).Append("\r\n");

			//get all the exceptions and add their error messages
			while (ex != null)
			{
				errMsg.Append("\t").Append(ex.Message).Append("\r\n");	
				ex = ex.InnerException;
			}
		}

		internal static void CreateFileAssociation()
		{
			FileAssociation FA = new FileAssociation();
			FA.Extension = "dnml";
			FA.ContentType = "application/dotnetscript";
			FA.FullName = "DotNetScriptFiles";
			FA.ProperName = "DotNetScriptFiles";
			FA.AddCommand("open", "\"" + Assembly.GetExecutingAssembly().Location + "\" \" %1\"");			
			FA.AddCommand("edit", "notepad.exe %1");
			FA.IconPath = Assembly.GetExecutingAssembly().Location;
			FA.IconIndex = 0;
			FA.Create();
		}

		internal static void RemoveFileAssociation()
		{
			FileAssociation FA = new FileAssociation();
			FA.Extension = "dnml";
			FA.ContentType = "application/dotnetscript";
			FA.FullName = "DotNetScriptFiles";
			FA.ProperName = "DotNetScriptFiles";
			FA.AddCommand("open", Assembly.GetExecutingAssembly().Location + " %1");
			FA.AddCommand("edit", "notepad.exe %1");
			FA.IconPath = Assembly.GetExecutingAssembly().Location;
			FA.IconIndex = 0;
			FA.Remove();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -