Sfall-ScriptEditor
384 строки · 14.7 Кб
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Windows.Forms;
5using System.Text;
6
7using ScriptEditor.TextEditorUtilities;
8using ICSharpCode.TextEditor.Util;
9
10namespace ScriptEditor
11{
12public partial class RegisterScript : Form
13{
14private static TextEditor TE;
15private Encoding lstEncoding;
16
17private class Entry
18{
19public int row;
20public string script;
21public string desc;
22public string name;
23public int vars;
24public string msglip;
25
26public Entry(int row, string line)
27{
28this.row = row;
29int descPos = line.IndexOf(';');
30int varPos = line.LastIndexOf("# local_vars=");
31script = line.Remove(descPos).Trim();
32descPos++;
33desc = line.Substring(descPos, varPos - descPos).Trim();
34int.TryParse(line.Substring(varPos + 13), out vars);
35}
36
37public string GetAsString()
38{
39return script.PadRight(16) + "; " + desc.PadRight(45) + " # local_vars=" + vars.ToString();
40}
41
42public string GetMsgAsString()
43{
44if (name != null && Settings.EncCodePage.CodePage == 866)
45name = name.Replace('\u0425', '\u0058'); //Replacement of Russian letter "X", to English letter
46return ("{" + (row + 101) + "}{" + msglip + "}{" + name + "}").PadRight(60) + "# " + script.PadRight(16) + "; " + desc;
47}
48}
49
50private struct cell
51{
52public int row;
53public int col;
54}
55
56cell SelectLine = new cell();
57
58private readonly List<string> lines;
59private readonly List<string> linesMsg;
60
61private readonly string lstPath;
62private readonly string msgPath;
63private readonly string headerPath;
64private bool cancel = false;
65private bool doAdd = false;
66private int scriptNumb = -1;
67private bool returnLine;
68private bool notSaved;
69
70private bool NotSaved
71{
72get { return notSaved; }
73set {
74Save_button.Text = "Saved";
75notSaved = value;
76if (value)
77Save_button.Image = imageList1.Images[1];
78else
79Save_button.Image = imageList1.Images[0];
80}
81}
82
83const string DESCMSG = "#\r\n# This file was built using Sfall Script Editor.\r\n#";
84const string SCRIPT_H = "SCRIPT_";
85
86private void AddRow(Entry e)
87{
88dgvScripts.Rows.Add(e, e.row + 1, e.script, e.desc, e.vars, e.name);
89dgvScripts.Rows[dgvScripts.Rows.Count - 1].Cells[1].ToolTipText = "Unpack script from .dat";
90}
91
92private RegisterScript(string script, string lst, string msg, string header)
93{
94lstPath = lst;
95msgPath = msg;
96headerPath = header;
97InitializeComponent();
98
99dgvScripts.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
100char[] space = new char[] { ' ' };
101
102// определяем кодировку
103StreamReader fsr = FileReader.OpenStream(File.OpenRead(lst), Encoding.Default);
104lstEncoding = fsr.CurrentEncoding;
105fsr.Close();
106
107lines = new List<string>(File.ReadAllLines(lst, lstEncoding));
108
109if (script != null) {
110DefinetextBox.Text = SCRIPT_H + Path.GetFileNameWithoutExtension(script).ToUpper();
111for (int i = 0; i < lines.Count; i++)
112{
113if (string.Compare(lines[i].Split(space, StringSplitOptions.RemoveEmptyEntries)[0], script, true) == 0) {
114scriptNumb = i;
115break;
116}
117} // new reg script
118if (scriptNumb == -1) {
119scriptNumb = lines.Count;
120lines.Add(script + "; # local_vars=0");
121doAdd = true;
122AllowCheckBox.Checked = Settings.allowDefine;
123Save_button.Image = imageList1.Images[1];
124notSaved = true;
125}
126AllowCheckBox.Enabled = true;
127DefinetextBox.Enabled = true;
128} else
129NotSaved = false;
130
131Entry[] entries = new Entry[lines.Count];
132for (int i = 0; i < lines.Count; i++)
133entries[i] = new Entry(i, lines[i]);
134lines.Clear();
135if (msgPath != null) {
136linesMsg = new List<string>(File.ReadAllLines(msg, Settings.EncCodePage));
137for (int i = 0; i < linesMsg.Count; i++)
138{
139string[] line = linesMsg[i].Split('}');
140if (line.Length != 4)
141continue;
142line[0] = line[0].TrimStart(' ', '{');
143int lineno;
144if (!int.TryParse(line[0], out lineno)) continue;
145lineno -= 101;
146if (lineno >= entries.Length) continue;
147entries[lineno].name = line[2].TrimStart(' ', '{');
148entries[lineno].msglip = line[1].TrimStart(' ', '{');
149}
150linesMsg.Clear();
151}
152for (int i = 0; i < entries.Length; i++)
153{
154AddRow(entries[i]);
155}
156if (scriptNumb != -1) {
157dgvScripts.FirstDisplayedScrollingRowIndex = (scriptNumb <= 5) ? scriptNumb : scriptNumb - 5;
158dgvScripts.Rows[scriptNumb].Selected = true;
159}
160dgvScripts.CellValueChanged += dgvScripts_CellValueChanged;
161}
162
163public static void Registration(string script)
164{
165if (Settings.outputDir == null) {
166MessageBox.Show("No output path selected.", "Error");
167return;
168}
169string lstPath = Path.Combine(Settings.outputDir, "scripts.lst");
170if (!File.Exists(lstPath)) {
171MessageBox.Show("scripts.lst does not exist in your scripts output directory", "Error");
172return;
173}
174string msgPath = Path.Combine(Settings.outputDir, "../text/english/game/scrname.msg");
175if (!File.Exists(msgPath)) {
176if (Settings.showWarnings) MessageBox.Show("Could not find file scrname.msg", "Warning");
177msgPath = null;
178}
179string scriptH = null;
180if (Settings.pathScriptsHFile == null) {
181if (Settings.showWarnings) MessageBox.Show("The path to scripts.h has not been set.", "Warning");
182} else {
183scriptH = Settings.pathScriptsHFile;
184if (!File.Exists(scriptH)) scriptH = null;
185}
186// Show form
187TE = (TextEditor)ActiveForm;
188TE.RegistredScriptDialogShow = true;
189(new RegisterScript(script, lstPath, msgPath, scriptH)).Show(ActiveForm);
190}
191
192private void RegisterScript_FormClosing(object sender, FormClosingEventArgs e)
193{
194if (cancel) return;
195if (NotSaved) {
196if (MessageBox.Show("Save all changed to files?", "Warning", MessageBoxButtons.YesNo) == DialogResult.Yes) {
197Save_button_Click(null, null);
198}
199}
200UndatFile.selectDatFile = null;
201TE.RegistredScriptDialogShow = false;
202}
203
204private void Save_button_Click(object sender, EventArgs e)
205{
206if (!NotSaved) return;
207dgvScripts.EndEdit();
208Entry[] entries = new Entry[dgvScripts.Rows.Count];
209for (int i = 0; i < entries.Length; i++)
210{
211int index = (int)dgvScripts.Rows[i].Cells[1].Value;
212entries[index - 1] = (Entry)dgvScripts.Rows[i].Cells[0].Value;
213}
214
215foreach (Entry entry in entries)
216lines.Add(entry.GetAsString());
217
218File.WriteAllLines(lstPath, lines.ToArray(), (FileReader.IsUnicode(lstEncoding)) ? new UTF8Encoding(false) : lstEncoding);
219lines.Clear();
220
221if (msgPath != null) {
222linesMsg.Add(DESCMSG);
223foreach (Entry entry in entries)
224linesMsg.Add(entry.GetMsgAsString());
225
226File.WriteAllLines(msgPath, linesMsg.ToArray(), Settings.EncCodePage);
227linesMsg.Clear();
228}
229
230NotSaved = false;
231
232if (AllowCheckBox.Checked && headerPath == null) {
233MessageBox.Show("The definition was not added in header file.\nCould not find file scripts.h", "Script header error");
234return;
235}
236if (doAdd && AllowCheckBox.Checked) {
237Entry entry = entries[scriptNumb];
238List<string> hlines = new List<string>(File.ReadAllLines(headerPath));
239for (int j = hlines.Count - 1; j >= 0; j--)
240{
241if (hlines[j].IndexOf('#') != -1 || j == 0) {
242hlines.Insert(j, ("#define " + DefinetextBox.Text.ToUpperInvariant()).PadRight(32) + ("(" + (entry.row + 1) + ")").PadRight(8) + "// " + entry.script.PadRight(16) + "; " + entry.desc);
243break;
244}
245}
246File.WriteAllLines(headerPath, hlines.ToArray());
247doAdd = false;
248}
249}
250
251private void RegisterScript_KeyDown(object sender, KeyEventArgs e)
252{
253if (e.KeyCode == Keys.Escape) {
254cancel = true;
255e.Handled = true;
256Close();
257}
258}
259
260private void dgvScripts_CellValueChanged(object sender, DataGridViewCellEventArgs e)
261{
262if (e.RowIndex == -1 || e.ColumnIndex == -1 || returnLine)
263return;
264DataGridViewCell cell = dgvScripts.Rows[e.RowIndex].Cells[e.ColumnIndex];
265Entry entry = (Entry)dgvScripts.Rows[e.RowIndex].Cells[0].Value;
266string val = (string)cell.Value;
267if (val == null) val = "";
268switch (e.ColumnIndex) {
269case 2:
270if (Path.GetFileNameWithoutExtension(val).Length > 8) {
271MessageBox.Show("Script file names must be 8 characters.", "Name Error");
272returnLine = true;
273cell.Value = entry.script;
274break;
275}
276entry.script = val;
277break;
278case 3:
279entry.desc = val;
280break;
281case 4:
282int.TryParse(val, out entry.vars);
283break;
284case 5:
285if (val.IndexOfAny(new char[] { '{', '}' }) == -1)
286entry.name = val;
287break;
288}
289if (!returnLine) NotSaved = true;
290returnLine = false;
291}
292
293private cell Finds(int rowStart, int colStart, int rev = 1)
294{
295cell cell = new cell();
296string find_str = FindtextBox.Text.Trim();
297if (find_str.Length == 0) return cell;
298if (rev == -1 && rowStart == 0) rowStart = dgvScripts.RowCount - 1;
299for (int row = rowStart; row < dgvScripts.RowCount; row += rev)
300{
301if (row < 0) break;
302for (int col = colStart; col < dgvScripts.ColumnCount; col++)
303{
304if (dgvScripts.Rows[row].Cells[col].Value == null)
305continue;
306string value = dgvScripts.Rows[row].Cells[col].Value.ToString();
307if (value.IndexOf(find_str, 0, StringComparison.OrdinalIgnoreCase) != -1) {
308cell.row = row;
309cell.col = col;
310break;
311}
312}
313if (cell.col != 0) break;
314colStart = 1;
315}
316if (cell.col != 0) {
317dgvScripts.FirstDisplayedScrollingRowIndex = (cell.row <= 5) ? cell.row : cell.row - 5;
318dgvScripts.Rows[cell.row].Cells[cell.col].Selected = true;
319}
320return cell;
321}
322
323private void Addbutton_Click(object sender, EventArgs e)
324{
325dgvScripts.Sort(dgvScripts.Columns[1], System.ComponentModel.ListSortDirection.Ascending);
326Entry[] entries = new Entry[1];
327entries[0] = new Entry(dgvScripts.RowCount, "none.int; # local_vars=");
328AddRow(entries[0]);
329dgvScripts.FirstDisplayedScrollingRowIndex = dgvScripts.RowCount - 1;
330NotSaved = true;
331}
332
333private void Delbutton_Click(object sender, EventArgs e)
334{
335dgvScripts.Sort(dgvScripts.Columns[1], System.ComponentModel.ListSortDirection.Ascending);
336dgvScripts.Rows.RemoveAt(dgvScripts.RowCount - 1);
337dgvScripts.FirstDisplayedScrollingRowIndex = dgvScripts.RowCount - 1;
338NotSaved = true;
339}
340
341private void Downbutton_Click(object sender, EventArgs e)
342{
343cell curfind = Finds(SelectLine.row, SelectLine.col + 1);
344if (curfind.col == 0) MessageBox.Show("Nothing found.");
345else SelectLine = curfind;
346}
347
348private void Upbutton_Click(object sender, EventArgs e)
349{
350cell curfind = Finds(SelectLine.row, SelectLine.col + 1, -1);
351if (curfind.col == 0) MessageBox.Show("Nothing found.");
352else SelectLine = curfind;
353}
354
355private void dgvScripts_CellClick(object sender, DataGridViewCellEventArgs e)
356{
357SelectLine.col = e.ColumnIndex;
358SelectLine.row = e.RowIndex;
359}
360
361private void dgvScripts_CellContentClick(object sender, DataGridViewCellEventArgs e)
362{
363if (e.ColumnIndex == 1 && e.RowIndex > -1)
364OpenScript(dgvScripts.Rows[e.RowIndex].Cells[2].Value.ToString());
365}
366
367private void OpenScript(string scriptName)
368{
369if (Settings.outputDir == null)
370return;
371
372string pathScript = Path.Combine(Settings.outputDir, scriptName);
373if (!File.Exists(pathScript)) {
374var undat = new UndatFile();
375if (!undat.UnpackFile(ref pathScript)) {
376if (pathScript != null)
377MessageBox.Show("Unpack script file error.", "Open Script");
378return;
379}
380}
381TE.Open(pathScript, TextEditor.OpenType.File, false, outputFolder : true);
382}
383}
384}
385