예제 #1
0
        public override JsObject Construct(JsInstance[] parameters, Type[] genericArgs, IJintVisitor visitor)
        {
            JsFunction instance = New();

            instance.Arguments = new List <string>();

            for (int i = 0; i < parameters.Length - 1; i++)
            {
                string arg = parameters[i].ToString();

                foreach (string a in arg.Split(','))
                {
                    instance.Arguments.Add(a.Trim());
                }
            }

            if (parameters.Length >= 1)
            {
                Program p = JintEngine.Compile(parameters[parameters.Length - 1].Value.ToString(), visitor.DebugMode);
                instance.Statement = new BlockStatement()
                {
                    Statements = p.Statements
                };
            }

            return(instance);
        }
예제 #2
0
        /// <summary>
        /// 15.1.2.1
        /// </summary>
        public JsInstance Eval(JsInstance[] arguments)
        {
            if (JsInstance.CLASS_STRING != arguments[0].Class)
            {
                return(arguments[0]);
            }

            Program p;

            try {
                p = JintEngine.Compile(arguments[0].ToString(), Visitor.DebugMode);
            }
            catch (Exception e) {
                throw new JsException(this.SyntaxErrorClass.New(e.Message));
            }

            try {
                p.Accept((IStatementVisitor)Visitor);
            }
            catch (Exception e) {
                throw new JsException(this.EvalErrorClass.New(e.Message));
            }

            return(Visitor.Result);
        }
예제 #3
0
        public override JsObject Construct(
            JsInstance[] parameters,
            Type[] genericArgs,
            IJintVisitor visitor)
        {
            JsFunction jsFunction = this.New();

            jsFunction.Arguments = new List <string>();
            for (int index = 0; index < parameters.Length - 1; ++index)
            {
                string str1    = parameters[index].ToString();
                char[] chArray = new char[1] {
                    ','
                };
                foreach (string str2 in str1.Split(chArray))
                {
                    jsFunction.Arguments.Add(str2.Trim());
                }
            }
            if (parameters.Length >= 1)
            {
                Program program = JintEngine.Compile(parameters[parameters.Length - 1].Value.ToString(), visitor.DebugMode);
                jsFunction.Statement = (Statement) new BlockStatement()
                {
                    Statements = program.Statements
                };
            }
            return((JsObject)jsFunction);
        }
예제 #4
0
        public override void Load(string code)
        {
            Engine = new JintEngine(Options.Ecmascript5)
                     .AllowClr(true);

            Engine.SetParameter("Commands", chatCommands)
            .SetParameter("DataStore", DataStore.GetInstance())
            .SetParameter("Find", Find.GetInstance())
            .SetParameter("GlobalData", GlobalData)
            .SetParameter("Plugin", this)
            .SetParameter("Server", Server.GetInstance())
            .SetParameter("ServerConsoleCommands", consoleCommands)
            .SetParameter("Util", Util.GetInstance())
            .SetParameter("Web", Web)
            .SetParameter("World", World.GetInstance())
            .SetFunction("importClass", new importit(importClass));

            try {
                Program = JintEngine.Compile(code, false);

                Globals = (from statement in Program.Statements
                           where statement.GetType() == typeof(FunctionDeclarationStatement)
                           select((FunctionDeclarationStatement)statement).Name).ToList <string>();

                Engine.Run(Program);
                State = PluginState.Loaded;
            } catch (Exception ex) {
                Logger.LogException(ex);
                State = PluginState.FailedToLoad;
            }

            PluginLoader.GetInstance().OnPluginLoaded(this);
        }
예제 #5
0
        public JsInstance Eval(JsInstance[] arguments)
        {
            if ("String" != arguments[0].Class)
            {
                return(arguments[0]);
            }
            Program program;

            try
            {
                program = JintEngine.Compile(arguments[0].ToString(), this.Visitor.DebugMode);
            }
            catch (Exception ex)
            {
                throw new JsException((JsInstance)this.SyntaxErrorClass.New(ex.Message));
            }
            try
            {
                program.Accept((IStatementVisitor)this.Visitor);
            }
            catch (Exception ex)
            {
                throw new JsException((JsInstance)this.EvalErrorClass.New(ex.Message));
            }
            return(this.Visitor.Result);
        }
