Skip to content

Commit

Permalink
Merge pull request #39 from anno-mods/devel/local-bundles
Browse files Browse the repository at this point in the history
support local bundles
  • Loading branch information
jakobharder authored May 28, 2023
2 parents 69f76b3 + 9cc9e42 commit 0726717
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how

## 1.12 Simplified Build and Deploy

- 1.12.9: Support local bundles
- 1.12.8: Treat icon LOD generation under `data/ui` differently
- 1.12.7: Updated xmltest, annodiff to GU17.1
- 1.12.6: Use same resolution for icon LODs not placed in data/ui/2kimages
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "anno-modding-tools",
"displayName": "Anno Modding Tools",
"description": "Modding tools for Anno 1800",
"version": "1.12.8",
"version": "1.12.9",
"publisher": "JakobHarder",
"repository": {
"type": "git",
Expand Down
55 changes: 41 additions & 14 deletions src/builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class ModBuilder {
converter.init(this._logger, this._asAbsolutePath);
}

public async build(filePath: string): Promise<boolean> {
public async build(filePath: string, targetFolder?: string): Promise<boolean> {
this._logger.log('Build ' + filePath);
const modJson = utils.readModinfo(path.dirname(filePath));
if (!modJson) {
Expand All @@ -68,7 +68,7 @@ export class ModBuilder {
return false;
}

const outFolder = this._getOutFolder(filePath, modJson);
const outFolder = targetFolder ?? this._getOutFolder(filePath, modJson);
const cache = path.join(path.dirname(filePath), '.modcache');
this._logger.log('Target folder: ' + outFolder);
utils.ensureDir(outFolder);
Expand All @@ -85,7 +85,7 @@ export class ModBuilder {
},
{
"action": "static",
"pattern": "{banner.*,content*.txt,README.md,data/config/**/*,**/*.include.xml}"
"pattern": "{banner.*,content*.txt,README.md,data/config/**/*,**/*.include.xml,**/icon*.png}"
},
{
"action": "cf7",
Expand Down Expand Up @@ -158,18 +158,11 @@ export class ModBuilder {

if (modJson.bundle) {
for (const bundle of modJson.bundle) {
const fileName = path.basename(bundle);
const version = path.basename(path.dirname(bundle)).replace(/[^\w\-]/g, '');
const targetPath = path.join(cache, 'downloads', utils.insertEnding(fileName, '-' + version));
if (!fs.existsSync(targetPath)) {
this._logger.log(` * download ${version}/${fileName}`);
utils.downloadFile(bundle, targetPath, this._logger);
if (bundle.startsWith('.')) {
await this._buildBundle(filePath, bundle, cache, outFolder);
} else {
this._downloadBundle(bundle, cache, outFolder);
}
else {
this._logger.log(` * skip download of ${version}/${fileName}`);
}
this._logger.log(` <= extract content`);
utils.extractZip(targetPath, outFolder, this._logger);
}
}

Expand Down Expand Up @@ -200,6 +193,40 @@ export class ModBuilder {
return true;
}

private async _buildBundle(bundleTarget: string, bundle: string, cache: string, outFolder: string) {
const modinfoPath = path.join(path.dirname(bundleTarget), bundle, 'modinfo.json');
if (!fs.existsSync(modinfoPath)) {
this._logger.error(` cannot bundle ${bundle}`);
return;
}

const modinfo = utils.readModinfo(path.dirname(modinfoPath));
if (!modinfo) {
this._logger.error(` cannot bundle ${bundle}`);
return;
}

const modName = this._getModName(modinfoPath, modinfo.modinfo);
const targetFolder = path.join(outFolder, modName);

await this.build(modinfoPath, targetFolder);
}

private _downloadBundle(bundle: string, cache: string, outFolder: string) {
const fileName = path.basename(bundle);
const version = path.basename(path.dirname(bundle)).replace(/[^\w\-]/g, '');
const targetPath = path.join(cache, 'downloads', utils.insertEnding(fileName, '-' + version));
if (!fs.existsSync(targetPath)) {
this._logger.log(` * download ${version}/${fileName}`);
utils.downloadFile(bundle, targetPath, this._logger);
}
else {
this._logger.log(` * skip download of ${version}/${fileName}`);
}
this._logger.log(` <= extract content`);
utils.extractZip(targetPath, outFolder, this._logger);
}

private _getOutFolder(filePath: string, modJson: any) {
let outFolder = modJson.out ?? '${annoMods}/${modName}';
outFolder = outFolder.replace('${modName}', this._getModName(filePath, modJson.modinfo ?? modJson));
Expand Down
7 changes: 4 additions & 3 deletions src/other/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,10 @@ export function readModinfo(modPath: string): IModinfo | undefined {
}
if (result.modinfo.ModDependencies && Array.isArray(result.modinfo.ModDependencies)) {
for (let i = 0; i < result.modinfo?.ModDependencies.length; i++) {
if (result.modinfo.ModDependencies[i].startsWith("http")) {
result.bundle.push(result.modinfo.ModDependencies[i]);
result.modinfo.ModDependencies[i] = path.basename(result.modinfo.ModDependencies[i], '.zip');
const dep = result.modinfo.ModDependencies[i];
if (dep.startsWith("http") || dep.startsWith(".")) {
result.bundle.push(dep);
result.modinfo.ModDependencies[i] = path.basename(dep, '.zip');
}
}
}
Expand Down

0 comments on commit 0726717

Please sign in to comment.