// Update Database from Directory public Task LoadCustomSongs(string folder = "", string songid = "") { if (MapDatabase.DatabaseLoading) { return(Task.CompletedTask); } DatabaseLoading = true; return(Task.Run(() => { if (songid == "") { this._chatManager.QueueChatMessage($"Starting song indexing {folder}"); } var StarTime = DateTime.UtcNow; if (folder == "") { folder = Path.Combine(Environment.CurrentDirectory, "customsongs"); } var files = new List <FileInfo>(); // List that will hold the files and subfiles in path var folders = new List <DirectoryInfo>(); // List that hold direcotries that cannot be accessed var di = new DirectoryInfo(folder); FullDirList(di, "*"); if (RequestBotConfig.Instance.additionalsongpath != "") { di = new DirectoryInfo(RequestBotConfig.Instance.additionalsongpath); FullDirList(di, "*"); } void FullDirList(DirectoryInfo dir, string searchPattern) { try { foreach (var f in dir.GetFiles(searchPattern)) { if (f.FullName.EndsWith("info.json")) { files.Add(f); } } } catch { Console.WriteLine("Directory {0} \n could not be accessed!!!!", dir.FullName); return; } foreach (var d in dir.GetDirectories()) { folders.Add(d); FullDirList(d, searchPattern); } } // This might need some optimization this._chatManager.QueueChatMessage($"Processing {files.Count} maps. "); foreach (var item in files) { //msg.Add(item.FullName,", "); string id = "", version = "0"; this.GetIdFromPath(item.DirectoryName, ref id, ref version); try { if (MapDatabase.MapLibrary.ContainsKey(id)) { continue; } var song = JSONObject.Parse(File.ReadAllText(item.FullName)).AsObject; string hash; JSONNode difficultylevels = song["difficultyLevels"].AsArray; var FileAccumulator = new StringBuilder(); foreach (var level in difficultylevels) { //Instance.QueueChatMessage($"key={level.Key} value={level.Value}"); try { FileAccumulator.Append(File.ReadAllText($"{item.DirectoryName}\\{level.Value["jsonPath"].Value}")); } catch { //Instance.QueueChatMessage($"key={level.Key} value={level.Value}"); //throw; } } hash = this.Bot.CreateMD5FromString(FileAccumulator.ToString()); var levelId = string.Join("∎", hash, song["songName"].Value, song["songSubName"].Value, song["authorName"], song["beatsPerMinute"].AsFloat.ToString()) + "∎"; if (LevelId.ContainsKey(levelId)) { LevelId[levelId].Path = item.DirectoryName; continue; } song.Add("id", id); song.Add("version", version); song.Add("hashMd5", hash); this._songMapFactory.Create(song, levelId, item.DirectoryName); } catch (Exception) { this._chatManager.QueueChatMessage($"Failed to process {item}."); } } var duration = DateTime.UtcNow - StarTime; if (songid == "") { this._chatManager.QueueChatMessage($"Song indexing done. ({duration.TotalSeconds} secs."); } DatabaseImported = true; DatabaseLoading = false; })); }
// Early code... index a full zip archive. public async Task LoadZIPDirectory(string folder = "") { if (MapDatabase.DatabaseLoading) { return; } if (string.IsNullOrEmpty(folder)) { folder = Environment.CurrentDirectory; } await Task.Run(() => { var startingmem = GC.GetTotalMemory(true); this._chatManager.QueueChatMessage($"Starting to read archive."); var addcount = 0; var StarTime = DateTime.Now; var di = new DirectoryInfo(folder); foreach (var f in di.GetFiles("*.zip")) { try { var x = ZipFile.OpenRead(f.FullName); var info = x.Entries.First <ZipArchiveEntry>(e => (e.Name.EndsWith("info.json"))); var id = ""; var version = ""; this.GetIdFromPath(f.Name, ref id, ref version); if (MapDatabase.MapLibrary.ContainsKey(id)) { if (MapLibrary[id].Path != "") { MapLibrary[id].Path = f.FullName; } continue; } var song = JSONObject.Parse(this.Readzipjson(x)).AsObject; string hash; JSONNode difficultylevels = song["difficultyLevels"].AsArray; var FileAccumulator = new StringBuilder(); foreach (var level in difficultylevels) { try { FileAccumulator.Append(this.Readzipjson(x, level.Value)); } catch { //Instance.QueueChatMessage($"key={level.Key} value={level.Value}"); //throw; } } hash = this.Bot.CreateMD5FromString(FileAccumulator.ToString()); var levelId = string.Join("∎", hash, song["songName"].Value, song["songSubName"].Value, song["authorName"], song["beatsPerMinute"].AsFloat.ToString()) + "∎"; if (LevelId.ContainsKey(levelId)) { LevelId[levelId].Path = f.FullName; continue; } addcount++; song.Add("id", id); song.Add("version", version); song.Add("hashMd5", hash); this._songMapFactory.Create(song, levelId, f.FullName); x = null; } catch (Exception) { this._chatManager.QueueChatMessage($"Failed to process {f.FullName}"); //Instance.QueueChatMessage(ex.ToString()); } } this._chatManager.QueueChatMessage($"Archive indexing done, {addcount} files added. ({(DateTime.Now - StarTime).TotalSeconds} secs."); GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce; GC.Collect(); this._chatManager.QueueChatMessage($"hashentries: {SongMap.HashCount} memory: {(GC.GetTotalMemory(false) - startingmem) / 1048576} MB"); }); MapDatabase.DatabaseLoading = false; }