14
14
15
15
#include " amber_script.h"
16
16
17
- #include " src/make_unique.h"
18
-
19
17
namespace amber {
20
18
namespace android {
21
19
namespace {
@@ -28,33 +26,39 @@ const char kShaderExtension[] = ".spv";
28
26
bool IsEndedWith (const std::string& path, const std::string& end) {
29
27
const size_t path_size = path.size ();
30
28
const size_t end_size = end.size ();
31
- if (path_size < end_size)
29
+ if (path_size < end_size) {
32
30
return false ;
31
+ }
33
32
34
33
return path.compare (path_size - end_size, end_size, end) == 0 ;
35
34
}
36
35
37
36
bool IsStartedWith (const std::string& path, const std::string& start) {
38
37
const size_t path_size = path.size ();
39
38
const size_t start_size = start.size ();
40
- if (path_size < start_size)
39
+ if (path_size < start_size) {
41
40
return false ;
41
+ }
42
42
43
43
return path.compare (0 , start_size, start) == 0 ;
44
44
}
45
45
46
46
std::string GetShaderID (const std::string& shader_name) {
47
47
size_t spv_extension_pos = shader_name.find_last_of (' .' );
48
- if (spv_extension_pos == std::string::npos)
48
+ if (spv_extension_pos == std::string::npos) {
49
49
return std::string ();
50
+ }
50
51
51
52
size_t shader_id_pos =
52
53
shader_name.find_last_of (' .' , spv_extension_pos - 1UL ) + 1UL ;
53
- if (shader_id_pos == std::string::npos)
54
+ if (shader_id_pos == std::string::npos) {
54
55
return std::string ();
56
+ }
55
57
56
- if (shader_id_pos >= spv_extension_pos || shader_name.size () <= shader_id_pos)
58
+ if (shader_id_pos >= spv_extension_pos ||
59
+ shader_name.size () <= shader_id_pos) {
57
60
return std::string ();
61
+ }
58
62
59
63
return shader_name.substr (shader_id_pos, spv_extension_pos - shader_id_pos);
60
64
}
@@ -67,27 +71,32 @@ AmberScriptLoader::~AmberScriptLoader() = default;
67
71
68
72
Result AmberScriptLoader::LoadAllScriptsFromAsset () {
69
73
auto shader_names = FindAllScriptsAndReturnShaderNames ();
70
- if (script_info_.empty ())
74
+ if (script_info_.empty ()) {
71
75
return Result (" No Amber script found" );
76
+ }
72
77
73
78
for (auto & info : script_info_) {
74
79
info.script_content = ReadScript (info.asset_name );
75
- if (info.script_content .empty ())
80
+ if (info.script_content .empty ()) {
76
81
return Result (info.asset_name + " :\n\t Empty Amber script" );
82
+ }
77
83
}
78
84
79
85
for (auto & info : script_info_) {
80
86
for (const auto & shader : shader_names) {
81
- if (!IsStartedWith (shader, info.asset_name + kShaderNameSignature ))
87
+ if (!IsStartedWith (shader, info.asset_name + kShaderNameSignature )) {
82
88
continue ;
89
+ }
83
90
84
91
auto shader_content = ReadSpvShader (shader);
85
- if (shader_content.empty ())
92
+ if (shader_content.empty ()) {
86
93
return Result (shader + " :\n\t Empty shader" );
94
+ }
87
95
88
96
auto id = GetShaderID (shader);
89
- if (id.empty ())
97
+ if (id.empty ()) {
90
98
return Result (shader + " :\n\t Fail to get shader ID" );
99
+ }
91
100
92
101
info.shader_map [id] = shader_content;
93
102
}
@@ -110,8 +119,9 @@ AmberScriptLoader::FindAllScriptsAndReturnShaderNames() {
110
119
script_info_.back ().asset_name = file_name_in_string;
111
120
}
112
121
113
- if (IsEndedWith (file_name_in_string, kShaderExtension ))
122
+ if (IsEndedWith (file_name_in_string, kShaderExtension )) {
114
123
shaders.push_back (file_name_in_string);
124
+ }
115
125
}
116
126
AAssetDir_close (asset);
117
127
@@ -123,8 +133,9 @@ std::vector<uint8_t> AmberScriptLoader::ReadContent(
123
133
auto asset_path = kAmberDir + asset_name;
124
134
AAsset* asset = AAssetManager_open (app_context_->activity ->assetManager ,
125
135
asset_path.c_str (), AASSET_MODE_BUFFER);
126
- if (!asset)
136
+ if (!asset) {
127
137
return std::vector<uint8_t >();
138
+ }
128
139
129
140
size_t size_in_bytes = AAsset_getLength (asset);
130
141
@@ -145,8 +156,9 @@ std::string AmberScriptLoader::ReadScript(const std::string& script_name) {
145
156
std::vector<uint32_t > AmberScriptLoader::ReadSpvShader (
146
157
const std::string& shader_name) {
147
158
auto content = ReadContent (shader_name);
148
- if (content.size () % sizeof (uint32_t ) != 0 )
159
+ if (content.size () % sizeof (uint32_t ) != 0 ) {
149
160
return std::vector<uint32_t >();
161
+ }
150
162
151
163
return std::vector<uint32_t >(
152
164
reinterpret_cast <uint32_t *>(content.data ()),
0 commit comments