Skip to content

Commit

Permalink
Replaced manga.txt with manga.json for easier extensability
Browse files Browse the repository at this point in the history
- Requires a manual switch of files
- Continued work on allowing multiple sources
  • Loading branch information
9vult committed Apr 9, 2020
1 parent 33021bc commit 24923fd
Show file tree
Hide file tree
Showing 17 changed files with 859 additions and 2,744 deletions.
24 changes: 22 additions & 2 deletions MikuReader-Core/Core/Managers/DatabaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,32 @@ private void Populate()
switch (Title.GetType(dir))
{
case TitleType.MANGA:
mangaDB.Add(new MangaDex(dir, MangaType.MANGADEX));
switch (Manga.GetSource(dir))
{
case MangaType.MANGADEX:
mangaDB.Add(new MangaDex(dir));
break;
case MangaType.KISSMANGA:
mangaDB.Add(new KissManga(dir));
break;
case MangaType.NULL:
default:
break;
}
break;

case TitleType.HENTAI:
hentaiDB.Add(new Nhentai(dir, HentaiType.NHENTAI));
switch (Hentai.GetSource(dir))
{
case HentaiType.NHENTAI:
hentaiDB.Add(new Nhentai(dir));
break;
case HentaiType.NULL:
default:
break;
}
break;

case TitleType.NULL:
default:
break;
Expand Down
20 changes: 20 additions & 0 deletions MikuReader-Core/Core/Model/Hentai.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,25 @@ public abstract class Hentai : Title
public abstract override string GetID();

public abstract override bool IsDownloading();

public static HentaiType GetSource(DirectoryInfo dir)
{
try
{
string input = File.ReadAllText(FileHelper.GetFilePath(dir, "manga.json"));
MangaInfo info = JsonConvert.DeserializeObject<MangaInfo>(input);
switch (info.Source)
{
case "nhentai":
return HentaiType.NHENTAI;
default:
return HentaiType.NULL;
}
}
catch (Exception)
{
return HentaiType.NULL;
}
}
}
}
22 changes: 22 additions & 0 deletions MikuReader-Core/Core/Model/Manga.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,27 @@ public abstract class Manga : Title

public abstract String[] GetDLChapters();

