<commit_before>// For conditions of distribution and use, see copyright notice in license.txt

#include "DebugOperatorNew.h"
#include <boost/thread.hpp>
#include <boost/algorithm/string.hpp>
#include <QList>
#include <boost/regex.hpp>
#include "MemoryLeakCheck.h"
#include "AssetAPI.h"
#include "Framework.h"
#include "IAssetTransfer.h"
#include "IAsset.h"
#include "IAssetStorage.h"
#include "IAssetProvider.h"
#include "RenderServiceInterface.h"
#include "LoggingFunctions.h"
#include "EventManager.h"
#include "CoreException.h"
#include "IAssetTypeFactory.h"
#include "IAssetUploadTransfer.h"
#include "GenericAssetFactory.h"
#include "AssetCache.h"
#include "Platform.h"
#include <QDir>
#include <QFileSystemWatcher>

DEFINE_POCO_LOGGING_FUNCTIONS("Asset")

using namespace Foundation;

AssetAPI::AssetAPI(bool isHeadless)
:assetCache(0),
diskSourceChangeWatcher(0),
isHeadless_(isHeadless)
{
    // The Asset API always understands at least this single built-in asset type "Binary".
    // You can use this type to request asset data as binary, without generating any kind of in-memory representation or loading for it.
    // Your module/component can then parse the content in a custom way.
    RegisterAssetTypeFactory(AssetTypeFactoryPtr(new BinaryAssetFactory("Binary")));
    isHeadless_ = isHeadless;
}

AssetAPI::~AssetAPI()
{
    delete assetCache;
    delete diskSourceChangeWatcher;
}

void AssetAPI::OpenAssetCache(QString directory)
{
    SAFE_DELETE(assetCache);
    SAFE_DELETE(diskSourceChangeWatcher);
    assetCache = new AssetCache(this, directory.toStdString().c_str());
    diskSourceChangeWatcher = new QFileSystemWatcher();
    connect(diskSourceChangeWatcher, SIGNAL(fileChanged(QString)), this, SLOT(OnAssetDiskSourceChanged(QString)));
}

std::vector<AssetProviderPtr> AssetAPI::GetAssetProviders() const
{
    return providers;
}

void AssetAPI::RegisterAssetProvider(AssetProviderPtr provider)
{
    for(uint i=0; i<providers.size(); ++i)
    {
        if (providers[i]->Name() == provider->Name())
        {
            LogWarning("AssetAPI::RegisterAssetProvider: Provider with name '" + provider->Name() + "' already registered.");
            return;
        }
    }
    providers.push_back(provider);
}

AssetStoragePtr AssetAPI::GetAssetStorage(const QString &name) const
{
    foreach(AssetProviderPtr provider, GetAssetProviders())
        foreach(AssetStoragePtr storage, provider->GetStorages())
            if (storage->Name() == name)
                return storage;
    return AssetStoragePtr();
}

/// \todo Delete this function and replace with a proper deserialization from string. -jj.
AssetStoragePtr AssetAPI::AddAssetStorage(const QString &url, const QString &name, bool setAsDefault)
{
    AssetStoragePtr newStorage;
    IAssetProvider *provider = GetProviderForAssetRef(url, "Binary").get();
    if (!provider)
    {
        LogError("AssetAPI::AddAssetStorage: Could not find a provider for the new storage location " + url);
        return newStorage; 
    }

    // Inspect if a storage already exists for this url and name combination
    std::vector<AssetStoragePtr> currentStorages = provider->GetStorages();
    for(int i=0; i<currentStorages.size(); ++i)
    {
        newStorage = currentStorages.at(i);
        if (!newStorage.get())
            continue;
        if (newStorage->BaseURL() == url && newStorage->Name() == name)
        {
            LogDebug("AssetAPI::AddAssetStorage: Found existing storage with same url and name, returning the existing storage.");
            break;
        }
        // Reset so the last valid provider wont be used! 
        newStorage = AssetStoragePtr();
    }

    // If the url name combination was not found, ask the provider to create a new one
    if (!newStorage.get())
        newStorage = provider->AddStorage(url, name);
    if (newStorage.get() && setAsDefault)
        SetDefaultAssetStorage(newStorage);
    if (!newStorage.get())
        LogError("AssetAPI::AddAssetStorage: Could not create new storage for " + url);
    return newStorage;
}

