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
use super::raw_types;
use super::string;
use crate::list;
use crate::runtime;
use crate::runtime::DMResult;
use std::ffi::CString;
use std::fmt;
use std::marker::PhantomData;

/// `Value` represents any value a DM variable can hold, such as numbers, strings, datums, etc.
///
/// There's a lot of lifetime shenanigans going on, the gist of it is to just not keep Values around for longer than your hook's execution.
pub struct Value {
	pub raw: raw_types::values::Value,
	phantom: PhantomData<*mut ()>,
}

impl PartialEq for Value {
	fn eq(&self, other: &Self) -> bool {
		unsafe { self.raw.tag == other.raw.tag && self.raw.data.id == other.raw.data.id }
	}
}

impl Eq for Value {}

impl std::hash::Hash for Value {
	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
		unsafe {
			self.raw.tag.hash(state);
			self.raw.data.id.hash(state);
		}
	}
}

impl Drop for Value {
	fn drop(&mut self) {
		unsafe {
			raw_types::funcs::dec_ref_count(self.raw);
		}
	}
}

impl Value {
	/// Creates a new value from raw tag and data.
	/// Use if you know what you are doing.
	pub unsafe fn new(
		tag: raw_types::values::ValueTag,
		data: raw_types::values::ValueData,
	) -> Value {
		let raw = raw_types::values::Value { tag, data };
		raw_types::funcs::inc_ref_count(raw);

		Value {
			raw,
			phantom: PhantomData {},
		}
	}

	/// Equivalent to DM's `global.vars`.
	pub fn globals() -> Value {
		Value {
			raw: raw_types::values::Value {
				tag: raw_types::values::ValueTag::World,
				data: raw_types::values::ValueData { id: 1 },
			},
			phantom: PhantomData {},
		}
	}

	/// Equivalent to DM's `world`.
	pub fn world() -> Value {
		Value {
			raw: raw_types::values::Value {
				tag: raw_types::values::ValueTag::World,
				data: raw_types::values::ValueData { id: 0 },
			},
			phantom: PhantomData {},
		}
	}

	/// Equivalent to DM's `null`.
	pub fn null() -> Value {
		Value {
			raw: raw_types::values::Value {
				tag: raw_types::values::ValueTag::Null,
				data: raw_types::values::ValueData { number: 0.0 },
			},
			phantom: PhantomData {},
		}
	}

	/// Gets a turf by ID, without bounds checking. Use turf_by_id if you're not sure about how to check the bounds.
	pub unsafe fn turf_by_id_unchecked(id: u32) -> Value {
		Value {
			raw: raw_types::values::Value {
				tag: raw_types::values::ValueTag::Turf,
				data: raw_types::values::ValueData { id },
			},
			phantom: PhantomData {},
		}
	}
	/// Gets a turf by ID, with bounds checking.
	pub fn turf_by_id(id: u32) -> DMResult {
		let world = Value::world();
		let max_x = world.get_number(crate::byond_string!("maxx"))? as u32;
		let max_y = world.get_number(crate::byond_string!("maxy"))? as u32;
		let max_z = world.get_number(crate::byond_string!("maxz"))? as u32;
		if (0..max_x * max_y * max_z).contains(&(id - 1)) {
			Ok(unsafe { Value::turf_by_id_unchecked(id) })
		} else {
			Err(runtime!("Attempted to get tile with invalid ID {}", id))
		}
	}

	/// Gets a turf by coordinates.
	pub fn turf(x: u32, y: u32, z: u32) -> DMResult {
		let world = Value::world();
		let max_x = world.get_number(crate::byond_string!("maxx"))? as u32;
		let max_y = world.get_number(crate::byond_string!("maxy"))? as u32;
		let max_z = world.get_number(crate::byond_string!("maxz"))? as u32;
		let x = x - 1; // thanks byond
		let y = y - 1;
		let z = z - 1;
		if (0..max_x).contains(&x) && (0..max_y).contains(&y) && (0..max_z).contains(&z) {
			Ok(unsafe { Value::turf_by_id_unchecked(x + y * max_x + z * max_x * max_y) })
		} else {
			Err(runtime!(
				"Attempted to get out-of-range tile at coords {} {} {}",
				x + 1,
				y + 1,
				z + 1
			))
		}
	}

