22#pragma warning( push )
23#pragma warning( disable : PROTOBUF_WARNINGS )
25#include <google/protobuf/stubs/common.h>
26#include "configuration.pb.h"
30#include "aeongames/ProtoBufHelpers.hpp"
32#include "aeongames/Node.hpp"
33#include "aeongames/AeonEngine.hpp"
34#include "aeongames/Renderer.hpp"
35#include "aeongames/Buffer.hpp"
36#include "aeongames/Texture.hpp"
37#include "aeongames/Mesh.hpp"
38#include "aeongames/Material.hpp"
39#include "aeongames/Pipeline.hpp"
40#include "aeongames/Model.hpp"
41#include "aeongames/Skeleton.hpp"
42#include "aeongames/Animation.hpp"
44#include "aeongames/ResourceFactory.hpp"
46#include "aeongames/Utilities.hpp"
47#include "aeongames/Resource.hpp"
55 __declspec ( dllexport ) DWORD NvOptimusEnablement{1};
56 __declspec ( dllexport )
int AmdPowerXpressRequestHighPerformance{1};
62 static bool gInitialized =
false;
63 static ConfigurationMsg gConfigurationMsg;
65 static std::vector<std::tuple<HMODULE, PluginModuleInterface* >> gPlugInCache;
67 static std::vector<std::tuple<void*, PluginModuleInterface* >> gPlugInCache;
69 static std::string gPlugInPath ( std::getenv (
"PATH" ) ? std::getenv (
"PATH" ) :
"" );
70 static void LoadPlugin (
const std::string& aDir,
const std::string& aFilename )
72 std::cout <<
LogLevel::Info <<
"Plugin: " << aFilename << std::endl;
74 HMODULE plugin = LoadLibraryEx ( aFilename.c_str(),
nullptr, 0 );
75 if (
nullptr == plugin )
77 std::cout <<
LogLevel::Error <<
"Failed to load " << aFilename <<
" Error " << GetLastError() << std::endl;
80 auto* pmi = ( PluginModuleInterface* ) GetProcAddress ( ( HINSTANCE ) plugin,
"PMI" );
83 std::cout <<
LogLevel::Warning << aFilename <<
" is not an AeonEngine Plugin." << std::endl;
84 FreeLibrary ( ( HINSTANCE ) plugin );
89#define SO_SUFFIX ".dylib"
91#define SO_SUFFIX ".so"
94 if ( ! ( plugin = dlopen ( aFilename.c_str(), RTLD_NOW | RTLD_GLOBAL ) ) )
96 if ( ! ( plugin = dlopen ( (
"lib" + aFilename + SO_SUFFIX ).c_str(), RTLD_NOW | RTLD_GLOBAL ) ) )
98 std::cout <<
LogLevel::Error <<
"Failed to load " << aFilename << std::endl;
103 PluginModuleInterface* pmi = ( PluginModuleInterface* ) dlsym ( plugin,
"PMI" );
104 if (
nullptr == pmi )
106 std::cout << aFilename <<
" is not an AeonEngine Plugin." << std::endl;
113 gPlugInCache.emplace_back ( plugin, pmi );
118 FreeLibrary ( plugin );
119 if ( !FreeLibrary ( ( HINSTANCE ) plugin ) )
121 std::cout <<
"FreeLibrary Failed: " << GetLastError() << std::endl;
125 if ( dlclose ( plugin ) != 0 )
127 std::cout << dlerror() << std::endl;
134 static std::string gConfigFile{
"game/config"};
141 [] (
const char* aArgument,
void* aUserData ) ->
void {
144 std::cout <<
LogLevel::Info <<
"Reading Configuration from " << aArgument << std::endl;
145 gConfigFile = aArgument;
157 GOOGLE_PROTOBUF_VERIFY_VERSION;
159 HANDLE hOut = GetStdHandle ( STD_OUTPUT_HANDLE );
161 GetConsoleMode ( hOut, &dwMode );
162 dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
163 SetConsoleMode ( hOut, dwMode );
170 catch (
const std::runtime_error& e )
175 gPlugInCache.reserve ( gConfigurationMsg.plugin_size() );
176 for (
auto& i : gConfigurationMsg.plugin() )
178 LoadPlugin ( gConfigurationMsg.plugindirectory(), i );
181 if ( gConfigurationMsg.package().size() )
185 SetResourcePath ( {gConfigurationMsg.package().begin(), gConfigurationMsg.package().end() } );
187 catch (
const std::runtime_error& e )
189 std::cerr << e.what() << std::endl;
194 [] ( uint32_t aPath )
196 auto model = std::make_unique<Model>();
197 model->LoadFromId ( aPath );
202 [] ( uint32_t aPath )
204 auto skeleton = std::make_unique<Skeleton>();
205 skeleton->LoadFromId ( aPath );
209 [] ( uint32_t aPath )
211 auto animation = std::make_unique<Animation>();
212 animation->LoadFromId ( aPath );
217 [] ( uint32_t aPath )
219 auto texture = std::make_unique<Texture>();
220 texture->LoadFromId ( aPath );
225 [] ( uint32_t aPath )
227 auto mesh = std::make_unique<Mesh>();
228 mesh->LoadFromId ( aPath );
233 [] ( uint32_t aPath )
235 auto pipeline = std::make_unique<Pipeline>();
236 pipeline->LoadFromId ( aPath );
241 [] ( uint32_t aPath )
243 auto material = std::make_unique<Material>();
244 material->LoadFromId ( aPath );
266 for (
auto& i : gPlugInCache )
268 std::get<1> ( i )->ShutDown();
270 FreeLibrary ( std::get<0> ( i ) );
272 dlclose ( std::get<0> ( i ) );
275#if defined(__linux__) && GOOGLE_PROTOBUF_VERSION > 3006001
277 google::protobuf::ShutdownProtobufLibrary();
279 gInitialized =
false;
282 static std::vector<Package> gResourcePath{};
285 std::vector<std::string> path;
286 path.reserve ( gResourcePath.size() );
287 for (
auto& i : gResourcePath )
289 path.emplace_back ( i.GetPath().string() );
295 gResourcePath.clear();
296 gResourcePath.reserve ( aPath.size() );
297 std::ostringstream stream;
298 for (
auto& i : aPath )
302 gResourcePath.emplace_back ( i );
304 catch (
const std::runtime_error& e )
306 stream << e.what() << std::endl;
313 if ( stream.rdbuf()->in_avail() > 0 )
316 throw std::runtime_error ( stream.str().c_str() );
322 for (
auto &i : gResourcePath )
324 auto resource = i.GetIndexTable().find ( crc );
325 if ( resource != i.GetIndexTable().end() )
327 return i.GetFileSize ( crc );
335 for (
auto &i : gResourcePath )
337 auto resource = i.GetIndexTable().find ( crc );
338 if ( resource != i.GetIndexTable().end() )
340 return resource->second;
343 return std::string{};
352 for (
auto &i : gResourcePath )
354 auto resource = i.GetIndexTable().find ( crc );
355 if ( resource != i.GetIndexTable().end() )
357 i.LoadFile ( crc, buffer, buffer_size );
362 throw std::runtime_error (
"Resource not found." );
364 void LoadResource (
const std::string& aFileName,
void* buffer,
size_t buffer_size )
Defines log severity levels and stream output for the AeonGames engine.
Header for the PKG file specification.
Defines the plugin module interface for dynamically loaded plugins.
Provides the DLL_PROTOBUF export/import macro for protobuf wrapper classes.
Command-line option handler.
<- This is here just for the literals
DLL void ProcessOpts(int argc, char *argv[], const OptionHandler *aOptionHandler, size_t aOptionHandlerCount)
Process command-line options.
DLL std::vector< std::string > GetResourcePath()
Get the list of resource search paths.
DLL size_t GetResourceSize(uint32_t crc)
uint32_t crc32i(const char *message, size_t size, uint32_t previous_crc)
Compute the CRC32 of a given message, continuing from a previous CRC value.
DLL bool InitializeGlobalEnvironment(int argc=0, char *argv[]=nullptr)
Initialize the global engine environment.
DLL bool UnregisterResourceConstructor(uint32_t aType)
Unregister a resource constructor.
DLL void LoadResource(uint32_t crc, void *buffer, size_t buffer_size)
@ Warning
Potential issues that may need attention.
@ Info
General informational messages.
DLL void FinalizeGlobalEnvironment()
Shut down the global engine environment and release resources.
DLL void ClearAllResources()
Remove all resources from the cache.
DLL void SetResourcePath(const std::vector< std::string > &aPath)
Set the list of resource search paths.
DLL bool RegisterResourceConstructor(uint32_t aType, const std::function< UniqueAnyPtr(uint32_t) > &aConstructor, UniqueAnyPtr &&aDefaultResource=nullptr)
Register a constructor for a resource type.
const std::array< OptionHandler, 1 > gOptionHandlers
Array of command-line option handlers.
void LoadProtoBufObject(T &t, const void *aData, size_t aSize, uint64_t aMagick)
Loads a Protocol Buffer Object from a meory buffer into the provided reference.
StartUpPtr StartUp
Called to initialize the plugin.