-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
156 lines (139 loc) · 6.14 KB
/
Copy pathProgram.cs
File metadata and controls
156 lines (139 loc) · 6.14 KB
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
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using CommandLine;
using CommandLine.Text;
namespace Sticky_Tool
{
class Program
{
class Options
{
[Option("type", HelpText = "Specifies the executable to act on")]
public string ExeType { get; set; }
[Option("replace", HelpText = "Specifies if replace is needed and indicates the replacement file")]
public string Replace { get; set; }
[Option("restore", HelpText = "Specifies if restore is needed")]
public bool Restore { get; set; }
[Option("register", HelpText = "Registers the replacement executable as a program")]
public bool Register { get; set; }
}
[DllImport("kernel32.dll")]
static extern bool FreeConsole();
[STAThread]
static void Main(string[] args)
{
//Introduction
var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
if (args.Count() == 0)
{
FreeConsole();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new gui());
}
Parser.Default.ParseArguments<Options>(args)
.WithParsed<Options>(o =>
{
Console.WriteLine("--------------------");
Console.WriteLine("Sticky Tool by Jacob Raffoul");
Console.WriteLine("Copyright (c) 2023 Raffoul Technologies");
Console.WriteLine($"Version: {version}");
Console.WriteLine("Learn more at https://github.com/RaffTechAU/Sticky-Tool");
Console.WriteLine("--------------------");
if (o.ExeType == null || o.ExeType.ToString() == "")
{
Console.WriteLine();
Console.WriteLine("--type must be provided!");
Console.WriteLine();
}
else if (o.Replace != null && !(o.Restore))
{
try
{
if (!(o.ExeType.EndsWith(".exe")))
{
o.ExeType += ".exe";
}
if (!(Array.Exists(Methods.validTypes, element => element[0] == o.ExeType.ToString())))
{
throw new InvalidOperationException("Provided type is not valid!");
}
var realType = o.ExeType.ToString();
var realPath = Path.GetFullPath(o.Replace);
if (!(File.Exists(realPath)) || !(realPath.EndsWith(".exe")))
{
throw new FileNotFoundException("Provided filepath is not valid!");
}
Methods.Replace(realType, realPath);
Console.WriteLine();
Console.WriteLine($"Successfully replaced {realType} with {realPath}!");
Console.WriteLine();
if (o.Register)
{
Methods.Register(realType);
Console.WriteLine();
Console.WriteLine($"Successfully registered {realType} as a program!");
Console.WriteLine();
}
}
catch (Exception err)
{
Console.WriteLine();
Console.WriteLine(err.Message);
Console.WriteLine();
}
}
else if (o.Restore && !(o.Register) && (o.Replace == "" || o.Replace == null))
{
try
{
if (!(o.ExeType.EndsWith(".exe"))) { o.ExeType += ".exe"; }
if (!(Array.Exists(Methods.validTypes, row => row[0] == o.ExeType.ToString())))
{
throw new InvalidOperationException("Provided type is not valid!");
}
var realType = o.ExeType.ToString();
Methods.Restore(realType);
Console.WriteLine();
Console.WriteLine($"Successfully restored {o.ExeType}!");
Console.WriteLine();
}
catch (Exception err)
{
Console.WriteLine();
if (err.GetType().Name.ToString() == "ArgumentException")
{
Console.WriteLine("Nothing to restore!");
}
else
{
Console.WriteLine(err.Message);
}
Console.WriteLine();
}
}
else
{
Console.WriteLine();
Console.WriteLine("Incorrect parameters provided. Please refer to --help");
Console.WriteLine();
}
})
.WithNotParsed<Options>(errs =>
{
if (errs.Any(x => x.Tag == ErrorType.HelpRequestedError))
{
//do nothing
}
else if (errs.Any(x => x.Tag == ErrorType.VersionRequestedError))
{
//do nothing
}
});
}
}
}