Example #1
0
        public static List <SFFile> CollectFiles(string mainfile)
        {
            List <SFFile>    files       = new List <SFFile>();
            HashSet <string> filesLoaded = new HashSet <string>();
            Stack <string>   filesToLoad = new Stack <string>();

            filesToLoad.Push(mainfile);

            HashSet <string> directoriesLoaded = new HashSet <string>();
            Stack <string>   directoriesToLoad = new Stack <string>();

            while (filesToLoad.Count > 0)
            {
                string filename = filesToLoad.Pop();
                if (filesLoaded.Add(filename))
                {
                    try
                    {
                        SFFile file = new SFFile(filename);
                        file.directives.includes.ForEach(( string n ) => filesToLoad.Push(n));
                        file.directives.includedirs.ForEach(( string n ) => directoriesToLoad.Push(n));
                        files.Add(file);
                    }
                    catch (Exception e)
                    {
                        throw new Exception("Failed to load file: " + filename + " (" + e.Message + ")", e);
                    }
                }

                while (directoriesToLoad.Count > 0)
                {
                    string dir = directoriesToLoad.Pop();
                    if (directoriesLoaded.Add(dir))
                    {
                        foreach (string f in FileSystem.Mounted.FindFile(dir))
                        {
                            filesToLoad.Push(f);
                        }
                    }
                }
            }
            return(files);
        }
Example #2
0
        public void Compile()
        {
            L = Lua.lua_open();
            Lua.lua_gc(L, Lua.LUA_GCSTOP, 0);
            Lua.luaL_openlibs(L);
            Lua.lua_gc(L, Lua.LUA_GCRESTART, 0);

            Lua.lua_sethook(L, (Lua.lua_State L, Lua.lua_Debug ar) => { this.cpuTimeCheck(); }, Lua.LUA_MASKCOUNT, 200);

            // Compile all the files and store in _G.scripts
            Lua.lua_createtable(L, 0, files.Count);
            foreach (SFFile file in files)
            {
                switch (file.directives.realm)
                {
                case "Server":
                    if (!Host.IsServer)
                    {
                        continue;
                    }
                    break;

                case "Client":
                    if (!Host.IsClient)
                    {
                        continue;
                    }
                    break;
                }

                Lua.lua_pushstring(L, file.filename);
                if (Lua.luaL_loadbuffer(L, file.code, "SF: " + file.filename) != 0)
                {
                    string err = Lua.lua_tostring(L, -1);
                    if (err is null)
                    {
                        err = "Error not a string";
                    }
                    Lua.lua_settop(L, 0);                       // Clear the stack

                    throw new StarfallException(err, "");
                }
                Lua.lua_settable(L, 1);
            }
            Lua.lua_setglobal(L, "_SCRIPTS");

            Lua.lua_pushcfunction(L, (Lua.lua_State L) => { Lua.luaL_getmetatable(L, Lua.luaL_checkstring(L, 1)); return(1); });
            Lua.lua_setglobal(L, "getMetatable");

            SFFile main = files[0];

            if (Host.IsClient)
            {
                if (!string.IsNullOrEmpty(main.directives.scriptclientmain))
                {
                    main = files.Find(( SFFile f ) => f.filename == main.directives.scriptclientmain);
                    if (main is null)
                    {
                        throw new StarfallException("Couldn't load clientmain: " + main.directives.scriptclientmain, "");
                    }
                }
            }
            // Call mainfile
            Lua.lua_pushcfunction(L, Lua.db_errorfb);
            Lua.lua_getglobal(L, "require");
            Lua.lua_pushstring(L, main.filename);
            if (Lua.lua_pcall(L, 1, 0, -3) != 0)
            {
                string err = Lua.lua_tostring(L, -1);
                if (err is null)
                {
                    err = "Error not a string";
                }
                Lua.lua_settop(L, 0);                   // Clear the stack
                throw new StarfallException(err, "");
            }
            Lua.lua_settop(L, 0);               // Clear the stack
        }