NoJobEngine
2.0.7
Toggle main menu visibility
Loading...
Searching...
No Matches
ProjectAssetOperations.cpp
Go to the documentation of this file.
1
#include "
Editor/Assets/ProjectAssetOperations.h
"
2
3
#include "
Engine/Asset/AssetManager.h
"
4
#include "
Engine/Assets/AssetRegistry.h
"
5
#include "
Engine/Assets/PrefabSerializer.h
"
6
#include "
Engine/Scene/Components.h
"
7
#include "
Engine/Scene/Scene.h
"
8
9
#include <algorithm>
10
#include <cctype>
11
#include <fstream>
12
13
namespace
NoJob
14
{
15
std::string
ProjectAssetOperations::SanitizeCppIdentifier
(std::string name)
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
26
bool
ProjectAssetOperations::CreateCppScript
(
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
73
AssetRegistry
registry(
AssetManager::GetAssetsDirectory
());
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
82
std::filesystem::path
ProjectAssetOperations::CreatePrefab
(
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
112
AssetRegistry
registry(
AssetManager::GetAssetsDirectory
());
113
registry.
Load
();
114
registry.
Register
(prefabPath,
AssetType::Prefab
);
115
registry.
Save
();
116
return
prefabPath;
117
}
118
}
AssetManager.h
AssetManager declarations for the Asset subsystem.
AssetRegistry.h
AssetRegistry declarations for the Asset Management subsystem.
Components.h
Components declarations for the Scenes and Native Scripting subsystem.
PrefabSerializer.h
PrefabSerializer declarations for the Asset Management subsystem.
ProjectAssetOperations.h
ProjectAssetOperations declarations for the Asset Management subsystem.
Scene.h
Scene declarations for the Scenes and Native Scripting subsystem.
NoJob::AssetManager::GetAssetsDirectory
static const std::filesystem::path & GetAssetsDirectory()
Definition
AssetManager.cpp:55
NoJob::AssetRegistry
Definition
AssetRegistry.h:15
NoJob::AssetRegistry::Load
bool Load()
Definition
AssetRegistry.cpp:127
NoJob::AssetRegistry::Register
AssetHandle Register(const std::filesystem::path &path, AssetType type=AssetType::Unknown)
Definition
AssetRegistry.cpp:75
NoJob::AssetRegistry::Save
bool Save() const
Definition
AssetRegistry.cpp:118
NoJob::Entity
Definition
Entity.h:16
NoJob::Entity::GetComponent
T & GetComponent()
Definition
Scene.h:851
NoJob::Entity::HasComponent
bool HasComponent() const
Definition
Scene.h:865
NoJob::PrefabSerializer::Save
static bool Save(Entity root, const std::filesystem::path &)
Definition
PrefabSerializer.cpp:122
NoJob::ProjectAssetOperations::LogFunction
std::function< void(std::string)> LogFunction
Definition
ProjectAssetOperations.h:18
NoJob::ProjectAssetOperations::CreatePrefab
static std::filesystem::path CreatePrefab(Entity source, const std::filesystem::path &directory, const LogFunction &log={})
Definition
ProjectAssetOperations.cpp:82
NoJob::ProjectAssetOperations::SanitizeCppIdentifier
static std::string SanitizeCppIdentifier(std::string name)
Definition
ProjectAssetOperations.cpp:15
NoJob::ProjectAssetOperations::CreateCppScript
static bool CreateCppScript(const std::filesystem::path &scriptsDirectory, const std::string &requestedName, const LogFunction &log={})
Definition
ProjectAssetOperations.cpp:26
NoJob
Definition
BTActionNode.h:13
NoJob::AssetType::Script
@ Script
Definition
Asset.h:18
NoJob::AssetType::Prefab
@ Prefab
Definition
Asset.h:18
NoJob::TagComponent
Definition
Components.h:29
Editor
Assets
ProjectAssetOperations.cpp
Generated by
1.18.0