Skip to content

Commit

Permalink
Refactored command-line argument parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
pigpigyyy committed Jan 15, 2025
1 parent 4d54f6a commit aae59b2
Show file tree
Hide file tree
Showing 18 changed files with 358 additions and 57 deletions.
8 changes: 4 additions & 4 deletions Assets/Script/Lib/Dora/en/Content.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ local record Content
-- An array of directories to search for resource files.
searchPaths: {string}

-- The path to the directory containing read-only resources.
const assetPath: string
-- The path to the directory containing read-only resources. Can only be altered by the user on platform Windows, MacOS and Linux.
assetPath: string

-- The path to the directory where files can be written.
const writablePath: string
-- The path to the directory where files can be written. Can only be altered by the user on platform Windows, MacOS and Linux.
writablePath: string

-- Loads the content of the file with the specified filename.
-- @param filename (string) The name of the file to load.
Expand Down
8 changes: 4 additions & 4 deletions Assets/Script/Lib/Dora/en/Dora.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3473,11 +3473,11 @@ class Content {
/** An array of directories to search for resource files. */
searchPaths: string[];

/** The path to the directory containing read-only resources. */
readonly assetPath: string;
/** The path to the directory containing read-only resources. Can only be altered by the user on platform Windows, MacOS and Linux. */
assetPath: string;

/** The path to the directory where files can be written. */
readonly writablePath: string;
/** The path to the directory where files can be written. Can only be altered by the user on platform Windows, MacOS and Linux. */
writablePath: string;

/**
* Loads the content of the file with the specified filename.
Expand Down
8 changes: 4 additions & 4 deletions Assets/Script/Lib/Dora/zh-Hans/Content.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ local record Content
-- 用于搜索资源文件的文件路径列表。
searchPaths: {string}

-- 游戏引擎只读资源所在目录的路径。
const assetPath: string
-- 游戏引擎只读资源所在目录的路径。只有在平台 WindowsMacOSLinux 上能被设置为新路径。
assetPath: string

-- 游戏引擎可以做写入操作的文件路径。
const writablePath: string
-- 游戏引擎可以做写入操作的文件路径。只有在平台 WindowsMacOSLinux 上能被设置为新路径。
writablePath: string

-- 以阻塞的方式读取文件内容。
-- @param filename (string) 要读取的文件名。
Expand Down
8 changes: 4 additions & 4 deletions Assets/Script/Lib/Dora/zh-Hans/Dora.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3470,11 +3470,11 @@ class Content {
/** 用于搜索资源文件的目录数组。 */
searchPaths: string[];

/** 包含只读资源的目录的路径。 */
readonly assetPath: string;
/** 包含只读资源的目录的路径。只有在平台 Windows、MacOS 和 Linux 上能被设置为新路径。 */
assetPath: string;

/** 可以写入文件的目录的路径。 */
readonly writablePath: string;
/** 可以写入文件的目录的路径。只有在平台 Windows、MacOS 和 Linux 上能被设置为新路径。 */
writablePath: string;

/**
* 加载具有指定文件名的文件的内容。
Expand Down
5 changes: 2 additions & 3 deletions Source/Basic/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,8 @@ bool Application::isAlwaysOnTop() const noexcept {
}

// This function runs in main (render) thread, and do render work
int Application::run(int argc, const char* const argv[]) {
int Application::run() {
Application::setSeed(s_cast<uint32_t>(std::time(nullptr)));
SharedContent.init(argc, argv);

if (SDL_Init(SDL_INIT_GAMECONTROLLER) != 0) {
Error("SDL failed to initialize! {}", SDL_GetError());
Expand Down Expand Up @@ -799,7 +798,7 @@ NS_DORA_END
// Entry functions needed by SDL2
#if BX_PLATFORM_OSX || BX_PLATFORM_ANDROID || BX_PLATFORM_IOS || BX_PLATFORM_LINUX
extern "C" int main(int argc, char* argv[]) {
return SharedApplication.run(argc, argv);
return SharedApplication.run();
}
#endif // BX_PLATFORM_OSX || BX_PLATFORM_ANDROID || BX_PLATFORM_IOS || BX_PLATFORM_LINUX

Expand Down
2 changes: 1 addition & 1 deletion Source/Basic/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Application : public NonCopyable {
PROPERTY_BOOL(AlwaysOnTop);
SDLEventHandler eventHandler;
QuitHandler quitHandler;
int run(int argc, const char* const argv[]);
int run();
void shutdown();
void invokeInRender(const std::function<void()>& func);
void invokeInLogic(const std::function<void()>& func);
Expand Down
65 changes: 41 additions & 24 deletions Source/Basic/Content.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,48 @@ static void trimTrailingSlashes(std::string& str) {
}
}

void Content::init(int argc, const char* const argv[]) {
for (int i = 0; i < argc; i++) {
if (argv[i] == "--asset"sv && i + 1 < argc) {
std::string assetPath = argv[++i];
std::error_code err;
std::string fullPath = fs::absolute(assetPath, err).lexically_normal().string();
if (err) {
Error("got invalid asset path \"{}\"", assetPath);
}
if (fs::exists(fullPath, err)) {
_assetPath = fullPath;
trimTrailingSlashes(_assetPath);
} else {
Error("got invalid asset path \"{}\"", assetPath);
}
}
void Content::setAssetPath(String assetPath) {
#if BX_PLATFORM_OSX || BX_PLATFORM_WINDOWS || BX_PLATFORM_LINUX
std::error_code err;
std::string fullPath = fs::absolute(assetPath.toString(), err).lexically_normal().string();
if (err) {
Issue("got invalid asset path \"{}\"", assetPath.toString());
}
if (fs::exists(fullPath, err)) {
_assetPath = fullPath;
trimTrailingSlashes(_assetPath);
} else {
Issue("got invalid asset path \"{}\"", assetPath.toString());
}
#else
Issue("changing asset path is not supported on platform \"{}\"", SharedApplication.getPlatform().toString());
#endif
}

const std::string& Content::getAssetPath() const noexcept {
return _assetPath;
}

void Content::setWritablePath(String writablePath) {
#if BX_PLATFORM_OSX || BX_PLATFORM_WINDOWS || BX_PLATFORM_LINUX
std::error_code err;
std::string fullPath = fs::absolute(writablePath.toString(), err).lexically_normal().string();
if (err) {
Issue("got invalid writable path \"{}\"", writablePath.toString());
}
if (fs::exists(fullPath, err)) {
_writablePath = fullPath;
trimTrailingSlashes(_writablePath);
} else {
Issue("got invalid writable path \"{}\"", writablePath.toString());
}
#else
Issue("changing writable path is not supported on platform \"{}\"", SharedApplication.getPlatform().toString());
#endif
}

const std::string& Content::getWritablePath() const noexcept {
return _writablePath;
}

Async* Content::getThread() const noexcept {
Expand Down Expand Up @@ -228,14 +253,6 @@ bool Content::visitDir(String path, const std::function<bool(String, String)>& f
return visit(path);
}

const std::string& Content::getAssetPath() const noexcept {
return _assetPath;
}

const std::string& Content::getWritablePath() const noexcept {
return _writablePath;
}

static std::tuple<std::string, std::string> splitDirectoryAndFilename(const std::string& filePath) {
std::string file = filePath;
std::string path;
Expand Down
5 changes: 2 additions & 3 deletions Source/Basic/Content.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ class Async;

class Content : public NonCopyable {
public:
PROPERTY_READONLY_CREF(std::string, AssetPath);
PROPERTY_READONLY_CREF(std::string, WritablePath);
PROPERTY_STRING(AssetPath);
PROPERTY_STRING(WritablePath);
PROPERTY_CREF(std::vector<std::string>, SearchPaths);
PROPERTY_READONLY(Async*, Thread);
virtual ~Content();
void init(int argc, const char* const argv[]);
bool exist(String filename);
bool isFolder(String path);
bool isAbsolutePath(String strPath);
Expand Down
Loading

0 comments on commit aae59b2

Please sign in to comment.