Skip to content

Commit

Permalink
0.2.0 - added runtime execution
Browse files Browse the repository at this point in the history
  • Loading branch information
ImAxel0 committed Apr 2, 2024
1 parent 5a5ab7c commit ac242d8
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 16 deletions.
35 changes: 24 additions & 11 deletions RedNodeEditor/ModBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,23 @@ public class ModBuilder
{ typeof(CustomEventNode), new List<NodeConnection> () },
};

public static ModData MakeModData(string modAuthor, string modVersion)
public static ModData MakeModData(string modAuthor, string modVersion, bool isRuntime = false)
{
if (string.IsNullOrEmpty(modAuthor))
if (!isRuntime)
{
Logger.Append("Error building the mod: mod author field can't be empty");
User32.MessageBox(IntPtr.Zero, "Mod author field can't be empty", "Error building the mod", User32.MB_FLAGS.MB_ICONWARNING | User32.MB_FLAGS.MB_TOPMOST);
return null;
}
if (string.IsNullOrEmpty(modAuthor))
{
Logger.Append("Error building the mod: mod author field can't be empty");
User32.MessageBox(IntPtr.Zero, "Mod author field can't be empty", "Error building the mod", User32.MB_FLAGS.MB_ICONWARNING | User32.MB_FLAGS.MB_TOPMOST);
return null;
}

if (string.IsNullOrEmpty(modVersion))
{
Logger.Append("Error building the mod: mod version field can't be empty");
User32.MessageBox(IntPtr.Zero, "Mod version field can't be empty", "Error building the mod", User32.MB_FLAGS.MB_ICONWARNING | User32.MB_FLAGS.MB_TOPMOST);
return null;
if (string.IsNullOrEmpty(modVersion))
{
Logger.Append("Error building the mod: mod version field can't be empty");
User32.MessageBox(IntPtr.Zero, "Mod version field can't be empty", "Error building the mod", User32.MB_FLAGS.MB_ICONWARNING | User32.MB_FLAGS.MB_TOPMOST);
return null;
}
}

foreach (var connList in BasePair.Values)
Expand Down Expand Up @@ -228,6 +231,16 @@ public static void SerializeModData(ModData modData)
CleanForNextBuild();
}

public static string SerializeModDataAsString(ModData modData)
{
var derived = GetDerivedTypes(typeof(SonsNode));
XmlSerializer xmlSerializer = new XmlSerializer(typeof(ModData), derived);
StringWriter stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter, modData);
CleanForNextBuild();
return stringWriter.ToString();
}

static void CleanForNextBuild()
{
var ifNodes = GraphEditor.GraphNodes.Where(node => node.GetType() == typeof(IfStatementNode));
Expand Down
69 changes: 69 additions & 0 deletions RedNodeEditor/PipeManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using RedLoader;
using System;
using System.Collections.Generic;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Vanara.PInvoke;

namespace RedNodeEditor;

public class PipeManager
{
static readonly string _pipeName = "RedNodePipe";

public static void ExecuteMod()
{
var modData = ModBuilder.MakeModData("RedNodeEditor", "1.0.0", true);
if (modData != null)
{
var modContent = ModBuilder.SerializeModDataAsString(modData);
if (Send(modContent, _pipeName))
{
Logger.Append("Mod executed successfully!");
User32.MessageBox(IntPtr.Zero, "Mod executed successfully!", "Runtime mod execution", User32.MB_FLAGS.MB_TOPMOST);
return;
}
}
Logger.Append("Couldn't execute mod at runtime");
User32.MessageBox(IntPtr.Zero, "Couldn't execute mod at runtime. Check if the game is running and RedNodeLoader is correctly installed"
, "Runtime mod execution", User32.MB_FLAGS.MB_ICONERROR | User32.MB_FLAGS.MB_TOPMOST);
}

public static void StopExecution()
{
if (Send("stop", _pipeName))
{
Logger.Append("Mod stopped successfully!");
User32.MessageBox(IntPtr.Zero, "Mod stopped successfully!", "Runtime mod execution", User32.MB_FLAGS.MB_TOPMOST);
return;
}
Logger.Append("Couldn't stop mod runtime mod");
User32.MessageBox(IntPtr.Zero, "Couldn't stop runtime mod", "Runtime mod execution", User32.MB_FLAGS.MB_ICONERROR | User32.MB_FLAGS.MB_TOPMOST);
}

private static bool Send(string modContent, string pipeName, int TimeOut = 1000)
{
try
{
NamedPipeClientStream pipeStream = new(".", pipeName, PipeDirection.Out, PipeOptions.Asynchronous);

pipeStream.Connect(TimeOut);
Logger.Append("Pipe connection established");

using (StreamWriter writer = new StreamWriter(pipeStream))
{
writer.WriteLine(modContent.Length);
writer.Write(modContent);
}
return true;
}
catch (Exception oEX)
{
Logger.Append(oEX.Message);
}
return false;
}
}
22 changes: 20 additions & 2 deletions RedNodeEditor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Numerics;
using Vanara.PInvoke;
using IconFonts;
using System.IO.Pipes;
using System.Xml.Serialization;

namespace RedNodeEditor;

Expand Down Expand Up @@ -97,7 +99,7 @@ static void RenderUI()

ImGui.End();

ShortcutHelp.ShortcutListener();
ShortcutHelp.ShortcutListener();
}

