Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Launch with JSON #67

Merged
merged 3 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*.jsonc
!examples/**/gui_definition.jsonc
!tests/**/*.jsonc
*.tuw

*.txt
!changelog.txt
Expand Down
20 changes: 16 additions & 4 deletions include/main_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ struct MenuData {
int menu_id;
};

#define EMPTY_DOCUMENT rapidjson::Document(rapidjson::kObjectType)

// Main window
class MainFrame {
private:
Expand Down Expand Up @@ -47,10 +49,20 @@ class MainFrame {
void JsonLoadFailed(const noex::string& msg) noexcept;

public:
explicit MainFrame(const rapidjson::Document& definition =
rapidjson::Document(rapidjson::kObjectType),
const rapidjson::Document& config =
rapidjson::Document(rapidjson::kObjectType)) noexcept;
explicit MainFrame(const rapidjson::Document& definition = EMPTY_DOCUMENT,
const rapidjson::Document& config = EMPTY_DOCUMENT,
const char* json_path = nullptr) noexcept {
Initialize(definition, config, json_path);
}

explicit MainFrame(const char* json_path) noexcept {
Initialize(EMPTY_DOCUMENT, EMPTY_DOCUMENT, json_path);
}

void Initialize(const rapidjson::Document& definition,
const rapidjson::Document& config,
const char* json_path) noexcept;

void UpdatePanel(unsigned definition_id) noexcept;
void OpenURL(int id) noexcept;
bool Validate() noexcept;
Expand Down
14 changes: 12 additions & 2 deletions include/noex/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,23 @@ class basic_string {
}

static const size_t npos;
size_t find(const charT c) const noexcept;
size_t find(charT c) const noexcept;
size_t find(const charT* str) const noexcept;
inline size_t find(const basic_string& str) const noexcept {
return find(str.c_str());
}

inline void push_back(const charT c) noexcept {
inline bool contains(charT c) const noexcept {
return find(c) != npos;
}
inline bool contains(const charT* str) const noexcept {
return find(str) != npos;
}
inline bool contains(const basic_string& str) const noexcept {
return find(str) != npos;
}

inline void push_back(charT c) noexcept {
this->append(&c, 1);
}

Expand Down
17 changes: 9 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "string_utils.h"
#include "tuw_constants.h"

int main_app() noexcept {
int main_app(const char* json_path = nullptr) noexcept {
#ifdef _WIN32
// Enable ANSI escape sequences on the console window.
EnableCSI();
Expand All @@ -34,7 +34,7 @@ int main_app() noexcept {
uiMainSteps();
#endif

MainFrame main_frame = MainFrame();
MainFrame main_frame = MainFrame(json_path);
uiMain();
return 0;
}
Expand Down Expand Up @@ -214,15 +214,16 @@ int main(int argc, char* argv[]) noexcept {
args.emplace_back(argv[i]);
#endif
}
char *exe_path_cstr = envuGetExecutablePath();
char *exe_dir = envuGetDirectory(exe_path_cstr);
envuSetCwd(exe_dir);
noex::string exe_path = envuStr(exe_path_cstr);
envuFree(exe_dir);

noex::string exe_path = envuStr(envuGetExecutablePath());

// Launch GUI if no args.
if (argc == 1) return main_app();
if (argc <= 1) return main_app();

// Launch GUI with a JSON path.
if (args[1].contains('.')) return main_app(args[1].c_str());

// Run as a CLI tool
const char* cmd_str = args[1].c_str();
int cmd_int = CmdToInt(cmd_str);
if (cmd_int == CMD_UNKNOWN) {
Expand Down
45 changes: 35 additions & 10 deletions src/main_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ constexpr char DEF_JSON[] = "gui_definition.json";
constexpr char DEF_JSONC[] = "gui_definition.jsonc";

// Main window
MainFrame::MainFrame(const rapidjson::Document& definition,
const rapidjson::Document& config) noexcept {
void MainFrame::Initialize(const rapidjson::Document& definition,
const rapidjson::Document& config,
const char* json_path) noexcept {
PrintFmt("%s v%s by %s\n", tuw_constants::TOOL_NAME,
tuw_constants::VERSION, tuw_constants::AUTHOR);
PrintFmt(tuw_constants::LOGO);
Expand All @@ -23,19 +24,27 @@ MainFrame::MainFrame(const rapidjson::Document& definition,
m_definition.CopyFrom(definition, m_definition.GetAllocator());
m_config.CopyFrom(config, m_config.GetAllocator());
bool ignore_external_json = false;
const char* json_path = DEF_JSON;
json_utils::JsonResult result = JSON_RESULT_OK;
if (!m_definition.IsObject() || m_definition.ObjectEmpty()) {
bool exists_external_json = true;

bool exists_external_json = true;
noex::string workdir;
if (json_path) {
char* full_json_path = envuGetFullPath(json_path);
workdir = envuStr(envuGetDirectory(full_json_path));
envuFree(full_json_path);
} else {
workdir = envuStr(envuGetDirectory(exe_path.c_str()));
// Find gui_definition.json
if (!envuFileExists(DEF_JSON)) {
json_path = DEF_JSON;
if (!envuFileExists(json_path)) {
// Find gui_definition.jsonc
if (envuFileExists(DEF_JSONC))
json_path = DEF_JSONC;
else
exists_external_json = false;
}
}
exists_external_json = envuFileExists(json_path);

json_utils::JsonResult result = JSON_RESULT_OK;
if (!m_definition.IsObject() || m_definition.ObjectEmpty()) {
ExeContainer exe;

result = exe.Read(exe_path);
Expand All @@ -59,7 +68,7 @@ MainFrame::MainFrame(const rapidjson::Document& definition,
if (!result.ok)
m_definition.SetObject();
} else {
result = { false, "gui_definition.json not found." };
result = { false, noex::string(json_path) + " not found." };
}
}
}
Expand Down Expand Up @@ -88,6 +97,22 @@ MainFrame::MainFrame(const rapidjson::Document& definition,
uiMainStep(1); // Need uiMainStep before using uiMsgBox
#endif

if (!workdir.empty()) {
// Set working directory
int ret = envuSetCwd(workdir.c_str());
if (ret != 0) {
// Failed to set CWD
PrintFmt("[LoadDefinition] Failed to set a path as CWD. (%s)\n", workdir.c_str());
}
}

{
// Show CWD
char* cwd = envuGetCwd();
PrintFmt("[LoadDefinition] CWD: %s\n", cwd);
envuFree(cwd);
}

if (ignore_external_json) {
noex::string msg = noex::string("WARNING: Using embedded JSON. ") +
json_path + " was ignored.\n";
Expand Down
2 changes: 1 addition & 1 deletion src/noex/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ template <typename charT>
const size_t basic_string<charT>::npos = static_cast<size_t>(-1);

template <typename charT>
size_t basic_string<charT>::find(const charT c) const noexcept {
size_t basic_string<charT>::find(charT c) const noexcept {
if (empty()) return npos;
const charT* p = begin();
for (; p < end(); p++) {
Expand Down
Loading