<commit_before>/*
 * Copyright (c) 2012-2016 <NAME> and individual contributors.
 * License: https://github.com/taylor001/crown/blob/master/LICENSE
 */

#include "device.h"
#include "array.h"
#include "config.h"
#include "input_manager.h"
#include "log.h"
#include "lua_environment.h"
#include "material_manager.h"
#include "memory.h"
#include "os.h"
#include "resource_loader.h"
#include "resource_manager.h"
#include "resource_package.h"
#include "types.h"
#include "world.h"
#include "filesystem.h"
#include "path.h"
#include "disk_filesystem.h"
#include "physics.h"
#include "audio.h"
#include "profiler.h"
#include "console_server.h"
#include "input_device.h"
#include "sjson.h"
#include "map.h"
#include "vector3.h"

#if CROWN_PLATFORM_ANDROID
	#include "apk_filesystem.h"
#endif // CROWN_PLATFORM_ANDROID

#define MAX_SUBSYSTEMS_HEAP 8 * 1024 * 1024

namespace crown
{

Device::Device(const DeviceOptions& opts)
	: _allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP)
	, _width(0)
	, _height(0)
	, _mouse_curr_x(0)
	, _mouse_curr_y(0)
	, _mouse_last_x(0)
	, _mouse_last_y(0)
	, _is_init(false)
	, _is_running(false)
	, _is_paused(false)
	, _frame_count(0)
	, _last_time(0)
	, _current_time(0)
	, _last_delta_time(0.0f)
	, _time_since_start(0.0)
	, _device_options(opts)
	, _bundle_filesystem(NULL)
	, _boot_package_id(uint64_t(0))
	, _boot_script_id(uint64_t(0))
	, _boot_package(NULL)
	, _lua_environment(NULL)
	, _resource_loader(NULL)
	, _resource_manager(NULL)
	, _input_manager(NULL)
	, _shader_manager(NULL)
	, _material_manager(NULL)
	, _unit_manager(NULL)
	, _worlds(default_allocator())
	, _bgfx_allocator(default_allocator())
{
}

void Device::init()
{
	// Initialize
	CE_LOGI("Initializing Crown Engine %s...", version());

	profiler_globals::init();

#if CROWN_PLATFORM_ANDROID
	_bundle_filesystem = CE_NEW(_allocator, ApkFilesystem)(const_cast<AAssetManager*>(_device_options.asset_manager()));
#else
	_bundle_filesystem = CE_NEW(_allocator, DiskFilesystem)(_device_options.bundle_dir());
#endif // CROWN_PLATFORM_ANDROID

	_resource_loader = CE_NEW(_allocator, ResourceLoader)(*_bundle_filesystem);
	_resource_manager = CE_NEW(_allocator, ResourceManager)(*_resource_loader);

	read_config();

	bgfx::init(bgfx::RendererType::Count
		, BGFX_PCI_ID_NONE
		, 0
		, &_bgfx_callback
		, &_bgfx_allocator
		);

	_shader_manager = CE_NEW(_allocator, ShaderManager)(default_allocator());
	_material_manager = CE_NEW(_allocator, MaterialManager)(default_allocator(), *_resource_manager);
	_input_manager = CE_NEW(_allocator, InputManager)(default_allocator());
	_unit_manager = CE_NEW(_allocator, UnitManager)(default_allocator());

	audio_globals::init();
	physics_globals::init();

	_boot_package = create_resource_package(_boot_package_id);
	_boot_package->load();
	_boot_package->flush();

	_lua_environment = CE_NEW(_allocator, LuaEnvironment)();
	_lua_environment->load_libs();
	_lua_environment->execute((LuaResource*)_resource_manager->get(SCRIPT_TYPE, _boot_script_id));
	_lua_environment->call_global("init", 0);

	_is_init = true;
	_is_running = true;
	_last_time = os::clocktime();

	CE_LOGD("Engine initialized");
}