예제 #6
0
 public IEnumerable <FunctionDeclarationStatement> GetSourceCodeGlobalFunctions()
 {
     foreach (Statement statement in JintEngine.Compile(Code, false).Statements)
     {
         if (statement.GetType() == typeof(FunctionDeclarationStatement))
         {
             FunctionDeclarationStatement funcDecl = (FunctionDeclarationStatement)statement;
             if (funcDecl != null)
             {
                 yield return(funcDecl);
             }
         }
     }
 }
예제 #7
0
        public override void Load(string code = "")
        {
            try
            {
                Engine = new JintEngine(Options.Ecmascript5)
                         .AllowClr(true);

                Engine.SetParameter("Plugin", this)
                .SetParameter("Server", Fougerite.Server.GetServer())
                .SetParameter("DataStore", DataStore.GetInstance())
                .SetParameter("Data", Data.GetData())
                .SetParameter("Web", new Fougerite.Web())
                .SetParameter("Util", Util.GetUtil())
                .SetParameter("World", World.GetWorld())
                    #pragma warning disable 618
                .SetParameter("PluginCollector", GlobalPluginCollector.GetPluginCollector())
                    #pragma warning restore 618
                .SetParameter("Loom", Fougerite.Loom.Current)
                .SetParameter("JSON", Fougerite.JsonAPI.GetInstance)
                .SetParameter("MySQL", Fougerite.MySQLConnector.GetInstance)
                .SetParameter("SQLite", Fougerite.SQLiteConnector.GetInstance)
                .SetFunction("importClass", new importit(importClass));
                Program = JintEngine.Compile(code, false);

                Globals = (from statement in Program.Statements
                           where statement.GetType() == typeof(FunctionDeclarationStatement)
                           select((FunctionDeclarationStatement)statement).Name).ToList <string>();

                Engine.Run(Program);

                object author  = GetGlobalObject("Author");
                object about   = GetGlobalObject("About");
                object version = GetGlobalObject("Version");
                Author  = author == null || (string)author == "undefined" ? "Unknown" : author.ToString();
                About   = about == null || (string)about == "undefined" ? "" : about.ToString();
                Version = version == null || (string)version == "undefined" ? "1.0" : version.ToString();

                State = PluginState.Loaded;
            }
            catch (Exception ex)
            {
                Logger.LogError("[Error] Failed to load lua plugin: " + ex);
                State = PluginState.FailedToLoad;
                PluginLoader.GetInstance().CurrentlyLoadingPlugins.Remove(Name);
            }

            PluginLoader.GetInstance().OnPluginLoaded(this);
        }
예제 #8
0
        public override void Load(string code)
        {
            try {
                if (CoreConfig.GetInstance().GetBoolValue("javascript", "checkHash") && !code.VerifyMD5Hash())
                {
                    Logger.LogDebug($"[{GetType().Name}] MD5Hash not found for: {Name}");
                    State = PluginState.HashNotFound;
                }
                else
                {
                    Engine = new JintEngine(Options.Ecmascript5)
                             .AllowClr(true);

                    Engine.SetParameter("DataStore", DataStore.GetInstance())
                    .SetParameter("GlobalData", GlobalData)
                    .SetParameter("Plugin", this)
                    .SetParameter("Util", Util.GetInstance())
                    .SetParameter("Web", Web)
                    .SetFunction("importClass", new importit(importClass));

                    AssignVariables();

                    Program = JintEngine.Compile(code, false);

                    Globals = (from statement in Program.Statements
                               where statement.GetType() == typeof(FunctionDeclarationStatement)
                               select((FunctionDeclarationStatement)statement).Name).ToList();

                    Engine.Run(Program);

                    object author  = GetGlobalObject("Author");
                    object about   = GetGlobalObject("About");
                    object version = GetGlobalObject("Version");
                    Author  = author == null ? "" : author.ToString();
                    About   = about == null ? "" : about.ToString();
                    Version = version == null ? "" : version.ToString();

                    State = PluginState.Loaded;
                }
            } catch (Exception ex) {
                Logger.LogException(ex);
                State = PluginState.FailedToLoad;
            }

            PluginLoader.GetInstance().OnPluginLoaded(this);
        }
