Skip to content

Commit a29b41d

Browse files
committed
libdnxpp+dx_sample: First feature complete release
Assuming nothing went wrong, libdnxpp should have feature parity with diannex.gml.
1 parent 67e7c54 commit a29b41d

25 files changed

Lines changed: 3276 additions & 0 deletions

CMakeLists.txt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
cmake_minimum_required(VERSION 3.26)
2+
project(libdnxpp)
3+
4+
set(CMAKE_CXX_STANDARD 23)
5+
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
6+
7+
option(BUILD_SAMPLE "Builds a sample program that uses the interepreter" ON)
8+
9+
set(ZLIB_USE_STATIC_LIBS ON)
10+
find_package(ZLIB REQUIRED)
11+
12+
add_library(libdnxpp SHARED
13+
include/diannex/common.hpp
14+
include/diannex/exceptions.hpp
15+
include/diannex/models.hpp
16+
include/diannex/utils.hpp
17+
include/diannex/utils/BinaryReader.hpp
18+
include/diannex/utils/generator.hpp
19+
include/diannex/utils/DxStack.hpp
20+
include/diannex/internal/DxValueConcepts.hpp
21+
include/diannex/DxInstructions.hpp
22+
include/diannex/DxData.hpp
23+
include/diannex/DxValue.hpp
24+
include/diannex/DxInterpreter.hpp
25+
src/DxData.cpp
26+
src/DxValue.cpp
27+
src/DxInterpreter.cpp
28+
src/DxInterpreterImpl.cpp
29+
src/utils/BinaryReader.cpp
30+
src/internal/DxDefinitionInstance.cpp
31+
)
32+
target_include_directories(libdnxpp PUBLIC include/ PRIVATE include/diannex)
33+
target_link_libraries(libdnxpp PRIVATE ZLIB::ZLIB)
34+
35+
if (BUILD_SAMPLE)
36+
add_subdirectory(sample/)
37+
endif ()

include/diannex/DxData.hpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*======================================================================================================================
2+
* Copyright © 2023 PeriBooty and Contributors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the “Software”), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
14+
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*====================================================================================================================*/
17+
#ifndef LIBDIANNEX_DXDATA_HPP
18+
#define LIBDIANNEX_DXDATA_HPP
19+
20+
#include "common.hpp"
21+
#include "models.hpp"
22+
23+
namespace diannex
24+
{
25+
class DxData
26+
{
27+
int m_currentCacheID{ -1 };
28+
29+
DxVec<DxStr> m_strings;
30+
DxVec<DxStr> m_translations;
31+
DxVec<std::byte> m_instructions;
32+
DxVec<DxFunction> m_functions;
33+
DxMap<DxStrRef, DxScene> m_scenes;
34+
DxMap<DxStrRef, DxDefinition> m_definitions;
35+
DxOpt<DxVec<DxStr>> m_originalText;
36+
public:
37+
static constexpr int FormatVersion = 4;
38+
static constexpr int TranslationFormatVersion = 0;
39+
40+
[[nodiscard]] DxStrRef string(size_t idx) const;
41+
42+
[[nodiscard]] DxStrRef translation(size_t idx) const;
43+
44+
[[nodiscard]] DxScene scene(const DxStrRef& name) const;
45+
46+
[[nodiscard]] const DxMap<DxStrRef, DxScene>& scenes() const;
47+
48+
[[nodiscard]] DxMap<DxStrRef, DxScene>& scenes_mut();
49+
50+
[[nodiscard]] DxROSpan<DxFunction> functions() const;
51+
52+
[[nodiscard]] DxSpan<DxFunction> functions_mut();
53+
54+
[[nodiscard]] DxDefinition definition(const DxStrRef& name) const;
55+
56+
[[nodiscard]] DxByteSpan instructions() const;
57+
58+
[[nodiscard]] inline int cacheID() const
59+
{ return m_currentCacheID; }
60+
61+
[[maybe_unused]] void loadTranslationFile(const DxStrRef& filename);
62+
63+
static DxData fromFile(const DxStrRef& filename);
64+
};
65+
}
66+
67+
#endif //LIBDIANNEX_DXDATA_HPP

