Started work on database handling.

This commit is contained in:
Arron David Nelson 2024-04-08 03:10:24 -07:00
parent 405acb026f
commit beba947c69
22 changed files with 918 additions and 44 deletions

View File

@ -82,7 +82,7 @@ set(EHS_SOURCES
include/ehs/ShdPtr.h
include/ehs/WkPtr.h
src/database/DVar.cpp include/ehs/database/DVar.h
src/db/DbVarTmpl.cpp include/ehs/db/DbVarTmpl.h
src/system/CPU.cpp include/ehs/system/CPU.h
src/system/Thread.cpp include/ehs/system/Thread.h
@ -162,6 +162,18 @@ set(EHS_SOURCES
src/io/hid/Input.cpp include/ehs/io/hid/Input.h
src/io/model/MdlCodec.cpp
include/ehs/io/mdl/MdlCodec.h
include/ehs/io/UsbBase.h
src/io/UsbBase.cpp
include/ehs/io/Usb_LNX.h
src/io/Usb_LNX.cpp
include/ehs/db/DbType.h
src/db/DbType.cpp
include/ehs/db/DbTable.h
include/ehs/db/DbObject.h
include/ehs/db/DbVar.h
src/db/DbVar.cpp
include/ehs/db/Database.h
src/db/DbObject.cpp
)
if (IS_OS_WINDOWS)

View File

@ -101,29 +101,73 @@ namespace ehs
return from * 57.295779513082320876798154814105;
}
template <typename T = float>
static T Exp(const T x)
{
T sum = 1;
T term = 1;
for (int n = 1; n <= 20; ++n)
{
term *= x / n;
sum += term;
}
return sum;
}
template <typename T = float>
static T Ln(T x)
{
if (x <= 0)
return -1;
T result = 0;
x = (x - 1) / (x + 1);
for (int i = 1; i <= 19; i += 2)
result += (1 / i) * Exp<T>(x * i);
return 2 * result;
}
/// A method for use of exponents.
/// @tparam T The data type to return;
/// @tparam I The data type to use as the exponent.
/// @param [in] from The value to use the exponent on.
/// @param [in] of The exponent.
/// @returns The result.
template<typename T = float, typename I = UInt_64>
static T Pow(const T from, const I of)
template<typename T = float, typename I = float>
static T Pow(const T base, const I exponent)
{
if (of < 0)
{
if (from == 0)
return -0;
if (base == 0)
return 0;
return 1 / (from * Pow<T>(from, (-of) - 1));
if (exponent == 0)
return 1;
UInt_32 exponentIsInteger = exponent == (UInt_32)exponent;
UInt_32 baseIsNegative = base < 0;
if (baseIsNegative && exponentIsInteger)
{
T result = Exp<T>(exponent * Ln<T>(-base));
if (Mod((float)exponent, 2.0f) != 0)
result = -result;
return result;
}
else if (!baseIsNegative)
return Exp<T>(exponent * Ln<T>(base));
return 0;
}
if (of == 0)
return 1;
else if (of == 1)
return from;
return from * Pow<T>(from, of - 1);
template <typename T = float>
static T Log10(const T x)
{
return Ln<T>(x) / Ln<T>(10);
}
static float Near(const float from);

View File

@ -1078,8 +1078,8 @@ namespace ehs
bool sizeKnown = size;
if (!sizeKnown)
while (&data[offset + sizeof(T) * size])
size++;
while (((T*)&data[offset])[size])
++size;
Str<T, O> result(size);
@ -1090,14 +1090,14 @@ namespace ehs
else
for (N i = 0; i < size; ++i)
for (N b = 0; b < sizeof(T); ++b)
((Byte*)&result[i])[sizeof(T) - i - 1] = data[offset + sizeof(T) * i + b];
((Byte*)&result[i])[b] = data[offset + sizeof(T) * i + b];
}
else
{
if (endianness == Endianness::LE)
for (N i = 0; i < size; ++i)
for (N b = 0; b < sizeof(T); ++b)
((Byte*)&result[i])[sizeof(T) - i - 1] = data[offset + sizeof(T) * i + b];
((Byte*)&result[i])[b] = data[offset + sizeof(T) * i + b];
else
Util::Copy(&result[0], &data[offset], result.Size(true));
}

View File

