Skip to content

Commit

Permalink
Merge pull request #4 from NeluQi/Nelu
Browse files Browse the repository at this point in the history
formatted code
  • Loading branch information
NeluQi authored Jul 6, 2019
2 parents 6235daf + 037eb05 commit 25c6cef
Show file tree
Hide file tree
Showing 13 changed files with 1,436 additions and 1,183 deletions.
39 changes: 25 additions & 14 deletions AnzuW/CommandLine/Command.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
#region copyright

// (c) 2019 Nelu & 601 (github.com/NeluQi)
// This code is licensed under MIT license (see LICENSE for details)

#endregion copyright

using System;

/// <summary>
///
/// Defines the <see cref="Command" />
/// </summary>
public class Command
{
public class Command {
/// <summary>
/// The Execute
/// </summary>
public delegate void Execute();

/// <summary>
/// Defines the MyExecute
/// </summary>
private event Execute MyExecute;

/// <summary>
/// Gets the Alias
/// </summary>
private string[] Alias { get; }

/// <summary>
/// Initializes a new instance of the <see cref="Command"/> class.
/// </summary>
/// <param name="Alias">The Alias<see cref="string[]"/></param>
/// <param name="execute">The execute<see cref="Execute"/></param>
public Command(string[] Alias, Execute execute) //TODO:
{
this.Alias = Alias ?? throw new ArgumentNullException("Àëèàñû êîìàíä íå ìîãóò áûòü ïóñòûìè", nameof(Alias));
MyExecute = execute ?? throw new ArgumentNullException("Äåëåãàò íå ìîæåò áûòü null", nameof(execute));
for (int i = 0; i <= this.Alias.Length; i++)
this.Alias = Alias ?? throw new ArgumentNullException("Error", nameof(Alias));
MyExecute = execute ?? throw new ArgumentNullException("Error", nameof(execute));
for(int i = 0; i <= this.Alias.Length; i++)
this.Alias[i].ToLower();
}

public void Check(string cmd)
{
//TODO:
/// <summary>
/// The Check
/// </summary>
/// <param name="cmd">The cmd<see cref="string"/></param>
public void Check(string cmd) {
}
}
}
112 changes: 85 additions & 27 deletions AnzuW/CommandLine/ConsoleHelper.cs
Original file line number Diff line number Diff line change
@@ -1,75 +1,105 @@
using Microsoft.Win32.SafeHandles;
using System;
using System.IO;
using System.Runtime.InteropServices;

internal static class ConsoleHelper
{
static public void Initialize(bool alwaysCreateNewConsole = true)
{
using Microsoft.Win32.SafeHandles;

/// <summary>
/// Defines the <see cref="ConsoleHelper" />
/// </summary>
internal static class ConsoleHelper {
/// <summary>
/// The Initialize
/// </summary>
/// <param name="alwaysCreateNewConsole">The alwaysCreateNewConsole<see cref="bool"/></param>
static public void Initialize(bool alwaysCreateNewConsole = true) {
bool consoleAttached = true;
if (alwaysCreateNewConsole
if(alwaysCreateNewConsole
|| (AttachConsole(ATTACH_PARRENT) == 0
&& Marshal.GetLastWin32Error() != ERROR_ACCESS_DENIED))
{
&& Marshal.GetLastWin32Error() != ERROR_ACCESS_DENIED)) {
consoleAttached = AllocConsole() != 0;
}

if (consoleAttached)
{
if(consoleAttached) {
InitializeOutStream();
InitializeInStream();
}
}

private static void InitializeOutStream()
{
/// <summary>
/// The InitializeOutStream
/// </summary>
private static void InitializeOutStream() {
var fs = CreateFileStream("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE, FileAccess.Write);
if (fs != null)
{
if(fs != null) {
var writer = new StreamWriter(fs) { AutoFlush = true };
Console.SetOut(writer);
Console.SetError(writer);
}
}

private static void InitializeInStream()
{
/// <summary>
/// The InitializeInStream
/// </summary>
private static void InitializeInStream() {
var fs = CreateFileStream("CONIN$", GENERIC_READ, FILE_SHARE_READ, FileAccess.Read);
if (fs != null)
{
if(fs != null) {
Console.SetIn(new StreamReader(fs));
}
}

/// <summary>
/// The CreateFileStream
/// </summary>
/// <param name="name">The name<see cref="string"/></param>
/// <param name="win32DesiredAccess">The win32DesiredAccess<see cref="uint"/></param>
/// <param name="win32ShareMode">The win32ShareMode<see cref="uint"/></param>
/// <param name="dotNetFileAccess">The dotNetFileAccess<see cref="FileAccess"/></param>
/// <returns>The <see cref="FileStream"/></returns>
private static FileStream CreateFileStream(string name, uint win32DesiredAccess, uint win32ShareMode,
FileAccess dotNetFileAccess)
{
FileAccess dotNetFileAccess) {
var file = new SafeFileHandle(CreateFileW(name, win32DesiredAccess, win32ShareMode, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero), true);
if (!file.IsInvalid)
{
if(!file.IsInvalid) {
var fs = new FileStream(file, dotNetFileAccess);
return fs;
}
return null;
}

#region Win API Functions and Constants

/// <summary>
/// The AllocConsole
/// </summary>
/// <returns>The <see cref="int"/></returns>
[DllImport("kernel32.dll",
EntryPoint = "AllocConsole",
SetLastError = true,
CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
private static extern int AllocConsole();

/// <summary>
/// The AttachConsole
/// </summary>
/// <param name="dwProcessId">The dwProcessId<see cref="UInt32"/></param>
/// <returns>The <see cref="UInt32"/></returns>
[DllImport("kernel32.dll",
EntryPoint = "AttachConsole",
SetLastError = true,
CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
private static extern UInt32 AttachConsole(UInt32 dwProcessId);

/// <summary>
/// The CreateFileW
/// </summary>
/// <param name="lpFileName">The lpFileName<see cref="string"/></param>
/// <param name="dwDesiredAccess">The dwDesiredAccess<see cref="UInt32"/></param>
/// <param name="dwShareMode">The dwShareMode<see cref="UInt32"/></param>
/// <param name="lpSecurityAttributes">The lpSecurityAttributes<see cref="IntPtr"/></param>
/// <param name="dwCreationDisposition">The dwCreationDisposition<see cref="UInt32"/></param>
/// <param name="dwFlagsAndAttributes">The dwFlagsAndAttributes<see cref="UInt32"/></param>
/// <param name="hTemplateFile">The hTemplateFile<see cref="IntPtr"/></param>
/// <returns>The <see cref="IntPtr"/></returns>
[DllImport("kernel32.dll",
EntryPoint = "CreateFileW",
SetLastError = true,
Expand All @@ -85,15 +115,43 @@ private static extern IntPtr CreateFileW(
IntPtr hTemplateFile
);

/// <summary>
/// Defines the GENERIC_WRITE
/// </summary>
private const UInt32 GENERIC_WRITE = 0x40000000;

/// <summary>
/// Defines the GENERIC_READ
/// </summary>
private const UInt32 GENERIC_READ = 0x80000000;

/// <summary>
/// Defines the FILE_SHARE_READ
/// </summary>
private const UInt32 FILE_SHARE_READ = 0x00000001;

/// <summary>
/// Defines the FILE_SHARE_WRITE
/// </summary>
private const UInt32 FILE_SHARE_WRITE = 0x00000002;

/// <summary>
/// Defines the OPEN_EXISTING
/// </summary>
private const UInt32 OPEN_EXISTING = 0x00000003;

/// <summary>
/// Defines the FILE_ATTRIBUTE_NORMAL
/// </summary>
private const UInt32 FILE_ATTRIBUTE_NORMAL = 0x80;

/// <summary>
/// Defines the ERROR_ACCESS_DENIED
/// </summary>
private const UInt32 ERROR_ACCESS_DENIED = 5;

/// <summary>
/// Defines the ATTACH_PARRENT
/// </summary>
private const UInt32 ATTACH_PARRENT = 0xFFFFFFFF;

#endregion Win API Functions and Constants
}
}
29 changes: 16 additions & 13 deletions AnzuW/CommandLine/ControllerCommand.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
#region copyright

// (c) 2019 Nelu & 601 (github.com/NeluQi)
// (c) 2019 Nelu & 601 (github.com/NeluQi)
// This code is licensed under MIT license (see LICENSE for details)

#endregion copyright

using System;

/// <summary>
///TODO:
/// TODO:
/// </summary>
internal class ControllerCommand
{
internal class ControllerCommand {
/// <summary>
/// Gets the Commands
/// </summary>
public Command[] Commands { get; }

public ControllerCommand(string[] args)
{
/// <summary>
/// Initializes a new instance of the <see cref="ControllerCommand"/> class.
/// </summary>
/// <param name="args">The args<see cref="string[]"/></param>
public ControllerCommand(string[] args) {
Commands = new Command[] {
new Command(new string[]{"--consolemode", "-con"}, ConsoleMode)
};
}

//TODO: Parse com line
public void ConsoleMode()
{
/// <summary>
/// The ConsoleMode
/// </summary>
public void ConsoleMode() {
ConsoleHelper.Initialize();
Console.WriteLine("Run console mode");
}
}
}
Loading

0 comments on commit 25c6cef

Please sign in to comment.