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
use crate::XClassItem;

use super::*;

#[derive(Serialize)]
pub struct UnityManagerWriter {
    compiler_version: &'static str,
    table_version: String,
    edit_time: String,
    config: UnityCodegen,
    tables: Vec<String>,
}

impl UnityManagerWriter {
    pub fn new(table: &MergedTable, unity: &UnityCodegen, version: &str) -> Self {
        Self {
            compiler_version: env!("CARGO_PKG_VERSION"),
            table_version: version.to_string(),
            edit_time: XCellValue::csharp_now(),
            config: unity.clone(),
            tables: table.table_names(&unity.suffix_table).into_iter().sorted().collect(),
        }
    }
}

impl UnityCodegen {
    pub fn ensure_path(&self, root: &Path) -> XResult<()> {
        if let Some(s) = self.unity_csharp_path(root, "test")?.parent() {
            create_dir_all(s)?
        }
        if let Some(s) = self.unity_binary_path(root, "test")?.parent() {
            create_dir_all(s)?
        }
        if let Some(s) = self.unity_xml_path(root, "test")?.parent() {
            create_dir_all(s)?
        }
        Ok(())
    }
    pub fn write_manager(&self, table: &MergedTable, root: &Path, version: &str) -> XResult<()> {
        let path = self.unity_manager_path(root)?;
        let ctx = Context::from_serialize(UnityManagerWriter::new(table, self, version))?;
        tera_render(include_str!("PartManager.cs.djv"), &ctx, &path, "PartManager.cs")?;
        Ok(())
    }
    pub fn write_class(&self, table: &XTable, root: &Path) -> XResult<()> {
        let file = format!("{}{}", table.name, self.suffix_table);
        let path = self.unity_csharp_path(root, &file)?;
        log::info!("写入 {}", self.unity_cs_relative(&file));
        match table.data {
            XTableKind::Array(_) => {
                tera_render(include_str!("PartDictionary.cs.djv"), &self.make_context(table), &path, "PartClass.cs")?;
            }
            XTableKind::Enumerate(_) => {
                tera_render(include_str!("PartDictionary.cs.djv"), &self.make_context(table), &path, "PartClass.cs")?;
            }
            XTableKind::Class(_) => {
                tera_render(include_str!("PartClass.cs.djv"), &self.make_context(table), &path, "PartClass.cs")?;
            }
            XTableKind::Dictionary(_) => {
                todo!()
            }
        }
        Ok(())
    }
    pub fn write_binary(&self, table: &XTable, root: &Path) -> XResult<()> {
        let file = format!("{}{}", table.name, self.suffix_table);
        let path = self.unity_binary_path(root, &file)?;
        log::info!("写入 {}", self.unity_bin_relative(&file));
        let cg = BinaryWriter::default();
        cg.write_binary(table, &path)
    }
    pub fn write_data_contract(&self, table: &XTable, root: &Path) -> XResult<()> {
        if !self.xml.enable {
            return Ok(());
        }
        let file = format!("{}{}", table.name, self.suffix_table);
        let path = self.unity_xml_path(root, &file)?;
        log::info!("写入 {}", self.unity_xml_relative(&file));
        let cg = DataContractWriter::new(&self.namespace, table, &self.suffix_table);
        cg.write_xml(&path)
    }
}

impl UnityCodegen {
    fn make_context(&self, table: &XTable) -> Context {
        let mut ctx = Context::new();
        ctx.insert("VERSION", env!("CARGO_PKG_VERSION"));
        ctx.insert("config", &self);
        ctx.insert("CLASS_NAME", &table.name);
        ctx.insert("TABLE_NAME", &format!("{}{}", table.name, self.suffix_table));
        ctx.insert("KEY", &table.data.key_field());
        ctx.insert("ID_TYPE", &table.data.key_type().as_csharp_type());
        let is_enum = table.is_enumerate();
        ctx.insert("enumerate", &is_enum);
        ctx.insert("CLASS_FIELDS", &table.data.make_class_field());
        ctx.insert("enumerate_fields", &table.data.make_enum_field());
        ctx
    }
}

#[derive(Serialize)]
struct CSharpField {
    summary: Vec<String>,
    remarks: Vec<String>,
    typing: String,
    reader: CSharpReader,
    writer: CSharpWriter,
    name: String,
    getter: String,
    default: String,
    has_default: bool,
}

#[derive(Serialize)]
struct CSharpEnum {
    name: String,
    number: String,
}

impl XTableKind {
    fn make_class_field(&self) -> Vec<CSharpField> {
        match self {
            XTableKind::Array(v) => v.headers.iter().map(|v| v.make_class_field()).collect(),
            XTableKind::Enumerate(v) => v.headers.iter().map(|v| v.make_class_field()).collect(),
            XTableKind::Class(v) => v.items.iter().map(|v| v.make_class_field()).collect(),
            XTableKind::Dictionary(_) => {
                todo!()
            }
        }
    }
    fn make_enum_field(&self) -> Vec<CSharpEnum> {
        match self {
            XTableKind::Array(_) => vec![],
            XTableKind::Enumerate(v) => {
                v.data.values().map(|v| CSharpEnum { name: v.name.to_string(), number: v.id.to_string() }).collect()
            }
            XTableKind::Class(_) => {
                vec![]
            }
            XTableKind::Dictionary(_) => {
                todo!()
            }
        }
    }
}

impl XClassItem {
    fn make_class_field(&self) -> CSharpField {
        let default = self.r#type.as_csharp_default();
        CSharpField {
            summary: self.summary.lines().map(|v| v.to_string()).collect(),
            remarks: self.details.lines().map(|v| v.to_string()).collect(),
            has_default: !default.is_empty(),
            typing: self.r#type.as_csharp_type(),
            writer: self.r#type.make_cs_binary_writer(&self.field),
            reader: self.r#type.make_cs_binary_reader(&self.field),
            name: self.field.clone(),
            getter: format!("Get{}", self.field.to_case(Case::Pascal)),
            default,
        }
    }
}

impl XCellHeader {
    fn make_class_field(&self) -> CSharpField {
        let default = self.typing.as_csharp_default();
        CSharpField {
            summary: self.summary.lines().map(|v| v.to_string()).collect(),
            remarks: self.details.lines().map(|v| v.to_string()).collect(),
            has_default: !default.is_empty(),
            typing: self.typing.as_csharp_type(),
            writer: self.typing.make_cs_binary_writer(&self.field_name),
            reader: self.typing.make_cs_binary_reader(&self.field_name),
            name: self.field_name.clone(),
            getter: format!("Get{}", self.field_name.to_case(Case::Pascal)),
            default,
        }
    }
}