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
use crate::*;
use ahash::RandomState;
use fxhash::FxHashMap;
use std::cell::RefCell;
use std::collections::{hash_map::Entry, HashMap};
use std::fmt;

//
// ### A note on Override IDs
//
// Procs in DM can be defined multiple times.
//
// ```
// /proc/hello() // Override #0 or base proc
//		world << "Hello"
//
//	/hello() // Override #1
//		..() // Calls override #0
//		world << "World"
//
//	/hello() // Override #2
//		..() // Calls override #1
//		world << "!!!"
//	```
//
//	To get the nth override, use [get_proc_override]: `let hello = get_proc_override("/proc/hello", n).unwrap()`
// [get_proc] retrieves the base proc.
//
//

/// Used to hook and call procs.
#[derive(Clone)]
pub struct Proc {
	pub id: raw_types::procs::ProcId,
	pub entry: *mut raw_types::procs::ProcEntry,
	pub path: String,
}

impl Proc {
	/// Finds the first proc with the given path
	pub fn find<S: Into<String>>(path: S) -> Option<Self> {
		get_proc(path)
	}

	/// Finds the n'th re-defined proc with the given path
	pub fn find_override<S: Into<String>>(path: S, override_id: u32) -> Option<Self> {
		get_proc_override(path, override_id)
	}

	pub fn from_id(id: raw_types::procs::ProcId) -> Option<Self> {
		let mut proc_entry: *mut raw_types::procs::ProcEntry = std::ptr::null_mut();
		unsafe {
			assert_eq!(
				raw_types::funcs::get_proc_array_entry(&mut proc_entry, id),
				1
			);
		}
		if proc_entry.is_null() {
			return None;
		}
		let proc_name = strip_path(unsafe { StringRef::from_id((*proc_entry).path).into() });
		Some(Proc {
			id: id,
			entry: proc_entry,
			path: proc_name.clone(),
		})
	}

	pub unsafe fn file_name(&self) -> Option<StringRef> {
		let bytecode = self.bytecode();
		if bytecode.len() < 2 || bytecode[0] != 0x84 {
			return None;
		}

		let file_id = raw_types::strings::StringId(bytecode[0x01]);
		if !file_id.valid() {
			return None;
		}

		Some(StringRef::from_id(file_id))
	}

	pub fn parameter_names(&self) -> Vec<StringRef> {
		unsafe {
			let (data, count) = raw_types::misc::get_parameters((*self.entry).parameters);
			(0..count)
				.map(|i| StringRef::from_variable_id((*data.add(i as usize)).name))
				.collect()
		}
	}

	pub fn local_names(&self) -> Vec<StringRef> {
		unsafe {
			let (names, count) = raw_types::misc::get_locals((*self.entry).locals);
			(0..count)
				.map(|i| StringRef::from_variable_id(*names.add(i as usize)))
				.collect()
		}
	}

	pub fn set_bytecode(&self, bytecode: Vec<u32>) {
		crate::bytecode_manager::set_bytecode(self, bytecode);
	}

	pub unsafe fn bytecode_mut_ptr(&self) -> (*mut u32, u16) {
		raw_types::misc::get_bytecode((*self.entry).bytecode)
	}

	pub unsafe fn bytecode(&self) -> &[u32] {
		let (ptr, count) = self.bytecode_mut_ptr();
		std::slice::from_raw_parts(ptr, count as usize)
	}

	/// Calls a global proc with the given arguments.
	///
	/// # Examples
	///
	/// This function is equivalent to `return do_explode(3)` in DM.
	/// ```ignore
	/// #[hook("/proc/my_proc")]
	/// fn my_proc_hook() -> DMResult {
	///     let proc = Proc::find("/proc/do_explode").unwrap();
	///     proc.call(&[&Value::from(3.0)])
	/// }
	/// ```
	pub fn call(&self, args: &[&Value]) -> runtime::DMResult {
		let mut ret = raw_types::values::Value {
			tag: raw_types::values::ValueTag::Null,
			data: raw_types::values::ValueData { id: 0 },
		};

		unsafe {
			// Increment ref-count of args permenently before passing them on
			for v in args {
				raw_types::funcs::inc_ref_count(v.raw);
			}

			let args: Vec<_> = args.iter().map(|e| e.raw).collect();

			if raw_types::funcs::call_proc_by_id(
				&mut ret,
				Value::null().raw,
				0,
				self.id,
				0,
				Value::null().raw,
				args.as_ptr(),
				args.len(),
				0,
				0,
			) == 1
			{
				return Ok(Value::from_raw_owned(ret));
			}
		}

		Err(runtime!("External proc call failed"))
	}

	pub fn override_id(&self) -> u32 {
		PROC_OVERRIDE_IDS.with(|override_ids| match override_ids.borrow().get(&self.id) {
			Some(id) => *id,
			None => 0,
		})
	}
}

impl fmt::Debug for Proc {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		let path = unsafe { (*self.entry).path };
		write!(f, "Proc({:?})", unsafe { StringRef::from_id(path) })
	}
}

thread_local!(static PROCS_BY_NAME: RefCell<HashMap<String, Vec<Proc>, RandomState>> = RefCell::new(HashMap::with_hasher(RandomState::default())));
thread_local!(static PROC_OVERRIDE_IDS: RefCell<FxHashMap<raw_types::procs::ProcId, u32>> = RefCell::new(FxHashMap::default()));

fn strip_path(p: String) -> String {
	p.replace("/proc/", "/").replace("/verb/", "/")
}

pub fn populate_procs() {
	let mut i: u32 = 0;
	loop {
		let proc = Proc::from_id(raw_types::procs::ProcId(i));
		if proc.is_none() {
			break;
		}
		let proc = proc.unwrap();

		PROC_OVERRIDE_IDS.with(|override_ids| {
			let mut override_ids = override_ids.borrow_mut();

			PROCS_BY_NAME.with(|h| {
				match h.borrow_mut().entry(proc.path.clone()) {
					Entry::Occupied(mut o) => {
						let vec = o.get_mut();
						override_ids.insert(proc.id, vec.len() as u32);
						vec.push(proc);
					}
					Entry::Vacant(v) => {
						override_ids.insert(proc.id, 0);
						v.insert(vec![proc]);
					}
				};
			});
		});

		i += 1;
	}
}

pub fn clear_procs() {
	PROCS_BY_NAME.with(|h| h.borrow_mut().clear());
	PROC_OVERRIDE_IDS.with(|override_ids| override_ids.borrow_mut().clear());
}

pub fn get_proc_override<S: Into<String>>(path: S, override_id: u32) -> Option<Proc> {
	let s = strip_path(path.into());
	PROCS_BY_NAME.with(|h| match h.borrow().get(&s)?.get(override_id as usize) {
		Some(p) => Some(p.clone()),
		None => None,
	})
}

/// Retrieves the 0th override of a proc.
pub fn get_proc<S: Into<String>>(path: S) -> Option<Proc> {
	get_proc_override(path, 0)
}