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
pub mod assembly;
pub mod csharp;
pub mod csv;
pub mod hpp;
pub mod output;
pub mod rust;

use assembly::Assembly;
use csharp::CSharp;
use csv::Csv;
use hpp::Hpp;
use rust::Rust;

use core::fmt;

use std::{
  ffi::OsStr,
  fs::File,
  io::{self, BufRead},
  path::Path,
};

extern crate log;

pub struct CodeGen {
  pub file_path: String,
  pub export_path: String,
  pub input_file_type: ParseTargets,
  pub export_file_type: WriteTargets,
  pub data: ParsedData,
}

pub enum ParseTargets {
  UNKNOWN,
  HPP,
  CSV,
}

pub enum WriteTargets {
  ASM,
  CSHARP,
  CSV,
  RUST,
}

fn get_extension_from_filename(filename: &str) -> Option<&str> {
  Path::new(filename).extension().and_then(OsStr::to_str)
}

fn get_type_from_extension(extension: &str) -> ParseTargets {
  match extension {
    "hpp" => ParseTargets::HPP,
    "csv" => ParseTargets::CSV,
    _ => ParseTargets::UNKNOWN,
  }
}

fn get_type(file_path: &str) -> ParseTargets {
  get_type_from_extension(get_extension_from_filename(file_path).unwrap())
}

#[derive(Clone)]
pub struct NwOffset {
  pub name: String,
  pub offset: String,
}

#[derive(Clone)]
pub enum Platform {
  X86,
  ARM,
  UNDEFINED,
}

impl Platform {
  pub fn from_str(s: &str) -> Platform {
    match s {
      "x86" => Platform::X86,
      "arm" => Platform::ARM,
      "X86" => Platform::X86,
      "ARM" => Platform::ARM,
      _ => Platform::UNDEFINED,
    }
  }
}

impl fmt::Display for Platform {
  fn fmt(&self,
         f: &mut fmt::Formatter)
         -> fmt::Result {
    match self {
      Platform::X86 => write!(f, "X86"),
      Platform::ARM => write!(f, "ARM"),
      Platform::UNDEFINED => write!(f, "UNDEFINED"),
    }
  }
}

pub struct ParsedData {
  pub major_version: u16,
  pub minor_version: u16,
  pub platform: Platform,
  pub offsets: Vec<NwOffset>,
}

impl CodeGen {
  pub fn new(input_file_path: &String,
             export_path: &String,
             _type: WriteTargets)
             -> Self {
    let input_file_type = get_type(input_file_path);
    let data: ParsedData = match input_file_type {
      ParseTargets::UNKNOWN => {
        println!("Unknown file type");
        ParsedData { major_version: 0,
                     minor_version: 0,
                     platform: Platform::UNDEFINED,
                     offsets: Vec::new() }
      }
      ParseTargets::HPP => {
        println!("Parsing hpp file");
        Hpp::parse(input_file_path, hpp::Type::Functions)
      }
      ParseTargets::CSV => {
        println!("Parsing csv file");
        Csv::parse(input_file_path)
      }
    };

    CodeGen { file_path: input_file_path.to_owned(),
              export_path: export_path.to_owned(),
              data,
              input_file_type: input_file_type,
              export_file_type: _type }
  }

  pub fn write(&self) {
    match self.export_file_type {
      WriteTargets::ASM => {
        Assembly::write(&self.data, &self.export_path);
      }
      WriteTargets::CSHARP => {
        CSharp::write(&self.data, &self.export_path);
      }
      WriteTargets::CSV => {
        Csv::write(&self.data, &self.export_path);
      }
      WriteTargets::RUST => {
        Rust::write(&self.data, &self.export_path);
      }
    };
  }
}

pub fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
  where P: AsRef<Path>
{
  let file = File::open(filename)?;
  Ok(io::BufReader::new(file).lines())
}