chore: initial commit

This commit is contained in:
fallenoak 2023-01-02 13:17:18 -06:00
commit 70b00c5c38
No known key found for this signature in database
GPG key ID: 7628F8E61AEA070D
965 changed files with 264882 additions and 0 deletions

18
src/util/Autorelease.hpp Normal file
View file

@ -0,0 +1,18 @@
#ifndef UTIL_AUTORELEASE_HPP
#define UTIL_AUTORELEASE_HPP
#include <Foundation/Foundation.h>
namespace System_Autorelease {
class ScopedPool {
public:
// Member variables
NSAutoreleasePool* pool;
// Member functions
ScopedPool();
~ScopedPool();
};
}
#endif

9
src/util/Autorelease.mm Normal file
View file

@ -0,0 +1,9 @@
#include "util/Autorelease.hpp"
System_Autorelease::ScopedPool::ScopedPool() {
this->pool = [[NSAutoreleasePool alloc] init];
}
System_Autorelease::ScopedPool::~ScopedPool() {
[this->pool release];
}

327
src/util/BlizzardCore.cpp Normal file
View file

@ -0,0 +1,327 @@
#include "util/BlizzardCore.hpp"
#include <cstring>
#include <ctime>
#include <map>
#include <storm/Memory.hpp>
#if defined(WHOA_SYSTEM_MAC)
#include <libkern/OSAtomic.h>
#elif defined(WHOA_SYSTEM_LINUX)
#include <atomic>
#endif
bool Blizzard::System_Thread::s_initialized;
Blizzard::System_Debug::AssertCallback Blizzard::System_Debug::s_assertCallback = nullptr;
Blizzard::Lock::DoOnceData Blizzard::System_Lock::s_initMutexAttrOnce;
Blizzard::System_Lock::MutexAttr Blizzard::System_Lock::s_mutexattr;
Blizzard::Thread::ThreadRecord* Blizzard::System_Thread::s_mainThread;
Blizzard::Lock::Mutex Blizzard::System_Thread::s_mutex;
Blizzard::Lock::Mutex Blizzard::System_Thread::s_registryMutex;
Blizzard::Thread::TLSSlot Blizzard::System_Thread::s_stackTraceEntryPointTLS;
Blizzard::Thread::TLSSlot Blizzard::System_Thread::s_threadRecordTLS;
std::map<Blizzard::Thread::ThreadRecord*, Blizzard::Thread::ThreadRecord*>* Blizzard::System_Thread::s_threadRegistry;
Blizzard::Thread::TLSSlot* Blizzard::System_Thread::s_slotList[128];
int32_t Blizzard::System_Thread::s_slotListUsed;
void Blizzard::Debug::Assert(const char* a1, const char* a2, uint32_t a3) {
if (System_Debug::s_assertCallback) {
System_Debug::s_assertCallback(a1, "", a2, a3);
}
}
void Blizzard::Debug::SetAssertHandler(Blizzard::System_Debug::AssertCallback callback) {
System_Debug::s_assertCallback = callback;
}
void* Blizzard::Memory::Allocate(uint32_t bytes) {
return SMemAlloc(bytes, __FILE__, __LINE__, 0);
}
void* Blizzard::Memory::Allocate(uint32_t a1, uint32_t a2, const char* a3, uint32_t a4, const char* a5) {
// TODO
// - what is a5?
// - arg order?
// - flag manipluation?
return SMemAlloc(a1, a3, a4, a2);
}
void Blizzard::Memory::Free(void* ptr) {
SMemFree(ptr);
}
int32_t Blizzard::String::Copy(char* dst, const char* src, size_t len) {
if (!len || !dst) {
return 0;
}
if (!src) {
*dst = 0;
return 0;
}
char* v3 = dst + len - 1;
char v4;
const char* v5;
char* v6;
int32_t result;
if (dst < v3 && (v4 = *src, v5 = src, v6 = dst, *src)) {
do {
*v6++ = v4;
if (v3 <= v6) {
break;
}
v4 = (v5++)[1];
} while ( v4 );
result = v6 - dst;
} else {
v6 = dst;
result = 0;
}
*v6 = 0;
return result;
}
uint32_t Blizzard::String::Length(const char* a1) {
if (a1) {
return strlen(a1);
} else {
return 0;
}
}
void Blizzard::String::MemFill(void* a1, uint32_t a2, unsigned char a3) {
memset(a1, a3, a2);
}
void Blizzard::Process::Sleep(uint32_t duration) {
struct timespec request;
request.tv_sec = 0;
request.tv_nsec = duration * 1000000;
nanosleep(&request, nullptr);
}
void Blizzard::Lock::DoOnce(DoOnceData& a1, void (*a2)(void*), void* a3) {
if (!a1.done) {
if (Blizzard::Lock::Atomic::Increment(&a1.atomic) == 1) {
a2(a3);
a1.done = true;
} else {
while (!a1.done) {
Blizzard::Process::Sleep(1);
}
}
}
}
bool Blizzard::Lock::MutexCreate(Blizzard::Lock::Mutex& a1) {
Blizzard::Lock::DoOnce(System_Lock::s_initMutexAttrOnce, System_Lock::InitAttr, 0);
bool result = pthread_mutex_init(&a1, &System_Lock::s_mutexattr);
// Blizzard::Debug::Assert(result == 0);
return result;
}
bool Blizzard::Lock::MutexEnter(Blizzard::Lock::Mutex& a1) {
bool result = pthread_mutex_lock(&a1);
// Blizzard::Debug::Assert(result == 0);
return result;
}
bool Blizzard::Lock::MutexLeave(Blizzard::Lock::Mutex& a1) {
bool result = pthread_mutex_unlock(&a1);
// Blizzard::Debug::Assert(result == 0);
return result;
}
int32_t Blizzard::Lock::Atomic::Increment(volatile int32_t* value) {
#if defined(WHOA_SYSTEM_MAC)
return OSAtomicAdd32(1, value);
#elif defined(WHOA_SYSTEM_LINUX)
volatile std::atomic<int32_t> atom(*value);
std::atomic_fetch_add<int32_t>(&atom, 1);
return atom;
#endif
}
void Blizzard::System_Lock::InitAttr(void* a1) {
pthread_mutexattr_init(&System_Lock::s_mutexattr);
pthread_mutexattr_settype(&System_Lock::s_mutexattr, PTHREAD_MUTEX_RECURSIVE);
}
void Blizzard::Thread::AllocateLocalStorage(TLSSlot* slot) {
System_Thread::AllocateLocalStorage(slot, 0);
}
void* Blizzard::Thread::RegisterLocalStorage(TLSSlot* slot, void* (*constructor)(void*), void* userData, void (*destructor)(void*)) {
if (!System_Thread::TLSSlotIsAllocated(slot) && !System_Thread::AllocateTLSSlot(slot, destructor)) {
// Blizzard::Debug::Assert(!"Unable to allocate thread-local storage");
}
void* value;
value = System_Thread::InternalGetLocalStorage(slot);
if (value) {
return value;
}
value = constructor(userData);
System_Thread::InternalSetLocalStorage(slot, value);
return value;
}
void Blizzard::Thread::SetLocalStorage(const TLSSlot* slot, const void* value) {
BLIZZARD_ASSERT(Blizzard::Thread::TLSSlotIsAllocated(slot));
System_Thread::InternalSetLocalStorage(slot, value);
}
bool Blizzard::Thread::TLSSlotIsAllocated(const TLSSlot* slot) {
return System_Thread::TLSSlotIsAllocated(slot);
}
void Blizzard::System_Thread::AddToRegistry(Thread::ThreadRecord* thread) {
Blizzard::Lock::MutexEnter(System_Thread::s_registryMutex);
if (!System_Thread::s_threadRegistry) {
void* v2 = Blizzard::Memory::Allocate(sizeof(std::map<Thread::ThreadRecord*, Thread::ThreadRecord*>), 0, __FILE__, __LINE__, nullptr);
System_Thread::s_threadRegistry = new (v2) std::map<Thread::ThreadRecord*, Thread::ThreadRecord*>();
}
System_Thread::s_threadRegistry->insert(std::pair<Thread::ThreadRecord*, Thread::ThreadRecord*>(thread, thread));
Blizzard::Lock::MutexLeave(System_Thread::s_registryMutex);
}
bool Blizzard::System_Thread::AllocateLocalStorage(Thread::TLSSlot* slot, void (*destructor)(void*)) {
System_Thread::InitThreadSystem();
BLIZZARD_ASSERT(!System_Thread::TLSSlotIsAllocated(slot));
int32_t result;
if (System_Thread::InternalAllocateLocalStorage(slot, destructor)) {
slot->destructor = destructor;
Blizzard::Lock::MutexEnter(System_Thread::s_mutex);
int32_t v3 = System_Thread::s_slotListUsed;
System_Thread::s_slotList[System_Thread::s_slotListUsed] = slot;
System_Thread::s_slotListUsed = v3 + 1;
Blizzard::Lock::MutexLeave(System_Thread::s_mutex);
result = 1;
} else {
BLIZZARD_ASSERT(!"failed to allocate TLS");
result = 0;
}
return result;
}
bool Blizzard::System_Thread::AllocateTLSSlot(Thread::TLSSlot* slot, void (*destructor)(void*)) {
System_Thread::InitThreadSystem();
Blizzard::Lock::MutexEnter(System_Thread::s_mutex);
int32_t result = 0;
if (System_Thread::TLSSlotIsAllocated(slot) || System_Thread::AllocateLocalStorage(slot, destructor)) {
result = 1;
}
Blizzard::Lock::MutexLeave(System_Thread::s_mutex);
return result;
}
void Blizzard::System_Thread::InitThreadSystem() {
if (System_Thread::s_initialized) {
return;
}
System_Thread::s_initialized = true;
Blizzard::Lock::MutexCreate(System_Thread::s_mutex);
Blizzard::Lock::MutexCreate(System_Thread::s_registryMutex);
Blizzard::Thread::AllocateLocalStorage(&System_Thread::s_threadRecordTLS);
Blizzard::Thread::AllocateLocalStorage(&System_Thread::s_stackTraceEntryPointTLS);
System_Thread::s_mainThread = System_Thread::NewThread(nullptr, nullptr, nullptr);
Blizzard::Thread::SetLocalStorage(&System_Thread::s_threadRecordTLS, System_Thread::s_mainThread);
System_Thread::s_mainThread->unkC = 1;
System_Thread::s_mainThread->unk10 = 1;
}
bool Blizzard::System_Thread::InternalAllocateLocalStorage(Thread::TLSSlot* slot, void (*destructor)(void*)) {
if (!pthread_key_create(&slot->key, destructor)) {
slot->allocated = true;
return 1;
} else {
return 0;
}
}
void* Blizzard::System_Thread::InternalGetLocalStorage(const Thread::TLSSlot* slot) {
return pthread_getspecific(slot->key);
}
void Blizzard::System_Thread::InternalSetLocalStorage(const Thread::TLSSlot* slot, const void* value) {
int32_t err = pthread_setspecific(slot->key, value);
BLIZZARD_ASSERT(err == 0);
}
Blizzard::Thread::ThreadRecord* Blizzard::System_Thread::NewThread(uint32_t (*a1)(void*), void* a2, const char* a3) {
const char* v3 = a3;
if (!a3) {
v3 = "";
}
int32_t v4 = Blizzard::String::Length(v3);
Blizzard::Thread::ThreadRecord* thread = static_cast<Blizzard::Thread::ThreadRecord*>(
Blizzard::Memory::Allocate(sizeof(Blizzard::Thread::ThreadRecord) + v4 + 1, 0, __FILE__, __LINE__, nullptr)
);
Blizzard::String::MemFill(thread, sizeof(Blizzard::Thread::ThreadRecord), 0);
thread->unk4 = a2;
thread->unk8 = a1;
thread->unkC = 0;
thread->unk10 = 2;
Blizzard::String::Copy(thread->unk2C, v3, v4 + 1);
System_Thread::AddToRegistry(thread);
return thread;
}
bool Blizzard::System_Thread::TLSSlotIsAllocated(const Thread::TLSSlot* slot) {
return slot->allocated;
}

132
src/util/BlizzardCore.hpp Normal file
View file

@ -0,0 +1,132 @@
#ifndef UTIL_BLIZZARD_CORE_HPP
#define UTIL_BLIZZARD_CORE_HPP
#include <cstdint>
#include <map>
#if defined(WHOA_SYSTEM_MAC) || defined(WHOA_SYSTEM_LINUX)
#include <pthread.h>
#endif
#if defined(NDEBUG)
#define BLIZZARD_ASSERT(x) if (!(x)) { return 0; }
#else
#define BLIZZARD_ASSERT(x) if (!(x)) { Blizzard::Debug::Assert(#x, __FILE__, __LINE__); }
#endif
namespace Blizzard {
namespace Memory {
// Functions
void* Allocate(uint32_t);
void* Allocate(uint32_t, uint32_t, const char*, uint32_t, const char*);
void Free(void*);
};
namespace String {
// Functions
int32_t Copy(char*, const char*, size_t);
uint32_t Length(const char*);
void MemFill(void*, uint32_t, unsigned char);
};
namespace Process {
// Functions
void Sleep(uint32_t);
}
namespace Lock {
// Types
typedef pthread_mutex_t Mutex;
struct DoOnceData {
bool done;
int32_t atomic;
};
// Functions
void DoOnce(DoOnceData&, void (*)(void*), void*);
bool MutexCreate(Mutex&);
bool MutexEnter(Mutex&);
bool MutexLeave(Mutex&);
namespace Atomic {
int32_t Increment(volatile int32_t*);
}
}
namespace System_Debug {
// Types
typedef void (*AssertCallback)(const char*, const char*, const char*, uint32_t);
// Variables
extern AssertCallback s_assertCallback;
}
namespace Debug {
// Functions
void Assert(const char*, const char*, uint32_t);
void SetAssertHandler(Blizzard::System_Debug::AssertCallback);
}
namespace System_Lock {
// Types
typedef pthread_mutexattr_t MutexAttr;
// Variables
extern Lock::DoOnceData s_initMutexAttrOnce;
extern MutexAttr s_mutexattr;
// Functions
void InitAttr(void*);
}
namespace Thread {
// Types
struct ThreadRecord {
pthread_t unk0;
void* unk4;
uint32_t (*unk8)(void*);
int32_t unkC;
int32_t unk10;
char* unk2C;
};
struct TLSSlot {
pthread_key_t key;
void (*destructor)(void*);
bool allocated;
};
// Functions
void AllocateLocalStorage(TLSSlot*);
void* RegisterLocalStorage(TLSSlot*, void* (*)(void*), void*, void (*)(void*));
void SetLocalStorage(const TLSSlot*, const void*);
bool TLSSlotIsAllocated(const TLSSlot*);
};
namespace System_Thread {
// Variables
extern bool s_initialized;
extern Thread::ThreadRecord* s_mainThread;
extern Lock::Mutex s_mutex;
extern Lock::Mutex s_registryMutex;
extern Thread::TLSSlot s_stackTraceEntryPointTLS;
extern Thread::TLSSlot s_threadRecordTLS;
extern std::map<Thread::ThreadRecord*, Thread::ThreadRecord*>* s_threadRegistry;
extern Thread::TLSSlot* s_slotList[128];
extern int32_t s_slotListUsed;
// Functions
void AddToRegistry(Thread::ThreadRecord*);
bool AllocateLocalStorage(Thread::TLSSlot*, void (*)(void*));
bool AllocateTLSSlot(Thread::TLSSlot*, void (*)(void*));
void InitThreadSystem(void);
bool InternalAllocateLocalStorage(Thread::TLSSlot*, void (*)(void*));
void* InternalGetLocalStorage(const Thread::TLSSlot*);
void InternalSetLocalStorage(const Thread::TLSSlot*, const void*);
Thread::ThreadRecord* NewThread(uint32_t (*)(void*), void*, const char*);
bool TLSSlotIsAllocated(const Thread::TLSSlot*);
};
};
#endif

17
src/util/Byte.hpp Normal file
View file

@ -0,0 +1,17 @@
#ifndef UTIL_BYTE_HPP
#define UTIL_BYTE_HPP
#include <cstdint>
#define BYTEn(x, n) (*((uint8_t*)&(x)+n))
#define BYTE1(x) BYTEn(x, 1)
#define BYTE2(x) BYTEn(x, 2)
#define BYTE3(x) BYTEn(x, 3)
#define BYTE4(x) BYTEn(x, 4)
#define LOBYTE(x) (*((uint8_t*)&(x)))
#define HIBYTE(x) (*((uint8_t*)&(x)+1))
#define LOWORD(x) (*((uint16_t*)&(x)))
#define HIWORD(x) (*((uint16_t*)&(x)+1))
#endif

36
src/util/CMakeLists.txt Normal file
View file

@ -0,0 +1,36 @@
file(GLOB PRIVATE_SOURCES "*.cpp")
if(WHOA_SYSTEM_MAC)
file(GLOB MAC_SOURCES "*.mm")
set_source_files_properties(${MAC_SOURCES}
PROPERTIES COMPILE_FLAGS "-x objective-c++"
)
list(APPEND PRIVATE_SOURCES ${MAC_SOURCES})
endif()
add_library(util STATIC
${PRIVATE_SOURCES}
)
target_include_directories(util
PRIVATE
${CMAKE_SOURCE_DIR}/src
)
target_link_libraries(util
PUBLIC
freetype-2.0
lua-5.1
common
storm
tempest
)
if(WHOA_SYSTEM_LINUX OR WHOA_SYSTEM_MAC)
target_link_libraries(util
PRIVATE
Threads::Threads
)
endif()

23
src/util/CStatus.cpp Normal file
View file

@ -0,0 +1,23 @@
#include "util/CStatus.hpp"
#include <cstdarg>
#include <cstdio>
CStatus CStatus::s_errorList;
void CStatus::Add(const CStatus& status) {
// TODO
}
void CStatus::Add(STATUS_TYPE severity, const char* format, ...) {
// TODO
// Remove temporary console debug logging
va_list args;
va_start(args, format);
vprintf(format, args);
printf("\n");
va_end(args);
}
CStatus& GetGlobalStatusObj() {
return CStatus::s_errorList;
}

29
src/util/CStatus.hpp Normal file
View file

@ -0,0 +1,29 @@
#ifndef UTIL_C_STATUS_HPP
#define UTIL_C_STATUS_HPP
enum STATUS_TYPE {
STATUS_INFO = 0x0,
STATUS_WARNING = 0x1,
STATUS_ERROR = 0x2,
STATUS_FATAL = 0x3,
STATUS_NUMTYPES = 0x4,
};
class CStatus {
public:
// Static variables
static CStatus s_errorList;
// Member functions
void Add(const CStatus&);
void Add(STATUS_TYPE, const char*, ...);
};
class CWOWClientStatus : public CStatus {
public:
void* m_logFile;
};
CStatus& GetGlobalStatusObj(void);
#endif

