-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubModule.cs
More file actions
153 lines (131 loc) · 5.22 KB
/
Copy pathSubModule.cs
File metadata and controls
153 lines (131 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System.IO;
using System;
using TaleWorlds.MountAndBlade;
using TaleWorlds.CampaignSystem;
using TaleWorlds.Core;
using TaleWorlds.Library;
using LLama.Common;
using LLama;
using System.Diagnostics;
using TaleWorlds.Diamond;
using TaleWorlds.Localization;
namespace Bannerlord.ChatGPT
{
public class SubModule : MBSubModuleBase
{
internal Process _server;
internal bool didIStarted;
internal LoggingSystem log;
protected override void OnGameStart(Game game, IGameStarter gameStarterObject)
{
// initialize the ChatAIbehavior
log = new LoggingSystem();
StartServer();
log.Removelog("Chat GPT mod is loaded");
if (gameStarterObject.GetType() == typeof(CampaignGameStarter))
{
base.OnCampaignStart(game, gameStarterObject);
CampaignGameStarter starter = (CampaignGameStarter)gameStarterObject;
starter.AddBehavior(new ChatAIbehavior());
}
}
private void StartServer()
{
string modelPath = "";
{
try
{
string modRelayerFolder = log.modRelayerFolder;
// string FileName = modRelayerFolder + "APIkey.txt";
string FileName = Path.Combine(modRelayerFolder, "APIkey.txt");
//Pass the file path and file name to the StreamReader constructor
StreamReader sr = new StreamReader(FileName);
//Read the line of text
modelPath = sr.ReadLine();
// close file
sr.Close();
}
catch (Exception e)
{
log.Addlog("model path is not properly read! Exception: " + e.ToString());
InformationManager.DisplayMessage(new InformationMessage(
new TextObject("{=xO4QZbQ7XE}API key is not properly read! Please Check your /Modules/Bannerlord_ChatGPT/APIkey.txt").ToString()));
}
}
try
{
if (modelPath != null && modelPath.Substring(0, 3) == "sk-" && modelPath.Length == 51)
{
return;
}
// string moduleFullPath = ModuleHelper.GetModuleFullPath("Inworld");
ProcessStartInfo startInfo = new ProcessStartInfo
{
WorkingDirectory = log.modRelayerFolder,
FileName = log.modRelayerFolder + "LLamaWebsocket.exe",
WindowStyle = ProcessWindowStyle.Minimized,
Arguments = modelPath,
};
didIStarted = true;
_server = Process.Start(startInfo);
_server.PriorityClass = ProcessPriorityClass.High;
}
catch(Exception ex)
{
log.Addlog("LLamaWebsocket.exe didn't start!" + ex.ToString());
}
}
protected override void OnSubModuleLoad()
{
base.OnSubModuleLoad();
InformationManager.DisplayMessage(new InformationMessage(
"Bannerlord.ChatGPT has been loaded".ToString()));
}
public override void OnMissionBehaviorInitialize(Mission mission)
{
base.OnMissionBehaviorInitialize(mission);
mission.AddMissionBehavior(new NPCchatMissionConversationView());
}
protected override void OnSubModuleUnloaded()
{
base.OnSubModuleUnloaded();
_server.Close();
}
}
class LoggingSystem
{
public string modRelayerFolder;
public LoggingSystem()
{
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
string parentFolder = Directory.GetParent(path: Directory.GetParent(path: Directory.GetParent(path).FullName).FullName).FullName;
modRelayerFolder = System.IO.Path.Combine(parentFolder, "ModRelayer\\");
using (StreamWriter outputFile = new StreamWriter(Path.Combine(modRelayerFolder, "log.txt")))
{
DateTime currentTime = DateTime.Now;
Console.WriteLine("Time: " + currentTime);
outputFile.WriteLine("Chat GPT mod is loaded");
}
}
public void Addlog(string Str)
{
Str = Str + System.DateTime.Now.ToString();
// Write the string array to a new file named "WriteLines.txt".
using (StreamWriter outputFile = new StreamWriter(Path.Combine(modRelayerFolder, "log.txt"),true))
{
DateTime currentTime = DateTime.Now;
outputFile.WriteLine("Time: " + currentTime);
outputFile.WriteLine(Str);
}
}
public void Removelog(string Str)
{
using (StreamWriter outputFile = new StreamWriter(Path.Combine(modRelayerFolder, "log.txt")))
{
DateTime currentTime = DateTime.Now;
outputFile.WriteLine("Time: " + currentTime);
outputFile.WriteLine(Str);
}
}
}
}