diff --git a/NadekoBot/Classes/DBHandler.cs b/NadekoBot/Classes/DBHandler.cs index eb67c6a6..36e5ccca 100644 --- a/NadekoBot/Classes/DBHandler.cs +++ b/NadekoBot/Classes/DBHandler.cs @@ -1,4 +1,4 @@ -using NadekoBot.Classes._DataModels; +using NadekoBot._DataModels; using SQLite; using System; using System.Collections.Generic; diff --git a/NadekoBot/Classes/Music/StreamRequest.cs b/NadekoBot/Classes/Music/StreamRequest.cs deleted file mode 100644 index 869a7b7a..00000000 --- a/NadekoBot/Classes/Music/StreamRequest.cs +++ /dev/null @@ -1,319 +0,0 @@ -/* -using System; -using System.Linq; -using System.Threading.Tasks; -using Discord; -using Discord.Commands; -using Discord.Audio; -using System.IO; -using System.Diagnostics; -using NadekoBot.Extensions; -using VideoLibrary; - -namespace NadekoBot.Classes.Music { - - public class StreamRequest { - public Server Server { get; } - public User User { get; } - public string Query { get; } - - public string Title { get; internal set; } = String.Empty; - private string Provider { get; set; } - - public string FullPrettyName => $"**【 {Title.TrimTo(55)} 】**`{(Provider == null ? "-" : Provider)}`"; - - private MusicStreamer musicStreamer = null; - public StreamState State => musicStreamer?.State ?? privateState; - private StreamState privateState = StreamState.Resolving; - - public bool IsPaused => MusicControls.IsPaused; - - public float Volume => MusicControls?.Volume ?? 1.0f; - - public MusicType LinkType { get; } - - public MusicPlayer MusicControls; - - public StreamRequest(CommandEventArgs e, string query, MusicPlayer mc, MusicType musicType = MusicType.Normal) { - if (e == null) - throw new ArgumentNullException(nameof(e)); - if (query == null) - throw new ArgumentNullException(nameof(query)); - this.MusicControls = mc; - this.Server = e.Server; - this.Query = query; - this.LinkType = musicType; - mc.AddSong(this); - } - - public async Task Resolve() { - string uri = null; - - - musicStreamer = new MusicStreamer(this, uri); - musicStreamer.OnCompleted += () => { - OnCompleted(); - }; - OnQueued(); - } - - internal string PrintStats() => musicStreamer?.Stats(); - - public event Action OnQueued = delegate { }; - public event Action OnStarted = delegate { }; - public event Action OnCompleted = delegate { }; - public event Action OnResolvingFailed = delegate { }; - - internal void Cancel() { - musicStreamer?.Cancel(); - } - - internal void Stop() { - musicStreamer?.Stop(); - } - - protected void Complete() { - OnCompleted(); - } - - internal async Task Start() { - int attemptsLeft = 4; - //wait for up to 4 seconds to resolve a link - try { - while (State == StreamState.Resolving) { - await Task.Delay(1000); - if (--attemptsLeft == 0) { - throw new TimeoutException("Resolving timed out."); - } - } - OnStarted(); - await musicStreamer.StartPlayback(); - } - catch (TimeoutException) { - Console.WriteLine("Resolving timed out."); - privateState = StreamState.Completed; - } - catch (Exception ex) { - Console.WriteLine("Error in start playback." + ex.Message); - privateState = StreamState.Completed; - } - } - } - - public class MusicStreamer { - private DualStream buffer; - - public StreamState State { get; internal set; } - public string Url { get; } - private bool IsCanceled { get; set; } - public bool IsPaused => parent.IsPaused; - - public event Action OnCompleted = delegate { }; - - StreamRequest parent; - private readonly object _bufferLock = new object(); - private bool prebufferingComplete = false; - - public MusicStreamer(StreamRequest parent, string directUrl) { - this.parent = parent; - this.buffer = new DualStream(); - this.Url = directUrl; - State = StreamState.Queued; - } - - public string Stats() => - "--------------------------------\n" + - $"Music stats for {string.Join("", parent.Title.TrimTo(50))}\n" + - $"Server: {parent.Server.Name}\n" + - $"Length:{buffer.Length * 1.0f / 1.MB()}MB Status: {State}\n" + - "--------------------------------\n"; - - private async Task BufferSong() { - //start feeding the buffer - - Process p = null; - try { - var psi = new ProcessStartInfo { - FileName = "ffmpeg", - Arguments = $"-i {Url} -f s16le -ar 48000 -ac 2 pipe:1 -loglevel quiet", //+ (NadekoBot.IsLinux ? "2> /dev/null" : "2>NUL"), - UseShellExecute = false, - RedirectStandardOutput = true, - }; - using (p = Process.Start(psi)) { - int attempt = 0; - while (true) { - while (buffer.writePos - buffer.readPos > 5.MB() && State != StreamState.Completed) { - prebufferingComplete = true; - await Task.Delay(200); - } - - if (State == StreamState.Completed) { - Console.WriteLine("Buffering canceled, stream is completed."); - try { - p.CancelOutputRead(); - } - catch { } - try { - p.Close(); - } - catch { } - p.Dispose(); - return; - } - if (buffer.readPos > 5.MiB() && buffer.writePos > 5.MiB()) { - var skip = 5.MB(); - lock (_bufferLock) { - byte[] data = new byte[buffer.Length - skip]; - Buffer.BlockCopy(buffer.GetBuffer(), skip, data, 0, (int)(buffer.Length - skip)); - var newReadPos = buffer.readPos - skip; - var newPos = buffer.Position - skip; - buffer = new DualStream(); - buffer.Write(data, 0, data.Length); - buffer.readPos = newReadPos; - buffer.Position = newPos; - } - } - int blockSize = 1920 * NadekoBot.client.GetService()?.Config?.Channels ?? 3840; - var buf = new byte[blockSize]; - int read = 0; - read = await p.StandardOutput.BaseStream.ReadAsync(buf, 0, blockSize); - //Console.WriteLine($"Read: {read}"); - if (read == 0) { - if (attempt == 5) { - Console.WriteLine($"Didn't read anything from the stream for {attempt} attempts. {buffer.Length / 1.MB()}MB length"); - try { - p.CancelOutputRead(); - } - catch { } - try { - p.Close(); - } - catch { } - p.Dispose(); - return; - } - else { - ++attempt; - await Task.Delay(20); - } - } - else { - attempt = 0; - lock (_bufferLock) { - buffer.Write(buf, 0, read); - } - } - } - } - } - catch { } - finally { - if (p != null) { - p.Dispose(); - p = null; - } - } - } - - internal async Task StartPlayback() { - Console.WriteLine("Starting playback."); - if (State == StreamState.Playing) return; - State = StreamState.Playing; - - Task.Factory.StartNew(async () => { - await BufferSong(); - }).ConfigureAwait(false); - - // prebuffering wait stuff start - int bufferAttempts = 0; - int waitPerAttempt = 500; - int toAttemptTimes = parent.LinkType != MusicType.Normal ? 4 : 8; - while (!prebufferingComplete && bufferAttempts++ < toAttemptTimes) { - await Task.Delay(waitPerAttempt); - } - if (prebufferingComplete) { - Console.WriteLine($"Prebuffering finished in {bufferAttempts * 500}"); - } - // prebuffering wait stuff end - - if (buffer.Length > 0) { - Console.WriteLine("Prebuffering complete."); - } - else { - Console.WriteLine("Nothing was buffered, try another song and check your GoogleApikey."); - } - - int blockSize = 1920 * NadekoBot.client.GetService()?.Config?.Channels ?? 3840; - byte[] voiceBuffer = new byte[blockSize]; - - int attempt = 0; - while (!IsCanceled) { - int readCount = 0; - //adjust volume - - lock (_bufferLock) { - readCount = buffer.Read(voiceBuffer, 0, blockSize); - } - - if (readCount == 0) { - if (attempt == 4) { - Console.WriteLine($"Failed to read {attempt} times. Breaking out."); - break; - } - else { - ++attempt; - await Task.Delay(15); - } - } - else - attempt = 0; - - if (State == StreamState.Completed) { - Console.WriteLine("Canceled"); - break; - } - voiceBuffer = adjustVolume(voiceBuffer, parent.Volume); - parent.MusicControls.VoiceClient.Send(voiceBuffer, 0, readCount); - - while (IsPaused) { - await Task.Delay(100); - } - } - Stop(); - } - - internal void Cancel() { - IsCanceled = true; - } - - internal void Stop() { - if (State == StreamState.Completed) return; - var oldState = State; - State = StreamState.Completed; - if (oldState == StreamState.Playing) - OnCompleted(); - } - } - - public class DualStream : MemoryStream { - public long readPos; - public long writePos; - - public DualStream() { - readPos = writePos = 0; - } - - public override int Read(byte[] buffer, int offset, int count) { - Position = readPos; - int read = base.Read(buffer, offset, count); - readPos = Position; - return read; - } - public override void Write(byte[] buffer, int offset, int count) { - Position = writePos; - base.Write(buffer, offset, count); - writePos = Position; - } - } -} -*/ \ No newline at end of file diff --git a/NadekoBot/Classes/NadekoStats.cs b/NadekoBot/Classes/NadekoStats.cs index 7a724d41..a8fc8f94 100644 --- a/NadekoBot/Classes/NadekoStats.cs +++ b/NadekoBot/Classes/NadekoStats.cs @@ -182,7 +182,7 @@ namespace NadekoBot .Sum(x => x.Users.Count(u => u.Status == UserStatus.Online))); var connectedServers = NadekoBot.Client.Servers.Count(); - Classes.DbHandler.Instance.InsertData(new Classes._DataModels.Stats + Classes.DbHandler.Instance.InsertData(new _DataModels.Stats { OnlineUsers = onlineUsers, RealOnlineUsers = realOnlineUsers, @@ -207,7 +207,7 @@ namespace NadekoBot try { commandsRan++; - Classes.DbHandler.Instance.InsertData(new Classes._DataModels.Command + Classes.DbHandler.Instance.InsertData(new _DataModels.Command { ServerId = (long)e.Server.Id, ServerName = e.Server.Name, diff --git a/NadekoBot/Modules/Administration/AdministrationModule.cs b/NadekoBot/Modules/Administration/AdministrationModule.cs index b09c61be..572849eb 100644 --- a/NadekoBot/Modules/Administration/AdministrationModule.cs +++ b/NadekoBot/Modules/Administration/AdministrationModule.cs @@ -1,8 +1,8 @@ using Discord; using Discord.Commands; using Discord.Modules; +using NadekoBot._DataModels; using NadekoBot.Classes; -using NadekoBot.Classes._DataModels; using NadekoBot.Extensions; using NadekoBot.Modules.Administration.Commands; using NadekoBot.Modules.Permissions.Classes; @@ -727,7 +727,7 @@ namespace NadekoBot.Modules.Administration await Task.Run(() => { SaveParseToDb("data/parsedata/Announcements.json"); - SaveParseToDb("data/parsedata/CommandsRan.json"); + SaveParseToDb<_DataModels.Command>("data/parsedata/CommandsRan.json"); SaveParseToDb("data/parsedata/Requests.json"); SaveParseToDb("data/parsedata/Stats.json"); SaveParseToDb("data/parsedata/TypingArticles.json"); diff --git a/NadekoBot/Modules/Administration/Commands/Remind.cs b/NadekoBot/Modules/Administration/Commands/Remind.cs index 54a82193..3b0f20d8 100644 --- a/NadekoBot/Modules/Administration/Commands/Remind.cs +++ b/NadekoBot/Modules/Administration/Commands/Remind.cs @@ -1,7 +1,7 @@ using Discord; using Discord.Commands; using NadekoBot.Classes; -using NadekoBot.Classes._DataModels; +using NadekoBot._DataModels; using NadekoBot.Classes; using System; using System.Collections.Generic; diff --git a/NadekoBot/Modules/Administration/Commands/ServerGreetCommand.cs b/NadekoBot/Modules/Administration/Commands/ServerGreetCommand.cs index 797f7a46..80e346b0 100644 --- a/NadekoBot/Modules/Administration/Commands/ServerGreetCommand.cs +++ b/NadekoBot/Modules/Administration/Commands/ServerGreetCommand.cs @@ -34,7 +34,7 @@ namespace NadekoBot.Modules.Administration.Commands NadekoBot.Client.UserJoined += UserJoined; NadekoBot.Client.UserLeft += UserLeft; - var data = Classes.DbHandler.Instance.GetAllRows(); + var data = Classes.DbHandler.Instance.GetAllRows<_DataModels.Announcement>(); if (!data.Any()) return; foreach (var obj in data) @@ -114,7 +114,7 @@ namespace NadekoBot.Modules.Administration.Commands public class AnnounceControls { - private Classes._DataModels.Announcement _model { get; } + private _DataModels.Announcement _model { get; } public bool Greet { get { return _model.Greet; } @@ -160,14 +160,14 @@ namespace NadekoBot.Modules.Administration.Commands set { _model.ServerId = (long)value; } } - public AnnounceControls(Classes._DataModels.Announcement model) + public AnnounceControls(_DataModels.Announcement model) { this._model = model; } public AnnounceControls(ulong serverId) { - this._model = new Classes._DataModels.Announcement(); + this._model = new _DataModels.Announcement(); ServerId = serverId; } diff --git a/NadekoBot/Modules/Conversations/Commands/RequestsCommand.cs b/NadekoBot/Modules/Conversations/Commands/RequestsCommand.cs index 98c2d8ff..e57e8b90 100644 --- a/NadekoBot/Modules/Conversations/Commands/RequestsCommand.cs +++ b/NadekoBot/Modules/Conversations/Commands/RequestsCommand.cs @@ -10,7 +10,7 @@ namespace NadekoBot.Classes.Conversations.Commands { public void SaveRequest(CommandEventArgs e, string text) { - Classes.DbHandler.Instance.InsertData(new Classes._DataModels.Request + DbHandler.Instance.InsertData(new _DataModels.Request { RequestText = text, UserName = e.User.Name, @@ -23,7 +23,7 @@ namespace NadekoBot.Classes.Conversations.Commands // todo what if it's too long? public string GetRequests() { - var task = Classes.DbHandler.Instance.GetAllRows(); + var task = DbHandler.Instance.GetAllRows<_DataModels.Request>(); var str = "Here are all current requests for NadekoBot:\n\n"; foreach (var reqObj in task) @@ -35,14 +35,14 @@ namespace NadekoBot.Classes.Conversations.Commands } public bool DeleteRequest(int requestNumber) => - Classes.DbHandler.Instance.Delete(requestNumber) != null; + DbHandler.Instance.Delete<_DataModels.Request>(requestNumber) != null; /// /// Delete a request with a number and returns that request object. /// /// RequestObject of the request. Null if none - public Classes._DataModels.Request ResolveRequest(int requestNumber) => - Classes.DbHandler.Instance.Delete(requestNumber); + public _DataModels.Request ResolveRequest(int requestNumber) => + DbHandler.Instance.Delete<_DataModels.Request>(requestNumber); internal override void Init(CommandGroupBuilder cgb) { diff --git a/NadekoBot/Modules/Conversations/Conversations.cs b/NadekoBot/Modules/Conversations/Conversations.cs index 10fb21ca..9c349152 100644 --- a/NadekoBot/Modules/Conversations/Conversations.cs +++ b/NadekoBot/Modules/Conversations/Conversations.cs @@ -89,7 +89,7 @@ namespace NadekoBot.Modules.Conversations if (string.IsNullOrWhiteSpace(text)) return; await Task.Run(() => - Classes.DbHandler.Instance.InsertData(new Classes._DataModels.UserQuote() + Classes.DbHandler.Instance.InsertData(new _DataModels.UserQuote() { DateAdded = DateTime.Now, Keyword = e.GetArg("keyword").ToLowerInvariant(), @@ -110,7 +110,7 @@ namespace NadekoBot.Modules.Conversations return; var quote = - Classes.DbHandler.Instance.GetRandom( + Classes.DbHandler.Instance.GetRandom<_DataModels.UserQuote>( uqm => uqm.Keyword == keyword); if (quote != null) diff --git a/NadekoBot/Modules/Games/Commands/SpeedTyping.cs b/NadekoBot/Modules/Games/Commands/SpeedTyping.cs index 1337f78a..d71045d3 100644 --- a/NadekoBot/Modules/Games/Commands/SpeedTyping.cs +++ b/NadekoBot/Modules/Games/Commands/SpeedTyping.cs @@ -1,7 +1,7 @@ using Discord; using Discord.Commands; using NadekoBot.Classes; -using NadekoBot.Classes._DataModels; +using NadekoBot._DataModels; using NadekoBot.Classes; using NadekoBot.Extensions; using System; diff --git a/NadekoBot/Classes/Music/MusicControls.cs b/NadekoBot/Modules/Music/Classes/MusicControls.cs similarity index 99% rename from NadekoBot/Classes/Music/MusicControls.cs rename to NadekoBot/Modules/Music/Classes/MusicControls.cs index 753eb767..70c5ef98 100644 --- a/NadekoBot/Classes/Music/MusicControls.cs +++ b/NadekoBot/Modules/Music/Classes/MusicControls.cs @@ -5,7 +5,7 @@ using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; -namespace NadekoBot.Classes.Music +namespace NadekoBot.Modules.Music.Classes { public enum MusicType diff --git a/NadekoBot/Classes/Music/PoopyBuffer.cs b/NadekoBot/Modules/Music/Classes/PoopyBuffer.cs similarity index 99% rename from NadekoBot/Classes/Music/PoopyBuffer.cs rename to NadekoBot/Modules/Music/Classes/PoopyBuffer.cs index 9d59d35b..fed64502 100644 --- a/NadekoBot/Classes/Music/PoopyBuffer.cs +++ b/NadekoBot/Modules/Music/Classes/PoopyBuffer.cs @@ -2,7 +2,7 @@ using System.Threading; using System.Threading.Tasks; -namespace NadekoBot.Classes.Music +namespace NadekoBot.Modules.Music.Classes { /// diff --git a/NadekoBot/Classes/Music/Song.cs b/NadekoBot/Modules/Music/Classes/Song.cs similarity index 99% rename from NadekoBot/Classes/Music/Song.cs rename to NadekoBot/Modules/Music/Classes/Song.cs index a5bf151b..2abf22f3 100644 --- a/NadekoBot/Classes/Music/Song.cs +++ b/NadekoBot/Modules/Music/Classes/Song.cs @@ -1,4 +1,5 @@ using Discord.Audio; +using NadekoBot.Classes; using NadekoBot.Extensions; using System; using System.Diagnostics; @@ -9,7 +10,7 @@ using System.Threading; using System.Threading.Tasks; using VideoLibrary; -namespace NadekoBot.Classes.Music +namespace NadekoBot.Modules.Music.Classes { public class SongInfo { diff --git a/NadekoBot/Classes/Music/SoundCloud.cs b/NadekoBot/Modules/Music/Classes/SoundCloud.cs similarity index 93% rename from NadekoBot/Classes/Music/SoundCloud.cs rename to NadekoBot/Modules/Music/Classes/SoundCloud.cs index 8277f2d6..10019574 100644 --- a/NadekoBot/Classes/Music/SoundCloud.cs +++ b/NadekoBot/Modules/Music/Classes/SoundCloud.cs @@ -1,15 +1,19 @@ -using System; +using NadekoBot.Classes; +using System; using System.Threading.Tasks; -namespace NadekoBot.Classes.Music { - public class SoundCloud { +namespace NadekoBot.Modules.Music.Classes +{ + public class SoundCloud + { private static readonly SoundCloud _instance = new SoundCloud(); public static SoundCloud Default => _instance; static SoundCloud() { } public SoundCloud() { } - public async Task GetVideoAsync(string url) { + public async Task GetVideoAsync(string url) + { if (string.IsNullOrWhiteSpace(url)) throw new ArgumentNullException(nameof(url)); if (string.IsNullOrWhiteSpace(NadekoBot.Creds.SoundCloudClientID)) @@ -28,8 +32,9 @@ namespace NadekoBot.Classes.Music { System.Text.RegularExpressions.Regex.IsMatch(url, "(.*)(soundcloud.com|snd.sc)(.*)"); } - public class SoundCloudVideo { - public string Kind =""; + public class SoundCloudVideo + { + public string Kind = ""; public long Id = 0; public SoundCloudUser User = new SoundCloudUser(); public string Title = ""; @@ -37,7 +42,8 @@ namespace NadekoBot.Classes.Music { public bool Streamable = false; public string StreamLink => $"https://api.soundcloud.com/tracks/{Id}/stream?client_id={NadekoBot.Creds.SoundCloudClientID}"; } - public class SoundCloudUser { + public class SoundCloudUser + { [Newtonsoft.Json.JsonProperty("username")] public string Name; } diff --git a/NadekoBot/Modules/Music/MusicModule.cs b/NadekoBot/Modules/Music/MusicModule.cs index 1d0d7107..00b1af41 100644 --- a/NadekoBot/Modules/Music/MusicModule.cs +++ b/NadekoBot/Modules/Music/MusicModule.cs @@ -1,10 +1,10 @@ using Discord; using Discord.Commands; using Discord.Modules; +using NadekoBot._DataModels; using NadekoBot.Classes; -using NadekoBot.Classes._DataModels; -using NadekoBot.Classes.Music; using NadekoBot.Extensions; +using NadekoBot.Modules.Music.Classes; using NadekoBot.Modules.Permissions.Classes; using System; using System.Collections.Concurrent; @@ -443,7 +443,7 @@ namespace NadekoBot.Modules.Music return; - var songInfos = currentPlaylist.Select(s => new Classes._DataModels.SongInfo + var songInfos = currentPlaylist.Select(s => new _DataModels.SongInfo { Provider = s.SongInfo.Provider, ProviderType = (int)s.SongInfo.ProviderType, @@ -519,7 +519,7 @@ namespace NadekoBot.Modules.Music psi.PlaylistId == playlist.Id); var songInfos = psis.Select(psi => DbHandler.Instance - .FindOne(si => si.Id == psi.SongInfoId)); + .FindOne<_DataModels.SongInfo>(si => si.Id == psi.SongInfoId)); await e.Channel.SendMessage($"`Attempting to load {songInfos.Count()} songs`"); foreach (var si in songInfos) diff --git a/NadekoBot/Modules/Pokemon/PokemonModule.cs b/NadekoBot/Modules/Pokemon/PokemonModule.cs index bee391a2..87e9a7e6 100644 --- a/NadekoBot/Modules/Pokemon/PokemonModule.cs +++ b/NadekoBot/Modules/Pokemon/PokemonModule.cs @@ -1,7 +1,7 @@ using Discord.Commands; using Discord.Modules; using NadekoBot.Classes; -using NadekoBot.Classes._DataModels; +using NadekoBot._DataModels; using NadekoBot.Classes.JSONModels; using NadekoBot.Extensions; using NadekoBot.Modules.Permissions.Classes; diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index eaea0f52..798488db 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -134,32 +134,31 @@ - - - - - - - - - + + + + + + + + - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + @@ -176,9 +175,9 @@ - - - + + + @@ -472,7 +471,9 @@ - + + +