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

File path related fixes and improvements. #946

Merged
merged 1 commit into from
May 29, 2022
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
7 changes: 3 additions & 4 deletions UndertaleModTool/Editors/UndertaleSoundEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NAudio.Vorbis;
using NAudio.Wave;
using UndertaleModLib;
Expand Down Expand Up @@ -66,10 +65,10 @@ private void Play_Click(object sender, RoutedEventArgs e)
filename = sound.File.Content + ".ogg";
else
filename = sound.File.Content;
string audioPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName((Application.Current.MainWindow as MainWindow).FilePath), filename);
string audioPath = Path.Combine(Path.GetDirectoryName((Application.Current.MainWindow as MainWindow).FilePath), filename);
if (File.Exists(audioPath))
{
switch (System.IO.Path.GetExtension(filename).ToLower())
switch (Path.GetExtension(filename).ToLower())
{
case ".wav":
wavReader = new WaveFileReader(audioPath);
Expand Down Expand Up @@ -109,7 +108,7 @@ private void Play_Click(object sender, RoutedEventArgs e)
{
try
{
string path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName((Application.Current.MainWindow as MainWindow).FilePath), "audiogroup" + sound.GroupID + ".dat");
string path = Path.Combine(Path.GetDirectoryName((Application.Current.MainWindow as MainWindow).FilePath), "audiogroup" + sound.GroupID + ".dat");
if (File.Exists(path))
{
if (loadedPath != path)
Expand Down
29 changes: 18 additions & 11 deletions UndertaleModTool/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1155,11 +1155,17 @@ public string GenerateMD5(string filename)
private async Task LoadGMLCache(string filename, LoaderDialog dialog = null)
{
await Task.Run(() => {
if (SettingsWindow.UseGMLCache && File.Exists(Path.Join("GMLCache", "index")))
if (SettingsWindow.UseGMLCache)
{
string cacheDirPath = Path.Combine(ExePath, "GMLCache");
string cacheIndexPath = Path.Combine(cacheDirPath, "index");

if (!File.Exists(cacheIndexPath))
return;

dialog?.Dispatcher.Invoke(() => dialog.ReportProgress("Loading decompiled code cache..."));

string[] indexLines = File.ReadAllLines(Path.Join("GMLCache", "index"));
string[] indexLines = File.ReadAllLines(cacheIndexPath);

int num = -1;
for (int i = 0; i < indexLines.Length; i++)
Expand All @@ -1172,7 +1178,7 @@ await Task.Run(() => {
if (num == -1)
return;

if (!File.Exists(Path.Join("GMLCache", num.ToString())))
if (!File.Exists(Path.Combine(cacheDirPath, num.ToString())))
{
ShowWarning("Decompiled code cache file for open data is missing, but its name present in the index.");

Expand All @@ -1181,7 +1187,7 @@ await Task.Run(() => {

string hash = GenerateMD5(filename);

using (StreamReader fs = new(Path.Join("GMLCache", num.ToString())))
using (StreamReader fs = new(Path.Combine(cacheDirPath, num.ToString())))
{
string prevHash = fs.ReadLine();

Expand Down Expand Up @@ -1242,14 +1248,15 @@ await Task.Run(async () => {
{
dialog?.Dispatcher.Invoke(() => dialog.ReportProgress("Saving decompiled code cache..."));

if (!File.Exists(Path.Join("GMLCache", "index")))
string cacheDirPath = Path.Combine(ExePath, "GMLCache");
string cacheIndexPath = Path.Combine(cacheDirPath, "index");
if (!File.Exists(cacheIndexPath))
{
Directory.CreateDirectory("GMLCache");

File.WriteAllText(Path.Join("GMLCache", "index"), filename);
Directory.CreateDirectory(cacheDirPath);
File.WriteAllText(cacheIndexPath, filename);
}

List<string> indexLines = File.ReadAllLines(Path.Join("GMLCache", "index")).ToList();
List<string> indexLines = File.ReadAllLines(cacheIndexPath).ToList();

int num = -1;
for (int i = 0; i < indexLines.Count; i++)
Expand Down Expand Up @@ -1284,7 +1291,7 @@ await Task.Run(async () => {

string hash = GenerateMD5(filename);

using (FileStream fs = File.Create(Path.Join("GMLCache", num.ToString())))
using (FileStream fs = File.Create(Path.Combine(cacheDirPath, num.ToString())))
{
fs.Write(Encoding.UTF8.GetBytes(hash + '\n'));
fs.Write(SystemJson.JsonSerializer.SerializeToUtf8Bytes(sortedCache));
Expand All @@ -1296,7 +1303,7 @@ await Task.Run(async () => {
}
}

File.WriteAllLines(Path.Join("GMLCache", "index"), indexLines);
File.WriteAllLines(cacheIndexPath, indexLines);

Data.GMLCacheWasSaved = true;
}
Expand Down
8 changes: 5 additions & 3 deletions UndertaleModTool/Scripts/Repackers/ImportFontData.csx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ string importFolder = PromptChooseDirectory();
if (importFolder == null)
throw new ScriptException("The import folder was not set.");

System.IO.Directory.CreateDirectory("Packager");
string packagerDirPath = Path.Combine(ExePath, "Packager");
string sourcePath = importFolder;
string searchPattern = "*.png";
string outName = "Packager/atlas.txt";
string outName = Path.Combine(packagerDirPath, "atlas.txt");
int textureSize = 2048;
int border = 2;
bool debug = false;

Directory.CreateDirectory(packagerDirPath);
Packer packer = new Packer();
packer.Process(sourcePath, searchPattern, textureSize, border, debug);
packer.SaveAtlasses(outName);
Expand Down Expand Up @@ -91,7 +93,7 @@ ScriptMessage("Import Complete!");

public void fontUpdate(UndertaleFont newFont)
{
using(StreamReader reader = new StreamReader(sourcePath + "glyphs_" + newFont.Name.Content + ".csv"))
using (StreamReader reader = new StreamReader(Path.Combine(sourcePath, "glyphs_" + newFont.Name.Content + ".csv")))
{
newFont.Glyphs.Clear();
string line;
Expand Down
5 changes: 3 additions & 2 deletions UndertaleModTool/Scripts/Repackers/ImportGML_2_3.csx
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ if (importFolder == null)

List<string> CodeList = new List<string>();

if (File.Exists(importFolder + "/LookUpTable.txt"))
string tablePath = Path.Combine(importFolder, "LookUpTable.txt");
if (File.Exists(tablePath))
{
int counter = 0;
string line;
System.IO.StreamReader file = new System.IO.StreamReader(importFolder + "/" + "LookUpTable.txt");
StreamReader file = new StreamReader(tablePath);
while((line = file.ReadLine()) != null)
{
if (counter > 0)
Expand Down
3 changes: 1 addition & 2 deletions UndertaleModTool/Scripts/Repackers/ImportGraphics.csx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ bool importAsSprite = false;

string importFolder = CheckValidity();

string workDirectory = Path.GetDirectoryName(FilePath) + Path.DirectorySeparatorChar;
string packDir = Path.Combine(workDirectory, "Packager");
string packDir = Path.Combine(ExePath, "Packager");
Directory.CreateDirectory(packDir);

string sourcePath = importFolder;
Expand Down
26 changes: 8 additions & 18 deletions UndertaleModTool/Scripts/Repackers/ImportShaderData.csx
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
using System.Text;
using System;
using System.Linq;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

EnsureDataLoaded();

// "Select 'Import_Loc.txt' file in 'Shader_Data'"
string importFolder = PromptChooseDirectory();
if (importFolder == null)
throw new ScriptException("The import folder was not set.");

string[] dirFiles = Directory.GetFiles(importFolder, "*.*", SearchOption.AllDirectories);
List<string> shadersToModify = new List<string>();
var shadersToModify = Directory.GetDirectories(importFolder).Select(x => Path.GetFileName(x));
List<string> shadersExisting = new List<string>();
List<string> shadersNonExist = new List<string>();
foreach (string file in dirFiles)
{
if (Path.GetFileName(file) == "Import_Loc.txt")
continue;
else
{
shadersToModify.Add(Path.GetDirectoryName(file).Replace(importFolder, ""));
}
}
List<string> currentList = new List<string>();
string res = "";

for (var i = 0; i < shadersToModify.Count; i++)
foreach (string shaderName in shadersToModify)
{
currentList.Clear();
for (int j = 0; j < Data.Shaders.Count; j++)
Expand All @@ -36,14 +26,14 @@ for (var i = 0; i < shadersToModify.Count; i++)
res += (x + "\n");
currentList.Add(x);
}
if (Data.Shaders.ByName(shadersToModify[i]) != null)
if (Data.Shaders.ByName(shaderName) != null)
{
Data.Shaders.Remove(Data.Shaders.ByName(shadersToModify[i]));
AddShader(shadersToModify[i]);
Data.Shaders.Remove(Data.Shaders.ByName(shaderName));
AddShader(shaderName);
Reorganize<UndertaleShader>(Data.Shaders, currentList);
}
else
AddShader(shadersToModify[i]);
AddShader(shaderName);
}


Expand Down Expand Up @@ -257,7 +247,7 @@ void AddShader(string shader_name)
{
string line;
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader(localImportDir + "VertexShaderAttributes.txt");
StreamReader file = new StreamReader(localImportDir + "VertexShaderAttributes.txt");
while((line = file.ReadLine()) != null)
{
if (line != "")
Expand Down
13 changes: 6 additions & 7 deletions UndertaleModTool/Scripts/Repackers/NewTextureRepacker.csx
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ async Task<List<TPageItem>> dumpTexturePageItems(string dir, bool reuse)

var tpageitems = await Task.Run(() => Data.TexturePageItems
.AsParallel()
.Select(item => dumpTexturePageItem(item, worker, $"{dir}texture_page_{Data.TexturePageItems.IndexOf(item)}.png", reuse))
.Select(item => dumpTexturePageItem(item, worker, Path.Combine(dir, $"texture_page_{Data.TexturePageItems.IndexOf(item)}.png"), reuse))
.ToList());

worker.Cleanup();
Expand Down Expand Up @@ -366,18 +366,17 @@ if (maxArea <= 0)

bool reuseTextures = false;

// Setup work directory and packager directory
string workDirectory = Path.GetDirectoryName(FilePath) + Path.DirectorySeparatorChar;
string packagerDirectory = $"{workDirectory}{Path.DirectorySeparatorChar}Packager{Path.DirectorySeparatorChar}";
if (System.IO.Directory.Exists(packagerDirectory))
// Setup packager directory
string packagerDirectory = Path.Combine(ExePath, "Packager");
if (Directory.Exists(packagerDirectory))
{
DialogResult dr = MessageBox.Show("Do you want to reuse previously extracted page items?",
"Texture Repacker", MessageBoxButtons.YesNo);

reuseTextures = dr == DialogResult.Yes;
}

System.IO.DirectoryInfo dir = System.IO.Directory.CreateDirectory(packagerDirectory);
Directory.CreateDirectory(packagerDirectory);

// Dump all the texture page items
ResetProgress("Existing Textures Exported");
Expand Down Expand Up @@ -420,7 +419,7 @@ int lastTextPage = Data.EmbeddedTextures.Count - 1;

// Now recreate texture pages and link the items to the pages
ResetProgress("Regenerating Texture Pages");
using (var f = new StreamWriter($"{packagerDirectory}log.txt"))
using (var f = new StreamWriter(Path.Combine(packagerDirectory, "log.txt")))
{
var atlasCount = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ using UndertaleModLib.Util;

EnsureDataLoaded();

// Get directory paths
string workDirectory = Path.GetDirectoryName(FilePath) + Path.DirectorySeparatorChar;
System.IO.DirectoryInfo dir = System.IO.Directory.CreateDirectory(workDirectory + Path.DirectorySeparatorChar + "Packager");
// Get directory path
DirectoryInfo dir = Directory.CreateDirectory(Path.Combine(ExePath, "Packager"));

// Clear any files if they already exist
foreach (FileInfo file in dir.GetFiles())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ List<string> GetCodeList(string importFolder)
{
int counter = 0;
string line;
System.IO.StreamReader file = new System.IO.StreamReader(index_path);
StreamReader file = new StreamReader(index_path);
while ((line = file.ReadLine()) != null)
{
if ((counter > 0) && (line.Length >= 1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ if (dlg.ShowDialog() == true)
{
try
{
System.IO.StreamReader file = new System.IO.StreamReader(dlgin.FileName);
StreamReader file = new StreamReader(dlgin.FileName);
string line;
line = file.ReadLine();
using (var stream = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ Would you like to overwrite it?");
}
}

BuiltinList list = new BuiltinList();
list.Initialize(Data);
BuiltinList list = new BuiltinList(Data);

List<String> extensionFunctions = new List<String>();
List<String> unknownFunctions = new List<String>();
Expand Down Expand Up @@ -89,7 +88,7 @@ if (unknownFunctions.Count > 0)
resultsToDisplay += (unknownFunctions[i] + "\r\n");
}
resultsToDisplay = SimpleTextInput("Prune Menu", "Delete one or more lines to remove those entries", resultsToDisplay, true);
string[] IndividualLineArray = resultsToDisplay.Split('\n'}, StringSplitOptions.RemoveEmptyEntries);
string[] IndividualLineArray = resultsToDisplay.Split('\n', StringSplitOptions.RemoveEmptyEntries);
foreach (var OneLine in IndividualLineArray)
{
unknownFunctions2.Add(OneLine.Trim());
Expand All @@ -116,4 +115,9 @@ if (unknownFunctions.Count > 0)
removed = "The function(s)\r\n" + removed;
ScriptMessage(removed + "were removed.");
}
}
else
{
ScriptMessage("No unknown functions were found.");
File.Delete(exportFolder + "unknown_functions.txt");
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ foreach (string file in dirFiles)
throw new ScriptException(spriteName + " is missing one or more indexes. The detected missing index is: " + prevFrameName);
}

// Get directory paths
string workDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Substring(6);
System.IO.DirectoryInfo dir = System.IO.Directory.CreateDirectory(workDirectory + Path.DirectorySeparatorChar + "Packager");
// Get directory path
DirectoryInfo dir = Directory.CreateDirectory(Path.Combine(ExePath, "Packager"));

// Clear any files if they already exist
foreach (FileInfo file in dir.GetFiles())
Expand Down Expand Up @@ -181,17 +180,17 @@ DirectoryInfo textureDirectory = new DirectoryInfo(importFolder);
FileInfo[] files = textureDirectory.GetFiles(searchPattern, SearchOption.AllDirectories);
foreach (FileInfo file in files)
{
string destFile = System.IO.Path.Combine(exportedTexturesFolder, file.Name);
string sourceFile = System.IO.Path.Combine(importFolder, file.Name);
string destFile = Path.Combine(exportedTexturesFolder, file.Name);
string sourceFile = Path.Combine(importFolder, file.Name);
string stripped = Path.GetFileNameWithoutExtension(sourceFile);
if (assetCoordinateDict.ContainsKey(stripped))
assetCoordinateDict.Remove(stripped);
System.IO.File.Copy(sourceFile, destFile, true);
File.Copy(sourceFile, destFile, true);
}

try
{
string[] marginLines = System.IO.File.ReadAllLines(importFolder + Path.DirectorySeparatorChar + "margins.txt");
string[] marginLines = File.ReadAllLines(importFolder + Path.DirectorySeparatorChar + "margins.txt");
foreach (String str in marginLines)
{
string key = str.Substring(0, str.IndexOf(','));
Expand Down
1 change: 0 additions & 1 deletion UndertaleModTool/Scripts/Unpackers/ExportShaderData.csx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ if (exportFolder == null)
throw new ScriptException("The export folder was not set.");

Directory.CreateDirectory(exportFolder + "/Shader_Data/");
File.WriteAllText(exportFolder + "/Shader_Data/" + "Import_Loc.txt", "Import location");

foreach(UndertaleShader shader in Data.Shaders)
{
Expand Down