예제 #9
0
 public static Statement jint_Compile(this string javascriptCode)
 {
     try
     {
         var compiled = JintEngine.Compile(javascriptCode, true);
         if (compiled.statements(true).size() < 2)
         {
             "in jint_Compile there was no AST created, so returning null".debug();
             return(null);
         }
         return(compiled);
     }
     catch (Exception ex)
     {
         ex.log("in Jint_ExtensionMethods.jint_Compile(...)");
         return(null);
     }
 }
예제 #10
0
        public override void Load(string code = "")
        {
            try {
                Engine = new JintEngine(Options.Ecmascript5)
                         .AllowClr(true);

                Engine.SetParameter("Commands", chatCommands)
                .SetParameter("DataStore", DataStore.GetInstance())
                .SetParameter("Find", Find.GetInstance())
                .SetParameter("GlobalData", GlobalData)
                .SetParameter("Plugin", this)
                .SetParameter("Server", Server.GetInstance())
                .SetParameter("ServerConsoleCommands", consoleCommands)
                .SetParameter("Util", Util.GetInstance())
                .SetParameter("Web", Web)
                .SetParameter("World", World.GetInstance())
                .SetFunction("importClass", new importit(importClass));
                Program = JintEngine.Compile(code, false);

                Globals = (from statement in Program.Statements
                           where statement.GetType() == typeof(FunctionDeclarationStatement)
                           select((FunctionDeclarationStatement)statement).Name).ToList <string>();

                Engine.Run(Program);

                object author  = GetGlobalObject("Author");
                object about   = GetGlobalObject("About");
                object version = GetGlobalObject("Version");
                Author  = author == null ? "" : author.ToString();
                About   = about == null ? "" : about.ToString();
                Version = version == null ? "" : version.ToString();

                State = PluginState.Loaded;
            } catch (Exception ex) {
                Logger.LogException(ex);
                State = PluginState.FailedToLoad;
                PluginLoader.GetInstance().CurrentlyLoadingPlugins.Remove(Name);
            }

            PluginLoader.GetInstance().OnPluginLoaded(this);
        }
예제 #11
0
        // Internal method for evaluating a script from within a script!
        private string EvalScript(string scriptName, string script)
        {
            Program p;

            try {
                p = JintEngine.Compile(script, Visitor.DebugMode);
            } catch (Exception e) {
                string m = "In script " + scriptName + ": ";
                throw new JsException(Visitor.Global, Visitor.Global.SyntaxErrorClass.New(m + e.Message));
            }
            try {
                p.Accept((IStatementVisitor)Visitor);
            } catch (Exception e) {
                throw new JsException(Visitor.Global, Visitor.Global.EvalErrorClass.New(e.Message));
            }
            if (Visitor.Result == null)
            {
                return("");
            }
            return(Visitor.Result.ToString());
        }
예제 #12
0
        public static bool IsJavaScript(string Text)
        {
            string TrimmedText = Text.Trim();

            if (TrimmedText.StartsWith("<") || TrimmedText.StartsWith("{") || TrimmedText.StartsWith("[") || TrimmedText.EndsWith(">"))
            {
                return(false);
            }
            if (!(TrimmedText.Contains("=") || TrimmedText.Contains(";") || TrimmedText.Contains("(") || TrimmedText.Contains(")")))
            {
                return(false);
            }

            try
            {
                Jint.Expressions.Program Prog = JintEngine.Compile(Text, false);
                if (Prog.Statements.Count == 0)
                {
                    return(false);
                }
            }
            catch { return(false); }
            return(true);
        }