163
src/util/CVar.cpp Normal file
View file

@ -0,0 +1,163 @@
#include "util/CVar.hpp"
#include <storm/String.hpp>
bool CVar::m_needsSave;
TSHashTable<CVar, HASHKEY_STRI> CVar::s_registeredCVars;
CVar* CVar::Lookup(const char* name) {
return name
? CVar::s_registeredCVars.Ptr(name)
: nullptr;
}
CVar* CVar::Register(const char* name, const char* help, uint32_t flags, const char* value, bool (*fcn)(CVar*, const char*, const char*, void*), uint32_t category, bool a7, void* arg, bool a9) {
CVar* var = CVar::s_registeredCVars.Ptr(name);
if (var) {
bool setReset = var->m_resetValue.GetString() == nullptr;
bool setDefault = var->m_defaultValue.GetString() == nullptr;
var->m_flags |= (var->m_flags & 0xFFFFFFCF);
var->m_callback = fcn;
var->m_arg = arg;
bool setValue = false;
if (fcn && !fcn(var, var->GetString(), var->GetString(), arg)) {
setValue = true;
}
var->Set(value, setValue, setReset, setDefault, false);
if (!a7) {
var->m_flags |= 0x80000000;
}
if (a9 && var->m_flags) {
var->m_flags |= 0x80;
}
} else {
var = CVar::s_registeredCVars.New(name, 0, 0);
var->m_stringValue.Copy(nullptr);
var->m_floatValue = 0.0f;
var->m_intValue = 0;
var->m_modified = 0;
var->m_category = category;
var->m_defaultValue.Copy(nullptr);
var->m_resetValue.Copy(nullptr);
var->m_latchedValue.Copy(nullptr);
var->m_callback = fcn;
var->m_flags = 0;
var->m_arg = arg;
var->m_help.Copy(help);
if (a7) {
var->Set(value, true, true, false, false);
} else {
var->Set(value, true, false, true, false);
}
var->m_flags = flags | 0x1;
if (!a7) {
var->m_flags |= 0x8000000;
}
if (a9 && var->m_flags) {
var->m_flags |= 0x80;
}
// TODO
// ConsoleCommandRegister(var->m_key.GetString(), &CvarCommandHandler, category, help);
}
return var;
}
CVar::CVar() : TSHashObject<CVar, HASHKEY_STRI>() {
// TODO
}
int32_t CVar::GetInt() {
return this->m_intValue;
}
const char* CVar::GetString() {
return this->m_stringValue.GetString();
}
void CVar::InternalSet(const char* value, bool setValue, bool setReset, bool setDefault, bool a6) {
if (this->m_flags & 0x4 || !value) {
return;
}
bool modified = false;
const char* existingValue = this->m_stringValue.GetString();
if (setValue && (!existingValue || SStrCmpI(value, existingValue, 0x7FFFFFFF))) {
modified = true;
this->m_stringValue.Copy(value);
this->m_intValue = SStrToInt(value);
this->m_floatValue = SStrToFloat(value);
}
if (setReset && !this->m_resetValue.GetString()) {
modified = true;
this->m_resetValue.Copy(value);
}
if (setDefault && !this->m_defaultValue.GetString()) {
this->m_defaultValue.Copy(value);
} else if (!modified) {
return;
}
if (a6) {
CVar::m_needsSave = 1;
}
}
bool CVar::Set(const char* value, bool setValue, bool setReset, bool setDefault, bool a6) {
if (setValue) {
if (this->m_callback) {
// TODO
// sub_86B5A0(this->m_callback);
if (!this->m_callback(this, this->m_stringValue.GetString(), value, this->m_arg)) {
return true;
}
}
this->m_modified++;
if (this->m_flags & 0x2) {
this->m_latchedValue.Copy(value);
CVar::m_needsSave = 1;
return true;
}
}
this->InternalSet(value, setValue, setReset, setDefault, a6);
return true;
}
int32_t CVar::Update() {
if (!(this->m_flags & 0x2)) {
return 0;
}
if (!this->m_latchedValue.GetString()) {
return 0;
}
this->InternalSet(this->m_latchedValue.GetString(), true, false, false, true);
this->m_latchedValue.Copy(nullptr);
return 1;
}

