/// <summary> /// 创建脚本文件信息对象 /// </summary> /// <param name="fileName"></param> /// <returns></returns> private ScriptFileInfo CreateScriptFile(string fileName) { ScriptFileInfo scriptFileInfo = null; if (!File.Exists(fileName)) { return(scriptFileInfo); } FileInfo fi = new FileInfo(fileName); if (fi.Extension == ".cs") { string fileCode = GetScriptCode(fileName); scriptFileInfo = new CSharpFileInfo(fileCode, fileName); using (var sr = fi.OpenText()) { scriptFileInfo.Source = Decode(sr.ReadToEnd(), fi.Extension); } scriptFileInfo.HashCode = CryptoHelper.ToMd5Hash(scriptFileInfo.Source); } else { TraceLog.WriteError("Not supported \"{0}\" file type.", fileName); } return(scriptFileInfo); }
private bool TryParseType(string scriptCode, string typeName, out Type type) { type = null; if (string.IsNullOrEmpty(scriptCode)) { typeName = typeName ?? ""; scriptCode = ParseScriptCode(typeName); } scriptCode = GetScriptCode(scriptCode); Assembly assembly = _csharpAssembly; ScriptFileInfo scriptInfo = _csharpCodeCache[scriptCode]; if (scriptInfo == null) { scriptInfo = _modelCodeCache[scriptCode]; assembly = _modelAssembly; } if (scriptInfo != null) { if (assembly != null) { type = assembly.GetType(typeName, false, true); } return(type != null); } return(false); }
/// <summary> /// 执行脚本 /// </summary> /// <param name="scriptInfo">ScriptFileInfo对象</param> /// <param name="typeName">csharp脚本指定对象类型</param> /// <param name="args">csharp脚本指定类型构造函数的参数</param> /// <returns>csharp脚本返回指定typeName实例对象;python脚本返回ScriptCode对象</returns> public static dynamic Execute(ScriptFileInfo scriptInfo, string typeName, params object[] args) { if (scriptInfo is PythonFileInfo) { if (_scriptEngine == null) { return(null); } var scope = _scriptEngine.CreateScope(); ((PythonFileInfo)scriptInfo).CompiledCode.Execute(scope); return(scope); } if (scriptInfo is CSharpFileInfo) { if (scriptInfo.ObjType == null) { var item = _watcherDict[scriptInfo.GroupName]; if (item != null && item.Assembly != null) { if (string.IsNullOrEmpty(typeName)) { typeName = ("Game.Script." + Path.GetFileNameWithoutExtension(scriptInfo.FileName)).Split(',')[0]; } scriptInfo.ObjType = item.Assembly.GetType(typeName, false, true); } } return(scriptInfo.ObjType != null?scriptInfo.ObjType.CreateInstance(args) : null); } throw new NotSupportedException("Not supported script type:" + scriptInfo.GetType().FullName); }
private ScriptFileInfo LoadScript(string scriptPath, string fileName, bool isReLoad = false) { ScriptFileInfo scriptFileInfo = null; string scriptCode = GetScriptCode(fileName); if (!isReLoad && _csharpCodeCache.ContainsKey(scriptCode)) { var old = _csharpCodeCache[scriptCode]; if (!File.Exists(fileName) || old.HashCode == GetFileHashCode(fileName)) { return(old); } } scriptFileInfo = CreateScriptFile(fileName); if (scriptFileInfo != null) { if (scriptPath == _modelScriptPath) { _modelCodeCache[scriptCode] = scriptFileInfo; } else { _csharpCodeCache[scriptCode] = scriptFileInfo; } } return(scriptFileInfo); }
/// <summary> /// /// </summary> /// <param name="fileName"></param> /// <returns></returns> public override bool VerifyScriptHashCode(string fileName) { string ext = Path.GetExtension(fileName); if (string.Compare(ext, ".cs", StringComparison.OrdinalIgnoreCase) != 0) { return(base.VerifyScriptHashCode(fileName)); } string scriptCode = GetScriptCode(fileName); if (File.Exists(fileName)) { if (fileName.EndsWith("AssemblyInfo.cs", StringComparison.OrdinalIgnoreCase)) { return(true); } ScriptFileInfo code = null; if (_modelCodeCache.ContainsKey(scriptCode)) { code = _modelCodeCache[scriptCode]; } if (_csharpCodeCache.ContainsKey(scriptCode)) { code = _csharpCodeCache[scriptCode]; } if (code == null) { return(false); } string source = Decode(File.ReadAllText(fileName), ext); return(code.HashCode == CryptoHelper.ToMd5Hash(source)); } return(false); }
/// <summary> /// 执行脚本 /// </summary> /// <param name="scriptCode">脚本标识</param> /// <param name="typeName">csharp脚本指定对象类型</param> /// <param name="args">csharp脚本指定类型构造函数的参数</param> /// <returns>csharp脚本返回指定typeName实例对象;python脚本返回ScriptCode对象</returns> public static dynamic Execute(string scriptCode, string typeName, params object[] args) { scriptCode = GetScriptCode(scriptCode); ScriptFileInfo scriptInfo = _scriptCodeCache[scriptCode]; if (scriptInfo != null) { return(Execute(scriptInfo, typeName, args)); } return(null); }
/// <summary> /// update py reference head file. /// </summary> private static void UpdatePythonReferenceLib() { if (DisablePython) { return; } StringBuilder pyCode = new StringBuilder(); pyCode.AppendLine(@"import clr, sys"); pyCode.AppendLine(@"clr.AddReference('ZyGames.Framework.Common')"); pyCode.AppendLine(@"clr.AddReference('ZyGames.Framework')"); pyCode.AppendLine(@"clr.AddReference('ZyGames.Framework.Game')"); var assmeblyNames = _watcherDict.Where(p => !p.Value.IsPython && p.Value.Assembly != null) .Select(p => p.Value.Assembly.GetName().Name); foreach (var assmeblyName in assmeblyNames) { pyCode.AppendFormat(@"clr.AddReference('{0}')", assmeblyName); pyCode.AppendLine(); } string scriptCode = GetScriptCode(ReferenceLibFile); ScriptFileInfo scriptInfo = _scriptCodeCache[scriptCode]; if (scriptInfo == null || scriptInfo.TryEnterLock()) { try { using (var sw = File.CreateText(ReferenceLibFile)) { sw.Write(pyCode.ToString()); sw.Flush(); sw.Close(); } } finally { if (scriptInfo != null) { scriptInfo.ExitLock(); } } } }
private ScriptFileInfo LoadScript(string scriptPath, string fileName) { ScriptFileInfo scriptFileInfo = null; string scriptCode = GetScriptCode(fileName); scriptFileInfo = CreateScriptFile(fileName); if (scriptFileInfo != null) { if (scriptPath == _modelScriptPath) { _modelCodeCache[scriptCode] = scriptFileInfo; } else { _csharpCodeCache[scriptCode] = scriptFileInfo; } } return(scriptFileInfo); }
private bool CreateInstance(string scriptCode, string typeName, object[] args, out object result) { result = null; if (string.IsNullOrEmpty(scriptCode)) { typeName = typeName ?? ""; int index = typeName.IndexOf(SettupInfo.CSharpScriptPath + ".", StringComparison.CurrentCultureIgnoreCase); if (index > -1) { scriptCode = typeName.Substring(index) + ".cs"; } else { var arr = typeName.Split(',')[0].Split('.'); scriptCode = arr[arr.Length - 1] + ".cs"; } } scriptCode = GetScriptCode(scriptCode); Assembly assembly = _csharpAssembly; ScriptFileInfo scriptInfo = _csharpCodeCache[scriptCode]; if (scriptInfo == null) { scriptInfo = _modelCodeCache[scriptCode]; assembly = _modelAssembly; } if (scriptInfo != null) { Type objType = null; if (assembly != null) { objType = assembly.GetType(typeName, false, true); } if (objType != null) { result = objType.CreateInstance(args); } return(true); } return(false); }
/// <summary> /// 加载脚本对象 /// </summary> /// <param name="fileName"></param> /// <param name="isUpdate"></param> public static ScriptFileInfo LoadScript(string fileName, bool isUpdate = false) { ScriptFileInfo scriptFileInfo = null; string scriptCode = GetScriptCode(fileName); if (!isUpdate && _scriptCodeCache.ContainsKey(scriptCode)) { var old = _scriptCodeCache[scriptCode]; if (!File.Exists(fileName) || old.HashCode == GetFileHashCode(fileName)) { return(old); } } scriptFileInfo = CreateScriptFile(fileName); if (scriptFileInfo != null) { _scriptCodeCache[scriptCode] = scriptFileInfo; } return(scriptFileInfo); }
/// <summary> /// 创建脚本文件信息对象 /// </summary> /// <param name="fileName"></param> /// <returns></returns> private ScriptFileInfo CreateScriptFile(string fileName) { ScriptFileInfo scriptFileInfo = null; if (!File.Exists(fileName)) { return(scriptFileInfo); } string ext = Path.GetExtension(fileName); if (string.Compare(ext, ".cs", StringComparison.OrdinalIgnoreCase) == 0) { string fileCode = GetScriptCode(fileName); scriptFileInfo = new CSharpFileInfo(fileCode, fileName); scriptFileInfo.Source = Decode(File.ReadAllText(fileName), ext); scriptFileInfo.HashCode = CryptoHelper.ToMd5Hash(scriptFileInfo.Source); } else { TraceLog.WriteError("Not supported \"{0}\" file type.", fileName); } return(scriptFileInfo); }
/// <summary> /// 创建脚本文件信息对象 /// </summary> /// <param name="fileName"></param> /// <returns></returns> private static ScriptFileInfo CreateScriptFile(string fileName) { ScriptFileInfo scriptFileInfo = null; if (!File.Exists(fileName)) { return(scriptFileInfo); } var watchDir = _watcherDict.Where(pair => fileName.StartsWith(pair.Value.Path)).FirstOrDefault(); FileInfo fi = new FileInfo(fileName); string fileCode = GetScriptCode(fileName); string fileHash = GetFileHashCode(fileName); if (fi.Extension == ".py") { CompiledCode compiledCode = CompilePython(fileName); if (compiledCode != null) { scriptFileInfo = new PythonFileInfo(fileCode, fileName, compiledCode); scriptFileInfo.HashCode = fileHash; scriptFileInfo.GroupName = watchDir.Key; } } else if (fi.Extension == ".cs") { scriptFileInfo = new CSharpFileInfo(fileCode, fileName); scriptFileInfo.HashCode = fileHash; scriptFileInfo.GroupName = watchDir.Key; } else { TraceLog.WriteError("Not supported \"{0}\" file type.", fileName); } return(scriptFileInfo); }
/// <summary> /// ִ�нű� /// </summary> /// <param name="scriptInfo">ScriptFileInfo����</param> /// <param name="typeName">csharp�ű�ָ����������</param> /// <param name="args">csharp�ű�ָ�������캯���IJ���</param> /// <returns>csharp�ű�����ָ��typeNameʵ������python�ű�����ScriptCode����</returns> public static dynamic Execute(ScriptFileInfo scriptInfo, string typeName, params object[] args) { if (scriptInfo is PythonFileInfo) { if (_scriptEngine == null) return null; var scope = _scriptEngine.CreateScope(); ((PythonFileInfo)scriptInfo).CompiledCode.Execute(scope); return scope; } if (scriptInfo is CSharpFileInfo) { if (scriptInfo.ObjType == null) { var item = _watcherDict[scriptInfo.GroupName]; if (item != null && item.Assembly != null && !item.IsUpdating()) { if (string.IsNullOrEmpty(typeName)) { typeName = ("Game.Script." + Path.GetFileNameWithoutExtension(scriptInfo.FileName)).Split(',')[0]; } scriptInfo.ObjType = item.Assembly.GetType(typeName, false, true); } } return scriptInfo.ObjType != null ? scriptInfo.ObjType.CreateInstance(args) : null; } throw new NotSupportedException("Not supported script type:" + scriptInfo.GetType().FullName); }
/// <summary> /// ִ�нű� /// </summary> /// <param name="scriptInfo">ScriptFileInfo����</param> /// <param name="typeName">csharp�ű�ָ����������</param> /// <param name="args">csharp�ű�ָ�������캯���IJ���</param> /// <returns>csharp�ű�����ָ��typeNameʵ������python�ű�����ScriptCode����</returns> public static dynamic Execute(ScriptFileInfo scriptInfo, string typeName, params object[] args) { if (scriptInfo is PythonFileInfo) { if (_scriptEngine == null) return null; var scope = _scriptEngine.CreateScope(); ((PythonFileInfo)scriptInfo).CompiledCode.Execute(scope); return scope; } if (scriptInfo is CSharpFileInfo) { if (_csharpAssembly != null) { typeName = (typeName ?? "Game.Script." + Path.GetFileNameWithoutExtension(scriptInfo.FileName)).Split(',')[0]; var type = _csharpAssembly.GetType(typeName, false, true); if (type != null) { return type.CreateInstance(args); } } return null; } throw new NotSupportedException("Not supported script type:" + scriptInfo.GetType().FullName); }