@ -1,6 +1,7 @@
#pragma once
#include "ehs/system/OS.h"
#include "ehs/system/Architecture.h"
#define EHS_MAX_PATH 0x104
#define EHS_UINT_8_MAX 0xFF
@ -25,6 +26,12 @@
#define EHS_LDOUBLE_MAX 1.79769e+308
#define EHS_LDOUBLE_MIN 2.22507e-308
#if defined(EHS_64_BIT)
#define EHS_SIZE_MAX 0xFFFFFFFFFFFFFFFF
#elif defined(EHS_32_BIT)
#define EHS_SIZE_MAX 0xFFFFFFFF
#endif
#define EHS_INFINITE EHS_UINT_32_MAX
namespace ehs
@ -52,4 +59,10 @@ namespace ehs
typedef signed long SInt_64;
typedef long Int_64;
#endif
#if defined(EHS_64_BIT)
typedef UInt_64 Size;
#elif defined(EHS_32_BIT)
typedef UInt_32 Size;
#endif
}

View File

@ -1,20 +0,0 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/BaseObj.h"
#include "ehs/Str.h"
namespace ehs
{
enum class DType : UInt_8
{
};
class DVar : public BaseObj
{
private:
Str_8 id;
UInt_64 hashId;
};
}

36
include/ehs/db/Database.h Normal file
View File

@ -0,0 +1,36 @@
#pragma once
#include "ehs/Array.h"
#include "DbTable.h"
namespace ehs
{
class Database
{
private:
UInt_64 hashId;
Str_8 id;
Array<DbTable> tables;
public:
Database();
Database(const Str_8& filePath);
UInt_64 GetHashId() const;
Str_8 GetId() const;
bool HasTable(UInt_64 hashId) const;
bool HasTable(Str_8& id) const;
bool CreateTable(Str_8 id);
DbTable* GetTable(UInt_64 hashId) const;
DbTable* GetTable(Str_8& id) const;
void Save();
};
}

47
include/ehs/db/DbObject.h Normal file
View File

@ -0,0 +1,47 @@
#pragma once
#include "ehs/Array.h"
#include "DbVar.h"
namespace ehs
{
class DbTable;
class DbObject
{
private:
friend class DbTable;
UInt_64 id;
Array<DbVar> vars;
bool loaded;
DbTable* parent;
public:
DbObject();
DbObject(DbObject&& obj) noexcept;
DbObject(const DbObject& obj);
DbObject& operator=(DbObject&& obj) noexcept;
DbObject& operator=(const DbObject& obj);
UInt_64 GetId();
bool HasVariable(UInt_64 hashId);
DbVar* GetVariable(UInt_64 hashId) const;
void Save();
void Load();
bool IsLoaded() const;
void Free();
DbTable* GetParent() const;
};
}

36
include/ehs/db/DbTable.h Normal file
View File

@ -0,0 +1,36 @@
#pragma once
#include "ehs/Version.h"
#include "ehs/Array.h"
#include "DbVarTmpl.h"
#include "DbObject.h"
namespace ehs
{
class DbTable
{
private:
UInt_64 hashId;
Str_8 id;
Version version;
Array<DbVarTmpl> varTmpls;
Array<DbObject> objects;
public:
DbTable();
DbTable();
UInt_64 GetHashId() const;
Str_8 GetId() const;
Version GetVersion() const;
bool CreateVariable(Str_8 id, DbType type);
bool CreateVariable(Str_8 id, DbType type, UInt_64 size);
void Save();
};
}

26
include/ehs/db/DbType.h Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include "ehs/Types.h"
namespace ehs
{
enum class DbType : UInt_8
{
SINT_64,
UINT_64,
SINT_32,
UINT_32,
SINT_16,
UINT_16,
SINT_8,
UINT_8,
FLOAT,
DOUBLE,
BOOLEAN,
CHAR_32,
CHAR_16,
CHAR_8
};
constexpr UInt_8 DbTypeToSize(DbType type);
}

37
include/ehs/db/DbVar.h Normal file
View File

@ -0,0 +1,37 @@
#pragma once
#include "ehs/Types.h"
namespace ehs
{
class DbVar
{
private:
UInt_64 hashId;
UInt_64 size;
Byte* data;
public:
~DbVar();
DbVar();
DbVar(UInt_64 hashId, UInt_64 size, Byte* data);
DbVar(DbVar&& var) noexcept;
DbVar(const DbVar& var);
DbVar& operator=(DbVar&& var) noexcept;
DbVar& operator=(const DbVar& var);
explicit operator Byte*() const;
UInt_64 GetHashId() const;
UInt_64 GetSize() const;
Byte* GetData() const;
};
}

View File

