1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-26 06:27:45 +00:00

update quest_parser for c++ scripting

- fixed pcb_reader path
This commit is contained in:
Tahir Akhlaq 2017-12-16 13:13:49 +00:00
parent 05e48baf7b
commit 83f88102f9
3 changed files with 185 additions and 115 deletions

View file

@ -6,9 +6,10 @@ file(GLOB_RECURSE SCRIPT_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
include_directories("${CMAKE_SOURCE_DIR}/src/servers/")
include_directories("${CMAKE_SOURCE_DIR}/src/servers/Server_Zone/")
include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Yc${CMAKE_CURRENT_SOURCE_DIR}/ScriptObject.h /FI${CMAKE_CURRENT_SOURCE_DIR}/ScriptObject.h")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Yc\"${CMAKE_CURRENT_SOURCE_DIR}/ScriptObject.h\" /FI\"${CMAKE_CURRENT_SOURCE_DIR}/ScriptObject.h\"")
endif()
message( "exec: ${EXECUTABLE_OUTPUT_DIRECTORY}" )

View file

@ -20,6 +20,8 @@
//#include <boost/algorithm/string.hpp>
#endif
std::string gamePath( "C:\\Program Files (x86)\\SquareEnix\\FINAL FANTASY XIV - A Realm Reborn\\game\\sqpack\\ffxiv" );
using namespace std::chrono_literals;
struct face
@ -103,18 +105,19 @@ std::string zoneNameToPath( const std::string& name )
}
}
}
inFile.close();
}
if( !path.empty() )
{
//path = path.substr( path.find_first_of( "/" ) + 1, path.size() - path.find_first_of( "/" ));
//path = std::string( "ffxiv/" ) + path;
path = std::string( "bg" ) + path.substr( 0, path.find( "/level/" ) );
path = std::string( "bg/" ) + path.substr( 0, path.find( "/level/" ) );
std::cout << "[Info] " << "Found path for " << name << ": " << path << std::endl;
}
else
{
throw std::runtime_error( "Unable to find path for " + name +
".\n\tPlease open 0a0000.win32.index with FFXIV Explorer and extract territorytype.exh as CSV\n\tand copy territorytype.exh.csv into pcb_reader.exe directory" );
".\n\tPlease double check spelling or open 0a0000.win32.index with FFXIV Explorer and extract territorytype.exh as CSV\n\tand copy territorytype.exh.csv into pcb_reader.exe directory if using standalone" );
}
return path;
}
@ -142,7 +145,6 @@ int main( int argc, char* argv[] )
auto startTime = std::chrono::system_clock::now();
// todo: support expansions
std::string gamePath = "C:\\Program Files (x86)\\SquareEnix\\FINAL FANTASY XIV - A Realm Reborn\\game\\sqpack\\ffxiv";
std::string zoneName = "r1f1";
if( argc > 1 )
@ -166,7 +168,6 @@ int main( int argc, char* argv[] )
#ifndef STANDALONE
xiv::dat::GameData data1( gamePath );
xiv::exd::ExdData eData( data1 );
const xiv::dat::Cat& test = data1.get_category( "bg" );
auto test_file = data1.get_file( bgLgbPath );

View file