public static void RenderMenuBar()
Expand Down Expand Up @@ -183,6 +185,22 @@ public static void RenderMenuBar()
ImGui.EndMenu();
}

if (ImGui.BeginMenu($"{FontAwesome6.FileExport} Runtime"))
{
if (ImGui.MenuItem("Execute mod", "Ctrl+E"))
{
PipeManager.ExecuteMod();
}
Drawings.NodeTooltip("Directly execute the node connections while in game.\nGame must be open and RedNodeLoader installed");

if (ImGui.MenuItem("Stop execution", "Ctrl+T"))
{
PipeManager.StopExecution();
}

ImGui.EndMenu();
}

if (ImGui.BeginMenu($"{FontAwesome6.Code} Build"))
{
if (ImGui.MenuItem("Build Mod", "Ctrl+B"))
Expand All @@ -201,7 +219,7 @@ public static void RenderMenuBar()
Drawings.NodeTooltip(ShortcutHelp.ShortcutInfo);

if (ImGui.MenuItem("Documentation"))
Process.Start(new ProcessStartInfo("https://app.gitbook.com/o/NHbK8lCm0flXIHLC9Bun/s/16eHiWrLvkdzPgx4n88e/") { UseShellExecute = true });
Process.Start(new ProcessStartInfo("https://alexs-organization-34.gitbook.io/rednodeeditor/") { UseShellExecute = true });

if (ImGui.MenuItem("SOTF Discord"))
Process.Start(new ProcessStartInfo("https://discord.gg/sotf") { UseShellExecute = true });
Expand Down
20 changes: 18 additions & 2 deletions RedNodeEditor/ProgramData.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System.Reflection;
using System.IO.Pipes;
using System.Reflection;
using Veldrid.ImageSharp;
using static Octree;
using static RedNodeEditor.SonsNode;

namespace RedNodeEditor;

public class ProgramData
{
public static readonly string AppVersion = "0.1.0";
public static readonly string AppVersion = "0.2.0";
public static readonly string ProjectExtension = ".rproj";
public static readonly string ModExtension = ".rmod";
public static string ExeFolder = "";
Expand Down Expand Up @@ -39,10 +41,22 @@ public static void Initialize()
.Where(t => typeof(SonsNode).IsAssignableFrom(t) && t.IsClass && t.IsSubclassOf(typeof(SonsNode)))
.ToList();

#if DEBUG
if (File.Exists(Path.Combine(ProjectsFolder, "Nodes.txt")))
{
File.Delete(Path.Combine(ProjectsFolder, "Nodes.txt"));
}
#endif

Logger.Append("Loading nodes...");
foreach (Type nodeType in nodeTypes)
{
SonsNode node = Activator.CreateInstance(nodeType) as SonsNode;

#if DEBUG
File.AppendAllText(Path.Combine(ProjectsFolder, "Nodes.txt"), node.Name + "\n");
#endif

if (node != null)
{
if (node.NodeType == NodeTypes.Variable)
Expand All @@ -61,5 +75,7 @@ public static void Initialize()
Logger.Append($"All {nodeTypes.Count} nodes loaded!\n");

ProjectData.CreateNewProject();

//Task.Run(() => PipeManager.WaitForConnection());
}
}
14 changes: 13 additions & 1 deletion RedNodeEditor/ShortcutHelp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,19 @@ public static void ShortcutListener()
ProjectData.CreateNewProject();
return;
}


if (ImGui.GetIO().KeyCtrl && ImGui.IsKeyPressed(ImGui.GetKeyIndex(ImGuiKey.E)))
{
PipeManager.ExecuteMod();
return;
}

if (ImGui.GetIO().KeyCtrl && ImGui.IsKeyPressed(ImGui.GetKeyIndex(ImGuiKey.T)))
{
PipeManager.StopExecution();
return;
}

if (ImGui.GetIO().KeyCtrl && ImGui.IsKeyPressed(ImGui.GetKeyIndex(ImGuiKey.B)))
{
ProjectDialogBuild.ShowDialog = true;
Expand Down

0 comments on commit ac242d8

Please sign in to comment.