Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1. /**
  2.  * The Forgotten Server - a free and open-source MMORPG server emulator
  3.  * Copyright (C) 2019  Mark Samman <mark.samman@gmail.com>
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License along
  16.  * with this program; if not, write to the Free Software Foundation, Inc.,
  17.  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18.  */
  19.  
  20. #include "otpch.h"
  21.  
  22. #include "script.h"
  23. #include <boost/filesystem.hpp>
  24. #include "configmanager.h"
  25.  
  26. extern LuaEnvironment g_luaEnvironment;
  27. extern ConfigManager g_config;
  28.  
  29. Scripts::Scripts() :
  30.     scriptInterface("Scripts Interface")
  31. {
  32.     scriptInterface.initState();
  33. }
  34.  
  35. Scripts::~Scripts()
  36. {
  37.     scriptInterface.reInitState();
  38. }
  39.  
  40. bool Scripts::loadScripts(std::string folderName, bool isLib, bool reload)
  41. {
  42.     namespace fs = boost::filesystem;
  43.  
  44.     const auto dir = fs::current_path() / "data" / folderName;
  45.     if(!fs::exists(dir) || !fs::is_directory(dir)) {
  46.         std::cout << "[Warning - Scripts::loadScripts] Can not load folder '" << folderName << "'." << std::endl;
  47.         return false;
  48.     }
  49.  
  50.     fs::recursive_directory_iterator endit;
  51.     std::vector<fs::path> v;
  52.     std::string disable = ("#");
  53.     std::string noShow = ("!");
  54.     for(fs::recursive_directory_iterator it(dir); it != endit; ++it) {
  55.         auto fn = it->path().parent_path().filename();
  56.         if ((fn == "lib" && !isLib) || fn == "events") {
  57.             continue;
  58.         }
  59.         if(fs::is_regular_file(*it) && it->path().extension() == ".lua") {
  60.             size_t found = it->path().filename().string().find(disable);
  61.             size_t found2 = it->path().filename().string().find(noShow);
  62.             if (found != std::string::npos) {
  63.                 if (g_config.getBoolean(ConfigManager::SCRIPTS_CONSOLE_LOGS) && found2 != std::string::npos) {
  64.                     std::cout << "> " << it->path().filename().string() << " [disabled]" << std::endl;
  65.                 }
  66.                 continue;
  67.             }
  68.             v.push_back(it->path());
  69.         }
  70.     }
  71.     sort(v.begin(), v.end());
  72.     std::string redir;
  73.     for (auto it = v.begin(); it != v.end(); ++it) {
  74.         const std::string scriptFile = it->string();
  75.         if (!isLib) {
  76.             if (redir.empty() || redir != it->parent_path().string()) {
  77.                 auto p = fs::path(it->relative_path());
  78.                 if (g_config.getBoolean(ConfigManager::SCRIPTS_CONSOLE_LOGS)) {
  79.                     std::cout << ">> [" << p.parent_path().filename() << "]" << std::endl;
  80.                 }
  81.                 redir = it->parent_path().string();
  82.             }
  83.         }
  84.  
  85.         if(scriptInterface.loadFile(scriptFile) == -1) {
  86.             std::cout << "> " << it->filename().string() << " [error]" << std::endl;
  87.             std::cout << "^ " << scriptInterface.getLastLuaError() << std::endl;
  88.             continue;
  89.         }
  90.  
  91.         if (g_config.getBoolean(ConfigManager::SCRIPTS_CONSOLE_LOGS)) {
  92.             if (!reload) {
  93.                 std::cout << "> " << it->filename().string() << " [loaded]" << std::endl;
  94.             } else {
  95.                 std::cout << "> " << it->filename().string() << " [reloaded]" << std::endl;
  96.             }
  97.         }
  98.     }
  99.  
  100.     return true;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement