Skip to content

Commit

Permalink
Remove the map package from the package manager cache
Browse files Browse the repository at this point in the history
  • Loading branch information
dpjudas committed Aug 30, 2024
1 parent eed5d04 commit bd27f6e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 45 deletions.
15 changes: 3 additions & 12 deletions SurrealEngine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,23 +277,19 @@ void Engine::LoadEntryMap()
LoadMap(GetDefaultURL("Entry"));
EntryLevelInfo = LevelInfo;
EntryLevel = Level;
EntryLevelPackage = std::move(LevelPackage);
LevelInfo = nullptr;
Level = nullptr;
LevelPackage = nullptr;
}

void Engine::UnloadMap()
{
if (!LevelPackage)
return;

NameString packageName = LevelPackage->GetPackageName();

LevelInfo = nullptr;
Level = nullptr;
LevelPackage = nullptr;

packages->UnloadPackage(packageName);
packages->UnloadMap(std::move(LevelPackage));
}

void Engine::LoadMap(const UnrealURL& url, const std::map<std::string, std::string>& travelInfo)
Expand All @@ -310,12 +306,7 @@ void Engine::LoadMap(const UnrealURL& url, const std::map<std::string, std::stri

// Load map objects

// Determine if we're getting a relative path
// Which is the case with Unreal's New game menu
if (url.Map.substr(0, 2) == "..")
LevelPackage = packages->GetPackageFromPath(url.Map);
else
LevelPackage = packages->GetPackage(FilePath::remove_extension(url.Map));
LevelPackage = packages->LoadMap(url.Map);

LevelInfo = UObject::Cast<ULevelInfo>(LevelPackage->GetUObject("LevelInfo", "LevelInfo0"));
if (packages->IsUnreal1())
Expand Down
3 changes: 2 additions & 1 deletion SurrealEngine/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,11 @@ class Engine : public GameWindowHost
ULevelInfo* EntryLevelInfo = nullptr;
ULevel* EntryLevel = nullptr;
UGameInfo* EntryGameInfo = nullptr;
std::unique_ptr<Package> EntryLevelPackage = nullptr;

ULevelInfo* LevelInfo = nullptr;
ULevel* Level = nullptr;
Package* LevelPackage = nullptr;
std::unique_ptr<Package> LevelPackage;
UGameInfo* GameInfo = nullptr;
UTexture* DefaultTexture = nullptr;
struct
Expand Down
48 changes: 19 additions & 29 deletions SurrealEngine/Package/PackageManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,42 +71,32 @@ Package* PackageManager::GetPackage(const NameString& name)
return package.get();
}

Package* PackageManager::GetPackageFromPath(const std::string& path)
std::unique_ptr<Package> PackageManager::LoadMap(const std::string& path)
{
auto absolute_path = FilePath::relative_to_absolute_from_system(FilePath::combine(launchInfo.gameRootFolder, "System"), path);

// Manually find the relevant package
for (auto& packageName : packageFilenames)
{
if (NameString(packageName.second) == NameString(absolute_path))
{
auto& package = packages[packageName.first];

if (!package)
package = std::make_unique<Package>(this, packageName.first, packageName.second);

return package.get();
}

}

Exception::Throw("Could not find package from the given path: " + path);
// Path is relative to the Maps folder?
// Or is it relative to the package requesting the map load?
// Or is it relative to the previous map?
//
// Only one of the above is most likely true. Lets begin with assuming its relative to the Maps folder.
std::string name = FilePath::remove_extension(FilePath::last_component(path));
std::string absolute_path = FilePath::relative_to_absolute_from_system(FilePath::combine(launchInfo.gameRootFolder, "Maps"), path);
return std::make_unique<Package>(this, name, absolute_path);
}

void PackageManager::UnloadPackage(const NameString& name)
void PackageManager::UnloadMap(std::unique_ptr<Package> package)
{
auto it = packages.find(name);
if (it != packages.end())
// Remove package from open streams cache:
auto streamit = openStreams.begin();
while (streamit != openStreams.end())
{
for (auto streamit = openStreams.begin(); streamit != openStreams.end(); ++streamit)
if (streamit->Pkg == package.get())
{
if (streamit->Pkg == it->second.get())
{
openStreams.erase(streamit);
break;
}
streamit = openStreams.erase(streamit);
}
else
{
++streamit;
}
packages.erase(it);
}
}

Expand Down
4 changes: 2 additions & 2 deletions SurrealEngine/Package/PackageManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class PackageManager
int GetEngineSubVersion() const { return launchInfo.engineSubVersion; }

Package *GetPackage(const NameString& name);
Package *GetPackageFromPath(const std::string& path);
Array<NameString> GetPackageNames() const;

void UnloadPackage(const NameString& name);
std::unique_ptr<Package> LoadMap(const std::string& path);
void UnloadMap(std::unique_ptr<Package> package);

std::shared_ptr<PackageStream> GetStream(Package* package);

Expand Down
2 changes: 1 addition & 1 deletion SurrealEngine/UI/Editor/EditorMainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ void EditorMainWindow::OnHelpAbout()

void EditorMainWindow::LoadMap(std::string& mapName)
{
engine->LevelPackage = engine->packages->GetPackage(FilePath::remove_extension(mapName));
engine->LevelPackage = engine->packages->LoadMap(mapName);
engine->LevelInfo = UObject::Cast<ULevelInfo>(engine->LevelPackage->GetUObject("LevelInfo", "LevelInfo0"));
engine->Level = UObject::Cast<ULevel>(engine->LevelPackage->GetUObject("Level", "MyLevel"));
engine->CameraActor = UObject::Cast<UActor>(engine->packages->NewObject("camera", "Engine", "Camera"));
Expand Down

0 comments on commit bd27f6e

Please sign in to comment.