Exemple #1
0
bool ProjectFileWriter::LoadFromFile(gd::Project & project, const gd::String & filename)
{
    //Load the XML document structure
    TiXmlDocument doc;
    if ( !doc.LoadFile(filename.ToLocale().c_str()) )
    {
        gd::String errorTinyXmlDesc = doc.ErrorDesc();
        gd::String error = _( "Error while loading :" ) + "\n" + errorTinyXmlDesc + "\n\n" +_("Make sure the file exists and that you have the right to open the file.");

        gd::LogError( error );
        return false;
    }

    #if defined(GD_IDE_ONLY)
    project.SetProjectFile(filename);
    project.SetDirty(false);
    #endif

    TiXmlHandle hdl( &doc );
    gd::SerializerElement rootElement;

    ConvertANSIXMLFile(hdl, doc, filename);

    //Load the root element
    TiXmlElement * rootXmlElement = hdl.FirstChildElement("project").ToElement();
    //Compatibility with GD <= 3.3
    if (!rootXmlElement) rootXmlElement = hdl.FirstChildElement("Project").ToElement();
    if (!rootXmlElement) rootXmlElement = hdl.FirstChildElement("Game").ToElement();
    //End of compatibility code
    gd::Serializer::FromXML(rootElement, rootXmlElement);

    //Unsplit the project
    #if defined(GD_IDE_ONLY) && !defined(GD_NO_WX_GUI)
    wxString projectPath = wxFileName::FileName(filename).GetPath();
    gd::Splitter splitter;
    splitter.Unsplit(rootElement, [&projectPath](gd::String path, gd::String name) {
        TiXmlDocument doc;
        gd::SerializerElement rootElement;

        gd::String filename = projectPath + path + "-" + MakeFileNameSafe(name);
        if (!doc.LoadFile(filename.ToLocale().c_str()))
        {
            gd::String errorTinyXmlDesc = doc.ErrorDesc();
            gd::String error = _( "Error while loading :" ) + "\n" + errorTinyXmlDesc + "\n\n" +_("Make sure the file exists and that you have the right to open the file.");

            gd::LogError(error);
            return rootElement;
        }

        TiXmlHandle hdl( &doc );
        gd::Serializer::FromXML(rootElement, hdl.FirstChildElement().ToElement());
        return rootElement;
    });
    #endif

    //Unserialize the whole project
    project.UnserializeFrom(rootElement);

    return true;
}
Exemple #2
0
bool ProjectFileWriter::LoadFromJSONFile(gd::Project & project, const gd::String & filename)
{
    std::ifstream ifs(filename.ToLocale().c_str());
    if (!ifs.is_open())
    {
        gd::String error = _( "Unable to open the file.") + _("Make sure the file exists and that you have the right to open the file.");
        gd::LogError(error);
        return false;
    }

    project.SetProjectFile(filename);
    project.SetDirty(false);

    std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
    gd::SerializerElement rootElement = gd::Serializer::FromJSON(str);
    project.UnserializeFrom(rootElement);

    return true;
}