NoJobEngine 2.0.7
Loading...
Searching...
No Matches
ProjectAssetOperations.cpp
Go to the documentation of this file.
2
8
9#include <algorithm>
10#include <cctype>
11#include <fstream>
12
13namespace NoJob
14{
16 {
17 name.erase(std::remove_if(name.begin(), name.end(),
18 [](unsigned char c){ return !(std::isalnum(c) || c == '_'); }),
19 name.end());
20 if (name.empty()) name = "NewScript";
21 if (std::isdigit(static_cast<unsigned char>(name.front())))
22 name.insert(name.begin(), '_');
23 return name;
24 }
25
27 const std::filesystem::path& scriptsDirectory,
28 const std::string& requestedName,
29 const LogFunction& log)
30 {
31 const std::string name = SanitizeCppIdentifier(requestedName);
32 std::error_code ec;
33 std::filesystem::create_directories(scriptsDirectory, ec);
34 if (ec)
35 {
36 if (log) log("[Scripts] Could not create Assets/Scripts: " + ec.message());
37 return false;
38 }
39
40 const auto headerPath = scriptsDirectory / (name + ".h");
41 const auto sourcePath = scriptsDirectory / (name + ".cpp");
42 if (std::filesystem::exists(headerPath) || std::filesystem::exists(sourcePath))
43 return false;
44
45 std::ofstream header(headerPath);
46 std::ofstream source(sourcePath);
47 if (!header || !source)
48 {
49 if (log) log("[Scripts] Could not write script files to: " + scriptsDirectory.string());
50 return false;
51 }
52
53 header << "#pragma once\n"
54 << "#include \"Engine/Scene/ScriptableEntity.h\"\n\n"
55 << "namespace NoJob\n{\n"
56 << " class " << name << " final : public Script\n"
57 << " {\n public:\n"
58 << " void OnCreate() override;\n"
59 << " void OnUpdate(float deltaTime) override;\n"
60 << " void OnDestroy() override;\n"
61 << " };\n}\n";
62
63 source << "#include \"" << name << ".h\"\n"
64 << "#include \"Engine/Scene/ScriptRegistry.h\"\n\n"
65 << "namespace NoJob\n{\n"
66 << " void " << name << "::OnCreate()\n {\n }\n\n"
67 << " void " << name << "::OnUpdate(float deltaTime)\n"
68 << " {\n (void)deltaTime;\n }\n\n"
69 << " void " << name << "::OnDestroy()\n {\n }\n\n"
70 << "}\n\n"
71 << "NOJOB_REGISTER_SCRIPT(" << name << ", \"Gameplay\")\n";
72
74 registry.Load();
75 registry.Register(headerPath, AssetType::Script);
76 registry.Register(sourcePath, AssetType::Script);
77 registry.Save();
78 if (log) log("[Scripts] Created " + name + ".h / " + name + ".cpp in Assets/Scripts.");
79 return true;
80 }
81
83 Entity source, const std::filesystem::path& directory, const LogFunction& log)
84 {
85 if (!source || !source.HasComponent<TagComponent>()) return {};
86
87 auto prefabName = source.GetComponent<TagComponent>().Tag;
88 for (char& c : prefabName)
89 if (c == '/' || c == '\\' || c == ':' || c == '*' || c == '?' ||
90 c == '"' || c == '<' || c == '>' || c == '|') c = '_';
91 if (prefabName.empty()) prefabName = "Prefab";
92
93 std::error_code ec;
94 std::filesystem::create_directories(directory, ec);
95 if (ec)
96 {
97 if (log) log("[Prefab] Could not create target directory: " + ec.message());
98 return {};
99 }
100
101 auto prefabPath = directory / (prefabName + ".nojobprefab");
102 int suffix = 1;
103 while (std::filesystem::exists(prefabPath))
104 prefabPath = directory / (prefabName + " (" + std::to_string(suffix++) + ").nojobprefab");
105
106 if (!PrefabSerializer::Save(source, prefabPath))
107 {
108 if (log) log("[Prefab] Failed to save: " + prefabPath.string());
109 return {};
110 }
111
113 registry.Load();
114 registry.Register(prefabPath, AssetType::Prefab);
115 registry.Save();
116 return prefabPath;
117 }
118}
AssetManager declarations for the Asset subsystem.
AssetRegistry declarations for the Asset Management subsystem.
Components declarations for the Scenes and Native Scripting subsystem.
PrefabSerializer declarations for the Asset Management subsystem.
ProjectAssetOperations declarations for the Asset Management subsystem.
Scene declarations for the Scenes and Native Scripting subsystem.
static const std::filesystem::path & GetAssetsDirectory()
AssetHandle Register(const std::filesystem::path &path, AssetType type=AssetType::Unknown)
T & GetComponent()
Definition Scene.h:851
bool HasComponent() const
Definition Scene.h:865
static bool Save(Entity root, const std::filesystem::path &)
std::function< void(std::string)> LogFunction
static std::filesystem::path CreatePrefab(Entity source, const std::filesystem::path &directory, const LogFunction &log={})
static std::string SanitizeCppIdentifier(std::string name)
static bool CreateCppScript(const std::filesystem::path &scriptsDirectory, const std::string &requestedName, const LogFunction &log={})