Skip to content

Commit

Permalink
Merge 7afde7d into 4b84d10
Browse files Browse the repository at this point in the history
  • Loading branch information
Sejoslaw authored May 9, 2021
2 parents 4b84d10 + 7afde7d commit fa23ab2
Show file tree
Hide file tree
Showing 27 changed files with 716 additions and 137 deletions.
17 changes: 17 additions & 0 deletions Negum.Core/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Text;

namespace Negum.Core.Extensions
{
/// <summary>
/// Contains extension methods for strings.
/// </summary>
///
/// <author>
/// https://github.com/TheNegumProject/Negum.Core
/// </author>
public static class StringExtensions
{
public static byte[] ToByteArray(this string text) =>
Encoding.UTF8.GetBytes(text);
}
}
61 changes: 7 additions & 54 deletions Negum.Core/Loaders/AbstractLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
using Negum.Core.Managers;
using Negum.Core.Managers.Types;
using Negum.Core.Models.Data;
using Negum.Core.Models.Sounds;
using Negum.Core.Models.Sprites;
using Negum.Core.Readers;

namespace Negum.Core.Loaders
Expand All @@ -30,65 +28,18 @@ protected virtual async Task<IStoryboard> ReadStoryboardAsync(string dirName, st
return null;
}

var defFilePath = this.FindFile(dirName, fileName);
var reader = NegumContainer.Resolve<IFilePathReader>();

var defFilePath = reader.FindFile(dirName, fileName);
var storyboard = new Storyboard();

storyboard.Manager = await this.ReadManagerAsync<IStoryboardManager>(defFilePath);
storyboard.Animation = await this.ReadManagerAsync<IAnimationManager, IAnimationReader>(defFilePath);
storyboard.Sprite = await this.GetSpriteAsync(dirName, storyboard.Manager.SceneDef.SpriteFile);
storyboard.Sprite = await reader.GetSpriteAsync(dirName, storyboard.Manager.SceneDef.SpriteFile);

return storyboard;
}

protected virtual async Task<ISound> GetSoundAsync(string dirName, string soundPath)
{
var file = this.FindFile(dirName, soundPath);

if (file == null)
{
return null;
}

var soundReader = NegumContainer.Resolve<ISoundPathReader>();
var sound = await soundReader.ReadAsync(file.FullName);

return sound;
}

protected virtual async Task<ISprite> GetSpriteAsync(string dirName, string spritePath)
{
var file = this.FindFile(dirName, spritePath);

if (file == null)
{
return null;
}

var reader = NegumContainer.Resolve<ISpritePathReader>();
var sprite = await reader.ReadAsync(file.FullName);

return sprite;
}

protected virtual FileInfo FindFile(string dirName, string fileName)
{
var path = Path.Combine(dirName, fileName);
var file = new FileInfo(path);

while (!file.Exists)
{
if (file.Directory.Parent == null)
{
return null;
}

path = Path.Combine(file.Directory.Parent.FullName, file.Name);
file = new FileInfo(path);
}

return file;
}

protected virtual IEnumerable<FileInfo> GetFiles(IEngine engine, string subdirectoryName) =>
this.GetDirectory(engine, subdirectoryName).GetFiles();

