Ejemplo n.º 1
0
void GD_API SendHttpRequest(const gd::String & host, const gd::String & uri, const gd::String & body,
    const gd::String & method, const gd::String & contentType, gd::Variable & responseVar)
{
    // Separate the host and the port number
    auto hostInfo = host.Split(U':');

    if(hostInfo.size() < 2)
        return; //Invalid address (there should be two elements: "http" and "//the.domain.com")

    // Create Http
    sf::Http http;
    http.setHost(hostInfo[0].ToUTF8() + ":" + hostInfo[1].ToUTF8(), hostInfo.size() > 2 ? hostInfo[2].To<unsigned short>() : 0);

    // Create request
    sf::Http::Request request;
    request.setMethod(method == "POST" ? sf::Http::Request::Post : sf::Http::Request::Get);
    request.setField("Content-Type", contentType.empty() ? "application/x-www-form-urlencoded" : contentType.ToUTF8());
    request.setUri(uri.ToUTF8());
    request.setBody(body.ToUTF8());

    // Send request & Get response
    sf::Http::Response response = http.sendRequest(request);

    if (response.getStatus() == sf::Http::Response::Ok)
    {
        responseVar.SetString(gd::String::FromUTF8(response.getBody()));
    }
    //else request failed.
}
Ejemplo n.º 2
0
void GD_API ReadStringFromFile( const gd::String & filename, const gd::String & group, RuntimeScene & scene, gd::Variable & variable )
{
    std::shared_ptr<XmlFile> file = XmlFilesManager::GetFile(filename, false);
    TiXmlHandle hdl( &file->GetTinyXmlDocument() );

    //D�coupage des groupes
    istringstream groupsStr( group.Raw() );
    std::string str;
    vector < gd::String > groups;
    while ( std::getline( groupsStr, str, '/' ) )
    {
        groups.push_back(gd::String::FromUTF8(str));
    }
    groups.erase(std::remove_if(groups.begin(), groups.end(), StringEmpty()), groups.end());

    //On avance petit � petit dans le fichier
    for (std::size_t i =0;i<groups.size();i++)
    {
        if ( !hdl.FirstChildElement(groups.at(i).c_str()).ToElement())
        {
            return;
        }
        hdl = hdl.FirstChildElement(groups.at(i).c_str());
    }

    //On stocke la valeur
    if ( hdl.ToElement()->Attribute("texte") == NULL ) return;

    //Update variable texte
    variable.SetString(hdl.ToElement()->Attribute("texte"));

    return;
}
Ejemplo n.º 3
0
/**
 * Show a dialog so as to get a text from user
 */
bool GD_EXTENSION_API ShowTextInput( RuntimeScene & scene, gd::Variable & variable, const gd::String & message, const gd::String & title )
{
    sf::Clock timeSpent;
    gd::String result;

    //Display the box
    #if defined(WINDOWS)
    CInputBox ibox(NULL);
    if (ibox.DoModal(title.ToWide().c_str(), message.ToWide().c_str()))
        result = gd::String::FromWide(ibox.Text);
    #endif
    #if defined(LINUX) || defined(MACOS)
    std::string strResult;
    nw::TextInput dialog(title.ToLocale(), message.ToLocale(), strResult);
    dialog.wait_until_closed();
    result = gd::String::FromLocale(strResult); //Convert from locale
    #endif

    scene.GetTimeManager().NotifyPauseWasMade(timeSpent.getElapsedTime().asMicroseconds());//Don't take the time spent in this function in account.

    //Update the variable
    variable.SetString(result);

    return true;
}
Ejemplo n.º 4
0
/**
 * Display an "open file" dialog
 */
void GD_EXTENSION_API ShowOpenFile( RuntimeScene & scene, gd::Variable & variable, const gd::String & title, gd::String filters )
{
    sf::Clock timeSpent;

    gd::String result;

    //Display the dialog
    #if defined(WINDOWS)
    //Process filters to match windows dialogs filters style.
    filters.Raw() = filters.Raw()+'\0';
    std::replace(filters.Raw().begin(), filters.Raw().end(), '|', '\0');

    OPENFILENAMEW toGetFileName; //Struct for the dialog
    wchar_t filePath[MAX_PATH];
    _wgetcwd(filePath, MAX_PATH);

    ZeroMemory(&toGetFileName, sizeof(OPENFILENAMEW));
    toGetFileName.lStructSize = sizeof(OPENFILENAMEW);
    toGetFileName.hwndOwner = NULL;
    toGetFileName.lpstrFile = filePath;
    toGetFileName.nMaxFile = MAX_PATH;
    toGetFileName.lpstrFilter = filters == "\0" ? NULL : filters.ToWide().c_str();
    toGetFileName.nFilterIndex = 1;
    toGetFileName.Flags = OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR;;

    if(GetOpenFileNameW(&toGetFileName) == TRUE)
        result = gd::String::FromWide(filePath);
    #endif
    #if defined(LINUX) || defined(MACOS)
    std::string strResult;
    nw::OpenFile * dialog = new nw::OpenFile(title.ToLocale(), true, strResult);
    dialog->wait_until_closed();
    result = gd::String::FromLocale(strResult);
    #endif

    scene.GetTimeManager().NotifyPauseWasMade(timeSpent.getElapsedTime().asMicroseconds());//Don't take the time spent in this function in account.

    //Update the variable
    variable.SetString(result);
}
Ejemplo n.º 5
0
/**
 * Show a message box with Yes/No buttons
 */
