Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add TaskInterface #191

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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