예제 #13
0
 public override void Compile()
 {
     this.compiledCode = JintEngine.Compile(SourceCode, false);
 }
 public void LoadPlugins(Player p)
 {
     Hooks.ResetHooks();
     this.ParsePlugin();
     foreach (Plugin plugin in this.plugins)
     {
         try
         {
             this.interpreter.Run(plugin.Code);
             foreach (Statement current in JintEngine.Compile(plugin.Code, false).Statements)
             {
                 if (current.GetType() == typeof(FunctionDeclarationStatement))
                 {
                     FunctionDeclarationStatement functionDeclarationStatement = (FunctionDeclarationStatement)current;
                     if (functionDeclarationStatement != null)
                     {
                         if (functionDeclarationStatement.Name == "On_ServerInit")
                         {
                             Hooks.OnServerInit += new Hooks.ServerInitDelegate(plugin.OnServerInit);
                         }
                         else if (functionDeclarationStatement.Name == "On_PluginInit")
                         {
                             Hooks.OnPluginInit += new Hooks.PluginInitHandlerDelegate(plugin.OnPluginInit);
                         }
                         else if (functionDeclarationStatement.Name == "On_ServerShutdown")
                         {
                             Hooks.OnServerShutdown += new Hooks.ServerShutdownDelegate(plugin.OnServerShutdown);
                         }
                         else if (functionDeclarationStatement.Name == "On_ItemsLoaded")
                         {
                             Hooks.OnItemsLoaded += new Hooks.ItemsDatablocksLoaded(plugin.OnItemsLoaded);
                         }
                         else if (functionDeclarationStatement.Name == "On_TablesLoaded")
                         {
                             Hooks.OnTablesLoaded += new Hooks.LootTablesLoaded(plugin.OnTablesLoaded);
                         }
                         else if (functionDeclarationStatement.Name == "On_Chat")
                         {
                             Hooks.OnChat += new Hooks.ChatHandlerDelegate(plugin.OnChat);
                         }
                         else if (functionDeclarationStatement.Name == "On_Console")
                         {
                             Hooks.OnConsoleReceived += new Hooks.ConsoleHandlerDelegate(plugin.OnConsole);
                         }
                         else if (functionDeclarationStatement.Name == "On_Command")
                         {
                             Hooks.OnCommand += new Hooks.CommandHandlerDelegate(plugin.OnCommand);
                         }
                         else if (functionDeclarationStatement.Name == "On_PlayerConnected")
                         {
                             Hooks.OnPlayerConnected += new Hooks.ConnectionHandlerDelegate(plugin.OnPlayerConnected);
                         }
                         else if (functionDeclarationStatement.Name == "On_PlayerDisconnected")
                         {
                             Hooks.OnPlayerDisconnected += new Hooks.DisconnectionHandlerDelegate(plugin.OnPlayerDisconnected);
                         }
                         else if (functionDeclarationStatement.Name == "On_PlayerKilled")
                         {
                             Hooks.OnPlayerKilled += new Hooks.KillHandlerDelegate(plugin.OnPlayerKilled);
                         }
                         else if (functionDeclarationStatement.Name == "On_PlayerHurt")
                         {
                             Hooks.OnPlayerHurt += new Hooks.HurtHandlerDelegate(plugin.OnPlayerHurt);
                         }
                         else if (functionDeclarationStatement.Name == "On_PlayerSpawning")
                         {
                             Hooks.OnPlayerSpawning += new Hooks.PlayerSpawnHandlerDelegate(plugin.OnPlayerSpawn);
                         }
                         else if (functionDeclarationStatement.Name == "On_PlayerSpawned")
                         {
                             Hooks.OnPlayerSpawned += new Hooks.PlayerSpawnHandlerDelegate(plugin.OnPlayerSpawned);
                         }
                         else if (functionDeclarationStatement.Name == "On_PlayerGathering")
                         {
                             Hooks.OnPlayerGathering += new Hooks.PlayerGatheringHandlerDelegate(plugin.OnPlayerGathering);
                         }
                         else if (functionDeclarationStatement.Name == "On_EntityHurt")
                         {
                             Hooks.OnEntityHurt += new Hooks.EntityHurtDelegate(plugin.OnEntityHurt);
                         }
                         else if (functionDeclarationStatement.Name == "On_EntityDecay")
                         {
                             Hooks.OnEntityDecay += new Hooks.EntityDecayDelegate(plugin.OnEntityDecay);
                         }
                         else if (functionDeclarationStatement.Name == "On_EntityDeployed")
                         {
                             Hooks.OnEntityDeployed += new Hooks.EntityDeployedDelegate(plugin.OnEntityDeployed);
                         }
                         else if (functionDeclarationStatement.Name == "On_NPCHurt")
                         {
                             Hooks.OnNPCHurt += new Hooks.HurtHandlerDelegate(plugin.OnNPCHurt);
                         }
                         else if (functionDeclarationStatement.Name == "On_NPCKilled")
                         {
                             Hooks.OnNPCKilled += new Hooks.KillHandlerDelegate(plugin.OnNPCKilled);
                         }
                         else if (functionDeclarationStatement.Name == "On_BlueprintUse")
                         {
                             Hooks.OnBlueprintUse += new Hooks.BlueprintUseHandlerDelagate(plugin.OnBlueprintUse);
                         }
                         else if (functionDeclarationStatement.Name == "On_DoorUse")
                         {
                             Hooks.OnDoorUse += new Hooks.DoorOpenHandlerDelegate(plugin.OnDoorUse);
                         }
                     }
                 }
             }
         }
         catch (Exception)
         {
             string arg = "Can't load plugin : " + plugin.Path.Remove(0, plugin.Path.LastIndexOf("\\") + 1);
             if (p != null)
             {
                 p.Message(arg);
             }
             else
             {
                 Server.GetServer().Broadcast(arg);
             }
         }
     }
 }
