<commit_before>/*
 * This file is part of the UnTech Editor Suite.
 * Copyright (c) 2016 - 2020, <NAME> <<EMAIL>>.
 * Distributed under The MIT License: https://opensource.org/licenses/MIT
 */

#include "project-data.h"
#include "project.h"
#include "models/common/errorlist.h"
#include "models/common/type-traits.h"
#include "models/metasprite/compiler/framesetcompiler.h"
#include <cassert>

namespace UnTech {
namespace Project {

static const idstring BLANK_ID{};

static std::array<std::string, 5> projectSettingNames{
    "Project Settings",
    "Interactive Tiles",
    "Action Points",
    "Entities",
    "Scenes",
};

// Template Magic
// ==============

template <class T>
static const idstring& itemNameString(const T& item)
{
    return item.name;
}
static const idstring& itemNameString(const MetaSprite::FrameSetFile& item)
{
    return item.name();
}

template <class ListT>
static const std::string& itemNameString(const ListT& list, unsigned index)
{
    return list.at(index).name;
}
template <class T>
static std::string itemNameString(const ExternalFileList<T>& list, unsigned index)
{
    const ExternalFileItem<T>& item = list.item(index);
    if (item.value) {
        return item.value->name;
    }
    else {
        return item.filename.filename().string();
    }
}
template <size_t N>
static const std::string& itemNameString(const std::array<std::string, N>& list, unsigned index)
{
    return list.at(index);
}
static const idstring& itemNameString(const std::vector<MetaSprite::FrameSetFile>& list, unsigned index)
{
    return list.at(index).name();
}

template <typename T>
static inline bool checkPrerequisite(const std::shared_ptr<T>& p)
{
    return p != nullptr;
}
template <typename T>
static inline bool checkPrerequisite(const std::optional<T>& p)
{
    return p.has_value();
}
template <typename T>
static inline bool checkPrerequisite(const T&)
{
    return true;
}

template <typename T>
static inline const DataStore<T>& expandPresquite(const DataStore<T>& ds)
{
    return ds;
}
template <typename T>
static inline const T& expandPresquite(const std::shared_ptr<T>& p)
{
    assert(p);
    return *p;
}
template <typename T>
static inline const T& expandPresquite(const std::optional<T>& p)
{
    assert(p);
    return *p;
}
template <typename T>
static inline const T& expandPresquite(const T& p)
{
    return p;
}

// Status/Data methods
// ==================
// All "write" methods MUST be implemented in the .cpp file and protected with a `std::unqiue_lock` or `std::lock_guard`

ResourceListStatus::ResourceListStatus(std::string tnSingle, std::string tnPlural)
    : _typeNameSingle(tnSingle)
    , _typeNamePlural(tnPlural)
    , _currentCompileId(1)
    , _state(ResourceState::Unchecked)
    , _resources()
{
}

void ResourceListStatus::clearAllAndResize(size_t size)
{
    std::lock_guard lock(_mutex);

    _resources.clear();
    _resources.resize(size);

    _state = ResourceState::Unchecked;
}

inline std::pair<std::string, std::string> ResourceListStatus::setStatus(unsigned index, ResourceStatus&& status)
{
    std::lock_guard lock(_mutex);

    status.compileId = _currentCompileId++;

    std::string oldName = _resources.at(index).name;
    std::string newName = status.name;

    _resources.at(index) = std::move(status);

    return { oldName, newName };
}

void ResourceListStatus::setStatusKeepName(unsigned index, ResourceStatus&& status)
{
    std::lock_guard lock(_mutex);

    auto& s = _resources.at(index);

    s.state = status.state;
    s.errorList = std::move(status.errorList);
}

void ResourceListStatus::updateState()
{<commit_msg>Fix ResourceListStatus::setStatusKeepName not setting compileId<commit_after>/*
 * This file is part of the UnTech Editor Suite.
 * Copyright (c) 2016 - 2020, <NAME> <<EMAIL>>.
 * Distributed under The MIT License: https://opensource.org/licenses/MIT
 */

#include "project-data.h"
#include "project.h"
#include "models/common/errorlist.h"
#include "models/common/type-traits.h"
#include "models/metasprite/compiler/framesetcompiler.h"
#include <cassert>

namespace UnTech {
namespace Project {

static const idstring BLANK_ID{};

static std::array<std::string, 5> projectSettingNames{
    "Project Settings",
    "Interactive Tiles",
    "Action Points",
    "Entities",
    "Scenes",
};

// Template Magic
// ==============

template <class T>
static const idstring& itemNameString(const T& item)
{
    return item.name;
}
static const idstring& itemNameString(const MetaSprite::FrameSetFile& item)
{
    return item.name();
}

template <class ListT>
static const std::string& itemNameString(const ListT& list, unsigned index)
{
    return list.at(index).name;
}
template <class T>
static std::string itemNameString(const ExternalFileList<T>& list, unsigned index)
{
    const ExternalFileItem<T>& item = list.item(index);
    if (item.value) {
        return item.value->name;
    }
    else {
        return item.filename.filename().string();
    }
}
template <size_t N>
static const std::string& itemNameString(const std::array<std::string, N>& list, unsigned index)
{
    return list.at(index);
}
static const idstring& itemNameString(const std::vector<MetaSprite::FrameSetFile>& list, unsigned index)
{
    return list.at(index).name();
}

template <typename T>
static inline bool checkPrerequisite(const std::shared_ptr<T>& p)
{
    return p != nullptr;
}
template <typename T>
static inline bool checkPrerequisite(const std::optional<T>& p)
{
    return p.has_value();
}
template <typename T>
static inline bool checkPrerequisite(const T&)
{
    return true;
}

template <typename T>
static inline const DataStore<T>& expandPresquite(const DataStore<T>& ds)
{
    return ds;
}
template <typename T>
static inline const T& expandPresquite(const std::shared_ptr<T>& p)
{
    assert(p);
    return *p;
}
template <typename T>
static inline const T& expandPresquite(const std::optional<T>& p)
{
    assert(p);
    return *p;
}
template <typename T>
static inline const T& expandPresquite(const T& p)
{
    return p;
}

// Status/Data methods
// ==================
// All "write" methods MUST be implemented in the .cpp file and protected with a `std::unqiue_lock` or `std::lock_guard`

ResourceListStatus::ResourceListStatus(std::string tnSingle, std::string tnPlural)
    : _typeNameSingle(tnSingle)
    , _typeNamePlural(tnPlural)
    , _currentCompileId(1)
    , _state(ResourceState::Unchecked)
    , _resources()
{
}

void ResourceListStatus::clearAllAndResize(size_t size)
{
    std::lock_guard lock(_mutex);

    _resources.clear();
    _resources.resize(size);

    _state = ResourceState::Unchecked;
}

inline std::pair<std::string, std::string> ResourceListStatus::setStatus(unsigned index, ResourceStatus&& status)
{
    std::lock_guard lock(_mutex);

    status.compileId = _currentCompileId++;

    std::string oldName = _resources.at(index).name;
    std::string newName = status.name;

    _resources.at(index) = std::move(status);

    return { oldName, newName };
}

void ResourceListStatus::setStatusKeepName(unsigned index, ResourceStatus&& status)
{
    std::lock_guard lock(_mutex);

    auto& s = _resources.at(index);

    s.compileId = _currentCompileId++;

    s.state = status.state;
    s.errorList = std::move(status.errorList);
}

void ResourceListStatus::updateState()
{
{'fraction_non_alphanumeric': 0.13729508196721313, 'fraction_numerical': 0.00273224043715847, 'mean_word_length': 4.66640866873065, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 59, 'https://': 2, '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': '6522250', 'n_tokens_mistral': 2371, 'n_tokens_neox': 2302, 'n_words': 588}