2025-07-23 10:19:42 -07:00
|
|
|
/* Copyright (C) 2025 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2009-04-18 10:00:33 -07:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2009-04-18 10:00:33 -07:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2009-04-18 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2006-12-09 06:39:52 -08:00
|
|
|
#include "precompiled.h"
|
2011-05-04 07:40:14 -07:00
|
|
|
#include "CmdLineArgs.h"
|
2011-05-04 05:16:51 -07:00
|
|
|
|
2011-05-06 11:45:30 -07:00
|
|
|
#include "lib/sysdep/sysdep.h"
|
2024-12-01 01:32:32 -08:00
|
|
|
#include "scriptinterface/Object.h"
|
|
|
|
|
#include "scriptinterface/ScriptConversions.h"
|
2025-07-23 10:19:42 -07:00
|
|
|
#include "scriptinterface/ScriptRequest.h"
|
2011-05-06 11:45:30 -07:00
|
|
|
|
2022-10-04 12:08:39 -07:00
|
|
|
#include <algorithm>
|
2025-07-23 10:19:42 -07:00
|
|
|
#include <js/CallArgs.h>
|
|
|
|
|
#include <js/RootingAPI.h>
|
|
|
|
|
#include <js/TypeDecls.h>
|
|
|
|
|
#include <js/Value.h>
|
|
|
|
|
#include <string>
|
2022-10-04 12:08:39 -07:00
|
|
|
#include <string_view>
|
2024-12-01 01:32:32 -08:00
|
|
|
#include <unordered_map>
|
2022-10-04 12:08:39 -07:00
|
|
|
|
2021-05-16 06:50:05 -07:00
|
|
|
CmdLineArgs g_CmdLineArgs;
|
|
|
|
|
|
2018-04-14 18:46:28 -07:00
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// Simple matcher for elements of the arguments container.
|
|
|
|
|
class IsKeyEqualTo
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
IsKeyEqualTo(const CStr& value) : m_Value(value) {}
|
|
|
|
|
|
|
|
|
|
bool operator()(const std::pair<CStr, CStr>& p) const
|
|
|
|
|
{
|
|
|
|
|
return p.first == m_Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const CStr m_Value;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2025-08-13 08:20:49 -07:00
|
|
|
CmdLineArgs::CmdLineArgs(const std::span<const char* const> argv)
|
2006-12-09 06:39:52 -08:00
|
|
|
{
|
2022-10-04 12:08:39 -07:00
|
|
|
if (argv.empty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
std::string arg0(argv[0]);
|
|
|
|
|
// avoid OsPath complaining about mixing both types of separators,
|
|
|
|
|
// which happens when running in the VC2010 debugger
|
|
|
|
|
std::replace(arg0.begin(), arg0.end(), '/', SYS_DIR_SEP);
|
|
|
|
|
m_Arg0 = arg0;
|
2006-12-09 06:39:52 -08:00
|
|
|
|
2022-10-04 12:08:39 -07:00
|
|
|
// Implicit conversion from const char* to std::string_view.
|
|
|
|
|
for (const std::string_view arg : argv.subspan(1))
|
2006-12-09 06:39:52 -08:00
|
|
|
{
|
|
|
|
|
// Only accept arguments that start with '-'
|
2022-10-04 12:08:39 -07:00
|
|
|
if (arg[0] != '-')
|
2018-04-14 18:46:28 -07:00
|
|
|
{
|
2022-10-04 12:08:39 -07:00
|
|
|
m_ArgsWithoutName.emplace_back(arg);
|
2006-12-09 06:39:52 -08:00
|
|
|
continue;
|
2018-04-14 18:46:28 -07:00
|
|
|
}
|
2006-12-09 06:39:52 -08:00
|
|
|
|
2017-09-30 09:12:18 -07:00
|
|
|
// Allow -arg and --arg
|
2022-10-04 12:08:39 -07:00
|
|
|
const std::string_view afterDashes = arg.substr(arg[1] == '-' ? 2 : 1);
|
2006-12-09 06:39:52 -08:00
|
|
|
|
|
|
|
|
// Check for "-arg=value"
|
2022-10-04 12:08:39 -07:00
|
|
|
const std::string_view::iterator eq =
|
|
|
|
|
std::find(afterDashes.begin(), afterDashes.end(), '=');
|
|
|
|
|
|
|
|
|
|
CStr value = (eq != afterDashes.end()) ? CStr{eq + 1, afterDashes.end()} : CStr{};
|
2006-12-09 06:39:52 -08:00
|
|
|
|
2022-10-04 12:08:39 -07:00
|
|
|
m_Args.emplace_back(CStr(afterDashes.begin(), eq), std::move(value));
|
2006-12-09 06:39:52 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-14 18:46:28 -07:00
|
|
|
bool CmdLineArgs::Has(const CStr& name) const
|
2006-12-09 06:39:52 -08:00
|
|
|
{
|
2018-04-14 18:46:28 -07:00
|
|
|
return std::any_of(m_Args.begin(), m_Args.end(), IsKeyEqualTo(name));
|
2006-12-09 06:39:52 -08:00
|
|
|
}
|
|
|
|
|
|
2018-04-14 18:46:28 -07:00
|
|
|
CStr CmdLineArgs::Get(const CStr& name) const
|
2006-12-09 06:39:52 -08:00
|
|
|
{
|
2018-04-14 18:46:28 -07:00
|
|
|
ArgsT::const_iterator it = std::find_if(m_Args.begin(), m_Args.end(), IsKeyEqualTo(name));
|
|
|
|
|
return it != m_Args.end() ? it->second : "";
|
2006-12-09 06:39:52 -08:00
|
|
|
}
|
|
|
|
|
|
2018-04-14 18:46:28 -07:00
|
|
|
std::vector<CStr> CmdLineArgs::GetMultiple(const CStr& name) const
|
2006-12-09 06:39:52 -08:00
|
|
|
{
|
|
|
|
|
std::vector<CStr> values;
|
|
|
|
|
ArgsT::const_iterator it = m_Args.begin();
|
2018-04-14 18:46:28 -07:00
|
|
|
while ((it = std::find_if(it, m_Args.end(), IsKeyEqualTo(name))) != m_Args.end())
|
2006-12-09 06:39:52 -08:00
|
|
|
{
|
|
|
|
|
values.push_back(it->second);
|
2018-04-14 18:46:28 -07:00
|
|
|
// Start searching from the next one in the next iteration
|
|
|
|
|
++it;
|
2006-12-09 06:39:52 -08:00
|
|
|
}
|
|
|
|
|
return values;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-04 05:16:51 -07:00
|
|
|
OsPath CmdLineArgs::GetArg0() const
|
2006-12-09 06:39:52 -08:00
|
|
|
{
|
|
|
|
|
return m_Arg0;
|
|
|
|
|
}
|
2018-04-14 18:46:28 -07:00
|
|
|
|
2024-12-16 09:59:47 -08:00
|
|
|
const CmdLineArgs::ArgsT& CmdLineArgs::GetArgs() const
|
|
|
|
|
{
|
|
|
|
|
return m_Args;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-14 18:46:28 -07:00
|
|
|
std::vector<CStr> CmdLineArgs::GetArgsWithoutName() const
|
|
|
|
|
{
|
|
|
|
|
return m_ArgsWithoutName;
|
|
|
|
|
}
|
2024-12-01 01:32:32 -08:00
|
|
|
|
|
|
|
|
template<> void Script::ToJSVal<CmdLineArgs>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CmdLineArgs& val)
|
|
|
|
|
{
|
|
|
|
|
if (!Script::CreateObject(rq, ret))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
std::unordered_map<CStr, std::vector<CStr>> args;
|
2024-12-16 09:59:47 -08:00
|
|
|
for (const std::pair<CStr, CStr>& arg : val.GetArgs())
|
2024-12-01 01:32:32 -08:00
|
|
|
args.emplace(arg.first, std::vector<CStr>{}).first->second.emplace_back(arg.second);
|
|
|
|
|
|
|
|
|
|
JS::RootedValue argVal(rq.cx);
|
|
|
|
|
for (const auto& arg : args)
|
|
|
|
|
{
|
|
|
|
|
if (arg.second.size() == 1)
|
|
|
|
|
{
|
|
|
|
|
if (arg.second[0].empty())
|
|
|
|
|
argVal = JS::UndefinedHandleValue;
|
|
|
|
|
else
|
|
|
|
|
Script::ToJSVal(rq, &argVal, arg.second[0]);
|
|
|
|
|
Script::SetProperty(rq, ret, arg.first.c_str(), argVal);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Script::ToJSVal(rq, &argVal, arg.second);
|
|
|
|
|
Script::SetProperty(rq, ret, arg.first.c_str(), argVal);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|