-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileFetcher.cs
118 lines (108 loc) · 4.23 KB
/
FileFetcher.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using HyoutaPluginBase;
using HyoutaTools.FileContainer;
using HyoutaTools.Tales.CPK;
using HyoutaUtils;
using HyoutaUtils.Streams;
namespace ToGLocInject {
internal class FileFetcher {
private Config Config;
Dictionary<(Version v, string cpk), HyoutaPluginBase.FileContainer.IContainer> cache;
public FileFetcher(Config config) {
Config = config;
cache = new Dictionary<(Version v, string cpk), HyoutaPluginBase.FileContainer.IContainer>();
cache.Add((Version.U, ""), config.GamefileContainerPS3US);
cache.Add((Version.J, ""), config.GamefileContainerPS3JP);
cache.Add((Version.E, ""), config.GamefileContainerPS3EU);
cache.Add((Version.W, ""), config.GamefileContainerWiiV2);
if (config.GamefileContainerWiiV0 != null) {
cache.Add((Version.Wv0, ""), config.GamefileContainerWiiV0);
}
}
private HyoutaPluginBase.FileContainer.INode ReturnAndCache(HyoutaPluginBase.FileContainer.INode node, string path, Version version) {
if (node != null && path != null) {
Directory.CreateDirectory(Path.GetDirectoryName(path));
using (FileStream fs = new FileStream(path, FileMode.Create)) {
using (DuplicatableStream ds = node.AsFile.DataStream.Duplicate()) {
StreamUtils.CopyStream(ds, fs, ds.Length);
}
}
}
return node;
}
private HyoutaPluginBase.FileContainer.INode GetFileInternal(string name, Version version) {
if (name == "boot.elf") {
switch (version) {
case Version.U: return Config.EbootBinPS3US;
case Version.J: return Config.EbootBinPS3JP;
case Version.E: return Config.EbootBinPS3EU;
case Version.W: return Config.MainDolWiiV2;
}
return null;
} else {
string cachepath = null;
if (Config.CachePath != null) {
cachepath = Path.Combine(Config.CachePath, version.ToString() + "_" + name.Replace('/', '_'));
if (File.Exists(cachepath)) {
return new FileFromStream(new DuplicatableByteArrayStream(new DuplicatableFileStream(cachepath).CopyToByteArrayAndDispose()));
}
}
HyoutaPluginBase.FileContainer.IContainer root = cache[(version, "")];
var split = name.Split('/');
HyoutaPluginBase.FileContainer.IContainer cpk;
if (!cache.TryGetValue((version, split[0]), out cpk)) {
cpk = new CpkContainer(root.GetChildByName(split[0]).AsFile.DataStream);
cache.Add((version, split[0]), cpk);
}
if (name == split[0]) {
return cpk;
}
if (split[1].EndsWith(".cpk")) {
HyoutaPluginBase.FileContainer.IContainer subcpk;
string p = split[0] + "/" + split[1];
if (!cache.TryGetValue((version, p), out subcpk)) {
var s = cpk.GetChildByName(split[1])?.AsFile?.DataStream;
if (s == null) {
return null;
}
subcpk = new CpkContainer(s);
cache.Add((version, p), subcpk);
}
if (name == p) {
return subcpk;
}
return ReturnAndCache(subcpk.GetChildByName(name.Split(new char[] { '/' }, 3)[2]), cachepath, version);
} else {
if (version == Version.U && name == "rootR.cpk/sys/ja/SysString.bin") {
return root.GetChildByName("Sys").AsContainer.GetChildByName("ja").AsContainer.GetChildByName("SysString.bin");
}
if (version != Version.W) {
var fixup = name.Split(new char[] { '/' }, 3);
if (fixup[1] == "sys" || fixup[1] == "str") {
return ReturnAndCache(cpk.GetChildByName("S" + fixup[1].Substring(1) + '/' + fixup[2]), cachepath, version);
} else {
return ReturnAndCache(cpk.GetChildByName(name.Split(new char[] { '/' }, 2)[1]), cachepath, version);
}
} else {
return ReturnAndCache(cpk.GetChildByName(name.Split(new char[] { '/' }, 2)[1]), cachepath, version);
}
}
}
}
public DuplicatableStream GetFile(string name, Version version) {
var f = GetFileInternal(name, version)?.AsFile?.DataStream;
if (f == null) {
throw new Exception("Failed to find " + version + ": " + name);
}
return f;
}
public HyoutaPluginBase.FileContainer.IFile TryGetFile(string name, Version version) {
return GetFileInternal(name, version)?.AsFile;
}
public HyoutaPluginBase.FileContainer.IContainer TryGetContainer(string name, Version version) {
return GetFileInternal(name, version)?.AsContainer;
}
}
}