Skip to content

Commit

Permalink
Merge pull request #58 from Cre3per/dump-types-per-module
Browse files Browse the repository at this point in the history
Dump types per module
  • Loading branch information
es3n1n committed Aug 30, 2024
2 parents f548b0f + aab95b6 commit fcc2d98
Show file tree
Hide file tree
Showing 12 changed files with 812 additions and 578 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ target_wrapper.*
# QtCreator CMake
CMakeLists.txt.user*

# QtCreator 4.8< compilation database
# QtCreator 4.8< compilation database
compile_commands.json

# QtCreator local machine specific files for imported projects
Expand Down Expand Up @@ -584,7 +584,7 @@ bld/

# Visual Studio 2015/2017 cache/options directory
.vs/
.vs\
.vs\\
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/

Expand Down
1 change: 1 addition & 0 deletions source2gen/include/sdk/interfaces/common/CUtlVector.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (C) 2024 neverlosecc
// See end of file for extended copyright information.
#pragma once
#include <cassert>
#include <sdk/interfaces/tier0/IMemAlloc.h>

template <class T>
Expand Down
62 changes: 37 additions & 25 deletions source2gen/include/sdk/interfaces/schemasystem/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "tools/platform.h"
#include <array>
#include <cstdint>
#include <limits>
#include <sdk/interfaces/client/game/datamap_t.h>
#include <sdk/interfaces/common/CBufferString.h>
#include <sdk/interfaces/common/CUtlMap.h>
Expand Down Expand Up @@ -382,7 +383,7 @@ class CSchemaType {
}

// @todo: @og: find out to what class pointer points.
[[nodiscard]] CSchemaType* GetRefClass();
[[nodiscard]] CSchemaType* GetRefClass() const;

