Skip to content

Commit

Permalink
fix build, compress assets with zstd
Browse files Browse the repository at this point in the history
  • Loading branch information
GEEKiDoS committed Dec 18, 2024
1 parent 3758a2c commit 910f67f
Show file tree
Hide file tree
Showing 54 changed files with 703 additions and 1,201 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
[submodule "deps/ifs_layeredfs"]
path = deps/ifs_layeredfs
url = https://github.com/GEEKiDoS/ifs_layeredfs.git
[submodule "deps/zstd"]
path = deps/zstd
url = https://github.com/facebook/zstd.git
104 changes: 66 additions & 38 deletions assets/launcher-ui/bin/gen-asset
Original file line number Diff line number Diff line change
@@ -1,26 +1,49 @@
#!/usr/bin/env node
import io from 'node:fs';
import mime from 'mime';
import { compress } from '@mongodb-js/zstd';

const assets = io.readdirSync('./dist', {
recursive: true,
}).filter(v => io.statSync('./dist/' + v).isFile());
(async () => {
const assets = io.readdirSync('./dist', {
recursive: true,
}).filter(v => io.statSync('./dist/' + v).isFile());

const rc = '// GENERATED BY npm build\r\n// DO NOT MODIFY\r\n\r\n#include "resource.hpp"\r\n\r\n' + assets
.map((v, i) => `ID_ASSET_${i}\tRCDATA\t"resources/ui-dist/${v.replaceAll('\\', '/')}"`)
.join('\r\n') + '\r\n';
const isCompressed = {};

io.writeFileSync('../../src/client/resource.g.rc', rc);
console.log(rc);
if (io.existsSync('../../src/client/resources/ui-dist/'))
io.rmSync('../../src/client/resources/ui-dist/', { recursive: true, force: true });

const resource = '// GENERATED BY npm build\r\n// DO NOT MODIFY\r\n\r\n' + assets
.map((v, i) => `#define ID_ASSET_${i} ${350 + i}`)
.join('\r\n') + '\r\n';
io.mkdirSync('../../src/client/resources/ui-dist/');
io.mkdirSync('../../src/client/resources/ui-dist/assets');

io.writeFileSync('../../src/client/resource.g.hpp', resource);
console.log(resource);
for (const v of assets) {
const buffer = io.readFileSync('./dist/' + v);
const compressed = await compress(buffer, 22);

const header = `// GENERATED BY npm build
if (compressed.length < buffer.length) {
isCompressed[v] = true;
io.writeFileSync('../../src/client/resources/ui-dist/' + v + '.zst', compressed)
} else {
isCompressed[v] = false;
io.writeFileSync('../../src/client/resources/ui-dist/' + v, buffer)
}
}

const rc = '// GENERATED BY npm build\r\n// DO NOT MODIFY\r\n\r\n#include "resource.hpp"\r\n\r\n' + assets
.map((v, i) => isCompressed[v] ? `ID_ASSET_${i}\tRCDATA\t"resources/ui-dist/${v.replaceAll('\\', '/')}.zst"` : `ID_ASSET_${i}\tRCDATA\t"resources/ui-dist/${v.replaceAll('\\', '/')}"`)
.join('\r\n') + '\r\n';

io.writeFileSync('../../src/client/resource.g.rc', rc);
console.log(rc);

const resource = '// GENERATED BY npm build\r\n// DO NOT MODIFY\r\n\r\n' + assets
.map((v, i) => `#define ID_ASSET_${i} ${350 + i}`)
.join('\r\n') + '\r\n';

io.writeFileSync('../../src/client/resource.g.hpp', resource);
console.log(resource);

const header = `// GENERATED BY npm build
// DO NOT MODIFY
#pragma once
Expand All @@ -31,6 +54,9 @@ const header = `// GENERATED BY npm build
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <utils/compression.hpp>
#include <utils/memory.hpp>
#include "resource.hpp"
namespace laochan::embedded
Expand All @@ -46,42 +72,44 @@ namespace laochan::embedded
return std::span<const std::uint8_t>(reinterpret_cast<std::uint8_t*>(LockResource(handle)), SizeofResource(nullptr, res));
}
auto read_resource_zstd(const int id)
{
auto resource = read_resource(id);
auto decompressed = utils::compression::zstd::decompress(std::string{ LPSTR(resource.data()), resource.size()});
auto buf = utils::memory::allocate<uint8_t>(decompressed.size());
std::memcpy(buf, decompressed.data(), decompressed.size());
return std::span<uint8_t>{ buf, decompressed.size() };
}
inline auto all()
{
std::map<std::string, saucer::embedded_file> rtn;
` + assets
.map((v, i) => {
const type = mime.getType(v);
const id = `ID_ASSET_${i}`;
const path = v.replaceAll('\\', '/');

if (v === "index.html")
return `
auto root = read_resource(${id});
.map((v, i) => {
const type = mime.getType(v);
const id = `ID_ASSET_${i}`;
const path = v.replaceAll('\\', '/');

if (v === "index.html")
return `
auto root = read_resource${isCompressed[v] ? '_zstd' : ''}(${id});
rtn.emplace("${path}", saucer::embedded_file{"${type}", root});
rtn.emplace("", saucer::embedded_file{"${type}", root});
`;
else
return ` rtn.emplace("${path}", saucer::embedded_file{"${type}", read_resource(${id})});`
})
.join('\r\n') + `
else
return ` rtn.emplace("${path}", saucer::embedded_file{"${type}", read_resource${isCompressed[v] ? '_zstd' : ''}(${id})});`
})
.join('\r\n') + `
return rtn;
}
}
`

io.writeFileSync('../../src/client/resources/all.hpp', header);
console.log(header);

if(io.existsSync('../../src/client/resources/ui-dist/'))
io.rmSync('../../src/client/resources/ui-dist/', { recursive: true, force: true });

io.mkdirSync('../../src/client/resources/ui-dist/');
io.mkdirSync('../../src/client/resources/ui-dist/assets');

assets.forEach(v => {
io.copyFileSync('./dist/' + v, '../../src/client/resources/ui-dist/' + v);
});
io.writeFileSync('../../src/client/resources/all.hpp', header);
console.log(header);
})();
Loading

0 comments on commit 910f67f

Please sign in to comment.