Finished cleaning up texture pack code
This commit is contained in:
parent
f66ab8a2be
commit
f15744b7a2
@ -5,6 +5,7 @@ using System.Collections.Generic;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using TrueCraft.Core;
|
||||
using Ionic.Zip;
|
||||
|
||||
namespace TrueCraft.Client.Rendering
|
||||
{
|
||||
@ -85,7 +86,8 @@ namespace TrueCraft.Client.Rendering
|
||||
if (texturePack == null)
|
||||
throw new ArgumentException();
|
||||
|
||||
foreach (var entry in texturePack.Archive.Entries)
|
||||
var archive = new ZipFile(Path.Combine(TexturePack.TexturePackPath, texturePack.Name));
|
||||
foreach (var entry in archive.Entries)
|
||||
{
|
||||
// Make sure to 'silence' errors loading custom texture packs;
|
||||
// they're unimportant as we can just use default textures.
|
||||
|
@ -42,7 +42,6 @@ namespace TrueCraft.Client
|
||||
private MouseComponent MouseComponent { get; set; }
|
||||
private GameTime GameTime { get; set; }
|
||||
private Microsoft.Xna.Framework.Vector3 Delta { get; set; }
|
||||
private TexturePack TexturePack { get; set; }
|
||||
private TextureMapper TextureMapper { get; set; }
|
||||
|
||||
private BasicEffect OpaqueEffect, TransparentEffect;
|
||||
@ -131,9 +130,9 @@ namespace TrueCraft.Client
|
||||
TextureMapper.LoadDefaults(GraphicsDevice);
|
||||
|
||||
// Load any custom textures if needed.
|
||||
TexturePack = (UserSettings.Local.SelectedTexturePack != TexturePack.DefaultID) ?
|
||||
new TexturePack(UserSettings.Local.SelectedTexturePack) : null;
|
||||
TextureMapper = new TextureMapper(GraphicsDevice, TexturePack);
|
||||
TextureMapper = new TextureMapper(GraphicsDevice);
|
||||
if (UserSettings.Local.SelectedTexturePack != TexturePack.Default.Name)
|
||||
TextureMapper.AddTexturePack(TexturePack.FromArchive(Path.Combine(TexturePack.TexturePackPath, UserSettings.Local.SelectedTexturePack)));
|
||||
|
||||
DejaVu = new FontRenderer(
|
||||
new Font(Content, "Fonts/DejaVu", FontStyle.Regular),
|
||||
|
@ -12,7 +12,18 @@ namespace TrueCraft.Core
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public const string DefaultID = "#Default";
|
||||
public static readonly TexturePack Unknown = new TexturePack(
|
||||
"?",
|
||||
File.OpenRead(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content/default-pack.png")),
|
||||
File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content/default-pack.txt")));
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static readonly TexturePack Default = new TexturePack(
|
||||
"Default",
|
||||
File.OpenRead(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content/pack.png")),
|
||||
File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content/pack.txt")));
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
@ -29,7 +40,47 @@ namespace TrueCraft.Core
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string Path { get; private set; }
|
||||
/// <param name="path"></param>
|
||||
public static TexturePack FromArchive(string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path) || !File.Exists(path))
|
||||
throw new ArgumentException();
|
||||
|
||||
string description = Unknown.Description;
|
||||
Stream image = Unknown.Image;
|
||||
try
|
||||
{
|
||||
var archive = new ZipFile(path);
|
||||
foreach (var entry in archive.Entries)
|
||||
{
|
||||
if (entry.FileName == "pack.txt")
|
||||
{
|
||||
using (var stream = entry.OpenReader())
|
||||
{
|
||||
using (var reader = new StreamReader(stream))
|
||||
description = reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
else if (entry.FileName == "pack.png")
|
||||
{
|
||||
using (var stream = entry.OpenReader())
|
||||
{
|
||||
var buffer = new byte[entry.UncompressedSize];
|
||||
stream.Read(buffer, 0, buffer.Length);
|
||||
image = new MemoryStream((int)entry.UncompressedSize);
|
||||
image.Write(buffer, 0, buffer.Length);
|
||||
|
||||
// Fixes 'GLib.GException: Unrecognized image file format' on Linux.
|
||||
image.Seek(0, SeekOrigin.Begin);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { return null; }
|
||||
|
||||
string name = new FileInfo(path).Name;
|
||||
return new TexturePack(name, image, description);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
@ -39,7 +90,7 @@ namespace TrueCraft.Core
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public ZipFile Archive { get; private set; }
|
||||
public Stream Image { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
@ -49,88 +100,14 @@ namespace TrueCraft.Core
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public MemoryStream Image { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public bool IsCorrupt { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public TexturePack()
|
||||
/// <param name="name"></param>
|
||||
/// <param name="image"></param>
|
||||
/// <param name="description"></param>
|
||||
public TexturePack(string name, Stream image, string description)
|
||||
{
|
||||
Path = TexturePack.DefaultID;
|
||||
Archive = new ZipFile();
|
||||
Name = "Default";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
public TexturePack(string path)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path) || !File.Exists(path))
|
||||
MakeDefault();
|
||||
|
||||
Path = path;
|
||||
var fileInfo = new FileInfo(path); // A bit weird, but it works.
|
||||
Name = fileInfo.Name;
|
||||
try { Archive = new ZipFile(path); }
|
||||
catch { IsCorrupt = true; }
|
||||
|
||||
GetPackInfo();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
private void MakeDefault()
|
||||
{
|
||||
Path = TexturePack.DefaultID;
|
||||
Archive = new ZipFile();
|
||||
Name = "Default";
|
||||
Image = null;
|
||||
Description = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private void GetPackInfo()
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var entry in Archive.Entries)
|
||||
{
|
||||
if (entry.FileName == "pack.txt")
|
||||
{
|
||||
using (var stream = entry.OpenReader())
|
||||
{
|
||||
using (var reader = new StreamReader(stream))
|
||||
Description = reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
else if (entry.FileName == "pack.png")
|
||||
{
|
||||
using (var stream = entry.OpenReader())
|
||||
{
|
||||
// Better way to do this?
|
||||
var buffer = new byte[entry.UncompressedSize];
|
||||
stream.Read(buffer, 0, buffer.Length);
|
||||
Image = new MemoryStream((int)entry.UncompressedSize);
|
||||
Image.Write(buffer, 0, buffer.Length);
|
||||
|
||||
// Fixes 'GLib.GException: Unrecognized image file format' on Linux.
|
||||
Image.Seek(0, SeekOrigin.Begin);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { IsCorrupt = true; }
|
||||
Name = name;
|
||||
Image = image;
|
||||
Description = description;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ namespace TrueCraft.Core
|
||||
Username = "";
|
||||
Password = "";
|
||||
LastIP = "";
|
||||
SelectedTexturePack = TexturePack.DefaultID;
|
||||
SelectedTexturePack = TexturePack.Default.Name;
|
||||
FavoriteServers = new FavoriteServer[0];
|
||||
}
|
||||
|
||||
|
@ -11,10 +11,6 @@ namespace TrueCraft.Launcher.Views
|
||||
public class OptionView : VBox
|
||||
{
|
||||
public LauncherWindow Window { get; set; }
|
||||
public Image DefaultImage { get; set; }
|
||||
public string DefaultDescription { get; set; }
|
||||
public Image UnknownImage { get; set; }
|
||||
public string UnknownDescription { get; set; }
|
||||
|
||||
public Label OptionLabel { get; set; }
|
||||
public Label TexturePackLabel { get; set; }
|
||||
@ -31,15 +27,6 @@ namespace TrueCraft.Launcher.Views
|
||||
|
||||
public OptionView(LauncherWindow window)
|
||||
{
|
||||
DefaultImage = Image.FromFile(
|
||||
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content/pack.png"));
|
||||
DefaultDescription = File.ReadAllText(
|
||||
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content/pack.txt"));
|
||||
UnknownImage = Image.FromFile(
|
||||
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content/default-pack.png"));
|
||||
UnknownDescription = File.ReadAllText(
|
||||
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content/default-pack.txt"));
|
||||
|
||||
_texturePacks = new List<TexturePack>();
|
||||
_lastTexturePack = null;
|
||||
|
||||
@ -74,7 +61,7 @@ namespace TrueCraft.Launcher.Views
|
||||
var texturePack = _texturePacks[TexturePackListView.SelectedRow];
|
||||
if (_lastTexturePack != texturePack)
|
||||
{
|
||||
UserSettings.Local.SelectedTexturePack = texturePack.Path;
|
||||
UserSettings.Local.SelectedTexturePack = texturePack.Name;
|
||||
UserSettings.Local.Save();
|
||||
}
|
||||
};
|
||||
@ -103,9 +90,8 @@ namespace TrueCraft.Launcher.Views
|
||||
private void LoadTexturePacks()
|
||||
{
|
||||
// We load the default texture pack specially.
|
||||
var defaultPack = new TexturePack();
|
||||
_texturePacks.Add(defaultPack);
|
||||
AddTexturePackRow(defaultPack);
|
||||
_texturePacks.Add(TexturePack.Default);
|
||||
AddTexturePackRow(TexturePack.Default);
|
||||
|
||||
// Make sure to create the texture pack directory if there is none.
|
||||
if (!Directory.Exists(TexturePack.TexturePackPath))
|
||||
@ -117,8 +103,8 @@ namespace TrueCraft.Launcher.Views
|
||||
if (!zip.EndsWith(".zip"))
|
||||
continue;
|
||||
|
||||
var texturePack = new TexturePack(zip);
|
||||
if (!texturePack.IsCorrupt)
|
||||
var texturePack = TexturePack.FromArchive(zip);
|
||||
if (texturePack != null)
|
||||
{
|
||||
_texturePacks.Add(texturePack);
|
||||
AddTexturePackRow(texturePack);
|
||||
@ -129,19 +115,10 @@ namespace TrueCraft.Launcher.Views
|
||||
private void AddTexturePackRow(TexturePack pack)
|
||||
{
|
||||
var row = TexturePackStore.AddRow();
|
||||
var isDefault = (pack.Path == TexturePack.DefaultID);
|
||||
if (isDefault)
|
||||
{
|
||||
TexturePackStore.SetValue(row, TexturePackImageField, DefaultImage.WithSize(IconSize.Medium));
|
||||
TexturePackStore.SetValue(row, TexturePackNameField, pack.Name);
|
||||
TexturePackStore.SetValue(row, TexturePackDescField, DefaultDescription);
|
||||
}
|
||||
else
|
||||
{
|
||||
TexturePackStore.SetValue(row, TexturePackImageField, (pack.Image == null) ? UnknownImage.WithSize(IconSize.Medium) : Image.FromStream(pack.Image).WithSize(IconSize.Medium));
|
||||
TexturePackStore.SetValue(row, TexturePackNameField, pack.Name);
|
||||
TexturePackStore.SetValue(row, TexturePackDescField, pack.Description ?? UnknownDescription);
|
||||
}
|
||||
|
||||
TexturePackStore.SetValue(row, TexturePackImageField, Image.FromStream(pack.Image).WithSize(IconSize.Medium));
|
||||
TexturePackStore.SetValue(row, TexturePackNameField, pack.Name);
|
||||
TexturePackStore.SetValue(row, TexturePackDescField, pack.Description);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user