Expand Down Expand Up @@ -159,7 +110,9 @@ protected virtual async Task<TManager> ReadManagerAsync<TManager, TReader>(strin
protected virtual async Task<TManager> FindManagerAsync<TManager>(string dirName, string filePath)
where TManager : IManager
{
var file = this.FindFile(dirName, filePath);
var reader = NegumContainer.Resolve<IFilePathReader>();

var file = reader.FindFile(dirName, filePath);
var manager = await this.ReadManagerAsync<TManager>(file);

return manager;
Expand Down
8 changes: 6 additions & 2 deletions Negum.Core/Loaders/ICharacterLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Negum.Core.Containers;
using Negum.Core.Engines;
using Negum.Core.Managers.Types;
using Negum.Core.Models.Characters;
using Negum.Core.Readers;

namespace Negum.Core.Loaders
{
Expand Down Expand Up @@ -60,14 +62,16 @@ protected virtual async Task<ICharacter> GetCharacterAsync(DirectoryInfo dir)
Directory = dir
};

var reader = NegumContainer.Resolve<IFilePathReader>();

character.CharacterManager = await this.ReadManagerAsync<ICharacterManager>(characterDefFile, characterDefFile.Name);
character.CommandsManager = await this.ReadManagerAsync<ICharacterCommandsManager>(characterDefFile, character.CharacterManager.Files.CommandFile);
character.ConstantsManager = await this.ReadManagerAsync<ICharacterConstantsManager>(characterDefFile, character.CharacterManager.Files.ConstantsFile);
character.StatesManager = await this.ReadManagerAsync<ICharacterConstantsManager>(characterDefFile, character.CharacterManager.Files.StatesFile);
character.CommonStatesManager = await this.ReadCommonStatesAsync(characterDefFile, character.CharacterManager.Files.CommonStatesFile);
character.Sprite = await this.GetSpriteAsync(characterDefFile.DirectoryName, character.CharacterManager.Files.SpriteFiles);
character.Sprite = await reader.GetSpriteAsync(characterDefFile.DirectoryName, character.CharacterManager.Files.SpriteFiles);
character.AnimationManager = await this.FindManagerAsync<IAnimationManager>(characterDefFile.DirectoryName, character.CharacterManager.Files.AnimationFile);
character.Sound = await this.GetSoundAsync(characterDefFile.DirectoryName, character.CharacterManager.Files.SoundFile);
character.Sound = await reader.GetSoundAsync(characterDefFile.DirectoryName, character.CharacterManager.Files.SoundFile);
character.AiHints = this.ReadAiHints(characterDefFile.DirectoryName, character.CharacterManager.Files.AiHintsDataFile);
character.Intro = await this.ReadStoryboardAsync(characterDefFile.FullName, character.CharacterManager.Arcade.IntroStoryboardFile);
character.Ending = await this.ReadStoryboardAsync(characterDefFile.FullName, character.CharacterManager.Arcade.EndingStoryboardFile);
Expand Down
28 changes: 23 additions & 5 deletions Negum.Core/Loaders/IDataLoader.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System.IO;
using System.Threading.Tasks;
using Negum.Core.Containers;
using Negum.Core.Engines;
using Negum.Core.Managers.Types;
using Negum.Core.Models.Data;
using Negum.Core.Models.Sounds;
using Negum.Core.Models.Sprites;
using Negum.Core.Readers;

namespace Negum.Core.Loaders
{
Expand Down Expand Up @@ -33,9 +37,11 @@ public async Task<IData> LoadAsync(IEngine engine)
var data = new NegumData();

data.ConfigManager = await this.FindManagerAsync<IConfigurationManager>(dir, configFileName);

var reader = NegumContainer.Resolve<IFilePathReader>();

var motifFileName = data.ConfigManager.Options.MotifFile;
var motifFile = this.FindFile(dir, motifFileName);
var motifFile = reader.FindFile(dir, motifFileName);

if (motifFile == null)
{
Expand All @@ -45,8 +51,8 @@ public async Task<IData> LoadAsync(IEngine engine)
var motifDirFullName = motifFile.Directory.FullName;

data.MotifManager = await this.FindManagerAsync<IMotifManager>(dir, data.ConfigManager.Options.MotifFile);
data.MotifSprite = await this.GetSpriteAsync(motifDirFullName, data.MotifManager.Files.SpriteFile);
data.MotifSound = await this.GetSoundAsync(motifDirFullName, data.MotifManager.Files.SoundFile);
data.MotifSprite = await this.GetSpriteAsync(reader, motifDirFullName, data.MotifManager.Files.SpriteFile);
data.MotifSound = await this.GetSoundAsync(reader, motifDirFullName, data.MotifManager.Files.SoundFile);
data.SelectionManager = await this.FindManagerAsync<ISelectionManager>(motifDirFullName, data.MotifManager.Files.SelectionFile);
data.FightManager = await this.FindManagerAsync<IFightManager>(motifDirFullName, data.MotifManager.Files.FightFile);
data.Logo = await this.ReadStoryboardAsync(motifDirFullName, data.MotifManager.Files.LogoStoryboardDefinition);
Expand All @@ -55,14 +61,26 @@ public async Task<IData> LoadAsync(IEngine engine)
return data;
}

protected override FileInfo FindFile(string dirName, string fileName)
protected virtual async Task<ISprite> GetSpriteAsync(IFilePathReader reader, string dirName, string fileName)
{
fileName = this.CleanupFileName(fileName);
return await reader.GetSpriteAsync(dirName, fileName);
}

protected virtual async Task<ISound> GetSoundAsync(IFilePathReader reader, string dirName, string fileName)
{
fileName = this.CleanupFileName(fileName);
return await reader.GetSoundAsync(dirName, fileName);
}

protected virtual string CleanupFileName(string fileName)
{
if (fileName.StartsWith("data/"))
{
fileName = fileName.Replace("data/", "");
}

return base.FindFile(dirName, fileName);
return fileName;
}
}
}
20 changes: 5 additions & 15 deletions Negum.Core/Loaders/IFontLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Negum.Core.Containers;
using Negum.Core.Engines;
using Negum.Core.Managers.Types;
using Negum.Core.Models.Fonts;
using Negum.Core.Readers;

namespace Negum.Core.Loaders
{
Expand All @@ -30,26 +31,15 @@ public class FontLoader : AbstractLoader, IFontLoader
public async Task<IEnumerable<IFont>> LoadAsync(IEngine engine)
{
var sources = this.GetFiles(engine, "font")
.Where(file => file.Extension.Equals(".def") || file.Extension.Equals(".fnt"));
.Where(file => file.Extension.Equals(".fnt") || file.Extension.Equals(".def"));

return await this.LoadMultipleAsync(sources, this.GetFontAsync);
}

protected virtual async Task<IFont> GetFontAsync(FileInfo file)
{
var font = new Font
{
File = file,
IsRaw = file.Extension.Equals(".fnt")
};

if (!font.IsRaw)
{
font.Manager = await this.ReadManagerAsync<IFontManager>(file);
font.Sprite = await this.GetSpriteAsync(file.DirectoryName, font.Manager.Def.File);
}

return font;
var reader = NegumContainer.Resolve<IFontPathReader>();
return await reader.ReadAsync(file.FullName);
}
}
}
6 changes: 5 additions & 1 deletion Negum.Core/Loaders/IStageLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Negum.Core.Containers;
using Negum.Core.Engines;
using Negum.Core.Managers.Types;
using Negum.Core.Models.Stages;
using Negum.Core.Readers;

