Пример #1
0
void CTemplateManager::GetTemplateList( CTemplateList* pList, const String_256& strTemplatePath, bool fLocal )
{
	// Don't bother with any of this is directory is invalid
	if( !wxDir::Exists( (PCTSTR)strTemplatePath ) )
		return;

	//Now search that path for templates
	//Start by setting the leaf name to *.xar
	String_256			strSearchFilename( _R(IDS_NEWTEMPLATES_DEFAULTTEMPLATEEXTENSION) );

	// Get the default entry names
	String_256			strPathOfDrawingTemplate   = CTemplateManager::GetDefaultDrawingTemplate().GetPath(FALSE);
	strPathOfDrawingTemplate.SwapChar( _T('_'), _T(' ') );
	String_256			strPathOfAnimationTemplate = CTemplateManager::GetDefaultAnimationTemplate().GetPath(FALSE);
	strPathOfAnimationTemplate.SwapChar( _T('_'), _T(' ') );
	String_256			strPathOfFile;

	TRACEUSER( "jlh92", _T("DefPath = %s, %s\n"), PCTSTR(strPathOfDrawingTemplate),
		PCTSTR(strPathOfAnimationTemplate) );

	// Build system template path
	PathName			pathTemplates( strTemplatePath );
	pathTemplates.SetFileNameAndType( strSearchFilename );
	PathName			pathOfFile( pathTemplates );
	String_256			strTemplates = pathTemplates.GetPath( FALSE );

	//And search the path for xar files that are
	//NOT the default animation or drawing templates
	String_256			strNameOfFile;
	if( FileUtil::StartFindingFiles( &strTemplates ) )
	{
		while( FileUtil::FindNextFile( &strNameOfFile ) )
		{
			pathOfFile.SetFileNameAndType( strNameOfFile );
			strPathOfFile = pathOfFile.GetFileName(TRUE);

			if( 0 != strPathOfFile.CompareTo( strPathOfDrawingTemplate, FALSE ) &&
				0 != strPathOfFile.CompareTo( strPathOfAnimationTemplate, FALSE ) )
			{
				(*pList)[strPathOfFile] = fLocal;
				TRACEUSER( "jlh92", _T("Curr = %s\n"), PCTSTR(strPathOfFile) );
			}
		}
		FileUtil::StopFindingFiles();
	}

	// Don't allow any errors set while searching to propagate outside this scope
	Error::ClearError();
}
Пример #2
0
int main(int argc, char* argv[])
{
  signal(SIGTERM, sighandler);
  signal(SIGINT, sighandler);

#ifndef WIN32
  signal(SIGPIPE, pipehandler);
#else
  signal(SIGBREAK, sighandler);
#endif

  std::srand((uint32_t)std::time(NULL));
  initPRNG();

  std::string cfg;
  std::vector<std::string> overrides;

  for (int i = 1; i < argc; i++)
  {
    const std::string arg(argv[i]);

    switch (arg[0])
    {
    case '-':   // option
      // we have only '-h' and '--help' now, so just return with help
      return printHelp(EXIT_SUCCESS);

    case '+':   // override
      overrides.push_back(arg.substr(1));
      break;

    default:    // otherwise, it is config file
      if (!cfg.empty())
      {
        LOG2(ERROR, "Only single CONFIG_FILE argument is allowed!");
        return EXIT_FAILURE;
      }
      cfg = arg;
      break;
    }
  }

  const std::string path_exe = pathOfExecutable();
  unsigned int search_count = 0;

  std::cout << "Executable is in directory \"" << path_exe << "\".\n"
            << "Home/App directory is \"" << getHomeDir() << "\".\n"
            << "Searching for configuration file..." << std::endl;

  if (!cfg.empty())
  {
    std::cout << ++search_count << ". in specified file (\"" << cfg << "\"): ";
    if (fileExists(cfg))
    {
      const std::pair<std::string, std::string> fullpath = pathOfFile(cfg);
      cfg = fullpath.first + PATH_SEPARATOR + fullpath.second;
      Mineserver::get()->config()->config_path = fullpath.first;
      std::cout << "FOUND at \"" << cfg << "\"!\n";
    }
    else
    {
      std::cout << "not found\n";
      cfg.clear();
    }
  }
  if (cfg.empty())
  {
#ifdef DEBUG
    std::cout << ++search_count << ". in executable directory: ";
    if (fileExists(path_exe + PATH_SEPARATOR + CONFIG_FILE))
    {
      cfg = path_exe + PATH_SEPARATOR + CONFIG_FILE;
      Mineserver::get()->config()->config_path = path_exe;
      std::cout << "FOUND at \"" << cfg << "\"!\n";
    }
    else
    {
#endif
      std::cout << "not found\n"
                << ++search_count << ". in home/app directory: ";

      cfg = getHomeDir() + PATH_SEPARATOR + CONFIG_FILE;
      Mineserver::get()->config()->config_path = getHomeDir();

      if (fileExists(getHomeDir() + PATH_SEPARATOR + CONFIG_FILE))
      {
        std::cout << "FOUND at \"" << cfg << "\"!\n";
      }
      else
      {
        std::cout << "not found\n"
                  << "No config file found! We will place the factory default in \"" << cfg << "\"." << std::endl;
      }
#ifdef DEBUG
    }
#endif
  }
  std::cout << "Configuration directory is \"" << Mineserver::get()->config()->config_path << "\"." << std::endl;

  // create home and copy files if necessary
  Mineserver::get()->configDirectoryPrepare(Mineserver::get()->config()->config_path);

  // load config
  Config & config = *Mineserver::get()->config();
  if (!config.load(cfg))
  {
    return EXIT_FAILURE;
  }

  LOG2(INFO, "Using config: " + cfg);

  if (overrides.size())
  {
    std::stringstream override_config;
    for (size_t i = 0; i < overrides.size(); i++)
    {
      LOG2(INFO, "Overriden: " + overrides[i]);
      override_config << overrides[i] << ';' << std::endl;
    }
    // override config
    if (!config.load(override_config))
    {
      LOG2(ERROR, "Error when parsing overrides: maybe you forgot to doublequote string values?");
      return EXIT_FAILURE;
    }
  }

  bool ret = Mineserver::get()->init();

  if (!ret)
  {
    LOG2(ERROR, "Failed to start Mineserver!");
  }
  else
  {
    ret = Mineserver::get()->run();
  }

  Mineserver::get()->free();

  return ret ? EXIT_SUCCESS : EXIT_FAILURE;
}
Пример #3
0
Mineserver::Mineserver(int args, char **argarray)
    :  argv(argarray),
       argc(args),
       m_socketlisten  (0),
       m_saveInterval  (0),
       m_lastSave      (std::time(NULL)),
       m_pvp_enabled   (false),
       m_damage_enabled(false),
       m_only_helmets  (false),
       m_running       (false),
       m_eventBase     (NULL),

       // core modules
       m_config        (new Config()),
       m_screen        (new CliScreen()),
       m_logger        (new Logger()),

       m_plugin        (NULL),
       m_chat          (NULL),
       m_furnaceManager(NULL),
       m_packetHandler (NULL),
       m_inventory     (NULL),
       m_mobs          (NULL)
{
    pthread_mutex_init(&m_validation_mutex,NULL);
    ServerInstance = this;
    InitSignals();

    std::srand((uint32_t)std::time(NULL));
    initPRNG();

    std::string cfg;
    std::vector<std::string> overrides;

    for (int i = 1; i < argc; i++)
    {
        const std::string arg(argv[i]);

        switch (arg[0])
        {
        case '-':   // option
            // we have only '-h' and '--help' now, so just return with help
            printHelp(0);
            throw CoreException();

        case '+':   // override
            overrides.push_back(arg.substr(1));
            break;

        default:    // otherwise, it is config file
            if (!cfg.empty())
                throw CoreException("Only single CONFIG_FILE argument is allowed!");
            cfg = arg;
            break;
        }
    }

    const std::string path_exe = "./";

    // If config file is provided as an argument
    if (!cfg.empty())
    {
        std::cout << "Searching for configuration file..." << std::endl;
        if (fileExists(cfg))
        {
            const std::pair<std::string, std::string> fullpath = pathOfFile(cfg);
            cfg = fullpath.first + PATH_SEPARATOR + fullpath.second;
            this->config()->config_path = fullpath.first;
        }
        else
        {
            std::cout << "Config not found...\n";;
            cfg.clear();
        }
    }

    if (cfg.empty())
    {
        if (fileExists(path_exe + PATH_SEPARATOR + CONFIG_FILE))
        {
            cfg = path_exe + PATH_SEPARATOR + CONFIG_FILE;
            this->config()->config_path = path_exe;
        }
        else
        {
            std::cout << "Config not found\n";
        }
    }

    // load config
    Config &configvar = *this->config();
    if (!configvar.load(cfg))
    {
        throw CoreException("Could not load config!");
    }

    m_plugin = new Plugin();

    LOG2(INFO, "Using config: " + cfg);

    if (overrides.size())
    {
        std::stringstream override_config;
        for (size_t i = 0; i < overrides.size(); i++)
        {
            LOG2(INFO, "Overriden: " + overrides[i]);
            override_config << overrides[i] << ';' << std::endl;
        }
        // override config
        if (!configvar.load(override_config))
            throw CoreException("Error when parsing overrides: maybe you forgot to doublequote string values?");
    }

    memset(&m_listenEvent, 0, sizeof(event));
    initConstants();
    // Write PID to file
    std::ofstream pid_out((config()->sData("system.pid_file")).c_str());
    if (!pid_out.fail())
    {
        pid_out << getpid();
    }
    pid_out.close();




    init_plugin_api();

    if (config()->bData("system.interface.use_cli"))
    {
        // Init our Screen
        screen()->init(VERSION);
    }


    LOG2(INFO, "Welcome to Mineserver v" + VERSION);
    LOG2(INFO, "Using zlib "+std::string(ZLIB_VERSION)+" libevent "+std::string(event_get_version()));

    LOG2(INFO, "Generating RSA key pair for protocol encryption");
    //Protocol encryption
    srand(microTime());
    if((rsa = RSA_generate_key(1024, 17, 0, 0)) == NULL)
    {
        LOG2(INFO, "KEY GENERATION FAILED!");
        exit(1);
    }
    LOG2(INFO, "RSA key pair generated.");

    /* Get ASN.1 format public key */
    x=X509_new();
    pk=EVP_PKEY_new();
    EVP_PKEY_assign_RSA(pk,rsa);
    X509_set_version(x,0);
    X509_set_pubkey(x,pk);

    int len;
    unsigned char *buf;
    buf = NULL;
    len = i2d_X509(x, &buf);

    //Glue + jesus tape, dont ask - Fador
    publicKey = std::string((char *)(buf+28),len-36);
    OPENSSL_free(buf);
    /* END key fetching */

    const std::string temp_nums="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890=-";

    const std::string temp_hex="0123456789abcdef";

    for(int i = 0; i < 4; i++)
    {
        encryptionBytes += (char)(temp_nums[rand()%temp_nums.size()]);
    }

    for(int i = 0; i < 16; i++)
    {
        serverID += (char)(temp_hex[rand()%temp_hex.size()]);
    }

    LOG2(INFO, "ServerID: " + serverID);

    if(!m_config->bData("system.user_validation"))
    {
        serverID = "-";
    }

    MapGen* mapgen = new MapGen();
    MapGen* nethergen = new NetherGen();
    MapGen* heavengen = new HeavenGen();
    MapGen* biomegen = new BiomeGen();
    MapGen* eximgen = new EximGen();
    m_mapGenNames.push_back(mapgen);
    m_mapGenNames.push_back(nethergen);
    m_mapGenNames.push_back(heavengen);
    m_mapGenNames.push_back(biomegen);
    m_mapGenNames.push_back(eximgen);

    m_saveInterval = m_config->iData("map.save_interval");

    m_only_helmets = m_config->bData("system.armour.helmet_strict");
    m_pvp_enabled = m_config->bData("system.pvp.enabled");
    m_damage_enabled = m_config->bData("system.damage.enabled");

    const char* key = "map.storage.nbt.directories"; // Prefix for worlds config
    if (m_config->has(key) && (m_config->type(key) == CONFIG_NODE_LIST))
    {
        std::list<std::string> tmp = m_config->mData(key)->keys();
        int n = 0;
        for (std::list<std::string>::const_iterator it = tmp.begin(); it != tmp.end(); ++it)
        {
            m_map.push_back(new Map());
            Physics* phy = new Physics;
            phy->map = n;

            m_physics.push_back(phy);
            RedstoneSimulation* red = new RedstoneSimulation;
            red->map = n;
            m_redstone.push_back(red);
            int k = m_config->iData((std::string(key) + ".") + (*it));
            if ((uint32_t)k >= m_mapGenNames.size())
            {
                std::ostringstream s;
                s << "Error! Mapgen number " << k << " in config. " << m_mapGenNames.size() << " Mapgens known";
                LOG2(INFO, s.str());
            }
            // WARNING: if k is too big this will be an access error! -- louisdx
            MapGen* m = m_mapGenNames[k];
            m_mapGen.push_back(m);
            n++;
        }
    }
    else
    {
        LOG2(WARNING, "Cannot find map.storage.nbt.directories.*");
    }

    if (m_map.size() == 0)
        throw CoreException("No worlds in Config");

    m_chat           = new Chat;
    m_furnaceManager = new FurnaceManager;
    m_packetHandler  = new PacketHandler;
    m_inventory      = new Inventory(m_config->sData("system.path.data") + '/' + "recipes", ".recipe", "ENABLED_RECIPES.cfg");
    m_mobs           = new Mobs;

} // End Mineserver constructor