Commit 779efeb8 authored by NTAuthority's avatar NTAuthority

shitton of untested and broken shit to try to get it to work in emscripten lol

parent 72565824
......@@ -103,12 +103,14 @@ struct ConsoleArgumentType
{
static std::string Unparse(const TArgument& argument)
{
static_assert(false, "Unknown ConsoleArgumentType unparse handler (try defining one?)");
//static_assert(false, "Unknown ConsoleArgumentType unparse handler (try defining one?)");
TArgument::__fatal();
}
static bool Parse(const std::string& input, TArgument* out)
{
static_assert(false, "Unknown ConsoleArgumentType parse handler (try defining one?)");
//static_assert(false, "Unknown ConsoleArgumentType parse handler (try defining one?)");
TArgument::__fatal();
}
};
......
......@@ -29,13 +29,13 @@ struct Constraints<T, std::enable_if_t<std::is_arithmetic<T>::value>>
{
inline static bool Compare(const T& value, const T& minValue, const T& maxValue)
{
if (ConsoleArgumentTraits<T>::Greater()(value, maxValue))
if (typename ConsoleArgumentTraits<T>::Greater()(value, maxValue))
{
console::Printf("Value out of range (%s) - should be at most %s\n", UnparseArgument(value).c_str(), UnparseArgument(maxValue).c_str());
return false;
}
if (ConsoleArgumentTraits<T>::Less()(value, minValue))
if (typename ConsoleArgumentTraits<T>::Less()(value, minValue))
{
console::Printf("Value out of range (%s) - should be at least %s\n", UnparseArgument(value).c_str(), UnparseArgument(minValue).c_str());
return false;
......@@ -46,19 +46,102 @@ struct Constraints<T, std::enable_if_t<std::is_arithmetic<T>::value>>
};
void MarkConsoleVarModified(ConsoleVariableManager* manager, const std::string& name);
}
enum ConsoleVariableFlags
{
ConVar_None = 0,
ConVar_Archive = 0x1,
ConVar_Modified = 2
};
class ConsoleVariableManager
{
public:
using THandlerPtr = std::shared_ptr<internal::ConsoleVariableEntryBase>;
using TVariableCB = std::function<void(const std::string& name, int flags, const THandlerPtr& variable)>;
using TWriteLineCB = std::function<void(const std::string& line)>;
public:
ConsoleVariableManager(console::Context* context);
~ConsoleVariableManager();
int Register(const std::string& name, int flags, const THandlerPtr& variable);
void Unregister(int token);
bool Process(const std::string& commandName, const ProgramArguments& arguments);
THandlerPtr FindEntryRaw(const std::string& name);
void AddEntryFlags(const std::string& name, int flags);
void RemoveEntryFlags(const std::string& name, int flags);
void ForAllVariables(const TVariableCB& callback, int flagMask = 0xFFFFFFFF);
void SaveConfiguration(const TWriteLineCB& writeLineFunction);
inline console::Context* GetParentContext()
{
return m_parentContext;
}
private:
struct Entry
{
std::string name;
int flags;
THandlerPtr variable;
int token;
inline Entry(const std::string& name, int flags, const THandlerPtr& variable, int token)
: name(name), flags(flags), variable(variable), token(token)
{
}
};
private:
console::Context* m_parentContext;
std::map<std::string, Entry, IgnoreCaseLess> m_entries;
std::shared_timed_mutex m_mutex;
std::atomic<int> m_curToken;
std::unique_ptr<ConsoleCommand> m_setCommand;
std::unique_ptr<ConsoleCommand> m_setaCommand;
std::unique_ptr<ConsoleCommand> m_toggleCommand;
std::unique_ptr<ConsoleCommand> m_vstrCommand;
public:
static ConsoleVariableManager* GetDefaultInstance();
};
namespace internal
{
template <typename T>
class ConsoleVariableEntry : public ConsoleVariableEntryBase
{
public:
ConsoleVariableEntry(ConsoleVariableManager* manager, const std::string& name, const T& defaultValue)
: m_manager(manager), m_name(name), m_trackingVar(nullptr), m_defaultValue(defaultValue), m_curValue(defaultValue), m_hasConstraints(false)
: m_manager(manager), m_name(name), m_trackingVar(nullptr), m_defaultValue(defaultValue), m_curValue(defaultValue), m_hasConstraints(false)
{
m_getCommand = std::make_unique<ConsoleCommand>(manager->GetParentContext(), name, [=]() {
m_getCommand = std::make_unique<ConsoleCommand>(manager->GetParentContext(), name, [=] ()
{
console::Printf(" \"%s\" is \"%s\"\n default: \"%s\"\n type: %s\n", name.c_str(), GetValue().c_str(), UnparseArgument(m_defaultValue).c_str(), ConsoleArgumentName<T>::Get());
});
m_setCommand = std::make_unique<ConsoleCommand>(manager->GetParentContext(), name, [=](const T& newValue) {
m_setCommand = std::make_unique<ConsoleCommand>(manager->GetParentContext(), name, [=] (const T& newValue)
{
SetRawValue(newValue);
});
}
......@@ -86,7 +169,7 @@ public:
// update from a tracking variable
if (m_trackingVar)
{
if (!ConsoleArgumentTraits<T>::Equal()(*m_trackingVar, m_curValue))
if (!(typename ConsoleArgumentTraits<T>::Equal()(*m_trackingVar, m_curValue)))
{
m_curValue = *m_trackingVar;
}
......@@ -120,7 +203,7 @@ public:
}
// update modified flags if changed
if (!ConsoleArgumentTraits<T>::Equal()(m_curValue, newValue))
if (!typename ConsoleArgumentTraits<T>::Equal()(m_curValue, newValue))
{
// indirection as manager isn't declared by now
MarkConsoleVarModified(m_manager, m_name);
......@@ -157,84 +240,6 @@ private:
};
}
enum ConsoleVariableFlags
{
ConVar_None = 0,
ConVar_Archive = 0x1,
ConVar_Modified = 2
};
class ConsoleVariableManager
{
public:
using THandlerPtr = std::shared_ptr<internal::ConsoleVariableEntryBase>;
using TVariableCB = std::function<void(const std::string& name, int flags, const THandlerPtr& variable)>;
using TWriteLineCB = std::function<void(const std::string& line)>;
public:
ConsoleVariableManager(console::Context* context);
~ConsoleVariableManager();
int Register(const std::string& name, int flags, const THandlerPtr& variable);
void Unregister(int token);
bool Process(const std::string& commandName, const ProgramArguments& arguments);
THandlerPtr FindEntryRaw(const std::string& name);
void AddEntryFlags(const std::string& name, int flags);
void RemoveEntryFlags(const std::string& name, int flags);
void ForAllVariables(const TVariableCB& callback, int flagMask = 0xFFFFFFFF);
void SaveConfiguration(const TWriteLineCB& writeLineFunction);
inline console::Context* GetParentContext()
{
return m_parentContext;
}
private:
struct Entry
{
std::string name;
int flags;
THandlerPtr variable;
int token;
inline Entry(const std::string& name, int flags, const THandlerPtr& variable, int token)
: name(name), flags(flags), variable(variable), token(token)
{
}
};
private:
console::Context* m_parentContext;
std::map<std::string, Entry, IgnoreCaseLess> m_entries;
std::shared_timed_mutex m_mutex;
std::atomic<int> m_curToken;
std::unique_ptr<ConsoleCommand> m_setCommand;
std::unique_ptr<ConsoleCommand> m_setaCommand;
std::unique_ptr<ConsoleCommand> m_toggleCommand;
std::unique_ptr<ConsoleCommand> m_vstrCommand;
public:
static ConsoleVariableManager* GetDefaultInstance();
};
template <typename TValue>
static std::shared_ptr<internal::ConsoleVariableEntry<TValue>> CreateVariableEntry(ConsoleVariableManager* manager, const std::string& name, const TValue& defaultValue)
{
......
#pragma once
// common standard library headers
#ifndef _MSC_VER
#define _stricmp strcasecmp
#define _strnicmp strncasecmp
#define abstract
// TODO: review _countof use and replace with safer C++ version
#define _countof(a) (sizeof(a)/sizeof(*(a)))
#endif
// containers
#include <vector>
......@@ -33,6 +42,9 @@
// manually include RW stuff as we don't want D3D lingering in our global namespace
//#include <rw.h>
#ifndef RW_INCLUDED
#define RW_INCLUDED
#include "src/rwbase.h"
#include "src/rwplugin.h"
#include "src/rwpipeline.h"
......@@ -43,6 +55,7 @@
//#include "src/rwd3d8.h"
//#include "src/rwd3d9.h"
//#include "src/rwogl.h"
#endif
#include <RwHelpers.h>
......
#pragma once
#ifdef _WIN32
// assertion handlers
namespace krt
{
......@@ -28,4 +29,10 @@ void Assert(AssertionType type, const char* assertionString, int line, const cha
#endif
#define check(n) \
_DO_ASSERT(ASSERT_CHECK, n, __COUNTER__)
\ No newline at end of file
_DO_ASSERT(ASSERT_CHECK, n, __COUNTER__)
#else
#include <assert.h>
#define check(n) \
!!(n) || (abort(), true);
#endif
\ No newline at end of file
// Lock utilities, like faster locks of RAII wrappers.
#pragma once
#if defined(EMSCRIPTEN)
#include <shared_mutex>
// looks quite evil
#define shared_timed_mutex mutex
#define lock_shared lock
#define unlock_shared unlock
#endif
namespace krt
{
......
#include "StdInc.h"
#include <utils/Assert.h>
#ifdef _WIN32
#include <Win32Resource.h>
#include <atlbase.h>
......@@ -213,4 +214,5 @@ void Assert(AssertionType type, const char* assertionString, int line, const cha
break;
}
}
}
\ No newline at end of file
}
#endif
\ No newline at end of file
#include <StdInc.h>
#include <utils/CallStack.h>
#ifdef _WIN32
#include <windows.h>
// dbghelp.h causes this warning
......@@ -26,4 +27,5 @@ std::unique_ptr<CallStack> CallStack::Build(int maxSteps, int skipDepth, void* s
return retval;
}
}
\ No newline at end of file
}
#endif
\ No newline at end of file
......@@ -3,6 +3,7 @@
namespace krt
{
#ifndef EMSCRIPTEN
static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> m_converter;
std::string ToNarrow(const std::wstring& wide)
......@@ -14,4 +15,15 @@ std::wstring ToWide(const std::string& narrow)
{
return m_converter.from_bytes(narrow);
}
#else
std::string ToNarrow(const std::wstring& wide)
{
return std::string(wide.begin(), wide.end());
}
std::wstring ToWide(const std::string& narrow)
{
return std::wstring(narrow.begin(), narrow.end());
}
#endif
}
\ No newline at end of file
......@@ -6,7 +6,7 @@ namespace krt
{
namespace vfs
{
class Win32Device : public Device
class LocalDevice : public Device
{
public:
virtual THandle Open(const std::string& fileName, bool readOnly) override;
......
This diff is collapsed.
......@@ -6,6 +6,10 @@
#include "WorldMath.h"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
namespace krt
{
int Main::Run(const ProgramArguments& arguments)
......@@ -60,6 +64,7 @@ int Main::Run(const ProgramArguments& arguments)
{
// create and initialize the game
#ifndef EMSCRIPTEN
Game TheGame(setList);
// run argument bits captures
......@@ -70,6 +75,25 @@ int Main::Run(const ProgramArguments& arguments)
// run the game
TheGame.Run();
#else
printf("Hi I'm main!\n");
auto TheGame = new Game(setList);
printf("Hi I'm mainy!\n");
// run argument bits captures
for (const auto& bit : argumentBits)
{
console::ExecuteSingleCommand(bit);
}
// run the game
emscripten_set_main_loop_arg([] (void* argument)
{
reinterpret_cast<Game*>(argument)->RunFrame();
}, TheGame, 0, true);
#endif
}
return 0;
......
#include "StdInc.h"
#include <fonts/FontRenderer.h>
#include <font/text_buffer_manager.h>
namespace krt
{
class FontRendererImpl : public FontRenderer
{
public:
virtual void Initialize(FontRendererGameInterface* gameInterface) override
{
}
virtual void DrawText(const std::string& text, const Rect& rect, const RGBA& color, float fontSize, float fontScale, const std::string& fontRef) override
{
}
virtual void DrawRectangle(const Rect& rect, const RGBA& color) override
{
}
virtual void DrawPerFrame() override
{
}
virtual bool GetStringMetrics(const std::string& characterString, float fontSize, float fontScale, const std::string& fontRef, Rect* outRect) override
{
*outRect = Rect(0, 0, 10, 10);
return true;
}
};
class DummyRenderPhase : public RenderPhase
{
public:
virtual ~DummyRenderPhase() override
{
}
virtual void SetupView(uint8_t id) override
{
}
virtual void Render() override
{
}
virtual const char* GetName() override
{
return "";
}
};
std::shared_ptr<RenderPhase> CreateFontRenderPhase(GameWindow* gameWindow)
{
return std::make_shared<DummyRenderPhase>();
}
static FontRendererImpl theImpl;
FontRenderer* TheFonts = &theImpl;
}
\ No newline at end of file
#include "StdInc.h"
#include <windows.h>
#include <RTMain.h>
#include <ProgramArguments.h>
......
#include <StdInc.h>
#include <Console.Base.h>
namespace krt
{
namespace sys
{
static void PrintConsole(const char* string)
{
for (const char* p = string; *p; p++)
{
if (*p == '^' && isdigit(p[1]))
{
++p;
}
else
{
printf("%c", *p);
}
}
}
void InitializeConsole()
{
console::AddPrintListener(PrintConsole);
}
const char* GetConsoleInput()
{
return nullptr;
}
}
}
\ No newline at end of file
#include <StdInc.h>
#include <sys/Timer.h>
#include <sys/time.h>
namespace krt
{
namespace sys
{
uint64_t Milliseconds()
{
timeval tp;
gettimeofday(&tp, NULL);
return ((tp.tv_sec) * 1000) + tp.tv_usec / 1000;
}
TimerContext::TimerContext()
{
}
TimerContext::~TimerContext()
{
}
}
}
\ No newline at end of file
......@@ -5,12 +5,12 @@ namespace krt
{
namespace vfs
{
uint64_t Device::OpenBulk(const std::string& fileName, uint64_t* ptr)
Device::THandle Device::OpenBulk(const std::string& fileName, uint64_t* ptr)
{
return INVALID_DEVICE_HANDLE;
}
uint64_t Device::Create(const std::string& filename)
Device::THandle Device::Create(const std::string& filename)
{
return INVALID_DEVICE_HANDLE;
}
......
#include <StdInc.h>
#include <vfs/Manager.h>
#include <vfs/Win32Device.h>
#include <vfs/LocalDevice.h>
namespace krt
{
......@@ -9,7 +9,7 @@ namespace vfs
{
Manager::Manager()
{
m_fallbackDevice = std::make_shared<Win32Device>();
m_fallbackDevice = std::make_shared<LocalDevice>();
}
StreamPtr Manager::OpenRead(const std::string& path)
......
#include <StdInc.h>
#include <vfs/LocalDevice.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
namespace krt
{
namespace vfs
{
Device::THandle LocalDevice::Open(const std::string& fileName, bool readOnly)
{
int fd = open(fileName.c_str(), (readOnly) ? O_RDONLY : O_RDWR);
if (fd < 0)
{
return INVALID_DEVICE_HANDLE;
}
return static_cast<THandle>(fd);
}
Device::THandle LocalDevice::OpenBulk(const std::string& fileName, uint64_t* ptr)
{
*ptr = 0;
return Open(fileName, true);
}
Device::THandle LocalDevice::Create(const std::string& filename)
{
int fd = creat(filename.c_str(), 0755);
if (fd < 0)
{
return INVALID_DEVICE_HANDLE;
}
return static_cast<THandle>(fd);
}
size_t LocalDevice::Read(THandle handle, void* outBuffer, size_t size)
{
assert(handle != Device::InvalidHandle);
ssize_t bytesRead = read(static_cast<int>(handle), outBuffer, size);
return (bytesRead < 0) ? -1 : static_cast<size_t>(bytesRead);
}
size_t LocalDevice::ReadBulk(THandle handle, uint64_t ptr, void* outBuffer, size_t size)
{
assert(handle != Device::InvalidHandle);
ssize_t bytesRead = pread(static_cast<int>(handle), outBuffer, size, static_cast<off_t>(ptr));
return (bytesRead < 0) ? -1 : static_cast<size_t>(bytesRead);
}
size_t LocalDevice::Write(THandle handle, const void* buffer, size_t size)
{
assert(handle != Device::InvalidHandle);
ssize_t bytesWritten = write(static_cast<int>(handle), buffer, size);
return (bytesWritten < 0) ? -1 : static_cast<size_t>(bytesWritten);
}
size_t LocalDevice::WriteBulk(THandle handle, uint64_t ptr, const void* buffer, size_t size)
{
assert(!"Not implemented!");
return -1;
}
size_t LocalDevice::Seek(THandle handle, intptr_t offset, int seekType)
{
assert(handle != Device::InvalidHandle);
return static_cast<size_t>(lseek(static_cast<int>(handle), static_cast<off_t>(offset), seekType));
}
bool LocalDevice::Close(THandle handle)
{
return (close(handle) == 0);
}
bool LocalDevice::CloseBulk(THandle handle)
{
return (close(handle) == 0);
}
bool LocalDevice::RemoveFile(const std::string& filename)
{
return (unlink(filename.c_str()) == 0);
}
bool LocalDevice::RenameFile(const std::string& from, const std::string& to)
{
return (rename(from.c_str(), to.c_str()) != 0);
}
bool LocalDevice::CreateDirectory(const std::string& name)
{
return (mkdir(name.c_str(), 0755) == 0);
}
bool LocalDevice::RemoveDirectory(const std::string& name)
{
return (rmdir(name.c_str()) == 0);
}
size_t LocalDevice::GetLength(THandle handle)
{
struct stat buf;
fstat(static_cast<int>(handle), &buf);
return buf.st_size;
}
Device::THandle LocalDevice::FindFirst(const std::string& folder, FindData* findData)
{