10000 feat: add VMT library by craftablescience · Pull Request #14 · craftablescience/sourcepp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add VMT library #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ option(SOURCEPP_USE_MDLPP "Build mdlpp library" ${SOURC
option(SOURCEPP_USE_STEAMPP "Build steampp library" ${SOURCEPP_LIBS_START_ENABLED})
option(SOURCEPP_USE_TOOLPP "Build toolpp library" ${SOURCEPP_LIBS_START_ENABLED})
option(SOURCEPP_USE_VCRYPTPP "Build vcryptpp library" ${SOURCEPP_LIBS_START_ENABLED})
option(SOURCEPP_USE_VMTPP "Build vmtpp library" ${SOURCEPP_LIBS_START_ENABLED})
option(SOURCEPP_USE_VPKPP "Build vpkpp library" ${SOURCEPP_LIBS_START_ENABLED})
option(SOURCEPP_USE_VTFPP "Build vtfpp library" ${SOURCEPP_LIBS_START_ENABLED})

Expand Down Expand Up @@ -54,6 +55,9 @@ endif()
if(SOURCEPP_USE_TOOLPP)
set(SOURCEPP_USE_KVPP ON CACHE INTERNAL "" FORCE)
endif()
if(SOURCEPP_USE_VMTPP)
set(SOURCEPP_USE_KVPP ON CACHE INTERNAL "" FORCE)
endif()
if(SOURCEPP_USE_VPKPP)
set(SOURCEPP_USE_KVPP ON CACHE INTERNAL "" FORCE)
endif()
Expand Down Expand Up @@ -194,6 +198,7 @@ add_sourcepp_library(mdlpp ) # sourcepp::mdlpp
add_sourcepp_library(steampp C PYTHON ) # sourcepp::steampp
add_sourcepp_library(toolpp PYTHON ) # sourcepp::toolpp
add_sourcepp_library(vcryptpp C CSHARP PYTHON ) # sourcepp::vcryptpp
add_sourcepp_library(vmtpp ) # sourcepp::vmtpp
add_sourcepp_library(vpkpp C CSHARP NO_TEST ) # sourcepp::vpkpp
add_sourcepp_library(vtfpp PYTHON BENCH) # sourcepp::vtfpp

Expand Down Expand Up @@ -257,7 +262,7 @@ endif()

# Print options
print_options(OPTIONS
USE_BSPPP USE_DMXPP USE_GAMEPP USE_KVPP USE_MDLPP USE_STEAMPP USE_TOOLPP USE_VCRYPTPP USE_VPKPP USE_VTFPP
USE_BSPPP USE_DMXPP USE_GAMEPP USE_KVPP USE_MDLPP USE_STEAMPP USE_TOOLPP USE_VCRYPTPP USE_VMTPP USE_VPKPP USE_VTFPP
BUILD_BENCHMARKS BUILD_C_WRAPPERS BUILD_CSHARP_WRAPPERS BUILD_PYTHON_WRAPPERS BUILD_WITH_OPENCL BUILD_WITH_TBB BUILD_WITH_THREADS BUILD_TESTS BUILD_WIN7_COMPAT
LINK_STATIC_MSVC_RUNTIME
VPKPP_SUPPORT_VPK_V54)
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
<td align="center">❌</td>
</tr>
<tr><!-- empty row to disable github striped bg color --></tr>
<tr>
<td rowspan="1"><code>vmtpp</code></td>
<td><a href="https://developer.valvesoftware.com/wiki/VMT">VMT</a></td>
<td align="center">✅</td>
<td align="center">❌</td>
<td rowspan="1" align="center"></td>
</tr>
<tr><!-- empty row to disable github striped bg color --></tr>
<tr>
<td rowspan="29"><code>vpkpp</code></td>
<td>007 v1.1, v1.3 (007 - Nightfire)</td>
Expand Down Expand Up @@ -321,7 +329,7 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
Libraries not starred should be considered stable, and their existing interfaces will not change much if at all. Note that wrappers
only exist for stable libraries.

