built-in FuzzyMap match serializing
This commit is contained in:
@@ -51,7 +51,12 @@ abstract FuzzyMap<T>(StringMap<T>) from StringMap<T> to StringMap<T> {
|
||||
|
||||
@:arrayAccess
|
||||
public inline function get(fuzzySearchKey:String):Null<T> {
|
||||
return this.get(bestMatch(fuzzySearchKey));
|
||||
var match = bestMatch(fuzzySearchKey);
|
||||
var value = this.get(match);
|
||||
if (match != null) {
|
||||
FuzzyMapTools.onMatchMade(this, fuzzySearchKey, value);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public inline function remove(fuzzySearchKey:String):Bool {
|
||||
|
39
kiss/src/kiss/FuzzyMapTools.hx
Normal file
39
kiss/src/kiss/FuzzyMapTools.hx
Normal file
@@ -0,0 +1,39 @@
|
||||
package kiss;
|
||||
|
||||
import haxe.Json;
|
||||
import haxe.ds.StringMap;
|
||||
|
||||
typedef MapInfo = {
|
||||
file:String,
|
||||
matches:Map<String,Dynamic>
|
||||
};
|
||||
|
||||
/**
|
||||
* FuzzyMap is highly inefficient, so you may wish to memoize the matches that it makes before
|
||||
* releasing your project. FuzzyMapTools helps with this
|
||||
*/
|
||||
class FuzzyMapTools {
|
||||
static var serializingMaps = new Map<StringMap<Dynamic>, MapInfo>();
|
||||
|
||||
public static function serializeMatches(m:StringMap<Dynamic>, file:String) {
|
||||
serializingMaps[m] = { file: file, matches: new Map() };
|
||||
}
|
||||
|
||||
@:allow(kiss.FuzzyMap)
|
||||
static function onMatchMade(m:StringMap<Dynamic>, key:String, value:Dynamic) {
|
||||
#if (sys || hxnodejs)
|
||||
if (serializingMaps.exists(m)) {
|
||||
var info = serializingMaps[m];
|
||||
info.matches[key] = value;
|
||||
sys.io.File.saveContent(info.file, Json.stringify(info.matches));
|
||||
}
|
||||
#end
|
||||
}
|
||||
|
||||
public static function loadMatches(m:StringMap<Dynamic>, json:String) {
|
||||
var savedMatches:haxe.DynamicAccess<Dynamic> = Json.parse(json);
|
||||
for (key => value in savedMatches.keyValueIterator()) {
|
||||
m.set(key, value);
|
||||
}
|
||||
}
|
||||
}
|
@@ -10,6 +10,8 @@ import hollywoo.Director;
|
||||
import haxe.Json;
|
||||
import uuid.Uuid;
|
||||
|
||||
using kiss.FuzzyMapTools;
|
||||
|
||||
enum DelayHandling {
|
||||
Auto;
|
||||
AutoWithSkip;
|
||||
|
@@ -11,7 +11,6 @@
|
||||
(prop :FuzzyMap<VoiceLine> voiceLines (new FuzzyMap<VoiceLine>))
|
||||
// Used to give unique, persistent IDs to voice tracks
|
||||
(prop :Map<String,Int> voiceTracksPerActor (new Map))
|
||||
(prop :Map<String,VoiceLine> matchedVoiceLines (new Map))
|
||||
|
||||
(prop &mut :DelayHandling delayHandling AutoWithSkip)
|
||||
|
||||
@@ -63,9 +62,13 @@
|
||||
[
|
||||
// "View" in the Model-View-Controller architecture:
|
||||
&prop :Director<Set,StagePosition,StageFacing,ScreenPosition,Actor,Sound,Song,Prop,VoiceTrack> director
|
||||
&opt :String voiceLinesJson
|
||||
]
|
||||
|
||||
(set director.movie this)
|
||||
(voiceLines.serializeMatches "matchedVoiceLines.json")
|
||||
(when voiceLinesJson
|
||||
(voiceLines.loadMatches voiceLinesJson))
|
||||
|
||||
(super)))
|
||||
|
||||
|
Reference in New Issue
Block a user