[[nodiscard]] ETypeCategory GetTypeCategory() const {
#if defined(CS2) || defined(DOTA2) || defined(DEADLOCK)
Expand Down Expand Up @@ -421,13 +422,13 @@ class CSchemaType_Ptr : public CSchemaType {
CSchemaType* m_pObjectType;
};

[[nodiscard]] inline CSchemaType* CSchemaType::GetRefClass() {
[[nodiscard]] inline CSchemaType* CSchemaType::GetRefClass() const {
if (GetTypeCategory() != ETypeCategory::Schema_Ptr)
return nullptr;

auto ptr = reinterpret_cast<CSchemaType_Ptr*>(this)->m_pObjectType;
auto ptr = reinterpret_cast<const CSchemaType_Ptr*>(this)->m_pObjectType;
while (ptr && ptr->GetTypeCategory() == ETypeCategory::Schema_Ptr)
ptr = reinterpret_cast<CSchemaType_Ptr*>(ptr)->m_pObjectType;
ptr = reinterpret_cast<const CSchemaType_Ptr*>(ptr)->m_pObjectType;

return ptr;
}
Expand Down Expand Up @@ -617,7 +618,7 @@ struct SchemaClassInfoData_t {

SchemaClassFieldData_t* m_pFields; // 0x0028
SchemaStaticFieldData_t* m_pStaticFields; // 0x0030
SchemaBaseClassInfoData_t* m_pBaseClassses; // 0x0038
SchemaBaseClassInfoData_t* m_pBaseClasses; // 0x0038
SchemaFieldMetadataOverrideSetData_t* m_pFieldMetadataOverrides; // 0x0040
SchemaMetadataEntryData_t* m_pStaticMetadata; // 0x0048
CSchemaSystemTypeScope* m_pTypeScope; // 0x0050
Expand All @@ -638,40 +639,41 @@ static_assert(offsetof(SchemaClassInfoData_t, m_pFn) == 0x68);

class CSchemaClassInfo : public SchemaClassInfoData_t {
public:
[[nodiscard]] std::string_view GetName() {
[[nodiscard]] std::string_view GetName() const {
if (m_pszName)
return {m_pszName};
return {};
}

[[nodiscard]] std::string_view GetModule() {
[[nodiscard]] std::string_view GetModule() const {
if (m_pszModule)
return {m_pszModule};
return {};
}

[[nodiscard]] std::optional<CSchemaClassInfo*> GetBaseClass() const {
if (m_nBaseClassSize && m_pBaseClassses)
return m_pBaseClassses->m_pClass;
if (m_nBaseClassSize && m_pBaseClasses)
return m_pBaseClasses->m_pClass;
return std::nullopt;
}

[[nodiscard]] std::vector<SchemaClassFieldData_t> GetFields() {
// TODO: when we have AddressSanitizer, try returning std::span instead of std::vector. Repeats for other functions.
[[nodiscard]] std::vector<SchemaClassFieldData_t> GetFields() const {
return {m_pFields, m_pFields + m_nFieldSize};
}

[[nodiscard]] std::vector<SchemaStaticFieldData_t> GetStaticFields() {
[[nodiscard]] std::vector<SchemaStaticFieldData_t> GetStaticFields() const {
return {m_pStaticFields, m_pStaticFields + m_nStaticFieldsSize};
}

[[nodiscard]] std::vector<SchemaMetadataEntryData_t> GetStaticMetadata() {
[[nodiscard]] std::vector<SchemaMetadataEntryData_t> GetStaticMetadata() const {
return {m_pStaticMetadata, m_pStaticMetadata + m_nStaticMetadataSize};
}

[[nodiscard]] std::string_view GetPrevClassName() const {
if (!m_pBaseClassses || !m_pBaseClassses->m_pClass)
if (!m_pBaseClasses || !m_pBaseClasses->m_pClass)
return {};
return m_pBaseClassses->m_pClass->GetName();
return m_pBaseClasses->m_pClass->GetName();
}

[[nodiscard]] bool IsA(CSchemaType* pInheritance) const {
Expand All @@ -686,26 +688,26 @@ class CSchemaClassInfo : public SchemaClassInfoData_t {
}

[[nodiscard]] bool RecursiveHasVirtualTable() const {
return HasVirtualTable() || (m_pBaseClassses && m_pBaseClassses->m_pClass && m_pBaseClassses->m_pClass->HasVirtualTable());
return HasVirtualTable() || (m_pBaseClasses && m_pBaseClasses->m_pClass && m_pBaseClasses->m_pClass->HasVirtualTable());
}

[[nodiscard]] bool IsInherits(const std::string_view from) const {
if (!m_nBaseClassSize || !m_pBaseClassses || !m_pBaseClassses->m_pClass)
if (!m_nBaseClassSize || !m_pBaseClasses || !m_pBaseClasses->m_pClass)
return false;
if (m_pBaseClassses->m_pClass->GetName() == from)
if (m_pBaseClasses->m_pClass->GetName() == from)
return true;
return false;
}

[[nodiscard]] bool IsRecursiveInherits(const std::string_view from) const {
return IsInherits(from) || (m_pBaseClassses && m_pBaseClassses->m_pClass && m_pBaseClassses->m_pClass->IsRecursiveInherits(from));
return IsInherits(from) || (m_pBaseClasses && m_pBaseClasses->m_pClass && m_pBaseClasses->m_pClass->IsRecursiveInherits(from));
}

[[nodiscard]] int GetSize() const {
return m_nSizeOf;
}

[[nodiscard]] std::uint8_t GetAligment() const {
[[nodiscard]] std::uint8_t GetAlignment() const {
return m_unAlignOf == std::numeric_limits<std::uint8_t>::max() ? 8 : m_unAlignOf;
}

Expand Down Expand Up @@ -765,36 +767,46 @@ static_assert(sizeof(CSchemaPtrMap<int, int>) == platform_specific{.windows = 0x
class CSchemaSystemTypeScope {
public:
void* InsertNewClassBinding(const std::string_view szName, void* a2) {
assert((std::strlen(szName.data()) == szName.size()) && "need a zero-terminated string");

return Virtual::Get<void* (*)(CSchemaSystemTypeScope*, const char*, void*)>(this, 0)(this, szName.data(), a2);
}

void* InsertNewEnumBinding(const std::string_view szName, void* a2) {
assert((std::strlen(szName.data()) == szName.size()) && "need a zero-terminated string");

return Virtual::Get<void* (*)(CSchemaSystemTypeScope*, const char*, void*)>(this, 1)(this, szName.data(), a2);
}

[[nodiscard]] CSchemaClassInfo* FindDeclaredClass(const std::string_view szName) {
[[nodiscard]] CSchemaClassInfo* FindDeclaredClass(const std::string_view szName) const {
assert((std::strlen(szName.data()) == szName.size()) && "need a zero-terminated string");

if constexpr (kSchemaSystemVersion == 2) {
CSchemaClassInfo* class_info;

Virtual::Get<void(__thiscall*)(void*, CSchemaClassInfo**, const char*)>(this, 2)(this, &class_info, szName.data());
Virtual::Get<void(__thiscall*)(const void*, CSchemaClassInfo**, const char*)>(this, 2)(this, &class_info, szName.data());
return class_info;
} else {
return Virtual::Get<CSchemaClassInfo*(__thiscall*)(void*, const char*)>(this, 2)(this, szName.data());
return Virtual::Get<CSchemaClassInfo*(__thiscall*)(const void*, const char*)>(this, 2)(this, szName.data());
}
}

[[nodiscard]] CSchemaEnumInfo* FindDeclaredEnum(const std::string_view szName) {
[[nodiscard]] CSchemaEnumInfo* FindDeclaredEnum(const std::string_view szName) const {
assert((std::strlen(szName.data()) == szName.size()) && "need a zero-terminated string");

if constexpr (kSchemaSystemVersion == 2) {
CSchemaEnumInfo* enum_info;

Virtual::Get<void(__thiscall*)(void*, CSchemaEnumInfo**, const char*)>(this, 3)(this, &enum_info, szName.data());
Virtual::Get<void(__thiscall*)(const void*, CSchemaEnumInfo**, const char*)>(this, 3)(this, &enum_info, szName.data());
return enum_info;
} else {
return Virtual::Get<CSchemaEnumInfo*(__thiscall*)(void*, const char*)>(this, 3)(this, szName.data());
return Virtual::Get<CSchemaEnumInfo*(__thiscall*)(const void*, const char*)>(this, 3)(this, szName.data());
}
}

[[nodiscard]] CSchemaType* FindSchemaTypeByName(const std::string_view szName) {
assert((std::strlen(szName.data()) == szName.size()) && "need a zero-terminated string");

if constexpr (kSchemaSystemVersion == 2) {
CSchemaType* schema_type;

Expand Down
3 changes: 3 additions & 0 deletions source2gen/include/sdk/interfaces/tier0/IMemAlloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

#pragma warning(push)
#pragma warning(disable:4191)

#include "tools/platform.h"
#include "tools/virtual.h"
#include <cstddef>

class IMemAlloc {
public:
Expand Down
4 changes: 3 additions & 1 deletion source2gen/include/sdk/sdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
#include <sdk/interfaceregs.h>
#include <sdk/interfaces/client/game/datamap_t.h>
#include <sdk/interfaces/schemasystem/schema.h>
#include <unordered_set>

namespace sdk {
inline CSchemaSystem* g_schema = nullptr;

void GenerateTypeScopeSdk(CSchemaSystemTypeScope* current);
void GenerateTypeScopeSdk(std::string_view module_name, const std::unordered_set<const CSchemaEnumBinding*>& enums,
const std::unordered_set<const CSchemaClassBinding*>& classes);
} // namespace sdk

// source2gen - Source2 games SDK generator
Expand Down
15 changes: 6 additions & 9 deletions source2gen/include/tools/codegen.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (C) 2024 neverlosecc
// See end of file for extended copyright information.
#pragma once
#include <array>
#include <cstdint>
#include <set>
#include <sstream>
Expand Down Expand Up @@ -124,10 +125,7 @@ namespace codegen {
if (decrement_tabs_count)
dec_tabs_count(kTabsPerBlock);

push_line("};");
if (move_cursor_to_next_line)
next_line();

push_line("};", move_cursor_to_next_line);
return *this;
}

Expand Down Expand Up @@ -183,8 +181,8 @@ namespace codegen {
}

// @todo: @es3n1n: add func params
self_ref begin_function(const std::string& prefix, const std::string& type_name, const std::string& func_name, const bool increment_tabs_count = true,
const bool move_cursor_to_next_line = true) {
self_ref begin_function(const std::string& prefix, const std::string& type_name, const std::string& func_name,
const bool increment_tabs_count = true, const bool move_cursor_to_next_line = true) {
return begin_block(std::format("{}{} {}()", prefix, type_name, escape_name(func_name)), "", increment_tabs_count, move_cursor_to_next_line);
}

Expand All @@ -209,7 +207,7 @@ namespace codegen {
R"(*reinterpret_cast<{}*>(interfaces::g_schema->FindTypeScopeForModule("{}")->FindDeclaredClass("{}")->GetStaticFields()[{}]->m_pInstance))",
type_name, mod_name, decl_class, index);
return_value(getter, false);
end_function(false, false);
end_function(false, true);

// @note: @es3n1n: restore tabs count
//
Expand Down Expand Up @@ -293,8 +291,7 @@ namespace codegen {
result.resize(name.size());

for (std::size_t i = 0; i < name.size(); i++)
result[i] =
std::ranges::find(kBlacklistedCharacters, name[i]) == std::end(kBlacklistedCharacters) ? name[i] : '_';
result[i] = std::ranges::find(kBlacklistedCharacters, name[i]) == std::end(kBlacklistedCharacters) ? name[i] : '_';

return result;
}
Expand Down
14 changes: 11 additions & 3 deletions source2gen/include/tools/field_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
// See end of file for extended copyright information.
#pragma once

#include <cstdint>
#include <cstddef>
#include <format>
#include <optional>
#include <sdk/interfaces/client/game/datamap_t.h>
#include <string>

enum class fieldtype_t : uint8_t;
#include <string_view>
#include <variant>
#include <vector>

namespace field_parser {
class field_info_t {
Expand Down Expand Up @@ -65,6 +69,10 @@ namespace field_parser {
}
};

/// @return @ref std::nullopt if type_name is not a built-in type
[[nodiscard]]
std::optional<std::string_view> type_name_to_cpp(std::string_view type_name);

field_info_t parse(const std::string& type_name, const std::string& name, const std::vector<std::size_t>& array_sizes);
field_info_t parse(const fieldtype_t& type_name, const std::string& name, const std::size_t& array_sizes = 1);
} // namespace field_parser
Expand Down
8 changes: 8 additions & 0 deletions source2gen/include/tools/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// See end of file for extended copyright information.
#pragma once

#include <ranges>
#include <string>
#include <string_view>
#include <tools/loader/loader.h>

namespace util {
Expand All @@ -19,6 +21,12 @@ namespace util {

return std::to_string(num);
}

[[nodiscard]] inline std::string EscapePath(std::string_view path) {
std::string result(path);
std::ranges::replace(result, ':', '_');
return result;
}
} // namespace util

// source2gen - Source2 games SDK generator
Expand Down
9 changes: 7 additions & 2 deletions source2gen/include/tools/virtual.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
// See end of file for extended copyright information.
#pragma once

#include <Include.h>
#include <cstdint>

namespace Virtual {
template <typename T>
inline T Get(void* instance, const unsigned int index) {
return (*static_cast<T**>(instance))[index];
return (*static_cast<T**>(static_cast<void*>(instance)))[index];
}

template <typename T>
inline T Get(const void* instance, const unsigned int index) {
return (*static_cast<T* const*>(static_cast<const void*>(instance)))[index];
}

template <typename T>
Expand Down
Loading

0 comments on commit fcc2d98

Please sign in to comment.