Skip to content
Draft
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
14 changes: 12 additions & 2 deletions TombIDE/TombIDE.Shared/NewStructure/Bases/GameProjectBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public abstract class GameProjectBase : IGameProject
public string Name { get; protected set; }
public string DirectoryPath { get; protected set; }

/// <summary>
/// The file name (not path) of the .trproj file. (e.g. <c>"MyProject.trproj"</c>)
/// </summary>
public string TrprojFileName { get; set; }

protected string CustomScriptDirectoryPath { get; set; }

public string LevelsDirectoryPath { get; set; }
Expand All @@ -51,6 +56,7 @@ public GameProjectBase(TrprojFile trproj, Version targetTrprojVersion)

Name = trproj.ProjectName;
DirectoryPath = Path.GetDirectoryName(trproj.FilePath);
TrprojFileName = Path.GetFileName(trproj.FilePath);

LevelsDirectoryPath = trproj.LevelSourcingDirectory;

Expand All @@ -70,6 +76,7 @@ public GameProjectBase(string name, string directoryPath, string levelsDirectory
{
Name = name;
DirectoryPath = directoryPath;
TrprojFileName = name + ".trproj";
LevelsDirectoryPath = levelsDirectoryPath;

if (SupportsCustomScriptPaths)
Expand All @@ -80,7 +87,7 @@ public GameProjectBase(string name, string directoryPath, string levelsDirectory
}

public virtual string GetTrprojFilePath()
=> Path.Combine(DirectoryPath, Path.GetFileNameWithoutExtension(GetEngineExecutableFilePath()) + ".trproj");
=> Path.Combine(DirectoryPath, TrprojFileName);

public virtual string GetLauncherFilePath()
{
Expand Down Expand Up @@ -154,7 +161,7 @@ public virtual LevelProject[] GetAllValidLevelProjects()
return result.ToArray();
}

public virtual void Rename(string newName, bool renameDirectory)
public virtual void Rename(string newName, bool renameDirectory, bool renameTrprojFile = false)
{
if (renameDirectory)
{
Expand All @@ -179,6 +186,9 @@ public virtual void Rename(string newName, bool renameDirectory)
}

Name = newName;

if (renameTrprojFile)
TrprojFileName = newName + ".trproj";
}

public virtual bool IsValid(out string errorMessage)
Expand Down
2 changes: 1 addition & 1 deletion TombIDE/TombIDE.Shared/NewStructure/IGameProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public interface IGameProject : IProject
List<string> GameLanguageNames { get; }

/// <summary>
/// .trproj file name = game's .exe file name (tomb4, PCTomb5, ...) + ".trproj"
/// Returns the full path to the project's .trproj file.
/// </summary>
string GetTrprojFilePath();

Expand Down
2 changes: 1 addition & 1 deletion TombIDE/TombIDE.Shared/NewStructure/IProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface IProject
/// <summary>
/// Renames the project and its directory if specified.
/// </summary>
void Rename(string newName, bool renameDirectory = false);
void Rename(string newName, bool renameDirectory = false, bool renameTrprojFile = false);

/// <summary>
/// Saves the project's settings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public bool IsValid(out string errorMessage)
public bool IsExternal(string relativeToLevelsDirectoryPath)
=> !DirectoryPath.StartsWith(relativeToLevelsDirectoryPath, StringComparison.OrdinalIgnoreCase);

public void Rename(string newName, bool renameDirectory = false)
public void Rename(string newName, bool renameDirectory = false, bool renameTrprojFile = false)
{
if (renameDirectory)
{
Expand Down
22 changes: 18 additions & 4 deletions TombIDE/TombIDE/Forms/FormRenameProject.Designer.cs

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

47 changes: 31 additions & 16 deletions TombIDE/TombIDE/Forms/FormRenameProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,50 @@ private void button_Apply_Click(object sender, EventArgs e)
throw new ArgumentException("Invalid name.");

bool renameDirectory = checkBox_RenameDirectory.Checked;
bool renameTrprojFile = checkBox_RenameTrproj.Checked;

if (newName == _targetProject.Name)
if (newName == _targetProject.Name && !renameDirectory && !renameTrprojFile)
{
// If the name hasn't changed, but the directory name is different and the user wants to rename it
if (Path.GetFileName(_targetProject.DirectoryPath) != newName && renameDirectory)
{
if (!Path.GetFileName(_targetProject.DirectoryPath).Equals(newName, StringComparison.OrdinalIgnoreCase))
{
string newDirectory = Path.Combine(Path.GetDirectoryName(_targetProject.DirectoryPath), newName);
DialogResult = DialogResult.Cancel;
return;
}

if (Directory.Exists(newDirectory))
throw new ArgumentException("A directory with the same name already exists in the parent directory.");
}
if (newName == _targetProject.Name && !renameDirectory)
{
// Only renaming the .trproj file
}
else if (newName == _targetProject.Name && renameDirectory)
{
if (!Path.GetFileName(_targetProject.DirectoryPath).Equals(newName, StringComparison.OrdinalIgnoreCase))
{
string newDirectory = Path.Combine(Path.GetDirectoryName(_targetProject.DirectoryPath), newName);

_targetProject.Rename(newName, true);
_targetProject.Save();
if (Directory.Exists(newDirectory))
throw new ArgumentException("A directory with the same name already exists in the parent directory.");
}
else
DialogResult = DialogResult.Cancel;
}
else
{
string newDirectory = Path.Combine(Path.GetDirectoryName(_targetProject.DirectoryPath), newName);

if (renameDirectory && Directory.Exists(newDirectory) && !newDirectory.Equals(_targetProject.DirectoryPath, StringComparison.OrdinalIgnoreCase))
throw new ArgumentException("A directory with the same name already exists in the parent directory.");
}

string oldTrprojFilePath = _targetProject.GetTrprojFilePath();

_targetProject.Rename(newName, renameDirectory, renameTrprojFile);
_targetProject.Save();

// Clean up old .trproj file after successful save
if (renameTrprojFile)
{
// After a directory rename, the old file is now inside the new directory
string oldTrprojFileName = Path.GetFileName(oldTrprojFilePath);
string oldTrprojPathAfterRename = Path.Combine(_targetProject.DirectoryPath, oldTrprojFileName);

_targetProject.Rename(newName, renameDirectory);
_targetProject.Save();
if (File.Exists(oldTrprojPathAfterRename) && !oldTrprojPathAfterRename.Equals(_targetProject.GetTrprojFilePath(), StringComparison.OrdinalIgnoreCase))
File.Delete(oldTrprojPathAfterRename);
}
}
catch (Exception ex)
Expand Down