void GD_EXTENSION_API ShowYesNoMsgBox( RuntimeScene & scene, gd::Variable & variable, const gd::String & message, const gd::String & title )
{
    sf::Clock timeSpent;

    gd::String result;

    //Display the box
    #if defined(WINDOWS)
    if( MessageBoxW(NULL, message.ToWide().c_str(), title.ToWide().c_str(), MB_ICONQUESTION | MB_YESNO) == IDYES)
        result = "yes";
    else
        result = "no";
    #endif
    #if defined(LINUX) || defined(MACOS)
    nw::YesNoMsgBox dialog(title.ToLocale(), message.ToLocale(), result.Raw());
    dialog.wait_until_closed();
    #endif

    scene.GetTimeManager().NotifyPauseWasMade(timeSpent.getElapsedTime().asMicroseconds());//Don't take the time spent in this function in account.

    //Update the variable
    variable.SetString(result); //Can only be "yes" or "no", no need to encode in UTF8
}
Ejemplo n.º 6
0
void GD_API SendHttpRequest(const std::string & host, const std::string & uri, const std::string & body,
    const std::string & method, const std::string & contentType, gd::Variable & responseVar)
{
    // Create Http
    sf::Http Http;
    Http.setHost(host);

    // Create request
    sf::Http::Request request;
    request.setMethod(method == "POST" ? sf::Http::Request::Post : sf::Http::Request::Get);
    request.setField("Content-Type", contentType.empty() ? "application/x-www-form-urlencoded" : contentType);
    request.setUri(uri);
    request.setBody(body);

    // Send request & Get response
    sf::Http::Response response = Http.sendRequest(request);

    if (response.getStatus() == sf::Http::Response::Ok)
    {
        responseVar.SetString(response.getBody());
    }
    //else request failed.
}
Ejemplo n.º 7
0
std::string GD_API VariableStructureToJSON(const gd::Variable & variable)
{
    if ( !variable.IsStructure() ) {
        if ( variable.IsNumber() )
            return ToString(variable.GetValue());
        else
            return StringToQuotedJSONString(variable.GetString().c_str());
    }

    std::string str = "{";
    bool firstChild = true;
    for(std::map<std::string, gd::Variable>::const_iterator i = variable.GetAllChildren().begin();
        i != variable.GetAllChildren().end();++i)
    {
        if ( !firstChild ) str += ",";
        str += StringToQuotedJSONString(i->first.c_str())+": "+VariableStructureToJSON(i->second);

        firstChild = false;
    }

    str += "}";
    return str;
}
Ejemplo n.º 8
0
const std::string& GD_API GetVariableString(const gd::Variable & variable)
{
    return variable.GetString();
};
Ejemplo n.º 9
0
double GD_API GetVariableValue(const gd::Variable & variable)
{
    return variable.GetValue();
};
Ejemplo n.º 10
0
void GD_API VariableRemoveChild(gd::Variable & variable, const std::string & childName)
{
    variable.RemoveChild(childName);
}
Ejemplo n.º 11
0
bool RuntimeObject::VariableChildExists(const gd::Variable &variable,
                                        const gd::String &childName) {
  return variable.HasChild(childName);
}
Ejemplo n.º 12
0
Archivo: Variable.cpp Proyecto: 4ian/GD
 */
/**
 * @file Tests covering events of GDevelop Core.
 */
#include "catch.hpp"

#include <algorithm>
#include <initializer_list>
#include <map>

#include "GDCore/CommonTools.h"
#include "GDCore/Project/VariablesContainer.h"

TEST_CASE("Variable", "[common][variables]") {
  SECTION("Basics") {
    gd::Variable variable;
    variable.SetValue(50);
    REQUIRE(variable.GetValue() == 50);
    REQUIRE(variable == 50);
    REQUIRE(variable.IsNumber() == true);
    REQUIRE(variable.IsStructure() == false);

    variable.SetString("MyString");
    REQUIRE(variable.GetString() == "MyString");
    REQUIRE(variable == "MyString");
    REQUIRE(variable.IsNumber() == false);
    REQUIRE(variable.IsStructure() == false);
  }
  SECTION("Conversions") {
    gd::Variable variable;
    variable.SetValue(50);
Ejemplo n.º 13
0
unsigned int RuntimeObject::GetVariableChildCount(gd::Variable &variable) {
  if (variable.IsStructure() == false) return 0;
  return variable.GetChildrenCount();
}
Ejemplo n.º 14
0
void RuntimeObject::VariableClearChildren(gd::Variable &variable) {
  variable.ClearChildren();
}
Ejemplo n.º 15
0
void RuntimeObject::VariableRemoveChild(gd::Variable &variable,
                                        const gd::String &childName) {
  variable.RemoveChild(childName);
}
Ejemplo n.º 16
0
bool GD_API VariableChildExists(const gd::Variable & variable, const std::string & childName)
{
    return variable.HasChild(childName);
}
Ejemplo n.º 17
0
unsigned int GD_API GetVariableChildCount(gd::Variable & variable)
{
    if (variable.IsStructure() == false) return 0;

    return variable.GetAllChildren().size();
}