built-in FuzzyMap match serializing

This commit is contained in:
2021-12-30 11:27:45 -07:00
parent 89c0c30e98
commit eb4ad546a5
4 changed files with 51 additions and 2 deletions

View File

@@ -51,7 +51,12 @@ abstract FuzzyMap<T>(StringMap<T>) from StringMap<T> to StringMap<T> {
@:arrayAccess @:arrayAccess
public inline function get(fuzzySearchKey:String):Null<T> { 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 { public inline function remove(fuzzySearchKey:String):Bool {

View 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);
}
}
}

View File

@@ -10,6 +10,8 @@ import hollywoo.Director;
import haxe.Json; import haxe.Json;
import uuid.Uuid; import uuid.Uuid;
using kiss.FuzzyMapTools;
enum DelayHandling { enum DelayHandling {
Auto; Auto;
AutoWithSkip; AutoWithSkip;

View File

@@ -11,7 +11,6 @@
(prop :FuzzyMap<VoiceLine> voiceLines (new FuzzyMap<VoiceLine>)) (prop :FuzzyMap<VoiceLine> voiceLines (new FuzzyMap<VoiceLine>))
// Used to give unique, persistent IDs to voice tracks // Used to give unique, persistent IDs to voice tracks
(prop :Map<String,Int> voiceTracksPerActor (new Map)) (prop :Map<String,Int> voiceTracksPerActor (new Map))
(prop :Map<String,VoiceLine> matchedVoiceLines (new Map))
(prop &mut :DelayHandling delayHandling AutoWithSkip) (prop &mut :DelayHandling delayHandling AutoWithSkip)
@@ -63,9 +62,13 @@
[ [
// "View" in the Model-View-Controller architecture: // "View" in the Model-View-Controller architecture:
&prop :Director<Set,StagePosition,StageFacing,ScreenPosition,Actor,Sound,Song,Prop,VoiceTrack> director &prop :Director<Set,StagePosition,StageFacing,ScreenPosition,Actor,Sound,Song,Prop,VoiceTrack> director
&opt :String voiceLinesJson
] ]
(set director.movie this) (set director.movie this)
(voiceLines.serializeMatches "matchedVoiceLines.json")
(when voiceLinesJson
(voiceLines.loadMatches voiceLinesJson))
(super))) (super)))