1+ program KTXRunner;
2+
3+ { $title KTXRunner}
4+
5+ uses KTX;
6+ uses System;
7+ uses System.Collections.Generic ;
8+ uses System.Reflection;
9+
10+ function ToEnum <T>(self: string; ignorecase: boolean := true): T; extensionmethod; where T: System.Enum;
11+ begin
12+ Result := T(Enum.Parse(typeof(T), self, ignorecase));
13+ end ;
14+
15+ function ToArgs (self: string); extensionmethod := new string[](' /' + self, ' -' + self);
16+
17+ type
18+ KTXRunnerContext = class ;
19+
20+ KTXArgumentAttribute = class (Attribute )
21+ public auto property ArgumentKey: string;
22+ public auto property Description: string;
23+ public auto property &Default: string;
24+
25+ public constructor (k, d, df: string) := (ArgumentKey, Description, &Default) := (k, d, df);
26+ end ;
27+
28+ KTXRunnerContext = class
29+ public auto property Path: string;
30+ public auto property IsOverlay: boolean;
31+ public auto property DrawingType: KTX.DrawingType;
32+ public auto property RGBToColorConvertType: KTX.RGBToColorConvertType;
33+ public auto property DrawingAlignmentType: KTX.DrawingAlignmentType;
34+
35+ public constructor (path: string) := self.Path := path;
36+
37+ public procedure Collect (context: KTXRunnerContext); virtual := exit;
38+
39+ public procedure CollectArguments (args: IEnumerable<KeyValuePair<string, string>>);
40+ begin
41+ foreach var t in ArgTypes do
42+ begin
43+ var arg := t.Item2.Default;
44+ foreach var pair in args do
45+ if t.Item2.ArgumentKey.ToArgs().Contains(pair.Key) then arg := pair.Value ;
46+ KTXRunnerContext(t.Item1.GetConstructors().Last().Invoke(new object [1 ](arg))).Collect(self);
47+ end ;
48+ end ;
49+
50+ public procedure Run () := Path.Draw(RGBToColorConvertType, DrawingType, DrawingAlignmentType, IsOverlay);
51+
52+ public static auto property ArgTypes: List<(&Type , KTXArgumentAttribute)>;
53+ static constructor ();
54+ begin
55+ ArgTypes := Assembly.GetExecutingAssembly().GetTypes()
56+ .Where(t -> t.GetCustomAttributes().Any(x -> x.GetType() = typeof(KTXArgumentAttribute)))
57+ .Select(x -> (x, x.GetCustomAttribute(typeof(KTXArgumentAttribute)) as KTXArgumentAttribute)).ToList();
58+ end ;
59+ end ;
60+
61+ [KTXArgumentAttribute(' o' , ' overlaying image (boolean)' , ' false' )]
62+ IsOverlayArg = class (KTXRunnerContext)
63+ public constructor (s: string) := IsOverlay := boolean.Parse(s);
64+ public procedure Collect (context: KTXRunnerContext); override := context.IsOverlay := IsOverlay;
65+ end ;
66+
67+ [KTXArgumentAttribute(' d' , ' drawing type (aline/hex)' , ' aline' )]
68+ DrawingTypeArg = class (KTXRunnerContext)
69+ public constructor (s: string) := DrawingType := s.ToEnum&<KTX.DrawingType>();
70+ public procedure Collect (context: KTXRunnerContext); override := context.DrawingType := DrawingType;
71+ end ;
72+
73+ [KTXArgumentAttribute(' c' , ' convert type (master, esthetic1024, v20, new)' , ' master' )]
74+ RGBToColorConvertTypeArg = class (KTXRunnerContext)
75+ public constructor (s: string) := RGBToColorConvertType := s.ToEnum&<KTX.RGBToColorConvertType>();
76+ public procedure Collect (context: KTXRunnerContext); override := context.RGBToColorConvertType := RGBToColorConvertType;
77+ end ;
78+
79+ [KTXArgumentAttribute(' a' , ' alignment (center, up, down, left, right, leftdown, rightup...)' , ' center' )]
80+ DrawingAlignmentTypeArg = class (KTXRunnerContext)
81+ public constructor (s: string) := DrawingAlignmentType := s.ToEnum&<KTX.DrawingAlignmentType>();
82+ public procedure Collect (context: KTXRunnerContext); override := context.DrawingAlignmentType := DrawingAlignmentType;
83+ end ;
84+
85+ begin
86+ var args := System.Environment.GetCommandLineArgs();
87+ if (args.Length < 2 ) or args.Contains(' [RUNMODE]' ) then
88+ begin
89+ Console.WriteLine(' This is console application! Run it with cmd!' +NewLine);
90+ Console.WriteLine(' First argument - path to png file' +NewLine+' Other keys:' );
91+ foreach var arg in KTXRunnerContext.ArgTypes.Select(x -> x.Item2) do
92+ Console.WriteLine($' /{arg.ArgumentKey} --- {arg.Description}' );
93+ Console.ReadLine();
94+ exit;
95+ end ;
96+
97+ var runner := new KTXRunnerContext(args[1 ]);
98+ runner.CollectArguments(args.Skip(2 ).Batch(2 , en -> new KeyValuePair<string, string>(en.First(), en.Skip(1 ).First())));
99+ runner.Run();
100+
101+ Console.CursorVisible := false;
102+ Console.ReadLine();
103+ end .
0 commit comments