Skip to content

Commit

Permalink
"Save as" fixed (overwriting and file extensions)
Browse files Browse the repository at this point in the history
  • Loading branch information
andybak committed Apr 14, 2024
1 parent e0ef08f commit 3a7f442
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
17 changes: 17 additions & 0 deletions Assets/Scripts/API/ApiMethods.GlobalCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ public static void SaveOverwrite()
SketchControlsScript.m_Instance.IssueGlobalCommand(rEnum, -1, -1);
}

[ApiEndpoint(
"save.as",
"Saves the current scene under a new name. (No need to include the .tilt suffix)",
"newSketch"

)]
public static void SaveAs(string filename)
{
string suffix = SaveLoadScript.TILT_SUFFIX;
if (filename.EndsWith(suffix))
{
filename = filename.Substring(0, filename.Length - suffix.Length);
}
var rEnum = SketchControlsScript.GlobalCommands.SaveAs;
SketchControlsScript.m_Instance.IssueGlobalCommand(rEnum, sParam: filename);
}

[ApiEndpoint("save.new", "Saves the current scene in a new slot")]
public static void SaveNew()
{
Expand Down
62 changes: 31 additions & 31 deletions Assets/Scripts/Save/SaveLoadScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,17 @@ public void ResetLastFilename()
}

// Create a name that is guaranteed not to exist.
public string GenerateNewUntitledFilename(string directory, string extension)
public string GenerateNewFilename(string desiredFilename, string directory, string extension)
{
int iIndex = m_LastNonexistentFileIndex;
int iSanity = 9999;
while (iSanity > 0)
{
string attempt = UNTITLED_PREFIX + iIndex.ToString();
string attempt = desiredFilename;
if (iIndex > 0)
{
attempt += "_" + iIndex;
}
--iSanity;
++iIndex;

Expand All @@ -280,35 +284,23 @@ public string GenerateNewUntitledFilename(string directory, string extension)
return attempt;
}
}

Debug.Assert(false, "Could not generate a name");
return null;
}


// Create a name that is guaranteed not to exist.
public string GenerateNewUntitledFilename(string directory, string extension)
{
string filename = UNTITLED_PREFIX;
return GenerateNewFilename(filename, directory, extension);
}

// Create a Tiltasaurus based name that is guaranteed not to exist.
public string GenerateNewTiltasaurusFilename(string directory, string extension)
{
int iIndex = 0;
int iSanity = 9999;
while (iSanity > 0)
{
string attempt = TILTASAURUS_PREFIX + Tiltasaurus.m_Instance.Prompt;
if (iIndex > 0)
{
attempt += "_" + iIndex.ToString();
}
--iSanity;
++iIndex;

attempt = Path.Combine(directory, attempt) + extension;
if (!File.Exists(attempt) && !Directory.Exists(attempt))
{
return attempt;
}
}

Debug.Assert(false, "Could not generate a name");
return null;
string filename = TILTASAURUS_PREFIX + Tiltasaurus.m_Instance.Prompt;
return GenerateNewFilename(filename, directory, extension);
}

public void SaveOverwriteOrNewIfNotAllowed()
Expand Down Expand Up @@ -355,11 +347,21 @@ public string TransferredSourceIdFrom(SceneFileInfo info)
}
}

public DiskSceneFileInfo GetNewNameSceneFileInfo(bool tiltasaurusMode = false)
public DiskSceneFileInfo GetNewNameSceneFileInfo(bool tiltasaurusMode = false, string filename = null)
{
DiskSceneFileInfo fileInfo = tiltasaurusMode
? new DiskSceneFileInfo(GenerateNewTiltasaurusFilename(m_SaveDir, TILT_SUFFIX))
: new DiskSceneFileInfo(GenerateNewUntitledFilename(m_SaveDir, TILT_SUFFIX));
string uniquePath;
// If no filename is passed in then generate one
if (string.IsNullOrWhiteSpace(filename))
{
uniquePath = tiltasaurusMode
? GenerateNewTiltasaurusFilename(m_SaveDir, TILT_SUFFIX)
: GenerateNewUntitledFilename(m_SaveDir, TILT_SUFFIX);
}
else
{
uniquePath = GenerateNewFilename(filename, m_SaveDir, TILT_SUFFIX);
}
DiskSceneFileInfo fileInfo = new DiskSceneFileInfo(uniquePath);
if (m_LastSceneFile.Valid)
{
fileInfo.SourceId = TransferredSourceIdFrom(m_LastSceneFile);
Expand Down Expand Up @@ -407,9 +409,7 @@ public IEnumerator<Timeslice> SaveNewName(bool tiltasaurusMode = false)

public IEnumerator<Timeslice> SaveAs(string filename)
{
string path = Path.Join(m_SaveDir, filename);
Debug.Log($"SaveAs: {path}");
return SaveLow(GetSceneFileInfoFromName(path));
return SaveLow(GetNewNameSceneFileInfo(false, filename));
}

/// In order to for this to work properly:
Expand Down

0 comments on commit 3a7f442

Please sign in to comment.