amuse/AudioUnit/AudioGroupFilePresenter.mm

867 lines
32 KiB
Plaintext
Raw Permalink Normal View History

#include "AudioGroupFilePresenter.hpp"
#include <athena/FileReader.hpp>
2016-06-04 02:34:35 +00:00
#include <amuse/AudioGroupProject.hpp>
2016-06-04 22:38:55 +00:00
#import "AmuseContainingApp.hpp"
2016-06-07 03:42:51 +00:00
#import "AudioUnitBackend.hpp"
#import "AudioUnitViewController.hpp"
2016-06-04 22:38:55 +00:00
static std::string StrToLower(const std::string& str)
{
std::string ret = str;
std::transform(ret.begin(), ret.end(), ret.begin(), tolower);
return ret;
}
2016-06-04 02:34:35 +00:00
2016-06-04 22:38:55 +00:00
@implementation AudioGroupDataToken
2016-06-04 02:34:35 +00:00
- (id)initWithDataCollection:(AudioGroupDataCollection *)collection
{
self = [super init];
m_collection = collection;
return self;
}
@end
@implementation AudioGroupCollectionToken
- (id)initWithCollection:(AudioGroupCollection *)collection
{
self = [super init];
m_collection = collection;
return self;
}
@end
2016-06-05 22:37:49 +00:00
@implementation AudioGroupSFXToken
- (id)initWithName:(NSAttributedString*)name loadId:(int)loadId sfx:(const amuse::SFXGroupIndex::SFXEntry*)sfx
{
self = [super init];
m_name = name;
m_loadId = loadId;
m_sfx = sfx;
return self;
}
@end
@implementation AudioGroupSampleToken
- (id)initWithName:(NSAttributedString*)name samp:(const std::pair<amuse::AudioGroupSampleDirectory::Entry,
amuse::AudioGroupSampleDirectory::ADPCMParms>*)sample
{
self = [super init];
m_name = name;
m_sample = sample;
return self;
}
@end
2016-06-07 03:42:51 +00:00
@implementation AudioGroupToken
2016-06-08 04:33:15 +00:00
- (id)initWithName:(NSString*)name id:(int)gid songGroup:(const amuse::SongGroupIndex*)group
2016-06-07 03:42:51 +00:00
{
self = [super init];
m_name = name;
m_song = group;
2016-06-08 04:33:15 +00:00
m_id = gid;
2016-06-07 03:42:51 +00:00
return self;
}
2016-06-08 04:33:15 +00:00
- (id)initWithName:(NSString*)name id:(int)gid sfxGroup:(const amuse::SFXGroupIndex*)group
2016-06-07 03:42:51 +00:00
{
self = [super init];
m_name = name;
m_sfx = group;
2016-06-08 04:33:15 +00:00
m_id = gid;
2016-06-07 03:42:51 +00:00
return self;
}
@end
@implementation AudioGroupFilePresenter
- (NSURL*)presentedItemURL
{
return m_groupURL;
}
- (NSOperationQueue*)presentedItemOperationQueue
{
2016-06-08 04:33:15 +00:00
return [NSOperationQueue mainQueue];
}
2016-06-04 02:34:35 +00:00
AudioGroupCollection::AudioGroupCollection(NSURL* url)
: m_url(url), m_token([[AudioGroupCollectionToken alloc] initWithCollection:this]) {}
2016-06-07 03:42:51 +00:00
void AudioGroupCollection::addCollection(AudioGroupFilePresenter* presenter,
std::vector<std::pair<std::string, amuse::IntrusiveAudioGroupData>>&& collection)
2016-06-04 02:34:35 +00:00
{
for (std::pair<std::string, amuse::IntrusiveAudioGroupData>& pair : collection)
{
NSURL* collectionUrl = [m_url URLByAppendingPathComponent:@(pair.first.c_str())];
amuse::IntrusiveAudioGroupData& dataIn = pair.second;
auto search = m_groups.find(pair.first);
if (search == m_groups.end())
{
search = m_groups.emplace(pair.first,
2016-06-04 22:38:55 +00:00
std::make_unique<AudioGroupDataCollection>(pair.first,
2016-06-04 02:34:35 +00:00
[collectionUrl URLByAppendingPathComponent:@"proj"],
[collectionUrl URLByAppendingPathComponent:@"pool"],
[collectionUrl URLByAppendingPathComponent:@"sdir"],
[collectionUrl URLByAppendingPathComponent:@"samp"],
2016-06-04 22:38:55 +00:00
[collectionUrl URLByAppendingPathComponent:@"meta"])).first;
2016-06-04 02:34:35 +00:00
}
2016-06-04 22:38:55 +00:00
AudioGroupDataCollection& dataCollection = *search->second;
2016-06-04 02:34:35 +00:00
dataCollection.m_projData.resize(dataIn.getProjSize());
memmove(dataCollection.m_projData.data(), dataIn.getProj(), dataIn.getProjSize());
dataCollection.m_poolData.resize(dataIn.getPoolSize());
memmove(dataCollection.m_poolData.data(), dataIn.getPool(), dataIn.getPoolSize());
dataCollection.m_sdirData.resize(dataIn.getSdirSize());
memmove(dataCollection.m_sdirData.data(), dataIn.getSdir(), dataIn.getSdirSize());
dataCollection.m_sampData.resize(dataIn.getSampSize());
memmove(dataCollection.m_sampData.data(), dataIn.getSamp(), dataIn.getSampSize());
dataCollection.m_metaData.emplace(dataIn.getDataFormat(), dataIn.getAbsoluteProjOffsets(), true);
2016-06-07 03:42:51 +00:00
dataCollection._indexData(presenter);
2016-06-04 02:34:35 +00:00
}
}
void AudioGroupCollection::update(AudioGroupFilePresenter* presenter)
{
NSFileManager* fman = [NSFileManager defaultManager];
NSArray<NSURL*>* contents =
[fman contentsOfDirectoryAtURL:m_url
includingPropertiesForKeys:@[NSURLIsDirectoryKey]
options:NSDirectoryEnumerationSkipsSubdirectoryDescendants |
NSDirectoryEnumerationSkipsHiddenFiles
error:nil];
if (!contents)
return;
for (NSURL* path in contents)
{
NSNumber* isDir;
[path getResourceValue:&isDir forKey:NSURLIsDirectoryKey error:nil];
if (isDir.boolValue)
{
auto search = m_groups.find(path.lastPathComponent.UTF8String);
if (search == m_groups.end())
{
std::string nameStr = path.lastPathComponent.UTF8String;
search =
m_groups.emplace(nameStr,
2016-06-04 22:38:55 +00:00
std::make_unique<AudioGroupDataCollection>(nameStr,
2016-06-04 02:34:35 +00:00
[path URLByAppendingPathComponent:@"proj"],
[path URLByAppendingPathComponent:@"pool"],
[path URLByAppendingPathComponent:@"sdir"],
[path URLByAppendingPathComponent:@"samp"],
2016-06-04 22:38:55 +00:00
[path URLByAppendingPathComponent:@"meta"])).first;
search->second->_attemptLoad(presenter);
2016-06-04 02:34:35 +00:00
}
}
}
}
2017-11-13 06:15:33 +00:00
bool AudioGroupCollection::doSearch(std::string_view str)
2016-06-04 22:38:55 +00:00
{
bool ret = false;
m_filterGroups.clear();
m_filterGroups.reserve(m_groups.size());
for (auto it = m_groups.begin() ; it != m_groups.end() ; ++it)
2020-04-11 22:49:30 +00:00
// TODO: Heterogeneous lookup when C++20 available
2017-11-13 06:15:33 +00:00
if (str.empty() || StrToLower(it->first).find(str.data()) != std::string::npos)
2016-06-04 22:38:55 +00:00
{
m_filterGroups.push_back(it);
ret = true;
}
return ret;
}
2016-06-07 03:42:51 +00:00
bool AudioGroupCollection::doActiveFilter()
{
bool ret = false;
m_filterGroups.clear();
m_filterGroups.reserve(m_groups.size());
for (auto it = m_groups.begin() ; it != m_groups.end() ; ++it)
if (it->second->m_metaData->active)
{
m_filterGroups.push_back(it);
ret = true;
}
return ret;
}
2016-06-05 22:37:49 +00:00
void AudioGroupCollection::addSFX(std::vector<AudioGroupSFXToken*>& vecOut)
{
for (auto it = m_groups.begin() ; it != m_groups.end() ; ++it)
{
if (!it->second->m_metaData->active)
continue;
const auto& sfxGroups = it->second->m_loadedGroup->getProj().sfxGroups();
std::map<int, const amuse::SFXGroupIndex*> sortGroups;
for (const auto& pair : sfxGroups)
sortGroups[pair.first] = &pair.second;
for (const auto& pair : sortGroups)
{
NSMutableAttributedString* name = [[NSMutableAttributedString alloc] initWithString:m_url.lastPathComponent attributes:@{NSForegroundColorAttributeName: [NSColor grayColor]}];
[name appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@" %s (%d)", it->first.c_str(), pair.first]]];
vecOut.push_back([[AudioGroupSFXToken alloc] initWithName:name loadId:0 sfx:nil]);
std::map<int, const amuse::SFXGroupIndex::SFXEntry*> sortSfx;
for (const auto& pair : pair.second->m_sfxEntries)
sortSfx[pair.first] = pair.second;
for (const auto& sfx : sortSfx)
{
name = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%d", sfx.first]];
vecOut.push_back([[AudioGroupSFXToken alloc] initWithName:name loadId:sfx.first sfx:sfx.second]);
}
}
}
}
void AudioGroupCollection::addSamples(std::vector<AudioGroupSampleToken*>& vecOut)
{
for (auto it = m_groups.begin() ; it != m_groups.end() ; ++it)
{
if (!it->second->m_metaData->active)
continue;
const auto& samps = it->second->m_loadedGroup->getSdir().sampleEntries();
std::map<int, const std::pair<amuse::AudioGroupSampleDirectory::Entry, amuse::AudioGroupSampleDirectory::ADPCMParms>*> sortSamps;
for (const auto& pair : samps)
sortSamps[pair.first] = &pair.second;
NSMutableAttributedString* name = [[NSMutableAttributedString alloc] initWithString:m_url.lastPathComponent attributes:@{NSForegroundColorAttributeName: [NSColor grayColor]}];
[name appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@" %s", it->first.c_str()]]];
vecOut.push_back([[AudioGroupSampleToken alloc] initWithName:name samp:nil]);
for (const auto& pair : sortSamps)
{
name = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%d", pair.first]];
vecOut.push_back([[AudioGroupSampleToken alloc] initWithName:name samp:pair.second]);
}
}
}
2017-12-16 00:18:57 +00:00
AudioGroupDataCollection::AudioGroupDataCollection(std::string_view name, NSURL* proj, NSURL* pool,
2016-06-04 02:34:35 +00:00
NSURL* sdir, NSURL* samp, NSURL* meta)
: m_name(name), m_proj(proj), m_pool(pool), m_sdir(sdir), m_samp(samp), m_meta(meta),
m_token([[AudioGroupDataToken alloc] initWithDataCollection:this]) {}
bool AudioGroupDataCollection::_attemptLoad(AudioGroupFilePresenter* presenter)
{
2016-06-05 22:37:49 +00:00
if (m_metaData && m_loadedData && m_loadedGroup)
2016-06-04 02:34:35 +00:00
return true;
if (!loadProj(presenter))
return false;
if (!loadPool(presenter))
return false;
if (!loadSdir(presenter))
return false;
if (!loadSamp(presenter))
return false;
if (!loadMeta(presenter))
return false;
2016-06-07 03:42:51 +00:00
return _indexData(presenter);
}
bool AudioGroupDataCollection::_indexData(AudioGroupFilePresenter* presenter)
{
amuse::Engine& engine = [presenter->m_audioGroupClient getAmuseEngine];
2016-06-04 02:34:35 +00:00
switch (m_metaData->fmt)
{
case amuse::DataFormat::GCN:
default:
m_loadedData.emplace(m_projData.data(), m_projData.size(),
m_poolData.data(), m_poolData.size(),
m_sdirData.data(), m_sdirData.size(),
m_sampData.data(), m_sampData.size(),
amuse::GCNDataTag{});
break;
case amuse::DataFormat::N64:
m_loadedData.emplace(m_projData.data(), m_projData.size(),
m_poolData.data(), m_poolData.size(),
m_sdirData.data(), m_sdirData.size(),
m_sampData.data(), m_sampData.size(),
m_metaData->absOffs, amuse::N64DataTag{});
break;
case amuse::DataFormat::PC:
m_loadedData.emplace(m_projData.data(), m_projData.size(),
m_poolData.data(), m_poolData.size(),
m_sdirData.data(), m_sdirData.size(),
m_sampData.data(), m_sampData.size(),
m_metaData->absOffs, amuse::PCDataTag{});
break;
}
2016-06-05 22:37:49 +00:00
m_loadedGroup = engine.addAudioGroup(*m_loadedData);
2016-06-07 03:42:51 +00:00
m_groupTokens.clear();
if (m_loadedGroup)
{
m_groupTokens.reserve(m_loadedGroup->getProj().songGroups().size() +
m_loadedGroup->getProj().sfxGroups().size());
{
const auto& songGroups = m_loadedGroup->getProj().songGroups();
std::map<int, const amuse::SongGroupIndex*> sortGroups;
for (const auto& pair : songGroups)
sortGroups[pair.first] = &pair.second;
for (const auto& pair : sortGroups)
{
NSString* name = [NSString stringWithFormat:@"%d", pair.first];
2016-06-08 04:33:15 +00:00
m_groupTokens.push_back([[AudioGroupToken alloc] initWithName:name id:pair.first
songGroup:pair.second]);
2016-06-07 03:42:51 +00:00
}
}
{
const auto& sfxGroups = m_loadedGroup->getProj().sfxGroups();
std::map<int, const amuse::SFXGroupIndex*> sortGroups;
for (const auto& pair : sfxGroups)
sortGroups[pair.first] = &pair.second;
for (const auto& pair : sortGroups)
{
NSString* name = [NSString stringWithFormat:@"%d", pair.first];
2016-06-08 04:33:15 +00:00
m_groupTokens.push_back([[AudioGroupToken alloc] initWithName:name id:pair.first
sfxGroup:pair.second]);
2016-06-07 03:42:51 +00:00
}
}
}
2016-06-05 22:37:49 +00:00
return m_loadedData && m_loadedGroup;
2016-06-04 02:34:35 +00:00
}
2016-06-04 22:38:55 +00:00
void AudioGroupDataCollection::enable(AudioGroupFilePresenter* presenter)
{
m_metaData->active = true;
NSFileCoordinator* coord = [[NSFileCoordinator alloc] initWithFilePresenter:presenter];
[coord coordinateWritingItemAtURL:m_meta options:0 error:nil
byAccessor:^(NSURL* newUrl)
{
FILE* fp = fopen(newUrl.path.UTF8String, "wb");
if (fp)
{
fwrite(&*m_metaData, 1, sizeof(*m_metaData), fp);
fclose(fp);
}
}];
}
void AudioGroupDataCollection::disable(AudioGroupFilePresenter* presenter)
{
m_metaData->active = false;
NSFileCoordinator* coord = [[NSFileCoordinator alloc] initWithFilePresenter:presenter];
[coord coordinateWritingItemAtURL:m_meta options:0 error:nil
byAccessor:^(NSURL* newUrl)
{
FILE* fp = fopen(newUrl.path.UTF8String, "wb");
if (fp)
{
fwrite(&*m_metaData, 1, sizeof(*m_metaData), fp);
fclose(fp);
}
}];
}
void AudioGroupDataCollection::moveURL(NSURL* oldUrl, NSURL* newUrl)
{
if (m_proj)
{
if ([m_proj isEqual:oldUrl])
m_proj = newUrl;
}
if (m_pool)
{
if ([m_pool isEqual:oldUrl])
m_pool = newUrl;
}
if (m_sdir)
{
if ([m_sdir isEqual:oldUrl])
m_sdir = newUrl;
}
if (m_samp)
{
if ([m_samp isEqual:oldUrl])
m_samp = newUrl;
}
}
2016-06-03 02:53:52 +00:00
bool AudioGroupDataCollection::loadProj(AudioGroupFilePresenter* presenter)
{
2016-06-03 02:53:52 +00:00
if (!m_proj)
return false;
NSFileCoordinator* coord = [[NSFileCoordinator alloc] initWithFilePresenter:presenter];
if (!coord)
2016-06-03 02:53:52 +00:00
return false;
NSError* err;
2016-06-04 02:34:35 +00:00
__block std::vector<uint8_t>& ret = m_projData;
2016-06-03 02:53:52 +00:00
[coord coordinateReadingItemAtURL:m_proj
options:NSFileCoordinatorReadingResolvesSymbolicLink error:&err
byAccessor:^(NSURL* newUrl)
{
athena::io::FileReader r([[newUrl path] UTF8String], 1024 * 32, false);
if (r.hasError())
return;
2016-06-04 02:34:35 +00:00
size_t len = r.length();
ret.resize(len);
r.readUBytesToBuf(ret.data(), len);
2016-06-03 02:53:52 +00:00
}];
2016-06-04 02:34:35 +00:00
return ret.size();
}
2016-06-03 02:53:52 +00:00
bool AudioGroupDataCollection::loadPool(AudioGroupFilePresenter* presenter)
{
if (!m_pool)
2016-06-03 02:53:52 +00:00
return false;
NSFileCoordinator* coord = [[NSFileCoordinator alloc] initWithFilePresenter:presenter];
if (!coord)
return false;
NSError* err;
2016-06-04 02:34:35 +00:00
__block std::vector<uint8_t>& ret = m_poolData;
2016-06-03 02:53:52 +00:00
[coord coordinateReadingItemAtURL:m_pool
options:NSFileCoordinatorReadingResolvesSymbolicLink error:&err
byAccessor:^(NSURL* newUrl)
{
athena::io::FileReader r([[newUrl path] UTF8String], 1024 * 32, false);
if (r.hasError())
return;
2016-06-04 02:34:35 +00:00
size_t len = r.length();
ret.resize(len);
r.readUBytesToBuf(ret.data(), len);
2016-06-03 02:53:52 +00:00
}];
2016-06-04 02:34:35 +00:00
return ret.size();
}
2016-06-03 02:53:52 +00:00
bool AudioGroupDataCollection::loadSdir(AudioGroupFilePresenter* presenter)
{
if (!m_sdir)
2016-06-03 02:53:52 +00:00
return false;
NSFileCoordinator* coord = [[NSFileCoordinator alloc] initWithFilePresenter:presenter];
if (!coord)
return false;
NSError* err;
2016-06-04 02:34:35 +00:00
__block std::vector<uint8_t>& ret = m_sdirData;
2016-06-03 02:53:52 +00:00
[coord coordinateReadingItemAtURL:m_sdir
options:NSFileCoordinatorReadingResolvesSymbolicLink error:&err
byAccessor:^(NSURL* newUrl)
{
athena::io::FileReader r([[newUrl path] UTF8String], 1024 * 32, false);
if (r.hasError())
return;
2016-06-04 02:34:35 +00:00
size_t len = r.length();
ret.resize(len);
r.readUBytesToBuf(ret.data(), len);
2016-06-03 02:53:52 +00:00
}];
2016-06-04 02:34:35 +00:00
return ret.size();
}
2016-06-03 02:53:52 +00:00
bool AudioGroupDataCollection::loadSamp(AudioGroupFilePresenter* presenter)
{
if (!m_samp)
2016-06-03 02:53:52 +00:00
return false;
NSFileCoordinator* coord = [[NSFileCoordinator alloc] initWithFilePresenter:presenter];
if (!coord)
return false;
NSError* err;
2016-06-04 02:34:35 +00:00
__block std::vector<uint8_t>& ret = m_sampData;
2016-06-03 02:53:52 +00:00
[coord coordinateReadingItemAtURL:m_samp
options:NSFileCoordinatorReadingResolvesSymbolicLink error:&err
byAccessor:^(NSURL* newUrl)
{
athena::io::FileReader r([[newUrl path] UTF8String], 1024 * 32, false);
if (r.hasError())
return;
2016-06-04 02:34:35 +00:00
size_t len = r.length();
ret.resize(len);
r.readUBytesToBuf(ret.data(), len);
2016-06-03 02:53:52 +00:00
}];
2016-06-04 02:34:35 +00:00
return ret.size();
}
bool AudioGroupDataCollection::loadMeta(AudioGroupFilePresenter* presenter)
{
if (!m_meta)
return false;
NSFileCoordinator* coord = [[NSFileCoordinator alloc] initWithFilePresenter:presenter];
if (!coord)
return false;
NSError* err;
2019-06-12 02:02:52 +00:00
__block std::optional<MetaData>& ret = m_metaData;
2016-06-04 02:34:35 +00:00
[coord coordinateReadingItemAtURL:m_meta
options:NSFileCoordinatorReadingResolvesSymbolicLink error:&err
byAccessor:^(NSURL* newUrl)
{
athena::io::FileReader r([[newUrl path] UTF8String], 1024 * 32, false);
if (r.hasError())
return;
ret.emplace(r);
}];
return ret.operator bool();
2016-06-03 02:53:52 +00:00
}
2016-06-08 04:33:15 +00:00
- (void)presentedSubitemDidChangeAtURL:(NSURL *)url
2016-06-03 02:53:52 +00:00
{
2016-06-08 04:33:15 +00:00
size_t relComps = url.pathComponents.count - m_groupURL.pathComponents.count;
if (relComps <= 1)
2016-06-04 02:34:35 +00:00
[self update];
}
2016-06-03 02:53:52 +00:00
- (void)presentedSubitemAtURL:(NSURL*)oldUrl didMoveToURL:(NSURL*)newUrl
{
2016-06-04 02:34:35 +00:00
for (auto& pair : m_audioGroupCollections)
{
2016-06-04 22:38:55 +00:00
for (auto& pair2 : pair.second->m_groups)
2016-06-04 02:34:35 +00:00
{
2016-06-04 22:38:55 +00:00
pair2.second->moveURL(oldUrl, newUrl);
2016-06-04 02:34:35 +00:00
}
}
}
2016-06-03 02:53:52 +00:00
- (NSInteger)outlineView:(NSOutlineView*)outlineView numberOfChildrenOfItem:(nullable id)item
{
2016-06-07 03:42:51 +00:00
m_lastOutlineView = outlineView;
2016-06-03 02:53:52 +00:00
if (!item)
2016-06-04 22:38:55 +00:00
return m_filterAudioGroupCollections.size();
2016-06-04 02:34:35 +00:00
AudioGroupCollection& collection = *((AudioGroupCollectionToken*)item)->m_collection;
2016-06-04 22:38:55 +00:00
return collection.m_filterGroups.size();
2016-06-03 02:53:52 +00:00
}
2016-06-03 02:53:52 +00:00
- (id)outlineView:(NSOutlineView*)outlineView child:(NSInteger)index ofItem:(nullable id)item
{
if (!item)
{
2016-06-04 22:38:55 +00:00
if (index >= m_filterAudioGroupCollections.size())
2016-06-04 02:34:35 +00:00
return nil;
2016-06-04 22:38:55 +00:00
return m_filterAudioGroupCollections[index]->second->m_token;
}
2016-06-04 02:34:35 +00:00
AudioGroupCollection& collection = *((AudioGroupCollectionToken*)item)->m_collection;
2016-06-04 22:38:55 +00:00
if (index >= collection.m_filterGroups.size())
2016-06-04 02:34:35 +00:00
return nil;
2016-06-04 22:38:55 +00:00
return collection.m_filterGroups[index]->second->m_token;
}
2016-06-03 02:53:52 +00:00
- (BOOL)outlineView:(NSOutlineView*)outlineView isItemExpandable:(id)item
{
2016-06-04 02:34:35 +00:00
if ([item isKindOfClass:[AudioGroupCollectionToken class]])
return YES;
2016-06-03 02:53:52 +00:00
return NO;
}
2016-06-04 02:34:35 +00:00
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
if ([item isKindOfClass:[AudioGroupCollectionToken class]])
{
AudioGroupCollection& collection = *((AudioGroupCollectionToken*)item)->m_collection;
if ([tableColumn.identifier isEqualToString:@"CollectionColumn"])
2016-06-04 22:38:55 +00:00
{
size_t totalOn = 0;
for (auto& pair : collection.m_groups)
if (pair.second->m_metaData->active)
++totalOn;
if (totalOn == 0)
return [NSNumber numberWithInt:NSOffState];
else if (totalOn == collection.m_groups.size())
return [NSNumber numberWithInt:NSOnState];
else
return [NSNumber numberWithInt:NSMixedState];
}
2016-06-04 02:34:35 +00:00
else if ([tableColumn.identifier isEqualToString:@"DetailsColumn"])
2016-06-07 03:42:51 +00:00
return [NSString stringWithFormat:@"%zu Group File%s",
collection.m_groups.size(),
collection.m_groups.size() > 1 ? "s" : ""];
2016-06-04 02:34:35 +00:00
}
else if ([item isKindOfClass:[AudioGroupDataToken class]])
{
AudioGroupDataCollection& data = *((AudioGroupDataToken*)item)->m_collection;
if ([tableColumn.identifier isEqualToString:@"CollectionColumn"])
2016-06-04 22:38:55 +00:00
return [NSNumber numberWithInt:data.m_metaData->active ? NSOnState : NSOffState];
2016-06-04 02:34:35 +00:00
else if ([tableColumn.identifier isEqualToString:@"DetailsColumn"])
{
2016-06-05 22:37:49 +00:00
if (!data.m_loadedGroup)
2016-06-04 02:34:35 +00:00
return @"";
2016-06-05 22:37:49 +00:00
if (data.m_loadedGroup->getProj().songGroups().size() && data.m_loadedGroup->getProj().sfxGroups().size())
return [NSString stringWithFormat:@"%zu Song Group%s, %zu SFX Group%s",
2016-06-05 22:37:49 +00:00
data.m_loadedGroup->getProj().songGroups().size(),
data.m_loadedGroup->getProj().songGroups().size() > 1 ? "s" : "",
data.m_loadedGroup->getProj().sfxGroups().size(),
data.m_loadedGroup->getProj().sfxGroups().size() > 1 ? "s" : ""];
else if (data.m_loadedGroup->getProj().songGroups().size())
return [NSString stringWithFormat:@"%zu Song Group%s",
2016-06-05 22:37:49 +00:00
data.m_loadedGroup->getProj().songGroups().size(),
data.m_loadedGroup->getProj().songGroups().size() > 1 ? "s" : ""];
else if (data.m_loadedGroup->getProj().sfxGroups().size())
return [NSString stringWithFormat:@"%zu SFX Group%s",
2016-06-05 22:37:49 +00:00
data.m_loadedGroup->getProj().sfxGroups().size(),
data.m_loadedGroup->getProj().sfxGroups().size() > 1 ? "s" : ""];
else
return @"";
2016-06-04 02:34:35 +00:00
}
}
return nil;
}
2016-06-04 22:38:55 +00:00
- (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(nullable id)object forTableColumn:(nullable NSTableColumn *)tableColumn byItem:(nullable id)item
{
bool dirty = false;
if ([item isKindOfClass:[AudioGroupCollectionToken class]])
{
AudioGroupCollection& collection = *((AudioGroupCollectionToken*)item)->m_collection;
if ([tableColumn.identifier isEqualToString:@"CollectionColumn"])
{
NSInteger active = [object integerValue];
if (active)
for (auto& pair : collection.m_groups)
pair.second->enable(self);
else
for (auto& pair : collection.m_groups)
pair.second->disable(self);
dirty = true;
}
}
else if ([item isKindOfClass:[AudioGroupDataToken class]])
{
AudioGroupDataCollection& data = *((AudioGroupDataToken*)item)->m_collection;
if ([tableColumn.identifier isEqualToString:@"CollectionColumn"])
{
NSInteger active = [object integerValue];
if (active)
data.enable(self);
else
data.disable(self);
dirty = true;
}
}
if (dirty)
2016-06-05 22:37:49 +00:00
[self resetIterators];
2016-06-04 22:38:55 +00:00
}
2016-06-04 02:34:35 +00:00
- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(nonnull id)cell forTableColumn:(nullable NSTableColumn *)tableColumn item:(nonnull id)item
{
if ([item isKindOfClass:[AudioGroupCollectionToken class]])
{
AudioGroupCollection& collection = *((AudioGroupCollectionToken*)item)->m_collection;
if ([tableColumn.identifier isEqualToString:@"CollectionColumn"])
((NSButtonCell*)cell).title = collection.m_url.lastPathComponent;
}
else if ([item isKindOfClass:[AudioGroupDataToken class]])
{
AudioGroupDataCollection& data = *((AudioGroupDataToken*)item)->m_collection;
if ([tableColumn.identifier isEqualToString:@"CollectionColumn"])
((NSButtonCell*)cell).title = @(data.m_name.c_str());
}
}
2016-06-04 22:38:55 +00:00
- (void)outlineViewSelectionDidChange:(NSNotification *)notification
{
DataOutlineView* ov = notification.object;
id item = [ov itemAtRow:ov.selectedRow];
[(AppDelegate*)NSApp.delegate outlineView:ov selectionChanged:item];
}
- (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id<NSDraggingInfo>)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
{
[outlineView setDropItem:nil dropChildIndex:NSOutlineViewDropOnItemIndex];
NSPasteboard* pboard = [info draggingPasteboard];
if ([[pboard types] containsObject:NSURLPboardType])
return NSDragOperationCopy;
return NSDragOperationNone;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id<NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index
{
NSPasteboard* pboard = [info draggingPasteboard];
if ([[pboard types] containsObject:NSURLPboardType])
{
NSURL* url = [NSURL URLFromPasteboard:pboard];
[(AppDelegate*)NSApp.delegate importURL:url];
return YES;
}
return NO;
}
2016-06-04 02:34:35 +00:00
- (BOOL)addCollectionName:(std::string&&)name items:(std::vector<std::pair<std::string, amuse::IntrusiveAudioGroupData>>&&)collection
{
NSFileCoordinator* coord = [[NSFileCoordinator alloc] initWithFilePresenter:self];
if (!coord)
return false;
NSURL* dir = [m_groupURL URLByAppendingPathComponent:@(name.c_str())];
2016-06-04 22:38:55 +00:00
__block AudioGroupCollection& insert = *m_audioGroupCollections.emplace(name, std::make_unique<AudioGroupCollection>(dir)).first->second;
2016-06-07 03:42:51 +00:00
insert.addCollection(self, std::move(collection));
2016-06-04 02:34:35 +00:00
[coord coordinateWritingItemAtURL:m_groupURL options:0 error:nil
byAccessor:^(NSURL* newUrl)
{
2016-06-04 22:38:55 +00:00
for (std::pair<const std::string, std::unique_ptr<AudioGroupDataCollection>>& pair : insert.m_groups)
2016-06-04 02:34:35 +00:00
{
NSURL* collectionUrl = [insert.m_url URLByAppendingPathComponent:@(pair.first.c_str())];
[[NSFileManager defaultManager] createDirectoryAtURL:collectionUrl withIntermediateDirectories:YES attributes:nil error:nil];
2016-06-04 22:38:55 +00:00
FILE* fp = fopen(pair.second->m_proj.path.UTF8String, "wb");
2016-06-04 02:34:35 +00:00
if (fp)
{
2016-06-04 22:38:55 +00:00
fwrite(pair.second->m_projData.data(), 1, pair.second->m_projData.size(), fp);
2016-06-04 02:34:35 +00:00
fclose(fp);
}
2016-06-04 22:38:55 +00:00
fp = fopen(pair.second->m_pool.path.UTF8String, "wb");
2016-06-04 02:34:35 +00:00
if (fp)
{
2016-06-04 22:38:55 +00:00
fwrite(pair.second->m_poolData.data(), 1, pair.second->m_poolData.size(), fp);
2016-06-04 02:34:35 +00:00
fclose(fp);
}
2016-06-04 22:38:55 +00:00
fp = fopen(pair.second->m_sdir.path.UTF8String, "wb");
2016-06-04 02:34:35 +00:00
if (fp)
{
2016-06-04 22:38:55 +00:00
fwrite(pair.second->m_sdirData.data(), 1, pair.second->m_sdirData.size(), fp);
2016-06-04 02:34:35 +00:00
fclose(fp);
}
2016-06-04 22:38:55 +00:00
fp = fopen(pair.second->m_samp.path.UTF8String, "wb");
2016-06-04 02:34:35 +00:00
if (fp)
{
2016-06-04 22:38:55 +00:00
fwrite(pair.second->m_sampData.data(), 1, pair.second->m_sampData.size(), fp);
2016-06-04 02:34:35 +00:00
fclose(fp);
}
2016-06-04 22:38:55 +00:00
fp = fopen(pair.second->m_meta.path.UTF8String, "wb");
2016-06-04 02:34:35 +00:00
if (fp)
{
2016-06-04 22:38:55 +00:00
fwrite(&*pair.second->m_metaData, 1, sizeof(*pair.second->m_metaData), fp);
2016-06-04 02:34:35 +00:00
fclose(fp);
}
}
}];
[self resetIterators];
2016-06-04 02:34:35 +00:00
return true;
}
2016-06-03 02:53:52 +00:00
- (void)update
{
NSFileCoordinator* coord = [[NSFileCoordinator alloc] initWithFilePresenter:self];
if (!coord)
return;
NSError* coordErr;
__block NSError* managerErr;
2016-06-04 22:38:55 +00:00
__block std::map<std::string, std::unique_ptr<AudioGroupCollection>>& theMap = m_audioGroupCollections;
2016-06-04 02:34:35 +00:00
__block AudioGroupFilePresenter* presenter = self;
2016-06-03 02:53:52 +00:00
[coord coordinateReadingItemAtURL:m_groupURL options:NSFileCoordinatorReadingResolvesSymbolicLink error:&coordErr
byAccessor:^(NSURL* newUrl)
2016-06-04 02:34:35 +00:00
{
NSFileManager* fman = [NSFileManager defaultManager];
NSArray<NSURL*>* contents =
[fman contentsOfDirectoryAtURL:newUrl
includingPropertiesForKeys:@[NSURLIsDirectoryKey]
options:NSDirectoryEnumerationSkipsSubdirectoryDescendants |
NSDirectoryEnumerationSkipsHiddenFiles
error:&managerErr];
if (!contents)
return;
2016-06-03 02:53:52 +00:00
2016-06-04 02:34:35 +00:00
for (NSURL* path in contents)
{
NSNumber* isDir;
[path getResourceValue:&isDir forKey:NSURLIsDirectoryKey error:nil];
if (isDir.boolValue)
{
auto search = theMap.find(path.lastPathComponent.UTF8String);
if (search == theMap.end())
{
2016-06-04 22:38:55 +00:00
search = theMap.emplace(path.lastPathComponent.UTF8String, std::make_unique<AudioGroupCollection>(path)).first;
search->second->update(presenter);
2016-06-04 02:34:35 +00:00
}
}
}
}];
[self resetIterators];
}
- (void)resetIterators
{
2016-06-07 03:42:51 +00:00
if ([(NSObject*)m_audioGroupClient isKindOfClass:[AppDelegate class]])
{
std::string search;
if (m_searchStr)
search = m_searchStr.UTF8String;
m_sfxTableData.clear();
m_sampleTableData.clear();
m_filterAudioGroupCollections.clear();
m_filterAudioGroupCollections.reserve(m_audioGroupCollections.size());
for (auto it = m_audioGroupCollections.begin() ; it != m_audioGroupCollections.end() ; ++it)
{
it->second->addSFX(m_sfxTableData);
it->second->addSamples(m_sampleTableData);
if (it->second->doSearch(search) || !m_searchStr || StrToLower(it->first).find(search) != std::string::npos)
m_filterAudioGroupCollections.push_back(it);
}
[m_lastOutlineView reloadItem:nil reloadChildren:YES];
[(AppDelegate*)m_audioGroupClient reloadTables];
}
else
2016-06-05 22:37:49 +00:00
{
2016-06-07 03:42:51 +00:00
m_sfxTableData.clear();
m_sampleTableData.clear();
m_filterAudioGroupCollections.clear();
m_filterAudioGroupCollections.reserve(m_audioGroupCollections.size());
for (auto it = m_audioGroupCollections.begin() ; it != m_audioGroupCollections.end() ; ++it)
{
it->second->addSFX(m_sfxTableData);
it->second->addSamples(m_sampleTableData);
if (it->second->doActiveFilter())
m_filterAudioGroupCollections.push_back(it);
}
[((AmuseAudioUnit*)m_audioGroupClient)->m_viewController->m_groupBrowser loadColumnZero];
2016-06-05 22:37:49 +00:00
}
}
2016-06-04 22:38:55 +00:00
- (void)setSearchFilter:(NSString*)str
{
2016-06-07 03:42:51 +00:00
m_searchStr = [str lowercaseString];
2016-06-04 22:38:55 +00:00
[self resetIterators];
}
- (void)removeSelectedItem
{
2016-06-07 03:42:51 +00:00
id item = [m_lastOutlineView itemAtRow:m_lastOutlineView.selectedRow];
if ([item isKindOfClass:[AudioGroupCollectionToken class]])
{
AudioGroupCollection& collection = *((AudioGroupCollectionToken*)item)->m_collection;
NSURL* collectionURL = collection.m_url;
NSString* lastComp = collectionURL.lastPathComponent;
m_audioGroupCollections.erase(lastComp.UTF8String);
[self resetIterators];
[[NSFileManager defaultManager] removeItemAtURL:collectionURL error:nil];
if (m_audioGroupCollections.empty())
2016-06-07 03:42:51 +00:00
[(AppDelegate*)NSApp.delegate outlineView:(DataOutlineView*)m_lastOutlineView selectionChanged:nil];
}
2016-06-04 22:38:55 +00:00
}
2016-06-07 03:42:51 +00:00
- (id)initWithAudioGroupClient:(id<AudioGroupClient>)client
{
2016-06-07 03:42:51 +00:00
m_audioGroupClient = client;
m_groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.axiodl.Amuse.AudioGroups"];
if (!m_groupURL)
return nil;
[NSFileCoordinator addFilePresenter:self];
2016-06-03 02:53:52 +00:00
[self update];
return self;
}
- (void)dealloc
{
[NSFileCoordinator removeFilePresenter:self];
}
@end