namespace Negum.Core.Loaders
{
Expand Down Expand Up @@ -55,11 +57,13 @@ public async Task<IEnumerable<IStage>> LoadAsync(IEngine engine)

protected virtual async Task<IStage> GetStageAsync(FileInfo defFile)
{
var reader = NegumContainer.Resolve<IFilePathReader>();

var stage = new Stage();

stage.File = defFile;
stage.Manager = await this.ReadManagerAsync<IStageManager>(defFile);
stage.Sprite = await this.GetSpriteAsync(defFile.DirectoryName, stage.Manager.BackgroundDef.SpriteFile);
stage.Sprite = await reader.GetSpriteAsync(defFile.DirectoryName, stage.Manager.BackgroundDef.SpriteFile);

return stage;
}
Expand Down
35 changes: 20 additions & 15 deletions Negum.Core/Models/Fonts/IFont.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Negum.Core.Managers.Types;
using Negum.Core.Models.Sprites;
using Negum.Core.Models.Math;

namespace Negum.Core.Models.Fonts
{
Expand All @@ -10,24 +9,29 @@ namespace Negum.Core.Models.Fonts
/// <author>
/// https://github.com/TheNegumProject/Negum.Core
/// </author>
public interface IFont : IFileReadable
public interface IFont
{
/// <summary>
/// Returns true if the current font was read from FNT file.
/// Size of font: width, height.
/// Width is used for spaces.
/// </summary>
bool IsRaw { get; }
IPoint Size { get; }

/// <summary>
/// Manager used to read this Font.
/// Could be null if IsRaw == true.
/// Spacing between font glyphs: width, height.
/// </summary>
IFontManager Manager { get; }
IPoint Spacing { get; }

/// <summary>
/// Sprite which contains information about glyphs used in the file.
/// Could be null if IsRaw == true OR if the file is TTF.
/// Drawing offset: x, y.
/// </summary>
ISprite Sprite { get; }
IPoint Offset { get; }

/// <summary>
/// Type of font.
/// I.e.: bitmap, Fixed, etc.
/// </summary>
string Type { get; }
}

/// <summary>
Expand All @@ -36,10 +40,11 @@ public interface IFont : IFileReadable
/// <author>
/// https://github.com/TheNegumProject/Negum.Core
/// </author>
public class Font : FileReadable, IFont
public class Font : IFont
{
public bool IsRaw { get; internal set; }
public IFontManager Manager { get; internal set; }
public ISprite Sprite { get; internal set; }
public IPoint Size { get; internal set; }
public IPoint Spacing { get; internal set; }
public IPoint Offset { get; internal set; }
public string Type { get; internal set; }
}
}
Loading

0 comments on commit fa23ab2

Please sign in to comment.