void Device::shutdown()
{
	CE_ASSERT(_is_init, "Engine is not initialized");

	_is_running = false;
	_is_init = false;

	_lua_environment->call_global("shutdown", 0);
	CE_DELETE(_allocator, _lua_environment);

	_boot_package->unload();
	destroy_resource_package(*_boot_package);

	physics_globals::shutdown();
	audio_globals::shutdown();

	CE_DELETE(_allocator, _unit_manager);<commit_msg>Cleanup<commit_after>/*
 * Copyright (c) 2012-2016 <NAME> and individual contributors.
 * License: https://github.com/taylor001/crown/blob/master/LICENSE
 */

#include "device.h"
#include "array.h"
#include "config.h"
#include "input_manager.h"
#include "log.h"
#include "lua_environment.h"
#include "material_manager.h"
#include "memory.h"
#include "os.h"
#include "resource_loader.h"
#include "resource_manager.h"
#include "resource_package.h"
#include "types.h"
#include "world.h"
#include "filesystem.h"
#include "path.h"
#include "disk_filesystem.h"
#include "physics.h"
#include "audio.h"
#include "profiler.h"
#include "console_server.h"
#include "input_device.h"
#include "sjson.h"
#include "map.h"
#include "vector3.h"

#if CROWN_PLATFORM_ANDROID
	#include "apk_filesystem.h"
#endif // CROWN_PLATFORM_ANDROID

#define MAX_SUBSYSTEMS_HEAP 8 * 1024 * 1024

namespace crown
{

Device::Device(const DeviceOptions& opts)
	: _allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP)
	, _width(0)
	, _height(0)
	, _mouse_curr_x(0)
	, _mouse_curr_y(0)
	, _mouse_last_x(0)
	, _mouse_last_y(0)
	, _is_init(false)
	, _is_running(false)
	, _is_paused(false)
	, _frame_count(0)
	, _last_time(0)
	, _current_time(0)
	, _last_delta_time(0.0f)
	, _time_since_start(0.0)
	, _device_options(opts)
	, _bundle_filesystem(NULL)
	, _boot_package_id(uint64_t(0))
	, _boot_script_id(uint64_t(0))
	, _boot_package(NULL)
	, _lua_environment(NULL)
	, _resource_loader(NULL)
	, _resource_manager(NULL)
	, _input_manager(NULL)
	, _shader_manager(NULL)
	, _material_manager(NULL)
	, _unit_manager(NULL)
	, _worlds(default_allocator())
	, _bgfx_allocator(default_allocator())
{
}

void Device::init()
{
	// Initialize
	CE_LOGI("Initializing Crown Engine %s...", version());

	profiler_globals::init();

#if CROWN_PLATFORM_ANDROID
	_bundle_filesystem = CE_NEW(_allocator, ApkFilesystem)(const_cast<AAssetManager*>(_device_options.asset_manager()));
#else
	_bundle_filesystem = CE_NEW(_allocator, DiskFilesystem)(_device_options.bundle_dir());
#endif // CROWN_PLATFORM_ANDROID

	_resource_loader  = CE_NEW(_allocator, ResourceLoader)(*_bundle_filesystem);
	_resource_manager = CE_NEW(_allocator, ResourceManager)(*_resource_loader);

	read_config();

	bgfx::init(bgfx::RendererType::Count
		, BGFX_PCI_ID_NONE
		, 0
		, &_bgfx_callback
		, &_bgfx_allocator
		);

	_shader_manager   = CE_NEW(_allocator, ShaderManager)(default_allocator());
	_material_manager = CE_NEW(_allocator, MaterialManager)(default_allocator(), *_resource_manager);
	_input_manager    = CE_NEW(_allocator, InputManager)(default_allocator());
	_unit_manager     = CE_NEW(_allocator, UnitManager)(default_allocator());
	_lua_environment  = CE_NEW(_allocator, LuaEnvironment)();

	audio_globals::init();
	physics_globals::init();

	_boot_package = create_resource_package(_boot_package_id);
	_boot_package->load();
	_boot_package->flush();

	_lua_environment->load_libs();
	_lua_environment->execute((LuaResource*)_resource_manager->get(SCRIPT_TYPE, _boot_script_id));
	_lua_environment->call_global("init", 0);

	_is_init = true;
	_is_running = true;
	_last_time = os::clocktime();

	CE_LOGD("Engine initialized");
}

void Device::shutdown()
{
	CE_ASSERT(_is_init, "Engine is not initialized");

	_is_running = false;
	_is_init = false;

	_lua_environment->call_global("shutdown", 0);

	_boot_package->unload();
	destroy_resource_package(*_boot_package);

	physics_globals::shutdown();
	audio_globals::shutdown();

	CE_DELETE(_allocator, _lua_environment);
	CE_DELETE(_allocator, _unit_manager);
{'fraction_non_alphanumeric': 0.1394822464280662, 'fraction_numerical': 0.012165794313198473, 'mean_word_length': 7.356973995271868, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6293122', 'n_tokens_mistral': 2787, 'n_tokens_neox': 2688, 'n_words': 387}