Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed AssetBrowser handling multiple files with same name. #14249

Merged
merged 1 commit into from Mar 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions OpenRA.Game/FileSystem/FileSystem.cs
Expand Up @@ -315,5 +315,10 @@ public static string ResolveAssemblyPath(string path, Manifest manifest, Install
var resolvedPath = Platform.ResolvePath(path);
return File.Exists(resolvedPath) ? resolvedPath : null;
}

public string GetPrefix(IReadOnlyPackage package)
{
return explicitMounts.ContainsValue(package) ? explicitMounts.First(f => f.Value == package).Key : null;
}
}
}
71 changes: 50 additions & 21 deletions OpenRA.Mods.Common/Widgets/Logic/AssetBrowserLogic.cs
Expand Up @@ -37,11 +37,11 @@ public class AssetBrowserLogic : ChromeLogic
ScrollItemWidget template;

IReadOnlyPackage assetSource = null;
List<string> availableShps = new List<string>();
bool animateFrames = false;

string currentPalette;
string currentFilename;
IReadOnlyPackage currentPackage;
Sprite[] currentSprites;
VqaPlayerWidget player = null;
bool isVideoLoaded = false;
Expand Down Expand Up @@ -116,7 +116,7 @@ public AssetBrowserLogic(Widget widget, Action onExit, ModData modData, World wo
}

filenameInput = panel.Get<TextFieldWidget>("FILENAME_INPUT");
filenameInput.OnTextEdited = () => ApplyFilter(filenameInput.Text);
filenameInput.OnTextEdited = () => ApplyFilter();
filenameInput.OnEscKey = filenameInput.YieldKeyboardFocus;

var frameContainer = panel.GetOrNull("FRAME_SELECTOR");
Expand Down Expand Up @@ -266,24 +266,26 @@ bool FilterAsset(string filename)
return false;
}

void ApplyFilter(string filename)
void ApplyFilter()
{
assetVisByName.Clear();
assetList.Layout.AdjustChildren();
assetList.ScrollToTop();

// Select the first visible
var firstVisible = assetVisByName.FirstOrDefault(kvp => kvp.Value);
if (firstVisible.Key != null)
LoadAsset(firstVisible.Key);
IReadOnlyPackage package;
string filename;

if (firstVisible.Key != null && modData.DefaultFileSystem.TryGetPackageContaining(firstVisible.Key, out package, out filename))
LoadAsset(package, filename);
}

void AddAsset(ScrollPanelWidget list, string filepath, ScrollItemWidget template)
void AddAsset(ScrollPanelWidget list, string filepath, IReadOnlyPackage package, ScrollItemWidget template)
{
var filename = Path.GetFileName(filepath);
var item = ScrollItemWidget.Setup(template,
() => currentFilename == filename,
() => { LoadAsset(filename); });
() => currentFilename == filepath && currentPackage == package,
() => { LoadAsset(package, filepath); });

item.Get<LabelWidget>("TITLE").GetText = () => filepath;
item.IsVisible = () =>
Expand All @@ -300,7 +302,7 @@ void AddAsset(ScrollPanelWidget list, string filepath, ScrollItemWidget template
list.AddChild(item);
}

bool LoadAsset(string filename)
bool LoadAsset(IReadOnlyPackage package, string filename)
{
if (isVideoLoaded)
{
Expand All @@ -312,27 +314,37 @@ bool LoadAsset(string filename)
if (string.IsNullOrEmpty(filename))
return false;

if (!modData.DefaultFileSystem.Exists(filename))
if (!package.Contains(filename))
return false;

isLoadError = false;

try
{
currentPackage = package;
currentFilename = filename;
var prefix = "";
var fs = modData.DefaultFileSystem as OpenRA.FileSystem.FileSystem;

if (fs != null)
{
prefix = fs.GetPrefix(package);
if (prefix != null)
prefix += "|";
}

if (Path.GetExtension(filename.ToLowerInvariant()) == ".vqa")
{
player = panel.Get<VqaPlayerWidget>("PLAYER");
currentFilename = filename;
player.Load(filename);
player.Load(prefix + filename);
player.DrawOverlay = false;
isVideoLoaded = true;
frameSlider.MaximumValue = (float)player.Video.Frames - 1;
frameSlider.Ticks = 0;
return true;
}

currentFilename = filename;
currentSprites = world.Map.Rules.Sequences.SpriteCache[filename];
currentSprites = world.Map.Rules.Sequences.SpriteCache[prefix + filename];
currentFrame = 0;
frameSlider.MaximumValue = (float)currentSprites.Length - 1;
frameSlider.Ticks = currentSprites.Length;
Expand Down Expand Up @@ -368,17 +380,34 @@ bool ShowSourceDropdown(DropDownButtonWidget dropdown)
void PopulateAssetList()
{
assetList.RemoveChildren();
availableShps.Clear();

var files = assetSource != null ? assetSource.Contents : modData.ModFiles.MountedPackages.SelectMany(f => f.Contents).Distinct();
foreach (var file in files.OrderBy(s => s))
var files = new SortedList<string, List<IReadOnlyPackage>>();

if (assetSource != null)
foreach (var content in assetSource.Contents)
files.Add(content, new List<IReadOnlyPackage> { assetSource });
else
{
if (allowedExtensions.Any(ext => file.EndsWith(ext, true, CultureInfo.InvariantCulture)))
foreach (var mountedPackage in modData.ModFiles.MountedPackages)
{
AddAsset(assetList, file, template);
availableShps.Add(file);
foreach (var content in mountedPackage.Contents)
{
if (!files.ContainsKey(content))
files.Add(content, new List<IReadOnlyPackage> { mountedPackage });
else
files[content].Add(mountedPackage);
}
}
}

foreach (var file in files.OrderBy(s => s.Key))
{
if (!allowedExtensions.Any(ext => file.Key.EndsWith(ext, true, CultureInfo.InvariantCulture)))
continue;

foreach (var package in file.Value)
AddAsset(assetList, file.Key, package, template);
}
}

bool ShowPaletteDropdown(DropDownButtonWidget dropdown, World world)
Expand Down