41
src/util/CVar.hpp Normal file
View file

@ -0,0 +1,41 @@
#ifndef UTIL_C_VAR_HPP
#define UTIL_C_VAR_HPP
#include <cstdint>
#include <common/String.hpp>
#include <storm/Hash.hpp>
class CVar : public TSHashObject<CVar, HASHKEY_STRI> {
public:
// Static variables
static TSHashTable<CVar, HASHKEY_STRI> s_registeredCVars;
static bool m_needsSave;
// Static functions
static CVar* Lookup(const char* name);
static CVar* Register(const char*, const char*, uint32_t, const char*, bool (*)(CVar*, const char*, const char*, void*), uint32_t, bool, void*, bool);
// Member variables
uint32_t m_category = 0;
uint32_t m_flags = 0;
RCString m_stringValue;
float m_floatValue = 0.0;
int32_t m_intValue = 0;
int32_t m_modified = 0;
RCString m_defaultValue;
RCString m_resetValue;
RCString m_latchedValue;
RCString m_help;
bool (*m_callback)(CVar*, const char*, const char*, void*) = nullptr;
void* m_arg = nullptr;
// Member functions
CVar();
int32_t GetInt();
const char* GetString(void);
void InternalSet(const char*, bool, bool, bool, bool);
bool Set(const char*, bool, bool, bool, bool);
int32_t Update();
};
#endif

30
src/util/Filesystem.cpp Normal file
View file

@ -0,0 +1,30 @@
#include "util/Filesystem.hpp"
#include <cstring>
#include <storm/String.hpp>
void OsCreateDirectory(const char* pathName, int32_t recursive) {
// TODO
}
void OsBuildFontFilePath(const char* fileName, char* buffer, size_t size) {
SStrPrintf(buffer, size, "%s\\%s", "Fonts", fileName);
}
char* OsPathFindExtensionWithDot(char* pathName) {
char* v1;
char* result;
v1 = strrchr(pathName, '\\');
if (!v1) {
v1 = strrchr(pathName, '/');
}
result = strrchr(pathName, '.');
if (!result || (v1 && v1 >= result)) {
result = (char*)&pathName[strlen(pathName)];
}
return result;
}

13
src/util/Filesystem.hpp Normal file
View file

@ -0,0 +1,13 @@
#ifndef UTIL_FILESYSTEM_HPP
#define UTIL_FILESYSTEM_HPP
#include <cstddef>
#include <cstdint>
void OsCreateDirectory(const char*, int32_t);
void OsBuildFontFilePath(const char*, char*, size_t);
char* OsPathFindExtensionWithDot(char*);
#endif

11
src/util/Log.cpp Normal file
View file

@ -0,0 +1,11 @@
#include "util/Log.hpp"
bool SLogCreate(const char* filename, uint32_t flags, void* log) {
// TODO
return true;
}
void SysMsgPrintf(SYSMSG_TYPE severity, const char* format, ...) {
// TODO
}

18
src/util/Log.hpp Normal file
View file

@ -0,0 +1,18 @@
#ifndef UTIL_LOG_HPP
#define UTIL_LOG_HPP
#include <cstdint>
enum SYSMSG_TYPE {
SYSMSG_INFO = 0x0,
SYSMSG_WARNING = 0x1,
SYSMSG_ERROR = 0x2,
SYSMSG_FATAL = 0x3,
SYSMSG_NUMTYPES = 0x4
};
bool SLogCreate(const char*, uint32_t, void*);
void SysMsgPrintf(SYSMSG_TYPE, const char*, ...);
#endif

10
src/util/Lua.hpp Normal file
View file