AssetStoragePtr AssetAPI::GetDefaultAssetStorage() const
{
    AssetStoragePtr defStorage = defaultStorage.lock();
    if (defStorage)
        return defStorage;<commit_msg>Marked bad Asset API asset storage handling functions that are due for deletion. (does not allow fully specifying configuration for generated storage per provider type). These are to be replaced with proper asset storage serialization and deserialization functions.<commit_after>// For conditions of distribution and use, see copyright notice in license.txt

#include "DebugOperatorNew.h"
#include <boost/thread.hpp>
#include <boost/algorithm/string.hpp>
#include <QList>
#include <boost/regex.hpp>
#include "MemoryLeakCheck.h"
#include "AssetAPI.h"
#include "Framework.h"
#include "IAssetTransfer.h"
#include "IAsset.h"
#include "IAssetStorage.h"
#include "IAssetProvider.h"
#include "RenderServiceInterface.h"
#include "LoggingFunctions.h"
#include "EventManager.h"
#include "CoreException.h"
#include "IAssetTypeFactory.h"
#include "IAssetUploadTransfer.h"
#include "GenericAssetFactory.h"
#include "AssetCache.h"
#include "Platform.h"
#include <QDir>
#include <QFileSystemWatcher>

DEFINE_POCO_LOGGING_FUNCTIONS("Asset")

using namespace Foundation;

AssetAPI::AssetAPI(bool isHeadless)
:assetCache(0),
diskSourceChangeWatcher(0),
isHeadless_(isHeadless)
{
    // The Asset API always understands at least this single built-in asset type "Binary".
    // You can use this type to request asset data as binary, without generating any kind of in-memory representation or loading for it.
    // Your module/component can then parse the content in a custom way.
    RegisterAssetTypeFactory(AssetTypeFactoryPtr(new BinaryAssetFactory("Binary")));
    isHeadless_ = isHeadless;
}

AssetAPI::~AssetAPI()
{
    delete assetCache;
    delete diskSourceChangeWatcher;
}

void AssetAPI::OpenAssetCache(QString directory)
{
    SAFE_DELETE(assetCache);
    SAFE_DELETE(diskSourceChangeWatcher);
    assetCache = new AssetCache(this, directory.toStdString().c_str());
    diskSourceChangeWatcher = new QFileSystemWatcher();
    connect(diskSourceChangeWatcher, SIGNAL(fileChanged(QString)), this, SLOT(OnAssetDiskSourceChanged(QString)));
}

std::vector<AssetProviderPtr> AssetAPI::GetAssetProviders() const
{
    return providers;
}

void AssetAPI::RegisterAssetProvider(AssetProviderPtr provider)
{
    for(uint i=0; i<providers.size(); ++i)
    {
        if (providers[i]->Name() == provider->Name())
        {
            LogWarning("AssetAPI::RegisterAssetProvider: Provider with name '" + provider->Name() + "' already registered.");
            return;
        }
    }
    providers.push_back(provider);
}

AssetStoragePtr AssetAPI::GetAssetStorage(const QString &name) const
{
    foreach(AssetProviderPtr provider, GetAssetProviders())
        foreach(AssetStoragePtr storage, provider->GetStorages())
            if (storage->Name() == name)
                return storage;
    return AssetStoragePtr();
}

/// \todo Delete this function and replace with a proper deserialization from string. -jj.
AssetStoragePtr AssetAPI::AddAssetStorage(const QString &url, const QString &name, bool setAsDefault)
{
    AssetStoragePtr newStorage;
    IAssetProvider *provider = GetProviderForAssetRef(url, "Binary").get();
    if (!provider)
    {
        LogError("AssetAPI::AddAssetStorage: Could not find a provider for the new storage location " + url);
        return newStorage; 
    }

    // Inspect if a storage already exists for this url and name combination
    std::vector<AssetStoragePtr> currentStorages = provider->GetStorages();
    for(size_t i = 0; i < currentStorages.size(); ++i)
    {
        newStorage = currentStorages.at(i);
        if (!newStorage)
            continue;
        if (newStorage->BaseURL() == url && newStorage->Name() == name)
        {
            LogDebug("AssetAPI::AddAssetStorage: Found existing storage with same url and name, returning the existing storage.");
            break;
        }
        // Reset so the last valid provider wont be used! 
        newStorage = AssetStoragePtr();
    }

    // If the url name combination was not found, ask the provider to create a new one
    if (!newStorage.get())
        newStorage = provider->AddStorage(url, name);
    if (newStorage.get() && setAsDefault)
        SetDefaultAssetStorage(newStorage);
    if (!newStorage.get())
        LogError("AssetAPI::AddAssetStorage: Could not create new storage for " + url);
    return newStorage;
}

AssetStoragePtr AssetAPI::GetDefaultAssetStorage() const
{
    AssetStoragePtr defStorage = defaultStorage.lock();
    if (defStorage)
        return defStorage;
{'fraction_non_alphanumeric': 0.1076870202772368, 'fraction_numerical': 0.0009164852789552068, 'mean_word_length': 4.081490104772992, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 37, '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': '7012828', 'n_tokens_mistral': 2479, 'n_tokens_neox': 2451, 'n_words': 688}