Skip to content

Commit b21275d

Browse files
committed
Bump some libraries, fixed some functions not linking and added multithreading
1 parent 2bed764 commit b21275d

10 files changed

Lines changed: 620 additions & 49 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ include_directories(
323323

324324
add_subdirectory(libultraship ${CMAKE_CURRENT_SOURCE_DIR}/libultraship)
325325
add_dependencies(${PROJECT_NAME} libultraship)
326-
target_link_libraries(${PROJECT_NAME} PRIVATE libultraship)
326+
target_link_libraries(${PROJECT_NAME} PRIVATE "$<LINK_LIBRARY:WHOLE_ARCHIVE,libultraship>")
327+
set_target_properties(${PROJECT_NAME} PROPERTIES ENABLE_EXPORTS TRUE)
327328

328329
if(${CMAKE_SYSTEM_NAME} STREQUAL "iOS")
329330
get_property(IOS_TOOLCHAIN_FILE GLOBAL PROPERTY IOS_TOOLCHAIN_FILE)

Torch

Submodule Torch updated 111 files

libultraship

Submodule libultraship updated 40 files

src/port/Engine.cpp

Lines changed: 110 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ typedef enum ExtractSteps {
175175
ES_EXTRACT_ARGS,
176176
ES_EXTRACT,
177177
ES_VERIFY,
178+
GS_COMPILE
178179
} ExtractSteps;
179180

180181
typedef enum PromptSteps {
@@ -225,7 +226,20 @@ void CheckAndCreateModFolder() {
225226
}
226227
}
227228

228-
void GameEngine::FinishInit() {
229+
void GameEngine::LoadResourceFiles() {
230+
std::unordered_map<std::string, std::string> defines = {
231+
{ "VERSION_US", "1" },
232+
{ "ENABLE_RUMBLE", "1" },
233+
{ "F3D_OLD", "1" },
234+
{ "F3D_GBI", "1" },
235+
{ "GBI_FLOATS", "1" },
236+
{ "_LANGUAGE_C", "1" },
237+
{ "_USE_MATH_DEFINES", "1" },
238+
{ "AVOID_UB", "1" }
239+
};
240+
241+
context->InitScriptSystem(defines, 1);
242+
229243
std::string romPath = Ship::Context::LocateFileAcrossAppDirs("sm64.o2r", "sm64");
230244
if (std::filesystem::exists(romPath)) {
231245
context->GetResourceManager()->GetArchiveManager()->AddArchive(romPath);
@@ -263,6 +277,20 @@ void GameEngine::FinishInit() {
263277
}
264278
}
265279

280+
auto archive = Ship::Context::GetInstance()->GetResourceManager()->GetArchiveManager();
281+
auto list = archive->GetArchives();
282+
283+
for (const auto& entry : *list) {
284+
const auto& info = entry->GetManifest();
285+
if (info.Main.empty()) {
286+
continue;
287+
}
288+
289+
this->totalScripts++;
290+
}
291+
}
292+
293+
void GameEngine::FinishInit() {
266294
#if (_DEBUG)
267295
auto defaultLogLevel = spdlog::level::debug;
268296
#else
@@ -369,7 +397,7 @@ void GameEngine::FinishInit() {
369397
DevConsole_Init();
370398
PortEnhancements_Init();
371399
ShipInit::InitAll();
372-
LoadScripts();
400+
context->GetScriptSystem()->LoadAll();
373401
}
374402

375403
void GameEngine::RunExtract(int argc, char* argv[]) {
@@ -399,7 +427,7 @@ void GameEngine::RunExtract(int argc, char* argv[]) {
399427
PromptSteps promptStep = PS_FILE_CHECK;
400428
std::atomic<bool> extracting = false;
401429
std::atomic<size_t> extractCount{ 0 }, totalExtract{ 0 };
402-
430+
std::atomic<size_t> compileCount{ 0 };
403431
std::string installPath = Ship::Context::GetAppBundlePath();
404432
std::string file;
405433

@@ -436,7 +464,7 @@ void GameEngine::RunExtract(int argc, char* argv[]) {
436464

437465
std::shared_ptr<BS::thread_pool> threadPool = std::make_shared<BS::thread_pool>(1);
438466
while (!extractDone) {
439-
if (GhostshipGui::PopupsQueued() > 0 || extracting) {
467+
if (GhostshipGui::PopupsQueued() > 0 || extracting || totalScripts > 0) {
440468
goto render;
441469
}
442470
switch (extractStep) {
@@ -701,7 +729,24 @@ void GameEngine::RunExtract(int argc, char* argv[]) {
701729
exit(0);
702730
});
703731
}
704-
extractDone = true;
732+
733+
extractStep = GS_COMPILE;
734+
continue;
735+
}
736+
case GS_COMPILE: {
737+
LoadResourceFiles();
738+
threadPool->submit_task([&]() -> void {
739+
auto scripting = Ship::Context::GetInstance()->GetScriptSystem();
740+
auto pre = [&](const std::shared_ptr<Ship::Archive>& archive) {
741+
auto& info = archive->GetManifest();
742+
file = info.Name;
743+
};
744+
auto post = [&]() {
745+
compileCount++;
746+
};
747+
scripting->CompileAll(pre, post);
748+
extractDone = true;
749+
});
705750
continue;
706751
}
707752
default:
@@ -754,6 +799,32 @@ void GameEngine::RunExtract(int argc, char* argv[]) {
754799
ImGui::PopStyleColor(3);
755800
ImGui::PopStyleVar(2);
756801
}
802+
803+
if(totalScripts > 0 && !ImGui::IsPopupOpen("Ghostship")) {
804+
ImGui::OpenPopup("Ghostship");
805+
}
806+
807+
if(totalScripts > 0) {
808+
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 3.0f);
809+
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(10.0f, 8.0f));
810+
auto color = UIWidgets::ColorValues.at(THEME_COLOR);
811+
ImGui::PushStyleColor(ImGuiCol_FrameBg, ImVec4(color.x, color.y, color.z, 0.6f));
812+
ImGui::PushStyleColor(ImGuiCol_PlotHistogram, ImVec4(color.x, color.y, color.z, 1.0f));
813+
ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(0.0f, 0.0f, 0.0f, 0.3f));
814+
if (ImGui::BeginPopupModal("Ghostship", NULL,
815+
ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize |
816+
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar |
817+
ImGuiWindowFlags_NoSavedSettings)) {
818+
float progress = (totalScripts > 0.0f ? (float)compileCount / (float)totalScripts : 0) * 100.0f;
819+
ImGui::Text("Loading %s...%s", file.c_str(),
820+
roundf(progress) == 100.0f ? " Done. Finishing up." : "");
821+
std::string overlay = compileCount > 0 ? fmt::format("{:.0f}%", progress) : "Starting Up";
822+
ImGui::ProgressBar(progress / 100.0f, ImVec2(600.0f, 50.0f), overlay.c_str());
823+
ImGui::EndPopup();
824+
}
825+
ImGui::PopStyleColor(3);
826+
ImGui::PopStyleVar(2);
827+
}
757828
gui->EndDraw();
758829
gsFast3dWindow->EndFrame();
759830
ImGui::PopStyleColor(2);
@@ -825,16 +896,42 @@ void GameEngine::ScaleImGui() {
825896

826897
void GameEngine::LoadScripts() {
827898
auto scripting = Ship::Context::GetInstance()->GetScriptSystem();
899+
Notification::Emit({ .message = "Loading mods this may take a while...", .remainingTime = (totalScripts * 5.0f), .mute = true });
900+
static std::shared_ptr<BS::thread_pool> mThreadPool = std::make_shared<BS::thread_pool>(1);
901+
mThreadPool->submit_task([&]() -> void {
902+
auto currentScriptName = std::make_shared<std::string>("");
903+
auto currentScript = std::make_shared<std::atomic<int>>(0);
904+
auto notification = std::make_shared<Notification::Options>();
905+
notification->mute = true;
906+
notification->remainingTime = 7.0f;
907+
try {
908+
scripting->CompileAll([&](const std::shared_ptr<Ship::Archive>& archive) {
909+
if (!archive) return;
910+
911+
auto& info = archive->GetManifest();
912+
*currentScriptName = info.Name;
913+
int scriptNum = ++(*currentScript);
914+
915+
notification->message = fmt::format("Loading {} ({}/{})", *currentScriptName, scriptNum, this->totalScripts);
916+
917+
Notification::Emit(*notification);
918+
});
919+
} catch (std::exception& e) {
920+
notification->message = fmt::format("Failed to build {} ({}/{})", *currentScriptName, (*currentScript + 1), this->totalScripts);
921+
SPDLOG_ERROR("Failed to build script {}: {}", *currentScriptName, e.what());
922+
Notification::Emit(*notification);
923+
}
828924

829-
try {
830-
scripting->LoadAll();
831-
Notification::Emit({ .message = "Loaded all scripts", .remainingTime = 7.0f });
832-
} catch (std::exception& e) {
833-
SPDLOG_ERROR("Failed to load scripts: {}", e.what());
834-
Notification::Emit({ .message = "Failed to load scripts, check log for details",
925+
try {
926+
context->GetScriptSystem()->LoadAll();
927+
Notification::Emit({ .message = "Finished loading mods!", .remainingTime = 5.0f, .mute = true });
928+
} catch (std::exception& e) {
929+
SPDLOG_ERROR("Failed to load scripts: {}", e.what());
930+
Notification::Emit({ .message = "Failed to load some mods, check logs for details.",
835931
.messageColor = ImVec4(1.0f, 0.5f, 0.5f, 1.0f),
836-
.remainingTime = 7.0f });
837-
}
932+
.remainingTime = 5.0f, .mute = true });
933+
}
934+
});
838935
}
839936

840937
void GameEngine::Create(int argc, char* argv[]) {

src/port/Engine.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class GameEngine {
3636
public:
3737
static GameEngine* Instance;
3838

39+
int totalScripts = 0;
40+
3941
ImFont *fontStandard;
4042
ImFont *fontStandardLarger;
4143
ImFont *fontStandardLargest;
@@ -56,9 +58,10 @@ class GameEngine {
5658
std::unordered_map<std::string, std::vector<uint8_t>>* dictionary;
5759

5860
GameEngine();
59-
static void LoadScripts();
6061
static void Create(int argc, char* argv[]);
6162
static bool GenAssetFile(bool exitOnFail = true);
63+
void LoadScripts();
64+
void LoadResourceFiles();
6265
void RunExtract(int argc, char* argv[]);
6366
void AudioInit();
6467
void FinishInit();

src/port/api/ui.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,26 @@ extern "C" void C_AddWidget(const char* sidebar, int column, const char* label,
6767
// 4. Unpack the Options Variant based on the type
6868
switch (config->type) {
6969
// --- COMBOBOXES ---
70-
case C_WIDGET_COMBOBOX:
7170
case C_WIDGET_CVAR_COMBOBOX:
71+
case C_WIDGET_COMBOBOX: {
72+
auto opt = UIWidgets::ComboboxOptions()
73+
.Tooltip(config->opts.combo.tooltip ? config->opts.combo.tooltip : "")
74+
.DefaultIndex(config->opts.combo.default_index)
75+
.ComponentAlignment(MapAlign(config->opts.combo.alignment))
76+
.LabelPosition(MapLabel(config->opts.combo.label_pos));
77+
if (config->opts.combo.map) {
78+
// Assuming the map is an array of C_ComboboxOption with a known size (e.g., 10)
79+
std::unordered_map<int32_t, const char*> comboMap;
80+
C_ComboboxOption* iter = config->opts.combo.map;
81+
while (iter->value) { // Assuming null-terminated array
82+
comboMap[iter->id] = iter->value;
83+
iter++;
84+
}
85+
opt.ComboMap(comboMap);
86+
}
87+
widget.Options(opt);
88+
break;
89+
}
7290
case C_WIDGET_AUDIO_BACKEND:
7391
case C_WIDGET_VIDEO_BACKEND: {
7492
auto opt = UIWidgets::ComboboxOptions()

src/port/api/ui.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,19 @@ typedef enum { C_LABEL_NEAR, C_LABEL_FAR } C_LabelPos;
3939
// ---------------------------------------------------------
4040
// 2. Specific Options Structs
4141
// ---------------------------------------------------------
42+
43+
typedef struct {
44+
int id; // ID to identify which combo map to use in C++
45+
const char* value;
46+
} C_ComboboxOption;
47+
4248
typedef struct {
4349
const char* tooltip;
4450
int default_index;
4551
int combo_map_id; // Pass an ID so C++ knows which std::map to inject
4652
C_ComponentAlign alignment;
4753
C_LabelPos label_pos;
54+
C_ComboboxOption* map;
4855
} C_ComboboxOpts;
4956

5057
typedef struct {

src/port/ui/GhostshipMenuDevTools.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,6 @@ void GhostshipMenu::AddMenuDevTools() {
108108
.HideInSearch(true)
109109
.Options(WindowButtonOptions().Tooltip("Enables the separate Save Editor Window."));
110110

111-
path.sidebarName = "Script Manager";
112-
AddSidebarEntry("Dev Tools", path.sidebarName, 2);
113-
AddWidget(path, "Reload Scripts", WIDGET_BUTTON)
114-
.Options(ButtonOptions().Tooltip("Reloads all scripts from disk.").Color(Colors::Orange))
115-
.Callback([](WidgetInfo& info) {
116-
Ship::Context::GetInstance()->GetScriptSystem()->UnloadAll();
117-
GameEngine::LoadScripts();
118-
});
119-
120111
// Console
121112
path.sidebarName = "Console";
122113
AddSidebarEntry("Dev Tools", path.sidebarName, 1);
@@ -149,7 +140,12 @@ void GhostshipMenu::AddModMenu() {
149140
WidgetPath path = { "Mods", "General", SECTION_COLUMN_1 };
150141

151142
AddSidebarEntry(path.sectionName, path.sidebarName, 1);
152-
AddWidget(path, "Mod Settings", WIDGET_TEXT);
143+
AddWidget(path, "Reload Scripts", WIDGET_BUTTON)
144+
.Options(ButtonOptions().Tooltip("Reloads all scripts from disk.").Color(Colors::Orange))
145+
.Callback([](WidgetInfo& info) {
146+
Ship::Context::GetInstance()->GetScriptSystem()->UnloadAll();
147+
GameEngine::Instance->LoadScripts();
148+
});
153149
};
154150

155151
} // namespace GhostshipGui

0 commit comments

Comments
 (0)