Elin Decompiled Documentation EA 23.271 Nightly
Loading...
Searching...
No Matches
BookList.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using System.IO;
3using System.Linq;
4
5public class BookList
6{
7 public class Item
8 {
9 public string[] lines;
10
11 public string title;
12
13 public string author;
14
15 public string id;
16
17 public string cat;
18
19 public int chance = 100;
20
21 public int skin;
22 }
23
24 public static Dictionary<string, Dictionary<string, Item>> dict;
25
26 public static void Init()
27 {
28 if (dict == null)
29 {
30 dict = new Dictionary<string, Dictionary<string, Item>>();
31 AddDir("Book", CorePath.CorePackage.Book);
32 AddDir("Scroll", CorePath.CorePackage.Scroll);
33 }
34 static void AddDir(string id, string path)
35 {
36 DirectoryInfo directoryInfo = new DirectoryInfo(path);
37 Dictionary<string, Item> dictionary = new Dictionary<string, Item>();
38 dict.Add(id, dictionary);
39 FileInfo[] files = directoryInfo.GetFiles();
40 foreach (FileInfo fileInfo in files)
41 {
42 if (!(fileInfo.Extension != ".txt"))
43 {
44 StreamReader streamReader = new StreamReader(fileInfo.FullName);
45 string[] array = streamReader.ReadLine().Split(',');
46 Item item = new Item
47 {
48 cat = id,
49 title = array[0],
50 author = ((array.Length >= 2 && !array[1].IsEmpty()) ? "nameAuthor".lang(array[1]) : "unknownAuthor".lang()),
51 chance = ((array.Length >= 3) ? array[2].ToInt() : 100),
52 skin = ((array.Length >= 4) ? array[3].ToInt() : 0),
53 id = fileInfo.Name.Replace(fileInfo.Extension, "")
54 };
55 dictionary.Add(item.id, item);
56 streamReader.Close();
57 }
58 }
59 }
60 }
61
62 public static Item GetRandomItem(string idCat = "Book")
63 {
64 Init();
65 return dict[idCat].Where((KeyValuePair<string, Item> p) => p.Value.chance != 0).ToList().RandomItemWeighted((KeyValuePair<string, Item> p) => p.Value.chance)
66 .Value;
67 }
68
69 public static Item GetItem(string id, string idCat = "Book")
70 {
71 Init();
72 return dict[idCat].TryGetValue(id) ?? dict["Book"]["_default"];
73 }
74}
item3. title
Definition: UIBook.cs:616
string[] lines
Definition: BookList.cs:9
string author
Definition: BookList.cs:13
string id
Definition: BookList.cs:15
string cat
Definition: BookList.cs:17
string title
Definition: BookList.cs:11
static void Init()
Definition: BookList.cs:26
static Item GetRandomItem(string idCat="Book")
Definition: BookList.cs:62
static Item GetItem(string id, string idCat="Book")
Definition: BookList.cs:69
static Dictionary< string, Dictionary< string, Item > > dict
Definition: BookList.cs:24
static string Scroll
Definition: CorePath.cs:75
static string Book
Definition: CorePath.cs:71