-
Notifications
You must be signed in to change notification settings - Fork 0
/
kincfile.js
222 lines (184 loc) · 6.84 KB
/
kincfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const fs = require('fs');
const path = require("path");
function gen_push_line(append) {
return append + "\n";
}
function getFileType(DirEnt_like)
{
return DirEnt_like.name.split(".")[1];
}
function get_all_subdirectories(path_inc)
{
let files = fs.readdirSync(path_inc, {withFileTypes: true});
let directories = files.filter(file => file.isDirectory());
let all_directories = [];
if (directories.length > 0)
{
for (const directory of directories)
{
all_directories.push(path_inc + directory.name + "/");
all_directories = all_directories.concat(get_all_subdirectories(path_inc + directory.name + "/"));
}
}
return all_directories;
}
//We'll use this format to write out all of our assets to file
//Same thing for sound, music, etc.
//We'll need to expand this to handle recursive directories in images and sounds and such, maybe
//Ok, it works, but we need to make it work for any resource type
//Gotta remove the hardcoding and stuff
//Yikes.
//What a terrible fate.
function generate_resource_files(
base_search_path,
asset_type,
arr_of_filetypes,
enum_prefix,
enum_name,
file_gen_comment,
generated_filename
)
{
if (fs.existsSync(base_search_path))
{
const basedir_path = [base_search_path];
//First, acquire the pathnames to every single asset
let subdir_paths = get_all_subdirectories(base_search_path);
let all_paths = basedir_path.concat(subdir_paths);
let image_enums = [];
let image_enum_names = [];
let image_paths = [];
for (const a_path of all_paths)
{
let directory_contents = fs.readdirSync(a_path, {withFileTypes: true});
for (const file of directory_contents)
{
if (!file.isFile()) continue;
let filetype = path.extname(file.name);
if (arr_of_filetypes.includes(filetype))
{
const base_file_path = a_path;
const asset_string_path = "\"" + base_file_path.split("Sources/")[1] + file.name + "\"";
const resource_relative_path = base_file_path.split("Sources/assets/images/")[1] + file.name;
const enum_postfix = resource_relative_path.replace(/\//g, "_").toUpperCase().split(".")[0];
image_paths.push(asset_string_path);
image_enums.push(enum_prefix + enum_postfix);
image_enum_names.push("\"" + enum_prefix + enum_postfix + "\"")
}
}
}
let autogenerated_text = "";
let header_file = "";
let object_file = "";
autogenerated_text += gen_push_line("/*");
autogenerated_text += gen_push_line(" NOTE: THIS FILE IS AUTOGENERATED VIA KMAKE");
autogenerated_text += gen_push_line(" THIS FILE SHOULD NOT BE EDITED MANUALLY");
autogenerated_text += gen_push_line("");
autogenerated_text += gen_push_line(" " + file_gen_comment);
autogenerated_text += gen_push_line(" Generated at: " + new Date().toDateString());
autogenerated_text += gen_push_line("*/");
autogenerated_text += gen_push_line("");
//------
//Start header file generation
//------
let function_and_type = "varia_resource_get_" + asset_type;
let refname = "" + asset_type + "_ref";
header_file += autogenerated_text;
header_file += gen_push_line("#pragma once");
header_file += gen_push_line("");
header_file += gen_push_line("typedef enum");
header_file += gen_push_line("{");
for (const elem of image_enums)
{
header_file += gen_push_line("\t" + elem + ",");
}
header_file += gen_push_line("");
header_file += gen_push_line(" " + enum_prefix + "MAXIMUM_COUNT");
header_file += gen_push_line("}");
header_file += gen_push_line(enum_name + ";");
header_file += gen_push_line("");
header_file += gen_push_line("char const * "+ function_and_type + "(" + enum_name + " " + refname + ");");
header_file += gen_push_line("char const * " + function_and_type + "_enum_name(" + enum_name + " " + refname + ");");
//-----
//End header file generation
//-----
//------
//Start object file generation
//------
object_file += autogenerated_text;
object_file += gen_push_line("#include \"assets/" + generated_filename + ".h\"");
object_file += gen_push_line("");
object_file += gen_push_line("static char const * " + enum_prefix.toLowerCase() + "enum_names[] = {");
for (const elem of image_enum_names)
{
object_file += gen_push_line("\t" + elem + ",");
}
object_file += gen_push_line("};");
object_file += gen_push_line("");
object_file += gen_push_line("static char const * " + enum_prefix.toLowerCase() + "paths[] = {");
for (const elem of image_paths)
{
object_file += gen_push_line("\t" + elem + ",");
}
object_file += gen_push_line("};");
object_file += gen_push_line("");
object_file += gen_push_line("char const * "+ function_and_type + "(" + enum_name + " " + refname + ")");
object_file += gen_push_line("{");
// object_file += gen_push_line(" return global_image_resources_paths[image_ref];");
object_file += gen_push_line(" return " + enum_prefix.toLowerCase() +"paths[" + refname +"];");
object_file += gen_push_line("}");
object_file += gen_push_line("");
object_file += gen_push_line("char const * " + function_and_type + "_enum_name(" + enum_name + " " + refname + ")");
object_file += gen_push_line("{");
object_file += gen_push_line(" return " + enum_prefix.toLowerCase() + "enum_names[" + refname + "];");
object_file += gen_push_line("}");
object_file += gen_push_line("");
//------
//End object file generation
//------
//Now, we want to generate the appropriate directory + asset name here, including filetype
fs.writeFileSync("Sources/assets/" + generated_filename + ".h", header_file);
fs.writeFileSync("Sources/assets/" + generated_filename + ".c", object_file);
}
}
//Image resource scan
generate_resource_files(
"Sources/assets/images/",
"image",
[".k", ".png"],
"VARIA_IMAGE_RESOURCE_",
"varia_image_resource_e",
"Autogenerated list of image resources within tectonic.",
"image_resources"
);
// Other resource scans can begin here
// generate_resource_files(
// "Sources/assets/images/",
// "music",
// [".k"],
// "TEC_MUSIC_RESOURCE_",
// "tec_music_resource_e",
// "Autogenerated list of music",
// "music_resources"
// )
/*
***************************************************
***************************************************
********* ********
********* Actual Project Instantiation ********
********* ********
***************************************************
***************************************************
*/
let project = new Project('Varia');
project.addFile('Sources/**');
project.addFiles('Sources/shaders/**');
project.addFiles('Sources/assets/**');
project.addIncludeDir('Sources');
project.addDefine('VARIA_LOG_LOGGING_ENABLED=1');
// project.addDefine('VARIA_DISABLE_UNFORCED_ASSERTS=1');
project.setDebugDir('Deployment');
resolve(project);
//Possible flags are:
//TEC_DISABLE_UNFORCED_ASSERTS to disable any non-mandatory assertions
//TEC_DISABLE_TESTS to disable the runtime tests launched before main