BVB Source Codes

CRYENGINE Show DialogLoaderMK2.cpp Source code

Return Download CRYENGINE: download DialogLoaderMK2.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. // -------------------------------------------------------------------------
  4. //  File name:   DialogLoaderMK2.cpp
  5. //  Version:     v1.00
  6. //  Created:     07/07/2006 by AlexL
  7. //  Compilers:   Visual Studio.NET
  8. //  Description: Dialog Loader
  9. // -------------------------------------------------------------------------
  10. //  History:
  11. //
  12. ////////////////////////////////////////////////////////////////////////////
  13. #include "StdAfx.h"
  14. #include "DialogLoaderMK2.h"
  15. #include "DialogCommon.h"
  16. #include <CryString/StringUtils.h>
  17. #include "ILevelSystem.h"
  18.  
  19. ////////////////////////////////////////////////////////////////////////////
  20. CDialogLoaderMK2::CDialogLoaderMK2(CDialogSystem* pDS) : m_pDS(pDS)
  21. {
  22.  
  23. }
  24.  
  25. ////////////////////////////////////////////////////////////////////////////
  26. CDialogLoaderMK2::~CDialogLoaderMK2()
  27. {
  28.  
  29. }
  30.  
  31. void CDialogLoaderMK2::InternalLoadFromPath(const string& stripPath, const string& path, TDialogScriptMap& outScriptMap, int& numLoaded, const char* levelName)
  32. {
  33.         ICryPak* pCryPak = gEnv->pCryPak;
  34.         _finddata_t fd;
  35.  
  36.         string realPath(path);
  37.         realPath.TrimRight("/\\");
  38.         string search(realPath);
  39.         search += "/*.*";
  40.  
  41.         intptr_t handle = pCryPak->FindFirst(search.c_str(), &fd);
  42.         if (handle != -1)
  43.         {
  44.                 do
  45.                 {
  46.                         if (strcmp(fd.name, ".") == 0 || strcmp(fd.name, "..") == 0)
  47.                                 continue;
  48.  
  49.                         if (fd.attrib & _A_SUBDIR)
  50.                         {
  51.                                 if (!gEnv->IsEditor()) //only load current levels dialogs
  52.                                 {
  53.                                         if (!levelName || stricmp(levelName, fd.name))
  54.                                                 continue;
  55.                                 }
  56.  
  57.                                 string subPath = realPath;
  58.                                 subPath += "/";
  59.                                 subPath += fd.name;
  60.                                 InternalLoadFromPath(stripPath, subPath, outScriptMap, numLoaded, levelName);
  61.                                 continue;
  62.                         }
  63.  
  64.                         if (stricmp(PathUtil::GetExt(fd.name), "dlg") != 0)
  65.                                 continue;
  66.  
  67.                         // fd.name contains the profile name
  68.                         string filename = realPath;
  69.                         filename += "/";
  70.                         filename += fd.name;
  71.                         bool ok = LoadScript(stripPath, filename, outScriptMap);
  72.                         if (ok)
  73.                                 ++numLoaded;
  74.                 }
  75.                 while (pCryPak->FindNext(handle, &fd) >= 0);
  76.  
  77.                 pCryPak->FindClose(handle);
  78.         }
  79. }
  80.  
  81. ////////////////////////////////////////////////////////////////////////////
  82. bool CDialogLoaderMK2::LoadScriptsFromPath(const string& path, TDialogScriptMap& outScriptMap, const char* levelName)
  83. {
  84.         string stripPath = path;
  85.         PathUtil::ToUnixPath(stripPath);
  86.         stripPath.TrimRight("/\\");
  87.         stripPath += "/";
  88.  
  89.         int numLoaded = 0;
  90.         InternalLoadFromPath(stripPath, path, outScriptMap, numLoaded, levelName);
  91.         return numLoaded > 0;
  92. }
  93.  
  94. ////////////////////////////////////////////////////////////////////////////
  95. bool CDialogLoaderMK2::LoadScript(const string& stripPath, const string& filename, TDialogScriptMap& outScriptMap)
  96. {
  97.         XmlNodeRef rootNode = GetISystem()->LoadXmlFromFile(filename);
  98.         if (!rootNode)
  99.         {
  100.                 GameWarning("[DIALOG] CDialogLoaderMK2::LoadScripts: Cannot find/load file '%s'", filename.c_str());
  101.                 return false;
  102.         }
  103.  
  104.         if (rootNode->isTag("DialogScript") == false)
  105.         {
  106.                 GameWarning("[DIALOG] CDialogLoaderMK2::LoadScripts: File '%s' not a dialog script.", filename.c_str());
  107.                 return false;
  108.         }
  109.  
  110.         string scriptName = PathUtil::ToUnixPath(filename);
  111.         // now remove prefix
  112.         if (CryStringUtils::stristr(scriptName.c_str(), stripPath.c_str()) == scriptName.c_str())
  113.                 scriptName = scriptName.Mid(stripPath.length());
  114.  
  115.         PathUtil::RemoveExtension(scriptName);
  116.         scriptName.replace('/', '.');
  117.  
  118.         // Make nice uppercase name, if storedId and filename match case-insensitive
  119.         const char* storedId = rootNode->getAttr("Name");
  120.         if (storedId != 0 && stricmp(storedId, scriptName.c_str()) == 0)
  121.         {
  122.                 scriptName.assign(storedId);
  123.         }
  124.  
  125.         CDialogScript* pScript = new CDialogScript(scriptName);
  126.         bool bOK = ProcessScript(pScript, rootNode);
  127.  
  128.         if (bOK)
  129.         {
  130.                 // try to complete the script
  131.                 if (pScript->Complete() == true)
  132.                 {
  133.                         // add it to the map
  134.                         std::pair<TDialogScriptMap::iterator, bool> inserted =
  135.                           outScriptMap.insert(TDialogScriptMap::value_type(pScript->GetID(), pScript));
  136.                         if (inserted.second == false)
  137.                         {
  138.                                 bOK = false;
  139.                                 GameWarning("[DIALOG] CDialogLoaderMK2::ProcessScript '%s': Script already defined. Discarded", scriptName.c_str());
  140.                         }
  141.                 }
  142.                 // completion not successful -> discard
  143.                 else
  144.                 {
  145.                         bOK = false;
  146.                         GameWarning("[DIALOG] CDialogLoaderMK2::ProcessScript '%s': Cannot complete Script. Discarded.", scriptName.c_str());
  147.                 }
  148.         }
  149.  
  150.         // discard pScript
  151.         if (bOK == false)
  152.         {
  153.                 delete pScript;
  154.         }
  155.  
  156.         return bOK;
  157. }
  158.  
  159. // returns actor's ID in outID [1 based]
  160. // or 0, when not found
  161. ////////////////////////////////////////////////////////////////////////////
  162. bool CDialogLoaderMK2::GetActor(const char* actor, int& outID)
  163. {
  164.         static const char* actorPrefix = "actor";
  165.         static const int actorPrefixLen = strlen(actorPrefix);
  166.  
  167.         assert(actor != 0 && *actor != '\0');
  168.         if (actor == 0 || *actor == '\0') // safety
  169.         {
  170.                 outID = 0;
  171.                 return true;
  172.         }
  173.  
  174.         const char* found = CryStringUtils::stristr(actor, actorPrefix);
  175.         if (found && sscanf(found + actorPrefixLen, "%d", &outID) == 1)
  176.                 return true;
  177.         outID = 0;
  178.         return false;
  179. }
  180.  
  181. // returns actor's ID in outID [1 based]
  182. // or 0, when not found
  183. ////////////////////////////////////////////////////////////////////////////
  184. bool CDialogLoaderMK2::GetLookAtActor(const char* actor, int& outID, bool& outSticky)
  185. {
  186.         static const char* actorPrefix = "actor";
  187.         static const int actorPrefixLen = strlen(actorPrefix);
  188.  
  189.         assert(actor != 0 && *actor != '\0');
  190.         if (actor == 0 || *actor == '\0') // safety
  191.         {
  192.                 outID = 0;
  193.                 outSticky = false;
  194.                 return true;
  195.         }
  196.  
  197.         if (actor[0] == '$')
  198.         {
  199.                 outSticky = true;
  200.                 ++actor;
  201.         }
  202.         else
  203.                 outSticky = false;
  204.  
  205.         const char* found = CryStringUtils::stristr(actor, actorPrefix);
  206.         if (found && sscanf(found + actorPrefixLen, "%d", &outID) == 1)
  207.                 return true;
  208.         outID = 0;
  209.         return false;
  210. }
  211.  
  212. ////////////////////////////////////////////////////////////////////////////
  213. bool CDialogLoaderMK2::ProcessScript(CDialogScript* pScript, const XmlNodeRef& node)
  214. {
  215.         CDialogScript::SScriptLine scriptLine;
  216.  
  217.         const char* scriptID = pScript->GetID();
  218.         string desc = node->getAttr("Description");
  219.         pScript->SetDescription(desc);
  220.  
  221.         for (int i = 0; i < node->getChildCount(); ++i)
  222.         {
  223.                 XmlNodeRef lineNode = node->getChild(i);
  224.                 if (lineNode && lineNode->isTag("Line"))
  225.                 {
  226.                         ResetLine(scriptLine);
  227.                         if (ReadLine(lineNode, scriptLine, scriptID, i) == true)
  228.                         {
  229.                                 pScript->AddLine(scriptLine);
  230.                         }
  231.                 }
  232.         }
  233.         return true;
  234. }
  235.  
  236. void CDialogLoaderMK2::ResetLine(CDialogScript::SScriptLine& scriptLine)
  237. {
  238.         scriptLine.m_actor = CDialogScript::NO_ACTOR_ID;
  239.         scriptLine.m_lookatActor = CDialogScript::NO_ACTOR_ID;
  240.         scriptLine.m_flagLookAtSticky = false;
  241.         scriptLine.m_flagResetFacial = false;
  242.         scriptLine.m_flagResetLookAt = false;
  243.         scriptLine.m_flagSoundStopsAnim = false;
  244.         scriptLine.m_flagAGSignal = false;
  245.         scriptLine.m_flagAGEP = false;
  246.         scriptLine.m_audioID = INVALID_AUDIO_CONTROL_ID;
  247.         //scriptLine.m_sound = "";
  248.         scriptLine.m_anim = "";
  249.         scriptLine.m_facial = "";
  250.         scriptLine.m_delay = 0.0f;
  251.         scriptLine.m_facialWeight = 0.0f;
  252.         scriptLine.m_facialFadeTime = 0.0f;
  253. }
  254.  
  255. bool CDialogLoaderMK2::ReadLine(const XmlNodeRef& lineNode, CDialogScript::SScriptLine& line, const char* scriptID, int lineNumber)
  256. {
  257.         if (lineNode->getAttr("actor", line.m_actor) == false)
  258.         {
  259.                 GameWarning("[DIALOG] CDialogLoaderMK2::ProcessScript '%s': No actor given in line %d", scriptID, lineNumber);
  260.                 return false;
  261.         }
  262.  
  263.         line.m_flagResetLookAt = false;
  264.         if (lineNode->getAttr("lookatActor", line.m_lookatActor))
  265.         {
  266.                 if (line.m_lookatActor == CDialogScript::STICKY_LOOKAT_RESET_ID)
  267.                 {
  268.                         line.m_flagResetLookAt = true;
  269.                         line.m_lookatActor = CDialogScript::NO_ACTOR_ID;
  270.                 }
  271.         }
  272.  
  273.         bool tmp;
  274.         if (lineNode->getAttr("flagLookAtSticky", tmp)) line.m_flagLookAtSticky = tmp;
  275.         if (lineNode->getAttr("flagSoundStopsAnim", tmp)) line.m_flagSoundStopsAnim = tmp;
  276.         if (lineNode->getAttr("flagAGSignal", tmp)) line.m_flagAGSignal = tmp;
  277.         if (lineNode->getAttr("flagAGEP", tmp)) line.m_flagAGEP = tmp;
  278.         const char* audioIDString = lineNode->getAttr("audioID");
  279.         if (audioIDString != 0)
  280.                 gEnv->pAudioSystem->GetAudioTriggerId(audioIDString, line.m_audioID);
  281.         else
  282.                 line.m_audioID = INVALID_AUDIO_CONTROL_ID;
  283.         line.m_anim = lineNode->getAttr("anim");
  284.         line.m_facial = lineNode->getAttr("facial");
  285.         if (CryStringUtils::stristr(line.m_facial, "#RESET#") != 0)
  286.         {
  287.                 line.m_flagResetFacial = true;
  288.                 line.m_facial = "";
  289.         }
  290.         lineNode->getAttr("delay", line.m_delay);
  291.         lineNode->getAttr("facialWeight", line.m_facialWeight);
  292.         lineNode->getAttr("facialFadeTime", line.m_facialFadeTime);
  293.         return true;
  294. }
  295.  
downloadDialogLoaderMK2.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top