diff --git a/.vs/youtube-downloader/v16/.suo b/.vs/youtube-downloader/v16/.suo index 6200525..5152693 100644 Binary files a/.vs/youtube-downloader/v16/.suo and b/.vs/youtube-downloader/v16/.suo differ diff --git a/Program.cs b/Program.cs index 5d5eff0..1206ab9 100644 --- a/Program.cs +++ b/Program.cs @@ -1,5 +1,6 @@ using CommandLine; using System; +using System.Collections.Generic; using System.IO; using System.IO.Pipes; using System.Reflection; @@ -13,104 +14,56 @@ namespace youtube_downloader class Program { - static void SendStringArray(System.IO.Stream s, string[] array) + public static string[] ArgsFromString(string s) { - s.Write(BitConverter.GetBytes(array.Length), 0, 4); - foreach (var item in array) + List s2 = new List(); + StringBuilder b = new StringBuilder(); + bool inQuote=false; + foreach(var c in s) { - byte[] lenOfitem = BitConverter.GetBytes(item.Length); - byte[] argtext = System.Text.Encoding.UTF8.GetBytes(item); - s.Write(lenOfitem, 0, lenOfitem.Length); - s.Write(argtext, 0, argtext.Length); + if(c == '\"') + { + inQuote = !inQuote; + continue; + } + if(c == ' ') + { + if (!inQuote) + { + if (b.Length > 0) + { + s2.Add(b.ToString()); + b.Clear(); + } + continue; + } + + + + } + b.Append(c); } - } - static string[] GetStringArray(System.IO.Stream s) - { - byte[] items = new byte[4]; - s.Read(items, 0, items.Length); - int items2 = BitConverter.ToInt32(items, 0); - - string[] arraydata = new string[items2]; - for (int i = 0; i < items2; i++) + if (b.Length > 0) { - s.Read(items, 0, 4); - int item3 = BitConverter.ToInt32(items, 0); - byte[] v = new byte[item3]; - s.Read(v, 0, item3); - arraydata[i] = System.Text.Encoding.UTF8.GetString(v); + s2.Add(b.ToString()); + b.Clear(); } - return arraydata; + return s2.ToArray(); } - static string _AppName = - Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().GetName().Name); static void Main(string[] args) { // we need to get our app name so that // we can create unique names for our mutex and our pipe - - var notAlreadyRunning = true; - // wrap the meat of the application code with a named mutex so it runs only once - using (var mutex = new Mutex(true, _AppName + "Singleton", out notAlreadyRunning)) + do { - if (notAlreadyRunning) - { - // do additional work here, startup stuff - // Console.WriteLine("Running. Press any key to exit..."); - // ... - // now process our initial main command line - _ProcessCommandLine(args); - // start the IPC sink. - var srv = new NamedPipeServerStream(_AppName + "IPC", - PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); - // it's easier to use the AsyncCallback than it is to use Tasks here: - // this can't block, so some form of async is a must - - srv.BeginWaitForConnection(new AsyncCallback(_ConnectionHandler), srv); - - // block here until exit - Console.ReadKey(); - // if this was a windows forms app you would put your - // "Applicantion.Run(new MyForm());" here - // finally, run any teardown code and exit - - srv.Close(); - } - else // another instance is running - { - // connect to the main app - var cli = new NamedPipeClientStream(".", _AppName + "IPC", PipeDirection.InOut); - cli.Connect(); - SendStringArray(cli, args); - byte[] leng = new byte[4]; - cli.Read(leng, 0, 4); - int sz = BitConverter.ToInt32(leng,0); - byte[] sdata = new byte[sz]; - cli.Read(sdata, 0, sz); - string strdata = System.Text.Encoding.UTF8.GetString(sdata); - Console.Write(strdata); - // serialize and send the command line - - cli.Close(); - // and exit - } - } - } - static void _ConnectionHandler(IAsyncResult result) - { - var srv = result.AsyncState as NamedPipeServerStream; - srv.EndWaitForConnection(result); - // we're connected, now deserialize the incoming command line + Console.Write("> "); + var a2 = ArgsFromString(Console.ReadLine()); + _ProcessCommandLine(a2); + } while (true); + - var inargs = GetStringArray(srv); - r = new RESPONSE(srv); - // process incoming command line - _ProcessCommandLine(inargs); - r.SendResponse(); - srv = new NamedPipeServerStream(_AppName + "IPC", PipeDirection.InOut, - 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); - - srv.BeginWaitForConnection(new AsyncCallback(_ConnectionHandler), srv); } + [Verb("exit", HelpText = "Download Video")] @@ -179,7 +132,7 @@ namespace youtube_downloader public string Page { get; set; } } - static RESPONSE r=null; + static void _ProcessCommandLine(string[] args) { CommandLine.Parser.Default.ParseArguments(args) @@ -228,15 +181,16 @@ namespace youtube_downloader } } private static int RunInfo(Info opts) - { if (r != null) - { + { + //if ( != null) + switch (opts.Page) { case "progress": if (opts.ForMachine) { string jsonData = Newtonsoft.Json.JsonConvert.SerializeObject(Server.Functions.Downloader.GetProgress()); - r.SendResponse(jsonData); + Console.WriteLine(jsonData); } else { @@ -248,15 +202,15 @@ namespace youtube_downloader sb.AppendLine(); sb.AppendLine("=======Video Info======="); WriteVideoInfo(sb, s.Saved, false); - r.SendResponse(sb.ToString()); - + Console.WriteLine(sb.ToString()); + } break; case "queue": if (opts.ForMachine) { string jsonData = Server.Functions.Downloader.GetQueue(); - r.SendResponse(jsonData); + Console.WriteLine(jsonData); } else { @@ -267,17 +221,17 @@ namespace youtube_downloader WriteVideoInfo(sb, item.Video, false); sb.AppendLine(); } - r.SendResponse(sb.ToString()); + Console.WriteLine(sb.ToString()); } break; case "location": - r.SendResponse(Server.Functions.Downloader.DL.StorageLocation); + Console.WriteLine(Server.Functions.Downloader.DL.StorageLocation); break; } - } + return 0; } diff --git a/RESPONSE.cs b/RESPONSE.cs deleted file mode 100644 index 171eba9..0000000 --- a/RESPONSE.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; - -namespace youtube_downloader -{ - internal class RESPONSE - { - public RESPONSE(System.IO.Stream strm) - { - this.strm = strm; - } - System.IO.Stream strm; - public void SendResponse(string response) - { - if(strm != null) - { - strm.Write(BitConverter.GetBytes(response.Length), 0, 4); - strm.Write(System.Text.Encoding.UTF8.GetBytes(response), 0, response.Length); - strm.Dispose(); - strm = null; - } - } - public void SendResponse() - { - SendResponse(""); - } - } -} \ No newline at end of file diff --git a/bin/Debug/youtube-downloader.exe b/bin/Debug/youtube-downloader.exe index 469a241..9afdb35 100644 Binary files a/bin/Debug/youtube-downloader.exe and b/bin/Debug/youtube-downloader.exe differ diff --git a/bin/Debug/youtube-downloader.pdb b/bin/Debug/youtube-downloader.pdb index d1830f0..1bb2a55 100644 Binary files a/bin/Debug/youtube-downloader.pdb and b/bin/Debug/youtube-downloader.pdb differ diff --git a/obj/Debug/youtube-downloader.csproj.AssemblyReference.cache b/obj/Debug/youtube-downloader.csproj.AssemblyReference.cache index 3388fec..6f8d20a 100644 Binary files a/obj/Debug/youtube-downloader.csproj.AssemblyReference.cache and b/obj/Debug/youtube-downloader.csproj.AssemblyReference.cache differ diff --git a/obj/Debug/youtube-downloader.csproj.CoreCompileInputs.cache b/obj/Debug/youtube-downloader.csproj.CoreCompileInputs.cache index b2dd89a..ddcdfba 100644 --- a/obj/Debug/youtube-downloader.csproj.CoreCompileInputs.cache +++ b/obj/Debug/youtube-downloader.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -6acf12007b654605f4a149e8bf10292d9c49014f +2a5e8789af6d18f40163043d238b63564245c869 diff --git a/obj/Debug/youtube-downloader.exe b/obj/Debug/youtube-downloader.exe index 469a241..9afdb35 100644 Binary files a/obj/Debug/youtube-downloader.exe and b/obj/Debug/youtube-downloader.exe differ diff --git a/obj/Debug/youtube-downloader.pdb b/obj/Debug/youtube-downloader.pdb index d1830f0..1bb2a55 100644 Binary files a/obj/Debug/youtube-downloader.pdb and b/obj/Debug/youtube-downloader.pdb differ diff --git a/youtube-downloader.csproj b/youtube-downloader.csproj index c2fe11f..d1df4be 100644 --- a/youtube-downloader.csproj +++ b/youtube-downloader.csproj @@ -88,7 +88,6 @@ -