/
Paths.hx
356 lines (307 loc) · 9.5 KB
/
Paths.hx
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package util;
import data.DataType;
import flash.media.Sound;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.graphics.FlxGraphic;
import flixel.graphics.frames.FlxAtlasFrames;
import flixel.graphics.frames.FlxFrame.FlxFrameAngle;
import flixel.math.FlxPoint;
import flixel.math.FlxRect;
import haxe.xml.Access;
import lime.utils.Assets;
import openfl.display.BitmapData;
import openfl.geom.Rectangle;
import openfl.system.System;
import openfl.utils.AssetType;
import openfl.utils.Assets as OpenFlAssets;
import sys.FileSystem;
import sys.io.File;
using StringTools;
class Paths
{
inline public static var SOUND_EXT = #if web "mp3" #else "ogg" #end;
inline public static var VIDEO_EXT = "mp4";
public static function excludeAsset(key:String)
{
if (!dumpExclusions.contains(key))
dumpExclusions.push(key);
}
public static var dumpExclusions:Array<String> = [
'assets/music/freakyMenu.$SOUND_EXT',
'assets/shared/music/breakfast.$SOUND_EXT',
'assets/shared/music/tea-time.$SOUND_EXT',
];
/// haya I love you for the base cache dump I took to the max
public static function clearUnusedMemory()
{
// clear non local assets in the tracked assets list
for (key in currentTrackedAssets.keys())
{
// if it is not currently contained within the used local assets
if (!localTrackedAssets.contains(key) && !dumpExclusions.contains(key))
{
// get rid of it
var obj = currentTrackedAssets.get(key);
@:privateAccess
if (obj != null)
{
openfl.Assets.cache.removeBitmapData(key);
FlxG.bitmap._cache.remove(key);
obj.destroy();
currentTrackedAssets.remove(key);
}
}
}
// run the garbage collector for good measure lmfao
System.gc();
}
// define the locally tracked assets
public static var localTrackedAssets:Array<String> = [];
public static function clearStoredMemory(?cleanUnused:Bool = false)
{
// clear anything not in the tracked assets list
@:privateAccess
for (key in FlxG.bitmap._cache.keys())
{
var obj = FlxG.bitmap._cache.get(key);
if (obj != null && !currentTrackedAssets.exists(key))
{
openfl.Assets.cache.removeBitmapData(key);
FlxG.bitmap._cache.remove(key);
obj.destroy();
}
}
// clear all sounds that are cached
for (key in currentTrackedSounds.keys())
{
if (!localTrackedAssets.contains(key) && !dumpExclusions.contains(key) && key != null)
{
// trace('test: ' + dumpExclusions, key);
Assets.cache.clear(key);
currentTrackedSounds.remove(key);
}
}
// flags everything to be cleared out next unused memory clear
localTrackedAssets = [];
openfl.Assets.cache.clear("songs");
}
static public var currentModDirectory:String = '';
static public var currentLevel:String;
static public function setCurrentLevel(name:String)
{
currentLevel = name.toLowerCase();
}
public static function getPath(file:String, type:AssetType, ?library:Null<String> = null)
{
if (library != null)
return getLibraryPath(file, library);
if (currentLevel != null)
{
var levelPath:String = '';
if (currentLevel != 'shared')
{
levelPath = getLibraryPathForce(file, currentLevel);
if (OpenFlAssets.exists(levelPath, type))
return levelPath;
}
levelPath = getLibraryPathForce(file, "shared");
if (OpenFlAssets.exists(levelPath, type))
return levelPath;
}
return getPreloadPath(file);
}
static public function getLibraryPath(file:String, library = "preload")
{
return if (library == "preload" || library == "default") getPreloadPath(file); else getLibraryPathForce(file, library);
}
inline static function getLibraryPathForce(file:String, library:String)
{
var returnPath = '$library:assets/$library/$file';
return returnPath;
}
inline public static function getPreloadPath(file:String = '')
{
return 'assets/$file';
}
inline static public function file(file:String, type:AssetType = TEXT, ?library:String)
{
return getPath(file, type, library);
}
inline static public function txt(key:String, ?library:String)
{
return getPath('data/$key.txt', TEXT, library);
}
inline static public function xml(key:String, ?library:String)
{
return getPath('data/$key.xml', TEXT, library);
}
inline static public function json(key:String, ?library:String)
{
return getPath('data/$key.json', TEXT, library);
}
inline static public function hscript(key:String, ?library:String)
{
return getPath('data/$key.hx', TEXT, library);
}
inline static public function shaderFragment(key:String, ?library:String)
{
return getPath('shaders/$key.frag', TEXT, library);
}
inline static public function shaderVertex(key:String, ?library:String)
{
return getPath('shaders/$key.vert', TEXT, library);
}
inline static public function lua(key:String, ?library:String)
{
return getPath('$key.lua', TEXT, library);
}
static public function video(key:String)
{
return 'assets/videos/$key.$VIDEO_EXT';
}
static public function sound(key:String, ?library:String):Sound
{
var sound:Sound = returnSound('sounds', key, library);
return sound;
}
inline static public function soundRandom(key:String, min:Int, max:Int, ?library:String)
{
return sound(key + FlxG.random.int(min, max), library);
}
inline static public function music(key:String, ?library:String):Sound
{
var file:Sound = returnSound('music', key, library);
return file;
}
inline static public function voices(song:String):Any
{
var songKey:String = '${song.toLowerCase().replace(' ', '-')}/Voices';
var voices = returnSound('songs', songKey);
return voices;
}
inline static public function inst(song:String):Any
{
var songKey:String = '${song.toLowerCase().replace(' ', '-')}/Inst';
var inst = returnSound('songs', songKey);
return inst;
}
inline static public function image(key:String, ?library:String):FlxGraphic
{
// streamlined the assets process more
var returnAsset:FlxGraphic = returnGraphic(key, library);
returnAsset.persist = true;
return returnAsset;
}
static public function getTextFromFile(key:String):String
{
#if sys
if (FileSystem.exists(getPreloadPath(key)))
return File.getContent(getPreloadPath(key));
if (currentLevel != null)
{
var levelPath:String = '';
if (currentLevel != 'shared')
{
levelPath = getLibraryPathForce(key, currentLevel);
if (FileSystem.exists(levelPath))
return File.getContent(levelPath);
}
levelPath = getLibraryPathForce(key, 'shared');
if (FileSystem.exists(levelPath))
return File.getContent(levelPath);
}
#end
return Assets.getText(getPath(key, TEXT));
}
inline static public function font(key:String)
{
return 'assets/fonts/$key';
}
inline static public function fileExists(key:String, type:AssetType, ?ignoreMods:Bool = false, ?library:String)
{
if (OpenFlAssets.exists(getPath(key, type)))
{
return true;
}
return false;
}
inline static public function getSparrowAtlas(key:String, ?library:String):FlxAtlasFrames
{
return FlxAtlasFrames.fromSparrow(image(key, library), file('images/$key.xml', library));
}
inline static public function getXMLAtlas(key:String, ?library:String):FlxAtlasFrames
{
return FlxAtlasFrames.fromTexturePackerXml(image(key, library), file('images/$key.xml', library));
}
inline static public function getPackerAtlas(key:String, ?library:String)
{
return FlxAtlasFrames.fromSpriteSheetPacker(image(key, library), file('images/$key.txt', library));
}
inline static public function getJSONAtlas(key:String, ?library:String)
{
return FlxAtlasFrames.fromTexturePackerJson(image(key, library), file('images/$key.json', library));
}
inline static public function formatToSongPath(path:String)
{
return path.toLowerCase().replace(' ', '-');
}
// completely rewritten asset loading? fuck!
public static var currentTrackedAssets:Map<String, FlxGraphic> = [];
public static function returnGraphic(key:String, ?library:String)
{
var path = getPath('images/$key.png', IMAGE, library);
if (OpenFlAssets.exists(path, IMAGE))
{
if (!currentTrackedAssets.exists(path))
{
var newGraphic:FlxGraphic = FlxG.bitmap.add(path, false, path);
newGraphic.persist = true;
currentTrackedAssets.set(path, newGraphic);
}
localTrackedAssets.push(path);
return currentTrackedAssets.get(path);
}
trace('oh no its returning null NOOOO $path', currentLevel); // MORE INFO CAUSE IDK WHATS WRONG OTHER WISE
return null;
}
public static var currentTrackedSounds:Map<String, Sound> = [];
public static function returnSound(path:String, key:String, ?library:String)
{
var file = null;
if (FileSystem.exists(file))
{
if (!currentTrackedSounds.exists(file))
{
currentTrackedSounds.set(file, Sound.fromFile(file));
}
localTrackedAssets.push(key);
return currentTrackedSounds.get(file);
}
// I hate this so god damn much
var gottenPath:String = getPath('$path/$key.$SOUND_EXT', SOUND, library);
gottenPath = gottenPath.substring(gottenPath.indexOf(':') + 1, gottenPath.length);
// trace(gottenPath);
if (!currentTrackedSounds.exists(gottenPath))
currentTrackedSounds.set(gottenPath, Sound.fromFile('./' + gottenPath));
// #else
// currentTrackedSounds.set(gottenPath, OpenFlAssets.getSound(getPath('$path/$key.$SOUND_EXT', SOUND, library)));
// #end
localTrackedAssets.push(key);
return currentTrackedSounds.get(gottenPath);
}
inline static public function getAtlasFromData(key:String, data:DataType)
{
switch (data)
{
case SPARROW:
return getSparrowAtlas(key);
case GENERICXML:
return getXMLAtlas(key);
case PACKER:
return getPackerAtlas(key);
case JSON:
return getJSONAtlas(key);
}
}
}