(&dagger;) Many text-based formats in Source are close to (if not identical to) KeyValues v1, such as [VMT](https://developer.valvesoftware.com/wiki/VMT) and [VMF](https://developer.valvesoftware.com/wiki/VMF_(Valve_Map_Format)).
(&dagger;) Many text-based formats in Source are close to (if not identical to) KeyValues v1, such as [RES](https://developer.valvesoftware.com/wiki/Resource_list_(Source)), [VDF](https://developer.valvesoftware.com/wiki/VDF), and [VMF](https://developer.valvesoftware.com/wiki/VMF_(Valve_Map_Format)).

## Wrappers

Expand Down
9 changes: 8 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
<td align="center">✅</td>
<td align="center">❌</td>
</tr>
<tr>
<td rowspan="1"><code>vmtpp</code></td>
<td><a href="https://developer.valvesoftware.com/wiki/VMT">VMT</a></td>
<td align="center">✅</td>
<td align="center">❌</td>
<td rowspan="1" align="center"></td>
</tr>
<tr>
<td rowspan="15"><code>vpkpp</code></td>
<td>007 v1.1, v1.3 (007 - Nightfire)</td>
Expand Down Expand Up @@ -280,7 +287,7 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
Libraries not starred should be considered stable, and their existing interfaces will not change much if at all. Note that wrappers
only exist for stable libraries.

(&dagger;) Many text-based formats in Source are close to (if not identical to) KeyValues v1, such as [VMT](https://developer.valvesoftware.com/wiki/VMT) and [VMF](https://developer.valvesoftware.com/wiki/VMF_(Valve_Map_Format)).
(&dagger;) Many text-based formats in Source are close to (if not identical to) KeyValues v1, such as [RES](https://developer.valvesoftware.com/wiki/Resource_list_(Source)), [VDF](https://developer.valvesoftware.com/wiki/VDF), and [VMF](https://developer.valvesoftware.com/wiki/VMF_(Valve_Map_Format)).

## Wrappers

Expand Down
97 changes: 97 additions & 0 deletions include/vmtpp/EntityAccess.h
6D47
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#pragma once

#include <sourcepp/Math.h>

namespace vmtpp {

/// Expose an interface to read values from the entity the VMT is attached to for material proxies.
class IEntityAccess {
public:
virtual ~IEntityAccess() = default;

/// "The number of seconds the current map has been running on the server for."
[[nodiscard]] virtual uint64_t getCurrentTime() const = 0;

[[nodiscard]] virtual float getRenderAlpha() const = 0;

[[nodiscard]] virtual float getAnimationProgress() const = 0;

/// "The distance in units between the current player and the origin of the entity that the material is applied to."
[[nodiscard]] virtual float getDistanceToCurrentPlayer() const = 0;

[[nodiscard]] virtual int getCurrentPlayerTeam() const = 0;

[[nodiscard]] virtual int getTeam() const = 0;

/// "The dot product of the current player's view angle and the relative origin of the material's entity."
[[nodiscard]] virtual float getCurrentPlayerViewDotProduct() const = 0;

[[nodiscard]] virtual float getCurrentPlayerSpeed() const = 0;

[[nodiscard]] virtual sourcepp::math::Vec3f getCurrentPlayerPosition() const = 0;

[[nodiscard]] virtual float getSpeed() const = 0;

[[nodiscard]] virtual sourcepp::math::Vec3f getOrigin() const = 0;

/// "A static random number associated with the entity the material is applied to."
[[nodiscard]] virtual float getRandomNumber() const = 0;

[[nodiscard]] virtual float getHealth() const = 0;

[[nodiscard]] virtual bool isNPC() const = 0;

[[nodiscard]] virtual bool isViewModel() const = 0;

[[nodiscard]] virtual sourcepp::math::Vec3f getWorldDimensionsMinimum() const = 0;

[[nodiscard]] virtual sourcepp::math::Vec3f getWorldDimensionsMaximum() const = 0;

[[nodiscard]] virtual sourcepp::math::Vec3f getCurrentPlayerCrosshairColor() const = 0;
};

class EntityAccessEmpty : public IEntityAccess {
public:
EntityAccessEmpty();

[[nodiscard]] uint64_t getCurrentTime() const override;

[[nodiscard]] float getRenderAlpha() const override;

[[nodiscard]] float getAnimationProgress() const override;

[[nodiscard]] float getDistanceToCurrentPlayer() const override;

[[nodiscard]] int getCurrentPlayerTeam() const override;

[[nodiscard]] int getTeam() const override;

[[nodiscard]] float getCurrentPlayerViewDotProduct() const override;

[[nodiscard]] float getCurrentPlayerSpeed() const override;

[[nodiscard]] sourcepp::math::Vec3f getCurrentPlayerPosition() const override;

[[nodiscard]] float getSpeed() const override;

[[nodiscard]] sourcepp::math::Vec3f getOrigin() const override;

[[nodiscard]] float getRandomNumber() const override;

[[nodiscard]] float getHealth() const override;

[[nodiscard]] bool isNPC() const override;

[[nodiscard]] bool isViewModel() const override;

[[nodiscard]] sourcepp::math::Vec3f getWorldDimensionsMinimum() const override;

[[nodiscard]] sourcepp::math::Vec3f getWorldDimensionsMaximum() const override;

[[nodiscard]] sourcepp::math::Vec3f getCurrentPlayerCrosshairColor() const override;

private:
float random;
};

} // namespace vmtpp
35 changes: 35 additions & 0 deletions include/vmtpp/Proxy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <string>
#include <unordered_map>

namespace vmtpp {

class IEntityAccess;

namespace Proxy {

// We're going to try to implement proxies in a way that's distinct from but still roughly
// comparable to the SDK, so it's easy to compare functionality and get a nice reference!

struct Data {
std::string name;
std::unordered_map<std::string, std::string> variables;
};

using Function = void(*)(Data&, std::unordered_map<std::string, std::string>&, const IEntityAccess&);

Function add(const std::string& name, Function proxy);

Function get(const std::string& name);

void exec(Data& data, std::unordered_map<std::string, std::string>& vmtVariables, const IEntityAccess& entity);

void remove(const std::string& name);

} // namespace Proxy

} // namespace vmtpp

#define VMTPP_MATERIAL_PROXY(name, proxy) \
vmtpp::Proxy::Function VMTPP_MATERIAL_PROXY_##name = vmtpp::Proxy::add(#name, proxy)
71 changes: 71 additions & 0 deletions include/vmtpp/VMT.h
Original file line number D F438 iff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once

#include <initializer_list>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>

#include "EntityAccess.h"
#include "Proxy.h"

namespace vmtpp {

namespace Value {

enum class Type {
INT,
FLOAT,
VEC3,
COLOR,
};

[[nodiscard]] Type getProbableType(std::string_view value);

[[nodiscard]] Type getProbableTypeBasedOnAssociatedValues(std::string_view value, std::initializer_list<std::string_view> others);

[[nodiscard]] int toInt(std::string_view value);

[[nodiscard]] std::string fromInt(int value);

[[nodiscard]] float toFloat(std::string_view value);

[[nodiscard]] std::string fromFloat(float value);

[[nodiscard]] sourcepp::math::Vec3f toVec3(std::string_view value);

[[nodiscard]] std::string fromVec3(sourcepp::math::Vec3f value);

[[nodiscard]] sourcepp::math::Vec4f toColor(std::string_view value);

[[nodiscard]] std::string fromColor(sourcepp::math::Vec4f value);

} // namespace Value

class VMT {
public:
explicit VMT(std::string_view vmt, const IEntityAccess& entityAccess_ = EntityAccessEmpty{}, int dxLevel = 98, int shaderDetailLevel = 3, std::string_view shaderFallbackSuffix = "DX9");

[[nodiscard]] std::string_view getShader() const;

[[nodiscard]] bool hasCompileFlag(std::string_view flag) const;

[[nodiscard]] const std::vector<std::string>& getCompileFlags() const;

[[nodiscard]] bool hasVariable(std::string_view key) const;

[[nodiscard]] std::string_view getVariable(std::string_view key) const;

[[nodiscard]] std::string_view operator[](std::string_view key) const;

void update();

private:
const IEntityAccess& entityAccess;
std::string shader;
std::vector<std::string> compileFlags;
std::unordered_map<std::string, std::string> variables;
std::vector<Proxy::Data> proxies;
};

} // namespace vmtpp
10 changes: 10 additions & 0 deletions include/vmtpp/vmtpp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

/*
* This header is just included so consumers of this library can
* include it the same way as any of the other SourcePP libraries.
*/

#include "EntityAccess.h"
#include "Proxy.h"
#include "VMT.h"
84 changes: 84 additions & 0 deletions src/vmtpp/EntityAccess.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <vmtpp/EntityAccess.h>

#include <chrono>

using namespace sourcepp;
using namespace vmtpp;

EntityAccessEmpty::EntityAccessEmpty() {
// MLP:FIM S09E20 04:58
static float randomNumberGeneratorWinkWink = 0.f;
this->random = randomNumberGeneratorWinkWink++;
}

uint64_t EntityAccessEmpty::getCurrentTime() const {
return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}

float EntityAccessEmpty::getRenderAlpha() const {
return 1.f;
}

float EntityAccessEmpty::getAnimationProgress() const {
return 0.f;
}

float EntityAccessEmpty::getDistanceToCurrentPlayer() const {
return 0.f;
}

int EntityAccessEmpty::getCurrentPlayerTeam() const {
return 0;
}

int EntityAccessEmpty::getTeam() const {
return 0;
}

float EntityAccessEmpty::getCurrentPlayerViewDotProduct() const {
return 0.f;
}

float EntityAccessEmpty::getCurrentPlayerSpeed() const {
return 0.f;
}

math::Vec3f EntityAccessEmpty::getCurrentPlayerPosition() const {
return {};
}

float EntityAccessEmpty::getSpeed() const {
return 0.f;
}

math::Vec3f EntityAccessEmpty::getOrigin() const {
return {};
}

float EntityAccessEmpty::getRandomNumber() const {
return this->random;
}

float EntityAccessEmpty::getHealth() const {
return 0.f;
}

bool EntityAccessEmpty::isNPC() const {
return false;
}

bool EntityAccessEmpty::isViewModel() const {
return false;
}

math::Vec3f EntityAccessEmpty::getWorldDimensionsMinimum() const {
return {};
}

math::Vec3f EntityAccessEmpty::getWorldDimensionsMaximum() const {
return {};
}

math::Vec3f EntityAccessEmpty::getCurrentPlayerCrosshairColor() const {
return {1.f, 1.f, 1.f};
}
Loading
Loading
0