-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStartup.cs
145 lines (130 loc) · 4.4 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using Microsoft.Win32.SafeHandles;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DriveCom
{
class Startup
{
private static SMIDevice _device = null;
public enum ExitCode
{
Success = 0,
Failure = 1
}
static void Main(string[] args)
{
try
{
Environment.ExitCode = (int)ExitCode.Success;
string drive = string.Empty;
foreach (var arg in args)
{
var parts = arg.TrimStart(new char[] { '/' }).Split(new char[] { '=' },
StringSplitOptions.RemoveEmptyEntries);
switch (parts[0].ToLower())
{
case "drive":
{
drive = parts[1];
break;
}
default:
{
break;
}
}
}
if (!string.IsNullOrEmpty(drive))
{
_OpenDrive(drive);
}
Console.WriteLine("Valid Commands:\nopen [drive]\nclose\nreadcid [magic byte] (Usually 2 or 4)\nwritecid [cid.bin]");
bool exiting = false;
while (!exiting)
{
Console.Write(">");
var line = Console.ReadLine();
var @params = line.Split(new char[] { ' ' });
try
{
switch (@params[0].ToLower())
{
case "open":
{
_OpenDrive(@params[1]);
break;
}
case "close":
{
_CloseDrive();
break;
}
case "readcid":
{
byte magic = 0x02;
if (@params.Length == 2)
{
magic = Convert.ToByte(@params[1]);
}
_device.getDeviceCID(magic);
_device.info.printInfo();
break;
}
case "writecid":
{
_device.setDeviceCID(@params[1]);
break;
}
case "quit":
case "exit":
{
exiting = true;
break;
}
default:
Console.WriteLine("Invalid command: " + @params[0]);
break;
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR: " + ex.ToString());
}
}
Console.WriteLine("Done.");
}
catch (Exception ex)
{
Environment.ExitCode = (int)ExitCode.Failure;
Console.WriteLine("FATAL: " + ex.ToString());
}
finally
{
if (_device != null)
{
_device.Close();
}
}
}
private static void _OpenDrive(string drive)
{
_CloseDrive();
_device = new SMIDevice(drive[0]);
_device.Open();
}
private static void _CloseDrive()
{
if (_device != null)
{
_device.Close();
_device = null;
}
}
}
}