| 167 | } |
| 168 | |
| 169 | template <typename T> Object* RegisterVariable(char const* name, char const* description, T const& value, VariableState state) |
| 170 | { |
| 171 | // Static registration (from code) cannot return nullptr and therefore has to trigger a fatal errors. |
| 172 | // Dynamic registration (from .ini or console) reports errors, but can return nullptr |
| 173 | auto handleError = [&](char const* message) -> void { |
| 174 | if (state.setby > SetBy::CODE) |
| 175 | donut::log::error(message, name); |
| 176 | else |
| 177 | donut::log::fatal(message, name); |
| 178 | }; |
| 179 | |
| 180 | if (IsValidName(name)) |
| 181 | { |
| 182 | std::lock_guard<std::mutex> lock(m_Mutex); |
| 183 | if (auto it = m_Dictionary.find(name); it != m_Dictionary.end()) |
| 184 | { |
| 185 | if (VariableImpl<T>* cvar = (VariableImpl<T>*)it->second->AsVariable()) |
| 186 | { |
| 187 | if (cvar->GetState().type == VariableType::IsA<T>()) |
| 188 | { |
| 189 | // cvar may have been referenced elsewhere but not be initialized yet |
| 190 | if (cvar->m_Description.empty() && IsValidName(description)) |
| 191 | cvar->m_Description = description; |
| 192 | |
| 193 | // override the value |
| 194 | cvar->SetData(value, (SetBy)state.setby); |
| 195 | |
| 196 | return cvar; |
| 197 | } |
| 198 | else |
| 199 | handleError("console variable '%s' already exists but is a different type"); |
| 200 | } |
| 201 | else |
| 202 | handleError("console variable with name '%s' already exists"); |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | assert(state.type == VariableType::IsA<T>()); |
| 207 | state.type = VariableType::IsA<T>(); // force type to be correct |
| 208 | |
| 209 | VariableImpl<T>* cvar = new VariableImpl<T>(value, description, state); |
| 210 | m_Dictionary[name] = cvar; |
| 211 | return cvar; |
| 212 | } |
| 213 | } |
| 214 | else |
| 215 | handleError("attempting to register a console variable with invalid name '%s'"); |
| 216 | return nullptr; |
| 217 | } |
| 218 | |
| 219 | Object* FindObject(std::string_view name) |
| 220 | { |