include/diannex/DxInstructions.hpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*======================================================================================================================
2+
* Copyright © 2023 PeriBooty and Contributors
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the “Software”), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
7+
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
14+
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*====================================================================================================================*/
17+
#ifndef LIBDIANNEX_DXINSTRUCTIONS_HPP
18+
#define LIBDIANNEX_DXINSTRUCTIONS_HPP
19+
20+
namespace diannex
21+
{
22+
enum class [[maybe_unused]] DxOpcode : unsigned char
23+
{
24+
nop = 0x00, // No-op
25+
26+
freeloc = 0x0A, // Frees a local variable from the stack frame (IF IT EXISTS!): [ID]
27+
28+
// Special register instructions
29+
save = 0x0B, // Copy the value on the top of the stack into the save register
30+
load = 0x0C, // Push the value from the save register onto the top of the stack
31+
32+
// Stack instructions
33+
pushu = 0x0F, // Push undefined value to stack
34+
pushi = 0x10, // Push 32-bit int: [int value]
35+
pushd = 0x11, // Push 64-bit floating point: [double value]
36+
37+
pushs = 0x12, // Push external string: [index]
38+
pushints = 0x13, // Push external interpolated string: [index, expr count]
39+
pushbs = 0x14, // Push internal binary string: [ID]
40+
pushbints = 0x15, // Push internal binary interpolated string: [ID, expr count]
41+
42+
makearr = 0x16, // Construct an array based off of stack: [size]
43+
pusharrind = 0x17, // Extract a single value out of an array, removing the array as well (uses stack for index)
44+
setarrind = 0x18, // Sets a value in an array on the top of the stack (uses stack for index and value)
45+
46+
setvarglb = 0x19, // Set a global variable from the stack: [string name]
47+
setvarloc = 0x1A, // Set a local variable from the stack: [ID]
48+
pushvarglb = 0x1B, // Pushes a global variable to the stack: [string name]
49+
pushvarloc = 0x1C, // Pushes a local variable to the stack: [ID]
50+
51+
pop = 0x1D, // Discards the value on the top of the stack
52+
dup = 0x1E, // Duplicates the value on the top of the stack
53+
dup2 = 0x1F, // Duplicates the values on the top two slots of the stack
54+
55+
// Operators
56+
add = 0x20, // Adds the two values on the top of the stack, popping them, pushing the result
57+
sub = 0x21, // ditto, subtracts
58+
mul = 0x22, // ditto, multiplies
59+
div = 0x23, // ditto, divides
60+
mod = 0x24, // ditto, modulo
61+
neg = 0x25, // Negates the value on the top of the stack, popping it, pushing the result
62+
inv = 0x26, // ditto, but inverts a boolean
63+
64+
bitls = 0x27, // Peforms bitwise left-shift using the top two values of stack, popping them, pushing the result
65+
bitrs = 0x28, // ditto, right-shift
66+
_bitand = 0x29, // ditto, and
67+
_bitor = 0x2A, // ditto, or
68+
bitxor = 0x2B, // ditto, xor
69+
bitneg = 0x2C, // ditto, negate (~)
70+
71+
pow = 0x2D, // Power binary operation using top two values of stack
72+
73+
cmpeq = 0x30, // Compares the top two values of stack to check if they are equal, popping them, pushing the result
74+
cmpgt = 0x31, // ditto, greater than
75+
cmplt = 0x32, // ditto, less than
76+
cmpgte = 0x33, // ditto, greater than or equal
77+
cmplte = 0x34, // ditto, less than or equal
78+
cmpneq = 0x35, // ditto, not equal
79+
80+
// Control flow
81+
j = 0x40, // Jumps to an instruction [int relative address]
82+
jt = 0x41, // ditto, but if the value on the top of the stack is truthy (which it pops off)
83+
jf = 0x42, // ditto, but if the value on the top of the stack is NOT truthy (which it pops off)
84+
exit = 0x43, // Exits the current stack frame
85+
ret = 0x44, // Exits the current stack frame, returning a value (from the stack, popping it off)
86+
call = 0x45, // Calls a function defined in the code [ID, int parameter count]
87+
callext = 0x46, // Calls a function defined by a game [string name, int parameter count]
88+
89+
choicebeg = 0x47, // Switches to the choice state in the interpreter- no other choices can run and
90+
// only one textrun can execute until after choicesel is executed
91+
choiceadd = 0x48, // Adds a choice, using the stack for the text and the % chance of appearing [int relative jump address]
92+
choiceaddt = 0x49, // ditto, but also if an additional stack value is truthy [int relative jump address]
93+
choicesel = 0x4A, // Pauses the interpreter, waiting for user input to select one of the choices, then jumps to one of them, resuming
94+
95+
chooseadd = 0x4B, // Adds a new address to one of the possible next statements, using stack for chances [int relative jump address] (to the current stack frame)
96+
chooseaddt = 0x4C, // ditto, but also if an additional stack value is truthy [int relative jump address]
97+
choosesel = 0x4D, // Jumps to one of the choices, using the addresses and chances/requirement values on the stack
98+
99+
textrun = 0x4E, // Pauses the interpreter, running a line of text from the stack
100+
};
101+
}
102+
103+
#endif //LIBDIANNEX_DXINSTRUCTIONS_HPP

0 commit comments

Comments
 (0)