/// <summary> /// Method to use for running Javascript Files. /// </summary> /// <param name="filePath">Path to JS or JSE File</param> public void RunScript(object filePath) { try { /* CODIGO JINTENGINE VIEJO Engine engine = new Engine(cfg => cfg.AllowClr(typeof(NinoJS.Resources.SQL).Assembly)) .SetValue("log", new Action<object>(Console.WriteLine)); engine.Execute(fileContent); */ string fileContent = new StreamReader(filePath.ToString()).ReadToEnd(); var engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging); var typeCollection = new HostTypeCollection("mscorlib", "System", "System.Core", "NinoJS.Resources"); engine.AddHostObject("clr", typeCollection); engine.AddHostObject("host", new HostFunctions()); engine.AddHostObject("extendedHost", new ExtendedHostFunctions()); engine.AddHostObject("console", new NinoJS.Core.Helpers.Console()); engine.Execute(fileContent); } catch (Exception ex) { System.Console.WriteLine(ex.ToString()); log.LogError(ex.ToString()); } }
/// <summary> /// Carregar plugins /// </summary> public static void LoadPlugins() { if (!Directory.Exists(Constants.PluginJS.PathPluginJS) || !Constants.PluginJS.AllowPluginJS) { return; } Engines.EnginePlugins.AddHostObject("RegisterFunc", new Action<Object, int>(RegisterFunc)); bool ErroPlugins = false; string[] FilesJS = Directory.GetFiles(Constants.PluginJS.PathPluginJS, "*.js", SearchOption.AllDirectories); for (int i = 0; i < FilesJS.Length; i++) { CurrentFile = FilesJS[i]; string Script = File.ReadAllText(FilesJS[i]); try { Engines.EnginePlugins.Execute(Script); Engines.EnginePlugins.Script.Register(); } catch { ErroPlugins = true; ConsoleConstants.WriteInConsole( "Falha ao carregar um plugin\nNome:" + FilesJS[i], Color.DarkRed); } } if (!ErroPlugins) ConsoleConstants.WriteInConsole("Plugins em JavaScript, carregados com sucesso", Color.DarkGreen); HostTypeCollection LibsExport = new HostTypeCollection("mscorlib", "System", "System.Core", "GLResourceModule"); Engines.EnginePlugins.AddHostObject("clr", LibsExport); Engines.EnginePlugins.AddHostObject("MessageBox", new Func<string, string, DialogResult>(MessageBox.Show)); }
/// <summary> /// Carregar plugins seguros, recebidos diretamente do servidor /// </summary> /// <param name="SafePlugins">Scripts</param> /// <param name="LibsImport">Libs para exportar para os scripts</param> public static void LoadSafePlugins(string[] SafePlugins, string[] LibsImport) { if (SafePlugins.Length == 0 || !Constants.PluginJS.AllowSafePlugin) return; if (LibsImport.Length > 0) { HostTypeCollection Libs = new HostTypeCollection(LibsImport); Engines.EngineSafePlugins.AddHostObject("clr", Libs); } Boolean ErroPlugins = false; Engines.EngineSafePlugins.AddHostObject("RegisterFunc", new Action<Object, int>(RegisterSafeFunc)); for (int i = 0; i < SafePlugins.Length; i++) { string CurrentScript = SafePlugins[i]; try { Engines.EngineSafePlugins.Execute(CurrentScript); Engines.EngineSafePlugins.Script.Register(); } catch (Exception ex) { ErroPlugins = true; ConsoleConstants.WriteInConsole( "Falha ao carregar um SafePlugin\nErro:" + ex.Message, Color.DarkRed); } } if (!ErroPlugins) ConsoleConstants.WriteInConsole("SafePlugins carregados com sucesso", Color.DarkGreen); }
public static void StartScript(string script, List<int> identEnts) { var scriptEngine = new JScriptEngine(); var collection = new HostTypeCollection(Assembly.LoadFrom("scripthookvdotnet.dll"), Assembly.LoadFrom("scripts\\NativeUI.dll")); scriptEngine.AddHostObject("API", collection); scriptEngine.AddHostObject("host", new HostFunctions()); scriptEngine.AddHostObject("script", new ScriptContext()); scriptEngine.AddHostType("Enumerable", typeof(Enumerable)); scriptEngine.AddHostType("List", typeof(IList)); scriptEngine.AddHostType("KeyEventArgs", typeof(KeyEventArgs)); scriptEngine.AddHostType("Keys", typeof(Keys)); foreach (var obj in identEnts) { var name = PropStreamer.Identifications[obj]; if (MapEditor.IsPed(new Prop(obj))) scriptEngine.AddHostObject(name, new Ped(obj)); else if (MapEditor.IsVehicle(new Prop(obj))) scriptEngine.AddHostObject(name, new Vehicle(obj)); else if (MapEditor.IsProp(new Prop(obj))) scriptEngine.AddHostObject(name, new Prop(obj)); } try { scriptEngine.Execute(script); } catch (ScriptEngineException ex) { LogException(ex); } finally { lock (ScriptEngines) ScriptEngines.Add(scriptEngine); } }