Skip to content

Commit

Permalink
Terra Launcher Integration
Browse files Browse the repository at this point in the history
* Also fixed a naming issue in the error message box.
  • Loading branch information
trigger-segfault committed Sep 24, 2017
1 parent 7a46b0e commit b61f713
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 7 deletions.
36 changes: 33 additions & 3 deletions TerrariaItemModifier/MainWindow.xaml.cs
Expand Up @@ -43,7 +43,10 @@ public partial class MainWindow : Window {
LoadSettings();

// Disable drag/drop text in textboxes so you can scroll their contents easily
DataObject.AddCopyingHandler(textBoxExe, (sender, e) => { if (e.IsDragDrop) e.CancelCommand(); });
DataObject.AddCopyingHandler(textBoxExe, OnTextBoxCancelDrag);

// Remove quotes from "Copy Path" command on paste
DataObject.AddPastingHandler(textBoxExe, OnTextBoxQuotesPaste);
}

#endregion
Expand Down Expand Up @@ -129,6 +132,26 @@ public partial class MainWindow : Window {
private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e) {
SaveSettings();
}

private void OnTextBoxCancelDrag(object sender, DataObjectCopyingEventArgs e) {
if (e.IsDragDrop)
e.CancelCommand();
}
private void OnTextBoxQuotesPaste(object sender, DataObjectPastingEventArgs e) {
var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true);
if (!isText) return;

var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
if (text.StartsWith("\"") || text.EndsWith("\"")) {
text = text.Trim('"');
Clipboard.SetText(text);
}
}

#endregion
//--------------------------------
#region Patching

