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
|
// Configuration parser
// Copyright (C) 2021-2022 Nguyễn Gia Phong
//
// This file is part of Black Shades.
//
// Black Shades is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Black Shades is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Black Shades. If not, see <https://www.gnu.org/licenses/>.
const Dir = std.fs.Dir;
const File = std.fs.File;
const IntegerBitSet = std.bit_set.IntegerBitSet;
const allocator = std.heap.c_allocator;
const cwd = std.fs.cwd;
const endian = @import("builtin").target.cpu.arch.endian();
const eql = std.mem.eql;
const join = std.fs.path.join;
const maxInt = std.math.maxInt;
const mkdir = std.os.mkdir;
const parseFloat = std.fmt.parseFloat;
const parseInt = std.fmt.parseInt;
const std = @import("std");
const stringToEnum = std.meta.stringToEnum;
const tokenize = std.mem.tokenize;
const Key = @import("gfz").Key;
const ini = @import("ini");
const data_dir = @import("build_options").data_dir;
const c = @import("cimport.zig");
const parseBool = @import("misc.zig").parseBool;
const default_levels_len = 13;
/// Open the given file for reading,
/// on error copy it from the game's data and try again.
fn openData(dir: Dir, filename: []const u8) !File {
return dir.openFile(filename, .{}) catch {
var data = try cwd().makeOpenPath(data_dir, .{});
defer data.close();
try data.copyFile(filename, dir, filename, .{});
return try dir.openFile(filename, .{});
};
}
const Level = extern struct {
environment: c_int,
evil_weapons: u8,
evil_rarity: u8,
guard_weapon: u8,
guard_reloads: u8,
time: c_int,
difficulty: f32,
};
fn parseLevels(dir_path: []const u8, length: usize) ![*]Level {
var dir = try cwd().makeOpenPath(dir_path, .{});
defer dir.close();
const input = try openData(dir, "levels.ini");
defer input.close();
const levels = try allocator.alloc(Level, length);
errdefer allocator.free(levels);
var parser = ini.parse(allocator, input.reader());
defer parser.deinit();
var i: usize = maxInt(usize);
while (try parser.next()) |record|
switch (record) {
.section => i +%= 1,
.property => |kv| if (eql(u8, kv.key, "environment")) {
levels[i].environment = if (eql(u8, kv.value, "sunny"))
c.sunny_environment
else if (eql(u8, kv.value, "foggy"))
c.foggy_environment
else if (eql(u8, kv.value, "snowy"))
c.snowy_environment
else if (eql(u8, kv.value, "rainy"))
c.rainy_environment
else if (eql(u8, kv.value, "firey"))
c.firey_environment
else if (eql(u8, kv.value, "night"))
c.night_environment
else return error.InvalidData;
} else if (eql(u8, kv.key, "evil weapons")) {
var weapons = IntegerBitSet(8).initEmpty();
var enums = tokenize(u8, kv.value, " ");
while (enums.next()) |weapon|
weapons.set(try parseInt(u3, weapon, 10));
levels[i].evil_weapons = weapons.mask;
} else if (eql(u8, kv.key, "evil rarity")) {
levels[i].evil_rarity = try parseInt(u8, kv.value, 10);
} else if (eql(u8, kv.key, "guard weapon")) {
levels[i].guard_weapon = try parseInt(u3, kv.value, 10);
} else if (eql(u8, kv.key, "guard reloads")) {
levels[i].guard_reloads = try parseInt(u8, kv.value, 10);
} else if (eql(u8, kv.key, "time")) {
levels[i].time = try parseInt(c_int, kv.value, 10);
} else if (eql(u8, kv.key, "difficulty")) {
levels[i].difficulty = try parseFloat(f32, kv.value);
} else return error.InvalidData,
else => return error.InvalidData,
};
return levels.ptr;
}
/// Game configuration.
pub const Config = extern struct {
width: c_int = 800,
height: c_int = 600,
vsync: bool = true,
blood: bool = true,
music: bool = true,
mouse_sensitivity: f32 = 1.0,
key: extern struct {
forwards: Key = .W,
backwards: Key = .S,
left: Key = .A,
right: Key = .D,
crouch: Key = .LEFT_CONTROL,
accelerate: Key = .LEFT_SHIFT,
dive: Key = .SPACE,
reload: Key = .R,
aim: Key = .E,
psychic_aim: Key = .Q,
psychic: Key = .Z,
switch_view: Key = .TAB,
switch_weapon: Key = .X,
skip: Key = .K,
pause: Key = .P,
slomo: Key = .B,
force: Key = .F,
} = .{},
levels: extern struct { ptr: [*]Level = undefined, len: usize = 0 } = .{},
debug: bool = false,
};
/// Parse config.ini in the given base directory.
pub fn parse(base_dir: []const u8) !Config {
const config_dir = try join(allocator, &.{ base_dir, "blackshades" });
defer allocator.free(config_dir);
var dir = try cwd().makeOpenPath(config_dir, .{});
defer dir.close();
const input = try openData(dir, "config.ini");
defer input.close();
var parser = ini.parse(allocator, input.reader());
defer parser.deinit();
var config = Config{};
var section: []u8 = "";
defer allocator.free(section);
while (try parser.next()) |record|
switch (record) {
.section => |heading| {
allocator.free(section);
section = try allocator.dupe(u8, heading);
},
.property => |kv| if (eql(u8, section, "graphics")) {
if (eql(u8, kv.key, "width"))
config.width = try parseInt(c_int, kv.value, 10)
else if (eql(u8, kv.key, "height"))
config.height = try parseInt(c_int, kv.value, 10)
else if (eql(u8, kv.key, "vsync"))
config.vsync = try parseBool(kv.value)
else if (eql(u8, kv.key, "blood"))
config.blood = try parseBool(kv.value)
else return error.InvalidData;
} else if (eql(u8, section, "audio")) {
if (eql(u8, kv.key, "music"))
config.music = try parseBool(kv.value)
else return error.InvalidData;
} else if (eql(u8, section, "input")) {
if (eql(u8, kv.key, "mouse sensitivity"))
config.mouse_sensitivity = try parseFloat(f32, kv.value)
else if (eql(u8, kv.key, "forwards key"))
config.key.forwards = stringToEnum(Key, kv.value).?
else if (eql(u8, kv.key, "backwards key"))
config.key.backwards = stringToEnum(Key, kv.value).?
else if (eql(u8, kv.key, "left key"))
config.key.left = stringToEnum(Key, kv.value).?
else if (eql(u8, kv.key, "right key"))
config.key.right = stringToEnum(Key, kv.value).?
else if (eql(u8, kv.key, "crouch key"))
config.key.crouch = stringToEnum(Key, kv.value).?
else if (eql(u8, kv.key, "accelerate key"))
config.key.accelerate = stringToEnum(Key, kv.value).?
else if (eql(u8, kv.key, "dive key"))
config.key.dive = stringToEnum(Key, kv.value).?
else if (eql(u8, kv.key, "reload key"))
config.key.reload = stringToEnum(Key, kv.value).?
else if (eql(u8, kv.key, "aim key"))
config.key.aim = stringToEnum(Key, kv.value).?
else if (eql(u8, kv.key, "psychic aim key"))
config.key.psychic_aim = stringToEnum(Key, kv.value).?
else if (eql(u8, kv.key, "psychic key"))
config.key.psychic = stringToEnum(Key, kv.value).?
else if (eql(u8, kv.key, "switch view key"))
config.key.switch_view = stringToEnum(Key, kv.value).?
else if (eql(u8, kv.key, "switch weapon key"))
config.key.switch_weapon = stringToEnum(Key, kv.value).?
else if (eql(u8, kv.key, "skip key"))
config.key.skip = stringToEnum(Key, kv.value).?
else if (eql(u8, kv.key, "pause key"))
config.key.pause = stringToEnum(Key, kv.value).?
else if (eql(u8, kv.key, "slomo key"))
config.key.slomo = stringToEnum(Key, kv.value).?
else if (eql(u8, kv.key, "force key"))
config.key.force = stringToEnum(Key, kv.value).?
else return error.InvalidData;
} else if (eql(u8, section, "misc")) {
if (eql(u8, kv.key, "custom levels"))
config.levels.len = try parseInt(usize, kv.value, 10)
else if (eql(u8, kv.key, "debug"))
config.debug = try parseBool(kv.value)
else return error.InvalidData;
} else return error.InvalidData,
else => return error.InvalidData,
};
if (config.levels.len > 0) {
config.levels.ptr = try parseLevels(config_dir, config.levels.len);
} else {
config.levels.len = default_levels_len;
config.levels.ptr = try parseLevels(data_dir, config.levels.len);
}
return config;
}
|