	fn get_by_id(&self, name_id: raw_types::strings::StringId) -> DMResult {
		let mut val = raw_types::values::Value {
			tag: raw_types::values::ValueTag::Null,
			data: raw_types::values::ValueData { id: 0 },
		};

		unsafe {
			if raw_types::funcs::get_variable(&mut val, self.raw, name_id) != 1 {
				let varname: String = string::StringRef::from_id(name_id).into();
				return Err(runtime!("Could not read {}.{}", &self, varname));
			}

			Ok(Self::from_raw(val))
		}
	}

	fn set_by_id(
		&self,
		name_id: raw_types::strings::StringId,
		new_value: raw_types::values::Value,
	) -> Result<(), runtime::Runtime> {
		unsafe {
			if raw_types::funcs::set_variable(self.raw, name_id, new_value) != 1 {
				let varname: String = string::StringRef::from_id(name_id).into();
				return Err(runtime!("Could not write to {}.{}", self, varname));
			}
		}
		Ok(())
	}

	/// Gets a variable by name.
	pub fn get<S: Into<string::StringRef>>(&self, name: S) -> DMResult {
		let name = name.into();
		self.get_by_id(name.get_id())
	}

	/// Gets a variable by name and safely casts it to a float.
	pub fn get_number<S: Into<string::StringRef>>(&self, name: S) -> DMResult<f32> {
		self.get(name)?.as_number()
	}

	/// Gets a variable by name and safely casts it to a string.
	pub fn get_string<S: Into<string::StringRef>>(&self, name: S) -> DMResult<String> {
		self.get(name)?.as_string()
	}

	/// Gets a variable by name and safely casts it to a [list::List].
	pub fn get_list<S: Into<string::StringRef>>(&self, name: S) -> DMResult<list::List> {
		let var = self.get(name)?;
		var.as_list()
	}

	/// Sets a variable by name to a given value.
	pub fn set<S: Into<string::StringRef>, V: Into<Value>>(
		&self,
		name: S,
		value: V,
	) -> DMResult<()> {
		let value = value.into();

		self.set_by_id(name.into().get_id(), value.raw)?;
		Ok(())
	}

	/// Check if the current value is a number and casts it.
	pub fn as_number(&self) -> DMResult<f32> {
		match self.raw.tag {
			raw_types::values::ValueTag::Number => unsafe { Ok(self.raw.data.number) },
			_ => Err(runtime!("Attempt to interpret non-number value as number")),
		}
	}

	/// Check if the current value is a string and casts it.
	pub fn as_string(&self) -> DMResult<String> {
		match self.raw.tag {
			raw_types::values::ValueTag::String => unsafe {
				Ok(string::StringRef::from_id(self.raw.data.string).into())
			},
			_ => Err(runtime!("Attempt to interpret non-string value as String")),
		}
	}

	/// Check if the current value is a list and casts it.
	pub fn as_list(&self) -> DMResult<list::List> {
		list::List::from_value(self)
	}

	/// Calls a method of the value with the given arguments.
	///
	/// # Examples:
	///
	/// This example is equivalent to `src.explode(3)` in DM.
	/// ```ignore
	/// src.call("explode", &[&Value::from(3.0)]);
	/// ```
	pub fn call<S: AsRef<str>>(&self, procname: S, args: &[&Value]) -> 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 procname = String::from(procname.as_ref()).replace("_", " ");
			let mut args: Vec<_> = args.iter().map(|e| e.raw).collect();
			let name_ref = string::StringRef::new(&procname)?;

