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+"))"; } }
virtual std::string GenerateCode(gd::Instruction & instruction, gd::EventsCodeGenerator & codeGenerator, gd::EventsCodeGenerationContext & context) { std::string outputCode; std::vector<std::string> realObjects = codeGenerator.ExpandObjectsName(instruction.GetParameter(0).GetPlainString(), context); for (unsigned int i = 0;i<realObjects.size();++i) { context.SetCurrentObject(realObjects[i]); context.ObjectsListNeeded(realObjects[i]); std::string newX, newY; std::string expression1Code; { gd::CallbacksForGeneratingExpressionCode callbacks(expression1Code, codeGenerator, context); gd::ExpressionParser parser(instruction.GetParameters()[2].GetPlainString()); if (!parser.ParseMathExpression(codeGenerator.GetPlatform(), codeGenerator.GetProject(), codeGenerator.GetLayout(), callbacks) || expression1Code.empty()) expression1Code = "0"; } std::string expression2Code; { gd::CallbacksForGeneratingExpressionCode callbacks(expression2Code, codeGenerator, context); gd::ExpressionParser parser(instruction.GetParameters()[4].GetPlainString()); if (!parser.ParseMathExpression(codeGenerator.GetPlatform(), codeGenerator.GetProject(), codeGenerator.GetLayout(), callbacks) || expression2Code.empty()) expression2Code = "0"; } std::string op1 = instruction.GetParameter(1).GetPlainString(); if ( op1 == "=" || op1.empty() ) newX = expression1Code; else if ( op1 == "/" || op1 == "*" || op1 == "-" || op1 == "+" ) newX = codeGenerator.GetObjectListName(realObjects[i], context)+"[i].getX() "+op1 + expression1Code; else return ""; std::string op2 = instruction.GetParameter(3).GetPlainString(); if ( op2 == "=" || op2.empty() ) newY = expression2Code; else if ( op2 == "/" || op2 == "*" || op2 == "-" || op2 == "+" ) newY = codeGenerator.GetObjectListName(realObjects[i], context)+"[i].getY() "+op2 + expression2Code; else return ""; std::string call = codeGenerator.GetObjectListName(realObjects[i], context)+"[i].setPosition("+newX+","+newY+")"; outputCode += "for(var i = 0, len = "+codeGenerator.GetObjectListName(realObjects[i], context)+".length ;i < len;++i) {\n"; outputCode += " "+call+";\n"; outputCode += "}\n"; context.SetNoCurrentObject(); } return outputCode; };
virtual std::string GenerateCode(gd::Instruction & instruction, gd::EventsCodeGenerator & codeGenerator, gd::EventsCodeGenerationContext & context) { codeGenerator.AddGlobalDeclaration(FunctionEvent::globalDeclaration); std::string functionName = instruction.GetParameter(0).GetPlainString(); const gd::Project & project = codeGenerator.GetProject(); const gd::Layout & scene = codeGenerator.GetLayout(); const FunctionEvent * functionEvent = FunctionEvent::SearchForFunctionInEvents(scene.GetEvents(), functionName); if ( !functionEvent ) { std::cout << "Function \""+functionName+"\" not found!" << std::endl; return "//Function \""+functionName+"\" not found.\n"; } std::string code; //Generate code for objects passed as arguments std::string objectsAsArgumentCode; { objectsAsArgumentCode += "runtimeContext->ClearObjectListsMap()"; std::vector<std::string> realObjects = codeGenerator.ExpandObjectsName(functionEvent->GetObjectsPassedAsArgument(), context); for (unsigned int i = 0;i<realObjects.size();++i) { context.EmptyObjectsListNeeded(realObjects[i]); objectsAsArgumentCode += ".AddObjectListToMap(\""+codeGenerator.ConvertToString(realObjects[i])+"\", "+ManObjListName(realObjects[i])+")"; } objectsAsArgumentCode += ".ReturnObjectListsMap()"; } //Generate code for evaluating parameters code += "std::vector<std::string> functionParameters;\n"; for (unsigned int i = 1;i<8;++i) { std::string parameterCode; gd::CallbacksForGeneratingExpressionCode callbacks(parameterCode, codeGenerator, context); gd::ExpressionParser parser(instruction.GetParameter(i).GetPlainString()); parser.ParseStringExpression(CppPlatform::Get(), project, scene, callbacks); if (parameterCode.empty()) parameterCode = "\"\""; code += "functionParameters.push_back("+parameterCode+");\n"; } code += "std::vector<std::string> * oldFunctionParameters = currentFunctionParameters;\n"; code += "currentFunctionParameters = &functionParameters;\n"; code += FunctionEvent::MangleFunctionName(*functionEvent)+"(runtimeContext, "+objectsAsArgumentCode+");\n"; code += "currentFunctionParameters = oldFunctionParameters;\n"; return code; };
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; }