@ -0,0 +1,45 @@
#pragma once
#include "DbType.h"
#include "ehs/EHS.h"
#include "ehs/BaseObj.h"
#include "ehs/Str.h"
namespace ehs
{
class DbVarTmpl : public BaseObj
{
private:
UInt_64 hashId;
Str_8 id;
DbType type;
bool array;
public:
DbVarTmpl();
DbVarTmpl(Str_8 id, DbType type, bool array);
DbVarTmpl(DbVarTmpl&& varTmpl) noexcept;
DbVarTmpl(const DbVarTmpl& varTmpl);
DbVarTmpl& operator=(DbVarTmpl&& varTmpl) noexcept;
DbVarTmpl& operator=(const DbVarTmpl& varTmpl);
UInt_64 GetHashId() const;
void SetId(Str_8 newId);
Str_8 GetId() const;
void SetType(DbType newType);
DbType GetType() const;
void SetIsArray(bool value);
bool IsArray() const;
};
}

48
include/ehs/io/UsbBase.h Normal file
View File

@ -0,0 +1,48 @@
#pragma once
#include "ehs/Types.h"
namespace ehs
{
class UsbBase
{
private:
UInt_32 bus;
UInt_32 address;
public:
virtual ~UsbBase() = default;
UsbBase();
UsbBase(UInt_32 bus, UInt_32 address);
UsbBase(UsbBase&& usb) noexcept;
UsbBase(const UsbBase& usb);
UsbBase& operator=(UsbBase&& usb) noexcept;
UsbBase& operator=(const UsbBase& usb);
virtual void Initialize() = 0;
virtual void Release() = 0;
virtual bool IsInitialized() const = 0;
virtual Size Send(const Byte* data, Size size) = 0;
void BulkSend(const Byte* data, Size size);
virtual Size Receive(Byte* data, Size size) = 0;
void BulkReceive(Byte** data, Size* size);
UInt_32 GetBus() const;
UInt_32 GetAddress() const;
bool IsValid() const;
};
}

34
include/ehs/io/Usb_LNX.h Normal file
View File

@ -0,0 +1,34 @@
#pragma once
#include "UsbBase.h"
#include "File.h"
namespace ehs
{
class Usb final : public UsbBase
{
private:
int hdl;
public:
~Usb() override;
Usb();
Usb(UInt_32 bus, UInt_32 address);
Usb(Usb&& usb) noexcept;
Usb(const Usb& usb);
Usb& operator=(Usb&& usb) noexcept;
Usb& operator=(const Usb& usb);
void Initialize() override;
void Release() override;
bool IsInitialized() const override;
};
}

View File

@ -3,9 +3,11 @@
#if defined(_M_AMD64) || defined(_M_X64) || defined(__x86_64__)
#define EHS_LITTLE_ENDIAN
#define EHS_ARCH_X64
#define EHS_64_BIT
#elif defined(_M_ARM64) || defined(__aarch64__)
#define EHS_LITTLE_ENDIAN
#define EHS_LITTLE_ENDIAN
#define EHS_ARCH_ARM64
#define EHS_64_BIT
#else
#error Unsupported architecture.
#endif

View File

@ -1,5 +0,0 @@
#include "ehs/database/DVar.h"
namespace ehs
{
}

98
src/db/DbObject.cpp Normal file
View File

@ -0,0 +1,98 @@
#include "ehs/db/DbObject.h"
namespace ehs
{
DbObject::DbObject()
: id(0), loaded(false), parent(nullptr)
{
}
DbObject::DbObject(DbObject&& obj) noexcept
: id(obj.id), vars((Array<DbVar>&&)obj.vars), loaded(obj.loaded), parent(obj.parent)
{
obj.id = 0;
obj.loaded = false;
obj.parent = nullptr;
}
DbObject::DbObject(const DbObject& obj)
: id(obj.id), vars(obj.vars), loaded(obj.loaded), parent(obj.parent)
{
}
DbObject& DbObject::operator=(DbObject&& obj) noexcept
{
if (this == &obj)
return *this;
id = obj.id;
vars = (Array<DbVar>&&)obj.vars;
loaded = obj.loaded;
parent = obj.parent;
obj.id = 0;
obj.loaded = false;
obj.parent = nullptr;
return *this;
}
DbObject& DbObject::operator=(const DbObject& obj)
{
if (this == &obj)
return *this;
id = obj.id;
vars = obj.vars;
loaded = obj.loaded;
parent = obj.parent;
return *this;
}
UInt_64 DbObject::GetId()
{
return id;
}
bool DbObject::HasVariable(UInt_64 hashId)
{
for (UInt_64 i = 0; i < vars.Size(); ++i)
if (vars[i].GetHashId() == hashId)
return true;
return false;
}
DbVar* DbObject::GetVariable(UInt_64 hashId) const
{
for (UInt_64 i = 0; i < vars.Size(); ++i)
if (vars[i].GetHashId() == hashId)
return &vars[i];
return nullptr;
}
void DbObject::Save()
{
}
void DbObject::Load()
{
}
bool DbObject::IsLoaded() const
{
return loaded;
}
void DbObject::Free()
{
vars.Clear();
}
DbTable* DbObject::GetParent() const
{
return parent;
}
}