private void OnPatch(object sender = null, RoutedEventArgs e = null) {
MessageBoxResult result;
if (!ValidPathTest())
Expand Down Expand Up @@ -268,10 +291,17 @@ public partial class MainWindow : Window {

private void OnLaunchTerraria(object sender, RoutedEventArgs e) {
try {
if (File.Exists(Patcher.ExePath))
if (File.Exists(Patcher.ExePath)) {
Process.Start(Patcher.ExePath);
else
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = Patcher.ExePath;
start.Arguments = TerrariaLocator.FindTerraLauncherSaveDirectory(Patcher.ExePath);
start.WorkingDirectory = Patcher.ExeDirectory;
Process.Start(start);
}
else {
TriggerMessageBox.Show(this, MessageIcon.Warning, "Could not locate the Terraria executable! Cannot launch Terraria.", "Missing Executable");
}
}
catch {
TriggerMessageBox.Show(this, MessageIcon.Warning, "The current path to Terraria is invalid! Cannot launch Terraria.", "Invalid Path");
Expand Down
86 changes: 84 additions & 2 deletions TerrariaItemModifier/Util/TerrariaLocator.cs
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.Win32;

namespace TerrariaItemModifier.Util {
Expand All @@ -14,6 +15,8 @@ public static class TerrariaLocator {

/**<summary>The located or empty Terraria executable path.</summary>*/
public static readonly string TerrariaPath;
/**<summary>The path to Terra Launcher's configuration file.</summary>*/
public static readonly string TerraLauncherConfigPath;

#endregion
//========= CONSTRUCTORS =========
Expand All @@ -22,19 +25,98 @@ public static class TerrariaLocator {
/**<summary>Start looking for the Terraria executable.</summary>*/
static TerrariaLocator() {
TerrariaPath = FindTerrariaPath();
TerraLauncherConfigPath = FindConfigPath();
}

#endregion
//========== SAVE PATHS ==========
#region Save Paths

/**<summary>Reads the specified executable's selected save directory from Terra Launcher's config.</summary>*/
public static string FindTerraLauncherSaveDirectory(string exePath) {
string arguments = "";
if (!string.IsNullOrWhiteSpace(TerraLauncherConfigPath)) {
string oldCurrentDirectory = Directory.GetCurrentDirectory();
try {
// Set the current directory for relative paths
Directory.SetCurrentDirectory(Path.GetDirectoryName(TerraLauncherConfigPath));

exePath = Path.GetFullPath(exePath).ToLower();

XmlDocument doc = new XmlDocument();
doc.Load(TerraLauncherConfigPath);

// Check if the user disable Trigger Tool Integration
bool boolValue;
XmlNode integration = doc.SelectSingleNode("TerraLauncher/Integration");
if (integration != null && bool.TryParse(integration.InnerText, out boolValue) && boolValue) {
XmlNode gamesNode = doc.SelectSingleNode("TerraLauncher/Games");
if (gamesNode != null) {
string result = ReadConfigFolder(exePath, gamesNode);
if (!string.IsNullOrWhiteSpace(result))
arguments = "-savedirectory \"" + result + "\"";
}
}
}
catch { }
// Set the current directory back
Directory.SetCurrentDirectory(oldCurrentDirectory);
}
return arguments;
}
/**<summary>Reads a Terra Launcher config folder and returns the save directory.</summary>*/
private static string ReadConfigFolder(string exePath, XmlNode folderNode) {
XmlNodeList nodeList = folderNode.SelectNodes("Folder");
foreach (XmlNode node in nodeList) {
string result = ReadConfigFolder(exePath, node);
if (!string.IsNullOrWhiteSpace(result))
return result;
}

nodeList = folderNode.SelectNodes("Game");

foreach (XmlNode node in folderNode) {
string result = null;
if (node.Name == "Folder") {
result = ReadConfigFolder(exePath, node);
}
else if (node.Name == "Game") {
XmlNode exeNode = node.SelectSingleNode("ExePath");
if (exeNode != null && Path.GetFullPath(exeNode.InnerText).ToLower() == exePath) {
XmlNode saveNode = node.SelectSingleNode("SaveDirectory");
if (saveNode != null && !string.IsNullOrWhiteSpace(saveNode.InnerText))
return saveNode.InnerText;
}
}
if (!string.IsNullOrWhiteSpace(result))
return result;
}
foreach (XmlNode node in nodeList) {
XmlNode exeNode = node.SelectSingleNode("ExePath");
if (exeNode != null && Path.GetFullPath(exeNode.InnerText).ToLower() == exePath) {
XmlNode saveNode = node.SelectSingleNode("SaveDirectory");
if (saveNode != null && !string.IsNullOrWhiteSpace(saveNode.InnerText))
return saveNode.InnerText;
}
}
return null;
}

#endregion
//=========== LOCATORS ===========
#region Locators

/**<summary>Starts looking for the Terra Launcher config path.</summary>*/
private static string FindConfigPath() {
return Registry.GetValue("HKEY_CURRENT_USER\\Software\\TriggersToolsGames\\TerraLauncher", "ConfigPath", null) as string;
}
/**<summary>Starts looking for the Terraria executable.</summary>*/
private static string FindTerrariaPath() {
try {
// Check the windows registry for steam installation path
string steamPath = Registry.GetValue("HKEY_CURRENT_USER\\Software\\Valve\\Steam", "SteamPath", null) as string;
string result = SeekDirectory(steamPath);
if (result != null) {
if (!string.IsNullOrWhiteSpace(result)) {
return result;
}
}
Expand All @@ -50,7 +132,7 @@ public static class TerrariaLocator {
else if (envVar.Key.ToLower().Contains("steam")) {
result = SeekDirectory(envVar.Value);
}
if (result != null) {
if (!string.IsNullOrWhiteSpace(result)) {
return result;
}
}
Expand Down
4 changes: 2 additions & 2 deletions TerrariaItemModifier/Windows/ErrorMessageBox.xaml
Expand Up @@ -5,10 +5,10 @@

<Grid x:Name="clientArea" Width="504" Height="230">
<Label Margin="18,15,18,0" VerticalAlignment="Top" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Padding="0">
<TextBlock TextWrapping="WrapWithOverflow" Text="Unhandled Exception in Terra Launcher!" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="#FF952727" FontWeight="Bold"/>
<TextBlock TextWrapping="WrapWithOverflow" Text="Unhandled Exception in Terraria Item Modifier!" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="#FF952727" FontWeight="Bold"/>
</Label>
<Label HorizontalAlignment="Right" Margin="0,10,18,0" VerticalAlignment="Top" FontWeight="Bold">
<Hyperlink NavigateUri="https://github.com/trigger-death/TerraLauncher" RequestNavigate="OnRequestNavigate" Focusable="False">
<Hyperlink NavigateUri="https://github.com/trigger-death/TerrariaItemModifier" RequestNavigate="OnRequestNavigate" Focusable="False">
<Run Text="Github Page"/>
</Hyperlink>
</Label>
Expand Down

0 comments on commit b61f713

Please sign in to comment.