gd::String EventsCodeGenerator::GenerateObjectBehaviorFunctionCall(gd::String objectListName, gd::String behaviorName, const gd::BehaviorMetadata & autoInfo, const gd::ExpressionCodeGenerationInformation & codeInfo, gd::String parametersStr, gd::String defaultOutput, gd::EventsCodeGenerationContext & context) { bool castNeeded = !autoInfo.className.empty(); if ( codeInfo.staticFunction ) { if ( !castNeeded ) return "(gd::Behavior::"+codeInfo.functionCallName+"("+parametersStr+"))"; else return "("+autoInfo.className+"::"+codeInfo.functionCallName+"("+parametersStr+"))"; } else if ( context.GetCurrentObject() == objectListName && !context.GetCurrentObject().empty()) { if ( !castNeeded ) return "("+ManObjListName(objectListName)+"[i]->GetBehaviorRawPointer(\""+behaviorName+"\")->"+codeInfo.functionCallName+"("+parametersStr+"))"; else return "(static_cast<"+autoInfo.className+"*>("+ManObjListName(objectListName)+"[i]->GetBehaviorRawPointer(\""+behaviorName+"\"))->"+codeInfo.functionCallName+"("+parametersStr+"))"; } else { if ( !castNeeded ) return "(( "+ManObjListName(objectListName)+".empty() ) ? "+defaultOutput+" :"+ManObjListName(objectListName)+"[0]->GetBehaviorRawPointer(\""+behaviorName+"\")->"+codeInfo.functionCallName+"("+parametersStr+"))"; else return "(( "+ManObjListName(objectListName)+".empty() ) ? "+defaultOutput+" : "+"static_cast<"+autoInfo.className+"*>("+ManObjListName(objectListName)+"[0]->GetBehaviorRawPointer(\""+behaviorName+"\"))->"+codeInfo.functionCallName+"("+parametersStr+"))"; } }
gd::String EventsCodeGenerator::GenerateObjectFunctionCall(gd::String objectListName, const gd::ObjectMetadata & objMetadata, const gd::ExpressionCodeGenerationInformation & codeInfo, gd::String parametersStr, gd::String defaultOutput, gd::EventsCodeGenerationContext & context) { bool castNeeded = !objMetadata.className.empty(); if ( codeInfo.staticFunction ) { if ( !castNeeded ) return "(RuntimeObject::"+codeInfo.functionCallName+"("+parametersStr+"))"; else return "("+objMetadata.className+"::"+codeInfo.functionCallName+"("+parametersStr+"))"; } else if ( context.GetCurrentObject() == objectListName && !context.GetCurrentObject().empty()) { if ( !castNeeded ) return "("+ManObjListName(objectListName)+"[i]->"+codeInfo.functionCallName+"("+parametersStr+"))"; else return "(static_cast<"+objMetadata.className+"*>("+ManObjListName(objectListName)+"[i])->"+codeInfo.functionCallName+"("+parametersStr+"))"; } else { if ( !castNeeded ) return "(( "+ManObjListName(objectListName)+".empty() ) ? "+defaultOutput+" :"+ ManObjListName(objectListName)+"[0]->"+codeInfo.functionCallName+"("+parametersStr+"))"; else return "(( "+ManObjListName(objectListName)+".empty() ) ? "+defaultOutput+" : "+"static_cast<"+objMetadata.className+"*>("+ManObjListName(objectListName)+"[0])->"+codeInfo.functionCallName+"("+parametersStr+"))"; } }
gd::String EventsCodeGenerator::GenerateObject( const gd::String& objectName, const gd::String& type, gd::EventsCodeGenerationContext& context) { gd::String output; if (type == "objectList") { std::vector<gd::String> realObjects = ExpandObjectsName(objectName, context); output += "runtimeContext->ClearObjectListsMap()"; for (std::size_t i = 0; i < realObjects.size(); ++i) { context.ObjectsListNeeded(realObjects[i]); output += ".AddObjectListToMap(\"" + ConvertToString(realObjects[i]) + "\", " + ManObjListName(realObjects[i]) + ")"; } output += ".ReturnObjectListsMap()"; } else if (type == "objectListWithoutPicking") { std::vector<gd::String> realObjects = ExpandObjectsName(objectName, context); output += "runtimeContext->ClearObjectListsMap()"; for (std::size_t i = 0; i < realObjects.size(); ++i) { context.ObjectsListWithoutPickingNeeded(realObjects[i]); output += ".AddObjectListToMap(\"" + ConvertToString(realObjects[i]) + "\", " + ManObjListName(realObjects[i]) + ")"; } output += ".ReturnObjectListsMap()"; } else if (type == "objectPtr") { std::vector<gd::String> realObjects = ExpandObjectsName(objectName, context); if (find(realObjects.begin(), realObjects.end(), context.GetCurrentObject()) != realObjects.end() && !context.GetCurrentObject().empty()) { // If object currently used by instruction is available, use it directly. output += ManObjListName(context.GetCurrentObject()) + "[i]"; } else { for (std::size_t i = 0; i < realObjects.size(); ++i) { context.ObjectsListNeeded(realObjects[i]); output += "(!" + ManObjListName(realObjects[i]) + ".empty() ? " + ManObjListName(realObjects[i]) + "[0] : "; } output += GenerateBadObject(); for (std::size_t i = 0; i < realObjects.size(); ++i) output += ")"; } } return output; }
gd::String EventsCodeGenerator::GenerateGetVariable( const gd::String& variableName, const VariableScope& scope, gd::EventsCodeGenerationContext& context, const gd::String& objectName) { gd::String output; const gd::VariablesContainer* variables = NULL; if (scope == LAYOUT_VARIABLE) { output = "runtimeContext->GetSceneVariables()"; if (HasProjectAndLayout()) { variables = &GetLayout().GetVariables(); } } else if (scope == PROJECT_VARIABLE) { output = "runtimeContext->GetGameVariables()"; if (HasProjectAndLayout()) { variables = &GetProject().GetVariables(); } } else { std::vector<gd::String> realObjects = ExpandObjectsName(objectName, context); output = "RuntimeVariablesContainer::GetBadVariablesContainer()"; for (std::size_t i = 0; i < realObjects.size(); ++i) { context.ObjectsListNeeded(realObjects[i]); // Generate the call to GetVariables() method. if (context.GetCurrentObject() == realObjects[i] && !context.GetCurrentObject().empty()) output = GetObjectListName(realObjects[i], context) + "[i]->GetVariables()"; else output = "((" + GetObjectListName(realObjects[i], context) + ".empty() ) ? " + output + " : " + GetObjectListName(realObjects[i], context) + "[0]->GetVariables())"; } if (HasProjectAndLayout()) { if (GetLayout().HasObjectNamed( objectName)) // We check first layout's objects' list. variables = &GetLayout().GetObject(objectName).GetVariables(); else if (GetProject().HasObjectNamed( objectName)) // Then the global objects list. variables = &GetProject().GetObject(objectName).GetVariables(); } } // Optimize the lookup of the variable when the variable is declared. //(In this case, it is stored in an array at runtime and we know its // position.) if (variables && variables->Has(variableName)) { std::size_t index = variables->GetPosition(variableName); if (index < variables->Count()) { output += ".Get(" + gd::String::From(index) + ")"; return output; } } output += ".Get(" + ConvertToStringExplicit(variableName) + ")"; return output; }
gd::String EventsCodeGenerator::GenerateParameterCodes(const gd::String & parameter, const gd::ParameterMetadata & metadata, gd::EventsCodeGenerationContext & context, const gd::String & previousParameter, std::vector < std::pair<gd::String, gd::String> > * supplementaryParametersTypes) { gd::String argOutput; //Code only parameter type if ( metadata.type == "currentScene" ) { argOutput += "*runtimeContext->scene"; } //Code only parameter type else if ( metadata.type == "objectList" ) { std::vector<gd::String> realObjects = ExpandObjectsName(parameter, context); argOutput += "runtimeContext->ClearObjectListsMap()"; for (std::size_t i = 0;i<realObjects.size();++i) { context.ObjectsListNeeded(realObjects[i]); argOutput += ".AddObjectListToMap(\""+ConvertToString(realObjects[i])+"\", "+ManObjListName(realObjects[i])+")"; } argOutput += ".ReturnObjectListsMap()"; } //Code only parameter type else if ( metadata.type == "objectListWithoutPicking" ) { std::vector<gd::String> realObjects = ExpandObjectsName(parameter, context); argOutput += "runtimeContext->ClearObjectListsMap()"; for (std::size_t i = 0;i<realObjects.size();++i) { context.EmptyObjectsListNeeded(realObjects[i]); argOutput += ".AddObjectListToMap(\""+ConvertToString(realObjects[i])+"\", "+ManObjListName(realObjects[i])+")"; } argOutput += ".ReturnObjectListsMap()"; } //Code only parameter type else if ( metadata.type == "objectPtr") { std::vector<gd::String> realObjects = ExpandObjectsName(parameter, context); if ( find(realObjects.begin(), realObjects.end(), context.GetCurrentObject()) != realObjects.end() && !context.GetCurrentObject().empty()) { //If object currently used by instruction is available, use it directly. argOutput += ManObjListName(context.GetCurrentObject())+"[i]"; } else { for (std::size_t i = 0;i<realObjects.size();++i) { context.ObjectsListNeeded(realObjects[i]); argOutput += "(!"+ManObjListName(realObjects[i])+".empty() ? "+ManObjListName(realObjects[i])+"[0] : "; } argOutput += "NULL"; for (std::size_t i = 0;i<realObjects.size();++i) argOutput += ")"; } } else if (metadata.type == "scenevar") { VariableCodeGenerationCallbacks callbacks(argOutput, *this, context, VariableCodeGenerationCallbacks::LAYOUT_VARIABLE); gd::VariableParser parser(parameter); if ( !parser.Parse(callbacks) ) { cout << "Error :" << parser.firstErrorStr << " in: "<< parameter << endl; argOutput = "runtimeContext->GetSceneVariables().GetBadVariable()"; } } else if (metadata.type == "globalvar") { VariableCodeGenerationCallbacks callbacks(argOutput, *this, context, VariableCodeGenerationCallbacks::PROJECT_VARIABLE); gd::VariableParser parser(parameter); if ( !parser.Parse(callbacks) ) { cout << "Error :" << parser.firstErrorStr << " in: "<< parameter << endl; argOutput = "runtimeContext->GetGameVariables().GetBadVariable()"; } } else if (metadata.type == "objectvar") { //Object is either the object of the previous parameter or, if it is empty, //the object being picked by the instruction. gd::String object = previousParameter; if ( object.empty() ) object = context.GetCurrentObject(); VariableCodeGenerationCallbacks callbacks(argOutput, *this, context, object); gd::VariableParser parser(parameter); if ( !parser.Parse(callbacks) ) { cout << "Error :" << parser.firstErrorStr << " in: "<< parameter << endl; argOutput = "runtimeContext->GetGameVariables().GetBadVariable()"; } } else { argOutput += gd::EventsCodeGenerator::GenerateParameterCodes(parameter, metadata, context, previousParameter, supplementaryParametersTypes); } return argOutput; }