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

BookManager.cs « Manager - github.com/ClusterM/hakchi2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ba4c32124cc6ab901dcb6db8b1491118b209a6a0 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace com.clusterrr.hakchi_gui.Manager
{
    public class BookManager
    {
        private string BookLibraryPath = System.IO.Path.Combine(Program.BaseDirectoryExternal, "Books\\data.dat");
        private static BookManager instance;
        public static BookManager getInstance()
        {
            if(instance == null)
            {
                instance = new BookManager();
            }
            return instance;
        }
        private BookManager()
        {
            LoadSettings();
        }
        private void LoadSettings()
        {
            if (System.IO.File.Exists(BookLibraryPath))
            {
                _BookLibrary = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Book>>(System.IO.File.ReadAllText(BookLibraryPath));
            }
        }
        public void SaveSettings()
        {
            string directory = System.IO.Path.GetDirectoryName(BookLibraryPath);
            if(!System.IO.Directory.Exists(directory))
            {
                System.IO.Directory.CreateDirectory(directory);
            }
            System.IO.File.WriteAllText(BookLibraryPath, Newtonsoft.Json.JsonConvert.SerializeObject(_BookLibrary, Newtonsoft.Json.Formatting.Indented));
        }

        public Book GetBookByName(string name)
        {
            Book ret = null;
            foreach(Book b in _BookLibrary)
            {
                if(b.Name == name)
                {
                    ret = b;
                    break;
                }
            }
            if(ret == null)
            {
                ret = new Book();
                ret.Name = name;
                _BookLibrary.Add(ret);
                SaveSettings();
            }
            return ret;
        }
        public List<Book> GetLibrary()
        {
            List<Book> ret = new List<Book>();
            ret.AddRange(_BookLibrary);
            return ret;
        }
        private List<Book> _BookLibrary = new List<Book>();
        public class Book
        {
            public override string ToString()
            {
                return Name;
            }
            public Book()
            {
                Pages = new List<Page>();
            }
            public List<Page> Pages { get; set; }
            public string Name { get; set; }
            public int RootPageId { get; set; }
            private int GetNextPageId()
            {
                int currentHigh = 0;
                foreach(Page p in Pages)
                {
                    if(p.Id > currentHigh)
                    {
                        currentHigh = p.Id;
                    }
                }
                return currentHigh + 1;
            }
            public void AddPage(string name)
            {
                bool found = false;

                foreach(Page p in Pages)
                {
                    if(p.FriendlyName ==name)
                    {
                        found = true;
                    }
                }
                if(!found)
                {
                    Page p = new Page();
                    p.FriendlyName = name;
                    p.Id = GetNextPageId();
                    Pages.Add(p);
                    BookManager.getInstance().SaveSettings();
                }
            }
        }
        public class Page
        {
            public override string ToString()
            {
                return FriendlyName;
            }
            public Page()
            {
                Entries = new List<Entry>();
            }
            public string FriendlyName { get; set; }
            public List<Entry> Entries { get; set; }
            public int Id { get; set; }
        }
        public  class Entry
        {
            public Entry()
            {
                IsLink = false;
            }
            public string Label { get; set; }
            public CoverManager.Cover Cover { get; set; }
            public int PageId;

            public RomManager.Rom Rom { get; set; }
            public EmulatorManager.Emulator Emulator { get; set; }
            public bool IsLink { get; set; }
        }
       
    }
}