sc2 0.3.3

Rust implementation of the StarCraft II Client API
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use std::env::home_dir;
use std::mem;
use std::path::{PathBuf, MAIN_SEPARATOR};

use futures::prelude::*;
use futures::unsync;
use glob::glob;
use organelle::{Axon, Constraint, Impulse, Soma};
use regex::Regex;

use super::{Error, ErrorKind, Result};
use data::Rect;
use instance::{Instance, InstanceKind, InstanceSettings};
use synapses::{Dendrite, Synapse};

/// endpoint port settings
#[allow(missing_docs)]
#[derive(Debug, Copy, Clone)]
pub struct PortSet {
    pub game_port: u16,
    pub base_port: u16,
}

/// all port settings for a game
#[allow(missing_docs)]
#[derive(Debug, Clone)]
pub struct GamePorts {
    pub shared_port: u16,
    pub server_ports: PortSet,
    pub client_ports: Vec<PortSet>,
}

/// sender for launcher
#[derive(Debug, Clone)]
pub struct LauncherTerminal {
    tx: unsync::mpsc::Sender<LauncherRequest>,
}

impl LauncherTerminal {
    fn new(tx: unsync::mpsc::Sender<LauncherRequest>) -> Self {
        Self { tx: tx }
    }

    /// launch an instance
    #[async]
    pub fn launch(self) -> Result<Instance> {
        let (tx, rx) = unsync::oneshot::channel();

        await!(
            self.tx
                .send(LauncherRequest::Launch(tx))
                .map_err(|_| Error::from("unable to send launch request"))
        )?;

        await!(rx.map_err(|_| Error::from("unable to receive instance")))
    }

    /// get a set of game ports
    #[async]
    pub fn get_game_ports(self) -> Result<GamePorts> {
        let (tx, rx) = unsync::oneshot::channel();

        await!(
            self.tx
                .send(LauncherRequest::Ports(tx))
                .map_err(|_| Error::from("unable to send ports request"))
        )?;

        await!(rx.map_err(|_| Error::from("unable to receive ports")))
    }
}

#[derive(Debug)]
enum LauncherRequest {
    Launch(unsync::oneshot::Sender<Instance>),
    Ports(unsync::oneshot::Sender<GamePorts>),
}

/// receiver for launcher
#[derive(Debug)]
pub struct LauncherDendrite {
    rx: unsync::mpsc::Receiver<LauncherRequest>,
}

impl LauncherDendrite {
    fn new(rx: unsync::mpsc::Receiver<LauncherRequest>) -> Self {
        Self { rx: rx }
    }
}

/// create a launcher synapse
pub fn synapse() -> (LauncherTerminal, LauncherDendrite) {
    let (tx, rx) = unsync::mpsc::channel(1);

    (LauncherTerminal::new(tx), LauncherDendrite::new(rx))
}

/// launches game instances upon request
pub struct Launcher {
    exe: PathBuf,
    pwd: Option<PathBuf>,
    current_port: u16,
    use_wine: bool,
}

/// builder used to create launcher
pub struct LauncherBuilder {
    dir: Option<PathBuf>,
    use_wine: bool,
    base_port: u16,
}

impl LauncherBuilder {
    /// create a new builder
    pub fn new() -> Self {
        Self {
            dir: None,
            use_wine: false,
            base_port: 9168,
        }
    }

    /// installation directory
    ///
    /// auto-detect if not specified
    pub fn install_dir(self, dir: PathBuf) -> Self {
        Self {
            dir: Some(dir),
            ..self
        }
    }

    /// use Wine to run the game - for unix users
    pub fn use_wine(self, flag: bool) -> Self {
        Self {
            use_wine: flag,
            ..self
        }
    }

    /// starting point for game ports
    pub fn base_port(self, port: u16) -> Self {
        Self {
            base_port: port,
            ..self
        }
    }

