-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesktopHALModule.cpp
More file actions
205 lines (173 loc) · 7.31 KB
/
Copy pathDesktopHALModule.cpp
File metadata and controls
205 lines (173 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
* @file DesktopHALModule.cpp
* @brief Desktop platform host for the Deki Desktop HAL module.
*
* Owns the desktop program entry (main) and brings up the desktop HAL
* (memory + filesystem providers) before the engine initializes. This mirrors
* the ESP32 HAL module (ESP32HALModule.cpp), whose ESP32BackendInit sets the
* device backends before app_main(). Display/input/time come from a separate
* graphics module (deki-sdl3-integration), exactly as lovyangfx supplies the
* device display alongside the ESP32 HAL.
*/
#include "DesktopHALModule.h"
#include "interop/DekiPlugin.h"
#include "DekiModuleFeatureMeta.h"
#include "reflection/ComponentRegistry.h"
#include "reflection/ComponentFactory.h"
// =============================================================================
// Desktop platform entry + HAL bring-up (standalone simulator build)
// =============================================================================
#if defined(SIMULATOR)
#include "DekiMain.h"
#include "DekiLogSystem.h"
#include "providers/DekiMemory.h"
#include "providers/DekiFileSystem.h"
#include "providers/IDekiFileSystem.h"
#include "platforms/desktop/DesktopMemoryProvider.h"
#include "platforms/desktop/DesktopFileSystem.h"
#include "assets/AssetManager.h"
#include "assets/AssetLookupTable.h"
#include "assets/AssetPackReader.h"
#include <cstdio>
// Load the deployed asset registry from the SD-card mount (S:/) so prefab/asset
// lookups by key resolve. On a device this is done by SDCardComponent once the SD
// mounts; the simulator's S:/ is just ./storage/, already live via DesktopFileSystem.
static void LoadDeployedAssetTable() {
const char* tablePath = "S:/asset_table.bin";
IDekiFileSystem* fs = DekiFileSystem::GetFileSystemForPath(tablePath);
if (!fs) { DEKI_LOG_WARNING("Simulator: no filesystem for %s", tablePath); return; }
auto handle = fs->OpenFile(tablePath, IDekiFileSystem::OpenMode::READ_BINARY);
if (!handle) { DEKI_LOG_WARNING("Simulator: %s not found", tablePath); return; }
long size = fs->GetFileSize(handle);
if (size <= 0) { fs->CloseFile(handle); DEKI_LOG_WARNING("Simulator: %s is empty", tablePath); return; }
// Persist for the process lifetime: AssetLookupTable references this buffer.
static uint8_t* s_tableData = nullptr;
if (s_tableData) delete[] s_tableData;
s_tableData = new uint8_t[static_cast<size_t>(size)];
size_t read = fs->ReadFile(handle, s_tableData, static_cast<size_t>(size));
fs->CloseFile(handle);
if (read != static_cast<size_t>(size)) {
DEKI_LOG_ERROR("Simulator: short read on %s (%zu of %ld)", tablePath, read, size);
delete[] s_tableData; s_tableData = nullptr; return;
}
if (Deki::AssetManager::Get()->LoadAssetLookupTable(s_tableData, static_cast<size_t>(size))) {
DEKI_LOG_INFO("Simulator: loaded asset_table.bin (%u entries)", Deki::AssetLookupTable::GetEntryCount());
Deki::AssetPackReader::Instance().LoadPackIndex("S:/pack_index.bin");
} else {
DEKI_LOG_ERROR("Simulator: failed to parse asset_table.bin");
}
}
int main(int argc, char* argv[]) {
(void)argc; (void)argv;
setvbuf(stdout, nullptr, _IONBF, 0); // unbuffered: logs survive a crash
// Standalone sim has no editor console; route engine logs to stdout/file.
DekiLogSystem::SetLogCallback([](LogLevel level, const std::string& msg,
const char* file, int line) {
(void)level; (void)file; (void)line;
static FILE* lf = fopen("C:/tmp/dekigame.log", "w");
if (lf) { fprintf(lf, "%s\n", msg.c_str()); fflush(lf); }
});
// Desktop HAL providers must be live before DekiEngine::Initialize() runs (it calls
// DekiMemory/DekiFileSystem::Initialize()). Set them up here in main() rather than a
// static initializer to avoid static-init-order issues with the provider singletons.
DekiMemory::SetBackend(new DesktopMemoryProvider());
DekiFileSystem::SetFileSystem(new DesktopFileSystem());
// S:/ (./storage/) is now live; load the exported asset registry so the startup
// scene + assets resolve by key. Must precede the boot prefab's startup-scene load.
LoadDeployedAssetTable();
return DekiMain();
}
#endif // SIMULATOR
#ifdef DEKI_EDITOR
// Auto-generated registration helpers
extern void DekiDesktopHAL_RegisterComponents();
extern int DekiDesktopHAL_GetAutoComponentCount();
extern const DekiComponentMeta* DekiDesktopHAL_GetAutoComponentMeta(int index);
// Track if already registered to avoid duplicates
static bool s_DesktopHALRegistered = false;
extern "C" {
/**
* @brief Ensure deki-desktop-hal module is loaded and components are registered
*/
DEKI_DESKTOP_HAL_API int DekiDesktopHAL_EnsureRegistered(void)
{
if (s_DesktopHALRegistered)
return DekiDesktopHAL_GetAutoComponentCount();
s_DesktopHALRegistered = true;
// Auto-generated: registers all Desktop HAL components with ComponentRegistry + ComponentFactory
DekiDesktopHAL_RegisterComponents();
return DekiDesktopHAL_GetAutoComponentCount();
}
// =============================================================================
// Plugin metadata (for dynamic loading compatibility)
// =============================================================================
DEKI_PLUGIN_API const char* DekiPlugin_GetName(void)
{
return "Deki Desktop HAL Module";
}
DEKI_PLUGIN_API const char* DekiPlugin_GetVersion(void)
{
#ifdef DEKI_MODULE_VERSION
return DEKI_MODULE_VERSION;
#else
return "0.0.0-dev";
#endif
}
DEKI_PLUGIN_API const char* DekiPlugin_GetReflectionJson(void)
{
return "{}";
}
DEKI_PLUGIN_API int DekiPlugin_Init(void)
{
return 0;
}
DEKI_PLUGIN_API void DekiPlugin_Shutdown(void)
{
s_DesktopHALRegistered = false;
}
DEKI_PLUGIN_API int DekiPlugin_GetComponentCount(void)
{
return DekiDesktopHAL_GetAutoComponentCount();
}
DEKI_PLUGIN_API const DekiComponentMeta* DekiPlugin_GetComponentMeta(int index)
{
return DekiDesktopHAL_GetAutoComponentMeta(index);
}
DEKI_PLUGIN_API void DekiPlugin_RegisterComponents(void)
{
DekiDesktopHAL_EnsureRegistered();
}
// =============================================================================
// Module Feature API
// =============================================================================
static const DekiModuleFeatureInfo s_Features[] = {
{"memory", "Memory", "Desktop memory backend (malloc/free)", false},
{"filesystem", "File System", "Desktop file system (fopen/fread/fwrite)", false},
};
DEKI_PLUGIN_API int DekiPlugin_GetFeatureCount(void)
{
return sizeof(s_Features) / sizeof(s_Features[0]);
}
DEKI_PLUGIN_API const DekiModuleFeatureInfo* DekiPlugin_GetFeature(int index)
{
if (index < 0 || index >= DekiPlugin_GetFeatureCount())
return nullptr;
return &s_Features[index];
}
// =============================================================================
// Module-specific feature API (for linked DLL access without name conflicts)
// =============================================================================
DEKI_DESKTOP_HAL_API const char* DekiDesktopHAL_GetName(void)
{
return "Desktop HAL";
}
DEKI_DESKTOP_HAL_API int DekiDesktopHAL_GetFeatureCount(void)
{
return DekiPlugin_GetFeatureCount();
}
DEKI_DESKTOP_HAL_API const DekiModuleFeatureInfo* DekiDesktopHAL_GetFeature(int index)
{
return DekiPlugin_GetFeature(index);
}
} // extern "C"
#endif // DEKI_EDITOR