			if raw_types::funcs::call_datum_proc_by_name(
				&mut ret,
				Value::null().raw,
				2,
				name_ref.value.raw.data.string,
				self.raw,
				args.as_mut_ptr(),
				args.len(),
				0,
				0,
			) == 1
			{
				return Ok(Value::from_raw_owned(ret));
			}
		}

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

	// ugh
	pub fn to_dmstring(&self) -> DMResult<string::StringRef> {
		match self.raw.tag {
			raw_types::values::ValueTag::Null
			| raw_types::values::ValueTag::Number
			| raw_types::values::ValueTag::String => {
				return Ok(string::StringRef::new(format!("{}", self.raw).as_str())?)
			}

			_ => {}
		}

		let mut id = raw_types::strings::StringId(0);

		unsafe {
			if raw_types::funcs::to_string(&mut id, self.raw) != 1 {
				return Err(runtime!("to_string failed on {:?}", self));
			}
			Ok(string::StringRef::from_id(id))
		}
	}

	pub fn to_string(&self) -> DMResult<String> {
		match self.raw.tag {
			raw_types::values::ValueTag::Null
			| raw_types::values::ValueTag::Number
			| raw_types::values::ValueTag::String => return Ok(format!("{}", self.raw)),

			_ => {}
		}

		let mut id = raw_types::strings::StringId(0);

		unsafe {
			if raw_types::funcs::to_string(&mut id, self.raw) != 1 {
				return Err(runtime!("to_string failed on {:?}", self));
			}
			Ok(String::from(string::StringRef::from_id(id)))
		}
	}

	/// Gets the type of the Value as a string
	pub fn get_type(&self) -> Result<String, runtime::Runtime> {
		self.get(crate::byond_string!("type"))?.to_string()
	}

	/// Checks whether this Value's type is equal to `typepath`.
	pub fn is_exact_type<S: AsRef<str>>(&self, typepath: S) -> bool {
		match self.get_type() {
			Err(_) => false,
			Ok(my_type) => my_type == typepath.as_ref(),
		}
	}

	pub fn is_truthy(&self) -> bool {
		match self.raw.tag {
			raw_types::values::ValueTag::Null => false,
			raw_types::values::ValueTag::Number => unsafe { self.raw.data.number != 0.0 },

			_ => true,
		}
	}

	/// Creates a Value that references a byond string.
	/// Will panic if the given string contains null bytes
	///
	/// # Examples:
	/// ```ignore
	/// let my_string = Value::from_string("Testing!");
	/// ```
	pub fn from_string<S: AsRef<str>>(data: S) -> DMResult {
		let string = CString::new(data.as_ref())
			.map_err(|_| runtime!("tried to create string containing NUL"))?;

		unsafe {
			let mut id = raw_types::strings::StringId(0);

			assert_eq!(raw_types::funcs::get_string_id(&mut id, string.as_ptr()), 1);

			Ok(Value::new(
				raw_types::values::ValueTag::String,
				raw_types::values::ValueData { string: id },
			))
		}
	}

	pub fn from_string_raw(data: &[u8]) -> DMResult {
		let string =
			CString::new(data).map_err(|_| runtime!("tried to create string containing NUL"))?;

		unsafe {
			let mut id = raw_types::strings::StringId(0);

			assert_eq!(raw_types::funcs::get_string_id(&mut id, string.as_ptr()), 1);

			Ok(Value::new(
				raw_types::values::ValueTag::String,
				raw_types::values::ValueData { string: id },
			))
		}
	}

	/// blah blah lifetime is not verified with this so use at your peril
	pub unsafe fn from_raw(v: raw_types::values::Value) -> Self {
		Value::new(v.tag, v.data)
	}

	/// same as from_raw but does not increment the reference count (assumes we already own this reference)
	pub unsafe fn from_raw_owned(v: raw_types::values::Value) -> Value {
		Value {
			raw: v,
			phantom: PhantomData {},
		}
	}
}

impl Clone for Value {
	fn clone(&self) -> Value {
		unsafe { Value::from_raw(self.raw) }
	}
}

impl fmt::Display for Value {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(f, "{}", self.raw)
	}
}

impl fmt::Debug for Value {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(f, "{:?}", self.raw)
	}
}