コード例 #1
0
        public bool LoadMods(string modPath)
        {
            var errors = false;

            var mainModFiles = Directory.EnumerateFiles(modPath, "main.lua", SearchOption.AllDirectories);

            if (!mainModFiles.Any())
            {
                return(false);
            }

            Trace.WriteLine("Mods found: " + mainModFiles.Count());

            foreach (var filePath in mainModFiles)
            {
                var folderPath = (Path.GetDirectoryName(filePath));
                var folderName = Path.GetFileName(folderPath);

                _cwd = Path.GetFullPath(folderPath);
                this.ModCount++;

                Trace.WriteLine(string.Format("Loading '{0}'...", folderName));

                if (Melua.luaL_dofile(L, filePath) != 0)
                {
                    Trace.WriteLine(string.Format("Error in {1}", folderName, Melua.lua_tostring(L, -1)));
                    errors = true;
                }
            }

            return(errors);
        }
コード例 #2
0
        private int DoPath(IntPtr L, string path, bool errorOnMissing)
        {
            var fullPath = path;
            var isHttp   = (path.StartsWith("http://") || path.StartsWith("https://"));
            var status   = 0;

            if (isHttp)
            {
                try
                {
                    var wc     = new WebClient();
                    var script = wc.DownloadString(fullPath);

                    status = Melua.luaL_dostring(L, script);
                }
                catch (WebException ex)
                {
                    if (errorOnMissing)
                    {
                        return(Melua.melua_error(L, "Failed to include remote script '{0}' ({1}).", fullPath, ex.Message));
                    }
                }
            }
            else
            {
                fullPath = Path.GetFullPath(Path.Combine(_cwd, path));

                if (!IsInsideCwd(fullPath))
                {
                    return(Melua.melua_error(L, "Invalid path. ({0})", path));
                }

                if (!File.Exists(fullPath))
                {
                    if (errorOnMissing)
                    {
                        return(Melua.melua_error(L, "File not found. ({0})", path));
                    }
                    else
                    {
                        return(0);
                    }
                }

                status = Melua.luaL_dofile(L, fullPath);
            }

            if (status == 1)             // Error in do/load
            {
                return(Melua.melua_error(L, Melua.lua_tostring(L, -1)));
            }

            if (status != 0)             // Error in execution
            {
                return(status);
            }

            var returnValues = Melua.lua_gettop(L);

            return(returnValues);
        }