2018-11-22 00:18:42 -05:00
|
|
|
|
using OpenDiablo2.Common.Interfaces;
|
|
|
|
|
using OpenDiablo2.Common.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace OpenDiablo2.Core
|
|
|
|
|
{
|
|
|
|
|
public sealed class MPQProvider : IMPQProvider
|
|
|
|
|
{
|
|
|
|
|
private readonly GlobalConfiguration globalConfiguration;
|
|
|
|
|
private readonly MPQ[] mpqs;
|
2018-11-23 15:22:35 -05:00
|
|
|
|
private Dictionary<string, int> mpqLookup = new Dictionary<string, int>();
|
2018-11-22 00:18:42 -05:00
|
|
|
|
|
|
|
|
|
public MPQProvider(GlobalConfiguration globalConfiguration)
|
|
|
|
|
{
|
|
|
|
|
this.globalConfiguration = globalConfiguration;
|
2018-11-24 17:54:15 -05:00
|
|
|
|
// TODO: Make this less dumb. We need to an external file to configure mpq load order.
|
|
|
|
|
var mpqsToLoad = new string[]
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
};
|
2018-11-22 00:18:42 -05:00
|
|
|
|
this.mpqs = Directory
|
|
|
|
|
.EnumerateFiles(globalConfiguration.BaseDataPath, "*.mpq")
|
|
|
|
|
.Where(x => !Path.GetFileName(x).StartsWith("patch"))
|
|
|
|
|
.Select(file => new MPQ(file))
|
|
|
|
|
.ToArray();
|
2018-11-23 15:22:35 -05:00
|
|
|
|
|
2018-11-24 17:54:15 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Load the base game files
|
2018-11-23 15:22:35 -05:00
|
|
|
|
for(var i = 0; i < mpqs.Count(); i++)
|
|
|
|
|
{
|
2018-11-24 17:54:15 -05:00
|
|
|
|
if (Path.GetFileName(mpqs[i].Path).StartsWith("d2exp") || Path.GetFileName(mpqs[i].Path).StartsWith("d2x"))
|
|
|
|
|
continue;
|
|
|
|
|
|
2018-11-23 15:22:35 -05:00
|
|
|
|
foreach(var file in mpqs[i].Files)
|
|
|
|
|
{
|
|
|
|
|
mpqLookup[file.ToLower()] = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-24 17:54:15 -05:00
|
|
|
|
|
|
|
|
|
// Load the expansion game files
|
|
|
|
|
for (var i = 0; i < mpqs.Count(); i++)
|
|
|
|
|
{
|
|
|
|
|
if (!Path.GetFileName(mpqs[i].Path).StartsWith("d2exp") && !Path.GetFileName(mpqs[i].Path).StartsWith("d2x"))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
foreach (var file in mpqs[i].Files)
|
|
|
|
|
{
|
|
|
|
|
mpqLookup[file.ToLower()] = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-22 00:18:42 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<MPQ> GetMPQs() => mpqs;
|
|
|
|
|
|
|
|
|
|
public Stream GetStream(string fileName)
|
2018-11-24 17:54:15 -05:00
|
|
|
|
=> mpqs[mpqLookup[fileName.ToLower()]].OpenFile(fileName);
|
|
|
|
|
|
2018-11-22 00:18:42 -05:00
|
|
|
|
|
2018-11-24 17:54:15 -05:00
|
|
|
|
public IEnumerable<string> GetTextFile(string fileName)
|
|
|
|
|
=> new StreamReader(mpqs[mpqLookup[fileName.ToLower()]].OpenFile(fileName)).ReadToEnd().Split('\n');
|
2018-11-22 00:18:42 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|