    /// build the settings object
    pub fn create(self) -> Result<Launcher> {
        let dir = {
            if let Some(dir) = self.dir {
                dir
            } else {
                auto_detect_starcraft(self.use_wine)?
            }
        };
        let (exe, arch) = select_exe(&dir, self.use_wine)?;
        let pwd = select_pwd(&dir, arch);

        Ok(Launcher {
            exe: exe,
            pwd: pwd,
            current_port: self.base_port,
            use_wine: self.use_wine,
        })
    }
}

impl Launcher {
    fn launch(&mut self) -> Result<Instance> {
        let mut instance = Instance::from_settings(InstanceSettings {
            kind: {
                if self.use_wine {
                    InstanceKind::Wine
                } else {
                    InstanceKind::Native
                }
            },
            exe: Some(self.exe.clone()),
            pwd: self.pwd.clone(),
            address: ("127.0.0.1".into(), self.current_port),
            window_rect: Rect::<u32> {
                x: 10,
                y: 10,
                w: 1024,
                h: 768,
            },
            ports: PortSet {
                game_port: self.current_port + 1,
                base_port: self.current_port + 2,
            },
        })?;

        self.current_port += 3;

        instance.start()?;

        Ok(instance)
    }

    #[async]
    fn listen(mut self, dendrite: LauncherDendrite) -> Result<()> {
        #[async]
        for req in dendrite.rx.map_err(|_| Error::from("streams can't fail")) {
            match req {
                LauncherRequest::Launch(tx) => {
                    tx.send(self.launch()?)
                        .map_err(|_| Error::from("unable to send instance"))?;
                },
                LauncherRequest::Ports(tx) => {
                    tx.send(self.create_game_ports())
                        .map_err(|_| Error::from("unable to send game ports"))?;
                },
            }
        }

        Ok(())
    }

    /// create a set of ports for multiplayer games
    pub fn create_game_ports(&mut self) -> GamePorts {
        let ports = GamePorts {
            shared_port: self.current_port,
            server_ports: PortSet {
                game_port: self.current_port + 1,
                base_port: self.current_port + 2,
            },
            client_ports: vec![],
        };

        self.current_port += 3;

        ports
    }
}

/// soma in charge of launching game instances and assigning ports
pub struct LauncherSoma {
    launcher: Option<Launcher>,
    dendrite: Option<LauncherDendrite>,
}

impl LauncherSoma {
    /// create a launcher from settings
    pub fn axon(launcher: Launcher) -> Result<Axon<Self>> {
        Ok(Axon::new(
            Self {
                launcher: Some(launcher),
                dendrite: None,
            },
            vec![Constraint::One(Synapse::Launcher)],
            vec![],
        ))
    }
}

impl Soma for LauncherSoma {
    type Synapse = Synapse;
    type Error = Error;

