Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Config.cs « CoolgirlCombiner « tools_sources - github.com/coolgirl-multicart/coolgirl-multirom-builder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fb3ba80dcb4239a017a8244985362f4211ce880a (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace com.clusterrr.Famicom.CoolGirl
{
    internal class Config
    {
        const string DEFAULT_MAPPERS_FILE = @"coolgirl-mappers.json";
        const string DEFAULT_FIXES_FILE = @"coolgirl-fixes.json";
        const string DEFAULT_SYMBOLS_FILE = @"coolgirl-symbols.json";
        public const string commandPrepare = "prepare";
        public const string commandCombine = "combine";
        public const string commandBuild = "build";

        public enum CombinerCommand
        {
            Prepare,
            Combine,
            Build,
        }

        public enum CombinerLanguage
        {
            English,
            Russian
        }

        public CombinerCommand Command { get; private set; } = CombinerCommand.Prepare;
        public string MappersFile { get; private set; }
        public string FixesFile { get; private set; }
        public string SymbolsFile { get; private set; }
        public string NesAsm { get; private set; } = "nesasm";
        public string NesAsmArgs { get; private set; } = "";
        public string SourcesDir { get; private set; } = ".";
        public string? GamesFile { get; private set; } = null;
        public string? AsmFile { get; private set; } = null;
        public string OffsetsFile { get; private set; } = "offsets.json";
        public string? ReportFile { get; private set; } = null;
        public string? LoaderFile { get; private set; } = null;
        public string? UnifFile { get; private set; } = null;
        public string? Nes20File { get; private set; } = null;
        public string? BinFile { get; private set; } = null;
        public CombinerLanguage Language { get; private set; } = CombinerLanguage.English;
        public HashSet<int> BadSectors { get; private set; } = new();
        public bool NoSort { get; private set; } = false;
        public uint MaxRomSizeMB { get; private set; } = 256;
        public uint MaxChrRamSizeKB { get; private set; } = 256;

        private Config()
        {
            MappersFile = Path.Combine(Path.GetDirectoryName(AppContext.BaseDirectory) ?? String.Empty, DEFAULT_MAPPERS_FILE);
            if (!File.Exists(MappersFile) && !OperatingSystem.IsWindows())
                MappersFile = Path.Combine("/etc", DEFAULT_MAPPERS_FILE);
            FixesFile = Path.Combine(Path.GetDirectoryName(AppContext.BaseDirectory) ?? String.Empty, DEFAULT_FIXES_FILE);
            if (!File.Exists(FixesFile) && !OperatingSystem.IsWindows())
                FixesFile = Path.Combine("/etc", DEFAULT_FIXES_FILE);
            SymbolsFile
             = Path.Combine(Path.GetDirectoryName(AppContext.BaseDirectory) ?? String.Empty, DEFAULT_SYMBOLS_FILE);
            if (!File.Exists(SymbolsFile) && !OperatingSystem.IsWindows())
                SymbolsFile = Path.Combine("/etc", DEFAULT_SYMBOLS_FILE);
        }

        // TODO: Replace magic strings with constants
        public static Config? Parse(string[] args)
        {
            Config config = new Config();

            if (args.Length > 0)
            {
                string command = args[0].ToLower();
                switch (command)
                {
                    case commandPrepare:
                        config.Command = CombinerCommand.Prepare;
                        break;
                    case commandCombine:
                        config.Command = CombinerCommand.Combine;
                        break;
                    case commandBuild:
                        config.Command = CombinerCommand.Build;
                        break;
                    default:
                        if (!string.IsNullOrEmpty(command))
                            Console.WriteLine("Unknown command: " + command);
                        return null;
                }
            }
            else return null;
            for (int i = 1; i < args.Length; i++)
            {
                string param = args[i];
                while (param.StartsWith("-")) param = param.Substring(1);
                string value = i < args.Length - 1 ? args[i + 1] : "";
                switch (param.ToLower())
                {
                    case "mappers":
                        config.MappersFile = value;
                        i++;
                        break;
                    case "fixes":
                        config.FixesFile = value;
                        i++;
                        break;
                    case "symbols":
                        config.SymbolsFile = value;
                        i++;
                        break;
                    case "games":
                        config.GamesFile = value;
                        i++;
                        break;
                    case "asm":
                        config.AsmFile = value;
                        i++;
                        break;
                    case "offsets":
                        config.OffsetsFile = value;
                        i++;
                        break;
                    case "report":
                        config.ReportFile = value;
                        i++;
                        break;
                    case "loader":
                        config.LoaderFile = value;
                        i++;
                        break;
                    case "unif":
                        config.UnifFile = value;
                        i++;
                        break;
                    case "nes20":
                        config.Nes20File = value;
                        i++;
                        break;
                    case "bin":
                        config.BinFile = value;
                        i++;
                        break;
                    case "nosort":
                        config.NoSort = true;
                        break;
                    case "maxromsize":
                        config.MaxRomSizeMB = uint.Parse(value);
                        i++;
                        break;
                    case "maxchrsize":
                        config.MaxChrRamSizeKB = uint.Parse(value);
                        i++;
                        break;
                    case "language":
                        switch (value.ToLower())
                        {
                            case "eng":
                                config.Language = CombinerLanguage.English;
                                break;
                            case "rus":
                                config.Language = CombinerLanguage.Russian;
                                break;
                            default:
                                throw new InvalidDataException($"Invalid language: {value}");
                        }
                        i++;
                        break;
                    case "badsectors":
                        foreach (var v in value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                            config.BadSectors.Add(int.Parse(v));
                        i++;
                        break;
                    case "nesasm":
                        config.NesAsm = value;
                        i++;
                        break;
                    case "nesasm-args":
                    case "nesasmargs":
                        config.NesAsmArgs = value;
                        i++;
                        break;
                    case "sources":
                        config.SourcesDir = value;
                        i++;
                        break;
                    default:
                        Console.WriteLine("Unknown parameter: " + param);
                        return null;
                }
            }

            if ((config.GamesFile == null) && ((config.Command == CombinerCommand.Prepare) || (config.Command == CombinerCommand.Build)))
            {
                Console.WriteLine("Missing required parameter: --games");
                return null;
            }
            if ((config.AsmFile == null) && (config.Command == CombinerCommand.Prepare))
            {
                Console.WriteLine("Missing required parameter: --asm");
                return null;
            }
            if ((config.OffsetsFile == null) && ((config.Command == CombinerCommand.Prepare) || (config.Command == CombinerCommand.Combine)))
            {
                Console.WriteLine("Missing required parameter: --offsets");
                return null;
            }
            if ((config.LoaderFile == null) && (config.Command == CombinerCommand.Combine))
            {
                Console.WriteLine("Missing required parameter: --loader");
                return null;
            }
            if ((config.UnifFile == null) && (config.Nes20File == null) && (config.BinFile == null)
                && ((config.Command == CombinerCommand.Combine) || (config.Command == CombinerCommand.Build)))
            {
                Console.WriteLine("At least one parameter required: --unif, --nes20 or --bin");
                return null;
            }

            if (string.IsNullOrEmpty(config.AsmFile))
                config.AsmFile = Path.Combine(config.SourcesDir, "games.asm");

            return config;
        }

        public static void PrintHelp()
        {
            var exename = Path.GetFileName(Process.GetCurrentProcess()?.MainModule?.FileName);
            Console.WriteLine("--- Usage ---");
            Console.WriteLine("First step:");
            Console.WriteLine($" {exename} prepare --games <games.txt> --asm <games.asm> --offsets <offsets.json> [--report <report.txt>] [--nosort] [--maxromsize <size_mb>] [--maxchrsize <size_kb>] [--language <eng|rus>] [--badsectors <sectors>]");
            Console.WriteLine("  {0,-20}{1}", "--games", "- input plain text file with a list of ROM files");
            Console.WriteLine("  {0,-20}{1}", "--asm", "- output file for the loader");
            Console.WriteLine("  {0,-20}{1}", "--offsets", "- output file with offsets for every game");
            Console.WriteLine("  {0,-20}{1}", "--report", "- output report file (human readable)");
            Console.WriteLine("  {0,-20}{1}", "--nosort", "- disable automatic sort by name");
            Console.WriteLine("  {0,-20}{1}", "--maxromsize", "- maximum size for final file (in megabytes)");
            Console.WriteLine("  {0,-20}{1}", "--maxchrsize", "- maximum CHR RAM size (in kilobytes), default is 256");
            Console.WriteLine("  {0,-20}{1}", "--language", "- language for system messages: \"eng\" or \"rus\"");
            Console.WriteLine("  {0,-20}{1}", "--badsectors", "- comma-separated list of bad sectors,");
            Console.WriteLine("Second step:");
            Console.WriteLine($" {exename} combine --loader <menu.nes> --offsets <offsets.json> [--unif <multirom.unf>] [--nes20 multirom.nes] [--bin <multirom.bin>]");
            Console.WriteLine("  {0,-20}{1}", "--loader", "- loader (compiled using the asm file generated by the first step)");
            Console.WriteLine("  {0,-20}{1}", "--offsets", "- input file with offsets for every game (generated by the first step)");
            Console.WriteLine("  {0,-20}{1}", "--unif", "- output UNIF file");
            Console.WriteLine("  {0,-20}{1}", "--nes20", "- output NES 2.0 file");
            Console.WriteLine("  {0,-20}{1}", "--bin", "- output raw binary file");
            Console.WriteLine("All at once:");
            Console.WriteLine($" {exename} build --games <games.txt> --asm <games.asm> [--nesasm <nesasm>] [--nesasm-args <args>] [--sources <path>] [--report <report.txt>] [--nosort] [--maxromsize <size_mb>] [--maxchrsize <size_kb>] [--language <language>] [--badsectors <sectors>] [--unif <multirom.unf>] [--nes20 <multirom.nes>] [--bin <multirom.bin>]");
            Console.WriteLine("  {0,-20}{1}", "--games", "- input plain text file with list of ROM files");
            Console.WriteLine("  {0,-20}{1}", "--asm", "- output file for the loader");
            Console.WriteLine("  {0,-20}{1}", "--nesasm", "- path to the nesasm compiler executable");
            Console.WriteLine("  {0,-20}{1}", "--nesasm-args", "- additional command-line arguments for nesasm");
            Console.WriteLine("  {0,-20}{1}", "--sources", "- directory with loader source files, default is current directory");
            Console.WriteLine("  {0,-20}{1}", "--report", "- output report file (human readable)");
            Console.WriteLine("  {0,-20}{1}", "--nosort", "- disable automatic sort by name");
            Console.WriteLine("  {0,-20}{1}", "--maxromsize", "- maximum size for final file (in megabytes)");
            Console.WriteLine("  {0,-20}{1}", "--maxchrsize", "- maximum CHR RAM size (in kilobytes), default is 256");
            Console.WriteLine("  {0,-20}{1}", "--language", "- language for system messages: \"eng\" or \"rus\", default is \"eng\"");
            Console.WriteLine("  {0,-20}{1}", "--badsectors", "- comma-separated list of bad sectors,");
            Console.WriteLine("  {0,-20}{1}", "--unif", "- output UNIF file");
            Console.WriteLine("  {0,-20}{1}", "--nes20", "- output NES 2.0 file");
            Console.WriteLine("  {0,-20}{1}", "--bin", "- output raw binary file");
        }
    }
}