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 : 20 : #include "ObjectManager.h" 21 : 22 : #include "graphics/ObjectBase.h" 23 : #include "graphics/ObjectEntry.h" 24 : #include "lib/debug.h" 25 : #include "lib/path.h" 26 : #include "lib/utf8.h" 27 : #include "ps/CLogger.h" 28 : #include "ps/ConfigDB.h" 29 : #include "ps/Filesystem.h" 30 : #include "ps/Profiler2.h" 31 : #include "ps/XML/Xeromyces.h" 32 : #include "scriptinterface/Interface.h" 33 : #include "simulation2/Simulation2.h" 34 : #include "simulation2/components/ICmpTerrain.h" 35 : #include "simulation2/components/ICmpVisual.h" 36 : #include "simulation2/system/Component.h" 37 : #include "simulation2/system/Entity.h" 38 : 39 : #include <cstddef> 40 : #include <functional> 41 : #include <string> 42 : 43 2 : bool CObjectManager::ObjectKey::operator< (const CObjectManager::ObjectKey& a) const 44 : { 45 2 : if (ObjectBaseIdentifier < a.ObjectBaseIdentifier) 46 : return true; 47 2 : else if (ObjectBaseIdentifier > a.ObjectBaseIdentifier) 48 : return false; 49 : else 50 2 : return ActorVariation < a.ActorVariation; 51 : } 52 : 53 0 : static Status ReloadChangedFileCB(void* param, const VfsPath& path) 54 : { 55 0 : return static_cast<CObjectManager*>(param)->ReloadChangedFile(path); 56 : } 57 : 58 1 : CObjectManager::CObjectManager(CMeshManager& meshManager, CSkeletonAnimManager& skeletonAnimManager, CSimulation2& simulation) 59 1 : : m_MeshManager(meshManager), m_SkeletonAnimManager(skeletonAnimManager), m_Simulation(simulation) 60 : { 61 1 : RegisterFileReloadFunc(ReloadChangedFileCB, this); 62 : 63 4 : m_QualityHook = std::make_unique<CConfigDBHook>(g_ConfigDB.RegisterHookAndCall("max_actor_quality", [this]() { ActorQualityChanged(); })); 64 4 : m_VariantDiversityHook = std::make_unique<CConfigDBHook>(g_ConfigDB.RegisterHookAndCall("variant_diversity", [this]() { VariantDiversityChanged(); })); 65 : 66 2 : if (!g_Xeromyces.AddValidator(g_VFS, "actor", "art/actors/actor.rng")) 67 2 : LOGERROR("CObjectManager: failed to load actor grammar file 'art/actors/actor.rng'"); 68 1 : } 69 : 70 1 : CObjectManager::~CObjectManager() 71 : { 72 1 : UnloadObjects(); 73 : 74 1 : UnregisterFileReloadFunc(ReloadChangedFileCB, this); 75 2 : } 76 : 77 2 : std::pair<bool, CActorDef&> CObjectManager::FindActorDef(const CStrW& actorName) 78 : { 79 2 : ENSURE(!actorName.empty()); 80 : 81 2 : decltype(m_ActorDefs)::iterator it = m_ActorDefs.find(actorName); 82 2 : if (it != m_ActorDefs.end() && !it->second.outdated) 83 1 : return { true, *it->second.obj }; 84 : 85 1 : std::unique_ptr<CActorDef> actor = std::make_unique<CActorDef>(*this); 86 : 87 5 : VfsPath pathname = VfsPath("art/actors/") / actorName; 88 : 89 1 : bool success = true; 90 1 : if (!actor->Load(pathname)) 91 : { 92 : // In case of failure, load a placeholder - we want to have an actor around for hotloading. 93 : // (this will leave garbage actors in the object manager if loading files with typos in the name, 94 : // but that's unlikely to be a large memory problem). 95 0 : LOGERROR("CObjectManager::FindActorDef(): Cannot find actor '{}'", 96 : utf8_from_wstring(actorName)); 97 0 : actor->LoadErrorPlaceholder(pathname); 98 : success = false; 99 : } 100 : 101 1 : return { success, *m_ActorDefs.insert_or_assign(actorName, std::move(actor)).first->second.obj }; 102 : } 103 : 104 2 : CObjectEntry* CObjectManager::FindObjectVariation(const CActorDef* actor, const std::vector<std::set<CStr>>& selections, uint32_t seed) 105 : { 106 2 : if (!actor) 107 : return nullptr; 108 : 109 2 : const std::shared_ptr<CObjectBase>& base = actor->GetBase(m_QualityLevel); 110 : 111 2 : std::vector<const std::set<CStr>*> completeSelections; 112 6 : for (const std::set<CStr>& selectionSet : selections) 113 4 : completeSelections.emplace_back(&selectionSet); 114 : // To maintain a consistent look between quality levels, first complete with the highest-quality variants. 115 : // then complete again at the required quality level (since not all variants may be available). 116 2 : std::set<CStr> highQualitySelections = actor->GetBase(255)->CalculateRandomRemainingSelections(seed, selections); 117 2 : completeSelections.emplace_back(&highQualitySelections); 118 : // We don't have to pass the high-quality selections here because they have higher priority anyways. 119 2 : std::set<CStr> remainingSelections = base->CalculateRandomRemainingSelections(seed, selections); 120 2 : completeSelections.emplace_back(&remainingSelections); 121 2 : return FindObjectVariation(base, completeSelections); 122 4 : } 123 : 124 2 : CObjectEntry* CObjectManager::FindObjectVariation(const std::shared_ptr<CObjectBase>& base, const std::vector<const std::set<CStr>*>& completeSelections) 125 : { 126 2 : PROFILE2("FindObjectVariation"); 127 : 128 : // Look to see whether this particular variation has already been loaded 129 2 : std::vector<std::uint8_t> choices = base->CalculateVariationKey(completeSelections); 130 2 : ObjectKey key (base->GetIdentifier(), choices); 131 2 : decltype(m_Objects)::iterator it = m_Objects.find(key); 132 2 : if (it != m_Objects.end() && !it->second.outdated) 133 1 : return it->second.obj.get(); 134 : 135 : // If it hasn't been loaded, load it now. 136 : 137 1 : std::unique_ptr<CObjectEntry> obj = std::make_unique<CObjectEntry>(base, m_Simulation); 138 : 139 : // TODO (for some efficiency): use the pre-calculated choices for this object, 140 : // which has already worked out what to do for props, instead of passing the 141 : // selections into BuildVariation and having it recalculate the props' choices. 142 : 143 1 : if (!obj->BuildVariation(completeSelections, choices, *this)) 144 : return nullptr; 145 : 146 1 : return m_Objects.insert_or_assign(key, std::move(obj)).first->second.obj.get(); 147 4 : } 148 : 149 0 : CTerrain* CObjectManager::GetTerrain() 150 : { 151 0 : CmpPtr<ICmpTerrain> cmpTerrain(m_Simulation, SYSTEM_ENTITY); 152 0 : if (!cmpTerrain) 153 : return NULL; 154 0 : return cmpTerrain->GetCTerrain(); 155 : } 156 : 157 6 : CObjectManager::VariantDiversity CObjectManager::GetVariantDiversity() const 158 : { 159 6 : return m_VariantDiversity; 160 : } 161 : 162 1 : void CObjectManager::UnloadObjects() 163 : { 164 1 : m_Objects.clear(); 165 1 : m_ActorDefs.clear(); 166 1 : } 167 : 168 0 : Status CObjectManager::ReloadChangedFile(const VfsPath& path) 169 : { 170 0 : bool changed = false; 171 : 172 : // Mark old entries as outdated so we don't reload them from the cache 173 0 : for (std::pair<const ObjectKey, Hotloadable<CObjectEntry>>& object : m_Objects) 174 0 : if (!object.second.outdated && object.second.obj->m_Base->UsesFile(path)) 175 : { 176 0 : object.second.outdated = true; 177 0 : changed = true; 178 : } 179 : 180 0 : const CSimulation2::InterfaceListUnordered& cmps = m_Simulation.GetEntitiesWithInterfaceUnordered(IID_Visual); 181 : 182 : // Reload actors that use a changed object 183 0 : for (std::pair<const CStrW, Hotloadable<CActorDef>>& actor : m_ActorDefs) 184 : { 185 0 : if (!actor.second.outdated && actor.second.obj->UsesFile(path)) 186 : { 187 0 : actor.second.outdated = true; 188 0 : changed = true; 189 : 190 : // Slightly ugly hack: The graphics system doesn't preserve enough information to regenerate the 191 : // object with all correct variations, and we don't want to waste space storing it just for the 192 : // rare occurrence of hotloading, so we'll tell the component (which does preserve the information) 193 : // to do the reloading itself 194 0 : for (CSimulation2::InterfaceListUnordered::const_iterator eit = cmps.begin(); eit != cmps.end(); ++eit) 195 0 : static_cast<ICmpVisual*>(eit->second)->Hotload(actor.first); 196 : } 197 : } 198 : 199 0 : if (changed) 200 : // Trigger an interpolate call - needed because the game may be paused & if so, models disappear. 201 0 : m_Simulation.Interpolate(0.f, 0.f, 0.f); 202 : 203 0 : return INFO::OK; 204 : } 205 : 206 1 : void CObjectManager::ActorQualityChanged() 207 : { 208 1 : const int quality{g_ConfigDB.Get("max_actor_quality", 0)}; 209 1 : if (quality == m_QualityLevel) 210 : return; 211 : 212 1 : m_QualityLevel = quality > 255 ? 255 : quality < 0 ? 0 : quality; 213 : 214 : // No need to reload entries or actors, but we do need to reload all units. 215 1 : const CSimulation2::InterfaceListUnordered& cmps = m_Simulation.GetEntitiesWithInterfaceUnordered(IID_Visual); 216 1 : for (CSimulation2::InterfaceListUnordered::const_iterator eit = cmps.begin(); eit != cmps.end(); ++eit) 217 0 : static_cast<ICmpVisual*>(eit->second)->Hotload(); 218 : 219 : // Trigger an interpolate call - needed because the game is generally paused & models disappear otherwise. 220 1 : m_Simulation.Interpolate(0.f, 0.f, 0.f); 221 : } 222 : 223 1 : void CObjectManager::VariantDiversityChanged() 224 : { 225 2 : const std::string value{g_ConfigDB.Get("variant_diversity", std::string{})}; 226 1 : VariantDiversity variantDiversity = VariantDiversity::FULL; 227 1 : if (value == "none") 228 : variantDiversity = VariantDiversity::NONE; 229 1 : else if (value == "limited") 230 0 : variantDiversity = VariantDiversity::LIMITED; 231 : // Otherwise assume full. 232 : 233 1 : if (variantDiversity == m_VariantDiversity) 234 1 : return; 235 : 236 0 : m_VariantDiversity = variantDiversity; 237 : 238 : // Mark old entries as outdated so we don't reload them from the cache. 239 0 : for (std::pair<const ObjectKey, Hotloadable<CObjectEntry>>& object : m_Objects) 240 0 : object.second.outdated = true; 241 : 242 : // Reload actors. 243 0 : for (std::pair<const CStrW, Hotloadable<CActorDef>>& actor : m_ActorDefs) 244 0 : actor.second.outdated = true; 245 : 246 : // Reload visual actor components. 247 0 : const CSimulation2::InterfaceListUnordered& cmps = m_Simulation.GetEntitiesWithInterfaceUnordered(IID_Visual); 248 0 : for (CSimulation2::InterfaceListUnordered::const_iterator eit = cmps.begin(); eit != cmps.end(); ++eit) 249 0 : static_cast<ICmpVisual*>(eit->second)->Hotload(); 250 : 251 : // Trigger an interpolate call - needed because the game is generally paused & models disappear otherwise. 252 0 : m_Simulation.Interpolate(0.f, 0.f, 0.f); 253 1 : }