forked from Kode/khamake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlashExporter.js
161 lines (136 loc) · 4.92 KB
/
FlashExporter.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
"use strict";
const path = require('path');
const KhaExporter = require('./KhaExporter.js');
const Converter = require('./Converter.js');
const Files = require('./Files.js');
const Haxe = require('./Haxe.js');
const Options = require('./Options.js');
const Paths = require('./Paths.js');
const exportImage = require('./ImageTool.js');
const HaxeProject = require('./HaxeProject.js');
const fs = require('fs-extra');
function adjustFilename(filename) {
filename = filename.replaceAll('.', '_');
filename = filename.replaceAll('-', '_');
filename = filename.replaceAll('/', '_');
return filename;
}
class FlashExporter extends KhaExporter {
constructor(khaDirectory, directory, embedflashassets) {
super(khaDirectory, directory);
this.directory = directory;
this.embed = embedflashassets;
this.images = [];
this.sounds = [];
this.blobs = [];
this.addSourceDirectory(path.join(khaDirectory.toString(), 'Backends/Flash'));
}
sysdir() {
return 'flash';
}
exportSolution(name, platform, khaDirectory, haxeDirectory, from, targetOptions) {
let defines = [
'swf-script-timeout=60',
'sys_' + platform,
'sys_g1', 'sys_g2', 'sys_g3', 'sys_g4',
'sys_a1', 'sys_a2'
];
if (this.embed) defines.push('KHA_EMBEDDED_ASSETS');
let defaultFlashOptions = {
framerate : 60,
stageBackground : 'ffffff',
swfVersion : '16.0'
}
let flashOptions = targetOptions ? targetOptions.flash ? targetOptions.flash : defaultFlashOptions : defaultFlashOptions;
const options = {
from: from.toString(),
to: path.join(this.sysdir(), 'kha.swf'),
sources: this.sources,
defines: defines,
parameters: this.parameters,
haxeDirectory: haxeDirectory.toString(),
system: this.sysdir(),
language: 'as',
width: this.width,
height: this.height,
name: name,
framerate: 'framerate' in flashOptions ? flashOptions.framerate : defaultFlashOptions.framerate,
stageBackground: 'stageBackground' in flashOptions ? flashOptions.stageBackground : defaultFlashOptions.stageBackground,
swfVersion : 'swfVersion' in flashOptions ? flashOptions.swfVersion : defaultFlashOptions.swfVersion
};
HaxeProject(this.directory.toString(), options);
if (this.embed) {
this.writeFile(this.directory.resolve(Paths.get("..", "Sources", "Assets.hx")));
this.p("package;");
this.p();
this.p("import flash.display.BitmapData;");
this.p("import flash.media.Sound;");
this.p("import flash.utils.ByteArray;");
this.p();
for (let image of this.images) {
this.p("@:bitmap(\"flash/" + image + "\") class Assets_" + adjustFilename(image) + " extends BitmapData { }");
}
this.p();
for (let sound of this.sounds) {
this.p("@:file(\"flash/" + sound + "\") class Assets_" + adjustFilename(sound) + " extends ByteArray { }");
}
this.p();
for (let blob of this.blobs) {
this.p("@:file(\"flash/" + blob + "\") class Assets_" + adjustFilename(blob) + " extends ByteArray { }");
}
this.p();
this.p("class Assets {");
this.p("public static function visit(): Void {", 1);
this.p("", 2);
this.p("}", 1);
this.p("}");
this.closeFile();
}
if (Options.compilation) {
return Haxe.executeHaxe(this.directory, haxeDirectory, ['project-' + this.sysdir() + '.hxml']);
}
else {
return 0;
}
}
/*copyMusic(platform, from, to, encoders) {
Files.createDirectories(this.directory.resolve(this.sysdir()).resolve(to).parent());
var ogg = Converter.convert(from, this.directory.resolve(this.sysdir()).resolve(to + '.ogg'), encoders.oggEncoder);
var mp3 = Converter.convert(from, this.directory.resolve(this.sysdir()).resolve(to + '.mp3'), encoders.mp3Encoder);
var files = [];
if (ogg) {
files.push(to + '.ogg');
if (this.embed) this.sounds.push(to + '.ogg');
}
if (mp3) {
files.push(to + '.mp3');
if (this.embed) this.sounds.push(to + '.mp3');
}
return files;
}*/
copySound(platform, from, to, encoders) {
Files.createDirectories(this.directory.resolve(this.sysdir()).resolve(to).parent());
Converter.convert(from, this.directory.resolve(this.sysdir()).resolve(to + '.ogg'), encoders.oggEncoder);
if (this.embed) this.sounds.push(to + '.ogg');
return [to + '.ogg'];
}
copyImage(platform, from, to, asset) {
let format = exportImage(from, this.directory.resolve(this.sysdir()).resolve(to), asset, undefined, false);
if (this.embed) this.images.push(to + '.' + format);
return [to + '.' + format];
}
copyBlob(platform, from, to) {
fs.copySync(from.toString(), this.directory.resolve(this.sysdir()).resolve(to).toString(), { clobber: true });
if (this.embed) this.blobs.push(to);
return [to];
}
copyVideo(platform, from, to, encoders) {
Files.createDirectories(this.directory.resolve(this.sysdir()).resolve(to).parent());
Converter.convert(from, this.directory.resolve(this.sysdir()).resolve(to + '.mp4'), encoders.h264Encoder);
return [to + '.mp4'];
}
addShader(shader) {
if (this.embed) this.blobs.push(shader);
}
}
module.exports = FlashExporter;