39
src/db/DbType.cpp Normal file
View File

@ -0,0 +1,39 @@
#include "ehs/db/DbType.h"
namespace ehs
{
constexpr UInt_8 DbTypeToSize(const DbType type)
{
switch (type)
{
case DbType::SINT_64:
return 8;
case DbType::UINT_64:
return 8;
case DbType::SINT_32:
return 4;
case DbType::UINT_32:
return 4;
case DbType::SINT_16:
return 2;
case DbType::UINT_16:
return 2;
case DbType::SINT_8:
return 1;
case DbType::UINT_8:
return 1;
case DbType::FLOAT:
return 4;
case DbType::DOUBLE:
return 8;
case DbType::BOOLEAN:
return 1;
case DbType::CHAR_32:
return 4;
case DbType::CHAR_16:
return 2;
case DbType::CHAR_8:
return 1;
}
}
}

84
src/db/DbVar.cpp Normal file
View File

@ -0,0 +1,84 @@
#include "ehs/db/DbVar.h"
namespace ehs
{
DbVar::~DbVar()
{
delete[] data;
}
DbVar::DbVar()
: hashId(0), size(0), data(nullptr)
{
}
DbVar::DbVar(UInt_64 hashId, UInt_64 size, Byte* data)
: hashId(hashId), size(size), data(data)
{
}
DbVar::DbVar(DbVar&& var) noexcept
: hashId(var.hashId), size(var.size), data(var.data)
{
var.hashId = 0;
var.size = 0;
var.data = nullptr;
}
DbVar::DbVar(const DbVar& var)
: hashId(0), size(0), data(nullptr)
{
}
DbVar& DbVar::operator=(DbVar&& var) noexcept
{
if (this == &var)
return *this;
delete[] data;
hashId = var.hashId;
size = var.size;
data = var.data;
var.hashId = 0;
var.size = 0;
var.data = nullptr;
return *this;
}
DbVar& DbVar::operator=(const DbVar& var)
{
if (this == &var)
return *this;
delete[] data;
hashId = 0;
size = 0;
data = nullptr;
return *this;
}
DbVar::operator Byte*() const
{
return data;
}
UInt_64 DbVar::GetHashId() const
{
return hashId;
}
UInt_64 DbVar::GetSize() const
{
return size;
}
Byte* DbVar::GetData() const
{
return data;
}
}

93
src/db/DbVarTmpl.cpp Normal file
View File

@ -0,0 +1,93 @@
#include "ehs/db/DbVarTmpl.h"
namespace ehs
{
DbVarTmpl::DbVarTmpl()
: hashId(0), type(DbType::SINT_64), array(false)
{
}
DbVarTmpl::DbVarTmpl(Str_8 id, DbType type, bool array)
: hashId(id.Hash_64()), id((Str_8&&)id), type(type), array(array)
{
}
DbVarTmpl::DbVarTmpl(DbVarTmpl&& varTmpl) noexcept
: hashId(varTmpl.hashId), id((Str_8&&)varTmpl.id), type(varTmpl.type), array(varTmpl.array)
{
varTmpl.hashId = 0;
varTmpl.type = DbType::SINT_64;
varTmpl.array = false;
}
DbVarTmpl::DbVarTmpl(const DbVarTmpl& varTmpl)
: hashId(varTmpl.hashId), id(varTmpl.id), type(varTmpl.type), array(varTmpl.array)
{
}
DbVarTmpl& DbVarTmpl::operator=(DbVarTmpl&& varTmpl) noexcept
{
if (this == &varTmpl)
return *this;
hashId = varTmpl.hashId;
id = (Str_8&&)varTmpl.id;
type = varTmpl.type;
array = varTmpl.array;
varTmpl.hashId = 0;
varTmpl.type = DbType::SINT_64;
varTmpl.array = false;
return *this;
}
DbVarTmpl& DbVarTmpl::operator=(const DbVarTmpl& varTmpl)
{
if (this == &varTmpl)
return *this;
hashId = varTmpl.hashId;
id = varTmpl.id;
type = varTmpl.type;
array = varTmpl.array;
return *this;
}
UInt_64 DbVarTmpl::GetHashId() const
{
return hashId;
}
void DbVarTmpl::SetId(Str_8 newId)
{
hashId = newId.Hash_64();
id = (Str_8&&)newId;
}
Str_8 DbVarTmpl::GetId() const
{
return id;
}
void DbVarTmpl::SetType(DbType newType)
{
type = newType;
}
DbType DbVarTmpl::GetType() const
{
return type;
}
void DbVarTmpl::SetIsArray(bool value)
{
array = value;
}
bool DbVarTmpl::IsArray() const
{
return array;
}
}