예제 #15
0
        /// <summary>
        /// Loads a script file to the current execution context
        /// </summary>
        public static bool LoadScriptFile(string scriptFile)
        {
            bool loaded = false;

            try
            {
                //work out the full path of the script file to be loaded
                if (!Path.IsPathRooted(scriptFile))
                {
                    string basePath;
                    if (scriptFile.StartsWith("~"))
                    {
                        //relative to scripts path
                        basePath   = ScriptsPath;
                        scriptFile = scriptFile.Substring(1);
                    }
                    else
                    {
                        //relative to current script folder
                        basePath = Path.GetDirectoryName(scriptFile);
                    }

                    while (scriptFile.StartsWith(@"\"))
                    {
                        scriptFile = scriptFile.Substring(1);
                    }
                    scriptFile = Path.GetFullPath(Path.Combine(basePath, scriptFile));
                }


                //check that script file is not already loaded
                if (!loadedScripts.Contains(scriptFile, StringComparer.OrdinalIgnoreCase))
                {
                    //check that script exists
                    if (File.Exists(scriptFile))
                    {
                        //load file into the current ExecutionVisitor
                        string script = File.ReadAllText(scriptFile);

                        //load into current execution visitor
                        Program program = JintEngine.Compile(script, false);
                        foreach (var statement in program.ReorderStatements())
                        {
                            statement.Accept(m_Instance.visitor);
                        }

                        //add to scripts collection to stop reloading
                        loadedScripts.Add(scriptFile);
                        loaded = true;
                    }
                    else
                    {
                        m_Logger.WarnFormat("The script file to append could not be found at {0}", scriptFile);
                    }
                }
                else
                {
                    //alread loaded
                    loaded = true;
                }
            }
            catch (Exception exp)
            {
                m_Logger.WarnFormat("An error occurred appending the script {0} in the script engine: {1}", scriptFile, exp.Message);
            }

            return(loaded);
        }