UNPKG

4.23 kBJavaScriptView Raw
1import { join as pathJoin, dirname } from 'path';
2import fse from 'fs-extra';
3import { PathType } from "@shockpkg/archive-files/module.mjs";
4import { pathRelativeBase, pathRelativeBaseMatch } from "../util.mjs";
5import { peResourceReplace } from "../util/windows.mjs";
6import { Projector } from "../projector.mjs";
7/**
8 * ProjectorWindows constructor.
9 *
10 * @param path Output path.
11 */
12
13export class ProjectorWindows extends Projector {
14 /**
15 * Icon file.
16 */
17
18 /**
19 * Icon data.
20 */
21
22 /**
23 * Version strings.
24 */
25 constructor(path) {
26 super(path);
27 this.iconFile = null;
28 this.iconData = null;
29 this.versionStrings = null;
30 }
31 /**
32 * Projector file extension.
33 *
34 * @returns File extension.
35 */
36
37
38 get extension() {
39 return '.exe';
40 }
41 /**
42 * Config file newline characters.
43 *
44 * @returns Newline characters.
45 */
46
47
48 get configNewline() {
49 return '\r\n';
50 }
51 /**
52 * Lingo file newline characters.
53 *
54 * @returns Newline characters.
55 */
56
57
58 get lingoNewline() {
59 return '\r\n';
60 }
61 /**
62 * Splash image file extension.
63 *
64 * @returns File extension.
65 */
66
67
68 get splashImageExtension() {
69 return '.BMP';
70 }
71 /**
72 * Get the SKL name.
73 *
74 * @returns File name.
75 */
76
77
78 get sklName() {
79 return 'Projec32.skl';
80 }
81 /**
82 * Get icon data if any specified, from data or file.
83 *
84 * @returns Icon data or null.
85 */
86
87
88 async getIconData() {
89 const {
90 iconData,
91 iconFile
92 } = this;
93 return iconData || (iconFile ? fse.readFile(iconFile) : null);
94 }
95 /**
96 * Write the projector skeleton from archive.
97 *
98 * @param skeleton Skeleton path.
99 */
100
101
102 async _writeSkeleton(skeleton) {
103 const {
104 path,
105 shockwave,
106 sklName,
107 xtrasName,
108 xtrasPath
109 } = this;
110 const xtrasMappings = this.getIncludeXtrasMappings();
111 let foundProjectorSkl = false;
112 let foundXtras = false;
113
114 const xtrasHandler = async entry => {
115 // Check if Xtras path.
116 const xtrasRel = pathRelativeBase(entry.volumePath, xtrasName, true);
117
118 if (xtrasRel === null) {
119 return false;
120 }
121
122 foundXtras = true; // Find output path if being included, else skip.
123
124 const dest = this.includeXtrasMappingsDest(xtrasMappings, xtrasRel);
125
126 if (!dest) {
127 return true;
128 }
129
130 await entry.extract(pathJoin(xtrasPath, dest));
131 return true;
132 };
133
134 const projectorSklHandler = async entry => {
135 const entryPath = entry.volumePath; // Should not be in sub directory.
136
137 if (entryPath.includes('/')) {
138 return false;
139 } // Check if skl path.
140
141
142 if (!pathRelativeBaseMatch(entryPath, sklName, true)) {
143 return false;
144 }
145
146 foundProjectorSkl = true;
147 await entry.extract(path);
148 return true;
149 };
150
151 const projectorDllHandler = async entry => {
152 const entryPath = entry.volumePath; // Should not be in sub directory.
153
154 if (entryPath.includes('/')) {
155 return false;
156 } // Check if dll path.
157
158
159 if (!/\.dll$/i.test(entryPath)) {
160 return false;
161 } // Exclude if shockwave projector.
162
163
164 if (shockwave) {
165 return true;
166 }
167
168 await entry.extract(pathJoin(dirname(path), entryPath));
169 return true;
170 };
171
172 const archive = await this.getSkeletonArchive(skeleton);
173 await archive.read(async entry => {
174 if (entry.type === PathType.RESOURCE_FORK) {
175 return;
176 }
177
178 if (await xtrasHandler(entry)) {
179 return;
180 }
181
182 if (await projectorSklHandler(entry)) {
183 return;
184 }
185
186 if (await projectorDllHandler(entry)) {
187 return;
188 }
189 });
190
191 if (!foundProjectorSkl) {
192 throw new Error(`Failed to locate: ${sklName}`);
193 }
194
195 if (!foundXtras) {
196 throw new Error(`Failed to locate: ${xtrasName}`);
197 }
198 }
199 /**
200 * Modify the projector skeleton.
201 */
202
203
204 async _modifySkeleton() {
205 const iconData = await this.getIconData();
206 const {
207 versionStrings
208 } = this;
209
210 if (!(iconData || versionStrings)) {
211 return;
212 }
213
214 await peResourceReplace(this.path, {
215 iconData,
216 versionStrings
217 });
218 }
219
220}
221//# sourceMappingURL=windows.mjs.map