Skip to content

Commit a6c59ae

Browse files
feat(vmtpp): add VMT library
1 parent e298a46 commit a6c59ae

File tree

10 files changed

+997
-1
lines changed

10 files changed

+997
-1
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ option(SOURCEPP_USE_KVPP "Build kvpp library"
2121
option(SOURCEPP_USE_MDLPP "Build mdlpp library" ON)
2222
option(SOURCEPP_USE_STEAMPP "Build steampp library" ON)
2323
option(SOURCEPP_USE_VICEPP "Build vicepp library" ON)
24+
option(SOURCEPP_USE_VMTPP "Build vmtpp library" ON)
2425
option(SOURCEPP_USE_VPKPP "Build vpkpp library" ON)
2526
option(SOURCEPP_USE_VTFPP "Build vtfpp library" ON)
2627
option(SOURCEPP_BUILD_C_WRAPPERS "Build C wrappers for supported libraries" ON)
@@ -32,6 +33,9 @@ option(SOURCEPP_USE_STATIC_MSVC_RUNTIME "Link to static MSVC runtime library"
3233
if(SOURCEPP_USE_STEAMPP)
3334
set(SOURCEPP_USE_KVPP ON CACHE INTERNAL "")
3435
endif()
36+
if(SOURCEPP_USE_VMTPP)
37+
set(SOURCEPP_USE_KVPP ON CACHE INTERNAL "")
38+
endif()
3539
if(SOURCEPP_USE_VPKPP)
3640
set(SOURCEPP_USE_BSPPP ON CACHE INTERNAL "")
3741
set(SOURCEPP_USE_KVPP ON CACHE INTERNAL "")
@@ -92,6 +96,7 @@ add_sourcepp_library(kvpp)
9296
add_sourcepp_library(mdlpp)
9397
add_sourcepp_library(steampp C)
9498
add_sourcepp_library(vicepp C CSHARP)
99+
add_sourcepp_library(vmtpp)
95100
add_sourcepp_library(vpkpp C CSHARP NO_TEST)
96101
add_sourcepp_library(vtfpp)
97102

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,17 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
102102
<td align="center">✅</td>
103103
<td align="center">C<br>C#</td>
104104
</tr>
105+
<tr>
106+
<td><code>vmtpp</code></td>
107+
<td>
108+
<ul>
109+
<li><a href="https://developer.valvesoftware.com/wiki/VMT">VMT</a></li>
110+
</ul>
111+
</td>
112+
<td align="center">✅</td>
113+
<td align="center">❌</td>
114+
<td align="center"></td>
115+
</tr>
105116
<tr>
106117
<td><code>vpkpp</code></td>
107118
<td>
@@ -135,7 +146,7 @@ Several modern C++20 libraries for sanely parsing Valve formats, rolled into one
135146
</tr>
136147
</table>
137148

138-
(\*) Many text-based formats in Source are close to (if not identical to) KeyValues v1, such as [VDF](https://developer.valvesoftware.com/wiki/VDF), [VMT](https://developer.valvesoftware.com/wiki/VMT), and [VMF](https://developer.valvesoftware.com/wiki/VMF_(Valve_Map_Format)).
149+
(\*) 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)).
139150

140151
(&dagger;) The MDL parser is not complete. It is usable in its current state, but it does not currently parse more complex components like animations. This parser is still in development.
141152

include/vmtpp/EntityAccess.h

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#pragma once
2+
3+
#include <sourcepp/math/Vector.h>
4+
5+
namespace vmtpp {
6+
7+
/// Expose an interface to read values from the entity the VMT is attached to for material proxies.
8+
class IEntityAccess {
9+
public:
10+
virtual ~IEntityAccess() = default;
11+
12+
/// "The number of seconds the current map has been running on the server for."
13+
[[nodiscard]] virtual uint64_t getCurrentTime() const = 0;
14+
15+
[[nodiscard]] virtual float getRenderAlpha() const = 0;
16+
17+
[[nodiscard]] virtual float getAnimationProgress() const = 0;
18+
19+
/// "The distance in units between the current player and the origin of the entity that the material is applied to."
20+
[[nodiscard]] virtual float getDistanceToCurrentPlayer() const = 0;
21+
22+
[[nodiscard]] virtual int getCurrentPlayerTeam() const = 0;
23+
24+
[[nodiscard]] virtual int getTeam() const = 0;
25+
26+
/// "The dot product of the current player's view angle and the relative origin of the material's entity."
27+
[[nodiscard]] virtual float getCurrentPlayerViewDotProduct() const = 0;
28+
29+
[[nodiscard]] virtual float getCurrentPlayerSpeed() const = 0;
30+
31+
[[nodiscard]] virtual sourcepp::math::Vec3f getCurrentPlayerPosition() const = 0;
32+
33+
[[nodiscard]] virtual float getSpeed() const = 0;
34+
35+
[[nodiscard]] virtual sourcepp::math::Vec3f getOrigin() const = 0;
36+
37+
/// "A static random number associated with the entity the material is applied to."
38+
[[nodiscard]] virtual float getRandomNumber() const = 0;
39+
40+
[[nodiscard]] virtual float getHealth() const = 0;
41+
42+
[[nodiscard]] virtual bool isNPC() const = 0;
43+
44+
[[nodiscard]] virtual bool isViewModel() const = 0;
45+
46+
[[nodiscard]] virtual sourcepp::math::Vec3f getWorldDimensionsMinimum() const = 0;
47+
48+
[[nodiscard]] virtual sourcepp::math::Vec3f getWorldDimensionsMaximum() const = 0;
49+
50+
[[nodiscard]] virtual sourcepp::math::Vec3f getCurrentPlayerCrosshairColor() const = 0;
51+
};
52+
53+
class EntityAccessEmpty : public IEntityAccess {
54+
public:
55+
EntityAccessEmpty();
56+
57+
[[nodiscard]] uint64_t getCurrentTime() const override;
58+
59+
[[nodiscard]] float getRenderAlpha() const override;
60+
61+
[[nodiscard]] float getAnimationProgress() const override;
62+
63+
[[nodiscard]] float getDistanceToCurrentPlayer() const override;
64+
65+
[[nodiscard]] int getCurrentPlayerTeam() const override;
66+
67+
[[nodiscard]] int getTeam() const override;
68+
69+
[[nodiscard]] float getCurrentPlayerViewDotProduct() const override;
70+
71+
[[nodiscard]] float getCurrentPlayerSpeed() const override;
72+
73+
[[nodiscard]] sourcepp::math::Vec3f getCurrentPlayerPosition() const override;
74+
75+
[[nodiscard]] float getSpeed() const override;
76+
77+
[[nodiscard]] sourcepp::math::Vec3f getOrigin() const override;
78+
79+
[[nodiscard]] float getRandomNumber() const override;
80+
81+
[[nodiscard]] float getHealth() const override;
82+
83+
[[nodiscard]] bool isNPC() const override;
84+
85+
[[nodiscard]] bool isViewModel() const override;
86+
87+
[[nodiscard]] sourcepp::math::Vec3f getWorldDimensionsMinimum() const override;
88+
89+
[[nodiscard]] sourcepp::math::Vec3f getWorldDimensionsMaximum() const override;
90+
91+
[[nodiscard]] sourcepp::math::Vec3f getCurrentPlayerCrosshairColor() const override;
92+
93+
private:
94+
float random;
95+
};
96+
97+
} // namespace vmtpp

include/vmtpp/Proxy.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <unordered_map>
5+
6+
namespace vmtpp {
7+
8+
class IEntityAccess;
9+
10+
namespace Proxy {
11+
12+
// We're going to try to implement proxies in a way that's distinct from but still roughly
13+
// comparable to the SDK, so it's easy to compare functionality and get a nice reference!
14+
15+
struct Data {
16+
std::string name;
17+
std::unordered_map<std::string, std::string> variables;
18+
};
19+
20+
using Function = void(*)(Data&, std::unordered_map<std::string, std::string>&, const IEntityAccess&);
21+
22+
Function add(const std::string& name, Function proxy);
23+
24+
Function get(const std::string& name);
25+
26+
void exec(Data& data, std::unordered_map<std::string, std::string>& vmtVariables, const IEntityAccess& entity);
27+
28+
void remove(const std::string& name);
29+
30+
} // namespace Proxy
31+
32+
} // namespace vmtpp
33+
34+
#define VMTPP_MATERIAL_PROXY(name, proxy) \
35+
vmtpp::Proxy::Function VMTPP_MATERIAL_PROXY_##name = vmtpp::Proxy::add(#name, proxy)

include/vmtpp/vmtpp.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#pragma once
2+
3+
#include <initializer_list>
4+
#include <string>
5+
#include <string_view>
6+
#include <unordered_map>
7+
#include <vector>
8+
9+
#include "EntityAccess.h"
10+
#include "Proxy.h"
11+
12+
namespace vmtpp {
13+
14+
namespace Value {
15+
16+
enum class Type {
17+
INT,
18+
FLOAT,
19+
VEC3,
20+
COLOR,
21+
};
22+
23+
[[nodiscard]] Type getProbableType(std::string_view value);
24+
25+
[[nodiscard]] Type getProbableTypeBasedOnAssociatedValues(std::string_view value, std::initializer_list<std::string_view> others);
26+
27+
[[nodiscard]] int toInt(std::string_view value);
28+
29+
[[nodiscard]] std::string fromInt(int value);
30+
31+
[[nodiscard]] float toFloat(std::string_view value);
32+
33+
[[nodiscard]] std::string fromFloat(float value);
34+
35+
[[nodiscard]] sourcepp::math::Vec3f toVec3(std::string_view value);
36+
37+
[[nodiscard]] std::string fromVec3(sourcepp::math::Vec3f value);
38+
39+
[[nodiscard]] sourcepp::math::Vec4f toColor(std::string_view value);
40+
41+
[[nodiscard]] std::string fromColor(sourcepp::math::Vec4f value);
42+
43+
} // namespace Value
44+
45+
class VMT {
46+
public:
47+
explicit VMT(std::string_view vmt, const IEntityAccess& entityAccess_ = EntityAccessEmpty{}, int dxLevel = 98, int shaderDetailLevel = 3, std::string_view shaderFallbackSuffix = "DX9");
48+
49+
[[nodiscard]] std::string_view getShader() const;
50+
51+
[[nodiscard]] bool hasCompileFlag(std::string_view flag) const;
52+
53+
[[nodiscard]] const std::vector<std::string>& getCompileFlags() const;
54+
55+
[[nodiscard]] bool hasVariable(std::string_view key) const;
56+
57+
[[nodiscard]] std::string_view getVariable(std::string_view key) const;
58+
59+
[[nodiscard]] std::string_view operator[](std::string_view key) const;
60+
61+
void update();
62+
63+
private:
64+
const IEntityAccess& entityAccess;
65+
std::string shader;
66+
std::vector<std::string> compileFlags;
67+
std::unordered_map<std::string, std::string> variables;
68+
std::vector<Proxy::Data> proxies;
69+
};
70+
71+
} // namespace vmtpp

src/vmtpp/EntityAccess.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <vmtpp/EntityAccess.h>
2+
3+
#include <chrono>
4+
5+
using namespace vmtpp;
6+
7+
EntityAccessEmpty::EntityAccessEmpty() {
8+
static float randomNumberGeneratorWinkWink = 0.f;
9+
this->random = randomNumberGeneratorWinkWink++;
10+
}
11+
12+
uint64_t EntityAccessEmpty::getCurrentTime() const {
13+
return std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
14+
}
15+
16+
float EntityAccessEmpty::getRenderAlpha() const {
17+
return 1.f;
18+
}
19+
20+
float EntityAccessEmpty::getAnimationProgress() const {
21+
return 0.f;
22+
}
23+
24+
float EntityAccessEmpty::getDistanceToCurrentPlayer() const {
25+
return 0.f;
26+
}
27+
28+
int EntityAccessEmpty::getCurrentPlayerTeam() const {
29+
return 0;
30+
}
31+
32+
int EntityAccessEmpty::getTeam() const {
33+
return 0;
34+
}
35+
36+
float EntityAccessEmpty::getCurrentPlayerViewDotProduct() const {
37+
return 0.f;
38+
}
39+
40+
float EntityAccessEmpty::getCurrentPlayerSpeed() const {
41+
return 0.f;
42+
}
43+
44+
sourcepp::math::Vec3f EntityAccessEmpty::getCurrentPlayerPosition() const {
45+
return {};
46+
}
47+
48+
float EntityAccessEmpty::getSpeed() const {
49+
return 0.f;
50+
}
51+
52+
sourcepp::math::Vec3f EntityAccessEmpty::getOrigin() const {
53+
return {};
54+
}
55+
56+
float EntityAccessEmpty::getRandomNumber() const {
57+
return this->random;
58+
}
59+
60+
float EntityAccessEmpty::getHealth() const {
61+
return 0.f;
62+
}
63+
64+
bool EntityAccessEmpty::isNPC() const {
65+
return false;
66+
}
67+
68+
bool EntityAccessEmpty::isViewModel() const {
69+
return false;
70+
}
71+
72+
sourcepp::math::Vec3f EntityAccessEmpty::getWorldDimensionsMinimum() const {
73+
return {};
74+
}
75+
76+
sourcepp::math::Vec3f EntityAccessEmpty::getWorldDimensionsMaximum() const {
77+
return {};
78+
}
79+
80+
sourcepp::math::Vec3f EntityAccessEmpty::getCurrentPlayerCrosshairColor() const {
81+
return {1.f, 1.f, 1.f};
82+
}

0 commit comments

Comments
 (0)