Skip to content

Commit

Permalink
Remove baseDir from setup, just use cwd
Browse files Browse the repository at this point in the history
  • Loading branch information
Chicken-Bones committed Apr 5, 2019
1 parent d734043 commit 8b9652b
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/src
/logs
/setup/logs
**/bin/
**/obj/
**/packages/
Expand Down
3 changes: 1 addition & 2 deletions setup.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
cd setup\bin
start setup.exe ..\..
start setup\bin\setup.exe
18 changes: 8 additions & 10 deletions setup/Setup/DecompileTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ public void WriteProjectFile(TextWriter writer, IEnumerable<Tuple<string, string
CSharpFormattingOptions = FormattingOptionsFactory.CreateAllman()
};

public string FullSrcDir => Path.Combine(baseDir, srcDir);

public DecompileTask(ITaskInterface taskInterface, string srcDir, bool serverOnly = false) : base(taskInterface)
{
this.srcDir = srcDir;
Expand All @@ -109,8 +107,8 @@ public override void Run()
{
taskInterface.SetStatus("Deleting Old Src");

if (Directory.Exists(FullSrcDir))
Directory.Delete(FullSrcDir, true);
if (Directory.Exists(srcDir))
Directory.Delete(srcDir, true);

var items = new List<WorkItem>();
var files = new HashSet<string>();
Expand Down Expand Up @@ -225,7 +223,7 @@ private WorkItem ExtractResource(string name, Resource res)
{
return new WorkItem("Extracting: " + name, () =>
{
var path = Path.Combine(FullSrcDir, name);
var path = Path.Combine(srcDir, name);
CreateParentDirectory(path);

var s = res.TryOpenStream();
Expand All @@ -250,7 +248,7 @@ private WorkItem DecompileSourceFile(DecompilerTypeSystem ts, IGrouping<string,
{
return new WorkItem("Decompiling: " + src.Key, () =>
{
var path = Path.Combine(FullSrcDir, src.Key);
var path = Path.Combine(srcDir, src.Key);
CreateParentDirectory(path);

using (var w = new StreamWriter(path))
Expand All @@ -266,7 +264,7 @@ private WorkItem WriteAssemblyInfo(DecompilerTypeSystem ts)
{
return new WorkItem("Decompiling: AssemblyInfo.cs", () =>
{
var path = Path.Combine(FullSrcDir, Path.Combine("Properties", "AssemblyInfo.cs"));
var path = Path.Combine(srcDir, "Properties/AssemblyInfo.cs");
CreateParentDirectory(path);

using (var w = new StreamWriter(path))
Expand All @@ -289,7 +287,7 @@ private WorkItem WriteProjectFile(PEFile module, Guid guid,
//flatten the file list
var files = sources.Select(src => Tuple.Create("Compile", src.Key))
.Concat(resources.Select(res => Tuple.Create("EmbeddedResource", res.Item1)))
.Concat(new[] {Tuple.Create("Compile", Path.Combine("Properties", "AssemblyInfo.cs"))});
.Concat(new[] {Tuple.Create("Compile", "Properties/AssemblyInfo.cs")});

//sort the assembly references
var refs = module.AssemblyReferences.OrderBy(r => r.Name).ToArray();
Expand All @@ -299,7 +297,7 @@ private WorkItem WriteProjectFile(PEFile module, Guid guid,

projectDecompiler.ProjectGuid = guid;

var path = Path.Combine(FullSrcDir, name);
var path = Path.Combine(srcDir, name);
CreateParentDirectory(path);

using (var w = new StreamWriter(path))
Expand All @@ -316,7 +314,7 @@ private WorkItem WriteProjectUserFile(PEFile module, string debugWorkingDir)
var name = Path.GetFileNameWithoutExtension(module.Name) + ".csproj.user";
return new WorkItem("Writing: " + name, () =>
{
var path = Path.Combine(FullSrcDir, name);
var path = Path.Combine(srcDir, name);
CreateParentDirectory(path);

using (var w = new StreamWriter(path))
Expand Down
36 changes: 16 additions & 20 deletions setup/Setup/DiffTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,34 @@ public class DiffTask : Task


public readonly string baseDir;
public readonly string srcDir;
public readonly string patchedDir;
public readonly string patchDir;
public readonly ProgramSetting<DateTime> cutoff;

public string FullBaseDir => Path.Combine(Program.baseDir, baseDir);
public string FullSrcDir => Path.Combine(Program.baseDir, srcDir);
public string FullPatchDir => Path.Combine(Program.baseDir, patchDir);

public DiffTask(ITaskInterface taskInterface, string baseDir, string srcDir, string patchDir,
ProgramSetting<DateTime> cutoff) : base(taskInterface)
{
this.baseDir = baseDir;
this.srcDir = srcDir;
this.patchedDir = srcDir;
this.patchDir = patchDir;
this.cutoff = cutoff;
}

public override void Run()
{
var patchFiles = new HashSet<string>(
Directory.EnumerateFiles(FullPatchDir, "*", SearchOption.AllDirectories)
.Select(file => RelPath(FullPatchDir, file)));
Directory.EnumerateFiles(patchDir, "*", SearchOption.AllDirectories)
.Select(file => RelPath(patchDir, file)));
var oldFiles = new HashSet<string>(
Directory.EnumerateFiles(FullBaseDir, "*", SearchOption.AllDirectories)
.Select(file => RelPath(FullBaseDir, file))
Directory.EnumerateFiles(baseDir, "*", SearchOption.AllDirectories)
.Select(file => RelPath(baseDir, file))
.Where(relPath => !relPath.EndsWith(".patch") && !excluded.Any(relPath.StartsWith)));

var items = new List<WorkItem>();

foreach (var file in Directory.EnumerateFiles(FullSrcDir, "*", SearchOption.AllDirectories))
foreach (var file in Directory.EnumerateFiles(patchedDir, "*", SearchOption.AllDirectories))
{
var relPath = RelPath(FullSrcDir, file);
var relPath = RelPath(patchedDir, file);
oldFiles.Remove(relPath);
if (!extensions.Any(relPath.EndsWith))
continue;
Expand All @@ -58,19 +54,19 @@ public override void Run()
if (excluded.Any(relPath.StartsWith) || File.GetLastWriteTime(file) < cutoff.Get())
continue;

items.Add(File.Exists(Path.Combine(FullBaseDir, relPath))
items.Add(File.Exists(Path.Combine(baseDir, relPath))
? new WorkItem("Creating Diff: " + relPath, () => Diff(relPath))
: new WorkItem("Copying: " + relPath, () => Copy(file, Path.Combine(FullPatchDir, relPath))));
: new WorkItem("Copying: " + relPath, () => Copy(file, Path.Combine(patchDir, relPath))));
}

ExecuteParallel(items);

taskInterface.SetStatus("Deleting Unnessesary Patches");
foreach (var file in patchFiles)
File.Delete(Path.Combine(FullPatchDir, file));
File.Delete(Path.Combine(patchDir, file));

taskInterface.SetStatus("Noting Removed Files");
var removedFileList = Path.Combine(FullPatchDir, RemovedFileList);
var removedFileList = Path.Combine(patchDir, RemovedFileList);
if (oldFiles.Count > 0)
File.WriteAllText(removedFileList, string.Join("\r\n", oldFiles));
else if (File.Exists(removedFileList))
Expand All @@ -81,8 +77,8 @@ public override void Run()

private void Diff(string relPath)
{
var srcFile = Path.Combine(FullSrcDir, relPath);
var baseFile = Path.Combine(FullBaseDir, relPath);
var srcFile = Path.Combine(patchedDir, relPath);
var baseFile = Path.Combine(baseDir, relPath);

/*string temp = null;
if (srcFile.EndsWith(".cs") && format != null)
Expand All @@ -92,12 +88,12 @@ private void Diff(string relPath)
baseFile = temp;
}*/

var patch = CallDiff(baseFile, srcFile, Path.Combine(baseDir, relPath), Path.Combine(srcDir, relPath));
var patch = CallDiff(baseFile, srcFile, Path.Combine(baseDir, relPath), Path.Combine(patchedDir, relPath));

/*if (temp != null)
File.Delete(temp);*/

var patchFile = Path.Combine(FullPatchDir, relPath + ".patch");
var patchFile = Path.Combine(patchDir, relPath + ".patch");
if (patch.Trim() != "") {
CreateParentDirectory(patchFile);
File.WriteAllText(patchFile, StripDestHunkOffsets(patch));
Expand Down
8 changes: 4 additions & 4 deletions setup/Setup/HookGenTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ public override void Run() {
if (!File.Exists(Program.TerrariaPath))
throw new FileNotFoundException(Program.TerrariaPath);

var outputPath = Path.Combine(Program.ReferencesDir, "TerrariaHooks.Windows.dll");
var outputPath = Path.Combine(Program.referencesDir, "TerrariaHooks.Windows.dll");
if (File.Exists(outputPath))
File.Delete(outputPath);

taskInterface.SetStatus($"Hooking: Terraria.exe -> TerrariaHooks.dll");
HookGen(Program.TerrariaPath, outputPath, Program.ReferencesDir);
HookGen(Program.TerrariaPath, outputPath, Program.referencesDir);

taskInterface.SetStatus($"XnaToFna: TerrariaHooks.Windows.dll -> TerrariaHooks.Mono.dll");

var monoPath = Path.Combine(Program.ReferencesDir, "TerrariaHooks.Mono.dll");
var monoPath = Path.Combine(Program.referencesDir, "TerrariaHooks.Mono.dll");
if (File.Exists(monoPath))
File.Delete(monoPath);

File.Copy(outputPath, monoPath);
XnaToFna(monoPath, Program.ReferencesDir);
XnaToFna(monoPath, Program.referencesDir);

File.Delete(Path.ChangeExtension(monoPath, "pdb"));
}
Expand Down
4 changes: 2 additions & 2 deletions setup/Setup/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private void RunTask(Task task)

private void RunTaskThread(Task task)
{
var errorLogFile = Path.Combine(Program.LogDir, "error.log");
var errorLogFile = Path.Combine(Program.logsDir, "error.log");
try
{
if (File.Exists(errorLogFile))
Expand Down Expand Up @@ -183,7 +183,7 @@ private void RunTaskThread(Task task)
labelStatus.Text = "Error: " + e.Message.Trim();
}));

Task.CreateDirectory(Program.LogDir);
Task.CreateDirectory(Program.logsDir);
File.WriteAllText(errorLogFile, status + "\r\n" + e);
}
finally
Expand Down
34 changes: 15 additions & 19 deletions setup/Setup/PatchTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@ public class PatchTask : Task

private readonly ConcurrentBag<PatchedFile> results = new ConcurrentBag<PatchedFile>();

public string FullBaseDir => Path.Combine(Program.baseDir, baseDir);
public string FullPatchedDir => Path.Combine(Program.baseDir, patchedDir);
public string FullPatchDir => Path.Combine(Program.baseDir, patchDir);

public PatchTask(ITaskInterface taskInterface, string baseDir, string srcDir, string patchDir, ProgramSetting<DateTime> cutoff) : base(taskInterface)
public PatchTask(ITaskInterface taskInterface, string baseDir, string patchedDir, string patchDir, ProgramSetting<DateTime> cutoff) : base(taskInterface)
{
this.baseDir = baseDir;
this.patchedDir = srcDir;
this.patchedDir = patchedDir;
this.patchDir = patchDir;
this.cutoff = cutoff;
}
Expand All @@ -51,44 +47,44 @@ public override void Run()
mode = (Patcher.Mode) Settings.Default.PatchMode;
taskInterface.SetStatus("Deleting Old Src");

if (Directory.Exists(FullPatchedDir))
Directory.Delete(FullPatchedDir, true);
if (Directory.Exists(patchedDir))
Directory.Delete(patchedDir, true);

var baseFiles = Directory.EnumerateFiles(FullBaseDir, "*", SearchOption.AllDirectories);
var patchFiles = Directory.EnumerateFiles(FullPatchDir, "*", SearchOption.AllDirectories);
var baseFiles = Directory.EnumerateFiles(baseDir, "*", SearchOption.AllDirectories);
var patchFiles = Directory.EnumerateFiles(patchDir, "*", SearchOption.AllDirectories);

var noCopy = new HashSet<string>();
var removedFileList = Path.Combine(FullPatchDir, DiffTask.RemovedFileList);
var removedFileList = Path.Combine(patchDir, DiffTask.RemovedFileList);
if(File.Exists(removedFileList))
foreach (var line in File.ReadAllLines(removedFileList))
noCopy.Add(line);

var items = new List<WorkItem>();
foreach (var file in patchFiles) {
var relPath = RelPath(FullPatchDir, file);
var relPath = RelPath(patchDir, file);
if (relPath.EndsWith(".patch")) {
items.Add(new WorkItem("Patching: " + relPath, () => Patch(file)));
noCopy.Add(relPath.Substring(0, relPath.Length - 6));
}
else if (relPath != DiffTask.RemovedFileList) {
items.Add(new WorkItem("Copying: " + relPath, () => Copy(file, Path.Combine(FullPatchedDir, relPath))));
items.Add(new WorkItem("Copying: " + relPath, () => Copy(file, Path.Combine(patchedDir, relPath))));
}
}

foreach (var file in baseFiles)
{
var relPath = RelPath(FullBaseDir, file);
var relPath = RelPath(baseDir, file);
if (DiffTask.excluded.Any(relPath.StartsWith) || noCopy.Contains(relPath))
continue;

var srcPath = Path.Combine(FullPatchedDir, relPath);
var srcPath = Path.Combine(patchedDir, relPath);
items.Add(new WorkItem("Copying: " + relPath, () => Copy(file, srcPath)));
}

try
{
CreateDirectory(Program.LogDir);
logFile = new StreamWriter(Path.Combine(Program.LogDir, "patch.log"));
CreateDirectory(Program.logsDir);
logFile = new StreamWriter(Path.Combine(Program.logsDir, "patch.log"));

taskInterface.SetMaxProgress(items.Count);
ExecuteParallel(items);
Expand Down Expand Up @@ -125,9 +121,9 @@ private void Patch(string patchPath)
var pf = new PatchedFile {
patchFilePath = patchPath,
patchFile = PatchFile.FromText(File.ReadAllText(patchPath)),
rootDir = Program.baseDir
rootDir = ""
};
pf.title = RelPath(FullBaseDir, pf.BasePath);
pf.title = RelPath(baseDir, pf.BasePath);

if (!File.Exists(pf.BasePath))
{
Expand Down
31 changes: 4 additions & 27 deletions setup/Setup/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ namespace Terraria.ModLoader.Setup
{
static class Program
{
public static string baseDir;
public static readonly string appDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
public static readonly string libDir = Path.Combine(appDir, "..", "lib");
public static readonly string toolsDir = Path.Combine(appDir, "..", "tools");
public static string LogDir => Path.Combine(baseDir, "logs");
public static string ReferencesDir => Path.Combine(baseDir, "references");
public static string TMLBinDir => Path.Combine(baseDir, "src", "tModLoader", "bin", "x86");
public static readonly string logsDir = Path.Combine("setup", "logs");
public static readonly string referencesDir = "references";

public static string SteamDir => Settings.Default.SteamDir;
public static string TerrariaPath => Path.Combine(SteamDir, "Terraria.exe");
Expand All @@ -38,18 +36,14 @@ static void Main(string[] args) {
if (name == "Mono.Cecil" && assemblyName.Version < new Version(0, 10)) //multiple cecil versions need to be loaded separately
name += "_0.9.6.0";

return ResolveAssemblyFrom(libDir, name) ?? ResolveAssemblyFrom(ReferencesDir, name);
return ResolveAssemblyFrom(libDir, name) ?? ResolveAssemblyFrom(referencesDir, name);
};

if (args.Length == 1 && args[0] == "--steamdir") {
Console.WriteLine(SteamDir);
return;
}

LoadBaseDir(args);
if (baseDir == null)
return;

Application.Run(new MainForm());
}

Expand All @@ -60,23 +54,6 @@ private static Assembly ResolveAssemblyFrom(string libDir, string name)
return path != null ? Assembly.LoadFrom(path) : null;
}

private static void LoadBaseDir(string[] args) {
if (args.Length > 0 && Directory.Exists(args[0])) {
baseDir = args[0];
return;
}

var dialog = new FolderBrowserDialog {
SelectedPath = Directory.GetCurrentDirectory(),
Description = "Select tModLoader root directory"
};

if (dialog.ShowDialog() != DialogResult.OK)
return;

baseDir = dialog.SelectedPath;
}

public static int RunCmd(string dir, string cmd, string args,
Action<string> output = null,
Action<string> error = null,
Expand Down Expand Up @@ -130,7 +107,7 @@ public static int RunCmd(string dir, string cmd, string args,
public static bool SelectTerrariaDialog() {
while (true) {
var dialog = new OpenFileDialog {
InitialDirectory = Path.GetFullPath(Directory.Exists(SteamDir) ? SteamDir : baseDir),
InitialDirectory = Path.GetFullPath(Directory.Exists(SteamDir) ? SteamDir : "."),
Filter = "Terraria|Terraria.exe",
Title = "Select Terraria.exe"
};
Expand Down
2 changes: 1 addition & 1 deletion setup/Setup/SelectFilesForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public string[] GetPaths() {

public string GetDirectory() {
var dir = GetPaths()[0];
return Directory.Exists(dir) ? dir : File.Exists(dir) ? Path.GetDirectoryName(dir) : Program.baseDir;
return Directory.Exists(dir) ? dir : File.Exists(dir) ? Path.GetDirectoryName(dir) : ".";
}

private void ValidatePaths() {
Expand Down
Loading

0 comments on commit 8b9652b

Please sign in to comment.