70
72
void ScriptModule::setScriptFile( QString scriptFile, bool )
72
m_script = fileToString( scriptFile, "ScriptModule::setScriptFile" );
74
m_scriptFile = scriptFile;
75
m_scriptFolder = QFileInfo( scriptFile ).absolutePath();
76
setScript( fileToString( scriptFile, "ScriptBase::setScriptFile" ) );
75
79
void ScriptModule::setScript( QString script )
82
86
if( !m_aEngine ) return -1;
84
std::string script = m_script.toStdString();
85
int len = m_script.size();
87
88
m_aEngine->GarbageCollect( asGC_FULL_CYCLE );
89
asIScriptModule* mod = m_aEngine->GetModule( 0, asGM_ALWAYS_CREATE );
90
int r = mod->AddScriptSection("script", &script[0], len );
91
if( r < 0 ) { qDebug() << "\nScriptModule::compileScript: AddScriptSection() failed\n"; return -1; }
94
if( r < 0 ) { qDebug() << endl << m_elmId+" ScriptModule::compileScript Error"<< endl; return -1; }
96
//qDebug() << "\nScriptModule::compileScript: Build() Success\n";
89
m_asModule = m_aEngine->GetModule( 0, asGM_ALWAYS_CREATE );
91
int r = compileSection( m_scriptFile, m_script );
92
if( r < 0 ) return -1;
94
r = m_asModule->Build();
95
if( r < 0 ) { qDebug() << endl << m_elmId+" ScriptBase::compileScript Error"<< endl; return -1; }
97
//qDebug() << "\nScriptBase::compileScript: Build() Success\n";
101
int ScriptModule::compileSection( QString sriptFile, QString text )
105
QStringList lines = text.split("\n");
107
for( QString line : lines ) // Get includes
109
if( line.contains("#include") )
111
QString file = line.remove("#include").remove(" ");
112
while( file.startsWith(" ") ) file = file.right( file.length()-1 );
114
if( file.startsWith("\"") )
116
file = file.right( file.length()-1 );
117
file = file.split("\"").first();
118
file.prepend( m_scriptFolder+"/" );
120
else if( file.startsWith("<") )
122
file = file.remove("<").split(">").first();
123
file.prepend( MainWindow::self()->getDataFilePath("scriptlib")+"/" );
125
line = fileToString( file, "ScriptBase::compileScript" );
126
int r = compileSection( file, line );
130
text.append( line+"\n");
132
std::string script = text.toStdString();
134
int r = m_asModule->AddScriptSection( sriptFile.toLocal8Bit().data(), &script[0], script.size() );
135
if( r < 0 ) { qDebug() << "\nScriptBase::compileSection: AddScriptSection() failed\n"; return -1; }
100
140
/*int ScriptModule::SaveBytecode(asIScriptEngine *engine, const char *outputFile)
102
142
CBytecodeStream stream;