public static MangaType GetSource(DirectoryInfo dir)
{
try
{
string input = File.ReadAllText(FileHelper.GetFilePath(dir, "manga.json"));
MangaInfo info = JsonConvert.DeserializeObject<MangaInfo>(input);
switch (info.Source)
{
case "mangadex":
return MangaType.MANGADEX;
case "kissmanga":
return MangaType.KISSMANGA;
default:
return MangaType.NULL;
}
}
catch (Exception)
{
return MangaType.NULL;
}
}

}
}
20 changes: 20 additions & 0 deletions MikuReader-Core/Core/Model/MangaInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace MikuReader.Core
{
public class MangaInfo
{
public string Type { get; set; }
public string Source { get; set; }
public string Id { get; set; }
public string Name { get; set; }
public string LangCode { get; set; }
public string Group { get; set; }
public string UserName { get; set; }
public string Chapter { get; set; }
public string Page { get; set; }
public string Latest { get; set; }
}
}
7 changes: 5 additions & 2 deletions MikuReader-Core/Core/Model/Title.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -85,7 +86,9 @@ public static TitleType GetType(DirectoryInfo dir)
{
try
{
switch (File.ReadAllLines(FileHelper.GetFilePath(dir, "manga.txt"))[0])
string input = File.ReadAllText(FileHelper.GetFilePath(dir, "manga.json"));
MangaInfo info = JsonConvert.DeserializeObject<MangaInfo>(input);
switch (info.Type)
{
case "manga":
return TitleType.MANGA;
Expand Down
2 changes: 1 addition & 1 deletion MikuReader-Core/Core/Title/HentaiType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace MikuReader.Core
{
public enum HentaiType
{
NHENTAI
NHENTAI, NULL
}
}
83 changes: 46 additions & 37 deletions MikuReader-Core/Core/Title/KissManga/KissManga.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,13 @@ public class KissManga : Manga
private List<Chapter> chapters;
private DirectoryInfo mangaRoot;

private readonly MangaType type;

/// <summary>
/// Create a new Manga when the files exist
/// </summary>
/// <param name="location">Root directory for this Manga</param>
public KissManga(DirectoryInfo location, MangaType type)
public KissManga(DirectoryInfo location)
{
this.mangaRoot = location;
this.type = type;
chapters = new List<Chapter>();
_Load(true);
}
Expand All @@ -47,11 +44,10 @@ public KissManga(DirectoryInfo location, MangaType type)
/// </summary>
/// <param name="location">Root directory for this Manga</param>
/// <param name="mangaUrl">MangaDex URL</param>
public KissManga(DirectoryInfo location, string mangaUrl, MangaType type)
public KissManga(DirectoryInfo location, string mangaUrl)
{
this.mangaRoot = location;
chapters = new List<Chapter>();
this.type = type;
_Create(mangaUrl);
_Load(true);
}
Expand All @@ -61,17 +57,17 @@ public KissManga(DirectoryInfo location, string mangaUrl, MangaType type)
/// </summary>
public override void _Load(bool doChapters)
{
string[] info = File.ReadAllLines(FileHelper.GetFilePath(mangaRoot, "manga.txt"));
if (info.Length < 9) { throw new FileLoadException("'manga.txt' did not contain all required fields!"); }
// info[0] is the type identifier
id = info[1];
name = info[2];
userlang = info[3];
usergroup = info[4];
usertitle = info[5];
currentchapter = info[6];
currentpage = info[7];
lastchapter = info[8];
string input = File.ReadAllText(FileHelper.GetFilePath(mangaRoot, "manga.json"));
MangaInfo info = JsonConvert.DeserializeObject<MangaInfo>(input);

id = info.Id;
name = info.Name;
userlang = info.LangCode;
usergroup = info.Group;
usertitle = info.UserName;
currentchapter = info.Chapter;
currentpage = info.Page;
lastchapter = info.Latest;

if (doChapters)
_PopulateChapters();
Expand All @@ -88,16 +84,23 @@ public override void _Create(string mangaUrl)
string lang_code = "gb"; // Not that it matters for KissManga

FileHelper.CreateFolder(FileHelper.APP_ROOT, KissMangaHelper.GetUrlName(mangaUrl));
File.WriteAllLines(Path.Combine(mangaRoot.FullName, "manga.txt"), new string[] {
"manga",
KissMangaHelper.GetUrlName(mangaUrl),
title,
lang_code, // TODO: Custom user languages
"^any-group", // TODO: Custom user groups
title, // TODO: Custom user title
"1", "1", // Chapter 1, page 1
"1" // TODO: Get latest chapter for language and group
});

MangaInfo info = new MangaInfo()
{
Type = "manga",
Source = "kissmanga",
Id = KissMangaHelper.GetUrlName(mangaUrl),
Name = title,
LangCode = lang_code,
Group = "^any-group",
UserName = title,
Chapter = "1",
Page = "1",
Latest = "1"
};

string output = JsonConvert.SerializeObject(info);
File.WriteAllText(Path.Combine(mangaRoot.FullName, "manga.json"), output);

_Load(false);
GetSetPrunedChapters(true);
Expand Down Expand Up @@ -173,16 +176,22 @@ public override void Save(string chapter, string page)
this.currentchapter = chapter;
this.currentpage = page;

File.WriteAllLines(Path.Combine(mangaRoot.FullName, "manga.txt"), new string[] {
"manga",
mangaRoot.Name,
name,
userlang,
usergroup,
usertitle,
chapter, page,
lastchapter // TODO: Get latest chapter for language and group
});
MangaInfo info = new MangaInfo()
{
Type = "manga",
Source = "kissmanga",
Id = mangaRoot.Name,
Name = name,
LangCode = userlang,
Group = usergroup,
UserName = usertitle,
Chapter = chapter,
Page = page,
Latest = lastchapter
};

string output = JsonConvert.SerializeObject(info);
File.WriteAllText(Path.Combine(mangaRoot.FullName, "manga.json"), output);
}

public override void UpdateProperties(string title, string lang, string group)
Expand Down
83 changes: 46 additions & 37 deletions MikuReader-Core/Core/Title/MangaDex/MangaDex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,13 @@ public class MangaDex : Manga
private List<Chapter> chapters;
private DirectoryInfo mangaRoot;

private readonly MangaType type;

/// <summary>
/// Create a new Manga when the files exist
/// </summary>
/// <param name="location">Root directory for this Manga</param>
public MangaDex(DirectoryInfo location, MangaType type)
public MangaDex(DirectoryInfo location)
{
this.mangaRoot = location;
this.type = type;
chapters = new List<Chapter>();
_Load(true);
}
Expand All @@ -47,11 +44,10 @@ public MangaDex(DirectoryInfo location, MangaType type)
/// </summary>
/// <param name="location">Root directory for this Manga</param>
/// <param name="mangaUrl">MangaDex URL</param>
public MangaDex(DirectoryInfo location, string mangaUrl, MangaType type)
public MangaDex(DirectoryInfo location, string mangaUrl)
{
this.mangaRoot = location;
chapters = new List<Chapter>();
this.type = type;
_Create(mangaUrl);
_Load(true);
}
Expand All @@ -61,17 +57,17 @@ public MangaDex(DirectoryInfo location, string mangaUrl, MangaType type)
/// </summary>
public override void _Load(bool doChapters)
{
string[] info = File.ReadAllLines(FileHelper.GetFilePath(mangaRoot, "manga.txt"));
if (info.Length < 9) { throw new FileLoadException("'manga.txt' did not contain all required fields!"); }
// info[0] is the type identifier
id = info[1];
name = info[2];
userlang = info[3];
usergroup = info[4];
usertitle = info[5];
currentchapter = info[6];
currentpage = info[7];
lastchapter = info[8];
string input = File.ReadAllText(FileHelper.GetFilePath(mangaRoot, "manga.json"));
MangaInfo info = JsonConvert.DeserializeObject<MangaInfo>(input);

id = info.Id;
name = info.Name;
userlang = info.LangCode;
usergroup = info.Group;
usertitle = info.UserName;
currentchapter = info.Chapter;
currentpage = info.Page;
lastchapter = info.Latest;

if (doChapters)
_PopulateChapters();
Expand All @@ -92,16 +88,23 @@ public override void _Create(string mangaUrl)
string lang_code = "gb";

FileHelper.CreateFolder(FileHelper.APP_ROOT, MangaDexHelper.GetMangaID(mangaUrl));
File.WriteAllLines(Path.Combine(mangaRoot.FullName, "manga.txt"), new string[] {
"manga",
MangaDexHelper.GetMangaID(mangaUrl),
title,
lang_code, // TODO: Custom user languages
"^any-group", // TODO: Custom user groups
title, // TODO: Custom user title
"1", "1", // Chapter 1, page 1
"1" // TODO: Get latest chapter for language and group
});

MangaInfo info = new MangaInfo()
{
Type = "manga",
Source = "mangadex",
Id = MangaDexHelper.GetMangaID(mangaUrl),
Name = title,
LangCode = lang_code,
Group = "^any-group",
UserName = title,
Chapter = "1",
Page = "1",
Latest = "1"
};

string output = JsonConvert.SerializeObject(info);
File.WriteAllText(Path.Combine(mangaRoot.FullName, "manga.json"), output);

_Load(false);
GetSetPrunedChapters(true);
Expand Down Expand Up @@ -311,16 +314,22 @@ public override void Save(string chapter, string page)
this.currentchapter = chapter;
this.currentpage = page;

File.WriteAllLines(Path.Combine(mangaRoot.FullName, "manga.txt"), new string[] {
"manga",
mangaRoot.Name,
name,
userlang,
usergroup,
usertitle,
chapter, page,
lastchapter // TODO: Get latest chapter for language and group
});
MangaInfo info = new MangaInfo()
{
Type = "manga",
Source = "mangadex",
Id = mangaRoot.Name,
Name = name,
LangCode = userlang,
Group = usergroup,
UserName = usertitle,
Chapter = chapter,
Page = page,
Latest = lastchapter
};

string output = JsonConvert.SerializeObject(info);
File.WriteAllText(Path.Combine(mangaRoot.FullName, "manga.json"), output);
}

public override void UpdateProperties(string title, string lang, string group)
Expand Down
2 changes: 1 addition & 1 deletion MikuReader-Core/Core/Title/MangaType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace MikuReader.Core
{
public enum MangaType
{
MANGADEX, KISSMANGA
MANGADEX, KISSMANGA, NULL
}
}
Loading

0 comments on commit 24923fd

Please sign in to comment.