source helping stuff

This commit is contained in:
Yoshman29
2022-10-06 19:36:01 +02:00
parent a2f674f412
commit 6709e9cbc2
9 changed files with 211 additions and 76 deletions
+123 -30
View File
@@ -1,44 +1,137 @@
// REDO THIS FOR FILE SUPPORT KINDA LIKE PSYCH BUT NOT MESSY
package funkin.mods;
import lime.utils.Log;
import sys.FileStat;
import sys.FileSystem;
import openfl.utils.AssetLibrary;
import lime.utils.AssetLibrary;
import lime.utils.AssetManifest;
import haxe.io.Path;
import lime.app.Event;
import lime.app.Future;
import lime.app.Promise;
import lime.media.AudioBuffer;
import lime.graphics.Image;
import lime.net.HTTPRequest;
import lime.text.Font;
import lime.utils.AssetType;
import lime.utils.Bytes;
using StringTools;
class ModsAssetLibrary extends AssetLibrary {
private var editedTimes:Map<String, Float> = [];
#if sys
public override function __fromManifest(manifest:AssetManifest) {
super.__fromManifest(manifest);
public var folderPath:String;
public var libName:String;
var basePath = manifest.rootPath;
if (basePath == null) basePath = "";
if (basePath != "") basePath += "/";
var stat:FileStat;
for(asset in manifest.assets) {
var id = asset.id;
var assetPath = __cacheBreak(basePath + Reflect.field(asset, "path"));
try {
stat = FileSystem.stat(assetPath);
if (stat != null) {
editedTimes.set(id, stat.mtime.getTime());
}
} catch(e) {
trace(e);
}
}
public function new(folderPath:String, libName:String) {
this.folderPath = folderPath;
this.libName = libName;
super();
}
#end
#if sys
private var editedTimes:Map<String, Float> = [];
var _parsedAsset:String = null;
public function getEditedTime(asset:String):Null<Float> {
#if sys
return editedTimes[asset];
#else
return null;
#end
}
public override function getAudioBuffer(id:String):AudioBuffer {
if (__isCacheValid(cachedAudioBuffers, id))
return cachedAudioBuffers.get(id);
else {
if (!exists(id, "SOUND")) {
Log.error('ModsAssetLibrary: Audio Buffer at $id does not exist.');
return null;
}
var path = getAssetPath();
editedTimes[id] = FileSystem.stat(path).mtime.getTime();
var e = AudioBuffer.fromFile(path);
cachedAudioBuffers.set(id, e);
return e;
}
}
public override function getBytes(id:String):Bytes {
if (__isCacheValid(cachedBytes, id))
return cachedBytes.get(id);
else {
if (!exists(id, "BINARY")) {
Log.error('ModsAssetLibrary: Bytes at $id does not exist.');
return null;
}
var path = getAssetPath();
editedTimes[id] = FileSystem.stat(path).mtime.getTime();
var e = Bytes.fromFile(path);
cachedBytes.set(id, e);
return e;
}
}
public override function getFont(id:String):Font {
if (__isCacheValid(cachedFonts, id))
return cachedFonts.get(id);
else {
if (!exists(id, "FONT")) {
Log.error('ModsAssetLibrary: Font at $id does not exist.');
return null;
}
var path = getAssetPath();
editedTimes[id] = FileSystem.stat(path).mtime.getTime();
var e = Font.fromFile(path);
cachedFonts.set(id, e);
return e;
}
}
public override function getImage(id:String):Image {
if (__isCacheValid(cachedImages, id))
return cachedImages.get(id);
else {
if (!exists(id, "IMAGE")) {
Log.error('ModsAssetLibrary: Image at $id does not exist.');
return null;
}
var path = getAssetPath();
editedTimes[id] = FileSystem.stat(path).mtime.getTime();
var e = Image.fromFile(path);
cachedImages.set(id, e);
return e;
}
}
public override function getPath(id:String):String {
if (!__parseAsset(id)) return null;
return getAssetPath();
}
public override function exists(asset:String, type:String):Bool {
if(!__parseAsset(asset)) return false;
return FileSystem.exists(getAssetPath());
}
private function getAssetPath() {
return '$folderPath/$_parsedAsset';
}
private function __isCacheValid(cache:Map<String, Dynamic>, asset:String) {
if (!editedTimes.exists(asset))
return false;
if (editedTimes[asset] == null) return true;
if (editedTimes[asset] < FileSystem.stat(getPath(asset)).mtime.getTime()) return false;
return true;
}
private function __parseAsset(asset:String):Bool {
var prefix = 'assets/$libName/';
if (!asset.startsWith(prefix)) return false;
_parsedAsset = asset.substr(prefix.length);
return true;
}
#end
}