Initial commit, adds extracting angelscript script headers from scriptresolver

This commit is contained in:
Deukhoofd 2020-09-19 18:08:55 +02:00
commit e3d23b582f
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
22 changed files with 28144 additions and 0 deletions

136
.clang-format Normal file
View File

@ -0,0 +1,136 @@
# ClangFormatConfigureSource: 'clang-format-file:///home/nathan/Projects/PokemonLibraries/PkmnLib/.clang-format'
---
Language: Cpp
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: false
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 120
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Merge
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 1
SortPriority: 0
- Regex: '.*'
Priority: 3
SortPriority: 0
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentCaseLabels: true
IndentGotoLabels: true
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
Standard: c++20
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
- Try
TabWidth: 8
UseCRLF: false
UseTab: Never
...

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/cmake-build-debug/
/cmake-build-release/
/.idea/

16
CMakeLists.txt Normal file
View File

@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.17)
project(PkmnLibTools)
# Enable all warnings, and make them error when occurring.
add_compile_options(-Wall -Wextra -Werror)
set(CMAKE_CXX_STANDARD 20)
set(STATICC TRUE)
include(CmakeConanSetup.cmake)
SetupConan()
file(GLOB_RECURSE SRC_FILES src/*.cpp src/*.hpp)
add_executable(PkmnLibTools ${SRC_FILES})
SET(_LINKS Arbutils CreatureLib pkmnLib)
target_link_libraries(PkmnLibTools PUBLIC ${_LINKS})

30
CmakeConanSetup.cmake Normal file
View File

@ -0,0 +1,30 @@
function(SetupConan)
# If conan isn't set up yet, we need to install the dependencies.
if (NOT EXISTS ${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
message(WARNING "The file conanbuildinfo.cmake doesn't exist, running conan install.")
# If we're linking C statically, we also want to do so for our dependencies.
set(CONAN_STATIC_C False)
if (STATICC)
set(CONAN_STATIC_C True)
endif (STATICC)
if (NOT WINDOWS)
execute_process(COMMAND conan install ${CMAKE_SOURCE_DIR} --install-folder=${CMAKE_BINARY_DIR} --build outdated
-s compiler=clang
)
else ()
execute_process(COMMAND conan install ${CMAKE_SOURCE_DIR} --install-folder=${CMAKE_BINARY_DIR} --build outdated
-s compiler=gcc
-s compiler.libcxx=libstdc++11
-s os=Windows
-o *:shared=True
-o *:staticC=${CONAN_STATIC_C}
-o AngelScript:link_std_statically=True
-o CreatureLib:level_size=8
)
endif ()
endif ()
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
endfunction()

19
conanfile.py Normal file
View File

@ -0,0 +1,19 @@
from conans import ConanFile, CMake
from conans.errors import ConanInvalidConfiguration
class PkmnLibConan(ConanFile):
name = "PkmnLibTools"
license = "TODO"
url = "https://git.p-epsilon.com/Deukhoofd/PkmnLib"
description = ""
settings = "os", "compiler", "build_type"
generators = "cmake"
exports_sources = "*"
compiler = "clang"
def requirements(self):
self.requires("Arbutils/latest@epsilon/master")
self.requires("CreatureLib/latest@epsilon/master")
self.requires("PkmnLib/latest@epsilon/master")
self.requires("AngelScript/2.34@AngelScript/Deukhoofd")

4310
extern/args.hpp vendored Normal file

File diff suppressed because it is too large Load Diff

22875
extern/json.hpp vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,69 @@
#include "BuildItems.hpp"
#include <fstream>
#include <iostream>
#include "../../extern/json.hpp"
using json = nlohmann::json;
#define GET(o, objectKey, key) \
auto _##objectKey = o[#objectKey]; \
if (_##objectKey.is_null()) { \
std::cout << "Failed to retrieve key '" << #objectKey << "' for object with value '" << key << "' in file '" \
<< path << "'\n"; \
return nullptr; \
}
static CreatureLib::Library::ItemCategory ParseItemCategory(std::string& in) {
std::transform(in.begin(), in.end(), in.end(), tolower);
if (in == "item")
return CreatureLib::Library::ItemCategory::MiscItem;
if (in == "medicine")
return CreatureLib::Library::ItemCategory::Medicine;
if (in == "berry")
return CreatureLib::Library::ItemCategory::Berry;
if (in == "mail")
return CreatureLib::Library::ItemCategory::Mail;
if (in == "key")
return CreatureLib::Library::ItemCategory::KeyItem;
if (in == "pokeball")
return CreatureLib::Library::ItemCategory::CaptureDevice;
if (in == "tm")
return CreatureLib::Library::ItemCategory::MoveLearner;
std::cout << "Unknown Item Type: '" << in << "'\n";
return static_cast<CreatureLib::Library::ItemCategory>(255);
}
PkmnLib::Library::ItemLibrary* BuildItems::Build(const std::string& path) {
std::ifstream fileStream(path.c_str());
if (fileStream.fail()) {
std::cout << "Failed to load Items data file at '" << path << "'\n";
return nullptr;
}
auto lib = new PkmnLib::Library::ItemLibrary();
json j;
fileStream >> j;
for (auto i : j.items()) {
auto val = i.value();
GET(val, name, i);
GET(val, itemType, i);
GET(val, flags, i);
GET(val, price, i);
GET(val, flingPower, i);
auto itemTypeStr = _itemType.get<std::string>();
CreatureLib::Library::ItemCategory itemType = ParseItemCategory(itemTypeStr);
if (static_cast<int>(itemType) == 255)
return nullptr;
auto flags = std::unordered_set<uint>();
for (auto flagIndex : _flags.items()) {
flags.insert(ArbUt::StringView(flagIndex.value().get<std::string>().c_str()));
}
auto item = new PkmnLib::Library::Item(ArbUt::StringView(_name.get<std::string>().c_str()), itemType,
CreatureLib::Library::BattleItemCategory::None, _price.get<int32_t>(),
flags, _flingPower.get<uint8_t>());
lib->Insert(item->GetName(), item);
}
return lib;
}
#undef GET

View File

@ -0,0 +1,10 @@
#ifndef GEN7TESTS_BUILDITEMS_HPP
#define GEN7TESTS_BUILDITEMS_HPP
#include <PkmnLib/Library/Items/ItemLibrary.hpp>
class BuildItems {
public:
static PkmnLib::Library::ItemLibrary* Build(const std::string& path);
};
#endif // GEN7TESTS_BUILDITEMS_HPP

View File

@ -0,0 +1,113 @@
#include "BuildMoves.hpp"
#include <fstream>
#include <iostream>
#include "../../extern/json.hpp"
using json = nlohmann::json;
#define GET(o, objectKey, key) \
auto _##objectKey = o[#objectKey]; \
if (_##objectKey.is_null()) { \
std::cout << "Failed to retrieve key '" << #objectKey << "' for object with value '" << key << "' in file '" \
<< path << "'\n"; \
return nullptr; \
}
static PkmnLib::Library::MoveCategory ParseCategory(const std::string& input) {
if (input == "physical")
return PkmnLib::Library::MoveCategory::Physical;
else if (input == "special")
return PkmnLib::Library::MoveCategory::Special;
else if (input == "status")
return PkmnLib::Library::MoveCategory::Status;
std::cout << "Invalid move category '" << input << ".\n";
return static_cast<PkmnLib::Library::MoveCategory>(255);
}
PkmnLib::Library::MoveLibrary* BuildMoves::Build(const std::string& path,
const CreatureLib::Library::TypeLibrary* types) {
std::ifstream fileStream(path.c_str());
if (fileStream.fail()) {
std::cout << "Failed to load Move data file at '" << path << "'\n";
return nullptr;
}
auto lib = new PkmnLib::Library::MoveLibrary();
json j;
fileStream >> j;
for (auto i : j.items()) {
auto val = i.value();
GET(val, name, i);
GET(val, type, i);
GET(val, power, i);
GET(val, pp, i);
GET(val, accuracy, i);
GET(val, priority, i);
GET(val, target, i);
GET(val, category, i);
GET(val, flags, i);
if (_pp.get<uint8_t>() == 0)
continue;
auto type = types->GetTypeId(ArbUt::StringView(_type.get<std::string>().c_str()));
auto category = ParseCategory(_category.get<std::string>());
if (static_cast<int>(category) == 255)
return nullptr;
CreatureLib::Library::AttackTarget target;
if (!CreatureLib::Library::AttackTargetHelper::TryParse(_target.get<std::string>().c_str(), target)) {
std::cout << "Invalid target: '" << _target.get<std::string>() << "' for move with name '"
<< _name.get<std::string>() << "'\n";
return nullptr;
}
auto flags = std::unordered_set<uint32_t>();
for (auto flagIndex : _flags.items()) {
flags.insert(ArbUt::StringView(flagIndex.value().get<std::string>().c_str()));
}
CreatureLib::Library::SecondaryEffect* effect = nullptr;
auto jsonEffect = val["effect"];
if (jsonEffect != nullptr) {
auto name = jsonEffect["name"];
auto chanceJson = jsonEffect["chance"];
auto parametersJson = jsonEffect["parameters"];
if (name != nullptr) {
ArbUt::List<CreatureLib::Library::EffectParameter*> parameters;
auto chance = -1.0f;
if (chanceJson != nullptr) {
chance = chanceJson.get<float>();
}
if (parametersJson != nullptr) {
for (auto& kv : parametersJson.items()) {
auto& p = kv.value();
auto t = p.type();
switch (t) {
case json::value_t::boolean:
parameters.Append(new CreatureLib::Library::EffectParameter(p.get<bool>()));
break;
case json::value_t::number_integer:
case json::value_t::number_unsigned:
parameters.Append(new CreatureLib::Library::EffectParameter(p.get<int64_t>()));
break;
case json::value_t::number_float:
parameters.Append(new CreatureLib::Library::EffectParameter(p.get<float>()));
break;
default: continue;
}
}
}
effect = new CreatureLib::Library::SecondaryEffect(
chance, ArbUt::StringView(name.get<std::string>().c_str()), parameters);
}
}
if (effect == nullptr) {
effect = new CreatureLib::Library::SecondaryEffect();
}
auto move = new PkmnLib::Library::MoveData(ArbUt::StringView(_name.get<std::string>().c_str()), type,
category, _power.get<uint8_t>(), _accuracy.get<uint8_t>(),
_pp.get<uint8_t>(), target, _priority.get<int8_t>(), effect, flags);
lib->Insert(ArbUt::StringView(move->GetName().c_str()), move);
}
return lib;
}
#undef GET

View File

@ -0,0 +1,11 @@
#ifndef GEN7TESTS_BUILDMOVES_HPP
#define GEN7TESTS_BUILDMOVES_HPP
#include <CreatureLib/Library/TypeLibrary.hpp>
#include <PkmnLib/Library/Moves/MoveLibrary.hpp>
class BuildMoves {
public:
static PkmnLib::Library::MoveLibrary* Build(const std::string& path, const CreatureLib::Library::TypeLibrary* types);
};
#endif // GEN7TESTS_BUILDMOVES_HPP

View File

@ -0,0 +1,87 @@
#include "BuildNatures.hpp"
#include <cstring>
#include <fstream>
#include <iostream>
static CreatureLib::Library::Statistic ParseStatistic(const std::string& stat) {
if (stat.empty())
return static_cast<CreatureLib::Library::Statistic>(255);
if (stat == "Attack")
return CreatureLib::Library::Statistic::PhysicalAttack;
if (stat == "Defense")
return CreatureLib::Library::Statistic::PhysicalDefense;
if (stat == "SpecialAttack")
return CreatureLib::Library::Statistic::MagicalAttack;
if (stat == "SpecialDefense")
return CreatureLib::Library::Statistic::MagicalDefense;
if (stat == "Speed")
return CreatureLib::Library::Statistic::Speed;
std::cout << "Invalid stat was given: '" << stat << "'.\n";
return static_cast<CreatureLib::Library::Statistic>(254);
}
PkmnLib::Library::NatureLibrary* BuildNatures::Build(const std::string& path) {
std::ifstream file(path);
if (file.fail()) {
std::cout << "Failed to load Natures data file at '" << path << "'\n";
return nullptr;
}
std::string line;
if (!std::getline(file, line)) {
std::cout << "Failed to read Natures data file at '" << path << "'\n";
return nullptr;
}
auto divider = ',';
if (strncmp(line.c_str(), "sep=", 4) == 0) {
divider = line[4];
std::getline(file, line);
}
auto library = new PkmnLib::Library::NatureLibrary();
std::string substr;
while (std::getline(file, line)) {
size_t lastStart = 0;
uint8_t current = 0;
std::string natureName;
std::string increasedStat;
std::string decreasedStat;
for (size_t i = 0; i < line.length(); i++) {
if (line[i] == divider) {
switch (current) {
case 0: natureName = line.substr(lastStart, i - lastStart); break;
case 1: increasedStat = line.substr(lastStart, i - lastStart); break;
case 2: decreasedStat = line.substr(lastStart, i - lastStart); break;
default:
std::cout << "Unknown field in nature data: '" << line.substr(lastStart, i - lastStart)
<< "'.\n";
return nullptr;
}
lastStart = i + 1;
current++;
}
}
if (current == 2)
decreasedStat = line.substr(lastStart, line.length() - lastStart);
auto parsedIncreased = ParseStatistic(increasedStat);
float increasedModifier = 1.1;
if (static_cast<int>(parsedIncreased) == 254)
return nullptr;
if (static_cast<int>(parsedIncreased) == 255)
increasedModifier = 1.0;
auto parsedDecreased = ParseStatistic(decreasedStat);
float decreasedModifier = 0.9;
if (static_cast<int>(parsedDecreased) == 254)
return nullptr;
if (static_cast<int>(parsedDecreased) == 255)
decreasedModifier = 1.0;
std::cout << "Registered nature with name '" << natureName << "'.\n";
library->LoadNature(
ArbUt::StringView(natureName.c_str()),
new PkmnLib::Library::Nature(parsedIncreased, parsedDecreased, increasedModifier, decreasedModifier));
}
return library;
}

View File

@ -0,0 +1,10 @@
#ifndef GEN7TESTS_BUILDNATURES_HPP
#define GEN7TESTS_BUILDNATURES_HPP
#include <PkmnLib/Library/Natures/NatureLibrary.hpp>
class BuildNatures {
public:
static PkmnLib::Library::NatureLibrary* Build(const std::string& path);
};
#endif // GEN7TESTS_BUILDNATURES_HPP

View File

@ -0,0 +1,130 @@
#include "BuildSpecies.hpp"
#include <fstream>
#include <iostream>
#define GET(o, objectKey, key) \
auto _##objectKey = o[#objectKey]; \
if (_##objectKey.is_null()) { \
std::cout << "Failed to retrieve key '" << #objectKey << "' for object with key '" << key << "' in file '" \
<< path << "'\n"; \
return nullptr; \
}
PkmnLib::Library::SpeciesLibrary* BuildSpecies::BuildLibrary(const std::string& path,
const CreatureLib::Library::TypeLibrary* types) {
std::ifstream fileStream(path.c_str());
if (fileStream.fail()) {
std::cout << "Failed to load Pokemon data file at '" << path << "'\n";
return nullptr;
}
auto lib = new PkmnLib::Library::SpeciesLibrary();
json j;
fileStream >> j;
for (json::iterator it = j.begin(); it != j.end(); ++it) {
auto val = it.value();
GET(val, id, it.key());
GET(val, species, it.key());
GET(val, genderRatio, it.key());
GET(val, growthRate, it.key());
GET(val, baseHappiness, it.key());
GET(val, catchRate, it.key());
GET(val, color, it.key());
GET(val, genderDifference, it.key());
GET(val, eggGroups, it.key());
GET(val, eggCycles, it.key());
GET(val, tags, it.key());
GET(val, formes, it.key());
PkmnLib::Library::PokemonSpecies* species = nullptr;
ArbUt::List<ArbUt::StringView> eggGroups;
for (const auto& eg : _eggGroups){
eggGroups.Append(eg.get<std::string>().c_str());
}
auto defaultForme = _formes["default"];
if (!defaultForme.is_null()) {
auto forme = BuildForme("default", defaultForme, it.key(), path, types);
species = new PkmnLib::Library::PokemonSpecies(
_id.get<uint16_t>(), ArbUt::StringView(_species.get<std::string>().c_str()), forme,
_genderRatio.get<int8_t>() / static_cast<float>(100),
ArbUt::StringView(_growthRate.get<std::string>().c_str()), _catchRate.get<uint8_t>(),
_baseHappiness.get<uint8_t>(), eggGroups);
}
for (json::iterator formeIt = _formes.begin(); formeIt != _formes.end(); ++formeIt) {
if (formeIt.key() == "default") {
continue;
}
auto forme = BuildForme(formeIt.key(), formeIt.value(), it.key(), path, types);
if (forme == nullptr)
return nullptr;
if (species == nullptr) {
species = new PkmnLib::Library::PokemonSpecies(
_id.get<uint16_t>(), ArbUt::StringView(_species.get<std::string>().c_str()), forme,
static_cast<float>(_genderRatio.get<int8_t>()) / static_cast<float>(100),
ArbUt::StringView(_growthRate.get<std::string>().c_str()), _catchRate.get<uint8_t>(),
_baseHappiness.get<uint8_t>(), eggGroups);
} else {
if (species->HasForme(ArbUt::StringView(formeIt.key().c_str()))) {
std::cout << "Species '" << it.key() << "' has duplicate forme '" << formeIt.key()
<< "'. Skipping.\n";
delete forme;
continue;
}
species->SetVariant(ArbUt::StringView(formeIt.key().c_str()), forme);
}
}
if (species == nullptr) {
std::cout << "Pokemon with key '" << it.key() << "' does not have any formes.\n";
return nullptr;
}
lib->Insert(ArbUt::StringView(it.key().c_str()), species);
}
return lib;
}
static CreatureLib::Library::StatisticSet<uint16_t> ParseStatistics(json& json) {
return CreatureLib::Library::StatisticSet<uint16_t>(
json["hp"].get<uint16_t>(), json["attack"].get<uint16_t>(), json["defense"].get<uint16_t>(),
json["specialAttack"].get<uint16_t>(), json["specialDefense"].get<uint16_t>(), json["speed"].get<uint16_t>());
}
const PkmnLib::Library::PokemonForme* BuildSpecies::BuildForme(const std::string& name, json& forme,
const std::string& baseKeyName, const std::string& path,
const CreatureLib::Library::TypeLibrary* typeLibrary) {
GET(forme, abilities, baseKeyName << " -> " << name);
GET(forme, hiddenAbilities, baseKeyName << " -> " << name);
GET(forme, baseStats, baseKeyName << " -> " << name);
GET(forme, evReward, baseKeyName << " -> " << name);
GET(forme, types, baseKeyName << " -> " << name);
GET(forme, height, baseKeyName << " -> " << name);
GET(forme, weight, baseKeyName << " -> " << name);
GET(forme, baseExp, baseKeyName << " -> " << name);
GET(forme, moves, baseKeyName << " -> " << name);
auto typeStrings = _types.get<std::vector<std::string>>();
auto types = ArbUt::List<uint8_t>(typeStrings.size());
for (size_t i = 0; i < typeStrings.size(); i++) {
types[i] = typeLibrary->GetTypeId(ArbUt::StringView(typeStrings[i].c_str()));
}
auto stats = ParseStatistics(_baseStats);
auto abilityStrings = _abilities.get<std::vector<std::string>>();
auto abilities = ArbUt::List<ArbUt::StringView>(abilityStrings.size());
for (size_t i = 0; i < abilityStrings.size(); i++) {
abilities[i] = ArbUt::StringView(abilityStrings[i].c_str());
}
auto hiddenAbilityStrings = _abilities.get<std::vector<std::string>>();
auto hiddenAbilities = ArbUt::List<ArbUt::StringView>(hiddenAbilityStrings.size());
for (size_t i = 0; i < hiddenAbilityStrings.size(); i++) {
hiddenAbilities[i] = ArbUt::StringView(abilityStrings[i].c_str());
}
return new PkmnLib::Library::PokemonForme(ArbUt::StringView(name.c_str()), _height.get<float>(),
_weight.get<float>(), _baseExp.get<uint32_t>(), types, stats, abilities,
hiddenAbilities, nullptr);
}
#undef GET

View File

@ -0,0 +1,20 @@
#ifndef GEN7TESTS_BUILDSPECIES_HPP
#define GEN7TESTS_BUILDSPECIES_HPP
#define LEVEL_U8 1
#include <PkmnLib/Library/PokemonLibrary.hpp>
#include <CreatureLib/Library/TypeLibrary.hpp>
#include <string>
#include "../../extern/json.hpp"
using json = nlohmann::json;
class BuildSpecies {
static const PkmnLib::Library::PokemonForme* BuildForme(const std::string& name, json& forme,
const std::string& baseKeyName, const std::string& path,
const CreatureLib::Library::TypeLibrary* typeLibrary);
public:
static PkmnLib::Library::SpeciesLibrary* BuildLibrary(const std::string& path,
const CreatureLib::Library::TypeLibrary* types);
};
#endif // GEN7TESTS_BUILDSPECIES_HPP

View File

@ -0,0 +1,69 @@
#include "BuildTypes.hpp"
CreatureLib::Library::TypeLibrary* BuildTypes::Build(const std::string& path) {
std::ifstream file(path);
if (file.fail()) {
std::cout << "Failed to load Types data file at '" << path << "'\n";
return nullptr;
}
std::string line;
if (!std::getline(file, line)) {
std::cout << "Failed to read Types data file at '" << path << "'\n";
return nullptr;
}
auto divider = ',';
if (strncmp(line.c_str(), "sep=", 4) == 0) {
divider = line[4];
std::getline(file, line);
}
auto library = new CreatureLib::Library::TypeLibrary();
bool hasSkippedFirst = false;
size_t lastStart = 0;
std::vector<uint8_t> types;
for (size_t i = 0; i < line.length(); i++) {
if (line[i] == divider) {
auto substr = line.substr(lastStart, i - lastStart);
lastStart = i + 1;
if (hasSkippedFirst) {
std::cout << "Registered type: " << substr << "\n";
auto val = library->RegisterType(ArbUt::StringView(substr.c_str()));
types.push_back(val);
} else {
hasSkippedFirst = true;
}
i++;
}
}
auto substr = line.substr(lastStart, line.length() - lastStart);
std::cout << "Registered type: " << substr << "\n";
auto val = library->RegisterType(ArbUt::StringView(substr.c_str()));
types.push_back(val);
while (std::getline(file, line)) {
uint8_t attackingType = 0;
bool gotType = false;
lastStart = 0;
int current = 0;
for (size_t i = 0; i < line.length(); i++) {
if (line[i] == divider) {
substr = line.substr(lastStart, i - lastStart);
lastStart = i + 1;
if (gotType) {
auto eff = std::atof(substr.c_str());
library->SetEffectiveness(attackingType, types[current], eff);
current++;
} else {
gotType = true;
attackingType = library->GetTypeId(ArbUt::StringView(substr.c_str()));
}
i++;
}
}
substr = line.substr(lastStart, line.length() - lastStart);
auto eff = std::atof(substr.c_str());
library->SetEffectiveness(attackingType, types[current], eff);
}
return library;
}

View File

@ -0,0 +1,13 @@
#ifndef GEN7TESTS_BUILDTYPES_HPP
#define GEN7TESTS_BUILDTYPES_HPP
#include <CreatureLib/Library/TypeLibrary.hpp>
#include <cstring>
#include <fstream>
#include <iostream>
class BuildTypes {
public:
static CreatureLib::Library::TypeLibrary* Build(const std::string& path);
};
#endif // GEN7TESTS_BUILDTYPES_HPP

View File

@ -0,0 +1,25 @@
#include "GrowthRatesBuilder.hpp"
#include <fstream>
#include <iostream>
#include "../../extern/json.hpp"
using json = nlohmann::json;
CreatureLib::Library::GrowthRateLibrary* GrowthRatesBuilder::Build(const std::string& path) {
std::ifstream fileStream(path.c_str());
if (fileStream.fail()) {
std::cout << "Failed to load Pokemon data file at '" << path << "'\n";
return nullptr;
}
auto lib = new CreatureLib::Library::GrowthRateLibrary();
json j;
fileStream >> j;
for (const auto& i : j.items()) {
const auto& name = i.key();
auto values = i.value();
lib->AddGrowthRate(ArbUt::StringView(name.c_str()),
new LookupGrowthRate(values.get<std::vector<uint32_t>>()));
}
return lib;
}

View File

@ -0,0 +1,33 @@
#ifndef GEN7TESTS_GROWTHRATESBUILDER_HPP
#define GEN7TESTS_GROWTHRATESBUILDER_HPP
#define LEVEL_U8 1
#include <CreatureLib/Library/GrowthRates/GrowthRate.hpp>
#include <CreatureLib/Library/GrowthRates/GrowthRateLibrary.hpp>
#include <utility>
#include <vector>
class LookupGrowthRate : public CreatureLib::Library::GrowthRate {
std::vector<uint32_t> _experience;
public:
LookupGrowthRate(std::vector<uint32_t> experience) : _experience(std::move(experience)) {}
[[nodiscard]] uint8_t CalculateLevel(uint32_t experience) const override {
for (size_t i = 0; i < _experience.size(); i++) {
if (_experience[i] > experience) {
return i;
}
}
return _experience[_experience.size() - 1];
}
[[nodiscard]] uint32_t CalculateExperience(uint8_t level) const override { return _experience[level - 1]; }
};
class GrowthRatesBuilder {
public:
static CreatureLib::Library::GrowthRateLibrary* Build(const std::string& file);
};
#endif // GEN7TESTS_GROWTHRATESBUILDER_HPP

View File

@ -0,0 +1,94 @@
#include "ScriptHeadersExporter.hpp"
#include <PkmnLib/ScriptResolving/AngelScript/AngelScriptResolver.hpp>
#include <filesystem>
static bool replace(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);
if (start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
static void PrintObjectType(asITypeInfo* type, const std::filesystem::path& dir){
auto name = std::string(type->GetName());
if (name == "string" || name == "array" || name == "ref")
return;
std::fstream fs;
fs.open((dir / name).concat(".as"), std::fstream::out);
auto isAbstract = (type->GetFlags() & asEObjTypeFlags::asOBJ_ABSTRACT) != 0;
fs << "shared ";
if (isAbstract)
fs << "abstract class ";
else
fs << "interface ";
fs << type->GetName() << " {" << std::endl;
auto propertyCount = type->GetMethodCount();
for (asUINT j = 0; j < propertyCount; j++) {
auto method = type->GetMethodByIndex(j);
if (method->IsProperty()) {
auto name = std::string(method->GetName());
auto decl = std::string(method->GetDeclaration(false, true, false));
auto realName = name.substr(4);
replace(decl, name, realName);
replace(decl, "() const", " { get const; }");
replace(decl, "()", " { get; }");
fs << "\t" << decl << std::endl;
} else {
auto name = std::string(method->GetName());
if (name == "opAssign")
continue;
auto decl = std::string(method->GetDeclaration(false, true, true));
replace(decl, "&in", " &in");
replace(decl, "&out", " &out");
fs << "\t" << decl;
if (isAbstract)
fs << "{};" << std::endl;
else
fs << ";" << std::endl;
}
}
fs << "}" << std::endl;
fs.close();
}
void ScriptHeadersExporter::Export(const std::string& outPath) {
auto resolver = new AngelScriptResolver();
resolver->Initialize(nullptr);
resolver->FinalizeModule();
auto module = resolver->GetMainModule();
auto engine = module->GetEngine();
auto typesCount = engine->GetObjectTypeCount();
std::filesystem::path dir(outPath);
for (asUINT i = 0; i < typesCount; i++) {
auto type = engine->GetObjectTypeByIndex(i);
PrintObjectType(type, dir);
}
auto moduleTypesCount = module->GetObjectTypeCount();
for (asUINT i = 0; i < moduleTypesCount; i++) {
auto type = module->GetObjectTypeByIndex(i);
PrintObjectType(type, dir);
}
auto enumCount = engine->GetEnumCount();
for (asUINT i = 0; i < enumCount; i++) {
auto en = engine->GetEnumByIndex(i);
auto name = en->GetName();
std::fstream fs;
fs.open((dir / name).concat(".as"), std::fstream::out);
fs << "shared enum " << en->GetName() << " {" << std::endl;
auto valueCount = en->GetEnumValueCount();
for (asUINT j = 0; j < valueCount; j++) {
int val;
auto name = en->GetEnumValueByIndex(j, &val);
fs << "\t" << name << " = " << val << "," << std::endl;
}
fs << "}" << std::endl;
fs.close();
}
}

View File

@ -0,0 +1,11 @@
#ifndef PKMNLIBTOOLS_SCRIPTHEADERSEXPORTER_HPP
#define PKMNLIBTOOLS_SCRIPTHEADERSEXPORTER_HPP
#include <string>
class ScriptHeadersExporter {
public:
static void Export(const std::string& outPath);
};
#endif // PKMNLIBTOOLS_SCRIPTHEADERSEXPORTER_HPP

60
src/main.cpp Normal file
View File

@ -0,0 +1,60 @@
#include <iostream>
#include "../extern/args.hpp"
#include "BuildData/BuildItems.hpp"
#include "BuildData/BuildMoves.hpp"
#include "BuildData/BuildNatures.hpp"
#include "BuildData/BuildSpecies.hpp"
#include "BuildData/BuildTypes.hpp"
#include "BuildData/GrowthRatesBuilder.hpp"
#include "Tools/ScriptHeadersExporter.hpp"
[[maybe_unused]] static PkmnLib::Library::PokemonLibrary* BuildDataLibrary() {
std::string typesFile = "Types.csv";
std::string naturesFile = "Natures.csv";
std::string pokemonFile = "Pokemon.json";
std::string moveFile = "Moves.json";
std::string itemsFile = "Items.json";
std::string growthRatesFile = "GrowthRates.json";
std::string scriptsPath = "Scripts";
auto typesLibrary = BuildTypes::Build(typesFile);
auto natureLibrary = BuildNatures::Build(naturesFile);
auto speciesLibrary = BuildSpecies::BuildLibrary(pokemonFile, typesLibrary);
auto movesLibrary = BuildMoves::Build(moveFile, typesLibrary);
auto itemsLibrary = BuildItems::Build(itemsFile);
auto growthRates = GrowthRatesBuilder::Build(growthRatesFile);
if (typesLibrary == nullptr || speciesLibrary == nullptr || natureLibrary == nullptr || movesLibrary == nullptr ||
itemsLibrary == nullptr || growthRates == nullptr)
return nullptr;
auto settings = new PkmnLib::Library::LibrarySettings(100, 4, 4096);
return new PkmnLib::Library::PokemonLibrary(settings, speciesLibrary, movesLibrary, itemsLibrary, growthRates,
typesLibrary, natureLibrary);
};
int main(int argc, const char* argv[]) {
args::ArgumentParser parser("PkmnLib tool executable.", "");
args::HelpFlag help(parser, "help", "Display this help menu", {'h', "help"});
args::Positional<std::string> tool(parser, "tool", "The tool to execute");
args::ValueFlag<std::string> outPath(parser, "outpath", "The path to output to.", {'o'});
try {
parser.ParseCLI(argc, argv);
} catch (const args::Help&) {
std::cout << parser;
return 0;
} catch (const args::ParseError& e) {
std::cerr << e.what() << std::endl;
std::cerr << parser;
return 1;
}
std::string toolValue = args::get(tool);
if (toolValue == "export-script-headers") {
std::string outPathValue = args::get(outPath);
ScriptHeadersExporter::Export(outPathValue);
} else {
std::stringstream ss;
ss << "Unknown tool called '" << toolValue << "'.";
throw std::logic_error(ss.str());
}
return 0;
}