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
use gospel::read::{Reader, Le as _};
use gospel::write::{Writer, Le as _, Label};
use crate::types::*;
use themelios_common::util::*;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ED6Name {
	pub id: NameId,
	pub name: TString,
	pub chip1: (FileId, FileId),
	pub chip2: (FileId, FileId),
	pub ms1: FileId,
	pub ms2: FileId,
	pub stch: FileId,
}

impl ED6Name {
	// This does not roundtrip in SC and 3rd: there's an unreferenced entry labeled "Joshua" just before 999.

	pub fn read(game: Game, data: &[u8]) -> Result<(Vec<ED6Name>, Vec<ED6Name>), ReadError> {
		let f = &mut Reader::new(data);
		if game.base() != BaseGame::Fc {
			let s1 = f.u16()? as usize;
			let t1 = Self::read_chunk(game, f, NameId(999))?;
			ensure!(f.pos() == s1);
			let t2 = Self::read_chunk(game, f, NameId(2999))?;
			Ok((t1, t2))
		} else {
			let t1 = Self::read_chunk(game, f, NameId(999))?;
			let t2 = Vec::new();
			Ok((t1, t2))
		}
	}

	fn read_chunk(game: Game, f: &mut Reader, endid: NameId) -> Result<Vec<ED6Name>, ReadError> {
		let mut t = Vec::new();
		loop {
			let r = Self::read_one(game, f)?;
			if r.id == endid {
				break
			} else {
				t.push(r);
			}
		}
		Ok(t)
	}

	fn read_one(game: Game, f: &mut Reader) -> Result<ED6Name, ReadError> {
		let g = &mut f.ptr16()?;
		let id = NameId(g.u16()?);
		g.check_u16(0)?;
		let ch1 = FileId(g.u32()?);
		let ch2 = FileId(g.u32()?);
		let cp1 = FileId(g.u32()?);
		let cp2 = FileId(g.u32()?);
		let ms1 = FileId(g.u32()?);
		let ms2 = FileId(g.u32()?);
		let stch = if game.base() != BaseGame::Fc {
			FileId(g.u32()?)
		} else {
			FileId::NONE
		};
		let name = TString(g.ptr16()?.string()?);
		Ok(ED6Name { id, name, chip1: (ch1, cp1), chip2: (ch2, cp2), ms1, ms2, stch })
	}

	pub fn write(game: Game, t1: &[ED6Name], t2: &[ED6Name]) -> Result<Vec<u8>, WriteError> {
		let mut f = Writer::new();
		let mut g = Writer::new();
		if game.base() != BaseGame::Fc {
			let l = Label::new();
			f.delay16(l);
			Self::write_chunk(game, &mut f, &mut g, t1, NameId(999))?;
			f.label(l);
			Self::write_chunk(game, &mut f, &mut g, t2, NameId(2999))?;
		} else {
			Self::write_chunk(game, &mut f, &mut g, t1, NameId(999))?;
			ensure!(t2.is_empty());
		}
		f.append(g);
		Ok(f.finish()?)
	}

	fn write_chunk(game: Game, f: &mut Writer, g: &mut Writer, t: &[ED6Name], endid: NameId) -> Result<(), WriteError> {
		for i in t {
			Self::write_one(game, f, g, i)?;
		}
		Self::write_one(game, f, g, &ED6Name {
			id: endid,
			name: TString::from(" "),
			chip1: (FileId::NONE, FileId::NONE),
			chip2: (FileId::NONE, FileId::NONE),
			ms1: FileId::NONE,
			ms2: FileId::NONE,
			stch: FileId::NONE,
		})?;
		Ok(())
	}

	fn write_one(game: Game, f: &mut Writer, g: &mut Writer, i: &ED6Name) -> Result<(), WriteError> {
		f.delay16(g.here());
		g.u16(i.id.0);
		g.u16(0);
		g.u32(i.chip1.0.0);
		g.u32(i.chip2.0.0);
		g.u32(i.chip1.1.0);
		g.u32(i.chip2.1.0);
		g.u32(i.ms1.0);
		g.u32(i.ms2.0);
		if game.base() != BaseGame::Fc {
			g.u32(i.stch.0);
		} else {
			ensure!(i.stch == FileId::NONE);
		};
		let mut h = g.ptr16();
		h.string(&i.name.0)?;
		g.append(h);
		Ok(())
	}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ED7Name {
	pub id: NameId,
	pub name: TString,
	pub chip1: FileId,
	pub chip2: FileId,
	pub ms1: FileId,
	pub ms2: FileId,
}

impl ED7Name {
	pub fn read(data: &[u8]) -> Result<Vec<ED7Name>, ReadError> {
		let mut f = Reader::new(data);
		let mut table = Vec::new();
		loop {
			let id = NameId(f.u16()?);
			let name = TString(f.ptr16()?.string()?);
			let chip1 = FileId(f.u32()?);
			let chip2 = FileId(f.u32()?);
			let ms1 = FileId(f.u32()?);
			let ms2 = FileId(f.u32()?);
			if id == NameId(999) { break }
			table.push(ED7Name { id, name, chip1, chip2, ms1, ms2 });
		}
		Ok(table)
	}

	pub fn write(table: &[ED7Name]) -> Result<Vec<u8>, WriteError> {
		let mut f = Writer::new();
		let mut g = Writer::new();
		for name in table {
			f.u16(name.id.0);
			f.delay16(g.here());
			f.u32(name.chip1.0);
			f.u32(name.chip2.0);
			f.u32(name.ms1.0);
			f.u32(name.ms2.0);
			g.string(&name.name.0)?;
		}

		f.u16(999);
		f.delay16(g.here());
		f.u32(0);
		f.u32(0);
		f.u32(0);
		f.u32(0);
		g.string(" ")?;

		f.append(g);
		Ok(f.finish()?)
	}
}