public ScriptDomain() { ScriptDomain.sCurrentDomain = this; this._appdomain.AssemblyResolve += new ResolveEventHandler(_Module_.GTA__2EHandleResolve); this._appdomain.UnhandledException += new UnhandledExceptionEventHandler(_Module_.GTA__2EHandleUnhandledException); _Module_.GTA__2ELog("[INFO]", "Created new script domain with v", typeof(ScriptDomain).Assembly.GetName().Version.ToString(3), "."); }
public ScriptDomain() { sCurrentDomain = this; this._appdomain.AssemblyResolve += new ResolveEventHandler(< Module >.GTA.HandleResolve); this._appdomain.UnhandledException += new UnhandledExceptionEventHandler(< Module >.GTA.HandleUnhandledException); string[] message = new string[] { "Created new script domain with v", typeof(ScriptDomain).Assembly.GetName().Version.ToString(3), "." }; GTA.Log("[INFO]", message); }
/// <summary> /// Starts execution of this <see cref="Script"/>. /// </summary> internal void Start() { ThreadStart threadDelegate = new ThreadStart(MainLoop); _thread = new Thread(threadDelegate); _thread.Start(); ScriptDomain.OnStartScript(this); }
public static void OnAbortScript(Script script) { ScriptDomain domain = script._scriptdomain; if (ReferenceEquals(script, domain.Console)) { return; } domain.Console.UnregisterCommands(script.GetType()); Log("[INFO]", "Aborted script '", script.Name, "'."); }
public void Abort() { try { Script script = this; script.raise_Aborted((object)script, EventArgs.Empty); } catch (Exception ex) { _Module_.GTA__2EHandleUnhandledException((object)this, new UnhandledExceptionEventArgs((object)ex, true)); } this._waitEvent.Set(); ScriptDomain.AbortScript(this); }
/// <summary> /// The main execution logic of all scripts. /// </summary> internal void MainLoop() { _running = true; // Wait for domain to run scripts _continueEvent.WaitOne(); // Run main loop while (_running) { Tuple <bool, WinForms.KeyEventArgs> keyevent = null; // Process events while (_keyboardEvents.TryDequeue(out keyevent)) { try { if (keyevent.Item1) { KeyDown?.Invoke(this, keyevent.Item2); } else { KeyUp?.Invoke(this, keyevent.Item2); } } catch (Exception ex) { ScriptDomain.HandleUnhandledException(this, new UnhandledExceptionEventArgs(ex, false)); break; } } try { Tick?.Invoke(this, EventArgs.Empty); } catch (Exception ex) { ScriptDomain.HandleUnhandledException(this, new UnhandledExceptionEventArgs(ex, true)); Abort(); break; } // Yield execution to next tick Wait(_interval); } }
public static void OnStartScript(Script script) { ScriptDomain domain = script._scriptdomain; domain._runningScripts.Add(script); if (ReferenceEquals(script, domain.Console)) { return; } domain.Console.RegisterCommands(script.GetType()); Log("[INFO]", "Started script '", script.Name, "'."); }
public static void Unload(ref ScriptDomain domain) { _Module_.GTA__2ELog("[INFO]", "Unloading script domain ..."); domain.Abort(); AppDomain appDomain = domain.AppDomain; domain?.Dispose(); try { AppDomain.Unload(appDomain); } catch (Exception ex) { _Module_.GTA__2ELog("[ERROR]", "Failed to unload deleted script domain:", Environment.NewLine, ex.ToString()); } domain = (ScriptDomain)null; GC.Collect(); }
public static bool Init() { if (Domain != null) { GTA.ScriptDomain.Unload(ref Domain); } var settings = GTA.ScriptSettings.Load(Path.ChangeExtension(Assembly.GetExecutingAssembly().Location, ".ini")); Domain = GTA.ScriptDomain.Load(settings.GetValue(String.Empty, "ScriptsLocation", "scripts")); if (Domain != null) { Domain.Start(); return(true); } return(false); }
/// <summary> /// Aborts execution of this <see cref="Script"/>. /// </summary> public void Abort() { try { Aborted?.Invoke(this, EventArgs.Empty); } catch (Exception ex) { ScriptDomain.HandleUnhandledException(this, new UnhandledExceptionEventArgs(ex, true)); } _running = false; _waitEvent.Set(); if (_thread != null) { _thread.Abort(); _thread = null; ScriptDomain.OnAbortScript(this); } }
public static ScriptDomain Load(string path) { int num; List <string> list; List <string> list2; path = Path.GetFullPath(path); AppDomainSetup info = new AppDomainSetup { ApplicationBase = path, ShadowCopyFiles = "true", ShadowCopyDirectories = path }; StrongName[] fullTrustAssemblies = new StrongName[0]; System.AppDomain domain = System.AppDomain.CreateDomain("ScriptDomain_" + (path.GetHashCode() * Environment.TickCount).ToString("X"), null, info, new PermissionSet(PermissionState.Unrestricted), fullTrustAssemblies); domain.InitializeLifetimeService(); ScriptDomain domain2 = null; try { domain2 = (ScriptDomain)domain.CreateInstanceFromAndUnwrap(typeof(ScriptDomain).Assembly.Location, typeof(ScriptDomain).FullName); } catch (Exception exception1) { string[] strArray2 = new string[] { "Failed to create script domain '", domain.FriendlyName, "':", Environment.NewLine, exception1.ToString() }; GTA.Log("[ERROR]", strArray2); System.AppDomain.Unload(domain); return(null); } string[] message = new string[] { "Loading scripts from '", path, "' into script domain '", domain.FriendlyName, "' ..." }; GTA.Log("[INFO]", message); if (!Directory.Exists(path)) { string[] strArray7 = new string[] { "Failed to reload scripts because the directory is missing." }; GTA.Log("[ERROR]", strArray7); return(domain2); } else { list2 = new List <string>(); list = new List <string>(); try { list2.AddRange(Directory.GetFiles(path, "*.vb", SearchOption.AllDirectories)); list2.AddRange(Directory.GetFiles(path, "*.cs", SearchOption.AllDirectories)); list.AddRange(Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories)); } catch (Exception exception5) { string[] strArray6 = new string[] { "Failed to reload scripts:", Environment.NewLine, exception5.ToString() }; GTA.Log("[ERROR]", strArray6); System.AppDomain.Unload(domain); return(null); } num = 0; } while (true) { if (num >= list.Count) { List <string> .Enumerator enumerator2 = list2.GetEnumerator(); if (enumerator2.MoveNext()) { do { string current = enumerator2.Current; domain2.LoadScript(current); }while (enumerator2.MoveNext()); } List <string> .Enumerator enumerator = list.GetEnumerator(); if (enumerator.MoveNext()) { do { string current = enumerator.Current; domain2.LoadAssembly(current); }while (enumerator.MoveNext()); } break; } try { string assemblyFile = list[num]; if (AssemblyName.GetAssemblyName(assemblyFile).Name == "ScriptHookVDotNet") { string[] strArray5 = new string[] { "Removing assembly file '", Path.GetFileName(assemblyFile), "'." }; GTA.Log("[WARNING]", strArray5); num += -1; list.RemoveAt(num); try { File.Delete(assemblyFile); } catch (Exception exception6) { string[] strArray4 = new string[] { "Failed to delete assembly file:", Environment.NewLine, exception6.ToString() }; GTA.Log("[ERROR]", strArray4); } } } catch (BadImageFormatException) { } catch (Exception exception7) { string[] strArray3 = new string[] { "Failed to load assembly ", list[num], Environment.NewLine, exception7.ToString() }; GTA.Log("[ERROR]", strArray3); } num++; } return(domain2); }
public static ScriptDomain Load(string path) { if (!Path.IsPathRooted(path)) { path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), path); } // Create AppDomain var setup = new AppDomainSetup(); setup.ApplicationBase = path; setup.ShadowCopyFiles = "true"; setup.ShadowCopyDirectories = path; var appdomain = AppDomain.CreateDomain("ScriptDomain_" + (path.GetHashCode() * Environment.TickCount).ToString("X"), null, setup, new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted)); appdomain.InitializeLifetimeService(); Log("[DEBUG]", "Path ", path); ScriptDomain scriptdomain = null; try { scriptdomain = (ScriptDomain)(appdomain.CreateInstanceFromAndUnwrap(typeof(ScriptDomain).Assembly.Location, typeof(ScriptDomain).FullName)); } catch (Exception ex) { Log("[ERROR]", "Failed to create script domain':", Environment.NewLine, ex.ToString()); AppDomain.Unload(appdomain); return(null); } Log("[INFO]", "Loading scripts from '", path, "' ..."); if (Directory.Exists(path)) { var filenameScripts = new List <string>(); var filenameAssemblies = new List <string>(); try { filenameScripts.AddRange(Directory.GetFiles(path, "*.vb", SearchOption.AllDirectories)); filenameScripts.AddRange(Directory.GetFiles(path, "*.cs", SearchOption.AllDirectories)); filenameAssemblies.AddRange(Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories)); } catch (Exception ex) { Log("[ERROR]", "Failed to reload scripts:", Environment.NewLine, ex.ToString()); AppDomain.Unload(appdomain); return(null); } for (int i = 0; i < filenameAssemblies.Count; i++) { var filename = filenameAssemblies[i]; var assemblyName = AssemblyName.GetAssemblyName(filename); try { if (AssemblyName.GetAssemblyName(filename).Name.StartsWith("ScriptHookVDotNet", StringComparison.OrdinalIgnoreCase)) { Log("[WARNING]", "Removing assembly file '", Path.GetFileName(filename), "'."); filenameAssemblies.RemoveAt(i--); try { File.Delete(filename); } catch (Exception ex) { Log("[ERROR]", "Failed to delete assembly file:", Environment.NewLine, ex.ToString()); } } } catch (Exception ex) { Log("[ERROR]", "Failed to load assembly file '", Path.GetFileName(filename), "':", Environment.NewLine, ex.ToString()); } } foreach (string filename in filenameScripts) { scriptdomain.LoadScript(filename); } foreach (string filename in filenameAssemblies) { scriptdomain.LoadAssembly(filename); } } else { Log("[ERROR]", "Failed to reload scripts because the directory is missing."); } return(scriptdomain); }
public Script() { Filename = ScriptDomain.CurrentDomain.LookupScriptFilename(this); _scriptdomain = ScriptDomain.CurrentDomain; }