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

Import TB CSV objects file #118

Merged
merged 6 commits into from
Sep 1, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion GameRealisticMap.Arma3/IO/ProjectDrive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public IEnumerable<string> FindAll(string pattern)
{
var physical = Directory.GetFiles(mountPath, pattern, SearchOption.AllDirectories)
.Select(file => file.Substring(mountPath.Length).TrimStart('\\'))
.Where(file => !file.StartsWith("temp\\", StringComparison.OrdinalIgnoreCase));
.Where(file => !file.StartsWith("temp\\", StringComparison.OrdinalIgnoreCase) && !file.StartsWith("grm-temp\\", StringComparison.OrdinalIgnoreCase));
if (secondarySource != null)
{
return secondarySource.FindAll(pattern).Concat(physical).Distinct(StringComparer.OrdinalIgnoreCase);
Expand Down
16 changes: 16 additions & 0 deletions GameRealisticMap.Arma3/TerrainBuilder/AmbiguousModelName.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace GameRealisticMap.Arma3.TerrainBuilder
{
public sealed class AmbiguousModelName : ApplicationException
{
public AmbiguousModelName(string name, IReadOnlyCollection<string> candidates)
: base($"Name '{name}' matches multiples files : '{string.Join("', '", candidates)}'")
{
Name = name;
Candidates = candidates;
}

public string Name { get; }

public IReadOnlyCollection<string> Candidates { get; }
}
}
4 changes: 4 additions & 0 deletions GameRealisticMap.Arma3/TerrainBuilder/IModelInfoLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ public interface IModelInfoLibrary
{
ModelInfo ResolveByName(string name);

bool TryResolveByName(string name, [MaybeNullWhen(false)] out ModelInfo model);

ModelInfo ResolveByPath(string path);

bool TryResolveByPath(string path, [MaybeNullWhen(false)] out ModelInfo model);

bool TryRegister(string name, string path);
}
}
20 changes: 19 additions & 1 deletion GameRealisticMap.Arma3/TerrainBuilder/ModelInfoLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public ModelInfoLibrary(IGameFileSystem fileSystem)
this.fileSystem = fileSystem;
}

public bool TryResolveByName(string name, [MaybeNullWhen(false)] out ModelInfo model)
{
return indexByName.TryGetValue(name, out model);
}

public ModelInfo ResolveByName(string name)
{
if (!indexByName.TryGetValue(name, out var modelInfo))
Expand All @@ -35,7 +40,7 @@ public ModelInfo ResolveByName(string name)
}
if (candidates.Count > 1)
{
throw new ApplicationException($"Name '{name}' matches multiples files : '{string.Join("', '", candidates)}'");
throw new AmbiguousModelName(name, candidates);
}
throw new ApplicationException($"Unknown model '{name}'");
}
Expand Down Expand Up @@ -215,5 +220,18 @@ public Task Save()
Directory.CreateDirectory(Path.GetDirectoryName(DefaultCachePath)!);
return SaveTo(DefaultCachePath);
}

public bool TryRegister(string name, string path)
{
var odol = ReadModelInfoOnly(path);
if (odol == null)
{
return false;
}

var model = new ModelInfo(name, path, odol.BoundingCenter.Vector3);
indexByName.Add(name, model);
return true;
}
}
}
63 changes: 63 additions & 0 deletions GameRealisticMap.Studio/Labels.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions GameRealisticMap.Studio/Labels.fr.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1108,4 +1108,25 @@ Dernière génération le {1}</value>
<data name="PartHasBeenAlreadyImported" xml:space="preserve">
<value>La partie #{0} a déjà été importée.</value>
</data>
<data name="RelativeElevation" xml:space="preserve">
<value>Positon relative (au sol)</value>
</data>
<data name="NoValidObjectDefinitionFoundInFile" xml:space="preserve">
<value>Aucune définition valide d'objet n'a été trouvé dans le fichier.</value>
</data>
<data name="NumObjectsToImport" xml:space="preserve">
<value>{0} objets à importer</value>
</data>
<data name="ImportObjectsFromFile" xml:space="preserve">
<value>Importer des objets depuis un fichier</value>
</data>
<data name="ImportsObjects" xml:space="preserve">
<value>Importer les objets</value>
</data>
<data name="ElevationMode" xml:space="preserve">
<value>Mode d'altitude</value>
</data>
<data name="AbsoluteElevation" xml:space="preserve">
<value>Position absolue</value>
</data>
</root>
21 changes: 21 additions & 0 deletions GameRealisticMap.Studio/Labels.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1108,4 +1108,25 @@ Last generated on {1}</value>
<data name="ImportNextPartPrompt" xml:space="preserve">
<value>Click on Refresh button when part #{0} is copied to continue import.</value>
</data>
<data name="ImportObjectsFromFile" xml:space="preserve">
<value>Import objects from file</value>
</data>
<data name="ImportsObjects" xml:space="preserve">
<value>Import objects</value>
</data>
<data name="RelativeElevation" xml:space="preserve">
<value>Relative elevation (to ground)</value>
</data>
<data name="AbsoluteElevation" xml:space="preserve">
<value>Absolute elevation</value>
</data>
<data name="ElevationMode" xml:space="preserve">
<value>Elevation mode</value>
</data>
<data name="NoValidObjectDefinitionFoundInFile" xml:space="preserve">
<value>No valid object definition found in file.</value>
</data>
<data name="NumObjectsToImport" xml:space="preserve">
<value>{0} objects to import</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Threading.Tasks;

