Skip to content

Commit

Permalink
Scripting WIP, gonna rebase later
Browse files Browse the repository at this point in the history
  • Loading branch information
Felk committed Jun 4, 2018
1 parent a6f7df3 commit 44b8406
Show file tree
Hide file tree
Showing 27 changed files with 3,771 additions and 2,658 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
path = Externals/Qt
url = https://github.com/dolphin-emu/ext-win-qt.git
branch = master
[submodule "Externals/python"]
path = Externals/python
url = https://github.com/Felk/ext-python.git
branch = master
1 change: 1 addition & 0 deletions Externals/python
Submodule python added at c7c640
1 change: 1 addition & 0 deletions Source/Core/Common/Logging/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ enum LOG_TYPE
PIXELENGINE,
PROCESSORINTERFACE,
POWERPC,
SCRIPTING,
SERIALINTERFACE,
SP1,
SYMBOLS,
Expand Down
1 change: 1 addition & 0 deletions Source/Core/Common/Logging/LogManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ LogManager::LogManager()
m_log[LogTypes::PIXELENGINE] = {"PE", "PixelEngine"};
m_log[LogTypes::PROCESSORINTERFACE] = {"PI", "ProcessorInt"};
m_log[LogTypes::POWERPC] = {"PowerPC", "IBM CPU"};
m_log[LogTypes::SCRIPTING] = {"Scripting", "Scripting"};
m_log[LogTypes::SERIALINTERFACE] = {"SI", "Serial Interface (SI)"};
m_log[LogTypes::SP1] = {"SP1", "Serial Port 1"};
m_log[LogTypes::SYMBOLS] = {"SYMBOLS", "Symbols"};
Expand Down
23 changes: 23 additions & 0 deletions Source/Core/Core/API/Events.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#include "Core/API/Events.h"

namespace API
{

template <typename T>
static std::vector<Listener<T>> listeners;

template <typename T>
std::vector<Listener<T>>& GetListenersVector()
{
return listeners<T>;
}

// Force template instantiations for all events.
// If events get used which do not have a GetListenersVector
// instantiation, spooky templating errors may appear.
template std::vector<Listener<Events::Frameadvance>>& GetListenersVector();
template std::vector<Listener<Events::Memory>>& GetListenersVector();
template std::vector<Listener<Events::Interrupt>>& GetListenersVector();

} // namespace API
91 changes: 91 additions & 0 deletions Source/Core/Core/API/Events.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#pragma once

#include <functional>
#include <vector>
#include "Common/CommonTypes.h"

namespace API
{
namespace Events
{
// Each event also needs a manual template instantiation in Events.cpp
struct Frameadvance
{
};
struct Memory
{
bool write;
u32 addr;
u32 value;
};
struct Interrupt
{
u32 cause_mask;
bool set;
};

} // namespace API::Events

// a listener on T is any function that takes a const reference to T as argument
template <typename T>
using Listener = std::function<void(const T&)>;
template <typename T>
using ListenerFuncPtr = void (*)(const T&);

template <typename T>
std::vector<Listener<T>>& GetListenersVector();

template <typename T>
void EmitEvent(T evt)
{
for (Listener<T>& listener : GetListenersVector<T>())
listener(evt);
}

template <typename T>
void ListenEvent(Listener<T> listener)
{
GetListenersVector<T>().push_back(std::move(listener));
}

// convenience overload
template <typename T>
void ListenEvent(ListenerFuncPtr<T> listenerFuncPtr)
{
ListenEvent(Listener<T>(listenerFuncPtr));
}

template <typename T>
bool UnlistenEvent(Listener<T> listener)
{
auto ls = GetListenersVector<T>();
auto it = ls.begin();
while (it != ls.end())
{
Listener<T> l = *it;
if (l.target_type() == listener.target_type())
{
// TODO how to check std::function for equality? Maybe do something completely different instead...
//ls.erase(l);
return true;
}
++it;
}

/*auto existing = std::find(ls.begin(), ls.end(), listener);
if (existing != ls.end())
{
ls.erase(existing);
return true;
}*/
return false;
}

// convenience overload
template <typename T>
bool UnlistenEvent(ListenerFuncPtr<T> listenerFuncPtr)
{
return UnlistenEvent(Listener<T>(listenerFuncPtr));
}

} // namespace API
7 changes: 7 additions & 0 deletions Source/Core/Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
#include "VideoCommon/OnScreenDisplay.h"
#include "VideoCommon/RenderBase.h"
#include "VideoCommon/VideoBackendBase.h"
#include "Scripting/ScriptingEngine.h"
#include "API/Events.h"

namespace Core
{
Expand Down Expand Up @@ -412,6 +414,9 @@ static void EmuThread(std::unique_ptr<BootParameters> boot)
DeclareAsCPUThread();
s_frame_step = false;

Scripting::Init();
Common::ScopeGuard scripting_guard{Scripting::Shutdown};

Movie::Init(*boot);
Common::ScopeGuard movie_guard{Movie::Shutdown};

Expand Down Expand Up @@ -787,6 +792,8 @@ void Callback_VideoCopiedToXFB(bool video_update)
if (s_on_state_changed_callback)
s_on_state_changed_callback(Core::GetState());
}

API::EmitEvent(API::Events::Frameadvance{});
}

void UpdateTitle()
Expand Down
Loading

0 comments on commit 44b8406

Please sign in to comment.