2018-12-08 11:51:11 -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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-11-22 22:53:05 -05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2018-12-05 22:29:45 -05:00
|
|
|
|
using OpenDiablo2.Common;
|
|
|
|
|
using OpenDiablo2.Common.Enums;
|
2018-11-22 22:53:05 -05:00
|
|
|
|
using OpenDiablo2.Common.Interfaces;
|
|
|
|
|
using OpenDiablo2.Common.Models;
|
|
|
|
|
|
|
|
|
|
namespace OpenDiablo2.Core
|
|
|
|
|
{
|
|
|
|
|
public sealed class ResourceManager : IResourceManager
|
|
|
|
|
{
|
2018-12-09 18:38:54 -05:00
|
|
|
|
static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
|
|
|
|
|
|
2018-12-08 11:51:11 -05:00
|
|
|
|
private readonly ICache cache;
|
2018-11-22 22:53:05 -05:00
|
|
|
|
private readonly IMPQProvider mpqProvider;
|
2018-11-24 03:07:41 -05:00
|
|
|
|
private readonly IEngineDataManager engineDataManager;
|
|
|
|
|
|
2018-12-08 11:51:11 -05:00
|
|
|
|
public Dictionary<string, List<AnimationData>> Animations { get; private set; }
|
2018-11-22 22:53:05 -05:00
|
|
|
|
|
2018-12-08 11:51:11 -05:00
|
|
|
|
public ResourceManager(ICache cache, IMPQProvider mpqProvider, IEngineDataManager engineDataManager)
|
2018-11-22 22:53:05 -05:00
|
|
|
|
{
|
2018-12-08 11:51:11 -05:00
|
|
|
|
this.cache = cache;
|
2018-11-22 22:53:05 -05:00
|
|
|
|
this.mpqProvider = mpqProvider;
|
2018-11-24 03:07:41 -05:00
|
|
|
|
this.engineDataManager = engineDataManager;
|
2018-12-05 22:29:45 -05:00
|
|
|
|
|
|
|
|
|
Animations = AnimationData.LoadFromStream(mpqProvider.GetStream(ResourcePaths.AnimationData));
|
2018-11-22 22:53:05 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ImageSet GetImageSet(string resourcePath)
|
2018-12-08 11:51:11 -05:00
|
|
|
|
=> cache.AddOrGetExisting($"ImageSet::{resourcePath}", () => ImageSet.LoadFromStream(mpqProvider.GetStream(resourcePath)));
|
2018-11-22 22:53:05 -05:00
|
|
|
|
|
|
|
|
|
public MPQFont GetMPQFont(string resourcePath)
|
2018-12-08 11:51:11 -05:00
|
|
|
|
=> cache.AddOrGetExisting($"Font::{resourcePath}", () => MPQFont.LoadFromStream(mpqProvider.GetStream($"{resourcePath}.DC6"), mpqProvider.GetStream($"{resourcePath}.tbl")));
|
2018-11-22 22:53:05 -05:00
|
|
|
|
|
2018-12-10 21:43:06 -05:00
|
|
|
|
public MPQDS1 GetMPQDS1(string resourcePath, LevelPreset level, LevelType levelType)
|
|
|
|
|
=> cache.AddOrGetExisting($"DS1::{resourcePath}::{level}::{levelType}", ()
|
|
|
|
|
=> new MPQDS1(mpqProvider.GetStream(resourcePath), level, levelType, engineDataManager, this) { MapFile = resourcePath });
|
2018-11-24 03:07:41 -05:00
|
|
|
|
|
2018-11-22 22:53:05 -05:00
|
|
|
|
public Palette GetPalette(string paletteFile)
|
2018-12-08 11:51:11 -05:00
|
|
|
|
=> cache.AddOrGetExisting($"Palette::{paletteFile}", () =>
|
2018-11-22 22:53:05 -05:00
|
|
|
|
{
|
|
|
|
|
var paletteNameParts = paletteFile.Split('\\');
|
|
|
|
|
var paletteName = paletteNameParts[paletteNameParts.Count() - 2];
|
2018-12-08 11:51:11 -05:00
|
|
|
|
return Palette.LoadFromStream(mpqProvider.GetStream(paletteFile), paletteName);
|
|
|
|
|
});
|
2018-11-24 03:07:41 -05:00
|
|
|
|
|
|
|
|
|
public MPQDT1 GetMPQDT1(string resourcePath)
|
2018-12-08 11:51:11 -05:00
|
|
|
|
=> cache.AddOrGetExisting($"DT1::{resourcePath}", () => new MPQDT1(mpqProvider.GetStream(resourcePath)));
|
2018-12-05 22:29:45 -05:00
|
|
|
|
|
2018-12-11 00:25:41 -05:00
|
|
|
|
public MPQCOF GetPlayerAnimation(eHero hero, eWeaponClass weaponClass, eMobMode mobMode, string shieldCode, string weaponCode)
|
2018-12-08 11:51:11 -05:00
|
|
|
|
=> cache.AddOrGetExisting($"COF::{hero}::{weaponClass}::{mobMode}", () =>
|
|
|
|
|
{
|
|
|
|
|
var path = $"{ResourcePaths.PlayerAnimationBase}\\{hero.ToToken()}\\COF\\{hero.ToToken()}{mobMode.ToToken()}{weaponClass.ToToken()}.cof";
|
2018-12-11 00:25:41 -05:00
|
|
|
|
return MPQCOF.Load(mpqProvider.GetStream(path), Animations, hero, weaponClass, mobMode, shieldCode, weaponCode);
|
2018-12-08 11:51:11 -05:00
|
|
|
|
});
|
2018-12-05 22:29:45 -05:00
|
|
|
|
|
|
|
|
|
public MPQDCC GetPlayerDCC(MPQCOF.COFLayer cofLayer, eArmorType armorType, Palette palette)
|
|
|
|
|
{
|
2018-12-07 18:45:54 -05:00
|
|
|
|
// TODO: We need to cache this...
|
2018-12-05 22:29:45 -05:00
|
|
|
|
byte[] binaryData;
|
2018-12-08 11:51:11 -05:00
|
|
|
|
|
2018-12-09 18:38:54 -05:00
|
|
|
|
var streamPath = cofLayer.GetDCCPath(armorType);
|
|
|
|
|
using (var stream = mpqProvider.GetStream(streamPath))
|
2018-12-05 22:29:45 -05:00
|
|
|
|
{
|
2018-12-07 18:45:54 -05:00
|
|
|
|
if (stream == null)
|
2018-12-09 18:38:54 -05:00
|
|
|
|
{
|
|
|
|
|
log.Error($"Could not load Player DCC: {streamPath}");
|
2018-12-07 18:45:54 -05:00
|
|
|
|
return null;
|
2018-12-09 18:38:54 -05:00
|
|
|
|
}
|
2018-12-07 18:45:54 -05:00
|
|
|
|
|
2018-12-05 22:29:45 -05:00
|
|
|
|
binaryData = new byte[stream.Length];
|
|
|
|
|
stream.Read(binaryData, 0, (int)stream.Length);
|
|
|
|
|
}
|
|
|
|
|
var result = new MPQDCC(binaryData, palette);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2018-12-08 11:51:11 -05:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
=> cache.AddOrGetExisting($"DCC::{cofLayer}::{armorType}::{palette.Name}", () =>
|
|
|
|
|
{
|
|
|
|
|
byte[] binaryData;
|
|
|
|
|
|
|
|
|
|
using (var stream = mpqProvider.GetStream(cofLayer.GetDCCPath(armorType)))
|
|
|
|
|
{
|
|
|
|
|
if (stream == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
binaryData = new byte[stream.Length];
|
|
|
|
|
stream.Read(binaryData, 0, (int)stream.Length);
|
|
|
|
|
}
|
|
|
|
|
var result = new MPQDCC(binaryData, palette);
|
|
|
|
|
return result;
|
|
|
|
|
});
|
|
|
|
|
/*
|
|
|
|
|
*/
|
2018-11-22 22:53:05 -05:00
|
|
|
|
}
|
|
|
|
|
}
|