forked from megasus/WoWEditor
/
FileManager.cs
106 lines (92 loc) · 3.4 KB
/
FileManager.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
using System;
using System.Diagnostics;
using System.IO;
namespace WoWEditor6.IO
{
class FileManager
{
public static FileManager Instance { get; private set; }
public IFileProvider Provider { get; private set; }
public string DataPath { get; set; }
public bool Initialized { get; private set; }
public event Action LoadComplete;
public FileDataVersion Version { get; private set; }
static FileManager()
{
Instance = new FileManager();
}
public Stream GetOutputStream(string path)
{
var fullPath = Path.Combine(Directory.GetCurrentDirectory(), "Output", path);
try
{
Directory.CreateDirectory(Path.GetDirectoryName(fullPath) ?? ".");
return File.Open(fullPath, FileMode.Create, FileAccess.Write, FileShare.None);
}
catch(Exception)
{
return null;
}
}
public Stream GetExistingFile(string path)
{
try
{
var fullPath = Path.Combine(Directory.GetCurrentDirectory(), "Output", path);
using (var strm = File.OpenRead(fullPath))
{
var retStream = new MemoryStream();
strm.CopyTo(retStream);
retStream.Position = 0;
return retStream;
}
}
catch(Exception)
{
return null;
}
}
public void InitFromPath()
{
if(string.IsNullOrEmpty(DataPath))
throw new InvalidOperationException("Cannot initialize file system without a path");
if (File.Exists(Path.Combine(DataPath, ".build.info")))
{
Files.Terrain.AdtFactory.Instance.Version = FileDataVersion.Warlords;
Files.Models.ModelFactory.Instance.Version = FileDataVersion.Warlords;
Files.Sky.SkyManager.InitVersion(FileDataVersion.Warlords);
Version = FileDataVersion.Warlords;
var mgr = new CASC.FileManager();
mgr.LoadComplete += () =>
{
Initialized = true;
if (LoadComplete != null)
LoadComplete();
};
Provider = mgr;
mgr.Initialize(DataPath);
}
else
InitMpq();
}
private void InitMpq()
{
var version = FileVersionInfo.GetVersionInfo(Path.Combine(DataPath, "Wow.exe"));
if (version.FilePrivatePart > 13000 || version.FilePrivatePart < 9000)
throw new NotImplementedException("MPQ is only implemented for WOTLK (builds 9000 - 13000)");
Files.Terrain.AdtFactory.Instance.Version = FileDataVersion.Lichking;
Files.Models.ModelFactory.Instance.Version = FileDataVersion.Lichking;
Files.Sky.SkyManager.InitVersion(FileDataVersion.Lichking);
Version = FileDataVersion.Lichking;
var mgr = new MPQ.FileManager();
mgr.LoadComplete += () =>
{
Initialized = true;
if (LoadComplete != null)
LoadComplete();
};
Provider = mgr;
mgr.InitFromPath(DataPath);
}
}
}