Resource Fixes

This commit is contained in:
Curle 2023-08-16 19:43:53 +01:00
parent 49ee9fc10d
commit 9fd733e8f4
8 changed files with 134 additions and 8 deletions

View File

@ -1,13 +1,34 @@
#include <fs/file.h>
#include <vector>
#include "management/synchronization.h"
#include "../../../../../../cmake-build-debug/_deps/spdlog-src/include/spdlog/spdlog.h"
#include <spdlog/spdlog.h>
#include <filesystem>
#include <fs/path.h>
#include <map>
namespace ShadowEngine {
// Because fuck Linux? Need platform-specific source files!
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
FileInput::FileInput() {
handle = (void*) INVALID_HANDLE_VALUE;
}
FileOutput::FileOutput() {
error = false;
handle = (void*) INVALID_HANDLE_VALUE;
}
bool FileOutput::open(std::string& path) {
}
#endif
/**
* An async operation to be performed.
* For reading files from disk into memory.

View File

@ -75,7 +75,7 @@ namespace ShadowEngine {
// Create a Virtual Filesystem based on the given path.
static std::unique_ptr<FileSystem> createVFS(std::string& basePath);
virtual ~FileSystem() {}
virtual ~FileSystem() = default;
// Open a file for reading.
virtual bool open(std::string& path, FileInput& input) = 0;
@ -103,7 +103,7 @@ namespace ShadowEngine {
virtual bool hasWork() = 0;
// Write new content to a file synchronously. The thread will be blocked when doing this.
virtual bool saveSync(const Path& file, const uint8_t* content, const size_t size) = 0;
virtual bool saveSync(const Path& file, const uint8_t* content, size_t size) = 0;
// Read content from a file synchronously. The thread will be blocked when doing this.
virtual bool readSync(const Path& file, struct OutputMemoryStream& content) = 0;

View File

@ -1,4 +1,5 @@
#include <fs/iostream.h>
#include <cstring>
namespace ShadowEngine {
@ -13,15 +14,16 @@ namespace ShadowEngine {
str.free();
}
void OutputMemoryStream::operator=(ShadowEngine::OutputMemoryStream &&str) noexcept {
OutputMemoryStream& OutputMemoryStream::operator=(ShadowEngine::OutputMemoryStream &&str) noexcept {
capacity = str.capacity;
buffer = str.buffer;
usage = str.usage;
str.free();
return *this;
}
void OutputMemoryStream::operator=(const ShadowEngine::OutputMemoryStream &rhs) noexcept {
OutputMemoryStream& OutputMemoryStream::operator=(const ShadowEngine::OutputMemoryStream &rhs) noexcept {
usage = rhs.usage;
if (rhs.capacity > 0) {
@ -32,6 +34,8 @@ namespace ShadowEngine {
buffer = nullptr;
capacity = 0;
}
return *this;
}
OutputMemoryStream::OutputMemoryStream(const ShadowEngine::OutputMemoryStream &rhs) noexcept {

View File

@ -0,0 +1,73 @@
#include <management/synchronization.h>
// This doesn't work on Linux. Sucks to be you? dpeter won't let me do system-specific source files.
#ifdef _WIN32
#include <intrin.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
namespace ShadowEngine { struct NewPlaceholder {}; }
inline void* operator new(size_t, ShadowEngine::NewPlaceholder, void* where) { return where; }
inline void operator delete(void*, ShadowEngine::NewPlaceholder, void*) { }
namespace ShadowEngine {
Semaphore::Semaphore(int initCount, int maxCount) {
id = ::CreateSemaphore(nullptr, initCount, maxCount, nullptr);
}
Semaphore::~Semaphore() {
::CloseHandle(id);
}
void Semaphore::raise() {
::ReleaseSemaphore(id, 1, nullptr);
}
void Semaphore::wait() {
::WaitForSingleObject(id, INFINITE);
}
ConditionVariable::ConditionVariable() {
memset(data, 0, sizeof(data));
auto* var = new (NewPlaceholder(), data) CONDITION_VARIABLE;
InitializeConditionVariable(var);
}
ConditionVariable::~ConditionVariable() {
((CONDITION_VARIABLE*)data)->~CONDITION_VARIABLE();
}
void ConditionVariable::sleep(ShadowEngine::Mutex &mut) {
::SleepConditionVariableSRW((CONDITION_VARIABLE*) data, (SRWLOCK*) mut.data, INFINITE, 0);
}
void ConditionVariable::wake() {
::WakeConditionVariable((CONDITION_VARIABLE*) data);
}
Mutex::Mutex() {
memset(data, 0, sizeof(data));
auto* lock = new (NewPlaceholder(), data) SRWLOCK;
::InitializeSRWLock(lock);
}
Mutex::~Mutex() {
auto* lock = (SRWLOCK*) data;
lock->~SRWLOCK();
}
void Mutex::enter() {
auto* lock = (SRWLOCK*) data;
::AcquireSRWLockExclusive(lock);
}
void Mutex::exit() {
auto* lock = (SRWLOCK*) data;
::ReleaseSRWLockExclusive(lock);
}
}
#endif

View File

@ -18,7 +18,7 @@ namespace ShadowEngine {
void exit();
private:
#ifdef _WIN32
uint8_t data[8];
uint8_t data[8] {};
#else
pthread_mutex_t mutex;
#endif

View File

@ -174,4 +174,21 @@ namespace ShadowEngine {
checkState();
}
static std::string type("prefab");
const ResourceType PrefabResource::TYPE(type);
PrefabResource::PrefabResource(const ShadowEngine::Path &path,
ShadowEngine::ResourceTypeManager &resource_manager) : Resource(path, resource_manager) {}
ResourceType PrefabResource::getType() const { return TYPE; }
void PrefabResource::unload() { data.clear(); }
bool PrefabResource::load(size_t size, const uint8_t *mem) {
data.resize(size);
memcpy(data.dataMut(), mem, size);
hash = StableHash(mem, size);
return true;
}
}

View File

@ -23,7 +23,8 @@ namespace ShadowEngine {
};
// A Resource Type that is guaranteed to be invalid.
const ResourceType INVALID_RESOURCE((std::string &) "");
static std::string empty;
const ResourceType INVALID_RESOURCE(empty);
// A specialization of HashFunc for ResourceTypes, since they already have a HeapHash within.
template<> struct HashFunc<ResourceType> {
@ -125,4 +126,15 @@ namespace ShadowEngine {
State state;
bool hooked = false;
};
struct PrefabResource : Resource {
PrefabResource(const Path& path, ResourceTypeManager& resource_manager);
ResourceType getType() const override;
void unload() override;
bool load(size_t size, const uint8_t* data) override;
OutputMemoryStream data;
StableHash hash;
static const ResourceType TYPE;
};
}

View File

@ -191,5 +191,4 @@ namespace ShadowEngine {
for (auto manager : managers)
manager.second->reload(path);
}
}