1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-09-27 21:56:19 -04:00

small refactoring (#39)

* refactoring of minipanel
* Yet another refactoring of panels
* removed unnecessary fields
* panel location is now relative to the panel position
* resource paths refactoring(?)
* small refactoring
This commit is contained in:
Kacper Drobny 2018-12-09 01:21:52 +01:00 committed by Tim Sarbin
parent c92402a3f4
commit 3baa4673c9
4 changed files with 80 additions and 58 deletions

View File

@ -1,17 +1,29 @@
using System; /* 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.Models;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading.Tasks;
using OpenDiablo2.Common.Models;
namespace OpenDiablo2.Common.Interfaces namespace OpenDiablo2.Common.Interfaces
{ {
public interface IMPQProvider public interface IMPQProvider : IEnumerable<MPQ>
{ {
IEnumerable<MPQ> GetMPQs(); IEnumerable<string> GetTextFile(string fileName);
IEnumerable<String> GetTextFile(string fileName);
Stream GetStream(string fileName); Stream GetStream(string fileName);
byte[] GetBytes(string fileName); byte[] GetBytes(string fileName);
void GetBytesAsync(string fileName, Action<byte[]> callback);
} }
} }

View File

@ -41,14 +41,12 @@ namespace OpenDiablo2.Core
this.getScene = getScene; this.getScene = getScene;
this.getResourceManager = getResourceManager; this.getResourceManager = getResourceManager;
this.getGameState = getGameState; this.getGameState = getGameState;
MPQs = mpqProvider.GetMPQs().ToArray();
} }
private void LoadPalettes() private void LoadPalettes()
{ {
log.Info("Loading palettes"); log.Info("Loading palettes");
var paletteFiles = MPQs.SelectMany(x => x.Files).Where(x => x.StartsWith("data\\global\\palette\\") && x.EndsWith(".dat")); var paletteFiles = mpqProvider.SelectMany(x => x.Files).Where(x => x.StartsWith("data\\global\\palette\\") && x.EndsWith(".dat"));
foreach (var paletteFile in paletteFiles) foreach (var paletteFile in paletteFiles)
{ {
var paletteNameParts = paletteFile.Split('\\'); var paletteNameParts = paletteFile.Split('\\');

View File

@ -1,23 +1,37 @@
using OpenDiablo2.Common.Interfaces; /* 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;
using OpenDiablo2.Common.Models; using OpenDiablo2.Common.Models;
using System; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OpenDiablo2.Core namespace OpenDiablo2.Core
{ {
public sealed class MPQProvider : IMPQProvider public sealed class MPQProvider : IMPQProvider
{ {
private readonly MPQ[] mpqs; private readonly IList<MPQ> mpqs;
private readonly Dictionary<string, int> mpqLookup = new Dictionary<string, int>(); private readonly Dictionary<string, int> mpqLookup = new Dictionary<string, int>();
public MPQProvider(GlobalConfiguration globalConfiguration) public MPQProvider(GlobalConfiguration globalConfiguration)
{ {
// TODO: Make this less dumb. We need to an external file to configure mpq load order. // TODO: Make this less dumb. We need to an external file to configure mpq load order.
this.mpqs = Directory this.mpqs = Directory
.EnumerateFiles(globalConfiguration.BaseDataPath, "*.mpq") .EnumerateFiles(globalConfiguration.BaseDataPath, "*.mpq")
.Where(x => !Path.GetFileName(x).StartsWith("patch")) .Where(x => !Path.GetFileName(x).StartsWith("patch"))
@ -59,15 +73,15 @@ namespace OpenDiablo2.Core
return result; return result;
} }
public void GetBytesAsync(string fileName, Action<byte[]> callback) public IEnumerator<MPQ> GetEnumerator()
{ {
var stream = GetStream(fileName); return mpqs.GetEnumerator();
var result = new byte[stream.Length];
stream.Read(result, 0, (int)stream.Length);
callback(result);
} }
public IEnumerable<MPQ> GetMPQs() => mpqs; IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public Stream GetStream(string fileName) public Stream GetStream(string fileName)
{ {
@ -77,10 +91,7 @@ namespace OpenDiablo2.Core
return mpqs[mpqLookup[fileName.ToLower()]].OpenFile(fileName); return mpqs[mpqLookup[fileName.ToLower()]].OpenFile(fileName);
} }
public IEnumerable<string> GetTextFile(string fileName) public IEnumerable<string> GetTextFile(string fileName)
=> new StreamReader(mpqs[mpqLookup[fileName.ToLower()]].OpenFile(fileName)).ReadToEnd().Split('\n'); => new StreamReader(mpqs[mpqLookup[fileName.ToLower()]].OpenFile(fileName)).ReadToEnd().Split('\n');
} }
} }

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using OpenDiablo2.Common; using OpenDiablo2.Common;
using OpenDiablo2.Common.Attributes; using OpenDiablo2.Common.Attributes;
using OpenDiablo2.Common.Enums; using OpenDiablo2.Common.Enums;
@ -51,9 +52,7 @@ namespace OpenDiablo2.Scenes
private readonly ITextBox characterNameTextBox; private readonly ITextBox characterNameTextBox;
private readonly Dictionary<eHero, HeroRenderInfo> heroRenderInfo = new Dictionary<eHero, HeroRenderInfo>(); private readonly Dictionary<eHero, HeroRenderInfo> heroRenderInfo = new Dictionary<eHero, HeroRenderInfo>();
private byte[] sfxAmazonSelect, sfxAmazonDeselect, sfxAssassinSelect, sfxAssassinDeselect, sfxBarbarianSelect, sfxBarbarianDeselect, private Dictionary<string, byte[]> sfxDictionary;
sfxDruidSelect, sfxDruidDeselect, sfxNecromancerSelect, sfxNecromancerDeselect, sfxPaladinSelect, sfxPaladinDeselect,
sfxSorceressSelect, sfxSorceressDeselect;
public SelectHeroClass( public SelectHeroClass(
IRenderWindow renderWindow, IRenderWindow renderWindow,
@ -75,7 +74,7 @@ namespace OpenDiablo2.Scenes
this.keyboardInfoProvider = keyboardInfoProvider; this.keyboardInfoProvider = keyboardInfoProvider;
this.soundProvider = soundProvider; this.soundProvider = soundProvider;
this.gameState = gameState; this.gameState = gameState;
sfxDictionary = new Dictionary<string, byte[]>();
backgroundSprite = renderWindow.LoadSprite(ResourcePaths.CharacterSelectBackground, Palettes.Fechar); backgroundSprite = renderWindow.LoadSprite(ResourcePaths.CharacterSelectBackground, Palettes.Fechar);
campfireSprite = renderWindow.LoadSprite(ResourcePaths.CharacterSelectCampfire, Palettes.Fechar, new Point(380, 335)); campfireSprite = renderWindow.LoadSprite(ResourcePaths.CharacterSelectCampfire, Palettes.Fechar, new Point(380, 335));
@ -224,21 +223,23 @@ namespace OpenDiablo2.Scenes
characterNameTextBox.Text = ""; characterNameTextBox.Text = "";
characterNameTextBox.Location = new Point(320, 493); characterNameTextBox.Location = new Point(320, 493);
mpqProvider.GetBytesAsync(ResourcePaths.SFXAmazonSelect, x => sfxAmazonSelect = x); Parallel.ForEach(new[]
mpqProvider.GetBytesAsync(ResourcePaths.SFXAmazonDeselect, x => sfxAmazonDeselect = x); {
mpqProvider.GetBytesAsync(ResourcePaths.SFXAssassinSelect, x => sfxAssassinSelect = x); ResourcePaths.SFXAmazonSelect,
mpqProvider.GetBytesAsync(ResourcePaths.SFXAssassinDeselect, x => sfxAssassinDeselect = x); ResourcePaths.SFXAmazonDeselect,
mpqProvider.GetBytesAsync(ResourcePaths.SFXBarbarianSelect, x => sfxBarbarianSelect = x); ResourcePaths.SFXAssassinSelect,
mpqProvider.GetBytesAsync(ResourcePaths.SFXBarbarianDeselect, x => sfxBarbarianDeselect = x); ResourcePaths.SFXAssassinDeselect,
mpqProvider.GetBytesAsync(ResourcePaths.SFXDruidSelect, x => sfxDruidSelect = x); ResourcePaths.SFXBarbarianSelect,
mpqProvider.GetBytesAsync(ResourcePaths.SFXDruidDeselect, x => sfxDruidDeselect = x); ResourcePaths.SFXBarbarianDeselect,
mpqProvider.GetBytesAsync(ResourcePaths.SFXNecromancerSelect, x => sfxNecromancerSelect = x); ResourcePaths.SFXDruidSelect,
mpqProvider.GetBytesAsync(ResourcePaths.SFXNecromancerDeselect, x => sfxNecromancerDeselect = x); ResourcePaths.SFXDruidDeselect,
mpqProvider.GetBytesAsync(ResourcePaths.SFXPaladinSelect, x => sfxPaladinSelect = x); ResourcePaths.SFXNecromancerSelect,
mpqProvider.GetBytesAsync(ResourcePaths.SFXPaladinDeselect, x => sfxPaladinDeselect = x); ResourcePaths.SFXNecromancerDeselect,
mpqProvider.GetBytesAsync(ResourcePaths.SFXSorceressSelect, x => sfxSorceressSelect = x); ResourcePaths.SFXPaladinSelect,
mpqProvider.GetBytesAsync(ResourcePaths.SFXSorceressDeselect, x => sfxSorceressDeselect = x); ResourcePaths.SFXPaladinDeselect,
ResourcePaths.SFXSorceressSelect,
ResourcePaths.SFXSorceressDeselect
}, (path => sfxDictionary.Add(path, mpqProvider.GetBytes(path))));
} }
private void OnOkclicked() private void OnOkclicked()
@ -475,25 +476,25 @@ namespace OpenDiablo2.Scenes
switch (hero) switch (hero)
{ {
case eHero.Barbarian: case eHero.Barbarian:
soundProvider.PlaySfx(sfxBarbarianSelect); soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXBarbarianSelect]);
break; break;
case eHero.Necromancer: case eHero.Necromancer:
soundProvider.PlaySfx(sfxNecromancerSelect); soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXNecromancerSelect]);
break; break;
case eHero.Paladin: case eHero.Paladin:
soundProvider.PlaySfx(sfxPaladinSelect); soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXPaladinSelect]);
break; break;
case eHero.Assassin: case eHero.Assassin:
soundProvider.PlaySfx(sfxAssassinSelect); soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXAssassinSelect]);
break; break;
case eHero.Sorceress: case eHero.Sorceress:
soundProvider.PlaySfx(sfxSorceressSelect); soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXSorceressSelect]);
break; break;
case eHero.Amazon: case eHero.Amazon:
soundProvider.PlaySfx(sfxAmazonSelect); soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXAmazonSelect]);
break; break;
case eHero.Druid: case eHero.Druid:
soundProvider.PlaySfx(sfxDruidSelect); soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXDruidSelect]);
break; break;
default: default:
break; break;
@ -505,25 +506,25 @@ namespace OpenDiablo2.Scenes
switch (hero) switch (hero)
{ {
case eHero.Barbarian: case eHero.Barbarian:
soundProvider.PlaySfx(sfxBarbarianDeselect); soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXBarbarianDeselect]);
break; break;
case eHero.Necromancer: case eHero.Necromancer:
soundProvider.PlaySfx(sfxNecromancerDeselect); soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXNecromancerDeselect]);
break; break;
case eHero.Paladin: case eHero.Paladin:
soundProvider.PlaySfx(sfxPaladinDeselect); soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXPaladinDeselect]);
break; break;
case eHero.Assassin: case eHero.Assassin:
soundProvider.PlaySfx(sfxAssassinDeselect); soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXAssassinDeselect]);
break; break;
case eHero.Sorceress: case eHero.Sorceress:
soundProvider.PlaySfx(sfxSorceressDeselect); soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXSorceressDeselect]);
break; break;
case eHero.Amazon: case eHero.Amazon:
soundProvider.PlaySfx(sfxAmazonDeselect); soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXAmazonDeselect]);
break; break;
case eHero.Druid: case eHero.Druid:
soundProvider.PlaySfx(sfxDruidDeselect); soundProvider.PlaySfx(sfxDictionary[ResourcePaths.SFXDruidDeselect]);
break; break;
default: default:
break; break;