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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#![deny(trivial_casts, trivial_numeric_casts,
        unsafe_code, unstable_features,
        unused_import_braces, unused_qualifications)]

/// This crate provides a native and easy way to read and write
/// *Grand Theft Auto: Vice City* settings file (gta_vc.set).
///
/// # Usage
/// This crate is on crates.io and can be used by adding gta_vc_settings to
/// your dependencies in your project's Cargo.toml.
///
/// ```toml
/// [dependencies]
/// gta_vc_settings = "0.1"
/// ```
///
/// and this to your crate root:
///
/// ```
/// extern crate gta_vc_settings;
/// ```
///
/// # Example: read a settings file.
/// ```
/// use gta_vc_settings::GtaVcSettings;
///
/// let mut settings = GtaVcSettings::new();
/// settings.read("./tests/gta_vc.set");
/// ```

/**
 *    Copyright (C) 2016 Jean Piere Dudey
 *
 *    This program 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.
 *
 *    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

extern crate byteorder;

use byteorder::{ReadBytesExt, WriteBytesExt, LittleEndian};
use std::fs::File;
use std::io::{Read, Write, Seek, SeekFrom};
use std::path::Path;
use std::str;
use std::io;

/// Enum representing result returned by `GtaVcSettings` functions.
#[derive(Debug)]
pub enum SettingsResult {
    /// Returned when the operation was completed succesfully.
    Ok,

    /// Returned when an I/O operation was errored.
    IoError(io::Error),

    /// Returned if the file is not valid yet.
    NotValidYet
}

/// Struct representing a settings file.
pub struct GtaVcSettings {
    /// Defaults to 3
    pub total_sections: u32,

    /// Primary controls, Secondary controls, Tertiary controls and Quaternary
    /// controls. 
    pub controls: [u8; 352],

    /// Some strings generated by the game.
    pub strings: [u8; 51],

    /// Unknown byte.
    pub unknown: u8,

    /// Mouse sensitivity.
    pub mouse_sensitivity: u32,

    /// Invert mouse vertically.
    /// (0 = on, 1 = off)
    pub mouse_invert_y: u8,

    /// Mouse controlled steering.
    /// (0 = on, 1 = off)
    pub steer_with_mouse: u8,

    /// SFX volume.
    /// (1 = lowest setting, 65 = highest setting, 49 = default setting)
    pub sfx_volume: u8,

    /// Music volume.
    /// (1 = lowest setting, 65 = highest setting, 49 = default setting)
    pub music_volume: u8,

    /// MP3 volume boost.
    /// (1 = lowest setting, 65 = highest setting, 0 = default setting)
    pub mp3_volume_boost: u8,

    /// Radio station.
    /// See http://www.gtamodding.com/wiki/List_of_radio_stations_(VC) for
    /// more information.
    pub current_radio: u8,

    /// Speakers configuration.
    /// (0 = 2 speakers, 1 = headphones, 2 = more than 2 speakers)
    pub speakers_configuration: u8,

    /// Audio hardware.
    pub audio_hardware: u8,

    /// Dynamic acoustic modeling.
    /// (0 = off, 1 = on)
    pub dynamic_acoustic_mode: u8,

    /// Brightness.
    /// (6 = lowest setting, 384 = highest setting, 256 = default)
    pub brightness: u16,

    /// Draw distance.
    /// (0.925 = lowest setting, 1.80 = highest setting, 1.20 = default setting)
    pub draw_distance: f32,

    /// Subtitles.
    /// (0 = off, 1 = on)
    pub subtitles: u8,

    /// Wide screen.
    /// (0 = off, 1 = on)
    pub widescreen: u8,

    /// Frame limiter.
    /// (0 = off, 1 = on)
    pub frame_limiter: u8,

    /// Screen resolution.
    pub video_mode: u8,

    /// Skin name without file extension with null terminator
    /// (default skin = $$""\0)
    pub skin_path: [u8; 256],

    /// Controls.
    /// (0 = standard, 1 = classic)
    pub standard_controls: u8,

    /// Language.
    /// (0 = English, 1 = French, 2 = German, 3 = Italian, 4 = Spanish)
    pub current_language: u8,

    /// HUD mode.
    /// (0 = off, 1 = on)
    pub hud_mode: u8,

    /// Radar mode.
    /// (0 = map & blips, 1 = blips only, 2 = off)
    pub radar_mode: u8,

    /// Map legend.
    pub map_legend: u8,
}

const NOT_VALID_STR: &'static str = "THIS FILE IS NOT VALID YET";

impl GtaVcSettings {
    /// Creates a GtaVcSettings with default data.
    ///
    /// # Examples
    /// ```
    /// use gta_vc_settings::GtaVcSettings;
    ///
    /// let settings = GtaVcSettings::new();
    /// assert_eq!(settings.total_sections, 3);
    /// ```
    pub fn new() -> GtaVcSettings {
        // TODO: Add more default values.
        GtaVcSettings {
            total_sections: 3,
            controls: [0; 352],
            strings: [0; 51],
            unknown: 0,
            mouse_sensitivity: 0,
            mouse_invert_y: 1,
            steer_with_mouse: 1,
            sfx_volume: 49,
            music_volume: 49,
            mp3_volume_boost: 0,
            current_radio: 0,
            speakers_configuration: 0,
            audio_hardware: 0,
            dynamic_acoustic_mode: 0,
            brightness: 256,
            draw_distance: 1.20,
            subtitles: 0,
            widescreen: 0,
            frame_limiter: 0,
            video_mode: 0,
            skin_path: [0; 256],
            standard_controls: 0,
            current_language: 0,
            hud_mode: 0,
            radar_mode: 0,
            map_legend: 0
        }
    }

    /// Opens and reads a GTA: Vice City settings file and returns a
    /// GtaVcSettings with it's data.
    ///
    /// # Examples
    /// ```
    /// use gta_vc_settings::GtaVcSettings;
    ///
    /// let mut settings = GtaVcSettings::new();
    /// settings.read("./tests/gta_vc.set");
    ///
    /// assert_eq!(settings.total_sections, 3);
    /// ```
    pub fn read<P: AsRef<Path>>(&mut self, path: P) -> SettingsResult {
        let mut file = match File::open(path) {
            Ok(mut file) => {
                let mut buffer = [0; 29];
                file.read(&mut buffer).unwrap();

                let bufstr = str::from_utf8(&buffer);
                let is_not_valid = bufstr == Ok(NOT_VALID_STR);

                if is_not_valid {
                    return SettingsResult::NotValidYet
                }

                file
            },
            Err(err) => return SettingsResult::IoError(err)
        };

        file.seek(SeekFrom::Start(0)).unwrap();
        self.total_sections = file.read_u32::<LittleEndian>().unwrap();

        if self.total_sections >= 3 {
            file.read(&mut self.controls).unwrap();
            file.read(&mut self.strings).unwrap();

            self.unknown = file.read_u8().unwrap();
            self.mouse_sensitivity = file.read_u32::<LittleEndian>().unwrap();
            self.mouse_invert_y = file.read_u8().unwrap();
            self.steer_with_mouse = file.read_u8().unwrap();
            self.sfx_volume = file.read_u8().unwrap();
            self.music_volume = file.read_u8().unwrap();
            self.mp3_volume_boost = file.read_u8().unwrap();
            self.current_radio = file.read_u8().unwrap();
            self.speakers_configuration = file.read_u8().unwrap();
            self.audio_hardware = file.read_u8().unwrap();
            self.dynamic_acoustic_mode = file.read_u8().unwrap();
            self.brightness = file.read_u16::<LittleEndian>().unwrap();
            self.draw_distance = file.read_f32::<LittleEndian>().unwrap();
            self.subtitles = file.read_u8().unwrap();
            self.widescreen = file.read_u8().unwrap();
            self.frame_limiter = file.read_u8().unwrap();
            self.video_mode = file.read_u8().unwrap();

            file.read(&mut self.skin_path).unwrap();

            self.standard_controls = file.read_u8().unwrap();
            self.current_language = file.read_u8().unwrap();
            self.hud_mode = file.read_u8().unwrap();
            self.radar_mode = file.read_u8().unwrap();
            self.map_legend = file.read_u8().unwrap();
        }

        SettingsResult::Ok
    }

    /// Writes a GTA: Vice City settings file to the filesystem.
    ///
    /// # Examples
    /// ```
    /// use gta_vc_settings::GtaVcSettings;
    ///
    /// let settings = GtaVcSettings::new();
    ///
    /// settings.write("./ouput.set");
    /// ```
    pub fn write<P: AsRef<Path>>(&self, path: P) -> SettingsResult {
        let mut file = match File::create(path) {
            Ok(f) => f,
            Err(err) => return SettingsResult::IoError(err)
        };

        file.write_u32::<LittleEndian>(self.total_sections).unwrap();

        file.write(&self.controls).unwrap();
        file.write(&self.strings).unwrap();

        file.write_u8(self.unknown).unwrap();
        file.write_u32::<LittleEndian>(self.mouse_sensitivity).unwrap();
        file.write_u8(self.mouse_invert_y).unwrap();
        file.write_u8(self.steer_with_mouse).unwrap();
        file.write_u8(self.sfx_volume).unwrap();
        file.write_u8(self.music_volume).unwrap();
        file.write_u8(self.mp3_volume_boost).unwrap();
        file.write_u8(self.current_radio).unwrap();
        file.write_u8(self.speakers_configuration).unwrap();
        file.write_u8(self.audio_hardware).unwrap();
        file.write_u8(self.dynamic_acoustic_mode).unwrap();
        file.write_u16::<LittleEndian>(self.brightness).unwrap();
        file.write_f32::<LittleEndian>(self.draw_distance).unwrap();
        file.write_u8(self.subtitles).unwrap();
        file.write_u8(self.widescreen).unwrap();
        file.write_u8(self.frame_limiter).unwrap();
        file.write_u8(self.video_mode).unwrap();

        file.write(&self.skin_path).unwrap();

        file.write_u8(self.standard_controls).unwrap();
        file.write_u8(self.current_language).unwrap();
        file.write_u8(self.hud_mode).unwrap();
        file.write_u8(self.radar_mode).unwrap();
        file.write_u8(self.map_legend).unwrap();
        
        SettingsResult::Ok
    }
}