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

RomManager.cs « Manager - github.com/ClusterM/hakchi2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 00ab1904801f91217989c5d915e9a45afca050ac (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace com.clusterrr.hakchi_gui.Manager
{
    public class RomManager
    {
        public string RomFolder = System.IO.Path.Combine(Program.BaseDirectoryExternal, "roms");
        public  delegate void RomModificationHandler (Rom modifiedRom);
        public event RomModificationHandler RomAdded;
        public class Rom
        {
            public override string ToString()
            {
                return DetectedName;
            }

            public Rom(string filePath)
            {
                LocalPath = filePath;
                

            }
            public string Size
            {
                get
                {
                    return Math.Round(((new System.IO.FileInfo(LocalPath)).Length / 1024.0 / 1024.0), 2).ToString() + " mB";
                }
            }
            public string Extension
            {
                get
                {
                    return System.IO.Path.GetExtension(LocalPath);
                }
            }
            public string LocalPath { get; set; }
         
            public string DetectedName
            {
                get
                {
                    string ret = System.IO.Path.GetFileNameWithoutExtension(LocalPath);
                    ret = Regex.Replace(ret, @" ?\(.*?\)", string.Empty).Trim();
                    ret = Regex.Replace(ret, @" ?\[.*?\]", string.Empty).Trim();
                    ret = ret.Replace("_", " ").Replace("  ", " ").Trim();
                    return ret;
                }
            
            }
        }
        private RomManager()
        {
            LoadLibrary();
        }
        public List<Rom> GetLibrary()
        {
            List<Rom> ret = new List<Rom>();
            ret.AddRange(_RomLibrary);
            return ret;
        }
        private void LoadLibrary()
        {
            string[] subFiles = System.IO.Directory.GetFiles(RomFolder, "*.*", System.IO.SearchOption.AllDirectories);
            foreach(string s in subFiles)
            {
                AddRom(s);
            }
        }
        private bool IsRomInLibrary(string localPath)
        {
            bool ret = false;
            foreach (Rom r in _RomLibrary)
            {
                if (localPath.ToLower() == r.LocalPath.ToLower())
                {
                    ret = true;
                    break;
                }
            }
            return ret;
        }
        public Rom GetRomByFileName(string fileName)
        {
            Rom ret = null;

            foreach (Rom r in _RomLibrary)
            {
                if (fileName.ToLower() == System.IO.Path.GetFileName(r.LocalPath.ToLower()))
                {
                    ret = r;
                    break;
                }
            }
            /*   if(ret == null)
               {
                   ret = AddRom(filePath);
               }
               */
            return ret;
        }
        public Rom GetRom(string filePath)
        {
            Rom ret = null;
           
            foreach (Rom r in _RomLibrary)
            {
                if (filePath.ToLower() == r.LocalPath.ToLower())
                {
                    ret = r;
                    break;
                }
            }
         /*   if(ret == null)
            {
                ret = AddRom(filePath);
            }
            */
            return ret;
        }
        public Rom AddRom(string filePath)
        {
            Rom ret = null;

            if (System.IO.File.Exists(filePath))
            {
                string destinationPath = System.IO.Path.Combine(RomFolder, System.IO.Path.GetExtension(filePath).Replace(".", "") + "\\" + System.IO.Path.GetFileName(filePath));
                string ext = System.IO.Path.GetExtension(filePath);
              //  if (EmulatorManager.getInstance().isFileValidRom(ext))
                {
                    if (!IsRomInLibrary(destinationPath))
                    {
                        if (destinationPath != filePath)
                        {
                            string folder = System.IO.Path.GetDirectoryName(destinationPath);
                            if (!System.IO.Directory.Exists(folder))
                            {
                                System.IO.Directory.CreateDirectory(folder);
                            }
                            if (System.IO.File.Exists(destinationPath))
                            {
                                System.IO.File.Delete(destinationPath);
                            }
                            System.IO.File.Copy(filePath, destinationPath);
                        }
                        ret = new Rom(destinationPath);

                        _RomLibrary.Add(ret);
                        if(RomAdded != null)
                        {
                            RomAdded(ret);
                        }
                    }
                    else
                    {
                        ret = GetRom(destinationPath);
                    }
                }
            }
            return ret;
        }

        private List<Rom> _RomLibrary = new List<Rom>();
        private static RomManager instance;
        public static RomManager getInstance()
        {
            if (instance == null)
            {
                instance = new RomManager();
            }
            return instance;
        }
    }
}