Skip to content

Commit

Permalink
feat: add TaskInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
ThirdEyeSqueegee committed Oct 23, 2023
1 parent ef9c376 commit bbfe569
Show file tree
Hide file tree
Showing 5 changed files with 133 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
16 changes: 16 additions & 0 deletions CommonLibSF/include/SFSE/Impl/Stubs.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

class ITaskDelegate;

This comment has been minimized.

Copy link
@qudix

qudix Oct 23, 2023

Contributor

unecessary, remnant of old commonlibse code


namespace SFSE
{
using PluginHandle = std::uint32_t;
Expand Down Expand Up @@ -47,5 +49,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;
};
}
}
49 changes: 49 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,54 @@ 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 detail::ITaskDelegate
{
public:
Task(TaskFn&& a_task);

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

private:
TaskFn _fn;
};

class PermanentTask : public detail::ITaskDelegate

This comment has been minimized.

Copy link
@qudix

qudix Oct 23, 2023

Contributor

Identical to the top one, no point in repetition

{
public:
PermanentTask(TaskFn&& a_task);

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
59 changes: 59 additions & 0 deletions CommonLibSF/src/SFSE/Interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,65 @@ 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 PermanentTask(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;
}

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

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

void TaskInterface::PermanentTask::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 bbfe569

Please sign in to comment.