<commit_before>#include "AssetCache.h"
#include "AssetAPI.h"
#include "IAsset.h"
#include "LoggingFunctions.h"

#include <QFileInfo>
#include <boost/filesystem.hpp>

DEFINE_POCO_LOGGING_FUNCTIONS("AssetCache")

QString SanitateAssetRefForCache(QString assetRef)
{ 
    assetRef.replace("/", "_");
    assetRef.replace("\\", "_");
    assetRef.replace(":", "_");
    assetRef.replace("*", "_");
    assetRef.replace("?", "_");
    assetRef.replace("\"", "_");
    assetRef.replace("'", "_");
    assetRef.replace("<", "_");
    assetRef.replace(">", "_");
    assetRef.replace("|", "_");
    return assetRef;
}

AssetCache::AssetCache(QString assetCacheDirectory)
:cacheDirectory(GuaranteeTrailingSlash(assetCacheDirectory))
{
    LogInfo("Using AssetCache in directory \"" + assetCacheDirectory.toStdString() + "\".");
}

QString AssetCache::FindAsset(QString assetRef)
{
    QString fileSource = cacheDirectory + SanitateAssetRefForCache(assetRef);
    if (QFile::exists(fileSource))
        return fileSource;
    else
        return "";
}

QString AssetCache::FindAssetByContentHash(QString contentHash)
{
    ///\todo Implement.
    return "";
}

void AssetCache::DeleteAsset(QString assetRef)
{
    QString fileSource = cacheDirectory + SanitateAssetRefForCache(assetRef);
    boost::filesystem::remove(fileSource.toStdString());
}

void AssetCache::ClearAssetCache()
{
    ///\todo Implement.
}

QString AssetCache::StoreAsset(const u8 *data, size_t numBytes, QString assetName, QString assetContentHash)
{
    QString filename = cacheDirectory + SanitateAssetRefForCache(assetName);

    bool success = SaveAssetFromMemoryToFile(data, numBytes, filename.toStdString().c_str());
    if (success)
    {
        LogDebug("Saved asset \"" + assetName.toStdString() + "\" to cache into file \"" + filename.toStdString() + "\"");
        return filename;
    }
    else
        return "";
}

void AssetCache::StoreAsset(AssetPtr asset)
{
    std::vector<u8> data;
    asset->SerializeTo(data);
    StoreAsset(&data[0], data.size(), asset->Name(), asset->ContentHash()); ///\todo Content hash can mismatch here.
}<commit_msg>Remove the saved to cache print all together, its bleeding to the console in release mode. Very annoying and seems that the cache is working well so no need to print this out imo. Add back locally if you are debugging cache stuff.<commit_after>#include "AssetCache.h"
#include "AssetAPI.h"
#include "IAsset.h"
#include "LoggingFunctions.h"

#include <QFileInfo>
#include <boost/filesystem.hpp>

DEFINE_POCO_LOGGING_FUNCTIONS("AssetCache")

QString SanitateAssetRefForCache(QString assetRef)
{ 
    assetRef.replace("/", "_");
    assetRef.replace("\\", "_");
    assetRef.replace(":", "_");
    assetRef.replace("*", "_");
    assetRef.replace("?", "_");
    assetRef.replace("\"", "_");
    assetRef.replace("'", "_");
    assetRef.replace("<", "_");
    assetRef.replace(">", "_");
    assetRef.replace("|", "_");
    return assetRef;
}

AssetCache::AssetCache(QString assetCacheDirectory)
:cacheDirectory(GuaranteeTrailingSlash(assetCacheDirectory))
{
    LogInfo("Using AssetCache in directory \"" + assetCacheDirectory.toStdString() + "\".");
}

QString AssetCache::FindAsset(QString assetRef)
{
    QString fileSource = cacheDirectory + SanitateAssetRefForCache(assetRef);
    if (QFile::exists(fileSource))
        return fileSource;
    else
        return "";
}

QString AssetCache::FindAssetByContentHash(QString contentHash)
{
    ///\todo Implement.
    return "";
}

void AssetCache::DeleteAsset(QString assetRef)
{
    QString fileSource = cacheDirectory + SanitateAssetRefForCache(assetRef);
    boost::filesystem::remove(fileSource.toStdString());
}

void AssetCache::ClearAssetCache()
{
    ///\todo Implement.
}

QString AssetCache::StoreAsset(const u8 *data, size_t numBytes, QString assetName, QString assetContentHash)
{
    QString filename = cacheDirectory + SanitateAssetRefForCache(assetName);

    bool success = SaveAssetFromMemoryToFile(data, numBytes, filename.toStdString().c_str());
    if (success)
    {
        //LogDebug("Saved asset \"" + assetName.toStdString() + "\" to cache into file \"" + filename.toStdString() + "\"");
        return filename;
    }
    else
        return "";
}

void AssetCache::StoreAsset(AssetPtr asset)
{
    std::vector<u8> data;
    asset->SerializeTo(data);
    StoreAsset(&data[0], data.size(), asset->Name(), asset->ContentHash()); ///\todo Content hash can mismatch here.
}
{'fraction_non_alphanumeric': 0.15171179234728127, 'fraction_numerical': 0.0013425822331617813, 'mean_word_length': 4.4645476772616135, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1959272', 'n_tokens_mistral': 1381, 'n_tokens_neox': 1376, 'n_words': 274}