namespace GameRealisticMap.Studio.Modules.Arma3WorldEditor.ViewModels
{
internal class AmbiguousItem
{
private readonly FileImporterViewModel parent;

public AmbiguousItem(FileImporterViewModel parent, string name, string path, Uri preview)
{
this.parent = parent;
Name = name;
Path = path;
Preview = preview;
}

public string Name { get; }

public string Path { get; }

public Uri Preview { get; }

public Task Resolve()
{
return parent.Resolve(Name,Path);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
using GameRealisticMap.Arma3.GameEngine;
using GameRealisticMap.Arma3.GameLauncher;
using GameRealisticMap.Arma3.IO;
using GameRealisticMap.Arma3.TerrainBuilder;
using GameRealisticMap.ElevationModel;
using GameRealisticMap.Reporting;
using GameRealisticMap.Studio.Modules.Arma3Data;
using GameRealisticMap.Studio.Modules.Arma3Data.Services;
using GameRealisticMap.Studio.Modules.Reporting;
using GameRealisticMap.Studio.Toolkit;
using Gemini.Framework;
using Microsoft.Win32;

namespace GameRealisticMap.Studio.Modules.Arma3WorldEditor.ViewModels
{
Expand Down Expand Up @@ -178,6 +182,8 @@ public float? SizeInMeters

public IGameFileSystem GameFileSystem => arma3Data.ProjectDrive;

public IModelInfoLibrary Library => arma3Data.Library;

public List<ModDependencyDefinition> Dependencies { get; set; } = new List<ModDependencyDefinition>();

public List<RevisionHistoryEntry> Backups
Expand Down Expand Up @@ -268,16 +274,61 @@ public Task ImportEden()
return windowManager.ShowDialogAsync(new EdenImporterViewModel(this));
}

public Task ImportFile()
{
var dialog = new OpenFileDialog();
dialog.Filter = "Text File|*.txt";
if (dialog.ShowDialog() == true)
{
return windowManager.ShowDialogAsync(new FileImporterViewModel(this, dialog.FileName));
}
return Task.CompletedTask;
}

internal void Apply(WrpEditBatch batch)
{
IoC.Get<IProgressTool>().RunTask("Import", task => {
Apply(batch, task);
return Task.CompletedTask;
}, false);
}

internal void Apply(List<TerrainBuilderObject> list)
{
IoC.Get<IProgressTool>().RunTask("Import", async task =>
{
if (World == null)
{
return;
}
await arma3Data.SaveLibraryCache();
var size = World.TerrainRangeX;
var grid = new ElevationGrid(size, CellSize!.Value);
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
grid[x, y] = World.Elevation[x + (y * size)];
}
}
var batch = new WrpEditBatch();
batch.Add.AddRange(list
.ProgressStep(task, "ToWrpObject")
.Select(l => l.ToWrpObject(grid))
.Select(o => new WrpAddObject(o.Transform.Matrix, o.Model)));
Apply(batch, task);
}, false);
}

private void Apply(WrpEditBatch batch, IProgressTaskUI task)
{
if (World == null)
{
return;
}
using var task = IoC.Get<IProgressTool>().StartTask("Import");
var processor = new WrpEditProcessor(task);
processor.Process(World, batch);
if ( ConfigFile != null)
if (ConfigFile != null)
{
if (Backups.Count > 0)
{
Expand Down Expand Up @@ -309,5 +360,7 @@ public void ClearActive()
entry.IsActive = false;
}
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public Task ClipboardImport()
{
if (Batch != null)
{
_ = Task.Run(() => parent.Apply(Batch));
parent.Apply(Batch);
}
return TryCloseAsync(true);
}
Expand Down
Loading
Loading