-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcover-gen.mjs
187 lines (161 loc) · 4.48 KB
/
cover-gen.mjs
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
import fs from 'fs';
import Sunniesnow from './sunniesnow.mjs';
import DEFAULT_GAME_SETTINGS from './default-settings.mjs'
Sunniesnow.CoverGen = class CoverGen {
static DEFAULT_SETTINGS = {
levelFile: 'upload',
levelFileOnline: '',
levelFileUpload: null,
chartSelect: '',
background: 'from-level',
backgroundOnline: 'default.svg',
backgroundFromLevel: '',
backgroundUpload: null,
backgroundBlur: 100,
backgroundBrightness: 0.5,
skin: 'default',
skinOnline: '',
skinUpload: null,
nickname: 'New Poet',
avatar: 'online',
avatarOnline: 'default.svg',
avatarUpload: null,
avatarGravatar: '',
width: 1920,
height: 1080,
avoidDownloadingFonts: false,
plugin: [],
pluginOnline: [],
pluginUpload: [],
help: false,
quiet: false,
suppressWarnings: false,
tempDir: process.env.TMPDIR || process.env.TEMP || '/tmp',
coverThemeImageX: null,
coverThemeImageY: null,
coverThemeImageWidth: null,
output: process.env.SUNNIESNOW_OUTPUT || 'output.png',
clean: false,
}
static HELP_MESSAGE = `Usage: sunniesnow-cover-gen [options]
--help=false print this message
--quiet=false do not print anything to stdout
--suppress-warnings do not print warnings to stderr
--temp-dir=$TMPDIR directory to store temporary files
--cover-theme-image-x x coordinate of the center of the cropped theme image
--cover-theme-image-y y coordinate of the center of the cropped theme image
--cover-theme-image-width width of the cropped theme image
--output=output.png output file name
--clean=false do not use assets downloaded before
See https://sunniesnow.github.io/game/help about following options:
--level-file
--level-file-online
--level-file-upload
--chart-select
--background
--background-online
--background-from-level
--background-upload
--background-blur
--background-brightness
--skin
--skin-online
--skin-upload
--nickname
--avatar
--avatar-online
--avatar-upload
--avatar-gravatar
--width
--height
--avoid-downloading-fonts
--plugin
--plugin-online
--plugin-upload`
static toBlob(obj) {
if (typeof obj === 'string') {
return new Blob([fs.readFileSync(obj)], {type: mime.getType(obj)});
}
if (obj instanceof Array) {
return obj.map(this.toBlob.bind(this));
}
if (obj instanceof Blob) {
return obj;
}
return new Blob([obj]);
}
static replaceWithBlob(options) {
for (const key in options) {
if (key.endsWith('Upload')) {
options[key] = this.toBlob(options[key]);
}
}
}
constructor(options) {
options = Object.assign({}, this.constructor.DEFAULT_SETTINGS, options);
this.quiet = options.quiet;
delete options.quiet;
this.suppressWarnings = options.suppressWarnings;
delete options.suppressWarnings;
this.tempDir = options.tempDir;
delete options.tempDir;
this.coverThemeImageX = options.coverThemeImageX;
delete options.coverThemeImageX;
this.coverThemeImageY = options.coverThemeImageY;
delete options.coverThemeImageY;
this.coverThemeImageWidth = options.coverThemeImageWidth;
delete options.coverThemeImageWidth;
this.output = options.output;
delete options.output;
this.clean = options.clean;
delete options.clean;
this.constructor.replaceWithBlob(options);
this.gameSettings = options;
}
print(string) {
if (!this.quiet) {
process.stdout.write(string);
}
}
reprint(string) {
if (!this.quiet) {
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(string);
}
}
println(string) {
if (!this.quiet) {
process.stdout.write(string + '\n');
}
}
async load() {
this.println('Loading...')
fs.mkdirSync(this.tempDir, {recursive: true});
Sunniesnow.game = new Sunniesnow.Game();
Sunniesnow.game.settings = Object.assign({}, DEFAULT_GAME_SETTINGS, this.gameSettings);
await Sunniesnow.Loader.loadChart();
Sunniesnow.Loader.load();
await Sunniesnow.Utils.until(time => {
Sunniesnow.game.app?.ticker?.update(time);
return Sunniesnow.game.scene && !(Sunniesnow.game.scene instanceof Sunniesnow.SceneLoading);
});
}
async run() {
await this.load();
const dataUrl = await Sunniesnow.CoverGenerator.generate();
const base64Data = dataUrl.replace(/^data:image\/png;base64,/, '');
fs.writeFileSync(this.output, base64Data, 'base64');
this.println('Done!');
}
static async run(options) {
if (options.help) {
console.log(this.HELP_MESSAGE);
process.exit();
}
Sunniesnow.record = new this(options);
await Sunniesnow.record.run();
process.exit();
}
};
export default Sunniesnow.CoverGen;