add Information MessageBox when splitting is done
[GitHub/mt8127/ROM0Split.git] / ROM0Split / Model / MainWindowModel.cs
CommitLineData
3004d53f
JA
1using System;
2using ROM0Split.Implementations;
3using Microsoft.Win32;
4using System.IO;
5using System.Windows;
6using YamlDotNet.RepresentationModel;
7
8namespace ROM0Split.Model
9{
10 class MainWindowModel : SuperViewModel
11 {
12 const int BUFFER_SIZE = 20 * 1024;
13 private string _scatterFile;
14 private string _rom0File;
15 private DelegateCommand _scatterFileDialog;
16 private DelegateCommand _rom0FileDialog;
17 private DelegateCommand _split;
18
19 public string scatterFile
20 {
21 get { return _scatterFile; }
22 set { SetProperty(ref _scatterFile, value); }
23 }
24
25 public string rom0File
26 {
27 get { return _rom0File; }
28 set { SetProperty(ref _rom0File, value); }
29 }
30
31 public DelegateCommand scatterFileDialog
32 {
33 get { return _scatterFileDialog; }
34 set { SetProperty(ref _scatterFileDialog, value); }
35 }
36
37 public DelegateCommand rom0FileDialog
38 {
39 get { return _rom0FileDialog; }
40 set { SetProperty(ref _rom0FileDialog, value); }
41 }
42
43 public DelegateCommand split
44 {
45 get { return _split; }
46 set { SetProperty(ref _split, value); }
47 }
48
49 private void OnscatterFileDialogExecute()
50 {
51 OpenFileDialog openFileDialog = new OpenFileDialog();
52 openFileDialog.ShowDialog();
53 scatterFile = openFileDialog.FileName;
54 openFileDialog = null;
55 }
56
57 private void Onrom0FileDialogExecute()
58 {
59 OpenFileDialog openFileDialog = new OpenFileDialog();
60 openFileDialog.ShowDialog();
61 rom0File = openFileDialog.FileName;
62 openFileDialog = null;
63 }
64
65 private void OnsplitExecute()
66 {
67 if (scatterFile == null)
68 {
69 MessageBox.Show("Please select a Scatter file", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
70 }
71 else if (rom0File == null)
72 {
73 MessageBox.Show("Please select a ROM_0 file", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
74 }
75 else
76 {
77 string basepath = System.AppDomain.CurrentDomain.BaseDirectory + "\\out";
78 FileStream stream = File.OpenRead(rom0File);
79 StringReader reader = new StringReader(File.ReadAllText(scatterFile));
80
81 if (!Directory.Exists(basepath))
82 Directory.CreateDirectory(basepath);
83
84 // Load the stream
85 var yaml = new YamlStream();
86 yaml.Load(reader);
87
88 // Examine the stream
89 var mapping = (YamlSequenceNode)yaml.Documents[0].RootNode;
90 foreach (YamlMappingNode entry in mapping.Children)
91 {
92 var node = entry.Children;
93 //Console.WriteLine(entry.Children);
94 if (node.ContainsKey("general") || node["partition_name"].ToString() == "PRELOADER")
95 continue;
96
97
98 string fileName = node["file_name"].ToString();
99 if (fileName == "NONE")
100 fileName = node["partition_name"].ToString();
101
102 byte[] buffer = new byte[BUFFER_SIZE];
103
104 Stream outFile = File.Create(basepath + "\\" + fileName);
105
106 stream.Position =
107 long.Parse(node["linear_start_addr"].ToString().Replace("0x", ""), System.Globalization.NumberStyles.HexNumber);
108
109 UInt64 size = UInt64.Parse(node["partition_size"].ToString().Replace("0x", ""), System.Globalization.NumberStyles.HexNumber);
110 UInt64 remaining = size;
111 int bytesRead;
112 while (remaining > 0 && (bytesRead = stream.Read(buffer, 0,
3b8e40a1 113 int.Parse(Math.Min(remaining, BUFFER_SIZE).ToString()))) > 0)
3004d53f
JA
114 {
115 outFile.Write(buffer, 0, bytesRead);
116 remaining -= (uint)bytesRead;
117 }
118
119 outFile.Flush();
120 outFile.Close();
121 outFile.Dispose();
122 Console.WriteLine(node["partition_index"]);
123 }
3b8e40a1
JA
124
125 MessageBox.Show("Splitting Done", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
3004d53f
JA
126 }
127 }
128
129 private void OndecryptExecute()
130 {
131 }
132
133 public MainWindowModel()
134 {
135 scatterFileDialog = new DelegateCommand(new Action(OnscatterFileDialogExecute));
136 rom0FileDialog = new DelegateCommand(new Action(Onrom0FileDialogExecute));
137 split = new DelegateCommand(new Action(OnsplitExecute));
138 }
139 }
140}