Skip to content

Commit

Permalink
feat: add TaskInterface (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThirdEyeSqueegee authored Oct 24, 2023
1 parent a0207a8 commit 209051a
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CommonLibSF/include/SFSE/API.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace SFSE

const MenuInterface* GetMenuInterface() noexcept;

const TaskInterface* GetTaskInterface() noexcept;

Trampoline& GetTrampoline();

void AllocTrampoline(std::size_t a_size, bool a_trySFSEReserve = true);
Expand Down
14 changes: 14 additions & 0 deletions CommonLibSF/include/SFSE/Impl/Stubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,19 @@ namespace SFSE
std::uint32_t interfaceVersion;
void (*Register)(void*);
};

struct SFSETaskInterface
{
std::uint32_t interfaceVersion;
void (*AddTask)(void*);
void (*AddPermanentTask)(void*);
};
}

class ITaskDelegate
{
public:
virtual void Run() = 0;
virtual void Destroy() = 0;
};
}
37 changes: 37 additions & 0 deletions CommonLibSF/include/SFSE/Interfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace SFSE
kMessaging,
kTrampoline,
kMenu,
kTask,

kTotal
};
Expand Down Expand Up @@ -116,6 +117,42 @@ namespace SFSE
[[nodiscard]] const detail::SFSEMenuInterface* GetProxy() const;
};

class TaskInterface
{
public:
using TaskFn = std::function<void()>;

enum Version : std::uint32_t
{
kVersion = 1
};

[[nodiscard]] std::uint32_t Version() const;

void AddTask(TaskFn a_fn) const;

void AddTask(ITaskDelegate* a_task) const;

void AddPermanentTask(TaskFn a_fn) const;

void AddPermanentTask(ITaskDelegate* a_task) const;

private:
class Task : public ITaskDelegate
{
public:
Task(TaskFn&& a_fn);

void Run() override;
void Destroy() override;

private:
TaskFn _fn;
};

[[nodiscard]] const detail::SFSETaskInterface* GetProxy() const;
};

struct PluginInfo
{
enum Version : std::uint32_t
Expand Down
7 changes: 7 additions & 0 deletions CommonLibSF/src/SFSE/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace SFSE
TrampolineInterface* trampolineInterface{};
MessagingInterface* messagingInterface{};
MenuInterface* menuInterface{};
TaskInterface* taskInterface{};

std::mutex apiLock;
std::vector<std::function<void()>> apiInitRegs;
Expand Down Expand Up @@ -69,6 +70,7 @@ namespace SFSE
storage.messagingInterface = detail::QueryInterface<MessagingInterface>(a_intfc, LoadInterface::kMessaging);
storage.trampolineInterface = detail::QueryInterface<TrampolineInterface>(a_intfc, LoadInterface::kTrampoline);
storage.menuInterface = detail::QueryInterface<MenuInterface>(a_intfc, LoadInterface::kMenu);
storage.taskInterface = detail::QueryInterface<TaskInterface>(a_intfc, LoadInterface::kTask);

storage.apiInit = true;
auto& regs = storage.apiInitRegs;
Expand Down Expand Up @@ -114,6 +116,11 @@ namespace SFSE
return detail::APIStorage::get().menuInterface;
}

const TaskInterface* GetTaskInterface() noexcept
{
return detail::APIStorage::get().taskInterface;
}

Trampoline& GetTrampoline()
{
static Trampoline trampoline;
Expand Down
45 changes: 45 additions & 0 deletions CommonLibSF/src/SFSE/Interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,51 @@ namespace SFSE
return reinterpret_cast<const detail::SFSEMenuInterface*>(this);
}

std::uint32_t TaskInterface::Version() const
{
return GetProxy()->interfaceVersion;
}

void TaskInterface::AddTask(TaskFn a_fn) const
{
return GetProxy()->AddTask(new Task(std::move(a_fn)));
}

void TaskInterface::AddTask(ITaskDelegate* a_task) const
{
return GetProxy()->AddTask(a_task);
}

void TaskInterface::AddPermanentTask(TaskFn a_fn) const
{
return GetProxy()->AddPermanentTask(new Task(std::move(a_fn)));
}

void TaskInterface::AddPermanentTask(ITaskDelegate* a_task) const
{
return GetProxy()->AddPermanentTask(a_task);
}

TaskInterface::Task::Task(TaskFn&& a_fn) :
_fn(std::move(a_fn))
{}

void TaskInterface::Task::Run()
{
_fn();
}

void TaskInterface::Task::Destroy()
{
delete this;
}

const detail::SFSETaskInterface* TaskInterface::GetProxy() const
{
assert(this);
return reinterpret_cast<const detail::SFSETaskInterface*>(this);
}

const PluginVersionData* PluginVersionData::GetSingleton() noexcept
{
return reinterpret_cast<const PluginVersionData*>(WinAPI::GetProcAddress(WinAPI::GetCurrentModule(), "SFSEPlugin_Version"));
Expand Down

0 comments on commit 209051a

Please sign in to comment.