mindustry logic execution, map- and schematic- parsing and rendering
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
use std::any::{Any, type_name};
use std::borrow::Cow;

use flate2::{Compress, Compression, Decompress, FlushCompress, FlushDecompress, Status};

use crate::block::{BlockLogic, make_register};
use crate::block::simple::{SimpleBlock, state_impl};
use crate::data::{DataRead, DataWrite};
use crate::data::dynamic::DynData;

make_register!
(
	MESSAGE: "message" => MessageLogic;
	SWITCH: "switch" => SwitchLogic;
	MICRO_PROCESSOR: "micro-processor" => ProcessorLogic{size: 1};
	LOGIC_PROCESSOR: "logic-processor" => ProcessorLogic{size: 2};
	HYPER_PROCESSOR: "hyper-processor" => ProcessorLogic{size: 3};
	MEMORY_CELL: "memory-cell" => SimpleBlock::new(1, true);
	MEMORY_BANK: "memory-bank" => SimpleBlock::new(2, true);
	LOGIC_DISPLAY: "logic-display" => SimpleBlock::new(3, true);
	LARGE_LOGIC_DISPLAY: "large-logic-display" => SimpleBlock::new(6, true);
);

pub struct MessageLogic;

impl MessageLogic
{
	state_impl!(pub String);
}

impl BlockLogic for MessageLogic
{
	fn get_size(&self) -> u8
	{
		1
	}
	
	fn is_symmetric(&self) -> bool
	{
		true
	}
	
	fn data_from_i32(&self, _: i32) -> DynData
	{
		DynData::Empty
	}
	
	fn deserialize_state(&self, data: DynData) -> Option<Box<dyn Any>>
	{
		match data
		{
			DynData::Empty | DynData::String(None) => Some(Self::create_state(String::new())),
			DynData::String(Some(s)) => Some(Self::create_state(s)),
			_ => panic!("{} cannot use data of {:?}", type_name::<Self>(), data.get_type()),
		}
	}
	
	fn clone_state(&self, state: &dyn Any) -> Box<dyn Any>
	{
		Box::new(Self::get_state(state).clone())
	}
	
	fn serialize_state(&self, state: &dyn Any) -> DynData
	{
		DynData::String(Some(Self::get_state(state).clone()))
	}
}

pub struct SwitchLogic;

impl SwitchLogic
{
	state_impl!(pub bool);
}

impl BlockLogic for SwitchLogic
{
	fn get_size(&self) -> u8
	{
		1
	}
	
	fn is_symmetric(&self) -> bool
	{
		true
	}
	
	fn data_from_i32(&self, _: i32) -> DynData
	{
		DynData::Empty
	}
	
	fn deserialize_state(&self, data: DynData) -> Option<Box<dyn Any>>
	{
		match data
		{
			DynData::Empty => Some(Self::create_state(true)),
			DynData::Boolean(enabled) => Some(Self::create_state(enabled)),
			_ => panic!("{} cannot use data of {:?}", type_name::<Self>(), data.get_type()),
		}
	}
	
	fn clone_state(&self, state: &dyn Any) -> Box<dyn Any>
	{
		Box::new(Self::get_state(state).clone())
	}
	
	fn serialize_state(&self, state: &dyn Any) -> DynData
	{
		DynData::Boolean(*Self::get_state(state))
	}
}

pub struct ProcessorLogic
{
	size: u8,
}

impl ProcessorLogic
{
	state_impl!(pub ProcessorState);
}

impl BlockLogic for ProcessorLogic
{
	fn get_size(&self) -> u8
	{
		self.size
	}
	
	fn is_symmetric(&self) -> bool
	{
		true
	}
	
	fn data_from_i32(&self, _: i32) -> DynData
	{
		DynData::Empty
	}
	
	fn deserialize_state(&self, data: DynData) -> Option<Box<dyn Any>>
	{
		match data
		{
			DynData::Empty => Some(Self::create_state(ProcessorState::new())),
			DynData::ByteArray(arr) =>
			{
				let mut input = arr.as_ref();
				let mut dec = Decompress::new(true);
				let mut raw = Vec::<u8>::new();
				raw.reserve(1024);
				loop
				{
					let t_in = dec.total_in();
					let t_out = dec.total_out();
					let res = dec.decompress_vec(input, &mut raw, FlushDecompress::Finish).unwrap();
					if dec.total_in() > t_in
					{
						// we have to advance input every time, decompress_vec only knows the output position
						input = &input[(dec.total_in() - t_in) as usize..];
					}
					match res
					{
						// there's no more input (and the flush mode says so), we need to reserve additional space
						Status::Ok | Status::BufError => (),
						// input was already at the end, so this is referring to the output
						Status::StreamEnd => break,
					}
					if dec.total_in() == t_in && dec.total_out() == t_out
					{
						// protect against looping forever
						panic!("decompressor stalled");
					}
					raw.reserve(1024);
				}
				let mut buff = DataRead::new(&raw);
				let ver = buff.read_u8().unwrap();
				if ver != 1
				{
					panic!("unknown version {ver}");
				}
				
				let code_len = buff.read_i32().unwrap();
				if code_len < 0 || code_len > 500 * 1024
				{
					panic!("invalid code length ({code_len})");
				}
				let mut code = Vec::<u8>::new();
				code.resize(code_len as usize, 0);
				buff.read_bytes(&mut code).unwrap();
				let code = String::from_utf8(code).unwrap();
				let link_cnt = buff.read_i32().unwrap();
				if link_cnt < 0
				{
					panic!("link count is negative ({link_cnt})");
				}
				let mut links = Vec::<ProcessorLink>::new();
				links.reserve(link_cnt as usize);
				for _ in 0..link_cnt
				{
					let name = buff.read_utf().unwrap();
					let x = buff.read_i16().unwrap();
					let y = buff.read_i16().unwrap();
					links.push(ProcessorLink{name: String::from(name), x, y});
				}
				Some(Self::create_state(ProcessorState{code, links}))
			},
			_ => panic!("{} cannot use data of {:?}", type_name::<Self>(), data.get_type()),
		}
	}
	
