00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "gameproject.h"
00022 #include "gameprojectprivate.h"
00023
00024 #include "scene.h"
00025
00026 #include <core/gdlhandler.h>
00027 #include <core/debughelper.h>
00028
00029 #include <QtCore/QCoreApplication>
00030 #include <QtCore/QStringList>
00031 #include <QtCore/QDir>
00032 #include <QtCore/QFile>
00033 #include <QtCore/QTextStream>
00034 #include <QMetaClassInfo>
00035
00036 REGISTER_OBJECTTYPE( GluonEngine, GameProject )
00037
00038 using namespace GluonEngine;
00039
00040 GameProject::GameProject( QObject* parent )
00041 : GluonObject( parent )
00042 , d( new GameProjectPrivate )
00043 {
00044 setGameProject( this );
00045 }
00046
00047 GameProject::GameProject( const GameProject& other, QObject* parent )
00048 : GluonObject( parent )
00049 , d( other.d )
00050 {
00051 }
00052
00053 GameProject::~GameProject()
00054 {
00055 }
00056
00057 bool
00058 GameProject::saveToFile() const
00059 {
00060
00061 GameProjectPrivate::saveChildren( this );
00062
00063
00064 QFile projectFile( filename().toLocalFile() );
00065 if( !projectFile.open( QIODevice::WriteOnly ) )
00066 return false;
00067
00068 QList<const GluonObject*> thisProject;
00069 thisProject.append( this );
00070
00071 QTextStream projectWriter( &projectFile );
00072 projectWriter << GluonCore::GDLHandler::instance()->serializeGDL( thisProject );
00073 projectFile.close();
00074
00075 return true;
00076 }
00077
00078 bool
00079 GameProject::loadFromFile()
00080 {
00081 DEBUG_FUNC_NAME
00082
00083
00084 DEBUG_TEXT( QString( "Changing working directory to %1" ).arg( QFileInfo( filename().toLocalFile() ).canonicalPath() ) );
00085 QDir::setCurrent( QFileInfo( filename().toLocalFile() ).canonicalPath() );
00086 setFilename( filename().toLocalFile() );
00087
00088 DEBUG_TEXT( QString( "Loading project from %1" ).arg( QFileInfo( filename().toLocalFile() ).fileName() ) );
00089 QFile projectFile( QFileInfo( filename().toLocalFile() ).fileName() );
00090 if( !projectFile.open( QIODevice::ReadOnly ) )
00091 return false;
00092
00093 QTextStream projectReader( &projectFile );
00094 QString fileContents = projectReader.readAll();
00095 projectFile.close();
00096
00097 if( fileContents.isEmpty() )
00098 return false;
00099
00100 QList<GluonObject*> objectList = GluonCore::GDLHandler::instance()->parseGDL( fileContents, parent() );
00101 if( objectList.count() > 0 )
00102 {
00103 if( objectList[0]->metaObject() )
00104 {
00105
00106
00107 if( objectList[0]->metaObject()->className() == metaObject()->className() )
00108 {
00109 DEBUG_TEXT( "Project successfully parsed - applying to local instance" );
00110 GameProject* loadedProject = qobject_cast<GameProject*>( objectList[0] );
00111
00112
00113
00114 qDeleteAll( children() );
00115
00116
00117
00118 foreach( QObject * child, loadedProject->children() )
00119 {
00120 GluonObject* theChild = qobject_cast<GluonObject*>( child );
00121 theChild->setParent( this );
00122 theChild->setGameProject( this );
00123 }
00124
00125
00126 setName( loadedProject->name() );
00127
00128
00129 const QMetaObject* metaobject = loadedProject->metaObject();
00130 int count = metaobject->propertyCount();
00131 for( int i = 0; i < count; ++i )
00132 {
00133 QMetaProperty metaproperty = metaobject->property( i );
00134 const QString theName( metaproperty.name() );
00135 if( theName == "objectName" || theName == "name" )
00136 continue;
00137 setProperty( metaproperty.name(), loadedProject->property( metaproperty.name() ) );
00138 }
00139
00140
00141 QList<QByteArray> propertyNames = loadedProject->dynamicPropertyNames();
00142 foreach( const QByteArray & propName, propertyNames )
00143 {
00144 setProperty( propName, loadedProject->property( propName ) );
00145 }
00146
00147
00148 sanitize();
00149
00150
00151 qDeleteAll( objectList );
00152
00153 DEBUG_TEXT( "Project loading successful!" );
00154 }
00155
00156 else
00157 {
00158 DEBUG_TEXT( QString( "First object loaded is not a Gluon::GameProject." ) );
00159 DEBUG_TEXT( QString( "Type of loaded object:" ).arg( objectList[0]->metaObject()->className() ) );
00160 DEBUG_TEXT( QString( "Name of loaded object:" ).arg( objectList[0]->name() ) );
00161 return false;
00162 }
00163 }
00164 else
00165 {
00166 return false;
00167 }
00168 }
00169
00170 return true;
00171 }
00172
00173 bool
00174 GameProject::loadFromFile( QUrl filename )
00175 {
00176 setFilename( filename );
00177 return loadFromFile();
00178 }
00179
00180
00181
00182
00183
00184 QString
00185 GameProject::description() const
00186 {
00187 return d->description;
00188 }
00189
00190 void
00191 GameProject::setDescription( QString newDescription )
00192 {
00193 d->description = newDescription;
00194 }
00195
00196 QUrl
00197 GameProject::homepage() const
00198 {
00199 return d->homepage;
00200 }
00201 void
00202 GameProject::setHomepage( QUrl newHomepage )
00203 {
00204 d->homepage = newHomepage;
00205 }
00206
00207 QList<QUrl>
00208 GameProject::mediaInfo() const
00209 {
00210 return d->mediaInfo;
00211 }
00212 void
00213 GameProject::setMediaInfo( QList<QUrl> newMediaInfo )
00214 {
00215 d->mediaInfo = newMediaInfo;
00216 }
00217
00218 QUrl
00219 GameProject::filename() const
00220 {
00221 return d->filename;
00222 }
00223
00224 void
00225 GameProject::setFilename( QUrl newFilename )
00226 {
00227 d->filename = newFilename;
00228 }
00229
00230 Scene *
00231 GameProject::entryPoint() const
00232 {
00233 return d->entryPoint;
00234 }
00235
00236 void
00237 GameProject::setEntryPoint( Scene* newEntryPoint )
00238 {
00239 d->entryPoint = newEntryPoint;
00240 }
00241
00242 #include "gameproject.moc"