Skip to content

Commit

Permalink
Move asset management, cleanup other files
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCurle committed Apr 7, 2024
1 parent 957b62f commit 231f990
Show file tree
Hide file tree
Showing 33 changed files with 976 additions and 868 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#pragma once
#include <fs/iostream.h>
#include <fs/path.h>
#include <management/delegate.h>
#include <memory>
#include "iostream.h"
#include "path.h"

template <class T> struct Delegate;

Expand All @@ -13,7 +12,7 @@ namespace ShadowEngine {
FileInput();
~FileInput() = default;

[[nodiscard]] bool open(std::string& path);
[[nodiscard]] bool open(const std::string& path);
void close();

using InputStream::read;
Expand All @@ -34,7 +33,7 @@ namespace ShadowEngine {
FileOutput();
~FileOutput() = default;

[[nodiscard]] bool open(std::string& path);
[[nodiscard]] bool open(const std::string& path);
void close();
void flush();
bool errored() const { return error; }
Expand Down Expand Up @@ -71,31 +70,31 @@ namespace ShadowEngine {
};

// Create a Filesystem that interacts with files on disk.
static std::unique_ptr<FileSystem> createDiskFS(std::string& basePath);
static std::unique_ptr<FileSystem> createDiskFS(const std::string& basePath);
// Create a Virtual Filesystem based on the given path.
static std::unique_ptr<FileSystem> createVFS(std::string& basePath);
static std::unique_ptr<FileSystem> createVFS(const std::string& basePath);

virtual ~FileSystem() = default;

// Open a file for reading.
virtual bool open(std::string& path, FileInput& input) = 0;
virtual bool open(const std::string& path, FileInput& input) = 0;
// Open a file for writing.
virtual bool open(std::string& path, FileOutput& output) = 0;
virtual bool open(const std::string& path, FileOutput& output) = 0;
// Check whether a file exists at the given path.
virtual bool fileExists(std::string& path) = 0;
virtual bool fileExists(const std::string& path) = 0;
// Get the time a file at the given path was last modified.
virtual size_t getLastModified(std::string& path) = 0;
virtual size_t getLastModified(const std::string& path) = 0;
// Copy a file from one path to another.
virtual bool copyFile(std::string& from, std::string& to) = 0;
virtual bool copyFile(const std::string& from, const std::string& to) = 0;
// Move a file from one path to another.
virtual bool moveFile(std::string& from, std::string& to) = 0;
virtual bool moveFile(const std::string& from, const std::string& to) = 0;
// Disassociate any files at the given path (not an immediate delete)
virtual bool deleteFile(std::string& path) = 0;
virtual bool deleteFile(const std::string& path) = 0;

// Get the path that this FileSystem originates at. The default is "/" for VFS, and whatever the Executable Path is for Disk FS.
virtual std::string const& getBasePath() const = 0;
// Set a new base path for the FileSystem. Any operations involving file paths will be relative to this new path.
virtual void setBasePath(std::string& path) = 0;
virtual void setBasePath(const std::string& path) = 0;

// Process all the callbacks for async file operations.
virtual void processCallbacks() = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace ShadowEngine {

HeapHash() = default;
// Hash a string; for paths and such.
explicit HeapHash(std::string& str);
explicit HeapHash(const std::string& str);
// Hash arbitrary data.
HeapHash(const void* data, uint32_t length);

Expand All @@ -39,7 +39,7 @@ namespace ShadowEngine {

HeapHash32() = default;
// Hash a string; for paths and such.
explicit HeapHash32(std::string& str);
explicit HeapHash32(const std::string& str);
// Hash arbitrary data.
HeapHash32(const void* data, uint32_t length);

Expand All @@ -59,7 +59,7 @@ namespace ShadowEngine {
struct StableHash {
static StableHash fromLong(size_t data);
StableHash() = default;
explicit StableHash(std::string& str);
explicit StableHash(const std::string& str);
StableHash(const void* data, uint32_t length);

bool operator!= (const StableHash& other) const { return hash != other.hash; }
Expand All @@ -80,7 +80,7 @@ namespace ShadowEngine {
struct StableHash32 {
static StableHash32 fromInt(uint32_t data);
StableHash32() = default;
StableHash32(std::string& str);
StableHash32(const std::string& str);
StableHash32(const void* data, uint32_t length);

bool operator!= (StableHash32& other) const { return hash != other.hash; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#pragma once

#include <string>
#include <fs/hash.h>
#include "hash.h"

namespace ShadowEngine {
/**
* Stores split data about a path, for easy referencing and decomposition.
* Not to be used as a replacement for the Path class.
*/
struct PathInfo {
explicit PathInfo(std::string& str);
explicit PathInfo(const std::string& str);

char extension[10];
char baseName[256];
Expand All @@ -28,21 +28,21 @@ namespace ShadowEngine {
// Make sure the path is valid.
// Always from the root.
// One slash separating.
static std::string normalise(std::string& path);
static std::string normalise(const std::string& path);
// Get the prelude of the given path.
static std::string getPrelude(std::string& path);
static std::string getPrelude(const std::string& path);
// Get the domain of the given path.
static std::string getDomain(std::string& path);
static std::string getDomain(const std::string& path);
// Get the directory of the given path.
static std::string getDirectory(std::string& path);
static std::string getDirectory(const std::string& path);
// Get the name of the file of the given path.
static std::string getFilename(std::string& path);
static std::string getFilename(const std::string& path);
// Get the file extension of the given path.
static std::string getExtension(std::string& path);
static std::string getExtension(const std::string& path);
// Check if the path has the given extension.
static bool hasExtension(std::string& path, std::string& ext);
static bool hasExtension(const std::string& path, const std::string& ext);
// Replace the extension of the given path.
static std::string replaceExtension(std::string& path, std::string& newExt);
static std::string replaceExtension(const std::string& path, const std::string& newExt);

Path();
explicit Path(const std::string& str);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#pragma once

#include "shadow/assets/fs/hash.h"
#include "shadow/assets/fs/path.h"
#include "shadow/assets/fs/file.h"
#include <shadow/util/DelegateList.h>

namespace ShadowEngine {

/**
* A runtime-only struct that determines the type of a resource - whether it be a texture, mesh, animation, or other data.
* Provides some specializations for living in a map.
*/
struct ResourceType {
ResourceType() = default;
explicit ResourceType(const std::string& name);
bool operator!=(const ResourceType& o) const { return o.hash != hash; }
bool operator==(const ResourceType& o) const { return o.hash == hash; }
bool operator< (const ResourceType& o) const { return o.hash.getHash() < hash.getHash(); }
bool isValid() const { return hash.getHash() != 0; }

HeapHash hash;
};

// A Resource Type that is guaranteed to be invalid.
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> {
static uint32_t get(const ResourceType& key) { return HashFunc<HeapHash>::get(key.hash); }
};

#pragma pack(1)
struct ResourceHeader {
static const uint32_t MAGIC;
uint32_t magic = MAGIC; // VXI Package header
uint32_t version = 0;
uint32_t flags = 0;
uint32_t padding = 0;
uint32_t decompressedSize = 0;
};
#pragma pack()

/**
* A basic Resource type.
* Represents a single file loaded from disk.
* May have dependencies on other Resources, and other Resources may depend on this.
* Resources are reference-counted, and are removed when they go out of usage.
*/

struct Resource {
friend struct ResourceTypeManager;
friend struct ResourceManager;

enum class State : uint32_t {
EMPTY = 0,
READY,
FAILED
};

using Observer = DelegateList<void(State, State, Resource&)>;

virtual ~Resource();
virtual ResourceType getType() const = 0;
State getState() const { return state; }

bool isEmpty() const { return state == State::EMPTY; }
bool isReady() const { return state == State::READY; }
bool isFailure() const { return state == State::FAILED; }

uint32_t getReferenceCount() const { return references; }

Observer const& getCallback() const { return callback; }
size_t getSize() const { return size; }

const Path& getPath() const { return path; }

struct ResourceTypeManager& getManager() { return manager; }

uint32_t decreaseReferences();
uint32_t increaseReferences() { return references++; }

bool toInitialize() const { return desiredState == State::READY; }
bool isHooked() const { return hooked; }

template <auto Function, typename C> void onLoaded(C* instance) {
callback.bind<Function>(instance);
if (isReady()) (instance->*Function)(State::READY, State::READY, *this);
}

protected:
Resource(Path path, ResourceTypeManager& manager);

virtual void onReadying() {}
virtual void unload() = 0;
virtual bool load(size_t size, const uint8_t* mem) = 0;

void onCreated(State newState);
void performUnload();
void addDependency(Resource& dependent);
void removeDependency(Resource& dependent);
void checkState();
void refresh();

State desiredState;
uint16_t emptyDependencies;
ResourceTypeManager& manager;

private:

void doLoad();
void fileLoaded(size_t fileSize, const uint8_t* mem, bool success);
void stateChanged(State old, State newState, Resource&);

Resource(const Resource&) = delete;
void operator=(const Resource&) = delete;

Observer callback;
size_t size;
Path path;
uint32_t references;
uint16_t failedDependencies;
FileSystem::AsyncHandle handle;
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;
};
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once
#include <map>
#include <fs/hash.h>
#include <fs/path.h>

namespace ShadowEngine {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
#pragma once

#include <string>
#include <map>
#include <list>


namespace Shadow::SFF {

class SFFElement
{
public:
SFFElement* parent;

std::string name;

bool isBlock;

std::string value;
typedef std::map<std::string, SFFElement*> ChildrenMap;

ChildrenMap children;

std::string GetStringProperty(std::string name);

SFFElement* GetFirstChild();

SFFElement* GetChildByIndex(int index);

SFFElement* GetChildByName(std::string name);

~SFFElement();

};

#pragma once

#include <string>
#include <map>
#include <list>


namespace Shadow::SFF {

class SFFElement
{
public:
SFFElement* parent;

std::string name;

bool isBlock;

std::string value;
typedef std::map<std::string, SFFElement*> ChildrenMap;

ChildrenMap children;

std::string GetStringProperty(std::string name);

SFFElement* GetFirstChild();

SFFElement* GetChildByIndex(int index);

SFFElement* GetChildByName(std::string name);

~SFFElement();

};

}
Loading

0 comments on commit 231f990

Please sign in to comment.