@ -0,0 +1,10 @@
#ifndef UTIL_LUA_HPP
#define UTIL_LUA_HPP
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#endif

125
src/util/SFile.cpp Normal file
View file

@ -0,0 +1,125 @@
#include "util/SFile.hpp"
#include <cstring>
#include <limits>
#include <storm/Memory.hpp>
// TODO Proper implementation
int32_t SFile::Close(SFile* file) {
delete file->m_filename;
file->m_stream->close();
delete file->m_stream;
delete file;
return 1;
}
// TODO Proper implementation
size_t SFile::GetFileSize(SFile* file, size_t* filesizeHigh) {
return file->m_size;
}
int32_t SFile::IsStreamingMode() {
// TODO
return 0;
}
// TODO Proper implementation
int32_t SFile::Load(SArchive* archive, const char* filename, void** buffer, size_t* bytes, size_t extraBytes, uint32_t flags, SOVERLAPPED* overlapped) {
size_t pathlen = strlen(filename);
char path[pathlen];
strcpy(path, filename);
for (int32_t i = 0; i < pathlen; ++i) {
if (path[i] == '\\') {
path[i] = '/';
}
}
std::ifstream file (path, std::ios::in | std::ios::binary | std::ios::ate);
size_t size;
char* data;
if (file.is_open()) {
size = static_cast<size_t>(file.tellg());
if (bytes) {
*bytes = size;
}
data = new char[size + extraBytes];
file.seekg(0, std::ios::beg);
file.read(data, size);
file.close();
if (extraBytes) {
memset(data + size, 0, extraBytes);
}
*buffer = data;
return 1;
} else {
return 0;
}
}
int32_t SFile::Open(const char* filename, SFile** file) {
return SFile::OpenEx(nullptr, filename, 0, file);
}
// TODO Proper implementation
int32_t SFile::OpenEx(SArchive* archive, const char* filename, uint32_t flags, SFile** file) {
size_t pathlen = strlen(filename);
char path[pathlen];
strcpy(path, filename);
for (int32_t i = 0; i < pathlen; ++i) {
if (path[i] == '\\') {
path[i] = '/';
}
}
SFile* fileptr = new SFile;
fileptr->m_filename = strdup(filename);
std::ifstream* stream = new std::ifstream(path, std::ios::in | std::ios::binary | std::ios::ate);
if (!stream->is_open()) {
return 0;
}
stream->seekg(0, std::ios::beg);
stream->ignore(std::numeric_limits<std::streamsize>::max());
std::streamsize size = stream->gcount();
stream->clear();
stream->seekg(0, std::ios::beg);
fileptr->m_stream = stream;
fileptr->m_size = size;
*file = fileptr;
return 1;
}
// TODO Proper implementation
int32_t SFile::Read(SFile* file, void* buffer, size_t bytestoread, size_t* bytesread, SOVERLAPPED* overlapped, TASYNCPARAMBLOCK* asyncparam) {
file->m_stream->read((char*)buffer, bytestoread);
if (bytesread) {
*bytesread = file->m_stream->gcount();
}
return 1;
}
int32_t SFile::Unload(void* ptr) {
SMemFree(ptr, __FILE__, __LINE__, 0);
return 1;
}

31
src/util/SFile.hpp Normal file
View file

@ -0,0 +1,31 @@
#ifndef UTIL_S_FILE_HPP
#define UTIL_S_FILE_HPP
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <iostream>
class SArchive;
struct SOVERLAPPED;
struct TASYNCPARAMBLOCK;
class SFile {
public:
// Static functions
static int32_t Close(SFile*);
static size_t GetFileSize(SFile*, size_t*);
static int32_t IsStreamingMode(void);
static int32_t Load(SArchive*, const char*, void**, size_t*, size_t, uint32_t, SOVERLAPPED*);
static int32_t Open(const char*, SFile**);
static int32_t OpenEx(SArchive*, const char*, uint32_t, SFile**);
static int32_t Read(SFile*, void*, size_t, size_t*, SOVERLAPPED*, TASYNCPARAMBLOCK*);
static int32_t Unload(void*);
// Member variables
const char* m_filename;
std::ifstream* m_stream; // TODO Proper implementation
std::streamsize m_size; // TODO Proper implementation
};
#endif

161
src/util/StringTo.cpp Normal file
View file

