Skip to content

Commit 1a3a29a

Browse files
committed
Remove longform command-line arguments and add -D flag
1 parent 0a5ba38 commit 1a3a29a

File tree

3 files changed

+81
-31
lines changed

3 files changed

+81
-31
lines changed

Decompiler.cs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using DSO.Util;
1717
using DSO.Versions;
1818
using static DSO.Constants.Decompiler;
19+
using static DSO.Util.CommandLineOptions;
1920

2021
namespace DSO
2122
{
@@ -131,10 +132,19 @@ private Tuple<int, int> DecompileDirectory(string path)
131132

132133
private bool DecompileFile(string path)
133134
{
134-
Logger.LogMessage($"Decompiling file: \"{path}\"");
135+
if (_options.OutputDisassembly == DisassemblyOutput.DisassemblyOnly)
136+
{
137+
Logger.LogMessage($"Disassembling file: \"{path}\"");
138+
}
139+
else
140+
{
141+
Logger.LogMessage($"Decompiling file: \"{path}\"");
142+
}
135143

136144
if (_options.GameIdentifier != GameIdentifier.Auto)
137145
{
146+
Logger.LogMessage($"\tUsing game settings: \"{GameVersion.GetDisplayName(_options.GameIdentifier)}\"", ConsoleColor.DarkGray);
147+
138148
return DecompileFile(path, _options.GameIdentifier, silentError: false);
139149
}
140150

@@ -179,7 +189,9 @@ private bool DecompileFile(string path, GameIdentifier identifier, bool silentEr
179189
GameVersion game = new();
180190
FileData fileData;
181191
Disassembly disassembly;
182-
List<Node> nodes;
192+
List<Node> nodes = [];
193+
194+
var disassemblyOnly = _options.OutputDisassembly == DisassemblyOutput.DisassemblyOnly;
183195

184196
try
185197
{
@@ -192,7 +204,11 @@ private bool DecompileFile(string path, GameIdentifier identifier, bool silentEr
192204
}
193205

194206
disassembly = new Disassembler.Disassembler().Disassemble(fileData, game.Ops);
195-
nodes = new Builder().Build(new ControlFlowAnalyzer().Analyze(disassembly), disassembly);
207+
208+
if (!disassemblyOnly)
209+
{
210+
nodes = new Builder().Build(new ControlFlowAnalyzer().Analyze(disassembly), disassembly);
211+
}
196212
}
197213
catch (Exception exception)
198214
{
@@ -206,13 +222,17 @@ private bool DecompileFile(string path, GameIdentifier identifier, bool silentEr
206222
return false;
207223
}
208224

209-
var scriptPath = $"{Directory.GetParent(path)}/{Path.GetFileNameWithoutExtension(path)}";
210225

211-
Logger.LogMessage($"Writing output file: \"{scriptPath}\"");
226+
if (!disassemblyOnly)
227+
{
228+
var scriptPath = $"{Directory.GetParent(path)}/{Path.GetFileNameWithoutExtension(path)}";
229+
230+
Logger.LogMessage($"Writing output file: \"{scriptPath}\"");
212231

213-
WriteScriptFile(scriptPath, nodes);
232+
WriteScriptFile(scriptPath, nodes);
233+
}
214234

215-
if (_options.OutputDisassembly)
235+
if (_options.OutputDisassembly != DisassemblyOutput.None)
216236
{
217237
var disassemblyPath = $"{Directory.GetParent(path)}/{Path.GetFileNameWithoutExtension(path)}{DISASM_EXTENSION}";
218238

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,17 @@ There are two ways to use this program: either as a typical console program, or
3030

3131
To use it normally, just drag a `.dso` file or a directory full of `.dso` files onto the program. It will try to automatically detect and decompile the file(s) that were passed in.
3232

33-
You can also use it as a command-line interface: `usage: dso-sharp path1[, path2[, ...]] [-h] [-q] [-g game_version] [-d] [-X]`
33+
You can also use it as a command-line interface: `usage: dso-sharp path1[, path2[, ...]] [-h] [-q] [-g game] [-d | -D] [-X]`
3434

3535

3636
| Flag | Description |
3737
|:-----------------------|:---------------|
38-
| `-h`, `--help` | Displays help. |
39-
| `-q`, `--quiet` | Disables all messages (except command-line argument errors). |
40-
| `-g`, `--game` | Specifies which game's scripts we are decompiling (default: `auto`). |
41-
| `-d`, `--disassembly` | Writes a `.disasm` file containing the disassembly. |
42-
| `-X`, `--cli` | Makes the program operate as a command-line interface that takes no keyboard input and closes immediately upon completion or failure. |
38+
| `-h` | Displays help. |
39+
| `-q` | Disables all messages (except command-line argument errors). |
40+
| `-g` | Specifies which game's scripts we are decompiling (default: `auto`). |
41+
| `-d` | Writes a `.disasm` file containing the disassembly. |
42+
| `-D` | Writes only the disassembly file and nothing else. |
43+
| `-X` | Makes the program operate as a command-line interface that takes no keyboard input and closes immediately upon completion or failure. |
4344

4445

4546
### Supported Games

Util/CommandLineParser.cs

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,24 @@
1010

1111
using DSO.Versions;
1212
using static DSO.Constants.Decompiler;
13+
using static DSO.Util.CommandLineOptions;
1314

1415
namespace DSO.Util
1516
{
1617
public class CommandLineOptions
1718
{
19+
public enum DisassemblyOutput
20+
{
21+
None,
22+
Disassembly,
23+
DisassemblyOnly,
24+
}
25+
1826
public readonly List<string> Paths = [];
1927

2028
public GameIdentifier GameIdentifier { get; set; } = GameIdentifier.Auto;
2129
public bool Quiet { get; set; } = false;
22-
public bool OutputDisassembly { get; set; } = false;
30+
public DisassemblyOutput OutputDisassembly { get; set; } = DisassemblyOutput.None;
2331
public bool CommandLineMode { get; set; } = false;
2432
}
2533

@@ -40,6 +48,7 @@ static public class CommandLineParser
4048
static public Tuple<bool, CommandLineOptions> Parse(string[] args)
4149
{
4250
var firstFlagSet = false;
51+
var unknownFlag = false;
4352
var options = new CommandLineOptions();
4453
var error = false;
4554
var paths = new List<string>();
@@ -58,24 +67,36 @@ static public Tuple<bool, CommandLineOptions> Parse(string[] args)
5867

5968
switch (arg)
6069
{
61-
case "--help" or "-h":
70+
case "-h":
6271
DisplayHelp();
6372
error = true;
6473
break;
6574

66-
case "--cli" or "-X":
75+
case "-X":
6776
options.CommandLineMode = true;
6877
break;
6978

70-
case "--quiet" or "-q":
79+
case "-q":
7180
options.Quiet = true;
7281
break;
7382

74-
case "--disassembly" or "-d":
75-
options.OutputDisassembly = true;
83+
case "-d":
84+
if (options.OutputDisassembly == DisassemblyOutput.None)
85+
{
86+
options.OutputDisassembly = DisassemblyOutput.Disassembly;
87+
}
88+
89+
break;
90+
91+
case "-D":
92+
if (options.OutputDisassembly != DisassemblyOutput.DisassemblyOnly)
93+
{
94+
options.OutputDisassembly = DisassemblyOutput.DisassemblyOnly;
95+
}
96+
7697
break;
7798

78-
case "--game" or "-g":
99+
case "-g":
79100
{
80101
error = i >= args.Length - 1 || args[i + 1].StartsWith('-');
81102

@@ -127,9 +148,9 @@ static public Tuple<bool, CommandLineOptions> Parse(string[] args)
127148
}
128149
else
129150
{
130-
Logger.LogError($"Unknown or unsupported flag '{arg}'");
151+
Logger.LogError($"Unknown or unsupported flag '{arg}'\n");
131152

132-
if (arg == "-H" || arg == "-Q" || arg == "-G" || arg == "-D")
153+
if (arg == "-H" || arg == "-Q" || arg == "-G")
133154
{
134155
Logger.LogError($"Did you mean '{arg.ToLower()}'?");
135156
}
@@ -138,6 +159,7 @@ static public Tuple<bool, CommandLineOptions> Parse(string[] args)
138159
Logger.LogError($"Did you mean '{arg.ToUpper()}'?");
139160
}
140161

162+
unknownFlag = true;
141163
error = true;
142164
}
143165

@@ -151,7 +173,14 @@ static public Tuple<bool, CommandLineOptions> Parse(string[] args)
151173
}
152174
}
153175

154-
if (!error && options.Paths.Count <= 0)
176+
if (error)
177+
{
178+
if (unknownFlag)
179+
{
180+
DisplayHelp();
181+
}
182+
}
183+
else if (options.Paths.Count <= 0)
155184
{
156185
if (!options.Quiet && !options.CommandLineMode)
157186
{
@@ -170,15 +199,15 @@ static public Tuple<bool, CommandLineOptions> Parse(string[] args)
170199
static private void DisplayHelp()
171200
{
172201
Logger.LogMessage(
173-
"usage: dso-sharp path1[, path2[, ...]] [-h] [-q] [-g game_version] [-d] [-X]\n" +
202+
"usage: dso-sharp path1[, path2[, ...]] [-h] [-q] [-g game] [-d | -D] [-X]\n" +
174203
" options:\n" +
175-
" -h, --help Displays help.\n" +
176-
" -q, --quiet Disables all messages (except command-line argument errors).\n" +
177-
" -g, --game Specifies which game settings to use (default: 'auto').\n" +
178-
" -d, --disassembly Writes a `" + DISASM_EXTENSION + "` file containing the disassembly.\n" +
179-
" -X, --cli Makes the program operate as a command-line interface\n" +
180-
" that takes no keyboard input and closes immediately\n" +
181-
" upon completion or failure.\n"
204+
" -h Displays help.\n" +
205+
" -q Disables all messages (except command-line argument errors).\n" +
206+
" -g Specifies which game settings to use (default: 'auto').\n" +
207+
" -d Writes a `" + DISASM_EXTENSION + "` file containing the disassembly.\n" +
208+
" -D Writes only the disassembly file and nothing else.\n" +
209+
" -X Makes the program operate as a command-line interface that takes\n" +
210+
" no keyboard input and closes immediately upon completion or failure.\n"
182211
);
183212

184213
DisplayVersions();

0 commit comments

Comments
 (0)