Skip to content

Commit

Permalink
Execute quake.rc at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
cormac-obrien committed Aug 31, 2018
1 parent 3589380 commit 9cf091d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
5 changes: 4 additions & 1 deletion src/bin/quake-client/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ impl ClientProgram {
console.clone(),
)));

// this will also execute config.cfg and autoexec.cfg (assuming an unmodified quake.rc)
console.borrow().stuff_text("exec quake.rc\n");

ClientProgram {
vfs: Rc::new(vfs),
cvars,
Expand Down Expand Up @@ -304,7 +307,7 @@ impl Program for ClientProgram {
}

// run console commands
self.console.borrow_mut().execute();
self.console.borrow().execute();

self.render();
}
Expand Down
55 changes: 36 additions & 19 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,21 @@ pub use self::cvars::register_cvars;
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt;
use std::io::BufReader;
use std::io::{BufReader, Read};
use std::net::ToSocketAddrs;
use std::rc::Rc;

use client::input::game::{Action, GameInput};
use client::sound::AudioSource;
use client::sound::Channel;
use client::sound::StaticSound;
use client::sound::{AudioSource, Channel, StaticSound};
use common::bsp;
use common::console::CmdRegistry;
use common::console::Console;
use common::console::CvarRegistry;
use common::console::{CmdRegistry, Console, CvarRegistry};
use common::engine;
use common::model::Model;
use common::model::ModelFlags;
use common::model::ModelKind;
use common::model::SyncType;
use common::net;
use common::net::connect::ConnectSocket;
use common::net::connect::Request;
use common::net::connect::Response;
use common::net::connect::CONNECT_PROTOCOL_VERSION;
use common::net::{BlockingMode, ButtonFlags, ClientCmd, ClientStat, ColorShift, EntityEffects,
EntityState, GameType, ItemFlags, NetError, PlayerColor, QSocket, ServerCmd,
SignOnStage, TempEntity};
use common::model::{Model, ModelFlags, ModelKind, SyncType};
use common::net::connect::{ConnectSocket, Request, Response, CONNECT_PROTOCOL_VERSION};
use common::net::{
self, BlockingMode, ButtonFlags, ClientCmd, ClientStat, ColorShift, EntityEffects, EntityState,
GameType, ItemFlags, NetError, PlayerColor, QSocket, ServerCmd, SignOnStage, TempEntity,
};
use common::vfs::Vfs;

use cgmath::Angle;
Expand Down Expand Up @@ -1578,6 +1568,33 @@ impl Client {
});
}),
).unwrap();

let vfs = self.vfs.clone();
let console = self.console.clone();
cmds.insert_or_replace(
"exec",
Box::new(move |args| {
match args.len() {
// exec (filename): execute a script file
1 => {
let mut script_file = match vfs.open(args[0]) {
Ok(s) => s,
Err(e) => {
println!("Couldn't exec {}: {:?}", args[0], e);
return;
}
};

let mut script = String::new();
script_file.read_to_string(&mut script).unwrap();

console.borrow().stuff_text(script);
}

_ => println!("exec (filename): execute a script file"),
}
}),
).unwrap();
}

pub fn spawn_temp_entity(&self, temp_entity: &TempEntity) {
Expand Down
20 changes: 11 additions & 9 deletions src/common/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ pub struct Console {

input: ConsoleInput,
hist: History,
buffer: String,
buffer: RefCell<String>,
output: Rc<RefCell<ConsoleOutput>>,
}

Expand All @@ -428,7 +428,7 @@ impl Console {
cvars,
input: ConsoleInput::new(),
hist: History::new(),
buffer: String::new(),
buffer: RefCell::new(String::new()),
output: output.clone(),
}
}
Expand All @@ -441,7 +441,7 @@ impl Console {
'\r' => {
// push this line to the execution buffer
let entered = self.get_string();
self.buffer.push_str(&entered);
self.buffer.borrow_mut().push_str(&entered);

// add the current input to the history
self.hist.add_line(self.input.get_text());
Expand Down Expand Up @@ -488,8 +488,11 @@ impl Console {
}

/// Interprets the contents of the execution buffer.
pub fn execute(&mut self) {
for line in (&self.buffer).split(|c| c == '\n' || c == ';') {
pub fn execute(&self) {
let text = self.buffer.borrow().to_owned();
self.buffer.borrow_mut().clear();

for line in text.split(|c| c == '\n' || c == ';') {
let mut tok = Tokenizer::new(line);
if let Some(arg_0) = tok.next() {
println!("arg0: {}", arg_0);
Expand All @@ -512,8 +515,6 @@ impl Console {
}
}
}

self.buffer.clear();
}

pub fn get_string(&self) -> String {
Expand All @@ -528,8 +529,9 @@ impl Console {
)
}

pub fn stuff_text<S>(&mut self, text: S) where S: AsRef<str> {
self.buffer.push_str(text.as_ref());
pub fn stuff_text<S>(&self, text: S) where S: AsRef<str> {
debug!("stuff_text:\n{}", text.as_ref());
self.buffer.borrow_mut().push_str(text.as_ref());
}

pub fn output(&self) -> Ref<ConsoleOutput> {
Expand Down

0 comments on commit 9cf091d

Please sign in to comment.