@ -0,0 +1,161 @@
#include "util/StringTo.hpp"
#include "util/Lua.hpp"
#include <storm/String.hpp>
uint64_t StringToClickAction(const char* actionStr) {
if (!actionStr || !*actionStr) {
return 0;
}
if (!SStrCmpI(actionStr, "LeftButtonDown", STORM_MAX_STR)) {
return 1;
}
if (!SStrCmpI(actionStr, "LeftButtonUp", STORM_MAX_STR)) {
return 0x80000000;
}
if (!SStrCmpI(actionStr, "MiddleButtonDown", STORM_MAX_STR)) {
return 2;
}
if (!SStrCmpI(actionStr, "MiddleButtonUp", STORM_MAX_STR)) {
return 0;
}
if (!SStrCmpI(actionStr, "RightButtonDown", STORM_MAX_STR)) {
return 4;
}
if (!SStrCmpI(actionStr, "RightButtonUp", STORM_MAX_STR)) {
return 0;
}
// TODO remaining buttons
return 0;
}
int32_t StringToBOOL(const char* str) {
return StringToBOOL(str, 0);
}
bool StringToBOOL(const char* str, int32_t def) {
if (!str) {
return def;
}
switch (*str) {
case '0':
case 'F':
case 'N':
case 'f':
case 'n':
return false;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case 'T':
case 'Y':
case 't':
case 'y':
return true;
default:
if (!SStrCmpI(str, "off", 0x7FFFFFFFu) || !SStrCmpI(str, "disabled", 0x7FFFFFFFu)) {
return false;
}
if (!SStrCmpI(str, "on", 0x7FFFFFFFu) || !SStrCmpI(str, "enabled", 0x7FFFFFFFu)) {
return true;
}
return def;
}
}
bool StringToBOOL(lua_State* L, int32_t idx, int32_t def) {
bool result;
const char* str;
switch (lua_type(L, idx)) {
case LUA_TNIL:
result = false;
break;
case LUA_TBOOLEAN:
result = lua_toboolean(L, idx);
break;
case LUA_TNUMBER:
result = lua_tonumber(L, idx) != 0;
break;
case LUA_TSTRING:
str = lua_tolstring(L, idx, 0);
result = StringToBOOL(str, def);
break;
default:
result = def;
break;
}
return result;
}
int32_t StringToDrawLayer(const char* string, int32_t& layer) {
struct drawlayer {
int32_t layer;
const char* string;
};
static drawlayer array_drawlayer[5] = {
{ 0, "BACKGROUND" },
{ 1, "BORDER" },
{ 2, "ARTWORK" },
{ 3, "OVERLAY" },
{ 4, "HIGHLIGHT" }
};
for (int32_t i = 0; i < 5; i++) {
if (!SStrCmpI(array_drawlayer[i].string, string, 0x7FFFFFFFu)) {
layer = array_drawlayer[i].layer;
return 1;
}
}
return 0;
}
int32_t StringToJustify(const char* string, uint32_t& justify) {
struct JustifyEntry {
uint32_t value;
const char* string;
};
static JustifyEntry justifyMap[6] = {
{ 0x1, "LEFT" },
{ 0x2, "CENTER" },
{ 0x4, "RIGHT" },
{ 0x8, "TOP" },
{ 0x10, "MIDDLE" },
{ 0x20, "BOTTOM" }
};
for (int32_t i = 0; i < 5; i++) {
if (!SStrCmpI(justifyMap[i].string, string, 0x7FFFFFFFu)) {
justify = justifyMap[i].value;
return 1;
}
}
return 0;
}

20
src/util/StringTo.hpp Normal file
View file

@ -0,0 +1,20 @@
#ifndef UTIL_STRING_TO_HPP
#define UTIL_STRING_TO_HPP
#include <cstdint>
struct lua_State;
uint64_t StringToClickAction(const char* actionStr);
int32_t StringToBOOL(const char*);
bool StringToBOOL(const char*, int32_t);
bool StringToBOOL(lua_State*, int32_t, int32_t);
int32_t StringToDrawLayer(const char*, int32_t&);
int32_t StringToJustify(const char*, uint32_t&);
#endif

View file

@ -0,0 +1,16 @@
#ifndef UTIL_UNIMPLEMENTED_HPP
#define UTIL_UNIMPLEMENTED_HPP
#include <cstdio>
#define WHOA_UNIMPLEMENTED() \
fprintf(stderr, "Function not yet implemented: %s in %s (line %i)\n", __FUNCTION__, __FILE__, __LINE__); \
return 0; \
void(0)
#define WHOA_UNIMPLEMENTED_VOID() \
fprintf(stderr, "Function not yet implemented: %s in %s (line %i)\n", __FUNCTION__, __FILE__, __LINE__); \
return; \
void(0)
#endif