BuildData/BuildLibrary.cpp

106 lines
4.9 KiB
C++
Raw Normal View History

2021-08-28 14:05:31 +00:00
#include "BuildLibrary.hpp"
#include <PkmnLib/ScriptResolving/AngelScript/AngelScriptResolver.hpp>
2021-11-20 08:55:09 +00:00
#include "BuildAbilities.hpp"
2021-08-28 14:05:31 +00:00
using namespace PkmnLib::Battling;
size_t TypeTimeMs;
size_t NaturesTimeMs;
size_t MovesTimeMs;
2021-11-20 08:55:09 +00:00
size_t AbilitiesTimeMs;
2021-08-28 14:05:31 +00:00
size_t SpeciesTimeMs;
size_t ItemsTimeMs;
size_t GrowthRateTimeMs;
size_t ScriptsTimeMs;
void BuildLibrary::LogBuildTimes() {
std::cout << "\tType Library Time: " << TypeTimeMs << "ms" << std::endl;
std::cout << "\tNatures Library Time: " << NaturesTimeMs << "ms" << std::endl;
std::cout << "\tMoves Library Time: " << MovesTimeMs << "ms" << std::endl;
std::cout << "\tSpecies Library Time: " << SpeciesTimeMs << "ms" << std::endl;
std::cout << "\tItems Library Time: " << ItemsTimeMs << "ms" << std::endl;
std::cout << "\tGrowth rates Library Time: " << GrowthRateTimeMs << "ms" << std::endl;
std::cout << "\tScripts Time: " << ScriptsTimeMs << "ms" << std::endl;
}
BattleLibrary* BuildLibrary::Build(const std::string& pathString,
std::function<void(ScriptResolver*)> onScriptInitialize) {
auto path = std::filesystem::path(pathString);
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
2021-08-29 10:14:27 +00:00
auto* typesLibrary = BuildTypes::Build((path / "Types.csv").generic_string());
2021-08-28 14:05:31 +00:00
TypeTimeMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
begin = std::chrono::steady_clock::now();
2021-08-29 10:14:27 +00:00
auto* natureLibrary = BuildNatures::Build((path / "Natures.csv").generic_string());
2021-08-28 14:05:31 +00:00
NaturesTimeMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
begin = std::chrono::steady_clock::now();
2021-08-29 10:14:27 +00:00
auto* movesLibrary = BuildMoves::Build((path / "Moves.json").generic_string(), typesLibrary);
2021-08-28 14:05:31 +00:00
MovesTimeMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
begin = std::chrono::steady_clock::now();
2021-11-20 08:55:09 +00:00
auto* talentsLibraries = BuildAbilities::Build((path / "Abilities.json").generic_string());
AbilitiesTimeMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
2021-08-28 14:05:31 +00:00
2021-11-20 08:55:09 +00:00
auto* speciesLibrary = BuildSpecies::BuildLibrary((path / "Pokemon.json").generic_string(), typesLibrary,
talentsLibraries, movesLibrary);
2021-08-28 14:05:31 +00:00
SpeciesTimeMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
begin = std::chrono::steady_clock::now();
2021-08-29 10:14:27 +00:00
auto* itemsLibrary = BuildItems::Build((path / "Items.json").generic_string());
2021-08-28 14:05:31 +00:00
ItemsTimeMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
begin = std::chrono::steady_clock::now();
2021-08-29 10:14:27 +00:00
auto* growthRates = GrowthRatesBuilder::Build((path / "GrowthRates.json").generic_string());
2021-08-28 14:05:31 +00:00
GrowthRateTimeMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
auto scriptsPath = path / "Scripts";
if (typesLibrary == nullptr || speciesLibrary == nullptr || natureLibrary == nullptr || movesLibrary == nullptr ||
itemsLibrary == nullptr || growthRates == nullptr) {
return nullptr;
}
auto settings = new PkmnLib::Library::LibrarySettings(100, 4, 4096);
2021-11-20 08:55:09 +00:00
auto staticLibrary =
new PkmnLib::Library::PokemonLibrary(settings, speciesLibrary, movesLibrary, itemsLibrary, growthRates,
typesLibrary, talentsLibraries, natureLibrary);
2021-08-28 14:05:31 +00:00
begin = std::chrono::steady_clock::now();
auto scriptResolver = BattleLibrary::CreateScriptResolver();
auto battleLib = new BattleLibrary(staticLibrary, new StatCalculator(), new DamageLibrary(false),
new ExperienceLibrary(), scriptResolver, new MiscLibrary(GetTime));
scriptResolver->Initialize(battleLib);
if (onScriptInitialize) {
onScriptInitialize(scriptResolver);
}
auto asScriptResolver = dynamic_cast<AngelScriptResolver*>(scriptResolver);
for (const auto& dirEntry : std::filesystem::recursive_directory_iterator(scriptsPath)) {
if (dirEntry.is_directory())
continue;
if (dirEntry.path().parent_path().stem() == "Interfaces")
continue;
if (dirEntry.path().extension() != ".as")
continue;
std::ifstream in(dirEntry.path());
std::string contents((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
2021-08-29 10:14:27 +00:00
asScriptResolver->CreateScript((const char*)dirEntry.path().c_str(), contents.c_str());
2021-08-28 14:05:31 +00:00
}
asScriptResolver->FinalizeModule();
ScriptsTimeMs =
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - begin).count();
return battleLib;
}