    #[async(boxed)]
    fn update(mut self, msg: Impulse<Self::Synapse>) -> Result<Self> {
        match msg {
            Impulse::AddDendrite(
                _,
                Synapse::Launcher,
                Dendrite::Launcher(dendrite),
            ) => {
                self.dendrite = Some(dendrite);

                Ok(self)
            },
            Impulse::Start(_, tx, handle) => {
                assert!(self.launcher.is_some());
                assert!(self.dendrite.is_some());

                handle.spawn(
                    mem::replace(&mut self.launcher, None)
                        .unwrap()
                        .listen(mem::replace(&mut self.dendrite, None).unwrap())
                        .or_else(move |e| {
                            tx.send(Impulse::Error(e.into()))
                                .map(|_| ())
                                .map_err(|_| ())
                        }),
                );

                Ok(self)
            },

            _ => bail!("unexpected impulse"),
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum ExeArch {
    X64,
    X32,
}

fn auto_detect_starcraft(use_wine: bool) -> Result<PathBuf> {
    if cfg!(windows) {
        let path_x86 = PathBuf::from("C:\\Program Files (x86)\\StarCraft II");
        let path = PathBuf::from("C:\\Program Files\\StarCraft II");

        if path_x86.is_dir() {
            Ok(path_x86)
        } else if path.is_dir() {
            Ok(path)
        } else {
            bail!(ErrorKind::ExeNotSpecified)
        }
    } else if use_wine {
        if let Some(home) = home_dir() {
            let path_x86 =
                home.join(".wine/drive_c/Program Files (x86)/StarCraft II");
            let path = home.join(".wine/drive_c/Program Files/StarCraft II");

            if path_x86.is_dir() {
                Ok(path_x86)
            } else if path.is_dir() {
                Ok(path)
            } else {
                bail!(ErrorKind::ExeNotSpecified)
            }
        } else {
            bail!(ErrorKind::ExeNotSpecified)
        }
    } else {
        bail!(ErrorKind::ExeNotSpecified)
    }
}

fn select_exe(dir: &PathBuf, use_wine: bool) -> Result<(PathBuf, ExeArch)> {
    if cfg!(target_os = "windows") && use_wine {
        bail!("wine not supported on windows")
    }

    let separator = match MAIN_SEPARATOR {
        '\\' => "\\\\",
        '/' => "/",
        _ => panic!("unsupported path separator {}", MAIN_SEPARATOR),
    };

    let glob_iter = match glob(
        &format!("{}/Versions/Base*/SC2*", dir.to_str().unwrap())[..],
    ) {
        Ok(iter) => iter,
        Err(_) => bail!("failed to read glob pattern"),
    };

    let exe_re =
        match Regex::new(&format!("Base([0-9]*){}SC2(_x64)?", separator)[..]) {
            Ok(re) => re,
            Err(_) => bail!("failed to parse regex"),
        };

    let mut current_version = 0;
    let mut current_arch = ExeArch::X32;
    let mut exe: Result<(PathBuf, ExeArch)> = Err("exe not found".into());

    for entry in glob_iter {
        match entry {
            Ok(path) => {
                let path_clone = path.clone();
                let path_str = match path_clone.to_str() {
                    Some(s) => s,
                    None => {
                        eprintln!("unable to convert path to string");
                        continue;
                    },
                };

                match exe_re.captures(&path_str[..]) {
                    Some(caps) => {
                        let v = match caps.get(1).unwrap().as_str().parse() {
                            Ok(v) => v,
                            Err(_) => {
                                eprintln!("unable to parse version as int");
                                continue;
                            },
                        };

                        let arch = match caps.get(2) {
                            Some(a) => match a.as_str() {
                                "_x64" => ExeArch::X64,
                                _ => {
                                    eprintln!("unrecognized suffix");
                                    continue;
                                },
                            },
                            None => ExeArch::X32,
                        };

                        if current_version < v {
                            current_version = v;

                            if use_wine {
                                if arch == ExeArch::X32 {
                                    exe = Ok((path, arch));
                                }
                            } else {
                                exe = Ok((path, arch));
                            }
                        } else if current_version == v && !use_wine {
                            current_arch = match current_arch {
                                ExeArch::X64 => ExeArch::X64,
                                ExeArch::X32 => match arch {
                                    ExeArch::X64 => ExeArch::X64,
                                    _ => ExeArch::X32,
                                },
                            };

                            exe = Ok((path, current_arch));
                        };
                    },
                    _ => (),
                }
            },
            _ => (),
        };
    }

    exe
}

fn select_pwd(dir: &PathBuf, arch: ExeArch) -> Option<PathBuf> {
    let separator = match MAIN_SEPARATOR {
        '\\' => "\\\\",
        '/' => "/",
        _ => panic!("unsupported path separator {}", MAIN_SEPARATOR),
    };

    let support_dir = PathBuf::from(
        &format!(
            "{}{}Support{}",
            dir.to_str().unwrap(),
            separator,
            match arch {
                ExeArch::X64 => "64",
                ExeArch::X32 => "",
            }
        )[..],
    );

    if support_dir.is_dir() {
        Some(support_dir)
    } else {
        None
    }
}