|
| 1 | +-- @noindex |
| 2 | +-- @author Ben 'Talagan' Babut |
| 3 | +-- @license MIT |
| 4 | +-- @description This file is part of Reannotate |
| 5 | + |
| 6 | +local Notes = require 'classes/notes' |
| 7 | + |
| 8 | +local MemCache = {} |
| 9 | +MemCache.__index = MemCache |
| 10 | + |
| 11 | +function MemCache:new() |
| 12 | + local instance = {} |
| 13 | + setmetatable(instance, self) |
| 14 | + instance:_initialize() |
| 15 | + return instance |
| 16 | +end |
| 17 | + |
| 18 | +function MemCache:_initialize() |
| 19 | + self._repo = {} |
| 20 | + |
| 21 | + MemCache.__singleton = self |
| 22 | +end |
| 23 | + |
| 24 | +function MemCache.GetObjectGUID(object) |
| 25 | + local guid = '' |
| 26 | + if reaper.ValidatePtr(object, "MediaTrack*") then |
| 27 | + local tguid = reaper.GetTrackGUID(object) |
| 28 | + guid = tguid |
| 29 | + elseif reaper.ValidatePtr(object,"MediaItem*") then |
| 30 | + local _, tguid = reaper.GetSetMediaItemInfo_String(object, "GUID", "", false) |
| 31 | + guid = tguid |
| 32 | + elseif reaper.ValidatePtr(object, "TrackEnvelope*") then |
| 33 | + local _, tguid = reaper.GetSetEnvelopeInfo_String(object, "GUID", "", false) |
| 34 | + guid = tguid |
| 35 | + elseif reaper.ValidatePtr(object, "ReaProject*") then |
| 36 | + local _, tguid = reaper.GetSetProjectInfo_String(object, "PROJECT_NAME", "", false) |
| 37 | + guid = tguid |
| 38 | + else |
| 39 | + error("Unhandled type for object") |
| 40 | + end |
| 41 | + return guid |
| 42 | +end |
| 43 | + |
| 44 | +function MemCache:getObjectCache(object) |
| 45 | + |
| 46 | + local guid = MemCache.GetObjectGUID(object) |
| 47 | + |
| 48 | + if not self._repo[guid] then |
| 49 | + -- Build object cache |
| 50 | + |
| 51 | + local name = '' |
| 52 | + local type = '' |
| 53 | + if reaper.ValidatePtr(object, "MediaTrack*") then |
| 54 | + local _, tname = reaper.GetTrackName(object) |
| 55 | + name = tname |
| 56 | + type = 'track' |
| 57 | + elseif reaper.ValidatePtr(object,"MediaItem*") then |
| 58 | + local take = reaper.GetActiveTake(object) |
| 59 | + if take then |
| 60 | + name = reaper.GetTakeName(take) |
| 61 | + end |
| 62 | + type = 'item' |
| 63 | + elseif reaper.ValidatePtr(object, "TrackEnvelope*") then |
| 64 | + local _, ename = reaper.GetEnvelopeName(object) |
| 65 | + name = ename |
| 66 | + type = 'env' |
| 67 | + elseif reaper.ValidatePtr(object, "ReaProject*") then |
| 68 | + name = "Project" |
| 69 | + type = 'project' |
| 70 | + else |
| 71 | + error("Unhandled type for object") |
| 72 | + end |
| 73 | + |
| 74 | + -- Cache miss, pull info from object |
| 75 | + self._repo[guid] = { |
| 76 | + guid = guid, |
| 77 | + object = object, |
| 78 | + name = name, |
| 79 | + type = type, |
| 80 | + -- Notes cache |
| 81 | + notes = Notes:new(object) |
| 82 | + } |
| 83 | + end |
| 84 | + |
| 85 | + return self._repo[guid] |
| 86 | +end |
| 87 | + |
| 88 | +function MemCache.instance() |
| 89 | + if not MemCache.__singleton then |
| 90 | + MemCache.__singleton = MemCache:new() |
| 91 | + end |
| 92 | + |
| 93 | + return MemCache.__singleton |
| 94 | +end |
| 95 | + |
| 96 | +return MemCache |
0 commit comments