28 this.rootUrl =
'https://api.matlib.gpuopen.com/api';
29 this.url = `${this.rootUrl}/materials`;
30 this.packageUrl = `${this.rootUrl}/packages`;
31 this.materials =
null;
32 this.materialNames =
null;
34 this.logger = console;
40 loadMaterialsFromCache() {
41 if (fs.existsSync(MATERIALS_CACHE_FILE)) {
43 const data = fs.readFileSync(MATERIALS_CACHE_FILE,
'utf8');
44 this.materials = JSON.parse(data);
46 this.materialNames = [];
47 for (
const jsonData of this.materials) {
48 for (
const material of jsonData.results) {
49 this.materialNames.push(material[
'title']);
52 this.logger.info(`Loaded GPUOpen materials from cache: ${MATERIALS_CACHE_FILE}`);
54 this.logger.warn(`Failed to load GPUOpen materials cache: ${e.message}`);
66 fs.writeFileSync(MATERIALS_CACHE_FILE, JSON.stringify(
this.materials,
null, 2));
67 this.logger.info(`Saved GPUOpen materials to cache: ${MATERIALS_CACHE_FILE}`);
69 this.logger.warn(`Failed to write GPUOpen materials cache: ${e.message}`);
71 return this.materials;
79 return this.materialNames;
89 if (fs.existsSync(MATERIALS_CACHE_FILE)) {
91 const data = fs.readFileSync(MATERIALS_CACHE_FILE,
'utf8');
92 this.materials = JSON.parse(data);
94 this.materialNames = [];
95 for (
const jsonData of this.materials) {
96 for (
const material of jsonData.results) {
97 this.materialNames.push(material[
'title']);
100 this.logger.info(`Loaded GPUOpen materials from cache: ${MATERIALS_CACHE_FILE}`);
101 return this.materials;
103 this.logger.warn(`Failed to load GPUOpen materials cache: ${e.message}`);
109 this.materialNames = [];
112 let params =
new URLSearchParams({
117 url +=
'?' + params.toString();
120 let haveMoreMaterials =
true;
121 while (haveMoreMaterials) {
123 console.log(
'Fetch materials from url:', url);
124 const response = await fetch(url);
126 throw new Error(`HTTP error! status: ${response.status}`);
128 const jsonData = await response.json();
129 this.materials.push(jsonData);
130 for (
const material of jsonData.results) {
131 this.materialNames.push(material[
'title']);
133 let nextURL = jsonData.next;
136 haveMoreMaterials =
true;
138 console.log(
'Finished fetching materials');
139 haveMoreMaterials =
false;
143 this.logger.info(`Error: ${error.message}`);
144 haveMoreMaterials =
true;
149 fs.writeFileSync(MATERIALS_CACHE_FILE, JSON.stringify(
this.materials,
null, 2));
150 this.logger.info(`Saved GPUOpen materials to cache: ${MATERIALS_CACHE_FILE}`);
152 this.logger.warn(`Failed to write GPUOpen materials cache: ${e.message}`);
154 return this.materials;
157 async downloadPackage(listNumber, materialNumber, packageId = 0) {
158 if (this.materials ===
null || this.materials.length === 0) {
162 const jsonData = this.materials[listNumber];
167 let jsonResults =
null;
168 let jsonResult =
null;
169 if (
'results' in jsonData) {
170 jsonResults = jsonData[
'results'];
171 if (jsonResults.length <= materialNumber) {
174 jsonResult = jsonResults[materialNumber];
182 let jsonPackages =
null;
183 if (
'packages' in jsonResult) {
184 jsonPackages = jsonResult[
'packages'];
190 if (jsonPackages.length <= packageId) {
193 const packageIdValue = jsonPackages[packageId];
195 if (!packageIdValue) {
199 const url = `${this.packageUrl}/${packageIdValue}/download`
200 const response = await fetch(url);
201 const data = await response.arrayBuffer();
202 const title = jsonResult[
'title'];
204 console.log(`- Loader: Downloaded package: ${title} from ${url}`);
205 return [data, title];
216 if (!this.materials) {
220 const materialsList = [];
224 const regex =
new RegExp(materialName,
'i');
226 this.materials.forEach((materialList, materialIndex) => {
227 let materialNumber = 0;
229 materialList[
'results'].forEach((material) => {
232 if (regex.test(material[
'title'])) {
234 listNumber: listNumber,
235 materialNumber: materialNumber,
236 title: material[
'title'],
245 return materialsList;
257 const downloadList = [];
260 if (foundList.length > 0) {
261 for (
const found of foundList) {
262 const listNumber = found[
'listNumber'];
263 const materialNumber = found[
'materialNumber'];
264 const matName = found[
'title'];
265 this.logger.info(`> Download material: ${matName} List: ${listNumber}. Index: ${materialNumber}`);
266 const [data, title] = await this.downloadPackage(listNumber, materialNumber, packageId);
267 downloadList.push([data, title]);