Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : #include "CmdLineArgs.h" 20 : 21 : #include "lib/sysdep/sysdep.h" 22 : #include "scriptinterface/Object.h" 23 : #include "scriptinterface/Conversions.h" 24 : #include "scriptinterface/Request.h" 25 : 26 : #include <algorithm> 27 : #include <js/CallArgs.h> 28 : #include <js/RootingAPI.h> 29 : #include <js/TypeDecls.h> 30 : #include <js/Value.h> 31 : #include <string> 32 : #include <string_view> 33 : #include <unordered_map> 34 : 35 : CmdLineArgs g_CmdLineArgs; 36 : 37 : namespace 38 : { 39 : 40 : // Simple matcher for elements of the arguments container. 41 145 : class IsKeyEqualTo 42 : { 43 : public: 44 7 : IsKeyEqualTo(const CStr& value) : m_Value(value) {} 45 : 46 51 : bool operator()(const std::pair<CStr, CStr>& p) const 47 : { 48 32 : return p.first == m_Value; 49 : } 50 : 51 : private: 52 : const CStr m_Value; 53 : }; 54 : 55 : } // namespace 56 : 57 8 : CmdLineArgs::CmdLineArgs(const std::span<const char* const> argv) 58 : { 59 8 : if (argv.empty()) 60 1 : return; 61 : 62 7 : std::string arg0(argv[0]); 63 : // avoid OsPath complaining about mixing both types of separators, 64 : // which happens when running in the VC2010 debugger 65 7 : std::replace(arg0.begin(), arg0.end(), '/', SYS_DIR_SEP); 66 14 : m_Arg0 = arg0; 67 : 68 : // Implicit conversion from const char* to std::string_view. 69 26 : for (const std::string_view arg : argv.subspan(1)) 70 : { 71 : // Only accept arguments that start with '-' 72 19 : if (arg[0] != '-') 73 : { 74 5 : m_ArgsWithoutName.emplace_back(arg); 75 5 : continue; 76 : } 77 : 78 : // Allow -arg and --arg 79 23 : const std::string_view afterDashes = arg.substr(arg[1] == '-' ? 2 : 1); 80 : 81 : // Check for "-arg=value" 82 14 : const std::string_view::iterator eq = 83 14 : std::find(afterDashes.begin(), afterDashes.end(), '='); 84 : 85 24 : CStr value = (eq != afterDashes.end()) ? CStr{eq + 1, afterDashes.end()} : CStr{}; 86 : 87 42 : m_Args.emplace_back(CStr(afterDashes.begin(), eq), std::move(value)); 88 14 : } 89 7 : } 90 : 91 10 : bool CmdLineArgs::Has(const CStr& name) const 92 : { 93 10 : return std::any_of(m_Args.begin(), m_Args.end(), IsKeyEqualTo(name)); 94 : } 95 : 96 8 : CStr CmdLineArgs::Get(const CStr& name) const 97 : { 98 8 : ArgsT::const_iterator it = std::find_if(m_Args.begin(), m_Args.end(), IsKeyEqualTo(name)); 99 15 : return it != m_Args.end() ? it->second : ""; 100 : } 101 : 102 3 : std::vector<CStr> CmdLineArgs::GetMultiple(const CStr& name) const 103 : { 104 3 : std::vector<CStr> values; 105 3 : ArgsT::const_iterator it = m_Args.begin(); 106 10 : while ((it = std::find_if(it, m_Args.end(), IsKeyEqualTo(name))) != m_Args.end()) 107 : { 108 4 : values.push_back(it->second); 109 : // Start searching from the next one in the next iteration 110 7 : ++it; 111 : } 112 3 : return values; 113 0 : } 114 : 115 3 : OsPath CmdLineArgs::GetArg0() const 116 : { 117 3 : return m_Arg0; 118 : } 119 : 120 0 : const CmdLineArgs::ArgsT& CmdLineArgs::GetArgs() const 121 : { 122 0 : return m_Args; 123 : } 124 : 125 1 : std::vector<CStr> CmdLineArgs::GetArgsWithoutName() const 126 : { 127 1 : return m_ArgsWithoutName; 128 : } 129 : 130 0 : template<> void Script::ToJSVal<CmdLineArgs>(const Script::Request& rq, JS::MutableHandleValue ret, const CmdLineArgs& val) 131 : { 132 0 : if (!Script::CreateObject(rq, ret)) 133 0 : return; 134 : 135 0 : std::unordered_map<CStr, std::vector<CStr>> args; 136 0 : for (const std::pair<CStr, CStr>& arg : val.GetArgs()) 137 0 : args.emplace(arg.first, std::vector<CStr>{}).first->second.emplace_back(arg.second); 138 : 139 0 : JS::RootedValue argVal(rq.cx); 140 0 : for (const auto& arg : args) 141 : { 142 0 : if (arg.second.size() == 1) 143 : { 144 0 : if (arg.second[0].empty()) 145 0 : argVal = JS::UndefinedHandleValue; 146 : else 147 0 : Script::ToJSVal(rq, &argVal, arg.second[0]); 148 0 : Script::SetProperty(rq, ret, arg.first.c_str(), argVal); 149 : } 150 : else 151 : { 152 0 : Script::ToJSVal(rq, &argVal, arg.second); 153 0 : Script::SetProperty(rq, ret, arg.first.c_str(), argVal); 154 : } 155 : } 156 0 : }