Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SelectModsForm.cs - github.com/ClusterM/hakchi2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a900f18be9b7da9b327f646be347cdd223119792 (plain)
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
180
181
182
183
184
185
186
187
188
using SevenZip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace com.clusterrr.hakchi_gui
{
    public partial class SelectModsForm : Form
    {
        private readonly string usermodsDirectory;
        private readonly string[] readmeFiles;

        public SelectModsForm(bool loadInstalledMods, bool allowDropMods, string[] filesToAdd = null)
        {
            InitializeComponent();
            usermodsDirectory = Path.Combine(Program.BaseDirectoryExternal, "user_mods");
            var modsList = new List<string>();
            if (loadInstalledMods && MainForm.Clovershell.IsOnline)
            {
                var modsstr = MainForm.Clovershell.ExecuteSimple("ls /var/lib/hakchi/hmod/uninstall-*", 1000, true);
                var installedMods = modsstr.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var mod in installedMods)
                {
                    var modname = mod;
                    int pos;
                    while ((pos = modname.IndexOf("/")) >= 0)
                        modname = modname.Substring(pos + 1);
                    modname = modname.Substring("uninstall-".Length);
                    if (MainForm.InternalMods.Contains(modname))
                        continue;
                    modsList.Add(modname);
                }
            }
            else
            {
                if (Directory.Exists(usermodsDirectory))
                {
                    modsList.AddRange(from m
                                      in Directory.GetDirectories(usermodsDirectory, "*.hmod", SearchOption.TopDirectoryOnly)
                                      select Path.GetFileNameWithoutExtension(m));
                    modsList.AddRange(from m
                                      in Directory.GetFiles(usermodsDirectory, "*.hmod", SearchOption.TopDirectoryOnly)
                                      select Path.GetFileNameWithoutExtension(m));
                }
            }
            readmeFiles = new string[] { "readme.txt", "readme.md", "readme" };
            checkedListBoxMods.Items.Clear();
            checkedListBoxMods.Items.AddRange(modsList.ToArray());
            checkedListBoxMods.Sorted = true;
            if (filesToAdd != null) AddMods(filesToAdd);
            this.AllowDrop = allowDropMods;
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (checkedListBoxMods.CheckedItems.Count > 0)
                DialogResult = DialogResult.OK;
            else
                DialogResult = DialogResult.Cancel;
            Close();
        }

        private void checkedListBoxMods_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (checkedListBoxMods.SelectedItem != null)
                new Thread(loadReadMe).Start(checkedListBoxMods.SelectedItem.ToString());
            else
            {
                textBoxReadme.Text = "";
                textBoxReadme.Enabled = false;
            }
        }

        void loadReadMe(object obj)
        {
            try
            {
                var selected = obj as string;
                var text = "";
                var dir = Path.Combine(usermodsDirectory, selected + ".hmod");
                if (Directory.Exists(dir))
                {
                    foreach (var f in readmeFiles)
                    {
                        var fn = Path.Combine(dir, f);
                        if (File.Exists(fn))
                        {
                            text = File.ReadAllText(fn);
                            break;
                        }
                    }
                }
                else if (File.Exists(dir))
                {
                    SevenZipExtractor.SetLibraryPath(Path.Combine(Program.BaseDirectoryInternal, IntPtr.Size == 8 ? @"tools\7z64.dll" : @"tools\7z.dll"));
                    using (var szExtractor = new SevenZipExtractor(dir))
                    {
                        var tar = new MemoryStream();
                        szExtractor.ExtractFile(0, tar);
                        tar.Seek(0, SeekOrigin.Begin);
                        using (var szExtractorTar = new SevenZipExtractor(tar))
                        {
                            foreach (var f in szExtractorTar.ArchiveFileNames)
                            {
                                if (readmeFiles.Contains(f.ToLower()))
                                {
                                    var o = new MemoryStream();
                                    szExtractorTar.ExtractFile(f, o);
                                    text = Encoding.UTF8.GetString(o.ToArray());
                                    if (!text.Contains("\r"))
                                        text = text.Replace("\n", "\r\n");
                                    break;
                                }
                            }
                        }
                    }
                }
                Invoke(new Action<string, string>(showReadMe), new object[] { selected, text });
            }
            catch
            {
            }
        }

        void showReadMe(string mod, string readme)
        {
            if (checkedListBoxMods.SelectedItem != null &&
                checkedListBoxMods.SelectedItem.ToString() == mod)
            {
                textBoxReadme.Text = readme;
                textBoxReadme.Enabled = readme.Length > 0;
            }
        }

        private void SelectModsForm_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
        }

        private void SelectModsForm_DragDrop(object sender, DragEventArgs e)
        {
            var files = (string[])e.Data.GetData(DataFormats.FileDrop);
            AddMods(files);
        }

        private void AddMods(string[] files)
        {
            var listAddedMods = new List<string>();
            foreach (var file in files)
            {
                var ext = Path.GetExtension(file).ToLower();
                if (ext == ".hmod")
                {
                    var target = Path.Combine(usermodsDirectory, Path.GetFileName(file));
                    if (file != target)
                        File.Copy(file, target, true);
                    listAddedMods.Add(Path.GetFileNameWithoutExtension(file));
                }
                else if (ext == ".7z" || ext == ".zip" || ext == ".rar")
                {
                    SevenZipExtractor.SetLibraryPath(Path.Combine(Program.BaseDirectoryInternal, IntPtr.Size == 8 ? @"tools\7z64.dll" : @"tools\7z.dll"));
                    using (var szExtractor = new SevenZipExtractor(file))
                    {
                        foreach (var f in szExtractor.ArchiveFileNames)
                            if (Path.GetExtension(f).ToLower() == ".hmod")
                            {
                                using (var outFile = new FileStream(Path.Combine(usermodsDirectory, Path.GetFileName(f)), FileMode.Create))
                                {
                                    szExtractor.ExtractFile(f, outFile);
                                    listAddedMods.Add(Path.GetFileNameWithoutExtension(f));
                                }
                            }
                    }
                }
            }
            foreach (var mod in listAddedMods)
            {
                checkedListBoxMods.Items.Remove(mod);
                checkedListBoxMods.Items.Add(mod, true);
            }
        }
    }
}