From e933ccb59c7f032d2ffc5c5f7046317a06e85e14 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Tue, 17 Oct 2023 02:30:52 +0200 Subject: codec,codec-minecraft: Adding basic foundations needed for the minecraft protocol --- c++/codec-minecraft/.nix/derivation.nix | 32 +++++++++ c++/codec-minecraft/SConscript | 38 +++++++++++ c++/codec-minecraft/SConstruct | 66 ++++++++++++++++++ c++/codec-minecraft/minecraft.h | 116 ++++++++++++++++++++++++++++++++ c++/codec-minecraft/var_int_data.cpp | 36 ++++++++++ c++/codec-minecraft/var_int_data.h | 31 +++++++++ c++/codec/schema.h | 15 ++++- 7 files changed, 331 insertions(+), 3 deletions(-) create mode 100644 c++/codec-minecraft/.nix/derivation.nix create mode 100644 c++/codec-minecraft/SConscript create mode 100644 c++/codec-minecraft/SConstruct create mode 100644 c++/codec-minecraft/minecraft.h create mode 100644 c++/codec-minecraft/var_int_data.cpp create mode 100644 c++/codec-minecraft/var_int_data.h (limited to 'c++') diff --git a/c++/codec-minecraft/.nix/derivation.nix b/c++/codec-minecraft/.nix/derivation.nix new file mode 100644 index 0000000..7704d13 --- /dev/null +++ b/c++/codec-minecraft/.nix/derivation.nix @@ -0,0 +1,32 @@ +{ lib +, stdenvNoCC +, scons +, clang +, clang-tools +, version +, forstio +}: + +let + +in stdenvNoCC.mkDerivation { + pname = "forstio-codec-minecraft"; + inherit version; + src = ./..; + + enableParallelBuilding = true; + + nativeBuildInputs = [ + scons + clang + clang-tools + ]; + + buildInputs = [ + forstio.core + forstio.async + forstio.codec + ]; + + outputs = ["out" "dev"]; +} diff --git a/c++/codec-minecraft/SConscript b/c++/codec-minecraft/SConscript new file mode 100644 index 0000000..4d1deab --- /dev/null +++ b/c++/codec-minecraft/SConscript @@ -0,0 +1,38 @@ +#!/bin/false + +import os +import os.path +import glob + + +Import('env') + +dir_path = Dir('.').abspath + +# Environment for base library +codec_minecraft_env = env.Clone(); + +codec_minecraft_env.sources = sorted(glob.glob(dir_path + "/*.cpp")) +codec_minecraft_env.headers = sorted(glob.glob(dir_path + "/*.h")) + +env.sources += codec_minecraft_env.sources; +env.headers += codec_minecraft_env.headers; + +## Shared lib +objects_shared = [] +codec_minecraft_env.add_source_files(objects_shared, codec_minecraft_env.sources, shared=True); +codec_minecraft_env.library_shared = codec_minecraft_env.SharedLibrary('#build/forstio-codec-minecraft', [objects_shared]); + +## Static lib +objects_static = [] +codec_minecraft_env.add_source_files(objects_static, codec_minecraft_env.sources, shared=False); +codec_minecraft_env.library_static = codec_minecraft_env.StaticLibrary('#build/forstio-codec-minecraft', [objects_static]); + +# Set Alias +env.Alias('library_codec_minecraft', [codec_minecraft_env.library_shared, codec_minecraft_env.library_static]); + +env.targets += ['library_codec_minecraft']; + +# Install +env.Install('$prefix/lib/', [codec_minecraft_env.library_shared, codec_minecraft_env.library_static]); +env.Install('$prefix/include/forstio/codec/minecraft/', [codec_minecraft_env.headers]); diff --git a/c++/codec-minecraft/SConstruct b/c++/codec-minecraft/SConstruct new file mode 100644 index 0000000..edd5f57 --- /dev/null +++ b/c++/codec-minecraft/SConstruct @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 + +import sys +import os +import os.path +import glob +import re + + +if sys.version_info < (3,): + def isbasestring(s): + return isinstance(s,basestring) +else: + def isbasestring(s): + return isinstance(s, (str,bytes)) + +def add_kel_source_files(self, sources, filetype, lib_env=None, shared=False, target_post=""): + + if isbasestring(filetype): + dir_path = self.Dir('.').abspath + filetype = sorted(glob.glob(dir_path+"/"+filetype)) + + for path in filetype: + target_name = re.sub( r'(.*?)(\.cpp|\.c\+\+)', r'\1' + target_post, path ) + if shared: + target_name+='.os' + sources.append( self.SharedObject( target=target_name, source=path ) ) + else: + target_name+='.o' + sources.append( self.StaticObject( target=target_name, source=path ) ) + pass + +def isAbsolutePath(key, dirname, env): + assert os.path.isabs(dirname), "%r must have absolute path syntax" % (key,) + +env_vars = Variables( + args=ARGUMENTS +) + +env_vars.Add('prefix', + help='Installation target location of build results and headers', + default='/usr/local/', + validator=isAbsolutePath +) + +env=Environment(ENV=os.environ, variables=env_vars, CPPPATH=[], + CPPDEFINES=['SAW_UNIX'], + CXXFLAGS=['-std=c++20','-g','-Wall','-Wextra'], + LIBS=['forstio-codec']) +env.__class__.add_source_files = add_kel_source_files +env.Tool('compilation_db'); +env.cdb = env.CompilationDatabase('compile_commands.json'); + +env.objects = []; +env.sources = []; +env.headers = []; +env.targets = []; + +Export('env') +SConscript('SConscript') + +env.Alias('cdb', env.cdb); +env.Alias('all', [env.targets]); +env.Default('all'); + +env.Alias('install', '$prefix') diff --git a/c++/codec-minecraft/minecraft.h b/c++/codec-minecraft/minecraft.h new file mode 100644 index 0000000..670b019 --- /dev/null +++ b/c++/codec-minecraft/minecraft.h @@ -0,0 +1,116 @@ +#pragma once + +#include +#include +#include + +namespace saw { +namespace encode { +struct Minecraft {}; +struct VarIntTransport {}; +} + +template +class data { +private: + ring_buffer buffer_; +public: + data() = default; + data(std::size_t size): + buffer_{size} + {} + + buffer& get_buffer(){ + return buffer_; + } +}; + +namespace mc { +namespace impl { +union minecraft_signed_four_conversion { + int32_t s; + uint32_t u; +}; + +union minecraft_signed_eight_conversion { + int64_t s; + uint64_t u; +}; + +template +class minecraft_encode { + static_assert(always_false, "This schema type is not being handled by the Minecraft encoding."); +}; + +template +class minecraft_encode{ + static error_or encode(const data& from, buffer& to){ + uint8_t encode_index = 0; + minecraft_signed_four_conversion value; + value.s = from.get(); + + /** + * VarInt max length is 5 bytes + */ + std::array encode_data; + + do { + uint8_t step = static_cast(value.u & 0x7F); + value.u = value.u >> 7; + if(value.u != 0){ + step |= 0x80; + } + encode_data[encode_index] = step; + ++encode_index; + }while(value.u != 0); + + auto err = buffer.push(encode_data[0], encode_index); + if (!err.template is_type()) { + return err; + } + + return no_error(); + } +}; + +template +class minecraft_decode { + static_assert(always_false, "This schema type is not being handled by the Minecraft encoding."); +}; + +template +class minecraft_decode{ + static error_or decode(buffer& from, data& to){ + uint8_t num_reads = 0; + + minecraft_signed_four_conversion value; + value.u = 0; + + uint8_t read{}; + do { + auto err = from.pop(read); + if( !err.template is_type() ){ + return err; + } + value.u |= ((read & 0x7F) << (7*num_reads)); + ++num_reads; + if(num_reads > 5){ + return make_error(); + } + } while( (read & 0x80) != 0); + + to.set(value.s); + + return no_error(); + } +}; + +} +} + + + +namespace mc { + +} +} diff --git a/c++/codec-minecraft/var_int_data.cpp b/c++/codec-minecraft/var_int_data.cpp new file mode 100644 index 0000000..59e4317 --- /dev/null +++ b/c++/codec-minecraft/var_int_data.cpp @@ -0,0 +1,36 @@ +#include "var_int_data.h" + +namespace saw { +data::data(): + data_{} +{} + +data::data(int32_t data): + data_{data} +{} + +int32_t data::get() const { + return data_; +} + +void data::set(int32_t data) { + data_ = data; +} + +data::data(): + data_{} +{} + +data::data(int64_t data): + data_{data} +{} + +int64_t data::get() const { + return data_; +} + +void data::set(int64_t data) { + data_ = data; +} + +} diff --git a/c++/codec-minecraft/var_int_data.h b/c++/codec-minecraft/var_int_data.h new file mode 100644 index 0000000..807c930 --- /dev/null +++ b/c++/codec-minecraft/var_int_data.h @@ -0,0 +1,31 @@ +#pragma once + +#include + +namespace saw { +template<> +class data { +private: + int32_t data_; +public: + data(); + data(int32_t data); + + int32_t get() const; + void set(int32_t); +}; + +template<> +class data { +private: + int64_t data_; +public: + data(); + data(int64_t data); + + int64_t get() const; + void set(int64_t); +}; + + +} diff --git a/c++/codec/schema.h b/c++/codec/schema.h index ba6dd4c..a8494fe 100644 --- a/c++/codec/schema.h +++ b/c++/codec/schema.h @@ -80,10 +80,19 @@ using UInt64 = Primitive; using Float32 = Primitive; using Float64 = Primitive; +/** + * Classes allowing to distinguish Ints from VarInts + */ +template +struct VariableLengthPrimitive {}; + +using VarInt = VariableLengthPrimitive; +using VarLong = VariableLengthPrimitive; + /** * Classes enabling Rpc calls */ -template +template struct Function {}; template struct Interface { @@ -92,8 +101,8 @@ template struct Interface { "This schema template doesn't support this type of template argument"); }; -template -struct Interface...> {}; +template +struct Interface,Names>...> {}; // NOLINTEND } // namespace schema -- cgit v1.2.3