Skip to content

Commit fa7d2c4

Browse files
committed
Added vs project
1 parent fe7f585 commit fa7d2c4

17 files changed

+1906
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using IVSDKDotNet;
2+
3+
namespace SCOScriptCodingHelper.Classes
4+
{
5+
internal class Logging
6+
{
7+
8+
#if DEBUG
9+
public static bool EnableLogging = true;
10+
#else
11+
public static bool EnableLogging = false;
12+
#endif
13+
14+
15+
public static void Log(string str, params object[] args)
16+
{
17+
if (!EnableLogging)
18+
return;
19+
20+
IVGame.Console.Print(string.Format("[SCOScriptCodingHelper] {0}", string.Format(str, args)));
21+
}
22+
public static void LogWarning(string str, params object[] args)
23+
{
24+
if (!EnableLogging)
25+
return;
26+
27+
IVGame.Console.PrintWarning(string.Format("[SCOScriptCodingHelper] {0}", string.Format(str, args)));
28+
}
29+
public static void LogError(string str, params object[] args)
30+
{
31+
if (!EnableLogging)
32+
return;
33+
34+
IVGame.Console.PrintError(string.Format("[SCOScriptCodingHelper] {0}", string.Format(str, args)));
35+
}
36+
37+
public static void LogDebug(string str, params object[] args)
38+
{
39+
#if DEBUG
40+
IVGame.Console.Print(string.Format("[SCOScriptCodingHelper] [DEBUG] {0}", string.Format(str, args)));
41+
#endif
42+
}
43+
44+
}
45+
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
using SCOScriptCodingHelper.Classes.Widgets;
6+
7+
namespace SCOScriptCodingHelper.Classes
8+
{
9+
public class ScriptDebug
10+
{
11+
12+
#region Variables
13+
public uint ThreadID;
14+
public string ScriptName;
15+
public bool IsDebuggingEnabled;
16+
17+
// Widgets
18+
public List<WidgetGroup> WidgetGroups;
19+
public WidgetGroup CurrentWidgetGroup;
20+
public List<string> ActiveWidgetGroupsList;
21+
public byte ActiveWidgetGroups;
22+
23+
// File
24+
public ScriptDebugFile DebugFile;
25+
#endregion
26+
27+
#region Constructor
28+
public ScriptDebug(uint threadId, string scriptName)
29+
{
30+
ThreadID = threadId;
31+
ScriptName = scriptName;
32+
33+
WidgetGroups = new List<WidgetGroup>();
34+
ActiveWidgetGroupsList = new List<string>();
35+
}
36+
#endregion
37+
38+
#region Functions
39+
/// <summary>
40+
/// Goes through all added widget groups and tries to find the widget with the given <paramref name="id"/>.
41+
/// </summary>
42+
public WidgetBase FindWidgetByID(int id)
43+
{
44+
return WidgetGroups.Where(x => x.GetWidget(id) != null).Select(x => x.GetWidget(id)).FirstOrDefault();
45+
}
46+
47+
public WidgetGroup GetCurrentWidgetGroup()
48+
{
49+
if (CurrentWidgetGroup == null)
50+
return null;
51+
52+
if (ActiveWidgetGroups > 1)
53+
return CurrentWidgetGroup.GetChild(ActiveWidgetGroupsList[ActiveWidgetGroupsList.Count - 1]);
54+
55+
return CurrentWidgetGroup;
56+
}
57+
public WidgetGroup GetWidgetGroup(string name)
58+
{
59+
return WidgetGroups.Where(x => x.Name == name).FirstOrDefault();
60+
}
61+
public WidgetGroup GetWidgetGroup(int id)
62+
{
63+
return WidgetGroups.Where(x => x.ID == id).FirstOrDefault();
64+
}
65+
66+
public bool DoesWidgetGroupExist(string name)
67+
{
68+
return GetWidgetGroup(name) != null;
69+
}
70+
public bool DoesWidgetGroupExist(int id)
71+
{
72+
return GetWidgetGroup(id) != null;
73+
}
74+
75+
public WidgetGroup CreateWidgetGroup(string name)
76+
{
77+
if (DoesWidgetGroupExist(name))
78+
return null;
79+
80+
// Create new widget group
81+
WidgetGroup widgetGroup = new WidgetGroup(name, ScriptName, ThreadID);
82+
83+
// Check if there is a current widget group
84+
if (CurrentWidgetGroup != null)
85+
{
86+
widgetGroup.IsChildren = true;
87+
88+
// Check where to add the child widget group
89+
WidgetGroup childWidgetGroup = CurrentWidgetGroup.GetChild(ActiveWidgetGroupsList[ActiveWidgetGroupsList.Count - 1]);
90+
91+
if (childWidgetGroup != null)
92+
{
93+
// Add to another child widget group
94+
childWidgetGroup.Children.Add(widgetGroup);
95+
}
96+
else
97+
{
98+
// Add widget group to current widget group
99+
CurrentWidgetGroup.Children.Add(widgetGroup);
100+
}
101+
}
102+
else
103+
{
104+
// Add widget group to global widget groups for this script
105+
WidgetGroups.Add(widgetGroup);
106+
}
107+
108+
// Store the name of this widget group
109+
ActiveWidgetGroupsList.Add(name);
110+
111+
// Set current widget group
112+
if (ActiveWidgetGroups == 0)
113+
CurrentWidgetGroup = widgetGroup;
114+
115+
// Increase global active widget groups counter
116+
ActiveWidgetGroups++;
117+
118+
return widgetGroup;
119+
}
120+
public bool DeleteWidgetGroup(int id)
121+
{
122+
WidgetGroup widgetGroup = GetWidgetGroup(id);
123+
124+
if (widgetGroup == null)
125+
return false;
126+
127+
return WidgetGroups.Remove(widgetGroup);
128+
}
129+
130+
public bool HasDebugFile()
131+
{
132+
return DebugFile != null;
133+
}
134+
public ScriptDebugFile CreateDebugFile()
135+
{
136+
if (HasDebugFile())
137+
return null;
138+
139+
DebugFile = new ScriptDebugFile(ScriptName);
140+
141+
if (!DebugFile.Open(string.Format("{0}\\Logs\\{1}_dbg.log", Main.Instance.PluginResourceFolder, ScriptName)))
142+
Utils.LogToDebugger("Failed to open script debug file for '{0}' ({1})!", ScriptName, ThreadID);
143+
144+
return DebugFile;
145+
}
146+
public void CloseDebugFile()
147+
{
148+
if (!HasDebugFile())
149+
return;
150+
151+
DebugFile.Close();
152+
DebugFile = null;
153+
}
154+
#endregion
155+
156+
#region Methods
157+
public void Cleanup()
158+
{
159+
IsDebuggingEnabled = false;
160+
161+
// Close debug file
162+
CloseDebugFile();
163+
164+
// Clear added widget groups
165+
WidgetGroups.ForEach(x => x.Cleanup());
166+
WidgetGroups.Clear();
167+
WidgetGroups = null;
168+
}
169+
170+
public void EndCurrentWidgetGroup()
171+
{
172+
if (ActiveWidgetGroups == 0)
173+
return;
174+
175+
ActiveWidgetGroups--;
176+
ActiveWidgetGroupsList.RemoveAt(ActiveWidgetGroupsList.Count - 1);
177+
178+
if (ActiveWidgetGroups == 0)
179+
{
180+
CurrentWidgetGroup = null;
181+
ActiveWidgetGroupsList.Clear();
182+
}
183+
}
184+
#endregion
185+
186+
}
187+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace SCOScriptCodingHelper.Classes
5+
{
6+
public class ScriptDebugFile
7+
{
8+
9+
#region Variables
10+
private string targetScriptName;
11+
private StreamWriter streamWriter;
12+
#endregion
13+
14+
#region Constructor
15+
public ScriptDebugFile(string scriptName)
16+
{
17+
targetScriptName = scriptName;
18+
}
19+
#endregion
20+
21+
public bool Open(string fileName)
22+
{
23+
if (streamWriter != null)
24+
return false;
25+
26+
try
27+
{
28+
streamWriter = File.AppendText(fileName);
29+
return true;
30+
}
31+
catch (Exception ex)
32+
{
33+
Logging.LogError("Failed to open script debug file for '{0}'! Details: {1}", targetScriptName, ex);
34+
}
35+
36+
return false;
37+
}
38+
public void Close()
39+
{
40+
if (streamWriter == null)
41+
return;
42+
43+
streamWriter.Dispose();
44+
streamWriter = null;
45+
}
46+
47+
public void Write(int value)
48+
{
49+
if (streamWriter == null)
50+
return;
51+
52+
streamWriter.Write(value);
53+
}
54+
public void Write(float value)
55+
{
56+
if (streamWriter == null)
57+
return;
58+
59+
streamWriter.Write(value);
60+
}
61+
public void Write(string value)
62+
{
63+
if (streamWriter == null)
64+
return;
65+
66+
streamWriter.Write(value);
67+
}
68+
69+
}
70+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Text;
4+
5+
namespace SCOScriptCodingHelper.Classes
6+
{
7+
internal class Utils
8+
{
9+
10+
public static void LogToDebugger(string message, params object[] args)
11+
{
12+
#if DEBUG
13+
Debugger.Log(0, "SCOScriptCodingHelper", string.Format(message, args) + "\n");
14+
#endif
15+
}
16+
17+
public static unsafe void WriteStringToAddress(IntPtr targetAddress, string value)
18+
{
19+
byte[] bytes = Encoding.ASCII.GetBytes(value + "\0"); // Convert to ASCII, add null terminator
20+
fixed (byte* ptr = bytes)
21+
{
22+
Buffer.MemoryCopy(ptr, (void*)targetAddress, bytes.Length, bytes.Length);
23+
}
24+
}
25+
26+
}
27+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace SCOScriptCodingHelper.Classes.Widgets
2+
{
3+
public abstract class WidgetBase
4+
{
5+
6+
#region Variables
7+
public int ID;
8+
public string Name;
9+
public bool ReadOnly;
10+
public WidgetType Type;
11+
#endregion
12+
13+
#region Constructor
14+
public WidgetBase(WidgetType type, string name)
15+
{
16+
ID = IVSDKDotNet.Native.Natives.GENERATE_RANDOM_INT();
17+
Name = name;
18+
Type = type;
19+
}
20+
public WidgetBase(WidgetType type)
21+
{
22+
ID = IVSDKDotNet.Native.Natives.GENERATE_RANDOM_INT();
23+
Type = type;
24+
}
25+
#endregion
26+
27+
public virtual void Cleanup() { }
28+
public abstract void Draw();
29+
30+
}
31+
}

0 commit comments

Comments
 (0)