@@ -10,14 +10,20 @@ const ConverterErrors = error{
10
10
WeirdGameDir ,
11
11
};
12
12
13
+ const Settings = struct {
14
+ input_folder_path : []const u8 = "" ,
15
+ output_folder_path : []const u8 = "" ,
16
+ game_executable_path : []const u8 = "" ,
17
+ };
18
+
13
19
pub fn main () ! void {
14
20
zglfw .init () catch {
15
21
std .log .err ("Failed to initialize GLFW library." , .{});
16
22
return ;
17
23
};
18
24
defer zglfw .terminate ();
19
25
20
- const window = zglfw .Window .create (1000 , 300 , "Legacy Mod Converter 1.0 for Pre-Release 5.2" , null ) catch {
26
+ const window = zglfw .Window .create (1600 , 300 , "Legacy Mod Converter 1.0 for Pre-Release 5.2" , null ) catch {
21
27
std .log .err ("Failed to create window." , .{});
22
28
return ;
23
29
};
@@ -45,6 +51,28 @@ pub fn main() !void {
45
51
);
46
52
defer zgui .backend .deinit ();
47
53
54
+ const settings_text = std .fs .cwd ().readFileAlloc (gpa , "settings.json" , 1337 ) catch | err | switch (err ) {
55
+ error .FileNotFound = > try gpa .dupe (u8 , "{}" ),
56
+ else = > return err ,
57
+ };
58
+ defer gpa .free (settings_text );
59
+
60
+ const settings_parsed = try std .json .parseFromSlice (Settings , gpa , settings_text , .{});
61
+ defer settings_parsed .deinit ();
62
+ var settings = settings_parsed .value ;
63
+
64
+ var input_folder_path_mut : [std .fs .MAX_PATH_BYTES + 1 :0 ]u8 = undefined ;
65
+ @memcpy (input_folder_path_mut [0.. settings .input_folder_path .len ], settings .input_folder_path );
66
+ input_folder_path_mut [settings .input_folder_path .len ] = 0 ;
67
+
68
+ var output_folder_path_mut : [std .fs .MAX_PATH_BYTES + 1 :0 ]u8 = undefined ;
69
+ @memcpy (output_folder_path_mut [0.. settings .output_folder_path .len ], settings .output_folder_path );
70
+ output_folder_path_mut [settings .output_folder_path .len ] = 0 ;
71
+
72
+ var game_executable_path_mut : [std .fs .MAX_PATH_BYTES + 1 :0 ]u8 = undefined ;
73
+ @memcpy (game_executable_path_mut [0.. settings .game_executable_path .len ], settings .game_executable_path );
74
+ game_executable_path_mut [settings .game_executable_path .len ] = 0 ;
75
+
48
76
while (! window .shouldClose ()) {
49
77
zglfw .pollEvents ();
50
78
@@ -55,45 +83,35 @@ pub fn main() !void {
55
83
56
84
// Set the starting window position and size to custom values
57
85
zgui .setNextWindowPos (.{ .x = 0.0 , .y = 0.0 , .cond = .always });
58
- // zgui.setNextWindowSize(.{ .w = -1.0, .h = -1.0, .cond = .always });
59
-
60
- if (zgui .begin ("a" , .{ .flags = .{ .no_title_bar = true , .no_resize = true , .no_background = true } })) {
61
- const static = struct {
62
- var input_mod_path_buf : [std .fs .MAX_PATH_BYTES + 1 ]u8 = undefined ;
63
- var output_mod_path_buf : [std .fs .MAX_PATH_BYTES + 1 ]u8 = undefined ;
64
- var progress : f32 = 0.0 ;
65
- };
66
- // std.debug.print("xd: {d}\n", .{"I:/Programming/Cortex-Command-Community-Project-Source".len});
67
- // zgui.pushItemWidth("I:/Programming/Cortex-Command-Community-Project-Source".len);
68
- // zgui.setNextItemWidth("I:/Programming/Cortex-Command-Community-Project-Source".len);
69
- if (zgui .inputTextWithHint ("Input/ directory path" , .{ .hint = "Copy-paste a path from File Explorer here" , .buf = static .input_mod_path_buf [0.. ] })) {
70
- std .debug .print ("The user edited input_mod_path_buf\n " , .{});
86
+ zgui .setNextWindowSize (.{ .w = -1.0 , .h = -1.0 , .cond = .always });
87
+ // zgui.setNextWindowSize(.{ .w = 420, .h = 300, .cond = .always });
88
+
89
+ if (zgui .begin ("invisible_title" , .{ .flags = .{ .no_title_bar = true , .no_resize = true , .no_background = true } })) {
90
+ zgui .setNextItemWidth (@max (zgui .calcTextSize (settings .input_folder_path , .{})[0 ], 585 ));
91
+ if (zgui .inputTextWithHint ("Input/ folder path" , .{ .hint = "Copy-paste a path from File Explorer here" , .buf = & input_folder_path_mut })) {
92
+ settings .input_folder_path = std .mem .span (@as ([* :0 ]u8 , & input_folder_path_mut ));
93
+ std .debug .print ("settings.input_folder_path is now '{s}'\n " , .{settings .input_folder_path });
94
+ try writeSettings (settings );
71
95
}
72
- // zgui.popItemWidth();
73
- if (zgui .inputTextWithHint ("Mods/ directory path" , .{ .hint = "Copy-paste a path from File Explorer here" , .buf = static .output_mod_path_buf [0.. ] })) {
74
- std .debug .print ("The user edited output_mod_path_buf\n " , .{});
96
+
97
+ zgui .setNextItemWidth (@max (zgui .calcTextSize (settings .output_folder_path , .{})[0 ], 585 ));
98
+ if (zgui .inputTextWithHint ("Mods/ folder path" , .{ .hint = "Copy-paste a path from File Explorer here" , .buf = & output_folder_path_mut })) {
99
+ settings .output_folder_path = std .mem .span (@as ([* :0 ]u8 , & output_folder_path_mut ));
100
+ std .debug .print ("settings.output_folder_path is now '{s}'\n " , .{settings .output_folder_path });
101
+ try writeSettings (settings );
75
102
}
103
+
76
104
if (zgui .button ("Convert" , .{ .w = 200.0 })) {
77
105
std .debug .print ("Converting...\n " , .{});
78
106
79
107
var arena = std .heap .ArenaAllocator .init (std .heap .page_allocator );
80
108
defer arena .deinit ();
81
109
var allocator = arena .allocator ();
82
110
83
- const cwd = std .fs .cwd ();
84
-
85
- var input_mod_path_buffer : [std .fs .MAX_PATH_BYTES ]u8 = undefined ;
86
- // const input_mod_path = try cwd.realpath("I:/Programming/Cortex-Command-Mod-Converter-Engine/tests/mod/in", &input_mod_path_buffer);
87
- const input_mod_path = try cwd .realpath ("I:/Programming/Cortex-Command-Community-Project-Data/LegacyModConverter-v1.0-pre5.2/Input" , & input_mod_path_buffer );
88
-
89
- var output_mod_path_buffer : [std .fs .MAX_PATH_BYTES ]u8 = undefined ;
90
- // const output_mod_path = try cwd.realpath("I:/Programming/Cortex-Command-Mod-Converter-Engine/tests/mod/out", &output_mod_path_buffer);
91
- const output_mod_path = try cwd .realpath ("I:/Programming/Cortex-Command-Community-Project-Data/Mods" , & output_mod_path_buffer );
92
-
93
111
var diagnostics : converter.Diagnostics = .{};
94
112
converter .convert (
95
- input_mod_path ,
96
- output_mod_path ,
113
+ settings . input_folder_path ,
114
+ settings . output_folder_path ,
97
115
allocator ,
98
116
& diagnostics ,
99
117
) catch | err | switch (err ) {
@@ -124,12 +142,31 @@ pub fn main() !void {
124
142
else = > | e | return e ,
125
143
};
126
144
145
+ // TODO: Run .convert() in a separate thread, letting it update a passed Progress struct so we can update a progress bar here?
146
+ // TODO: Check if std/Progress.zig is of use: https://ziglang.org/documentation/master/std/src/std/Progress.zig.html
147
+ // TODO: Look at this example of multithreading in Zig: https://gist.github.com/cabarger/d3879745b8477670070f826cad2f027d
148
+ // var progress: f32 = 0.0;
149
+ // _ = progress;
150
+ // zgui.pushStyleColor4f(.{ .idx = .plot_histogram, .c = .{ 0.1 + 0.5 * (1 - progress), 0.2 + 0.7 * progress, 0.3, 1.0 } });
151
+ // zgui.progressBar(.{ .fraction = progress, .overlay = "" });
152
+ // zgui.popStyleColor(.{});
153
+ // progress += 0.01;
154
+ // if (progress > 2.0) progress = 0.0;
155
+
127
156
std .debug .print ("Done converting!\n " , .{});
128
157
}
158
+
159
+ zgui .setNextItemWidth (@max (zgui .calcTextSize (settings .game_executable_path , .{})[0 ], 585 ));
160
+ if (zgui .inputTextWithHint ("Game .exe path" , .{ .hint = "Copy-paste a path from File Explorer here" , .buf = & game_executable_path_mut })) {
161
+ settings .game_executable_path = std .mem .span (@as ([* :0 ]u8 , & game_executable_path_mut ));
162
+ std .debug .print ("settings.game_executable_path is now '{s}'\n " , .{settings .game_executable_path });
163
+ try writeSettings (settings );
164
+ }
165
+
129
166
if (zgui .button ("Launch" , .{ .w = 200.0 })) {
130
- const path = "I:/Programming/Cortex-Command-Community-Project-Data/Cortex Command.debug.release.exe" ;
131
- try std .os .chdir (std .fs .path .dirname (path ) orelse return ConverterErrors .WeirdGameDir );
132
- var argv = [_ ][]const u8 {path };
167
+ // TODO: Handle settings.game_executable_path not being set yet
168
+ try std .os .chdir (std .fs .path .dirname (settings . game_executable_path ) orelse return ConverterErrors .WeirdGameDir );
169
+ var argv = [_ ][]const u8 {settings . game_executable_path };
133
170
const result = try std .ChildProcess .exec (.{ .argv = & argv , .allocator = gpa });
134
171
_ = result ;
135
172
}
@@ -138,27 +175,10 @@ pub fn main() !void {
138
175
defer arena .deinit ();
139
176
var allocator = arena .allocator ();
140
177
141
- // TODO: Why am I using cwd.realpath()?
142
-
143
- const cwd = std .fs .cwd ();
144
-
145
- var input_mod_path_buffer : [std .fs .MAX_PATH_BYTES ]u8 = undefined ;
146
- const input_mod_path = try cwd .realpath ("I:/Programming/Cortex-Command-Community-Project-Data/LegacyModConverter-v1.0-pre5.2/Input" , & input_mod_path_buffer );
147
-
148
- var output_mod_path_buffer : [std .fs .MAX_PATH_BYTES ]u8 = undefined ;
149
- const output_mod_path = try cwd .realpath ("I:/Programming/Cortex-Command-Community-Project-Data/Mods" , & output_mod_path_buffer );
150
-
151
- try converter .zip_mods (input_mod_path , output_mod_path , allocator );
178
+ try converter .zip_mods (settings .input_folder_path , settings .output_folder_path , allocator );
152
179
153
180
std .debug .print ("Done zipping!\n " , .{});
154
181
}
155
-
156
- zgui .pushStyleColor4f (.{ .idx = .plot_histogram , .c = .{ 0.1 + 0.5 * (1 - static .progress ), 0.2 + 0.7 * static .progress , 0.3 , 1.0 } });
157
- zgui .progressBar (.{ .fraction = static .progress , .overlay = "" });
158
- zgui .popStyleColor (.{});
159
-
160
- static .progress += 0.03 ;
161
- if (static .progress > 2.0 ) static .progress = 0.0 ;
162
182
}
163
183
zgui .end ();
164
184
@@ -184,3 +204,13 @@ pub fn main() !void {
184
204
_ = gctx .present ();
185
205
}
186
206
}
207
+
208
+ fn writeSettings (settings : Settings ) ! void {
209
+ const output_file = try std .fs .cwd ().createFile ("settings.json" , .{});
210
+ defer output_file .close ();
211
+
212
+ var buffered = std .io .bufferedWriter (output_file .writer ());
213
+ const buffered_writer = buffered .writer ();
214
+ try std .json .stringify (settings , .{}, buffered_writer );
215
+ try buffered .flush ();
216
+ }
0 commit comments