diff --git a/source/lib/path.h b/source/lib/path.h index 63a8541315..02083eceaa 100644 --- a/source/lib/path.h +++ b/source/lib/path.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2010 Wildfire Games +/* Copyright (c) 2013 Wildfire Games * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -205,6 +205,28 @@ public: return ret; } + /** + * Return the path before the common part of both paths + * @param other Indicates the start of the path which should be removed + * @note other should be a VfsPath, while this should be an OsPath + */ + Path BeforeCommon(Path other) const + { + Path ret = *this; + if(ret.empty() || other.empty()) + return L""; + + // Convert the separator to allow for string comparison + if(other.separator != ret.separator) + replace(other.path.begin(), other.path.end(), other.separator, ret.separator); + + const size_t idx = ret.path.rfind(other.path); + if(idx == String::npos) + return L""; + + return path.substr(0, idx); + } + static Status Validate(String::value_type c); private: diff --git a/source/ps/CacheLoader.cpp b/source/ps/CacheLoader.cpp index 79f0f39414..928d44f0e7 100644 --- a/source/ps/CacheLoader.cpp +++ b/source/ps/CacheLoader.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2012 Wildfire Games. +/* Copyright (C) 2013 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -143,8 +143,10 @@ VfsPath CCacheLoader::LooseCachePath(const VfsPath& sourcePath, const MD5& initi for (size_t i = 0; i < 8; ++i) digestPrefix << std::setfill(L'0') << std::setw(2) << (int)digest[i]; - // Construct the final path - return VfsPath("cache") / sourcePath.ChangeExtension(sourcePath.Extension().string() + L"." + digestPrefix.str() + m_FileExtension); + // Get the mod path + OsPath path; + m_VFS->GetRealPath(sourcePath, path); - // TODO: we should probably include the mod name, once that's possible (http://trac.wildfiregames.com/ticket/564) + // Construct the final path + return VfsPath("cache") / path_name_only(path.BeforeCommon(sourcePath).Parent().string().c_str()) / sourcePath.ChangeExtension(sourcePath.Extension().string() + L"." + digestPrefix.str() + m_FileExtension); }