@ -9,30 +9,73 @@
#include <set>
#include <src/servers/Server_Common/Exd/ExdData.h>
#include <src/servers/Server_Common/Logging/Logger.h>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <locale>
#include <fstream>
Core::Logger g_log;
Core::Data::ExdData g_exdData;
void createScript( boost::shared_ptr< Core::Data::QuestInfo > pQuestData, std::set< std::string > additionalList )
{
std::string header(
"// This is an automatically generated chai script template\n"
"// Content needs to be added by hand to make it function\n"
"// In order for this script to be loaded, change its extension to .chai\n"
"// and move it to the correct folder in bin/scripts/chai/quest\n"
"\n"
const std::string onTalkStr(
" void onTalk( uint32_t eventId, Entity::Player& player, uint64_t actorId )\n"
" {\n"
" auto actor = Core::Event::mapEventActorToRealActor( actorId );\n"
" }\n\n"
);
std::size_t splitPos = pQuestData->name_intern.find( "_" );
std::string className = pQuestData->name_intern.substr( 0, splitPos );
const std::string onWithinRangeStr(
" void onWithinRange( uint32_t eventId, Entity::Player& player, uint64_t eRangeId, float x, float y, float z )\n"
" {\n"
" }\n\n"
);
std::string sceneStr = " //////////////////////////////////////////////////////////////////////\n // Available Scenes in this quest, not necessarly all are used\n";
std::string seqStr = " // Steps in this quest ( 0 is before accepting, \n // 1 is first, 255 means ready for turning it in\n";
std::string questVarStr = " // Quest vars / flags used\n";
const std::string onEmoteStr(
" void onEmote( uint32_t eventId, Entity::Player& player, uint64_t actorId, uint32_t emoteId )\n"
" {\n"
" }\n\n"
);
std::string titleCase( const std::string& str )
{
if( str.empty() )
return str;
std::string retStr(str);
std::transform( str.begin(), str.end(), retStr.begin(), ::tolower );
std::locale loc;
retStr[0] = std::toupper( str[0], loc );
for( size_t i = 1; i < str.size(); ++i )
{
if( str[i - 1] == ' ' || str[i - 1] == '_' || ( std::isdigit( str[i - 1], loc ) && !std::isdigit( str[i], loc ) ) )
retStr[i] = std::toupper( str[i], loc );
}
return retStr;
}
void createScript( boost::shared_ptr< Core::Data::QuestInfo >& pQuestData, std::set< std::string >& additionalList )
{
std::string header(
"// This is an automatically generated C++ script template\n"
"// Content needs to be added by hand to make it function\n"
"// In order for this script to be loaded, change its extension to .cpp\n"
"// and move it to the correct folder in <root>/scripts/native/\n"
"\n"
"#include <ScriptObject.h>\n\n"
);
std::size_t splitPos( pQuestData->name_intern.find( "_" ) );
std::string className( pQuestData->name_intern.substr( 0, splitPos ) );
//className = "Quest" + className;
std::string sceneStr( " //////////////////////////////////////////////////////////////////////\n // Available Scenes in this quest, not necessarly all are used\n" );
std::string seqStr;
seqStr.reserve( 0xFFF );
seqStr += ( " // Steps in this quest ( 0 is before accepting, \n // 1 is first, 255 means ready for turning it in\n" );
std::string questVarStr( " // Quest vars / flags used\n" );
seqStr += " enum Sequence : uint8_t\n {\n";
for( auto &entry : additionalList )
{
if( entry.find( "OnScene" ) != std::string::npos )
@ -43,12 +86,19 @@ void createScript( boost::shared_ptr< Core::Data::QuestInfo > pQuestData, std::s
std::size_t numOff = sceneId.find_first_not_of( "0" );
sceneId = numOff != std::string::npos ? sceneId.substr( numOff ) : "0";
sceneStr += " def " + sceneName + "( player )\n";
sceneStr += " {\n";
sceneStr += " player.eventPlay( this.id, " + sceneId + ", NONE,\n";
sceneStr += " fun( player, eventId, param1, param2, param3 )\n";
sceneStr += " {} );\n";
sceneStr += " }\n\n";
sceneStr += std::string(
" void " +
sceneName +
"( Entity::Player& player )\n"
" {\n"
" player.eventPlay( this->getId(), " +
sceneId +
", 0,\n"
" [&]( Entity::Player& player, uint32_t eventId, uint16_t param1, uint16_t param2, uint16_t param3 )\n"
" {\n"
" });\n"
" }\n\n"
);
}
else if( entry.find( "Flag" ) != std::string::npos ||
entry.find( "QuestUI" ) != std::string::npos )
@ -59,38 +109,41 @@ void createScript( boost::shared_ptr< Core::Data::QuestInfo > pQuestData, std::s
{
if( entry.find( "SEQ_FINISH" ) != std::string::npos )
{
seqStr += " this.SEQ_FINISH = 255;\n";
seqStr += " SeqFinish = 255,\n";
}
else if( entry.find( "SEQ_OFFER" ) != std::string::npos )
{
}
else
{
std::string seqName = entry;
std::string seqName = titleCase( entry );
boost::replace_all( seqName, "_", "" );
std::string seqId = entry.substr( 4 );
seqStr += " this." + seqName + " = " + seqId + ";\n";
seqStr += " " + seqName + " = " + seqId + ",\n";
}
}
}
std::string rewards = " // Quest rewards \n";
rewards += ( pQuestData->reward_exp_factor != 0 ) ? " this.RewardExpFactor = " + std::to_string( pQuestData->reward_exp_factor ) + ";\n" : "";
rewards += ( pQuestData->reward_gil != 0 ) ? " this.RewardGil = " + std::to_string( pQuestData->reward_gil ) + ";\n" : "";
rewards += ( pQuestData->reward_emote != 0 ) ? " this.RewardEmote = " + std::to_string( pQuestData->reward_emote ) + ";\n" : "";
rewards += ( pQuestData->reward_action != 0 ) ? " this.RewardAction = " + std::to_string( pQuestData->reward_action ) + ";\n" : "";
rewards += ( pQuestData->reward_action_general1 != 0 ) ? " this.RewardGeneralAction1 = " + std::to_string( pQuestData->reward_action_general1 ) + ";\n" : "";
rewards += ( pQuestData->reward_action_general2 != 0 ) ? " this.RewardGeneralAction2 = " + std::to_string( pQuestData->reward_action_general2 ) + ";\n" : "";
rewards += ( pQuestData->reward_gc_seals != 0 ) ? " this.RewardGCSeals = " + std::to_string( pQuestData->reward_gc_seals ) + ";\n" : "";
rewards += ( pQuestData->reward_other != 0 ) ? " this.RewardOther = " + std::to_string( pQuestData->reward_other ) + ";\n" : "";
rewards += ( pQuestData->reward_reputation != 0 ) ? " this.RewardReputation = " + std::to_string( pQuestData->reward_reputation ) + ";\n" : "";
rewards += ( pQuestData->reward_tome_type != 0 ) ? " this.RewardTomeType = " + std::to_string( pQuestData->reward_tome_type ) + ";\n" : "";
rewards += ( pQuestData->reward_tome_count != 0 ) ? " this.RewardTomeCount = " + std::to_string( pQuestData->reward_tome_count ) + ";\n" : "";
rewards += ( pQuestData->instanced_content_unlock != 0 ) ? " this.InstancedContentUnlock = " + std::to_string( pQuestData->instanced_content_unlock ) + ";\n" : "";
seqStr += " };\n";
std::string rewards;
rewards.reserve( 0xFFF );
rewards += " // Quest rewards \n";
rewards += ( pQuestData->reward_exp_factor != 0 ) ? " static constexpr auto RewardExpFactor = " + std::to_string( pQuestData->reward_exp_factor ) + ";\n" : "";
rewards += ( pQuestData->reward_gil != 0 ) ? " static constexpr auto RewardGil = " + std::to_string( pQuestData->reward_gil ) + ";\n" : "";
rewards += ( pQuestData->reward_emote != 0 ) ? " static constexpr auto RewardEmote = " + std::to_string( pQuestData->reward_emote ) + ";\n" : "";
rewards += ( pQuestData->reward_action != 0 ) ? " static constexpr auto RewardAction = " + std::to_string( pQuestData->reward_action ) + ";\n" : "";
rewards += ( pQuestData->reward_action_general1 != 0 ) ? " static constexpr auto RewardGeneralAction1 = " + std::to_string( pQuestData->reward_action_general1 ) + ";\n" : "";
rewards += ( pQuestData->reward_action_general2 != 0 ) ? " static constexpr auto RewardGeneralAction2 = " + std::to_string( pQuestData->reward_action_general2 ) + ";\n" : "";
rewards += ( pQuestData->reward_gc_seals != 0 ) ? " static constexpr auto RewardGCSeals = " + std::to_string( pQuestData->reward_gc_seals ) + ";\n" : "";
rewards += ( pQuestData->reward_other != 0 ) ? " static constexpr auto RewardOther = " + std::to_string( pQuestData->reward_other ) + ";\n" : "";
rewards += ( pQuestData->reward_reputation != 0 ) ? " static constexpr auto RewardReputation = " + std::to_string( pQuestData->reward_reputation ) + ";\n" : "";
rewards += ( pQuestData->reward_tome_type != 0 ) ? " static constexpr auto RewardTomeType = " + std::to_string( pQuestData->reward_tome_type ) + ";\n" : "";
rewards += ( pQuestData->reward_tome_count != 0 ) ? " static constexpr auto RewardTomeCount = " + std::to_string( pQuestData->reward_tome_count ) + ";\n" : "";
rewards += ( pQuestData->instanced_content_unlock != 0 ) ? " static constexpr auto InstancedContentUnlock = " + std::to_string( pQuestData->instanced_content_unlock ) + ";\n" : "";
if( pQuestData->reward_item.size() > 0 )
{
rewards += " this.RewardItem = [";
rewards += " static constexpr auto RewardItem[] = { ";
for( size_t ca = 0; ca < pQuestData->reward_item.size(); ca++ )
{
rewards += std::to_string( pQuestData->reward_item.at( ca ) );
@ -99,12 +152,12 @@ void createScript( boost::shared_ptr< Core::Data::QuestInfo > pQuestData, std::s
rewards += ", ";
}
}
rewards += "];\n";
rewards += " };\n";
}
if( pQuestData->reward_item.size() > 0 )
{
rewards += " this.RewardItemCount = [";
rewards += " static constexpr auto RewardItemCount[] = { ";
for( size_t ca = 0; ca < pQuestData->reward_item_count.size(); ca++ )
{
rewards += std::to_string( pQuestData->reward_item_count.at( ca ) );
@ -113,12 +166,12 @@ void createScript( boost::shared_ptr< Core::Data::QuestInfo > pQuestData, std::s
rewards += ", ";
}
}
rewards += "];\n";
rewards += " };\n";
}
if( pQuestData->reward_item_optional.size() > 0 )
{
rewards += " this.RewardItemOptional = [";
rewards += " static constexpr auto RewardItemOptional[] = { ";
for( size_t ca = 0; ca < pQuestData->reward_item_optional.size(); ca++ )
{
rewards += std::to_string( pQuestData->reward_item_optional.at( ca ) );
@ -127,12 +180,12 @@ void createScript( boost::shared_ptr< Core::Data::QuestInfo > pQuestData, std::s
rewards += ", ";
}
}
rewards += "];\n";
rewards += " };\n";
}
if( pQuestData->reward_item_optional_count.size() > 0 )
{
rewards += " this.RewardItemOptionalCount = [";
rewards += " static constexpr auto RewardItemOptionalCount[] = { ";
for( size_t ca = 0; ca < pQuestData->reward_item_optional_count.size(); ca++ )
{
rewards += std::to_string( pQuestData->reward_item_optional_count.at( ca ) );
@ -141,7 +194,7 @@ void createScript( boost::shared_ptr< Core::Data::QuestInfo > pQuestData, std::s
rewards += ", ";
}
}
rewards += "];\n";
rewards += " };\n";
}
bool hasERange = false;
@ -168,10 +221,11 @@ void createScript( boost::shared_ptr< Core::Data::QuestInfo > pQuestData, std::s
script_entities.push_back( pQuestData->script_entity.at( ca ) + " = " + std::to_string( pQuestData->script_value.at( ca ) ) );
}
std::sort( script_entities.begin(), script_entities.end() );
for( auto& entity : script_entities )
{
sentities += " this." + entity + ";\n";
auto name = titleCase( entity );
boost::replace_all( name, "_", "" );
sentities += " static constexpr auto " + name + ";\n";
}
std::string additional = "// Quest Script: " + pQuestData->name_intern + "\n";
@ -180,78 +234,81 @@ void createScript( boost::shared_ptr< Core::Data::QuestInfo > pQuestData, std::s
additional += "// Start NPC: " + std::to_string( pQuestData->enpc_resident_start ) + "\n";
additional += "// End NPC: " + std::to_string( pQuestData->enpc_resident_end ) + "\n\n";
std::string scriptEntry = " //////////////////////////////////////////////////////////////////////\n";
scriptEntry += " // Event Handlers\n";
scriptEntry += " def onTalk( eventId, player, actorId )\n";
scriptEntry += " {\n";
scriptEntry += " var actor = mapActor( actorId );\n";
scriptEntry += " }\n\n";
std::string scriptEntry;
scriptEntry.reserve(0xFFFF);
scriptEntry += " //////////////////////////////////////////////////////////////////////\n // Event Handlers\n";
scriptEntry += onTalkStr;
if( hasERange )
{
scriptEntry += " def onWithinRange( eventId, player, eRangeId, x, y, z )\n";
scriptEntry += " {\n";
scriptEntry += " }\n\n";
scriptEntry += onWithinRangeStr;
}
if( hasEmote )
{
scriptEntry += " def onEmote( eventId, player, actorId, emoteId )\n";
scriptEntry += " {\n";
scriptEntry += " }\n\n";
scriptEntry += onEmoteStr;
}
for( auto enemy : enemy_ids )
{
scriptEntry += " def onMobKill_" + std::to_string(enemy) + "( player )\n";
scriptEntry += " {\n";
scriptEntry += " }\n\n";
scriptEntry += std::string(
" void onMobKill_" + std::to_string( enemy ) + "( Entity::Player& player )\n"
" {\n"
" }\n\n"
);
}
std::string constructor;
constructor += " def " + className + "Def()\n";
constructor += " {\n";
constructor += " // Basic quest information \n";
constructor += " this.name = \"" + pQuestData->name + "\";\n";
constructor += " this.id = " + std::to_string( pQuestData->id ) + ";\n\n";
constructor += std::string(
" private:\n"
" // Basic quest information \n");
constructor += questVarStr + "\n";
constructor += seqStr + "\n";
constructor += rewards + "\n";
constructor += sentities + "\n";
constructor += " }\n";
constructor += " public:\n";
constructor += " " + className + "() : EventScript" + "( \"" + pQuestData->name + "\", " + std::to_string( pQuestData->id ) + " ){}; \n";
constructor += " ~" + className + "(){}; \n";
std::string classString(
"class " + className + "Def \n"
"{\n"
+ constructor +
"\n"
+ sceneStr +
"class " + className + " : public EventScript\n"
"{\n" +
constructor +
"\n" +
sceneStr +
scriptEntry +
"};\n\n"
);
std::string objName = className;
std::string initObj = "GLOBAL " + objName + " = " + className + "Def();\n";
std::string initObj( "EXPORT_SCRIPTOBJECT( " + className + " );" );
std::ofstream outputFile;
outputFile.open( "generated/" + className + ".chai_generated" );
outputFile.open( "generated/" + className + ".cpp_generated" );
outputFile << header << additional << classString << initObj;
outputFile.close();
}
int main()
int main( int argc, char** argv )
{
g_log.init();
bool unluac = false;
// std::string datLocation( "/opt/sapphire_3_15_0/bin/sqpack" );
std::string datLocation( "C:\\SquareEnix\\FINAL FANTASY XIV - A Realm Reborn\\game\\sqpack\\ffxiv" );
std::string datLocation( "C:/Program Files (x86)/SquareEnix/FINAL FANTASY XIV - A Realm Reborn/game/sqpack/ffxiv" );
if ( argc > 1 )
datLocation = std::string( argv[1] );
if ( argc > 2 )
unluac = (bool)atoi( argv[2] );
g_log.info( "Setting up EXD data" );
if( !g_exdData.init( datLocation ) )
{
std::cout << datLocation << "\n";
g_log.fatal( "Error setting up EXD data " );
std::cout << "Usage: quest_parser \"path/to/ffxiv/game/sqpack\" <1/0 unluac export toggle>\n";
return 0;
}
@ -261,9 +318,14 @@ int main()
auto QuestDat = g_exdData.setupDatAccess( "Quest", xiv::exd::Language::en );
auto rows = QuestDat.get_rows();
for( auto row : rows )
{
if ( !boost::filesystem::exists( "./generated" ) )
boost::filesystem::create_directory( "./generated" );
std::cout << "Export in progress";
uint32_t updateInterval = rows.size() / 20;
uint32_t i = 0;
for( const auto& row : rows )
{
auto questInfo = g_exdData.getQuestInfo( row.first );
if( questInfo->name.empty() || questInfo->name_intern.empty() )
@ -302,12 +364,15 @@ int main()
outputFile1.open( "generated/" + questInfo->name_intern + ".luab", std::ios::binary );
outputFile1.write( &section[0], section.size() );
outputFile1.close();
if( unluac )
{
std::string command = std::string( "java -jar unluac_2015_06_13.jar " ) + "generated/" + questInfo->name_intern + ".luab" + ">> " + "generated/" + questInfo->name_intern + ".lua";
if ( system( command.c_str() ) == -1 )
{
g_log.error( "Error executing java command:\n" + command + "\nerrno: " + std::strerror( errno ) );
return errno;
}
}
for( ; ; )
{
@ -315,7 +380,7 @@ int main()
offset += entry.size() + 1;
if( entry.size() > 3
&& entry.find_first_not_of( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_" ) == std::string::npos )
&& entry.find_first_not_of( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_-" ) == std::string::npos )
{
if( entry.find( "SEQ" ) != std::string::npos
|| entry.find( "QuestUI" ) != std::string::npos
@ -333,8 +398,11 @@ int main()
createScript( questInfo, stringList );
++i;
if( i % updateInterval == 0 )
std::cout << ".";
//break;
}
std::cout << "\nDone!";
return 0;
}