2021SC@SDUSC
这一次我们来观察CommandLine模块的代码,顾名思义这一模块的内容是对命令行的命令进行解析。
首先来看CommandLine.h:
struct OptionsData
{
/// <summary>
/// The command line.
/// </summary>
const Char* CmdLine = nullptr;
/// <summary>
/// -widowed (use windowed mode)
/// </summary>
Nullable<bool> Windowed;
/// <summary>
/// -fullscreen (use fullscreen)
/// </summary>
Nullable<bool> Fullscreen;
/// <summary>
/// -vsync (enable vertical synchronization)
/// </summary>
Nullable<bool> VSync;
/// <summary>
/// -novsync (disable vertical synchronization)
/// </summary>
Nullable<bool> NoVSync;
/// <summary>
/// -nolog (disable output log file)
/// </summary>
Nullable<bool> NoLog;
/// <summary>
/// -std (redirect log to standard output)
/// </summary>
Nullable<bool> Std;
#if !BUILD_RELEASE
/// <summary>
/// -debug !ip:port! (Mono debugger address)
/// </summary>
Nullable<String> DebuggerAddress;
/// <summary>
/// -debugwait (instructs Mono debugger to wait for client attach for 5 seconds)
/// </summary>
Nullable<bool> WaitForDebugger;
稍微解释上面定义的变量:
1.CmdLine:即命令行,是个指针
2.Windowed:是否在使用窗口模式
3.Fullscreen:是否使用全屏模式
4.VSync:是否使用垂直同步
5.NoLog:是否使用输出日志文件
6.Std:是否重定向日志至标准输出
显然都是和设置、模式有关的变量。
Nullable<bool> D3D12;//DirectX 12状态的模式使用
Nullable<bool> D3D11;//DirectX 11
Nullable<bool> D3D10;//DirectX 10
Nullable<bool> Null;//
Nullable<bool> Vulkan;//
Nullable<bool> NVIDIA;//
Nullable<bool> AMD;//
Nullable<bool> Intel;//
Nullable<bool> MonoLog;//
Nullable<bool> Mute;//
Nullable<bool> LowDPI;//
这部分的变量大多与硬件和软件兼容配置有关。对于游戏引擎来说,需要考虑用户电脑配置能力并适配大多数用户。
String Project;//项目路径
Nullable<bool> NewProject;//创建新项目文件
Nullable<bool> GenProjectFiles;//创建项目脚本文件,与上面不同是创建脚本文件
Nullable<bool> ClearCache;//情况项目缓存
Nullable<bool> ClearCookerCache;//清空游戏缓存
Nullable<String> Build;//运行游戏项目,对游戏项目进行操作
Nullable<bool> SkipCompile;//跳过脚本编译
Nullable<bool> ShaderDebug;//渲染模式debug
Nullable<String> Play;//场景游玩
到了这一部分就是我们常见的游戏引擎命令。
/// <summary>
/// Parses the input command line.
/// </summary>
/// <param name="cmdLine">The command line.</param>
/// <returns>True if failed, otherwise false.</returns>
static bool Parse(const Char* cmdLine);
Parse是非常重要的方法,它能够解析命令行输入的命令语义,数据类型是bool类型,值得注意的是当成功解析时返回的是false。
接下来我们进入CommandLine.cpp看代码的具体实现:
parseArg()函数,在后面parse()函数中会调用该函数
bool ParseArg(Char* ptr, Char*& start, Char*& end)
{
// Skip leading whitespaces
while (*ptr && StringUtils::IsWhitespace(*ptr))
ptr++;
bool isInComma = false;
bool isUglyQuote = false;
start = ptr;
while (*ptr)
{
Char c = *ptr;
if (StringUtils::IsWhitespace(c) && !isInComma)
{
// End
end = ptr;
return false;
}
else if (c == '\"' || c == '\'')
{
if (isInComma)
{
// End comma
end = ptr;
if (isUglyQuote)
{
ptr += 2;
end -= 2;
}
return false;
}
else
{
// Special case (eg. Visual Studio Code adds soo many quotes to the args with spaces)
isUglyQuote = StringUtils::Compare(ptr, TEXT("\"\\\\\""), 4) == 0;
if (isUglyQuote)
{
ptr += 3;
}
// Start comma
isInComma = true;
start = ptr + 1;
}
}
ptr++;
}
return true;
}
其函数逻辑十分清晰,对参数ptr指针进行遍历,若为空格则指针加一,相当于跳过空格。
isInComma和isUglyQuote这两个变量作为语法判断的变量,初始化为false。
对ptr再次遍历,对指针是否为逗号或引用进行判断,并相应修改,对ptr指针的内容进行修改,修改成机器能够理解的字符串。