Skip to content

Commit

Permalink
Added TUser
Browse files Browse the repository at this point in the history
Added (not implemented) TFreeList
  • Loading branch information
AdventureT committed Dec 6, 2023
1 parent 5de401b commit ddc612a
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 1 deletion.
19 changes: 18 additions & 1 deletion OpenJPOG/Source/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
#include "TApplication/TApplication.h"
#include "TMemory.h"

class AApplication : public Toshi::TApplication
{
virtual TBOOL OnCreate(TINT argc, TPCHAR* const argv) override
{
return TTRUE;
}
};

static AApplication g_oTheApp;

int main(int argc, char** argv)
{
return 1;
if (g_oTheApp.Create("Jurassic Park: Operation Genesis - (c) Blue Tongue Software", argc, argv)) {
Toshi::TMemory::DebugPrintHALMemInfo(TNULL);
g_oTheApp.Execute();
}
g_oTheApp.Destroy();
return 0;
}
12 changes: 12 additions & 0 deletions Tools/UnitTests/Source/TKernel/TUser_Tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "TKernel/TUser.h"
#include <catch2/catch_test_macros.hpp>

TOSHI_NAMESPACE_USING

TEST_CASE("Register User", "[TUser]")
{
TUser user("AdventureT");
TSHORT iUserID = user.Register();
REQUIRE(iUserID != -1);
REQUIRE(user.IsRegistered());
}
31 changes: 31 additions & 0 deletions Toshi/Include/TKernel/TFreeList.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include "TMemory.h"

TOSHI_NAMESPACE_BEGIN

class TOSHI_EXPORT TFreeList
{
public:

struct Node
{
Node* pNext = TNULL;
};

TFreeList();

TINT GetCapacity() const { return m_iCapacity; }
TINT GetGrowSize() const { return m_iGrowSize; }
TINT GetFreeCount() { return m_iFreeCount; }
private:
TFreeList* m_pPrevList;
TINT m_iFreeCount; // 0x0
TUINT m_uiItemSize;
Node m_LastNode;
TINT m_iCapacity; // 0xC
Node m_RootNode;
TINT m_iGrowSize; // 0x1C
};

TOSHI_NAMESPACE_END
1 change: 1 addition & 0 deletions Toshi/Include/TKernel/TSystemTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class TOSHI_EXPORT TSystem
static TINT __stdcall StringCompareNoCase(TPCCHAR a_String1, TPCCHAR a_String2, TINT a_uiSize);
static TCHAR const* __stdcall StringCopy(TPCHAR a_DestinationString, TCHAR const* a_SourceString, TINT a_iCount);
static TPVOID __stdcall MemCopy(TPVOID a_dest, TPCVOID a_src, TUINT a_iSize);
static TPVOID __stdcall MemSet(TPVOID a_dest, TINT a_iValue, TINT m_iSize);

static TPBYTE GetScratchMem() { return ms_aScratchMem; }
private:
Expand Down
50 changes: 50 additions & 0 deletions Toshi/Include/TKernel/TUser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include "TDebug.h"

#define MAX_USERS 50

TOSHI_NAMESPACE_BEGIN

class TOSHI_EXPORT TUser
{
friend class TUserHandler;
public:
TUser(TCHAR const* a_sName);

TBOOL IsRegistered() { return m_iUserID != -1; }
TSHORT Register();
void Deregister();

TSHORT GetUserID() { return m_iUserID; }
TSHORT GetNameLength() { return m_iNameLen; }
TCHAR const* GetName() { return m_sName; }

private:
TCHAR const* m_sName; // 0x0
TSHORT m_iNameLen; // 0x4
TSHORT m_iUserID; // 0x6
};

class TOSHI_EXPORT TUserHandler
{
protected:
TUserHandler();
~TUserHandler() = default;
public:
TSHORT RegisterUser(TUser &a_rUser);
void DeregisterUser(TUser &a_rUser);
TUser* FindUser(TCHAR const* a_sName);

TINT GetUserCount() { return m_iUserCount; }
TUser* GetUser(TINT a_iUserID) { return m_pUsers[a_iUserID]; }
static TUserHandler& __stdcall GetSingleton();

private:
static TUserHandler ms_oSingleton;

TINT m_iUserCount; // 0x0
TUser* m_pUsers[MAX_USERS]; // 0x4
};

TOSHI_NAMESPACE_END
8 changes: 8 additions & 0 deletions Toshi/Source/TKernel/TFreeList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "TFreeList.h"

TOSHI_NAMESPACE_USING

TFreeList::TFreeList()
{
TASSERT(m_iGrowSize >= 0);
}
8 changes: 8 additions & 0 deletions Toshi/Source/TKernel/TSystemTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ TPVOID __stdcall TSystem::MemCopy(TPVOID a_dest, TPCVOID a_src, TUINT a_iSize)
*d++ = *s++;
return a_dest;
}

TPVOID __stdcall TSystem::MemSet(TPVOID a_dest, TINT a_iValue, TINT m_iSize)
{
TPCHAR dest = (TPCHAR)a_dest;
while (m_iSize--)
*dest++ = (TCHAR)a_iValue;
return a_dest;
}
70 changes: 70 additions & 0 deletions Toshi/Source/TKernel/TUser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "TUser.h"
#include "TSystemTools.h"

TOSHI_NAMESPACE_USING

TUserHandler TUserHandler::ms_oSingleton = TUserHandler();

TUser::TUser(TCHAR const* a_sName)
{
m_sName = a_sName;
m_iUserID = -1;
TVALIDADDRESS(m_sName);
m_iNameLen = TSystem::StringLength(m_sName);
}

TUserHandler::TUserHandler()
{
m_iUserCount = 0;
TSystem::MemSet(m_pUsers, 0, sizeof(m_pUsers));
}

TSHORT TUserHandler::RegisterUser(TUser& a_rUser)
{
TASSERT(TFALSE==a_rUser.IsRegistered());
TINT curUserCount = GetUserCount();
if (curUserCount < MAX_USERS) {
m_iUserCount++;
a_rUser.m_iUserID = curUserCount;
m_pUsers[curUserCount] = &a_rUser;
return a_rUser.GetUserID();
}
a_rUser.m_iUserID = -1;
return -1;
}

void TUserHandler::DeregisterUser(TUser& a_rUser)
{
if (a_rUser.GetUserID() != -1) {
m_pUsers[a_rUser.GetUserID()] = TNULL;
}
a_rUser.m_iUserID = -1;
}

TUser* TUserHandler::FindUser(TCHAR const* a_sName)
{
TINT iNameLength = TSystem::StringLength(a_sName);
for (TINT i = 0; i < MAX_USERS; i++) {
if (m_pUsers[i] &&
m_pUsers[i]->GetNameLength() == iNameLength &&
TSystem::StringCompareNoCase(m_pUsers[i]->GetName(), a_sName, -1) == 0) {
return m_pUsers[i];
}
}
return TNULL;
}

TUserHandler& TUserHandler::GetSingleton()
{
return ms_oSingleton;
}

TSHORT TUser::Register()
{
return TUserHandler::GetSingleton().RegisterUser(*this);
}

void TUser::Deregister()
{
TUserHandler::GetSingleton().DeregisterUser(*this);
}

0 comments on commit ddc612a

Please sign in to comment.