metaforce/hecl/include/HECL/Database.hpp

453 lines
14 KiB
C++
Raw Normal View History

2015-05-15 22:39:43 +00:00
#ifndef HECLDATABASE_HPP
#define HECLDATABASE_HPP
2015-05-17 04:55:29 +00:00
#include <iterator>
#include <string>
#include <functional>
#include <vector>
2015-06-09 22:19:59 +00:00
#include <map>
2015-06-11 09:41:10 +00:00
#include <list>
2015-06-10 23:34:14 +00:00
#include <unordered_map>
2015-06-12 09:08:49 +00:00
#include <unordered_set>
2015-06-09 22:19:59 +00:00
#include <memory>
#include <atomic>
2015-07-17 00:02:19 +00:00
#include <fstream>
#include <stdint.h>
2015-07-16 02:03:38 +00:00
#include <assert.h>
2015-05-17 04:55:29 +00:00
2015-06-11 04:55:06 +00:00
#include <Athena/IStreamReader.hpp>
#include <LogVisor/LogVisor.hpp>
2015-06-11 04:55:06 +00:00
2015-05-20 05:22:32 +00:00
#include "HECL.hpp"
2015-06-10 02:40:03 +00:00
namespace HECL
{
namespace Database
2015-05-17 04:55:29 +00:00
{
2015-06-12 09:08:49 +00:00
class Project;
2015-05-17 04:55:29 +00:00
extern LogVisor::LogModule LogModule;
2015-09-30 06:23:07 +00:00
typedef std::function<void(const HECL::SystemChar*, const HECL::SystemChar*, int, float)> FProgress;
2015-06-11 04:55:06 +00:00
/**
* @brief Nodegraph class for gathering dependency-resolved objects for packaging
*/
class PackageDepsgraph
{
public:
struct Node
{
enum
{
NODE_DATA,
NODE_GROUP
} type;
2015-06-12 09:08:49 +00:00
ProjectPath path;
ProjectPath cookedPath;
2015-06-11 04:55:06 +00:00
class ObjectBase* projectObj;
Node* sub;
Node* next;
};
private:
friend class Project;
std::vector<Node> m_nodes;
public:
const Node* getRootNode() const {return &m_nodes[0];}
};
/**
* @brief Subclassed by dataspec entries to manage per-game aspects of the data pipeline
*
* The DataSpec class manages interfaces for unpackaging, cooking, and packaging
* of data for interacting with a specific system/game-engine.
*/
class IDataSpec
{
public:
2015-07-16 02:03:38 +00:00
virtual ~IDataSpec() {}
2015-09-30 06:23:07 +00:00
using FProgress = FProgress;
2015-07-16 02:03:38 +00:00
2015-06-12 09:08:49 +00:00
/**
* @brief Extract Pass Info
*
* An extract pass iterates through a source package or image and
* reverses the cooking process by emitting editable resources
*/
2015-06-11 04:55:06 +00:00
struct ExtractPassInfo
{
2015-07-13 06:30:20 +00:00
SystemString srcpath;
2015-10-04 04:35:18 +00:00
std::vector<SystemString> extractArgs;
2015-07-18 04:35:01 +00:00
bool force;
2015-06-12 09:08:49 +00:00
};
2015-07-07 03:24:09 +00:00
/**
2015-07-10 05:28:33 +00:00
* @brief Extract Report Representation
2015-07-07 03:24:09 +00:00
*
2015-07-10 05:28:33 +00:00
* Constructed by canExtract() to advise the user of the content about
* to be extracted
2015-07-07 03:24:09 +00:00
*/
2015-07-10 05:28:33 +00:00
struct ExtractReport
2015-07-07 03:24:09 +00:00
{
SystemString name;
SystemString desc;
2015-07-10 05:28:33 +00:00
std::vector<ExtractReport> childOpts;
2015-07-07 03:24:09 +00:00
};
2015-10-04 04:35:18 +00:00
virtual bool canExtract(const ExtractPassInfo& info, std::vector<ExtractReport>& reps)
{(void)info;(void)reps;LogModule.report(LogVisor::Error, "not implemented");return false;}
2015-09-30 06:23:07 +00:00
virtual void doExtract(const ExtractPassInfo& info, FProgress progress)
2015-07-22 19:14:50 +00:00
{(void)info;(void)progress;}
2015-06-12 09:08:49 +00:00
2015-09-30 06:23:07 +00:00
virtual bool canCook(const ProjectPath& path)
{(void)path;LogModule.report(LogVisor::Error, "not implemented");return false;}
virtual void doCook(const ProjectPath& path, const ProjectPath& cookedPath)
{(void)path;(void)cookedPath;}
2015-06-11 04:55:06 +00:00
2015-06-12 09:08:49 +00:00
/**
* @brief Package Pass Info
*
* A package pass performs last-minute queries of source resources,
* gathers dependencies and packages cooked data together in the
* most efficient form for the dataspec
*/
2015-06-11 04:55:06 +00:00
struct PackagePassInfo
{
2015-06-12 09:08:49 +00:00
const PackageDepsgraph& depsgraph;
2015-06-11 04:55:06 +00:00
ProjectPath subpath;
2015-06-12 09:08:49 +00:00
ProjectPath outpath;
2015-06-11 04:55:06 +00:00
};
2015-08-05 22:59:59 +00:00
virtual bool canPackage(const PackagePassInfo& info,
2015-06-12 09:08:49 +00:00
SystemString& reasonNo)
2015-07-22 19:14:50 +00:00
{(void)info;reasonNo=_S("not implemented");return false;}
2015-08-05 22:59:59 +00:00
virtual void gatherDependencies(const PackagePassInfo& info,
2015-06-12 09:08:49 +00:00
std::unordered_set<ProjectPath>& implicitsOut)
2015-07-22 19:14:50 +00:00
{(void)info;(void)implicitsOut;}
2015-08-05 22:59:59 +00:00
virtual void doPackage(const PackagePassInfo& info)
2015-07-22 19:14:50 +00:00
{(void)info;}
2015-06-11 04:55:06 +00:00
};
2015-07-01 23:53:05 +00:00
/**
* @brief Pre-emptive indication of what the constructed DataSpec is used for
*/
enum DataSpecTool
{
TOOL_EXTRACT,
TOOL_COOK,
TOOL_PACKAGE
};
extern std::vector<const struct DataSpecEntry*> DATA_SPEC_REGISTRY;
2015-06-11 04:55:06 +00:00
/**
* @brief IDataSpec registry entry
2015-07-01 23:53:05 +00:00
*
* Auto-registers with data spec registry
2015-06-11 04:55:06 +00:00
*/
struct DataSpecEntry
{
2015-07-22 19:14:50 +00:00
const SystemChar* m_name;
const SystemChar* m_desc;
2015-07-28 23:54:54 +00:00
std::function<IDataSpec*(Project&, DataSpecTool)> m_factory;
2015-07-01 23:53:05 +00:00
2015-07-22 19:14:50 +00:00
DataSpecEntry(const SystemChar* name, const SystemChar* desc,
2015-07-28 23:54:54 +00:00
std::function<IDataSpec*(Project& project, DataSpecTool)>&& factory)
: m_name(name), m_desc(desc), m_factory(std::move(factory)) {}
2015-06-11 04:55:06 +00:00
};
2015-05-21 02:33:05 +00:00
/**
* @brief Base object to subclass for integrating with key project operations
*
* All project objects are provided with IDataObject pointers to their database
* entries. Subclasses register themselves with a type registry so instances
* are automatically constructed when performing operations like cooking and packaging.
*
* DO NOT CONSTRUCT THIS OR SUBCLASSES DIRECTLY!!
*/
2015-06-10 02:40:03 +00:00
class ObjectBase
2015-05-21 02:33:05 +00:00
{
2015-06-10 02:40:03 +00:00
friend class Project;
2015-06-11 04:55:06 +00:00
SystemString m_path;
2015-06-09 22:19:59 +00:00
protected:
2015-05-21 02:33:05 +00:00
/**
* @brief Byte-order of target system
*/
enum DataEndianness
{
DE_NONE,
DE_BIG, /**< Big-endian (PowerPC) */
DE_LITTLE /**< Little-endian (Intel) */
};
/**
* @brief Data-formats of target system
*/
enum DataPlatform
{
DP_NONE,
DP_GENERIC, /**< Scanline textures and 3-way shader bundle (GLSL, HLSL, SPIR-V) */
DP_REVOLUTION, /**< Tiled textures and GX register buffers */
DP_CAFE /**< Swizzled textures and R700 shader objects */
};
2015-06-09 22:19:59 +00:00
typedef std::function<void(const void* data, size_t len)> FDataAppender;
2015-05-21 02:33:05 +00:00
/**
2015-07-17 00:02:19 +00:00
* @brief Optional private method implemented by subclasses to cook objects
2015-05-21 02:33:05 +00:00
* @param dataAppender subclass calls this function zero or more times to provide cooked-data linearly
* @param endianness byte-order to target
* @param platform data-formats to target
* @return true if cook succeeded
*
* This method is called during IProject::cookPath().
* Part of the cooking process may include embedding database-refs to dependencies.
* This method should store the 64-bit value provided by IDataObject::id() when doing this.
*/
2015-07-17 00:02:19 +00:00
virtual bool cookObject(FDataAppender dataAppender,
DataEndianness endianness, DataPlatform platform)
2015-05-21 02:33:05 +00:00
{(void)dataAppender;(void)endianness;(void)platform;return true;}
2015-06-10 02:40:03 +00:00
typedef std::function<void(ObjectBase*)> FDepAdder;
2015-05-21 02:33:05 +00:00
/**
* @brief Optional private method implemented by CProjectObject subclasses to resolve dependencies
* @param depAdder subclass calls this function zero or more times to register each dependency
*
* This method is called during IProject::packagePath().
* Dependencies registered via this method will eventually have this method called on themselves
* as well. This is a non-recursive operation, no need for subclasses to implement recursion-control.
*/
2015-07-17 00:02:19 +00:00
virtual void gatherDeps(FDepAdder depAdder)
2015-05-21 02:33:05 +00:00
{(void)depAdder;}
2015-07-17 00:02:19 +00:00
/**
* @brief Get a packagable FourCC representation of the object's type
* @return FourCC of the type
*/
virtual FourCC getType() const
{return FourCC("NULL");}
2015-05-21 02:33:05 +00:00
public:
2015-06-11 04:55:06 +00:00
ObjectBase(const SystemString& path)
2015-06-09 22:19:59 +00:00
: m_path(path) {}
2015-09-30 06:23:07 +00:00
const SystemString& getPath() const {return m_path;}
2015-06-09 22:19:59 +00:00
2015-05-21 02:33:05 +00:00
};
/**
* @brief Main project interface
*
* Projects are intermediate working directories used for staging
* resources in their ideal editor-formats. This interface exposes all
* primary operations to perform on a given project.
*/
2015-06-09 22:19:59 +00:00
class Project
{
2015-06-11 04:55:06 +00:00
public:
2015-07-18 04:35:01 +00:00
struct ProjectDataSpec
{
const DataSpecEntry& spec;
ProjectPath cookedPath;
bool active;
};
2015-06-11 04:55:06 +00:00
private:
2015-06-10 02:40:03 +00:00
ProjectRootPath m_rootPath;
2015-09-30 06:23:07 +00:00
ProjectPath m_workRoot;
2015-07-18 04:35:01 +00:00
ProjectPath m_dotPath;
ProjectPath m_cookedRoot;
std::vector<ProjectDataSpec> m_compiledSpecs;
public:
2015-06-10 02:40:03 +00:00
Project(const HECL::ProjectRootPath& rootPath);
2015-06-09 22:57:21 +00:00
2015-06-10 23:34:14 +00:00
/**
* @brief Configuration file handle
*
* Holds a path to a line-delimited textual configuration file;
* opening a locked handle for read/write transactions
*/
2015-06-09 22:57:21 +00:00
class ConfigFile
{
2015-06-10 02:40:03 +00:00
SystemString m_filepath;
2015-10-04 04:35:18 +00:00
std::vector<std::string> m_lines;
2015-06-10 23:34:14 +00:00
FILE* m_lockedFile = NULL;
2015-06-09 22:57:21 +00:00
public:
2015-06-12 09:08:49 +00:00
ConfigFile(const Project& project, const SystemString& name,
const SystemString& subdir=_S("/.hecl/"));
2015-10-04 04:35:18 +00:00
std::vector<std::string>& lockAndRead();
2015-06-09 22:57:21 +00:00
void addLine(const std::string& line);
void removeLine(const std::string& refLine);
bool checkForLine(const std::string& refLine);
2015-06-10 23:34:14 +00:00
void unlockAndDiscard();
2015-06-11 09:41:10 +00:00
bool unlockAndCommit();
2015-06-10 23:34:14 +00:00
};
2015-06-11 04:55:06 +00:00
ConfigFile m_specs;
ConfigFile m_paths;
ConfigFile m_groups;
2015-06-10 23:34:14 +00:00
/**
* @brief A rough description of how 'expensive' a given cook operation is
*
* This is used to provide pretty colors during the cook operation
*/
2015-05-21 02:33:05 +00:00
enum Cost
{
2015-05-21 02:33:05 +00:00
C_NONE,
C_LIGHT,
C_MEDIUM,
C_HEAVY
};
/**
* @brief Get the path of the project's root-directory
* @return project root path
2015-05-21 02:33:05 +00:00
*
* Self explanatory
*/
2015-10-01 00:40:06 +00:00
const ProjectRootPath& getProjectRootPath() const {return m_rootPath;}
/**
* @brief Get the path of project's working directory
* @return project working path
*/
const ProjectPath& getProjectWorkingPath() const {return m_workRoot;}
2015-05-21 02:33:05 +00:00
2015-07-18 04:35:01 +00:00
/**
* @brief Get the path of project's cooked directory for a specific DataSpec
* @param DataSpec to retrieve path for
* @return project cooked path
*
2015-10-01 00:40:06 +00:00
* The cooked path matches the directory layout of the working directory
2015-07-18 04:35:01 +00:00
*/
2015-09-30 06:23:07 +00:00
const ProjectPath& getProjectCookedPath(const DataSpecEntry& spec) const
2015-07-18 04:35:01 +00:00
{
for (const ProjectDataSpec& sp : m_compiledSpecs)
if (&sp.spec == &spec)
return sp.cookedPath;
2015-07-22 19:14:50 +00:00
LogModule.report(LogVisor::FatalError, "Unable to find spec '%s'", spec.m_name);
2015-07-18 04:35:01 +00:00
return m_cookedRoot;
}
2015-05-21 02:33:05 +00:00
/**
2015-05-27 09:09:05 +00:00
* @brief Add given file(s) to the database
* @param path file or pattern within project
* @return true on success
2015-05-21 02:33:05 +00:00
*
* This method blocks while object hashing takes place
*/
2015-06-10 23:34:14 +00:00
bool addPaths(const std::vector<ProjectPath>& paths);
/**
2015-05-21 02:33:05 +00:00
* @brief Remove a given file or file-pattern from the database
2015-06-09 22:19:59 +00:00
* @param paths file(s) or pattern(s) within project
2015-05-21 02:33:05 +00:00
* @param recursive traverse into matched subdirectories
* @return true on success
*
* This method will not delete actual working files from the project
* directory. It will delete associated cooked objects though.
*/
2015-06-10 23:34:14 +00:00
bool removePaths(const std::vector<ProjectPath>& paths, bool recursive=false);
2015-05-21 02:33:05 +00:00
/**
* @brief Register a working sub-directory as a Dependency Group
* @param path directory to register as Dependency Group
* @return true on success
*
* Dependency Groups are used at runtime to stage burst load-transactions.
* They may only be added to directories and will automatically claim
* subdirectories as well.
*
* Cooked objects in dependency groups will be packaged contiguously
* and automatically duplicated if shared with other dependency groups.
* This contiguous storage makes for optimal loading from slow block-devices
* like optical drives.
*/
2015-06-10 23:34:14 +00:00
bool addGroup(const ProjectPath& path);
2015-05-21 02:33:05 +00:00
/**
* @brief Unregister a working sub-directory as a dependency group
* @param path directory to unregister as Dependency Group
* @return true on success
*/
2015-06-10 23:34:14 +00:00
bool removeGroup(const ProjectPath& path);
2015-06-09 22:19:59 +00:00
2015-06-12 04:02:23 +00:00
/**
* @brief Re-reads the data store holding user's spec preferences
*
* Call periodically in a long-term use of the HECL::Database::Project class.
* Install filesystem event-hooks if possible.
*/
void rescanDataSpecs();
2015-06-09 22:19:59 +00:00
/**
2015-06-09 23:21:45 +00:00
* @brief Return map populated with dataspecs targetable by this project interface
2015-06-09 22:19:59 +00:00
* @return Platform map with name-string keys and enable-status values
*/
2015-09-30 06:23:07 +00:00
const std::vector<ProjectDataSpec>& getDataSpecs() {return m_compiledSpecs;}
2015-06-09 22:19:59 +00:00
/**
2015-06-09 23:21:45 +00:00
* @brief Enable persistent user preference for particular spec string(s)
* @param specs String(s) representing unique spec(s) from listDataSpecs
2015-06-09 22:19:59 +00:00
* @return true on success
*/
2015-06-11 04:55:06 +00:00
bool enableDataSpecs(const std::vector<SystemString>& specs);
2015-06-09 22:19:59 +00:00
/**
2015-06-09 23:21:45 +00:00
* @brief Disable persistent user preference for particular spec string(s)
* @param specs String(s) representing unique spec(s) from listDataSpecs
2015-06-09 22:19:59 +00:00
* @return true on success
*/
2015-06-11 04:55:06 +00:00
bool disableDataSpecs(const std::vector<SystemString>& specs);
2015-05-21 02:33:05 +00:00
/**
* @brief Begin cook process for specified directory
* @param path directory of intermediates to cook
* @param feedbackCb a callback to run reporting cook-progress
* @param recursive traverse subdirectories to cook as well
* @return true on success
2015-05-21 02:33:05 +00:00
*
* Object cooking is generally an expensive process for large projects.
* This method blocks execution during the procedure, with periodic
* feedback delivered via feedbackCb.
*/
2015-09-30 06:23:07 +00:00
bool cookPath(const ProjectPath& path, FProgress feedbackCb, bool recursive=false);
2015-05-19 21:01:32 +00:00
/**
* @brief Interrupts a cook in progress (call from SIGINT handler)
2015-05-21 02:33:05 +00:00
*
* Database corruption is bad! sqlite is pretty robust at avoiding data corruption,
* but HECL spreads its data objects through the filesystem; this ensures that
* those objects are cleanly finalized or discarded before stopping.
*
* Note that this method returns immediately; the resumed cookPath()
* call will return as quickly as possible.
2015-05-19 21:01:32 +00:00
*/
2015-06-10 23:34:14 +00:00
void interruptCook();
2015-05-19 21:01:32 +00:00
/**
2015-05-21 02:33:05 +00:00
* @brief Delete cooked objects for directory
* @param path directory of intermediates to clean
* @param recursive traverse subdirectories to clean as well
* @return true on success
2015-05-21 02:33:05 +00:00
*
* Developers understand how useful 'clean' is. While ideally not required,
* it's useful for verifying that a rebuild from ground-up is doable.
*/
2015-06-10 23:34:14 +00:00
bool cleanPath(const ProjectPath& path, bool recursive=false);
2015-06-09 22:19:59 +00:00
/**
* @brief Constructs a full depsgraph of the project-subpath provided
* @param path Subpath of project to root depsgraph at
* @return Populated depsgraph ready to traverse
*/
2015-06-10 23:34:14 +00:00
PackageDepsgraph buildPackageDepsgraph(const ProjectPath& path);
2015-05-21 02:33:05 +00:00
};
2015-06-10 02:40:03 +00:00
}
2015-05-17 04:55:29 +00:00
}
2015-05-15 22:39:43 +00:00
#endif // HECLDATABASE_HPP