	fn clone_state(&self, state: &dyn Any) -> Box<dyn Any>
	{
		Box::new(Self::get_state(state).clone())
	}
	
	fn serialize_state(&self, state: &dyn Any) -> DynData
	{
		let state = Self::get_state(state);
		let mut rbuff = DataWrite::new();
		rbuff.write_u8(1).unwrap();
		if state.code.len() > i32::MAX as usize
		{
			panic!("code too long ({})", state.code.len());
		}
		rbuff.write_i32(state.code.len() as i32).unwrap();
		rbuff.write_bytes(state.code.as_bytes()).unwrap();
		if state.links.len() > i32::MAX as usize
		{
			panic!("too many links ({})", state.links.len());
		}
		rbuff.write_i32(state.links.len() as i32).unwrap();
		for link in state.links.iter()
		{
			rbuff.write_utf(&link.name).unwrap();
			rbuff.write_i16(link.x).unwrap();
			rbuff.write_i16(link.y).unwrap();
		}
		let mut input = rbuff.get_written();
		let mut comp = Compress::new(Compression::default(), true);
		let mut dst = Vec::<u8>::new();
		dst.reserve(1024);
		loop
		{
			let t_in = comp.total_in();
			let t_out = comp.total_out();
			let res = comp.compress_vec(input, &mut dst, FlushCompress::Finish).unwrap();
			if comp.total_in() > t_in
			{
				// we have to advance input every time, compress_vec only knows the output position
				input = &input[(comp.total_in() - t_in) as usize..];
			}
			match res
			{
				// there's no more input (and the flush mode says so), we need to reserve additional space
				Status::Ok | Status::BufError => (),
				// input was already at the end, so this is referring to the output
				Status::StreamEnd => break,
			}
			if comp.total_in() == t_in && comp.total_out() == t_out
			{
				// protect against looping forever
				panic!("compressor stalled");
			}
			dst.reserve(1024);
		}
		DynData::ByteArray(dst)
	}
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ProcessorLink
{
	name: String,
	x: i16,
	y: i16,
}

impl ProcessorLink
{
	pub fn new(name: Cow<'_, str>, x: i16, y: i16) -> Self
	{
		if name.len() > u16::MAX as usize
		{
			panic!("name too long ({})", name.len());
		}
		Self{name: name.into_owned(), x, y}
	}
	
	pub fn get_name(&self) -> &str
	{
		&self.name
	}
	
	pub fn get_pos(&self) -> (i16, i16)
	{
		(self.x, self.y)
	}
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ProcessorState
{
	code: String,
	links: Vec<ProcessorLink>
}

impl ProcessorState
{
	pub fn new() -> Self
	{
		Self{code: String::new(), links: Vec::new()}
	}
	
	pub fn get_code(&self) -> &str
	{
		&self.code
	}
	
	pub fn set_code(&mut self, code: Cow<'_, str>) -> Result<(), CodeError>
	{
		let as_str = &code as &str;
		if as_str.len() > 500 * 1024
		{
			return Err(CodeError::TooLong(as_str.len()));
		}
		match code
		{
			Cow::Borrowed(s) =>
			{
				self.code.clear();
				self.code.push_str(s);
			},
			Cow::Owned(s) => self.code = s,
		}
		Ok(())
	}
	
	pub fn get_links(&self) -> &[ProcessorLink]
	{
		&self.links
	}
	
	pub fn create_link(&mut self, mut name: String, x: i16, y: i16) -> Result<&ProcessorLink, CreateError>
	{
		if name.len() > u16::MAX as usize
		{
			return Err(CreateError::NameLength{len: name.len()})
		}
		for curr in self.links.iter()
		{
			if &name == &curr.name
			{
				return Err(CreateError::DuplicateName{name});
			}
			if x == curr.x && y == curr.y
			{
				name.clear();
				name.push_str(&curr.name);
				return Err(CreateError::DuplicatePos{name, x, y});
			}
		}
		let idx = self.links.len();
		self.links.push(ProcessorLink{name, x, y});
		Ok(&self.links[idx])
	}
	
	pub fn add_link(&mut self, link: ProcessorLink) -> Result<&ProcessorLink, CreateError>
	{
		self.create_link(link.name, link.x, link.y)
	}
	
	pub fn remove_link(&mut self, idx: usize) -> Option<ProcessorLink>
	{
		if idx < self.links.len()
		{
			Some(self.links.remove(idx))
		}
		else {None}
	}
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CodeError
{
	TooLong(usize),
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CreateError
{
	NameLength{len: usize},
	DuplicateName{name: String},
	DuplicatePos{name: String, x: i16, y: i16},
}