97
src/io/UsbBase.cpp Normal file
View File

@ -0,0 +1,97 @@
#include "ehs/io/UsbBase.h"
#include "ehs/Util.h"
#include <unistd.h>
namespace ehs
{
UsbBase::UsbBase()
: bus(0), address(0)
{
}
UsbBase::UsbBase(const UInt_32 bus, const UInt_32 address)
: bus(bus), address(address)
{
}
UsbBase::UsbBase(UsbBase&& usb) noexcept
: bus(usb.bus), address(usb.address)
{
usb.bus = 0;
usb.address = 0;
}
UsbBase::UsbBase(const UsbBase& usb)
: bus(usb.bus), address(usb.address)
{
}
UsbBase& UsbBase::operator=(UsbBase&& usb) noexcept
{
if (this == &usb)
return *this;
bus = usb.bus;
address = usb.address;
usb.bus = 0;
usb.address = 0;
return *this;
}
UsbBase& UsbBase::operator=(const UsbBase& usb)
{
if (this == &usb)
return *this;
bus = usb.bus;
address = usb.address;
return *this;
}
void UsbBase::BulkSend(const Byte* const data, const Size size)
{
Size out = 0;
while (out < size)
out = Send(&data[out], size);
}
void UsbBase::BulkReceive(Byte** data, Size* size)
{
Byte *result = nullptr;
Size total = 0;
Size in;
do
{
Byte *resize = new Byte[(total + *size) / *size];
Util::Copy(resize, result, total);
in = Receive(&resize[total], *size);
total += in;
delete[] result;
result = resize;
}
while (in);
*size = total;
}
UInt_32 UsbBase::GetBus() const
{
return bus;
}
UInt_32 UsbBase::GetAddress() const
{
return address;
}
bool UsbBase::IsValid() const
{
return bus && address;
}
}

106
src/io/Usb_LNX.cpp Normal file
View File

@ -0,0 +1,106 @@
#include "ehs/io/Usb_LNX.h"
#include <fcntl.h>
#include <unistd.h>
namespace ehs
{
Usb::~Usb()
{
}
Usb::Usb()
: hdl(-1)
{
}
Usb::Usb(const UInt_32 bus, const UInt_32 address)
: UsbBase(bus, address), hdl(-1)
{
Initialize();
}
Usb::Usb(Usb&& usb) noexcept
: UsbBase((UsbBase&&)usb), hdl(usb.hdl)
{
usb.hdl = -1;
}
Usb::Usb(const Usb& usb)
: UsbBase(usb), hdl(-1)
{
Initialize();
}
Usb& Usb::operator=(Usb&& usb) noexcept
{
if (this == &usb)
return *this;
UsbBase::operator=((UsbBase&&)usb);
hdl = usb.hdl;
usb.hdl = -1;
return *this;
}
Usb& Usb::operator=(const Usb& usb)
{
if (this == &usb)
return *this;
UsbBase::operator=(usb);
hdl = -1;
Initialize();
return *this;
}
void Usb::Initialize()
{
if (!IsValid())
{
EHS_LOG_INT("Error", 0, "Cannot initialize with an invalid object.");
return;
}
if (IsInitialized())
{
EHS_LOG_INT("Warning", 1, "Object is already initialized.");
return;
}
hdl = open("/dev/bus/usb/" + Str_8::FromNum(GetBus()) + "/" + Str_8::FromNum(GetAddress()), O_RDWR);
if (hdl == -1)
EHS_LOG_INT("Error", 2, "Failed to connect to USB device.");
}
void Usb::Release()
{
if (!IsValid())
{
EHS_LOG_INT("Error", 0, "Cannot release with an invalid object.");
return;
}
if (!IsInitialized())
{
EHS_LOG_INT("Warning", 1, "Object is already released.");
return;
}
close(hdl);
hdl = -1;
}
bool Usb::IsInitialized() const
{
return hdl != -1;
}
}

View File

@ -275,6 +275,7 @@ namespace ehs
meshes[i].SetIndices(data.ReadArray<UInt_32, UInt_64>());
/*
Bone& skeleton = mdl->GetSkeleton();
UInt_8 boneCount = data.Read<UInt_8>();
for (UInt_8 b = 0; b < boneCount; ++b)
@ -328,6 +329,7 @@ namespace ehs
}
}
}
*/
}
return true;