2018-12-08 19:21:52 -05:00
|
|
|
|
/* OpenDiablo 2 - An open source re-implementation of Diablo 2 in C#
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using OpenDiablo2.Common.Interfaces;
|
2018-11-22 00:18:42 -05:00
|
|
|
|
using OpenDiablo2.Common.Models;
|
2018-12-08 19:21:52 -05:00
|
|
|
|
using System.Collections;
|
2018-11-22 00:18:42 -05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace OpenDiablo2.Core
|
|
|
|
|
{
|
|
|
|
|
public sealed class MPQProvider : IMPQProvider
|
|
|
|
|
{
|
2018-12-08 19:21:52 -05:00
|
|
|
|
private readonly IList<MPQ> mpqs;
|
2018-12-08 09:32:09 -05:00
|
|
|
|
private readonly Dictionary<string, int> mpqLookup = new Dictionary<string, int>();
|
2018-11-22 00:18:42 -05:00
|
|
|
|
|
|
|
|
|
public MPQProvider(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.
|
2018-12-08 19:21:52 -05:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2018-12-08 18:02:54 -05:00
|
|
|
|
public byte[] GetBytes(string fileName)
|
|
|
|
|
{
|
|
|
|
|
var stream = GetStream(fileName);
|
|
|
|
|
var result = new byte[stream.Length];
|
|
|
|
|
stream.Read(result, 0, (int)stream.Length);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-08 19:21:52 -05:00
|
|
|
|
public IEnumerator<MPQ> GetEnumerator()
|
2018-12-08 18:02:54 -05:00
|
|
|
|
{
|
2018-12-08 19:21:52 -05:00
|
|
|
|
return mpqs.GetEnumerator();
|
2018-12-08 18:02:54 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-08 19:21:52 -05:00
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
|
{
|
|
|
|
|
return GetEnumerator();
|
|
|
|
|
}
|
2018-11-22 00:18:42 -05:00
|
|
|
|
|
|
|
|
|
public Stream GetStream(string fileName)
|
2018-12-07 18:45:54 -05:00
|
|
|
|
{
|
|
|
|
|
if (!mpqLookup.ContainsKey(fileName.ToLower()))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return mpqs[mpqLookup[fileName.ToLower()]].OpenFile(fileName);
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
}
|